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

Spaces is a high-performance C allocator for Linux x86-64 that introduces explicit heap regions and memory capping. It serves as a drop-in malloc replacement while offering advanced features like shared memory and runtime diagnostics.
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. 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. Runtime diagnostics— walk every live allocation in a heap region without an external profiler, recompilation, or debug build.
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
I maintain a codebase where different subsystems need their own memory budgets, and where cleanup is the hardest part. Existing options fell into two camps: fast black-box allocators (jemalloc, mimalloc) or structured region allocators (Apache APR) that aren't competitive as general replacements. Spaces tries to be both.
The trade-off: every free() reads one cache line from the slab header at the 64 KB boundary of the freed pointer. That costs ~5 ns when the line is cold. In exchange, free() discovers the size class, the owning thread, and the region identity in a single load, with no page map, no radix tree, and no external metadata structure.
Spaces also exposes live-allocation walking, per-region memory ceilings, and cross-process shared heaps. For uniform-sized objects, it offers zero fragmentation and O(1) performance.
Performance Summary:
Top 3 on 7 of 8 workloads in standard benchmarks (larson, malloc-large, mleak, etc.), beating jemalloc and tcmalloc in several categories. The main limitation is heavy thread-churn workloads where the slab-header cache miss on free() becomes more prominent.
Internal Architecture:
- 64 KB-aligned slabs for O(1) metadata lookup.
- 52 size classes (8 B – 8 KB).
- Lock-free fast path for same-thread malloc/free.
- ABA-safe Treiber stack for cross-thread free.
- Large allocations (> 8 KB) direct from
mmapwith huge-page support.
Source: Hacker News









