Is it possible to alloc some memory only if the free memory is larger than an amount?

FreeRTOS community,
I have a dynamic memory allocation need for some unimportant data. That is, dropping some of them is just fine when the memory is too little. I hope to reserve some memory for networking, so I hope not to use all memory out.
So I need to alloc the memory when the remaining memory is above some level. I see a race condition if I use vPortGetHeapStats, and calling the API each time I alloc some memory seems so large a overhead. Is there a better way for my purpose?

Use this func-“xPortGetFreeHeapSize”

With any API called before the actual alloc, there will be a race condition. That is, the memory size you got may be out-of-state when the alloc take place. I am thinking about a seperate memory pool for my purpose. Do you know if there is already some support for that?

The race can’t be that important as it is very fundamental, because you will always have the problem that right after you allocate the block, something else takes the memory.

Unless you build a special allocator where you can register a callback that is called when memory is exhausted that has the ability to release the memory (and you make sure the usage of that memory is safe for this, always inside a protected execution block) this will be an issue.

Write your own allocator that pre-allocates memory and provides malloc/free functions to other tasks.

Yes. That is what I am going to do.