Arena Allocator
Published:
Stack: C, operating systems, virtual memory
Overview
A small arena (bump / linear) allocator written in C, built on top of OS virtual memory instead of malloc.
Why it matters
Managing memory with malloc/free means every allocation has its own lifetime and you have to remember to free all of them, usually in some order that makes sense. Miss one and you leak. Free too early and you get use-after-free. Arena allocators sidestep this: you grab one big block, push objects into it as you go, and when you’re done you throw the whole block away. One lifetime, one cleanup, no per-allocation bookkeeping.
This particular version uses the OS directly (VirtualAlloc on Windows, mmap/mprotect on Linux) so the arena can reserve a large virtual address range up front but only commit physical pages when something is actually pushed. That way a big arena costs you almost nothing until you use it.
