Allocate and manage memory.
A Zig port of commonware's
commonware-allocator.
ALPHA upstream: breaking changes are expected. See commonware's stability levels. The allocator backlog in docs/lanes/parallel.md is done. Commonware's integration tests are ported, except the huge-page-advice ones.
Heap(heap.zig): astd.mem.Allocatorwith mimalloc's layout.allocator()hands it out;read_stats,contains, andcollectinspect and purge it.Config,Purge,Reserve,EagerCommit,Status,Effective(config.zig): a heap's configuration, mirroring commonware's mimalloc options, and what the heap runs with. The arena geometry constants live here too.good_size(size_class.zig): mimalloc's size classes, so a request gets the same block size as under commonware's mimalloc.Stats(stats.zig): a heap's statistics, with the fields of commonware's mimallocStats.metrics(metrics.zig): each exportedStatsfield with its name, help text, and kind, so a runtime can register every series.- arena.zig and os.zig: arenas of 64 KiB slices, and the OS mappings, unmappings, and purges a heap uses.
zig build test-allocator # unit and integration tests, including the seeded heap fuzzA std.testing.fuzz target drives the heap too; for long runs use
zig build test --fuzz -Doptimize=ReleaseSafe.
All of these are recorded in the parallel lane decisions.
- There is no mimalloc: zigware has no C dependencies.
Heapis a native allocator with mimalloc's layout, size classes, arenas, reservations, and delayed purges. - A heap is a value, not the process allocator, so configuration is per heap rather than once per process.
- Each thread gets a cache in the heap, as mimalloc's per-thread heaps: it allocates and frees
its own small pages without the heap's lock, other threads' frees into them wait on a
lock-free deferred list, and its last emptied pages are kept for a few refills;
collectdrains and releases them. Allocated bytes are summed over the caches when read, soStatsare exact once no thread is allocating (mimalloc's per-thread counters drift by up to a merge threshold). - There is no thread-exit hook (mimalloc's
mi_thread_done): a thread that exits keeps its cache slot and its pages, with any blocks other threads free into them, until a thread with the same id reuses the slot, and afterlocals_max(64) distinct threads later threads take the lock for every block. Only the measured bench harnesses use aHeap, and their threads come fromstd.Io.Threaded, which keeps its workers until theIois torn down; a program that churns short-lived threads needs athread_exitthat abandons the caller's pages first. - The mimalloc-only knobs (large OS pages, transparent huge pages, the stats merge threshold)
are recorded in
effectivebut change nothing. - The huge-page-advice integration tests are not ported: they read Linux smaps for mimalloc's THP advice.