Skip to content
Tech News
← Back to articles

A single-file C allocator with explicit heaps and tuning knobs

read original more articles
Why This Matters

Spaces offers a lightweight, single-file C allocator that provides explicit heap management, enabling developers to create, cap, inspect, share, and destroy memory regions efficiently. Its design simplifies memory handling in complex applications like parsers, caches, and diagnostics, reducing leaks and improving control. This innovation enhances both developer productivity and application stability in the Linux environment.

Key Takeaways

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