In this project I want to revamp Lush's memory management from scratch. Most of the work will be spent on modifying the compiler and the language runtime to work with a new garbage collector. The immediate benefit will be to Lush users, as automatic memory management will work the same way in compiled code as in the interpreter. Long-term benefits to the Lush project result through simplification of the compiler, which will make it much easier to add new language features.
There currently are two independent aspects in Lush's memory management that makes life difficult for programmers. The first is that there is no automatic memory management for objects created outside the interpreter. As a consequence, explicit interface code to third-party libraries need to be written to ensure that opaque data structures returned from a library function will be released when no longer referenced.
The other aspect is that the Lush-to-C compiler performs escape analysis and allocates objects on the stack whenever possible. Because of this, C functions generated by the compiler often have lots of hidden arguments through which memory for temporary variables or components of the return value are passed to the function. This is problematic when functions are to be used as arguments to other functions, or when methods are overloaded in subclasses (the compiler quits with an error when there is a mismatch in number of hidden arguments).
Existing code that uses manually managed memory (i.e., malloc & free) will continue to work as before. However, a new facility will allow to register memory allocated in this way with the runtime. Part of this new facility will be a new pointer type "managed pointer". The API for externally allocated objects will take a conventional (unmanaged) pointer and a destructor function and will return a managed pointer.
It is not my plan to design and implement a new garbage collector from scratch, but to copy from an existing system. Nickle uses an elegant scheme for integrating automatic memory management in C code. I plan to cannibalize the implementation of the Nickel garbage collector and to adopt the scheme of keeping track of transient memory with a global stack of root pointers. I also plan to improve on Nickel's system by running the collector in a separate process (thanks to Keith Packard for the idea), and by adding language support for managed pointers as outlined above.
(The first two weeks of SoC overlap with the end of Spring term at my school. During that time I may not be able to spend more than three days per week on the project due to other commitments.)