Skip to content

milutin2002/HeapMemory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 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 and benchmark.

✨ 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:

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.

🗺️ 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

No packages published