diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2018-01-25 09:57:02 -0500 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2018-01-25 09:57:02 -0500 |
commit | 7e7723e98f1d9d9a6f85665cc87543e7e37ac47c (patch) | |
tree | 88574aa4da3fe1ce588017d51491538863ed3d10 /node | |
parent | 4419734a7db21cd6a9ee8aba911f220cbecba085 (diff) | |
download | infinitytier-7e7723e98f1d9d9a6f85665cc87543e7e37ac47c.tar.gz infinitytier-7e7723e98f1d9d9a6f85665cc87543e7e37ac47c.zip |
A small memory use reduction.
Diffstat (limited to 'node')
-rw-r--r-- | node/Node.cpp | 53 | ||||
-rw-r--r-- | node/RuntimeEnvironment.hpp | 4 |
2 files changed, 40 insertions, 17 deletions
diff --git a/node/Node.cpp b/node/Node.cpp index 46081adb..c95e884b 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -107,18 +107,36 @@ Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64 } } + char *m = (char *)0; try { - RR->t = new Trace(RR); - RR->sw = new Switch(RR); - RR->mc = new Multicaster(RR); - RR->topology = new Topology(RR,tptr); - RR->sa = new SelfAwareness(RR); + const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0); + const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0); + const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0); + const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0); + 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 ( ... ) { - delete RR->sa; - delete RR->topology; - delete RR->mc; - delete RR->sw; - delete RR->t; + if (RR->sa) RR->sa->~SelfAwareness(); + if (RR->topology) RR->topology->~Topology(); + if (RR->mc) RR->mc->~Multicaster(); + if (RR->sw) RR->sw->~Switch(); + if (RR->t) RR->t->~Trace(); + ::free(m); throw; } @@ -131,11 +149,12 @@ Node::~Node() Mutex::Lock _l(_networks_m); _networks.clear(); // destroy all networks before shutdown } - delete RR->sa; - delete RR->topology; - delete RR->mc; - delete RR->sw; - delete RR->t; + if (RR->sa) RR->sa->~SelfAwareness(); + if (RR->topology) RR->topology->~Topology(); + if (RR->mc) RR->mc->~Multicaster(); + if (RR->sw) RR->sw->~Switch(); + if (RR->t) RR->t->~Trace(); + ::free(RR->rtmem); } 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 - // 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; { Mutex::Lock l(_networks_m); @@ -308,7 +327,7 @@ ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64 timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck; } - if ((now - _lastMemoizedTraceSettings) >= 10000) { + if ((now - _lastMemoizedTraceSettings) >= (ZT_HOUSEKEEPING_PERIOD / 4)) { _lastMemoizedTraceSettings = now; RR->t->updateMemoizedSettings(); } diff --git a/node/RuntimeEnvironment.hpp b/node/RuntimeEnvironment.hpp index 17170718..46350b4a 100644 --- a/node/RuntimeEnvironment.hpp +++ b/node/RuntimeEnvironment.hpp @@ -53,6 +53,7 @@ public: RuntimeEnvironment(Node *n) : node(n) ,localNetworkController((NetworkController *)0) + ,rtmem((void *)0) ,sw((Switch *)0) ,mc((Multicaster *)0) ,topology((Topology *)0) @@ -73,6 +74,9 @@ public: // This is set externally to an instance of this base class NetworkController *localNetworkController; + // Memory actually occupied by Trace, Switch, etc. + void *rtmem; + /* Order matters a bit here. These are constructed in this order * and then deleted in the opposite order on Node exit. The order ensures * that things that are needed are there before they're needed. |