x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
x-heap.h File Reference

Heap management and mark-sweep garbage collection. More...

#include "x-base.h"

Go to the source code of this file.

Detailed Description

Heap management and mark-sweep garbage collection.

Provides functions for marking reachable objects and sweeping unreachable ones. Requires X_HEAP to be defined.

Objects are linked into a singly-linked heap chain via x_obj_heap(). The GC lifecycle is:

  1. Mark: call x_heap_tree_mark() on known roots and x_heap_root_chain_mark() for the registered stack roots.
  2. Sweep: call x_heap_sweep() to free all unmarked objects. Objects with X_OBJ_FLAG_SHARED are always retained. Mark flags are cleared during sweep, preparing for the next cycle.

Rooting rules – an object survives GC if any of these apply:

  • It has X_OBJ_FLAG_SHARED set (permanent; used for base tree nodes).
  • It is reachable from a pair tree passed to x_heap_tree_mark().
  • It is reachable from an off-chain object registered on the root chain (x_heap_root_push()), marked each collection by x_heap_root_chain_mark().
  • It is stored in a base environment field (reachable when the base tree is marked).

A C local holding the only reference to a heap object is NOT a root by itself: the frame must register it (see the root chain below).

Objects not reachable by any of the above are collected during sweep.

Author
Jon Ruttan (jonru.nosp@m.ttan.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)