Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
prefer the stack to the heap (+ don't refill the host list if already full)
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 9 Mar 2017 11:04:02 +0000 (12:04 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 9 Mar 2017 11:04:02 +0000 (12:04 +0100)
include/simgrid/s4u/NetZone.hpp
src/s4u/s4u_netzone.cpp

index 38cfab2..c05a251 100644 (file)
@@ -40,7 +40,7 @@ protected:
 
   explicit NetZone(NetZone * father, const char* name);
   virtual ~NetZone();
-  std::vector<Host*>* hosts_ = new std::vector<Host*>();
+  std::vector<Host*> hosts_;
 
 public:
   /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
index 780baad..0a343db 100644 (file)
@@ -38,7 +38,6 @@ NetZone::~NetZone()
     delete static_cast<NetZone*>(elem);
   }
 
-  delete hosts_;
   xbt_dict_free(&children_);
   xbt_free(name_);
 }
@@ -76,12 +75,13 @@ NetZone* NetZone::father()
 
 std::vector<s4u::Host*>* NetZone::hosts()
 {
-  for (auto card : vertices_) {
-    s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name());
-    if (host != nullptr)
-      hosts_->push_back(host);
-  }
-  return hosts_;
+  if (hosts_.empty()) // Lazy initialization
+    for (auto card : vertices_) {
+      s4u::Host* host = simgrid::s4u::Host::by_name_or_null(card->name());
+      if (host != nullptr)
+        hosts_.push_back(host);
+    }
+  return &hosts_;
 }
 
 int NetZone::addComponent(kernel::routing::NetPoint* elm)