A custom dynamic memory allocator that implements malloc, free, realloc, and calloc using segregated explicit free lists, block splitting, coalescing, and heap consistency checking.
This repository focuses on the allocator implementation in mm.c. Testing traces, driver files, binaries, and build artifacts are intentionally excluded.
This allocator manages heap memory manually using block metadata and segregated free lists. Each block stores its size and allocation status in a header and footer, which allows the allocator to move between neighboring blocks and coalesce free space.
[ Header ][ Payload ][ Footer ]
Free blocks reuse their payload area to store free list pointers.
[ Header ][ Prev Free Ptr ][ Next Free Ptr ][ Free Space ][ Footer ]
Allocation requests are adjusted for metadata and 16-byte alignment. The allocator searches the appropriate size class for a fitting block, splits larger blocks when useful, coalesces adjacent free blocks when memory is released, and extends the heap when no fit is available.
The heap also uses prologue and epilogue blocks to simplify boundary cases during coalescing.
- Segregated explicit free lists organize free blocks by size class
- Best fit search is used within size classes
- Larger free blocks are split when the remainder can form a valid block
- Adjacent free blocks are coalesced to reduce fragmentation
- The heap extends when no existing free block can satisfy a request
- A debug heap checker validates allocator consistency
The allocator was tested using a local benchmarking and validation harness.
| Metric | Result |
|---|---|
| Correctness | Passed local validation tests |
| Average utilization | 60.6% |
| Average throughput | 6898 Kops/sec |
mm.c Allocator implementation
mm.h Allocator interface
README.md Project documentation
.gitignore Excluded files and build artifacts
Initializes the allocator, clears the free lists, creates the initial heap structure, and extends the heap with the first free block.
Allocates a block of at least size bytes after adjusting the request for metadata and alignment.
Releases a block, coalesces it with adjacent free blocks when possible, and returns it to the proper free list.
Resizes an allocation while preserving existing payload data. The allocator tries to reuse or expand the current block before allocating a new one.
Allocates space for an array and initializes the payload to zero. Includes an overflow check before computing the total size.
Checks heap and free list consistency during debugging. It validates alignment, heap bounds, header/footer agreement, coalescing, free list links, size class placement, and free block counts.
Requests more heap space, creates a new free block, writes a new epilogue, coalesces if possible, and inserts the block into a free list.
Merges a free block with adjacent free blocks when possible.
Searches the segregated free lists for a suitable block.
Places an allocation inside a selected free block and splits the remainder if it can form a valid free block.
Writes the header and footer for a block.
Maps a block size to the correct segregated free list class.
Inserts a free block into its size class free list.
Removes a free block from its size class free list.
Read the next and previous free block pointers stored in a free block’s payload.
Write the next and previous free block pointers inside a free block’s payload.
These helpers keep pointer arithmetic and metadata access consistent throughout the allocator.
| Helper | Purpose |
|---|---|
align(size) |
Rounds a size up to the required alignment |
max_size(a, b) |
Returns the larger of two sizes |
pack(size, allocate) |
Combines block size and allocation bit |
get_word(address) |
Reads one word from memory |
put_word(address, value) |
Writes one word to memory |
get_size(address) |
Extracts block size from a header/footer |
get_alloc(address) |
Extracts allocation status from a header/footer |
hdrp(blockp) |
Returns a block’s header address |
ftrp(blockp) |
Returns a block’s footer address |
next_blkp(blockp) |
Returns the next physical heap block |
prev_blkp(blockp) |
Returns the previous physical heap block |
aligned(p) |
Checks whether a pointer is properly aligned |
in_heap(p) |
Checks whether a pointer is inside the heap |
- Dynamic memory allocation
- Explicit and segregated free lists
- Block splitting and coalescing
- Heap metadata design
- Pointer arithmetic
- Alignment
- Fragmentation and throughput tradeoffs
- Heap consistency checking