I have already allocated a piece of big memory by mmap
, the memory size (as MEM_SIZE
) is large, so I want to use it as a memory pool, or a heap, so that I can use the memory efficiently. For example, every time I want a piece of memory (large but not larger than MEM_SIZE
), I can just easily use a function to allocate and get a void *
, and release this pointer later, and the pool/heap can collect this memory.
An example:
// allocate memory
void* mem_ptr = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// create a heap or memory pool based on the allocated memory
// is there a function works as create_heap(...)?
auto my_heap = create_heap(mem_ptr, MEM_SIZE);
// when I need to get some memory
void* mem_for_var1 = alloc(my_heap, mem_size_for_var1);
auto var1 = new (mem_for_var1) float();
// use var1
// release
...
And if there exists any memory allocator or memory pool library for C++ that can work as above, it is appreciated if you could tell me and leave some examples or the documents of it.
hideaway_cs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.