Skip to main content

Module gc

Module gc 

Source
Expand description

Mark-sweep garbage collection.

Roots for collection are the active environment and any explicitly named “live” values (such as the value an evaluator just produced). Marking is a fold over reachable Addresses; sweeping is delegated to Heap::retain. Both phases are pure functions: no mut, no interior mutability, no Rc/Arc.

Address reachability is the transitive closure of:

  1. Each binding in env is reachable.
  2. Each value in roots is reachable.
  3. From any Value::Closure, every binding in its captured environment is reachable.
  4. From any Value::Ref whose address is in the heap, the value at that address is reachable.

Dangling references (a Value::Ref whose address has no heap entry) are tolerated by the collector: the address is still marked live, since sweeping cannot create or resolve a dangling ref, only preserve it.

Functions§

collect
Retain only the cells reachable from roots and env, returning the resulting heap. The next-address counter is preserved so old addresses continue to detect as dangling rather than colliding with newly-allocated cells.
reachable
Compute the set of addresses reachable from roots and env through heap.