GC
The simplest way to integrate a GC in a language seems to be the Boehm garbage collector.
GC types:
- Conservative and precise (accurate)
- Tracing and direct
Tools
- Valgrind can be used to check for leaks:
valgrind --leak-check=yes ./program
- Xcode is bundled with
leaks
utility:leaks --atExit -- ./program
Intro
- Writing a Mark-Sweep Garbage Collector – Dmitry Soshnikov (2020) – GC using the actual process stack
- My fork that runs on Apple silicon
- Baby’s First Garbage Collector by Bob Nystrom (2013) – GC for a virtual machine
- Writing a Simple Garbage Collector in C (2020) – GC with a custom allocator
Advanced
LLVM
In particular, LLVM provides support for generating stack maps at call sites, polling for a safepoint, and emitting load and store barriers.
Specifying the GC style on a per-function basis allows LLVM to link together programs that use different garbage collection algorithms (or none at all).
All calls to
llvm.gcroot
must reside inside the first basic block.
It is also important to mark intermediate values with
llvm.gcroot
. For example, considerh(f(), g())
. Beware leaking the result off()
in the case thatg()
triggers a collection. Note, that stack variables must be initialized and marked withllvm.gcroot
in function’s prologue.