docs

#7.1. Object layout and memory spaces

On this page

Every object managed by the GC starts with a fixed header. This document defines the header format and the three memory spaces an object can reside in.

Note

This section is informative, except the 16-byte header, which is fixed as ABI in 8.1. The memory spaces describe the reference runtime and may change in any release.

#7.1.1. 16-byte header

The first 16 bytes of every GC object are the header.

offset  0 :  type_id    u32       // type identifier
offset  4 :  gc_flags   u32       // GC state bits
offset  8 :  gc_word    u64       // total object size (new address during evacuation)
offset 16 :  payload ...           // the object's actual fields

Both the header and the payload are 8-byte aligned. The layout is the same on 32-bit and 64-bit, so the header is 16 bytes on 32-bit targets too.

#type_id (offset 0, 4 bytes)

The type's unique identifier, assigned per type by the compiler.

It has several uses. In dynamic dispatch it serves as the tag of the any slot; in GC field scanning it locates fields through the type_id → field layout mapping; and in diagnostics it is used to show a user-friendly type name.

A type_id is stable for the lifetime of the process; the same type always has the same id.

#gc_flags (offset 4, 4 bytes)

Bits representing GC state.

BitMeaning
markWhether reached in the current GC cycle
stickysticky mark-bit (generation tracking)
pinnedMarked as not movable, e.g. for FFI
forwardedAlready evacuated to a new location (gc_word is the forwarding pointer)
largeLOS (Large Object Space) object
...Additional bits are decided by the GC implementation

The exact bit placement is decided by the GC implementation; the table only lists the kinds of bits semantically required.

#gc_word (offset 8, 8 bytes)

The object's total size (including the header). For an object moved during evacuation, the forwarded bit is set and this slot points to the new location (8.1.4).

#payload (offset 16+)

The region holding the object's fields. Field order and alignment are decided by the compiler; this applies to enum and union alike. A struct value is not a heap object of its own; stored in a heap object (list buffer, closure environment, coroutine frame) it is laid out inline here (8.8.3).

If an attribute such as #[repr(C)] is attached, the corresponding representation rules apply (Built-in attributes).

#7.1.2. Memory spaces

The GC places each object in one of the following three spaces. Literals and const / static data are not heap objects: they live in the program's data sections, outside these spaces, and the GC neither marks nor moves them (a static that holds a GC reference is a root, 7.4.1).

#1. Immix

The space where most ordinary objects live, using the Immix mark-region algorithm.

It is divided into 32 KiB blocks and 128 B lines. Mark-sweep is the default, and a full collection may move objects out of fragmented blocks (opportunistic evacuation). Generations are separated by the sticky mark-bit (7.3).

#2. Large Object Space (LOS)

Objects of 8 KiB or more, or requiring alignment greater than 8 bytes, and secret buffers (nabi_gc_secret_new, 8.5.1), are allocated individually in the LOS, with the LARGE bit of gc_flags set. The LOS is collected by mark-sweep and does not move objects. Old LOS objects have a card table (7.4.2). A secret buffer has the WIPE bit (8.1.3): the sweep that frees it overwrites its payload with zeros first.

#3. Pinned objects (FFI pinned)

A GC pointer passed to an FFI function of an extern block is not moved until the call ends, because the argument is in the roots (7.4.1). If foreign code needs to keep the address after the call, pin it with nabi_gc_pin. A pinned object has the PINNED bit of gc_flags set and stays in place. nabi_gc_unpin releases the pin.

Pinned objects are still collected by mark-sweep, but never evacuated.

#7.1.3. Movement between spaces

TLAB (allocation) → Immix
Immix → Immix         (evacuation in full collection)
Immix / LOS ↔ pinned  (pin / unpin, in place)

Objects do not move between spaces. Only evacuation changes an address.

#7.1.4. Motivation for the header width

The 16-byte header is the result of a time/space tradeoff.

For small objects, such as a 4-byte i32, a 16-byte header hurts memory efficiency. But 16 bytes hold type_id, flags and forwarding all at once, simplifying the algorithms, and the layout stays the same on 32-bit and 64-bit, simplifying cross-platform code too.

This tradeoff is a reasonable choice under the assumption that most objects are 4 bytes or larger.

#7.1.5. Registering type_id: nabi_gc_register_type

Each type's field layout must be registered with the GC.

nabi_gc_register_type(type_id, scan_table)

type_id is the type's identifier, and scan_table holds the offsets of the fields that are pointers to GC objects, plus additional metadata.

When marking an object, the GC uses its type_id to find the scan_table and follows the child pointers through it.

The compiler performs this registration at process start through extern "C" calls; the registration ABI is defined in Extern "C" symbols.

#7.1.6. Computing the total object size

size = 16  (header) + payload_size  (aligned size of the fields)

The allocator stores this total size in the header's gc_word for every object, Immix and LOS alike (8.1.4), so the collector reads it there.

Found a mistake? Edit this page on GitHub. Last updated