Skip to content

milutin2002/HeapMemory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HeapMemory

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/.


🔥 Why

  • 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

✨ Features

  • Fixed-size heap backed by a contiguous buffer (no system malloc needed).
  • 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).

📦 Project layout

HeapMemory/
├─ App/          # small demo / CLI usage examples
├─ Manager/      # heap manager sources (allocator, block metadata, etc.)
├─ Pictures/     # diagrams & notes
├─ CMakeLists.txt
└─ README.md

🚀 Quickstart

Build

# from repository root
mkdir -p build && cd build
cmake ..
cmake --build .

Run the demo

# After building:
./App/exec

🧭 Usage (example)

#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; }

🖼️ Diagrams & Visuals

The following examples show how the heap manager allocates and frees memory:

Process memory layout showing text, data, bss, heap, and stack segments before and after a sbrk call
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.

Heap Allocation Example
Example sequence of allocations and frees using HeapManager, showing calls to malloc, free, and displayState.

Heap State Output
Console output of displayState, showing the state of the free list, block sizes, and fragmentation after each operation.

HeapNode block list state after each line of App/main.cpp
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.

🗺️ Roadmap

  • Add benchmark suite
  • (Future) Add more allocation strategies (best-fit, next-fit) for comparison

📚 Learn more

This repo is mostly C++ with some CMake for building.

🤝 Contributing

PRs and issues are welcome — small improvements (docs, tests, diagrams) are great starts.

🧾 License

Choose one (MIT/Apache-2.0) and add a LICENSE file. If you already have a license, reference it here.

Releases

No releases published

Packages

 
 
 

Contributors