Skip to main content

Module closure_layout

Module closure_layout 

Source
Expand description

Typed closure layout for v2 runtime.

A TypedClosure parallels TypedStruct: it has an 8-byte HeapHeader followed by a function_id: u32 / type_id: u32 pair, then a compact C-style capture area with compile-time-known offsets.

§Memory layout

Heap variant (escaping closure):
  Offset  Size  Field
  ------  ----  -----
    0       8   HeapHeader
    8       4   function_id (u32)
   12       4   type_id (u32, ClosureTypeId.0)
   16+      ..  captures[] (C-laid-out per ClosureLayout)

Stack variant (non-escaping closure, Cranelift StackSlot):
  Offset  Size  Field
  ------  ----  -----
    0       4   function_id (u32)
    4       4   type_id (u32, ClosureTypeId.0)
    8+      ..  captures[]

Captures start 8-byte aligned in both variants (HeapHeader and the function_id+type_id pair are both 8 bytes). The relative offset of each capture inside the captures area is the same for both variants — only the preceding header differs.

§Keying

ClosureTypeIds are minted per capture signature (Vec<ConcreteType>), not per closure literal. The closure body is carried separately by function_id. Two literals with identical captures (e.g. two |x| x + 1 expressions with no captures) share ClosureTypeId(0). See docs/v2-closure-specialization.md §1.2.

Structs§

ClosureLayout
Computed layout for a closure’s captures.
ClosureRegistry
Registry of closure capture layouts, keyed on capture signature AND per-capture kind.
SharedCell
Interior-mutable cell backing a CaptureKind::Shared capture.
SharedCellGuard
RAII guard returned by SharedCell::lock. Releases the lock on Drop. Dereffs to the inner ValueWord.
StackClosure
Stack-allocated closure. No HeapHeader; captures follow the function_id/type_id pair at offset 8.
TypedClosureHeader
Heap-allocated closure. The HeapHeader is at offset 0; captures follow the function_id/type_id pair at offset 16.

Enums§

CaptureKind
Storage discipline for a closure capture.

Constants§

HEAP_CLOSURE_HEADER_SIZE
Byte size of the heap closure header: HeapHeader (8) + function_id (4) + type_id (4).
SHARED_CELL_LOCKED
Locked state byte value.
SHARED_CELL_STATE_OFFSET
Byte offset of the lock state byte within SharedCell. The JIT’s inline lock CAS targets this offset as a compile-time constant.
SHARED_CELL_UNLOCKED
Unlocked state byte value.
SHARED_CELL_VALUE_OFFSET
Byte offset of the value payload within SharedCell. The JIT’s inline load/store targets this offset as a compile-time constant.
STACK_CLOSURE_HEADER_SIZE
Byte size of the stack closure header: function_id (4) + type_id (4).

Functions§

native_kind_from_concrete_type
Map a ConcreteType to the matching NativeKind for closure-capture kind tracking (ADR-006 §2.7.8 / Q10).