Custom memory allocator demonstrating low-level systems programming in C. Features manual heap management, pointer manipulation, and efficient memory allocation strategies. Built with CMake and includes comprehensive performance analysis.
- Manual Heap Management: Custom
malloc(),free(), andrealloc()implementations - Memory Coalescing: Automatic merging of adjacent free blocks
- Allocation Strategies: First-fit, best-fit, and worst-fit algorithms
- Pointer Arithmetic: Low-level memory address manipulation
- Fragmentation Analysis: Tracking internal and external fragmentation
- Performance Benchmarks: Comparison with stdlib allocator
memory-manager-c/
├── src/
│ ├── memory_manager.c # Core allocator implementation
│ ├── memory_manager.h # Public API
│ └── main.c # Demo and testing
├── tests/
│ ├── test_allocator.c # Unit tests
│ └── benchmark.c # Performance tests
├── docs/
│ ├── architecture.md # Design documentation
│ └── performance.md # Benchmark results
├── CMakeLists.txt
├── .gitignore
└── README.md
- GCC or Clang compiler
- CMake 3.10+
- Make
# Clone the repository
git clone https://github.com/masoud-rafiee/memory-manager-c.git
cd memory-manager-c
# Create build directory
mkdir build && cd build
# Configure and build
cmake ..
make
# Run the demo
./memory_manager_demo
# Run tests
./run_teststypedef struct Block {
size_t size; // Block size (excluding header)
int is_free; // Allocation status
struct Block *next; // Next block in list
} Block;// Allocate memory
void* mm_malloc(size_t size);
// Free allocated memory
void mm_free(void* ptr);
// Reallocate memory
void* mm_realloc(void* ptr, size_t new_size);
// Display memory statistics
void mm_stats();-
First-Fit: Allocates first block large enough
- Fast allocation: O(n) worst case
- Higher fragmentation
-
Best-Fit: Allocates smallest sufficient block
- Minimal wasted space
- Slower search: O(n)
-
Worst-Fit: Allocates largest available block
- Reduces small fragments
- Slower allocation
| Operation | Custom Allocator | stdlib malloc | Difference |
|---|---|---|---|
| malloc (1KB) | 145 ns | 98 ns | 1.48x slower |
| free | 52 ns | 45 ns | 1.16x slower |
| realloc | 312 ns | 256 ns | 1.22x slower |
- Block header: 24 bytes per allocation
- Alignment: 8-byte boundary alignment
- Metadata ratio: ~2-5% for typical workloads
# Run all tests
./run_tests
# Run with valgrind
valgrind --leak-check=full ./run_tests
# Run benchmarks
./benchmark --iterations=10000- Basic allocation and deallocation
- Edge cases (zero-size, NULL pointers)
- Coalescing verification
- Fragmentation analysis
- Stress testing (rapid alloc/free cycles)
- Heap Management: Understanding how dynamic memory works
- Pointer Arithmetic: Safe manipulation of memory addresses
- Memory Alignment: Proper boundary alignment for CPU efficiency
- Fragmentation: Internal vs external fragmentation trade-offs
- Coalescing: Merging adjacent free blocks
- Metadata Management: Efficient header design
- C11: Modern C standard
- CMake: Cross-platform build system
- Valgrind: Memory leak detection
- GDB: Debugging
- perf: Performance profiling
- Computer Systems: A Programmer's Perspective (Bryant & O'Hallaron)
- The C Programming Language (Kernighan & Ritchie)
- GNU C Library documentation
# Debug build with sanitizers
cmake -DCMAKE_BUILD_TYPE=Debug \
-DENABLE_ASAN=ON \
-DENABLE_UBSAN=ON ..
# Release build with optimizations
cmake -DCMAKE_BUILD_TYPE=Release ..This project is licensed under the MIT License.
Masoud Rafiee
- GitHub: @masoud-rafiee
- LinkedIn: masoud-rafiee
- CS409 - Operating Systems
- Bishop's University
- Operating Systems: Three Easy Pieces
This is an educational implementation. For production use cases, rely on battle-tested allocators like:
- glibc malloc
- jemalloc
- tcmalloc
- mimalloc
Exploring the fundamentals of memory management 🛠️