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
- 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:
Process address space before and after sbrk(increment). The heap sits between the bss segment and the stack; brk/sbrk move the program break upward to hand the heap more memory from the OS, while the stack grows downward from the top of the address space. This HeapManager requests one such region up front and then serves allocations from it itself.
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.
The HeapNode linked list, block by block, after every displayState() call in App/main.cpp. Each box is one node: the dark strip is the 32-byte header (size/prev/next/free), the coloured strip is the data region labelled with its byte size — orange for allocated (free=0), green for free (free=1). It traces the whole story: the initial allocations, a block getting split by malloc, adjacent free blocks getting coalesced by free, and the heap finally merging back into a single free block.
- 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.