Low-Level Memory
Thread Safe
Malloc is widely considered one of the most challenging system projects. The goal is to rewrite the standard C library memory management functions
(malloc, free, calloc, realloc) to create a shared library capable of replacing the system's default allocator.
Grade achieved: 97% (Near-perfect implementation including advanced optimizations).
This project required a mastery of pointer arithmetic, memory alignment, and system calls. Instead of using sbrk (deprecated),
I utilized mmap to request pages directly from the kernel.
Memory is managed via a linked list of metadata headers embedded directly within the allocated pages. I implemented block splitting and coalescing (merging adjacent free blocks) to minimize fragmentation and reduce memory footprint.
| Feature | Implementation Detail |
|---|---|
| Allocation Strategy | Implementation of "Best-Fit" or "First-Fit" algorithms to speed up block finding while keeping memory usage low. |
| Realloc Optimization | Smart realloc that extends the current block if space allows, avoiding expensive memory copies (`memcpy`) when possible. |
| Alignment | Strict memory alignment (on long double boundaries) to prevent bus errors and ensure CPU efficiency. |
| Robustness | Ability to run complex real-world software (like ls, vim, or gcc) by preloading the library (LD_PRELOAD). |
This project was a definitive exercise in system optimization. achieving 97% required not just code that works, but code that is fast, efficient, and proofed against concurrency issues.