A small implementation of malloc, free, calloc, and realloc in C.
This project recreates core heap-management behavior by maintaining metadata blocks in a linked list and requesting memory from the operating system using sbrk().
The allocator supports multiple fit strategies, handles alignment, and tracks heap statistics to help visualize allocator behavior over time.
- Grows the heap on demand using
sbrk() - Maintains a linked list of heap blocks containing metadata:
- block size
- free/allocated status
- pointer to the next block
- 4-byte alignment for all requested allocations
- Locates a usable free block using a selectable fit strategy
- Splits free blocks when there is enough leftover space to form a valid new block
- Reuses freed blocks instead of always growing the heap
- Marks blocks as free when
free()is called - Coalesces adjacent free blocks to reduce fragmentation
- First Fit
- Best Fit
- Worst Fit
- Next Fit
Automatically prints heap statistics at program exit (registered with atexit()), including:
- number of mallocs
- number of frees
- number of reuses
- number of heap grows
- number of block splits
- number of coalesces
- number of blocks tracked
- total requested bytes
- maximum heap size reached