xo::mm::DArena
An arena reserves a large range of addresses up front and hands it out with a bump pointer. What it uses creeps forward continuously; what it commits — the memory actually backed by pages — can only move in whole superpages, so it advances in jumps.
32 MB reserved · 2 MB superpages
DArena tracks three pointers into one reserved range — lo_,
the bump pointer free_, and the commit frontier limit_ —
plus the end of the reservation, hi_. Every quantity above is a
difference between two of them.
hi_ - lo_. Fixed when the arena is built: one
mmap with no access rights. Costs address space, not memory.
committed_z_. Memory actually backed by pages, moved only by
expand() calling mprotect(PROT_READ|PROT_WRITE).
free_ - lo_. Everything the bump pointer has passed over, including
each allocation's 8-byte header and its padding up to 8-byte alignment.
allocated(); the gap from there to committed()
is available(), memory ready for the next allocation with no syscall.
Each allocation asks expand() for allocated() + z1 bytes.
If that still fits below limit_, nothing happens — the common case, and
the reason allocation is a pointer bump. When it doesn't fit, expand()
rounds the target up to the arena's alignment and mprotects the
difference in one call:
aligned_target_z = padding::with_padding(target_z, arena_align_z_)
That alignment is the 2 MB hugepage_z_ whenever the requested arena is at
least one superpage, and the 4 KB page size otherwise. Hence the staircase: sixteen
2 MB treads across a 32 MB reservation. Turn the superpages off above and the same run
climbs in 4 KB steps — so fine that the commit frontier looks continuous, which is
exactly the trade. Superpages relieve TLB pressure, at the cost of committing up to
2 MB before it is needed.
clear() sets free_ = lo_ and re-establishes the guard bytes.
It does not touch committed_z_ or limit_. Used and allocated
collapse; the physical memory stays with the process, ready to be handed out again
without another mprotect. Only unmap() — also what the
destructor runs — returns the range to the kernel.
This is a model, not a recording. The arithmetic follows the source — 8-byte alignment,
an 8-byte header per allocation, commit targets rounded to arena_align_z_ —
but the allocation sizes are an illustrative mix rather than a captured trace: three
populations — small objects, buffers, the occasional big one — scaled so the mean matches
the object-size slider. Guard bytes are off, matching the default
guard_z_ = 0. Overhead is worth driving with that slider: at 16 KB objects the
8-byte header and its padding come to well under a tenth of a percent of what the arena
hands out, and at 64 B they cost more than a tenth of it. On the reservation ribbon that
share is a stripe through the middle of the allocated block rather than a slab at its end,
since the headers are scattered through the range and not gathered anywhere; it is held to
a minimum of one pixel so a small share stays visible. Below roughly a pixel per allocation
the page view stops drawing boundaries and says so, since at that point they are a grey
wash rather than information. Something that ran out of reserve would capture
reserve_exhausted and return null rather than growing; this run never gets
that far.