KAME

kamepoolalloc — Structural Invariants

A catalogue of the invariants the lock-free pool allocator relies on for correctness. Purpose: when changing allocator.cpp / allocator_prv.h, grep the field or §-tag you are about to touch, find the invariants that constrain it, see what breaks if violated and whether a machine check guards it. This is the blast-radius map.

Each entry: INV-N — statement · enforced where · breaks if violated · verification.

Verification legend:


1. Chunk geometry (§15)

Constants: ALLOC_MIN_CHUNK_SIZE = 256 KiB (one “unit”); ALLOC_CHUNK_HEADER = 64; ALLOC_CHUNK_K_MAX = 4096; regions are ALLOC_MIN_MMAP_SIZE = 32 MiB (= 128 units); ALLOC_MAX_CHUNK_SIZE = 4 MiB (16 units).

2. Chunk-claim state machine — m_flags_packed (§13)

Layout: bit 31 = BIT_OWNED; bits 0..30 = MASK_CNT (live-slot count). Bit 30 is intentionally unused (was a retired BIT_RELEASED). BIT_OWNER_EXITED is modelled by the cleared-BIT_OWNED state, not a separate bit.

3. Region & radix (§13.2 / §13.3 / §19)

2-level radix g_lrc... s_radix_l1[L1] → RadixL2Node.entries[L2], slot value ∈ {KAME_RADIX_ABSENT=0, KAME_RADIX_POOL=1, KAME_RADIX_LARGE=2}.

4. back_offset & dedicated-chunk dispatch (§15 / §22)

s_back_offset[unit] (one byte/unit/region): base_u = u − back_off, chunk_base = region + base_u·256K. Bit 7 (0x80) flags a dedicated chunk.

5. owner-id identity

6. Large-recycle cache — L1/L2 (§21–§28)

K-major layout: g_lrc[k].slots[idx], k ∈ [0,LRC_K_MAX), idx ∈ [0,LRC_N_MAX]; each LrcKArray is alignas(KAME_CACHE_LINE). Kinds: LRC_CHUNK (idx [0,LRC_CHUNK_BND]), LRC_MMAP (idx (LRC_CHUNK_BND,N_MAX]).

7. Thread lifecycle & TLS (§20 / §23)

8. atomic_smart_ptr / LOCAL_REF_CAPACITY (shared with kamestm)


Verification coverage summary

subsystem machine-checked comment-only (Stage 2 candidates)
chunk-claim state machine (§13) INV-6 (TLA+ & GenMC), INV-7 (TLA+), INV-7b (GenMC), INV-8 (TLA+)
chunk geometry (§15) INV-1,2,4,5 (test) INV-3 (compile-assert only)
back_offset (§15/§22) INV-12 (TLA+), INV-13 (test)
radix (§13.3/§19/§27) INV-9 install (GenMC), INV-10,11 (test) INV-9 meta-handoff ordering (external)
recycle cache (§21–§28) INV-15,16,18 (GenMC), INV-19,20 (test) INV-17 (kind disjointness)
thread lifecycle (§20/§23) INV-21 (GenMC), INV-23 (test) INV-22 (deployment constraint, not model-checkable)
owner-id (§12) INV-14 (test)

Stage-2 progress: the high-blast-radius concurrency invariants are now GenMC-checked —

Remaining (low blast radius / not model-checkable): INV-17 (LRC kind disjointness — a pure-function clamp; a static_assert/runtime assert would suffice), INV-22 (the IE-TLS dlopen restriction — a deployment constraint, not a concurrency property), and INV-9’s meta-handoff ordering (external to the allocator — rides the client’s alloc→free synchronisation).