Overview of Code for Module 11

Files you will write or edit

vmheap.c

This file has been overhauled and extended to contain a garbage-collection infrastructure that’s nearly complete. You’ll add just three elements: scanning procedures for your own data structures (activation record and VM state), the collector itself, and a heap-growth policy.

There’s a lot to read here, so I’ve written a guide to the code.

vmstate.h and vmstack.h

You may have to edit these files to make sure that there is a pointer to every reachable object and there are no interior pointers (Get your VM state ready for garbage collection in the main handout).

vmrun.c

You have to make two changes here. First, you have to check gc_needed at every ordinary call, every tail call, and every backward branch. Second, when a GC is needed, you have to flush any cached state, call gc(), then reload any flushed state.

Second, you need to make sure that any payload that is allocated on the heap gets a properly initialized forwarding pointer. The easy way to do this is use the VMNEW macro.

Important changes to existing files

value.h, vmstring.h

Using a macro called GCMETA, I’ve edited the struct definitions so that every heap-allocated payload has a forwarded pointer. And that pointer is used (via macro GCVALIDATE) in the static inline value-reading functions. If the pointer is ever non-NULL, it means that the interpreter is seeing a stale version of an object, and the macro causes an assertion failure.

check-expect.c

The check function still holds onto a value until expect runs, but that value is no longer stored in a static variable—it is now stored in the VM state record.

New files you will need to know about

vstack.h, vstack.c

These files declare and implement a stack of values. The stack is the data structure used to implement the set of gray objects. The stack is a garbage-collector data structure, so it is allocated using malloc, not on the managed heap.

Other changes to existing files

vtable.c

Tables are allocated on the managed heap. I have added support for copying a table and providing access to its forwarding pointer.

vmstring.c

Now initializes the forwarding pointer in string payloads.

New files that aren’t terribly important

vmsizes.h

This file gathers in one place all the rules for knowing how big a heap-allocated payload is. The information is used in the copy functions defined in vmheap.c.

gcmeta.h

This file defines garbage-collection metadata and its initialization and validation. For our collector, the only metadata is a forwarding pointer. But if we wanted to add a tag to each heap payload, or we wanted to switch to a mark-sweep collector, we could make that change here.

gcdebug.h, gcdebug.c

Provides functions gcprint and gcprintf, which do something only when the SVMDEBUG gc field is non-NULL.