I\'ve written a simple garbage collector for a Postscript virtual machine, and I\'m having difficulty designing a decent set of rules for when to do a collection (when the f
Here is the periodic collection strategy incorporated into the original code:
enum { PERIOD = 10 };
unsigned gballoc(mfile *mem, unsigned sz) {
unsigned z = adrent(mem, FREE);
unsigned e;
static period = PERIOD;
memcpy(&e, mem->base+z, sizeof(e));
try_again:
while (e) {
if (szent(mem,e) >= sz) {
memcpy(mem->base+z, mem->base+adrent(mem,e), sizeof(unsigned));
return e;
}
z = adrent(mem,e);
memcpy(&e, mem->base+z, sizeof(e));
}
if (--period == 0) {
period = PERIOD;
collect(mem, 0);
goto try_again;
}
return mtalloc(mem, 0, sz);
}