How the Nasal GC works: Difference between revisions

Jump to navigation Jump to search
m
Line 332: Line 332:




= reap() =
This is the collector function called by garbageCollect() which also frees elements and allocates new blocks:
<syntaxhighlight lang="C">
// Collects all the unreachable objects into a free list, and
// allocates more space if needed.
static void reap(struct naPool* p)
{
    struct Block* b;
    int elem, freesz, total = poolsize(p);
    freesz = total < MIN_BLOCK_SIZE ? MIN_BLOCK_SIZE : total;
    freesz = (3 * freesz / 2) + (globals->nThreads * OBJ_CACHE_SZ);
    if(p->freesz < freesz) {
        naFree(p->free0);
        p->freesz = freesz;
        p->free = p->free0 = naAlloc(sizeof(void*) * p->freesz);
    }
    p->nfree = 0;
    p->free = p->free0;
    for(b = p->blocks; b; b = b->next)
        for(elem=0; elem < b->size; elem++) {
            struct naObj* o = (struct naObj*)(b->block + elem * p->elemsz);
            if(o->mark == 0)
                freeelem(p, o);
            o->mark = 0;
        }
    p->freetop = p->nfree;
    // allocs of this type until the next collection
    globals->allocCount += total/2;
    // Allocate more if necessary (try to keep 25-50% of the objects
    // available)
    if(p->nfree < total/4) {
        int used = total - p->nfree;
        int avail = total - used;
        int need = used/2 - avail;
        if(need > 0)
            newBlock(p, need);
    }
}
</syntaxhighlight>
= Contexts =
= Contexts =
https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/code.h#line71
https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/code.h#line71
Line 401: Line 442:
};
};
</syntaxhighlight>
</syntaxhighlight>


= Stack frames =
= Stack frames =

Navigation menu