Homework 4: a garbage collector for C

In this homework, we build a basic, conservative garbage collector for C programs. Starting from the set of root pointers present in the stack and global variables, we traverse the “object graph” (in the case of malloc, a chunk graph) to find all chunks reachable from the root. These are marked using the third lowest order bit in the size field of each chunk.

As of today we have not covered the concepts related to garbage collection in class; if you wish to get a head start on this homework, take a look at section 9.10, and 9.10.2 specifically.

The skeleton code in the public git repository provides supporting code for finding the limits of the global variable area in memory, as well as for the stack and heap areas. It also demonstrates very basic marking and sweeping. Your task is to complete the garbage collector, so that it frees all garbage (aka inaccessible memory), and leaves every other chunk intact. To compile the template, simply run make to compile, and run with ./hw4.

Performance requirements

For full points, your program should pass all tests in main.c - that is, it should free all inaccessible memory. At present, these are not coded as actual tests, they simply output the heap size and number of free and in-use chunks. Nevertheless, your program should produce the correct values.

Runtime performance is not a major goal for this homework. However if your program is taking more than 2 seconds, you almost certianly did something wrong.

Turn-in instructions

Merge the upstream public repostiory into your personal repository (the same as for homeworks 1 and 2) to get the skeleton code. You should only make changes to hw4.c. Make sure you’ve pushed your code to master before the deadline; if you want a commit besides the most recent commit graded, make sure to tag it with hw4-submission.

Due date

This homework is due Monday, March 28, at 11:59:59 PM.