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
andvmstack.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, callgc()
, 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 thestruct
definitions so that every heap-allocated payload has aforwarded
pointer. And that pointer is used (via macroGCVALIDATE
) in thestatic 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 untilexpect
runs, but that value is no longer stored in astatic
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
andgcprintf
, which do something only when theSVMDEBUG
gc
field is non-NULL.