This project is a custom implementation of the standard C memory allocation library (malloc, calloc, realloc, and free).
It relies on the mmap (and munmap) system calls and uses a Linked-List architecture to ensure optimal allocation and deallocation performance.
- Standard Allocation: Complete implementation of
malloc,free,realloc, andcalloc. - Linked-List Architecture
- Memory Optimization:
- Auto-Cleanup: As soon as a 4096-byte page (composed of multiple smaller blocks) is entirely freed, it is returned to the OS via
munmap.
- Auto-Cleanup: As soon as a 4096-byte page (composed of multiple smaller blocks) is entirely freed, it is returned to the OS via
A Makefile is provided to easily build and manage the project.
To compile the shared library (libmalloc.so):
make libraryTo clean up the generated build and test files:
make cleanTo use this implementation instead of the standard glibc malloc, you simply need to set the LD_PRELOAD environment variable when launching your executable:
LD_PRELOAD=./libmalloc.so ./your_programThis project comes with a very extensive testsuite that relies on a functional bash script as well as unit tests written with the Criterion framework and analyzed with AddressSanitizer (ASan).
The tests cover:
- Basic allocation tests (
malloc,free). - Data integrity checks with
calloc(zero-initialization). - Memory resizing tests with
realloc(expanding and shrinking, data copy verification).
To build the library and run the entire test suite automatically, run:
make check- Currently, the implementation is potentially not thread-safe (there are no mutexes protecting the global variable
g_list_free). Adding locks (such aspthread_mutex_t) would make it safe to use in a multi-threaded context.