Assignment 3

CS 550 Programming Languages
Instructor: Jeremy Johnson 
Due date: Wed. April 29 at midnight (no late assignments will be accepted)

Memory Allocation and Garbage Collection

This assignment modifies the solution to Assignment 2, by providing an implementation of the underlying dynamic memory management used to support lists. Dynamic memory management should use garbage collection with a mark and sweep algorithm. The implementation of dynamic memory will assume that all allocated blocks are the same size (i.e. will hold either an int or a pointer to a list cell) and will follow the implementation outlined in Lecture 3. Use a vector or array of elements (ints or pointers, which are indices, to list cells). An element is stored in a cons cell which has two fields: a car field and a cdr field. cons cells also need bits to distinguish between pointers and ints and should also have a bit, for marking, to be used by garbage collection.

You will need to reimplement cons, from assignment 2 so that it checks to see if there are any available cells and if so returns a cell and updates the available cell list, and if not calls garbage collection. Recall that the available cells in the heap (a vector of cons cells) are linked together in an available cell list. Garbage collection works by first marking all active lists, i.e. those accessible from variables in the active environments, and then reclaiming, i.e. putting in the available cell list, those cells in the heap that are not marked. Afterwards, cells need to be unmarked. Make sure that temporary cells, e.g. those used to build list constants, or those used when concatenating lists, are accessible to the garbage collector.

You will also need to reimplement car, cdr, and null? to access the car and cdr fields of cons cells and to check for lists that are null (list with index -1.

This assignment may be written in any of the following languages: C, C++, java, python, and their corresponding parser generators, though the current mini language interpreter provided from me is written in C++ and bison(there is some java code but that code does not suppport the extension of the mini language to allow procedures).

Background Information

Background information is available in lectures 2 and 3.

What to do

Redo the list processing functions in Assignment 2 to use your own implementation of dynamic memory allocation and garbage collection.
  1. Provide a heap of cons cells (use a vector or array). Organize available cells into an available cell list.
  2. Reimplement cons, car, cdr, and null? to use and access cells in the heap. All allocation should ultimately be done through cons. The cons function should get cells from the available cell list, and if none are available call garbage collection. If cells are reclaimed by garbage collection, the processing of cons should resume, otherwise an error message printed.
  3. Implement a mark and sweep garbage collector. Accessible cells are those that can be reached from variables in the active environments. You also need to make sure to handle temporary variables properly, i.e. those used to construct list constants and in support of the concatenation operator.
Make sure that you thoroughly test your garbage collection routines by executing several scenarios where garbage collection is required.

How to submit

Students should submit their solution electronically using webct. Only one submission is required per group. The group leader for the assignment should submit the assignment. Submit a gzipped tar file, called A2.tar.gz (the tar file should contain a directory called A7 which contains the files). The tar file should contain source code, instructions how to run your programs, sample input and output files, and a README file. The README file should describe all files that are included, contain instructions how to build and use the code, and outline how the code works. The README file should also contain a list of all group members and the group leader for the submitted assignment. You should also indicate how you tested your code. If your program is not working, you should clearly state this in the README file. Code should be documented (clear specifications and comments for any tricky parts of the code). Those questions that involve hand computations or proofs should be submitted as pdf or text files whose name indicates the question.