Skip to content
C project #20Advanced

Memory Allocation Simulator

Simulate malloc/free on a heap arena: block splitting, coalescing, first/best/worst-fit placement and fragmentation statistics.

Estimated time
~10 hours to build
Files
7 files · 480 lines
Technologies
C11, Doubly linked lists

Introduction

Every call to malloc asks an allocator to find space in the heap. This simulator makes that process visible: you allocate and free blocks and watch the heap map change, then compare placement strategies on the same workload.

Problem statement

Model a fixed-size heap as a doubly linked list of blocks. Support allocation with 8-byte alignment using first-fit, best-fit or worst-fit, splitting oversized blocks; freeing with coalescing of adjacent free blocks; a visual map; statistics including external fragmentation; and a comparison of the three strategies on a standard workload.

Objectives

  • Implement the data structures behind a real allocator
  • Split and coalesce blocks correctly
  • Measure external fragmentation
  • Compare placement policies experimentally

Features

  • first / best / worst-fit strategies
  • Alignment to 8 bytes
  • Coalescing with both neighbours
  • ASCII heap map and block list
  • Fragmentation statistics and a strategy comparison

Concepts demonstrated

  • Heap management
  • Fragmentation
  • Placement algorithms
  • Doubly linked lists

System architecture

Each Block records its offset, size, used flag and allocation ID, linked in address order. heap_alloc chooses a block by strategy, splits off the remainder as a new free block, and marks it used. heap_free marks a block free and merges it with a free successor and predecessor. Fragmentation = 1 − largest free block / total free.

TextArchitecture diagram
[#1 104B][free 256B][#4 304B][free 360B]
free #4 ─> merge with right neighbour ─> [#1][free 256B][free 664B → merged]

Folder structure

TextProject tree
memory-allocator-simulator/
├── README.md
├── Makefile
├── src/allocator.c
├── src/allocator.h
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/allocator.h
Block, Heap, strategies and the API.
src/allocator.c
Choosing, splitting, coalescing, the heap map and statistics.
src/main.c
Command interpreter and the strategy comparison workload.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Simulated heap: 1024 bytes, 8-byte alignment, first-fit. Type 'help'.
heap> alloc 100
allocated #1 (100 bytes requested)
heap> alloc 200
allocated #2 (200 bytes requested)
heap> alloc 50
allocated #3 (50 bytes requested)
heap> alloc 300
allocated #4 (300 bytes requested)
heap> map
[11111122222222222223334444444444444444444.......................]
0- 103 104 bytes #1
104- 303 200 bytes #2
304- 359 56 bytes #3
360- 663 304 bytes #4
664- 1023 360 bytes free
heap> free 2
freed #2
heap> free 3
freed #3
heap> map
[111111................4444444444444444444.......................]
0- 103 104 bytes #1
104- 359 256 bytes free
360- 663 304 bytes #4
664- 1023 360 bytes free
heap> alloc 120
allocated #5 (120 bytes requested)
heap> alloc 500
allocation of 500 bytes FAILED (no free block large enough)
heap> stats
strategy first-fit | used 528 / 1024 bytes (51.6%) in 3 block(s)
free 496 bytes in 2 block(s), largest 360 | external fragmentation 27.4%
allocations 5, frees 2, failed requests 1
heap> free 1
freed #1
heap> free 4
freed #4
heap> map
[......55555555..................................................]
0- 103 104 bytes free
104- 223 120 bytes #5
224- 1023 800 bytes free
heap> init 512 best
heap reset: 512 bytes, best-fit
heap> alloc 64
allocated #1 (64 bytes requested)
heap> alloc 128
allocated #2 (128 bytes requested)
heap> alloc 64
allocated #3 (64 bytes requested)
heap> free 2
freed #2
heap> alloc 40
allocated #4 (40 bytes requested)
heap> map
[1111111144444...........33333333................................]
0- 63 64 bytes #1
64- 103 40 bytes #4
104- 191 88 bytes free
192- 255 64 bytes #3
256- 511 256 bytes free
heap> compare
strategy failures largest free frag %
first-fit 0 40 61.5
best-fit 0 88 15.4
worst-fit 0 64 38.5
heap> quit
bye
Input
alloc 100
alloc 200
alloc 50
alloc 300
map
free 2
free 3
map
alloc 120
alloc 500
stats
free 1
free 4
map
init 512 best
alloc 64
alloc 128
alloc 64
free 2
alloc 40
map
compare
quit

Testing

  • Diff with tests/expected-output.txt.
  • Free every block and confirm the heap returns to one free block of the full size.
  • Run compare and explain why best-fit leaves the least fragmentation for this workload.

Common errors

Heap shows two adjacent free blocks

Coalescing must check both next and prev after freeing.

Allocated sizes are not multiples of 8

Round requests up with align_up before searching.

Possible improvements

  • Next-fit and buddy-system strategies
  • Segregated free lists for common sizes
  • A trace file of alloc/free commands for repeatable experiments

Additional challenges

  1. 1Implement the buddy allocator and compare its internal fragmentation.
  2. 2Add realloc that grows in place when the next block is free.
  3. 3Plot fragmentation over a long random workload.

Built and tested it yourself? Mark it to update your dashboard.