Here is the context of my application : I'm working on an embedded system which uses RAM from different devices. One part in the internal RAM of the microcontroller (128kB) and the other part is the external RAM (1MB). These memories are mapped into the address space of the microcontroller, but in non contiguous regions.
The internal RAM is used for System stack, tasks stacks and Heap.
The external RAM is used for statically allocated data (Pools, buffers, and all "static ...
" stuff)
I am trying to implement a simple memory management structure, and as part of it be able to create an allocator which could use the allocation algorithm of operator new
but using another memory source, not the system heap but a memory region elsewhere. Do you know if this is possible ?
An example of use could be to reserve 100kB of external RAM and to create an allocator to manage it, and then give it to a specified task which need this memory.
static const uint8_t* ramBase = reinterpret_cast<uint8_t*>(0x80000000);
static const uint32_t ramAreaSize = 0x19000; //100kB
BufferAllocator allocator(ramBase, ramAreaSize);
//...
//Assuming operator new is overloaded to use BufferAllocator
MyObject * obj = new (allocator) MyObject(some, parameter);
//...
The question is : how (if this is even possible) can I implement BufferAllocator
in order to use operator new
to manage the raw memory area ?
void* BufferAllocator::allocate(uint32_t bytes)
{
//I would like to write something like this
//and so let the responsibility to manage this memory area to "new"
//so I don't have to reimplement (or reuse) a different custom
// allocator
return ::operator new(ramBase, ramAreaSize, bytes)
}