My application is mostly organised in layers so I found that something like the APR memory pools would be the best way.
While reading on SO about C++ placement new posts here & here, and a more generic C allocation question I was thinking about hand-crafting a hierarchical pool allocator as suggested in one post, but in the pure NYI tradition I’m first asking if something like this already exists.
It could also have the nice property of being able to give back unused memory to the OS (since allocation could be done with mmap(MAP_ANON)) or could be allocating from the stack as suggested Ferrucico here.
I know of another good hierarchical memory allocator, but it calls
mallocunderneath the covers.That being said, Glibc’s
mallocalready usesmmap(MAP_ANON)for allocation larger thanmmap_threshold, which you can set viamallopt(M_MMAP_THRESHOLD, bytes). By default it is dynamically adjusted betweenWatch out if you lower it; by default no more than
#define DEFAULT_MMAP_MAX 65536pieces will be allocated usingmmap. This can be changed withmallopt(M_MMAP_MAX, count), but using manymmaps has an overhead.The environment variables
MALLOC_MMAP_THRESHOLD_etc. will also set these options.Obviously, memory that
mallocallocates withmmapis freed withmunmap. I’m not sure if any of this is documented anywhere outside of Glibc’s source code, or has any compatibility guarantees.