Save a memory allocation each time a property node is wrapped in Nasal. Instead use a direct access to the reference counter and explicitely delete the node if the ref counter is decremented to zero during the ghost destruction.
This commit is contained in:
parent
89115ccb1d
commit
2122e9eee3
2 changed files with 23 additions and 25 deletions
|
@ -458,14 +458,13 @@ static naRef f_fgcommand(naContext c, naRef me, int argc, naRef* args)
|
|||
naRef props = argc > 1 ? args[1] : naNil();
|
||||
if(!naIsString(cmd) || (!naIsNil(props) && !naIsGhost(props)))
|
||||
naRuntimeError(c, "bad arguments to fgcommand()");
|
||||
SGPropertyNode_ptr tmp, *node;
|
||||
SGPropertyNode_ptr node;
|
||||
if(!naIsNil(props))
|
||||
node = (SGPropertyNode_ptr*)naGhost_ptr(props);
|
||||
else {
|
||||
tmp = new SGPropertyNode();
|
||||
node = &tmp;
|
||||
}
|
||||
return naNum(globals->get_commands()->execute(naStr_data(cmd), *node));
|
||||
node = static_cast<SGPropertyNode*>(naGhost_ptr(props));
|
||||
else
|
||||
node = new SGPropertyNode;
|
||||
|
||||
return naNum(globals->get_commands()->execute(naStr_data(cmd), node));
|
||||
}
|
||||
|
||||
// settimer(func, dt, simtime) extension function. Falls through to
|
||||
|
@ -516,7 +515,7 @@ static naRef f_cmdarg(naContext c, naRef me, int argc, naRef* args)
|
|||
}
|
||||
|
||||
// Sets up a property interpolation. The first argument is either a
|
||||
// ghost (SGPropertyNode_ptr*) or a string (global property path) to
|
||||
// ghost (SGPropertyNode*) or a string (global property path) to
|
||||
// interpolate. The second argument is a vector of pairs of
|
||||
// value/delta numbers.
|
||||
static naRef f_interpolate(naContext c, naRef me, int argc, naRef* args)
|
||||
|
@ -524,7 +523,7 @@ static naRef f_interpolate(naContext c, naRef me, int argc, naRef* args)
|
|||
SGPropertyNode* node;
|
||||
naRef prop = argc > 0 ? args[0] : naNil();
|
||||
if(naIsString(prop)) node = fgGetNode(naStr_data(prop), true);
|
||||
else if(naIsGhost(prop)) node = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
|
||||
else if(naIsGhost(prop)) node = static_cast<SGPropertyNode*>(naGhost_ptr(prop));
|
||||
else return naNil();
|
||||
|
||||
naRef curve = argc > 1 ? args[1] : naNil();
|
||||
|
@ -1341,7 +1340,7 @@ int FGNasalSys::_listenerId = 0;
|
|||
|
||||
// setlistener(<property>, <func> [, <initial=0> [, <persistent=1>]])
|
||||
// Attaches a callback function to a property (specified as a global
|
||||
// property path string or a SGPropertyNode_ptr* ghost). If the third,
|
||||
// property path string or a SGPropertyNode* ghost). If the third,
|
||||
// optional argument (default=0) is set to 1, then the function is also
|
||||
// called initially. If the fourth, optional argument is set to 0, then the
|
||||
// function is only called when the property node value actually changes.
|
||||
|
@ -1354,7 +1353,7 @@ naRef FGNasalSys::setListener(naContext c, int argc, naRef* args)
|
|||
SGPropertyNode_ptr node;
|
||||
naRef prop = argc > 0 ? args[0] : naNil();
|
||||
if(naIsString(prop)) node = fgGetNode(naStr_data(prop), true);
|
||||
else if(naIsGhost(prop)) node = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
|
||||
else if(naIsGhost(prop)) node = static_cast<SGPropertyNode*>(naGhost_ptr(prop));
|
||||
else {
|
||||
naRuntimeError(c, "setlistener() with invalid property argument");
|
||||
return naNil();
|
||||
|
|
|
@ -26,17 +26,17 @@ using namespace std;
|
|||
|
||||
static void propNodeGhostDestroy(void* ghost)
|
||||
{
|
||||
SGPropertyNode_ptr* prop = (SGPropertyNode_ptr*)ghost;
|
||||
delete prop;
|
||||
SGPropertyNode* prop = static_cast<SGPropertyNode*>(ghost);
|
||||
if (!SGPropertyNode::put(prop)) delete prop;
|
||||
}
|
||||
|
||||
naGhostType PropNodeGhostType = { propNodeGhostDestroy, "prop" };
|
||||
|
||||
naRef propNodeGhostCreate(naContext c, SGPropertyNode* n)
|
||||
naRef propNodeGhostCreate(naContext c, SGPropertyNode* ghost)
|
||||
{
|
||||
if(!n) return naNil();
|
||||
SGPropertyNode_ptr* ghost = new SGPropertyNode_ptr(n);
|
||||
return naNewGhost(c, &PropNodeGhostType, ghost);
|
||||
if(!ghost) return naNil();
|
||||
SGPropertyNode::get(ghost);
|
||||
return naNewGhost(c, &PropNodeGhostType, ghost);
|
||||
}
|
||||
|
||||
naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
|
||||
|
@ -49,15 +49,14 @@ SGPropertyNode* ghostToPropNode(naRef ref)
|
|||
if (!naIsGhost(ref) || (naGhost_type(ref) != &PropNodeGhostType))
|
||||
return NULL;
|
||||
|
||||
SGPropertyNode_ptr* pp = (SGPropertyNode_ptr*) naGhost_ptr(ref);
|
||||
return pp->ptr();
|
||||
return static_cast<SGPropertyNode*>(naGhost_ptr(ref));
|
||||
}
|
||||
|
||||
#define NASTR(s) s ? naStr_fromdata(naNewString(c),(char*)(s),strlen(s)) : naNil()
|
||||
|
||||
//
|
||||
// Standard header for the extension functions. It turns the "ghost"
|
||||
// found in arg[0] into a SGPropertyNode_ptr*, and then "unwraps" the
|
||||
// found in arg[0] into a SGPropertyNode_ptr, and then "unwraps" the
|
||||
// vector found in the second argument into a normal-looking args
|
||||
// array. This allows the Nasal handlers to do things like:
|
||||
// Node.getChild = func { _getChild(me.ghost, arg) }
|
||||
|
@ -66,7 +65,7 @@ SGPropertyNode* ghostToPropNode(naRef ref)
|
|||
if(argc < 2 || !naIsGhost(args[0]) || \
|
||||
naGhost_type(args[0]) != &PropNodeGhostType) \
|
||||
naRuntimeError(c, "bad argument to props function"); \
|
||||
SGPropertyNode_ptr node = *(SGPropertyNode_ptr*)naGhost_ptr(args[0]);
|
||||
SGPropertyNode_ptr node = static_cast<SGPropertyNode*>(naGhost_ptr(args[0]));
|
||||
|
||||
#define NODEARG() \
|
||||
NODENOARG(); \
|
||||
|
@ -231,8 +230,8 @@ static naRef f_equals(naContext c, naRef me, int argc, naRef* args)
|
|||
if( !naIsGhost(rhs) || naGhost_type(rhs) != &PropNodeGhostType )
|
||||
return naNum(false);
|
||||
|
||||
SGPropertyNode_ptr node_rhs = *(SGPropertyNode_ptr*)naGhost_ptr(rhs);
|
||||
return naNum(node == node_rhs);
|
||||
SGPropertyNode* node_rhs = static_cast<SGPropertyNode*>(naGhost_ptr(rhs));
|
||||
return naNum(node.ptr() == node_rhs);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -535,7 +534,7 @@ static naRef f_removeChild(naContext c, naRef me, int argc, naRef* args)
|
|||
naRef child = naVec_get(argv, 0);
|
||||
naRef index = naVec_get(argv, 1);
|
||||
if(!naIsString(child) || !naIsNum(index)) return naNil();
|
||||
SGPropertyNode_ptr n = 0;
|
||||
SGPropertyNode_ptr n;
|
||||
try {
|
||||
n = node->removeChild(naStr_data(child), (int)index.num);
|
||||
} catch (const string& err) {
|
||||
|
@ -598,7 +597,7 @@ static naRef f_alias(naContext c, naRef me, int argc, naRef* args)
|
|||
naRef prop = naVec_get(argv, 0);
|
||||
try {
|
||||
if(naIsString(prop)) al = globals->get_props()->getNode(naStr_data(prop), true);
|
||||
else if(naIsGhost(prop)) al = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
|
||||
else if(naIsGhost(prop)) al = static_cast<SGPropertyNode*>(naGhost_ptr(prop));
|
||||
else throw string("props.alias() with bad argument");
|
||||
} catch (const string& err) {
|
||||
naRuntimeError(c, (char *)err.c_str());
|
||||
|
|
Loading…
Add table
Reference in a new issue