How to schedule collection cycles for custom mark-sweep collector?

后端 未结 2 404
后悔当初
后悔当初 2021-01-14 04:06

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

2条回答
  •  [愿得一人]
    2021-01-14 04:57

    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);
    }
    

提交回复
热议问题