2 GC Collector Scheme
#lang plai/collector |
GC Collector Scheme is based on PLAI Scheme. It provides additional procedures and syntax for writing garbage collectors.
2.1 Garbage Collector Interface
The GC Collector Scheme language provides the following functions that provide access to the heap and root set:
procedure
procedure
(heap-value? v) → boolean?
v : any/c
procedure
loc : location? val : heap-value?
procedure
(heap-ref loc) → heap-value?
loc : location?
syntax
(get-root-set id ...)
procedure
(procedure-roots proc) → (listof root?)
proc : procedure?
syntax
(with-heap heap-expr body-expr ...)
heap-expr : (vectorof heap-value?)
(test (with-heap (make-vector 20) (init-allocator) (gc:deref (gc:alloc-flat 2))) 2)
(test (with-heap (make-vector 4) (define f1 (gc:alloc-flat 1)) (define c1 (gc:cons f1 f1)) (with-roots (list c1) (gc:deref (gc:first (gc:cons f1 f1))))) 1)
2.2 Garbage Collector Exports
A garbage collector must define the following functions:
procedure
(init-allocator) → void?
procedure
(gc:deref loc) → heap-value?
loc : location?
procedure
(gc:alloc-flat val) → location?
val : heap-value?
Note that closures are flat values. The environment of a closure is internally managed, but contains references to values on the heap. Therefore, during garbage collection, the environment of reachable closures must be updated. The language exposes the environment via the procedure-roots function.
procedure
(gc:set-first! cons-cell first-value) → void?
cons-cell : location? first-value : location?
procedure
(gc:set-rest! cons-cell rest-value) → void?
cons-cell : location? rest-value : location?
procedure
loc : location?
Returns true if loc refers to a cons cell. This function should never signal an error.