How the Nasal GC works: Difference between revisions

Jump to navigation Jump to search
m
Line 221: Line 221:
In line 209, the memory region from the global pool is cast back to a pointer (struct naObj**): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line209
In line 209, the memory region from the global pool is cast back to a pointer (struct naObj**): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line209
the "nout" paramter is just there to update c->nfree[type] with the amount of free memory.
the "nout" paramter is just there to update c->nfree[type] with the amount of free memory.
== Memory blocks ==
A Nasal memory pool consists of memory blocks.
A memory block is a singly linked list: https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line10
<syntaxhighlight lang="C">
struct Block {
    int  size;
    char* block;
    struct Block* next;
};
</syntaxhighlight>
New memory blocks are allocated using newBlock(): https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/gc.c#line150
<syntaxhighlight lang="C">
static void newBlock(struct naPool* p, int need)
{
    int i;
    struct Block* newb;
    if(need < MIN_BLOCK_SIZE) need = MIN_BLOCK_SIZE;
    newb = naAlloc(sizeof(struct Block));
    newb->block = naAlloc(need * p->elemsz);
    newb->size = need;
    newb->next = p->blocks;
    p->blocks = newb;
    naBZero(newb->block, need * p->elemsz);
    if(need > p->freesz - p->freetop) need = p->freesz - p->freetop;
    p->nfree = 0;
    p->free = p->free0 + p->freetop;
    for(i=0; i < need; i++) {
        struct naObj* o = (struct naObj*)(newb->block + i*p->elemsz);
        o->mark = 0;
        p->free[p->nfree++] = o;
    }
    p->freetop += need;
}
</syntaxhighlight>
All core memory allocation takes place via wrappers in [https://gitorious.org/fg/simgear/blobs/next/simgear/nasal/misc.c#line8 misc.c]:
<syntaxhighlight lang="C">
void naFree(void* m) { free(m); }
void* naAlloc(int n) { return malloc(n); }
void* naRealloc(void* b, int n) { return realloc(b, n); }
void naBZero(void* m, int n) { memset(m, 0, n); }
</syntaxhighlight>


= The GC interface =
= The GC interface =

Navigation menu