Skip to content

Instantly share code, notes, and snippets.

@Dietr1ch
Last active August 10, 2022 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dietr1ch/a636578fe9f06faa5d46e1b78aa7ba4e to your computer and use it in GitHub Desktop.
Save Dietr1ch/a636578fe9f06faa5d46e1b78aa7ba4e to your computer and use it in GitHub Desktop.
Baby’s First Garbage Collector - Sweep
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