I feel like I am missing something painfully simple but I am trying to understand mark and sweep garbage collection per Andrew Appel’s Modern Compiler Implementation in ML book and there’s a small paragraph inside the Mark and Sweep section titled Pointer Reversal (270).
At this point I think I understand how it works. In a nutshell, as you traverse the graph you flip all the pointers so that your predecessor is inside your set of fields. Then when you are done with a given element, you flip the pointers back so they point at the right place again.
If that is correct, what exactly does it buy you? Appel attempts to explain this but I don’t fully grok his wording.
During marking, objects fall into three categories:
As marking proceeds, objects change state from category 1 to category 2, and from category 2 to category 3.
The garbage collector has to keep track of all objects in category 2 so that it can find all unmarked objects. But where does it store that information? Garbage collection may be running when memory is completely full, so it cannot dynamically allocate a data structure. It should build a data structure holding objects in category 2, using the memory that is already allocated. Pointer reversal is an algorithm for building a linked list of these objects without allocating memory.