This is a dynamic memory allocation library for C17, designed to mimic the behavior of malloc in libc.
The operating system provides mechanisms to allocate memory from the heap using system calls. While C offers straightforward functions that wrap these system calls, they come with both advantages and disadvantages.
sbrk:
- Allocates memory in a contiguous manner, making arbitrary deallocation difficult and leading to inefficient long-term memory usage and fragmentation.
- Has a complex usage pattern.
- System calls introduce overhead, making them "slow".
mmap:
- Uses a page-based allocation strategy, making it inefficient for small memory allocations.
- System calls introduce overhead, making them "slow".
Our goal is to enhance performance by reducing the amount of system calls, albeit at the cost of memory pre-allocation, memory fragmentation. In other words: additional overhead.
Ideally fragmentation is kept minial.
- Fewer system calls through pre-allocation, enabling more efficient memory distribution via pointer arithmetic.
- Reduced fragmentation through optimized memory management techniques.
- Simplified memory management, making allocation easier to handle.
- Increased memory usage due to additional metadata for tracking and managing allocations.
- Extra memory overhead from pre-allocation.
With our malloc implementation, we aim to optimize memory allocation while balancing performance and efficiency.
In this schema:
- The term header can be used interchangeably with metadata.
- Marena is the only element defined on the stack. Each thread has it's own instance as it is set as ThreadLocal.
- From left to right, the bigger element includes the smaller ones.
Is stored on the stack:
- The marena header (thus the pointers to the mregion heads).
Is stored on the heap (via mmap):
- Each mregion catalogued through their headers.
I was not satisfied by my original libft. I decided to recreate one that answers my needs while using a more modern approach, now that I know C better.
If you want to use this malloc implementation instead of the libc's one inside existing programs you have to:
- Prepend the path where
libft_malloc.sois located inLD_LIBRARY_PATHenvironment variable. Ensure our *.so file is read first and has precedence.
Please see how it's been done in mytester'sMakefile. - Ensure the existing program only uses functions that are present in both the header file of libc's malloc and ours.
