You must complete this program with the same partner you had for "Written: Continuations". This will be your team for the next few assignments.

Garbage Collection

In this assignment, you will implement two garbage collectors: mark & sweep, and stop & copy. As we have seen, garbage collection involves starting from a root set of references, which reside in local variables and stack frames, and searching for reachable values on the heap.

Writing your Garbage Collectors

Your garbage collectors must be written in the GC Collector Scheme language level. This langauge defines an interface to a program's stack and heap that you will use to implement garbage collection.

Your garbage collectors must implement the following functions:

To help you write these functions, the GC Collector Scheme language defines an interface for the heap and the roots:

Note that get-root-set accepts an arbitrary number of identifiers that it treats as roots and returns as part of the root set. That is, read-root returns the cell address stored in the location referenced by the identifiers and set-root! mutates the identifier to reference a new cell address. You may find this useful when you implement gc:cons; you may wish to add the first and last arguments to your root set in lieu of manually updating them if they move during garbage collection.

Testing your Garbage Collectors

You may write programs that exercise your garbage collectors using the GC Mutator Scheme language. This language is a subset of Scheme that uses a garbage collector that you specify. The first line of a test program must be:

(allocator-setup "" heap-size heap-offset)

"" must be the name of your collector's file. heap-size is the size of the heap your collector will use. heap-offset is the base address on the heap where allocation should start (usually 0).

The remainder of the program is in a subset of Scheme with numbers, symbols, lists, etc. The primitives of the language map directly to the procedures you define in your garbage collector.

Sample Code

To get you started, we've provided a sample mutator and a trivial collector that signals an error when the heap fills up. (In fact, our collector does signal an error with the mutator we've provided, if you don't increase the size of the heap.)


You must store bookkeeping data on the heap provided by GC Collector Scheme. You may store 2-3 atomic values, such as addresses into the heap as variables in your garbage collector. We will assume they represent machine registers. However, all compound data structures must be on the heap.

In particular, in the mark & sweep collector, you should maintain a free list in the heap. That is, you should not use any auxiliary data structure; instead, use the available space in the heap to keep track of the free list. You may use one extra box (“register”) to point to the start of the free list. You may need to adjust your allocation accordingly to have enough space to maintain free list pointers!

Some final words of advice:


A single member of your team should handin the assignment. Please include a README file that lists your team's members. From the directory containing the files for the assignment you wish to hand in, execute

/course/cs173/bin/cs173handin gc