Spaces
Spaces is a single-file C allocator for Linux x86-64. It works as a drop-in malloc replacement, but its distinctive feature is that it also gives you explicit heap regions: create a heap for a subsystem, cap its memory, inspect every live allocation, share it across processes, and destroy the entire region in one call.
Use cases:
Parser/compiler arenas — free an entire million-node AST with one spaces_chunk_destroy() call instead of chasing pointers.
— free an entire million-node AST with one call instead of chasing pointers. Memory-bounded caches — set a hard ceiling on a chunk and let the allocator enforce it, so your server doesn't OOM at 3 AM.
— set a hard ceiling on a chunk and let the allocator enforce it, so your server doesn't OOM at 3 AM. Runtime diagnostics — walk every live allocation in a heap region without an external profiler, recompilation, or debug build.
Quick start
gcc -O3 -pthread -fPIC -c spaces.c ar rc libspaces.a spaces.o gcc your_app.c -Wl,--whole-archive libspaces.a -Wl,--no-whole-archive -lpthread
#include "spaces.h" // A bounded cache with enforced ceiling SpacesChunk cache = spaces_chunk_create ( 0 ); spaces_chunk_set_ceiling ( cache , 256 * 1024 * 1024 ); void * p = spaces_chunk_alloc ( cache , request_size , 0 ); if (! p ) evict_oldest (); // ceiling enforced by the allocator spaces_chunk_destroy ( cache ); // instant teardown, everything freed
// Or just use it as malloc — same binary, no code changes void * p = malloc ( 4096 ); free ( p );
... continue reading