Suppose you have some programming language with manual memory management. What features does this language need to have in order to be able to implement precise garbage collection as a library, and not as a fundamental language construct?
By a precise GC I mean one where only pointers to the heap are traversed to ascertain which variables are or are not live.
Some additional considerations:
- C and C++ have the Boehm garbage collector, but I don't count this since it's not a precise GC. The Boehm collector assumes that anything on the stack that could be a pointer, based purely on memory alignment requirements, is a pointer. For example, any integer
(k % 4) == 0looks at a bit level like a pointer, since pointers must be 4-byte aligned.
- magpie transforms existing C code to use a precise garbage collector. The generated C code has a lot of stubs for garbage collection, i.e. stuff for registering any stack pointers into the heap with the collector. I don't count this because no one could ever be expected to write code that way; it's more of a compilation target for other languages.
I imagine that such a language would need to have:
- Macros or some form of metaprogramming, for encapsulating all of the extra code needed to do things like register GC roots.
- Some reflective mechanism that allows you to inspect structs or unions; you need to determine which members are pointers.
- Some reflective mechanism that allows you to examine the stack frame layout. This sounds a lot harder than 2.
I hope this isn't too vague or opinion-based but I've been wondering about it for a while.