I implement mark-and-sweep garbage collection in a simple scripting API I am working on and read about various garbage collection implementations. An API such as Lua uses mark-and-sweep with white, gray and blacklists.
The problem is that I can’t find an explanation of why there are such lists and why they are placed in these specific colors.
In my current, trivial implementation, I just use “dead” or “living” states. In a sweep, dead objects are deleted. I use a native heap, so I am not making any changes to my GC.
I write it in C.
void GorCollect(GorContext *ctx)
{
Value *v, *end;
Collectable *c, *n;
end = ctx->stack + ctx->stackTop + 1;
v = ctx->stack;
while(v != end)
{
if (gvisgc(v) && v->v.gc)
Mark(v->v.gc);
v = v++;
}
if (ctx->global)
Mark((Collectable *)ctx->global);
c = ctx->gchead;
ctx->gchead = 0;
while(c) {
n = c->next;
if (!c->marked)
FreeCollectable(ctx, c);
else
{
c->marked = 0;
if (!ctx->gchead)
c->next = 0;
else
c->next = ctx->gchead;
ctx->gchead = c;
}
c = n;
}
}
source
share