From: Thomas Geymayer Date: Mon, 14 Oct 2013 22:19:32 +0000 (+0200) Subject: Nasal: add naGCSave/naGCRelease for preventing objects being garbage collected. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bb82b9d168239dd4dc0ab93b338143a25ed5ef51;p=simgear.git 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. --- diff --git a/simgear/nasal/code.c b/simgear/nasal/code.c index 0ce9b053..bac8b92b 100644 --- a/simgear/nasal/code.c +++ b/simgear/nasal/code.c @@ -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,12 +767,25 @@ 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); - naFreeContext(c); + globals->save_hash = naNewHash(c); + naFreeContext(c); } int naStackDepth(naContext ctx) diff --git a/simgear/nasal/code.h b/simgear/nasal/code.h index 3db61077..4ff27afc 100644 --- a/simgear/nasal/code.h +++ b/simgear/nasal/code.h @@ -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; diff --git a/simgear/nasal/gc.c b/simgear/nasal/gc.c index c3f1e660..a965aa79 100644 --- a/simgear/nasal/gc.c +++ b/simgear/nasal/gc.c @@ -54,6 +54,7 @@ static void garbageCollect() } mark(globals->save); + mark(globals->save_hash); mark(globals->symbols); mark(globals->meRef); mark(globals->argRef); diff --git a/simgear/nasal/nasal.h b/simgear/nasal/nasal.h index e57c856b..0464c94c 100644 --- a/simgear/nasal/nasal.h +++ b/simgear/nasal/nasal.h @@ -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();