Project 2: Memory Allocator (malloc/free from scratch)

A complete memory allocator that replaces malloc/free, using only brk() and mmap() syscalls, with visual debugging output showing heap state.

Quick Reference

Attribute Value
Primary Language See main guide
Alternative Languages N/A
Difficulty Level 3: Advanced
Time Estimate 2-3 weeks
Knowledge Area Memory Management / Systems Programming
Tooling sbrk / mmap
Prerequisites C, pointers, basic understanding of processes

What You Will Build

A complete memory allocator that replaces malloc/free, using only brk() and mmap() syscalls, with visual debugging output showing heap state.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Managing the program break with brk()/sbrk() (maps to virtual memory, syscalls)
  • Using mmap() for large allocations (maps to virtual memory, page faults)
  • Implementing free lists and coalescing (maps to memory management algorithms)
  • Handling alignment requirements (maps to hardware memory model)
  • Debugging memory corruption (maps to understanding address space layout)

Key Concepts

  • Virtual address space layout: “Computer Systems: A Programmer’s Perspective” Ch. 9 - Bryant & O’Hallaron
  • Free list management: “Operating Systems: Three Easy Pieces” Ch. 17 - Arpaci-Dusseau
  • mmap syscall: “The Linux Programming Interface” Ch. 49 - Michael Kerrisk
  • Memory alignment: “C Interfaces and Implementations” Ch. 5 - David Hanson

Real-World Outcome

Deliver a working demo with observable output that proves the feature is correct.


Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: TRACK_A_OS_KERNEL_PROJECTS.md
  • “Computer Systems: A Programmer’s Perspective” by Bryant & O’Hallaron