Skip to content

Latest commit

 

History

History

allocator

Folders and files

README.md

allocator

Allocate and manage memory.

A Zig port of commonware's commonware-allocator.

Status

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.

Modules

  • Heap (heap.zig): a std.mem.Allocator with mimalloc's layout. allocator() hands it out; read_stats, contains, and collect inspect 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 mimalloc Stats.
  • metrics (metrics.zig): each exported Stats field 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.

Testing

zig build test-allocator  # unit and integration tests, including the seeded heap fuzz

A std.testing.fuzz target drives the heap too; for long runs use zig build test --fuzz -Doptimize=ReleaseSafe.

Differences from commonware

All of these are recorded in the parallel lane decisions.

  • There is no mimalloc: zigware has no C dependencies. Heap is 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; collect drains and releases them. Allocated bytes are summed over the caches when read, so Stats are 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 after locals_max (64) distinct threads later threads take the lock for every block. Only the measured bench harnesses use a Heap, and their threads come from std.Io.Threaded, which keeps its workers until the Io is torn down; a program that churns short-lived threads needs a thread_exit that abandons the caller's pages first.
  • The mimalloc-only knobs (large OS pages, transparent huge pages, the stats merge threshold) are recorded in effective but change nothing.
  • The huge-page-advice integration tests are not ported: they read Linux smaps for mimalloc's THP advice.