小编Ino*_*unx的帖子

是否可以使用operator new来从堆以外的地方分配?

以下是我的应用程序的上下文:我正在使用嵌入式系统,该系统使用来自不同设备的RAM.微控制器内部RAM(128kB)中的一部分是外部RAM(1MB).这些存储器映射到微控制器的地址空间,但是在非连续区域中.

内部RAM用于系统堆栈,任务堆栈和堆.外部RAM用于静态分配的数据(池,缓冲区和所有" static ..."内容)

我正在尝试实现一个简单的内存管理结构,并且作为其中的一部分能够创建一个分配器,它可以使用分配算法,operator new但使用另一个内存源,而不是系统堆,而是其他地方的内存区域.你知道这是否可行?

使用的一个示例可以是保留100kB的外部RAM并创建一个分配器来管理它,然后将其分配给需要该内存的指定任务.

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);
//...
Run Code Online (Sandbox Code Playgroud)

问题是:如何(如果这是可能的话)我可以实现BufferAllocator以便operator new用来管理原始内存区域?

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 …
Run Code Online (Sandbox Code Playgroud)

c++ embedded memory-management

5
推荐指数
1
解决办法
606
查看次数

标签 统计

c++ ×1

embedded ×1

memory-management ×1