Quite simply, how can I tell how much free memory I have in a memory pool? I've created a memory pool, initialized it with init_mempool and use malloc & free finctions for memory management. Is there a simple way to find out how much memory is still available to be allocated? I have a fairly dynamic application and I would like to insert some degugging triggers if memory gets low. I thought of simply using malloc to request a block of say 200 bytes and then free it again. Could do this once a second and generate a low memory error if it fails. Seems a bit clunky, is there an easier way that I have missed?? Thanks, Bryan Green.
Most of the memory is allocated at startup, while small amounts are malloc/free while app is running. Do you know how much memory you have to allocate at startup (you should)? And how much is "small amount"? Maybe could place the startup data in some appropriate large buffer (static memory). I've done so and called this "non returnable heap":
static uint8_t nrheap[NRHEAP_SIZE]; static uint8_t *nrheap_ptr = nrheap; // public uint16_t nrheap_requested = 0; uint16_t nrheap_num_calls = 0; uint8_t* nrheap_alloc( uint16_t size) { uint8_t *ptr = nrheap_ptr; nrheap_num_calls++; nrheap_requested += size; if (size > nrheap_free) { return (NULL); } nrheap_ptr += size; nrheap_free -= size; return ptr; }