Normally, one can use the malloc
and free
functions to allocate memory in an implementation-defined manner. However, often times one needs to manage the allocation of memory from a specific region. Examples include:
Instead of writing a one-off heap implementation each time this requirement occurs, is there a way to re-use malloc
to manage these regions (Linux)? Otherwise, can any of the "well-known" memory allocators (e.g. dmalloc, ptmalloc, etc.) support allocating from a specific region?
Example:
void *pool = mmap(/* my file */);
void *pool_manager = mallloc_init(pool, /* size */);
void *p = malloc_ex(pool, 1024);
free_ex(pool, p);
No, that does not exist. Neither malloc
nor any of its drop-in replacements can do that.
So, let us now get into why this stuff does not exist.
Inter-process shared memory is indeed shared between processes, but at different addresses in different processes, so a traditional heap manager does not work at all and specialized protocols are used to communicate between the processes that use things other than pointers (because pointers don't work cross-process because the addresses are different).
The same is true of memory mapped files. For memory-mapped files, typically the file format is still very-well-defined and nothing like a heap manager would be in it, and if there were it would be fully specified by the file format and you might find the thing in libraries for that file format, but a drop-in heap manager would never work. If you start talking about a heap manager in a memory-mapped file I think you're developing a database engine, and you're about to find out that heap management needs to be very different from in-RAM heap management because disk seek times are horrible.
I suppose you could use a heap manager for non-volatile memory for deeply embedded systems, but most of those use the far simpler technique of NVRAM is always statically allocated. My gut feeling is a general heap manager is a poor choice and a specialized manager attuned to the problem is so much better that nobody really considers a generic heap manager.
If you did find a general purpose heap library that took a memory region to use as a heap it would still work badly for you. Shared memory and memory-mapped files have to deal with the process crashing between any two instructions; thus can't use normal locking at all. There are specialized locks that are used for shared memory. The problem for memory-mapped IO is even worse and I can prove the locks cannot exist because the concurrency model is too bad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With