Search code examples
c++cembeddedheap-memorydynamic-memory-allocation

Create a heap at a specific memory address for dynamic memory allocation


Is there a way to create a heap at a specific memory location (separate from the system heap) that can be used for dynamic memory allocation, but makes use of the dynamic memory allocator built into the C library.

I'm looking for something functionally equivalent to this:

UserHeap heap(startAddress, size);

void* allocatedMemory1 = heap.Allocate(100);  //Allocate 100 bytes.
void* allocatedMemory2 = heap.Allocate(200);  //Allocate 200 bytes.

/* ... Do seomthing useful with the memory ... */

heap.Free(allocatedMemory1);
heap.Free(allocatedMemory2);

This uheap.h seems to have what I'm looking for, but this doesn't seem to exist in newlib. I'm wondering if GCC or newlib might have something. If not I think I may end up porting ptmalloc. However, its my understanding that this code is already in the C library and I'd hate to waste the memory reproducing it.

I'm using Sourcery Codebench Lite (GCC) with newlib (C library).


Solution

  • I finally came across a blog by Eli Bendersky that illustrates exactly what I was looking for. memmgr – a fixed-pool memory allocator