Custom heap memory manager in modern C++ — a small, educational project that explores how allocators work under the hood (free lists, block splitting/merging, alignment, and allocation strategies).
Built with CMake; C++ sources live in App/ and Manager/.
- Learn OS/internal concepts hands-on by implementing a heap on top of a raw buffer.
- Experiment with allocation and deallocation behavior.
- Provide a minimal, testable allocator you can instrument and benchmark.
- Fixed-size heap backed by a contiguous buffer (no system
mallocneeded). - Simple
allocate(size, align)/deallocate(ptr)API surface. - Block splitting when a request is smaller than a free block.
- Optional block coalescing (merge adjacent free blocks).
- Uses first-fit allocation strategy (scans from the beginning of the free list and picks the first suitable block).
HeapMemory/
├─ App/ # small demo / CLI usage examples
├─ Manager/ # heap manager sources (allocator, block metadata, etc.)
├─ Pictures/ # diagrams & notes
├─ CMakeLists.txt
└─ README.md
# from repository root
mkdir -p build && cd build
cmake ..
cmake --build .
# After building:
./App/exec
#include <cstddef> #include #include #include "../Manager/manager.h" using namespace std;
int main(int argc,char argv[]){ HeapManager m; int a=(int)m.malloc(4sizeof(int)); m.displayState(); int b=(int)m.malloc(6*sizeof(int)); m.displayState(); m.free(a); m.displayState(); m.free(b); m.displayState(); void *c=m.malloc(5); m.displayState(); void *d =m.malloc(6); m.displayState(); m.free(d); m.displayState(); void * f=m.malloc(3); m.displayState(); void *x=m.malloc(100); m.displayState(); m.free(c); m.displayState(); m.free(f); m.displayState(); void *g=m.malloc(1000); m.displayState(); m.free(g); m.displayState(); void *z =m.malloc(160); m.displayState(); void *y=m.malloc(240); m.displayState(); m.free(z); m.displayState(); m.free(x); m.displayState(); m.free(y); m.displayState(); return 0; }
The following examples show how the heap manager allocates and frees memory:
Example sequence of allocations and frees using HeapManager, showing calls to malloc, free, and displayState.
Console output of displayState, showing the state of the free list, block sizes, and fragmentation after each operation.
- Add benchmark suite
- (Future) Add more allocation strategies (best-fit, next-fit) for comparison
This repo is mostly C++ with some CMake for building.
PRs and issues are welcome — small improvements (docs, tests, diagrams) are great starts.
Choose one (MIT/Apache-2.0) and add a LICENSE file. If you already have a license, reference it here.