A garbage collector
Stage 4 of 9v2 · 21f12716

Graph marking

Traverse references with a work stack and tolerate cycles.

Graph marking

Traverse references with a work stack and tolerate cycles. Preserve earlier capabilities and use the fixture only to make observation deterministic.

Contract

The harness models the heap with JSON objects, integer addresses, and explicit references. Output sorts objects by address for deterministic results.

{
  "op": "mark",
  "roots": [0],
  "objects": [
    { "at": 0, "refs": [16] },
    { "at": 16, "refs": [0] },
    { "at": 32, "refs": [] }
  ]
}

produces exactly:

{ "marked": [0, 16] }

Emit compact JSON with a final newline. Invalid input leaves stdout empty, writes error to stderr, and exits non-zero.

Design and limits

Separate policy, mutable state, and system effects. Make resource ownership, cleanup order, and pre/post-operation invariants explicit. Do not replace the central mechanism with a simulation: the fixture controls inputs and time while your code implements isolation, memory, or synchronisation as appropriate.

Test empty boundaries, mid-operation failures, and idempotent cleanup. Sort output only at the presentation boundary.

Acceptance

  • The example produces the exact output.
  • Resources are not leaked and work is not duplicated.
  • Failure leaves the system coherent.