This memory allocator is akin to ptmalloc2, jemalloc, tcmalloc and many others. These allocators are the underlying code of malloc.
Heap allocators request chunks of memory from the operating system and place several (small) object inside these. Using the free call these memory objects can be freed up again, allowing for reuse by future malloc calls. Important performance considerations of a heap allocator not only include being fast but also to reduce memory fragmentation.
malloc, free and realloc have been implemented without using the standard library versions of these functions. brk(2) and sbrk(2) has been used for asking the kernel for more heap space.
(Normal heap allocators may also use mmap to request memory from the kernel.)
malloc,freeandreallocbehave exactly as specified by their man-page description, whilst the Notes sections have been skipped as these are implementation-specific.- This allocator does not place any restrictions on the maximum amount of memory supported or the maximum number of objects allocated. For example, it will scale regardless of whether the maximum
brksize is 64KB or 1TB. - A region of memory can be reused after freeing it with
free. reallocbehaves as described on its man-page and only allocates a new object when needed.- The
allocatorbatchesbrkcalls, i.e., it does not need to request memory from the kernel for every allocation. - The amortized overhead per allocation is on average 8 bytes or less.
- The allocator tries to optimize for locality (reuse recently freed memory).
- The allocator gives back memory to the kernel (using
brk) when a large portion of the allocated memory has been freed up. - The allocation functions work correctly without the
myprefix.
The file test/tests.c contains a number of (automated) test cases that evaluate the different aspects of your allocator. It can be invoked manually via ./test <test name>. Running make check will run all test cases.
- If you want to add support for replacing the system allocator (i.e., by adding non my prefixed functions) you can use your allocator for any existing program on your system. You can do this by prefixing any command with
LD_PRELOAD=/path/to/libmyalloc.so. For example, LD_PRELOAD=./libmyalloc.so lswill runlswith the allocator. - Naming the functions such as
mallocinstead ofmymallocnot only redirects all calls inside the code to thismalloc, but will also cause all internallibccalls to go to this allocator instead of the built-inlibcmalloc. Manylibcfunctions, such asprintf, internally make calls to malloc, and as such usingprintfinside the allocation code would cause an infinite loop. Therefore I prefix the allocator functions withmy, but you are free to remove it. - MacOS is currently not supported but you could set up a Linux environment if you do not already have one (e.g., with a VM using virtualbox).