-
-
Save Dietr1ch/a636578fe9f06faa5d46e1b78aa7ba4e to your computer and use it in GitHub Desktop.
Baby’s First Garbage Collector - Sweep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sweep(VM* vm) { | |
Object* object = vm->firstObject; // vm->firstObject might be a dangling pointer after this :/ | |
while (object) { | |
if (!object->marked) { | |
/* This object wasn't reached, so remove it from the list | |
and free it. */ | |
Object* unreached = object; | |
object = unreached->next; | |
free(unreached); | |
} else { | |
/* This object was reached, so unmark it (for the next GC) | |
and move on to the next. */ | |
object->marked = 0; | |
object = object->next; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment