Nasal: add naGCSave/naGCRelease for preventing objects being garbage collected.

These new functions are meant to replace the gcSave/gcRelease
methods of the NasalSystem class in FlightGear, as passing an
adapter to SimGear from FlightGear is quite a lot of useless work
just for being able to save objects.
This commit is contained in:
Thomas Geymayer 2013-10-15 00:19:32 +02:00
parent b1f865d461
commit bb82b9d168
4 changed files with 30 additions and 1 deletions

View File

@ -180,6 +180,8 @@ static void initGlobals()
globals->symbols = naNewHash(c);
globals->save = naNewVector(c);
globals->save_hash = naNewHash(c);
globals->next_gc_key = 0;
// Cache pre-calculated "me", "arg" and "parents" scalars
globals->meRef = naInternSymbol(naStr_fromdata(naNewString(c), "me", 2));
@ -765,11 +767,24 @@ void naSave(naContext ctx, naRef obj)
naVec_append(globals->save, obj);
}
int naGCSave(naRef obj)
{
int key = globals->next_gc_key++;
naHash_set(globals->save_hash, naNum(key), obj);
return key;
}
void naGCRelease(int key)
{
naHash_delete(globals->save_hash, naNum(key));
}
void naClearSaved()
{
naContext c;
c = naNewContext();
globals->save = naNewVector(c);
globals->save_hash = naNewHash(c);
naFreeContext(c);
}

View File

@ -62,7 +62,11 @@ struct Globals {
// A hash of symbol names
naRef symbols;
// Vector/hash containing objects which should not be freed by the gc
// TODO do we need a separate vector and hash?
naRef save;
naRef save_hash;
int next_gc_key;
struct Context* freeContexts;
struct Context* allContexts;

View File

@ -54,6 +54,7 @@ static void garbageCollect()
}
mark(globals->save);
mark(globals->save_hash);
mark(globals->symbols);
mark(globals->meRef);
mark(globals->argRef);

View File

@ -47,8 +47,17 @@ void naGC();
// "Save" this object in the context, preventing it (and objects
// referenced by it) from being garbage collected.
// TODO do we need a context? It is not used anyhow...
void naSave(naContext ctx, naRef obj);
// "Save" this object and get a key which allows do mark the object as free
// later on (with naGCFree).
int naGCSave(naRef obj);
// Release an object previously passed to naGCSave to allow it being cleaned up
// by the garbage collector.
void naGCRelease(int key);
// Drop all saved references
void naClearSaved();