How the Nasal GC works

From FlightGear wiki
Revision as of 15:05, 16 May 2012 by Hooray (talk | contribs) (Created page with "= Objective = Document how the existing Nasal GC works, so that it can be fully understood, before alternatives or additional implementations are added (feedback appreciated)....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Objective

Document how the existing Nasal GC works, so that it can be fully understood, before alternatives or additional implementations are added (feedback appreciated).

Memory allocation

All memory allocation takes place via wrappers in misc.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); }


Allocating Nasal types

Nasal types (scalars, vectors, hashes, funcs, ghosts) are allocated via wrappers in misc.c.

These wrappers are:


As can be seen, the key allocator function is naNew(). naNew() is really just a fancy malloc() replacement, i.e. it's just getting passed the size of the requested memory region.

The naNew() function is also implemented in misc.c, see line 66:

naRef naNew(struct Context* c, int type)	
{
    naRef result;
    if(c->nfree[type] == 0)
        c->free[type] = naGC_get(&globals->pools[type],
                                 OBJ_CACHE_SZ, &c->nfree[type]);
    result = naObj(type, c->free[type][--c->nfree[type]]);
    naTempSave(c, result);
    return result;
}

Pool storage

The interesting part is the naGC_get() function which isn't directly mapped to naAlloc() or malloc(), but instead manages the Nasal memory pools. For each native Nasal data type, there are separate memory pools in globals->[n]. The index (n) is one of T_VEC, T_HASH, T_FUNC etc.