Repository navigation
Performance and correctness release. Everything since 1.2.7.
Allocation and free paths
- Every zone has its own lock. Allocation and free take only that lock; the root lock is now reserved for creating, destroying and finding zones. Multi threaded throughput scales with the thread count: 8 threads doing alloc/free pairs run about 40x faster than in 1.2.7.
- Requests are served only from a zone of exactly their size class. Classes are exact up to 256 bytes and four per power of two above, so a chunk is never more than a quarter larger than its request. Previously a request could land in any cached zone that was big enough, which inflated memory up to 32x on mixed size workloads. The minimum chunk size is now 16 bytes.
- Pointer to zone lookup is one lock free hash probe. Zone user pages are aligned to their size and the map is keyed on that window. Big zones have their own map, so freeing a big zone no longer walks every live big zone.
- The chunk quarantine and zone cache moved into a per thread block the allocator maps itself and flushes when the thread exits, fixing a crash at thread exit on macOS where dyld could release thread local storage before the quarantine was flushed.
- Canary chunks are written lazily, one page at a time as the free list reaches them, so a fresh 4 MB zone costs only the pages it touches. The zone struct shrank to 1280 bytes and the zone array is no longer locked resident as a whole.
Memory returned to the kernel
- Retired zones are reset in place instead of unmapped and remapped, and zones that stay empty for
ZONE_PURGE_DELAY_MSare reset the same way, so freed memory goes back to the kernel and is zero when reused. iso_callocskips clearing chunks that were never handed out and big zones that were just mapped.- On Linux the zone array and, with
HUGE_PAGES=0, zone user pages are advisedMADV_NOHUGEPAGEso transparent huge pages do not back sparsely used zones.
Security
- Zone pointers stay masked in memory for the whole life of a zone.
- Each zone draws from its own random stream and starts its chunks at a random, alignment preserving offset.
- The free list shuffle is a uniform Fisher-Yates; the previous index produced only cyclic permutations.
- Zone 0 is reachable through the size lookup table again. Its index collided with the empty sentinel so the smallest default zone was never used.
Fixes
- Canary placement, bitmap bucket sharing, the internal printf, memory tagging, the heap profiler, aligned allocation, zone retirement off by one, big zone list handling, strdup, and the leak detector counting unwritten canary chunks.
Build and API
ZONE_CACHE_SZis gone. New knobs inconf.h:ZONE_PURGE_DELAY_MS,FREE_LIST_RESEED_REFILLS. Linux builds use-ftls-model=initial-exec, seeTLS_MODELin the Makefile. macOS no longer links the Security framework.iso_alloc_protect_rootnow makes concurrent allocator use from other threads fault rather than block, since frees no longer take the root lock.- Six new tests cover size classes, the pointer map, zone purging, calloc, thread exit, and lock contention.