Project 1: Custom Allocator

A set of custom memory allocators, including a simple “bump” allocator and a fixed-size “pool” allocator, that adhere to Zig’s std.mem.Allocator interface.

Quick Reference

Attribute Value
Primary Language Zig
Alternative Languages C, Rust
Difficulty Level 2: Intermediate
Time Estimate Weekend
Knowledge Area Memory Management
Tooling A custom memory allocator
Prerequisites Basic understanding of pointers and memory layouts.

What You Will Build

A set of custom memory allocators, including a simple “bump” allocator and a fixed-size “pool” allocator, that adhere to Zig’s std.mem.Allocator interface.

Why It Matters

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

Core Challenges

  • Implementing the Allocator VTable → maps to understanding function pointers and interfaces
  • Managing memory blocks → maps to pointer arithmetic and byte-level manipulation
  • Handling alignment → maps to @alignOf, @ptrToInt, and bit-masking
  • Integrating with std.ArrayList → maps to using your allocator with standard library types

Key Concepts

  • Allocator Interface: Zig Documentation - std.mem.Allocator
  • Pointer Arithmetic: “Low-Level Programming” by Igor Zhirkov, Ch. 4
  • Memory Alignment: “Computer Systems: A Programmer’s Perspective”, Ch. 3

Real-World Outcome

$ zig run main.zig
Bump Allocator:
  Allocated 16 bytes at 0x7ffee...00
  Allocated 32 bytes at 0x7ffee...10
  Resetting arena...
  Allocated 16 bytes at 0x7ffee...00 (same address as before)

Pool Allocator:
  Allocated object at 0x7ffee...40
  Allocated object at 0x7ffee...50
  Freed object at 0x7ffee...40
  Allocated object at 0x7ffee...40 (reused memory)

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