A small memory use reduction.

This commit is contained in:
Adam Ierymenko 2018-01-25 09:57:02 -05:00
parent 4419734a7d
commit 7e7723e98f
2 changed files with 40 additions and 17 deletions

View file

@ -107,18 +107,36 @@ Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64
} }
} }
char *m = (char *)0;
try { try {
RR->t = new Trace(RR); const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0);
RR->sw = new Switch(RR); const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0);
RR->mc = new Multicaster(RR); const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0);
RR->topology = new Topology(RR,tptr); const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0);
RR->sa = new SelfAwareness(RR); const unsigned long sas = sizeof(SelfAwareness) + (((sizeof(SelfAwareness) & 0xf) != 0) ? (16 - (sizeof(SelfAwareness) & 0xf)) : 0);
m = reinterpret_cast<char *>(::malloc(16 + ts + sws + mcs + topologys + sas));
if (!m)
throw std::bad_alloc();
RR->rtmem = m;
while (((uintptr_t)m & 0xf) != 0) ++m;
RR->t = new (m) Trace(RR);
m += ts;
RR->sw = new (m) Switch(RR);
m += sws;
RR->mc = new (m) Multicaster(RR);
m += mcs;
RR->topology = new (m) Topology(RR,tptr);
m += topologys;
RR->sa = new (m) SelfAwareness(RR);
} catch ( ... ) { } catch ( ... ) {
delete RR->sa; if (RR->sa) RR->sa->~SelfAwareness();
delete RR->topology; if (RR->topology) RR->topology->~Topology();
delete RR->mc; if (RR->mc) RR->mc->~Multicaster();
delete RR->sw; if (RR->sw) RR->sw->~Switch();
delete RR->t; if (RR->t) RR->t->~Trace();
::free(m);
throw; throw;
} }
@ -131,11 +149,12 @@ Node::~Node()
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
_networks.clear(); // destroy all networks before shutdown _networks.clear(); // destroy all networks before shutdown
} }
delete RR->sa; if (RR->sa) RR->sa->~SelfAwareness();
delete RR->topology; if (RR->topology) RR->topology->~Topology();
delete RR->mc; if (RR->mc) RR->mc->~Multicaster();
delete RR->sw; if (RR->sw) RR->sw->~Switch();
delete RR->t; if (RR->t) RR->t->~Trace();
::free(RR->rtmem);
} }
ZT_ResultCode Node::processWirePacket( ZT_ResultCode Node::processWirePacket(
@ -263,7 +282,7 @@ ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64
} }
// Get peers we should stay connected to according to network configs // Get peers we should stay connected to according to network configs
// Also get networks and whether they need config // Also get networks and whether they need config so we only have to do one pass over networks
std::vector< std::pair< SharedPtr<Network>,bool > > networkConfigNeeded; std::vector< std::pair< SharedPtr<Network>,bool > > networkConfigNeeded;
{ {
Mutex::Lock l(_networks_m); Mutex::Lock l(_networks_m);
@ -308,7 +327,7 @@ ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64
timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck; timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck;
} }
if ((now - _lastMemoizedTraceSettings) >= 10000) { if ((now - _lastMemoizedTraceSettings) >= (ZT_HOUSEKEEPING_PERIOD / 4)) {
_lastMemoizedTraceSettings = now; _lastMemoizedTraceSettings = now;
RR->t->updateMemoizedSettings(); RR->t->updateMemoizedSettings();
} }

View file

@ -53,6 +53,7 @@ public:
RuntimeEnvironment(Node *n) : RuntimeEnvironment(Node *n) :
node(n) node(n)
,localNetworkController((NetworkController *)0) ,localNetworkController((NetworkController *)0)
,rtmem((void *)0)
,sw((Switch *)0) ,sw((Switch *)0)
,mc((Multicaster *)0) ,mc((Multicaster *)0)
,topology((Topology *)0) ,topology((Topology *)0)
@ -73,6 +74,9 @@ public:
// This is set externally to an instance of this base class // This is set externally to an instance of this base class
NetworkController *localNetworkController; NetworkController *localNetworkController;
// Memory actually occupied by Trace, Switch, etc.
void *rtmem;
/* Order matters a bit here. These are constructed in this order /* Order matters a bit here. These are constructed in this order
* and then deleted in the opposite order on Node exit. The order ensures * and then deleted in the opposite order on Node exit. The order ensures
* that things that are needed are there before they're needed. * that things that are needed are there before they're needed.