Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug some memleaks in the new unit test
[simgrid.git] / src / surf / trace_mgr.cpp
index b9fdbb9..7fc1da1 100644 (file)
@@ -44,12 +44,17 @@ std::ostream& operator<<(std::ostream& out, const DatedValue& e)
   return out;
 }
 
-trace::trace()                   = default;
+trace::trace()
+{
+  /* Add the first fake event storing the time at which the trace begins */
+  tmgr::DatedValue val(0, -1);
+  event_list.push_back(val);
+}
 trace::~trace()                  = default;
 future_evt_set::future_evt_set() = default;
 simgrid::trace_mgr::future_evt_set::~future_evt_set()
 {
-  xbt_heap_free(p_heap);
+  xbt_heap_free(heap_);
 }
 }
 }
@@ -57,12 +62,11 @@ simgrid::trace_mgr::future_evt_set::~future_evt_set()
 tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity)
 {
   int linecount = 0;
-  tmgr::DatedValue* last_event;
+  tmgr_trace_t trace           = new simgrid::trace_mgr::trace();
+  tmgr::DatedValue* last_event = &(trace->event_list.back());
 
   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name);
 
-  tmgr_trace_t trace = new simgrid::trace_mgr::trace();
-
   std::vector<std::string> list;
   boost::split(list, input, boost::is_any_of("\n\r"));
   for (auto val : list) {
@@ -73,23 +77,17 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, dou
       continue;
     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
       continue;
-    if (sscanf(val.c_str(), "WAITFOR %lg\n", &periodicity) == 1)
+    if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
       continue;
 
     xbt_assert(sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
                name, linecount, input.c_str());
 
-    if (last_event) {
-      xbt_assert(last_event->date_ <= event.date_,
-                 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount,
-                 last_event->date_, event.date_, input.c_str());
+    xbt_assert(last_event->date_ <= event.date_,
+               "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount,
+               last_event->date_, event.date_, input.c_str());
+    last_event->date_ = event.date_ - last_event->date_;
 
-      last_event->date_ = event.date_ - last_event->date_;
-    } else {
-      /* Add the first fake event storing the time at which the trace begins */
-      tmgr::DatedValue first_event(event.date_, -1.0);
-      trace->event_list.push_back(first_event);
-    }
     trace->event_list.push_back(event);
     last_event = &(trace->event_list.back());
   }
@@ -116,25 +114,9 @@ tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
 
   std::stringstream buffer;
   buffer << f->rdbuf();
-  tmgr_trace_t trace = tmgr_trace_new_from_string(filename, buffer.str(), -1);
-
   delete f;
 
-  return trace;
-}
-
-tmgr_trace_t tmgr_empty_trace_new()
-{
-  tmgr_trace_t trace = new simgrid::trace_mgr::trace();
-  tmgr::DatedValue val(0, 0);
-  trace->event_list.push_back(val);
-
-  return trace;
-}
-
-void tmgr_trace_free(tmgr_trace_t trace)
-{
-  delete trace;
+  return tmgr_trace_new_from_string(filename, buffer.str(), -1);
 }
 
 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace  */
@@ -149,7 +131,7 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t tr
 
   xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!");
 
-  xbt_heap_push(p_heap, trace_iterator, 0. /*start_time*/);
+  xbt_heap_push(heap_, trace_iterator, 0. /*start_time*/);
 
   return trace_iterator;
 }
@@ -157,10 +139,9 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t tr
 /** @brief returns the date of the next occurring event (pure function) */
 double simgrid::trace_mgr::future_evt_set::next_date() const
 {
-  if (xbt_heap_size(p_heap))
-    return (xbt_heap_maxkey(p_heap));
-  else
-    return -1.0;
+  if (xbt_heap_size(heap_))
+    return (xbt_heap_maxkey(heap_));
+  return -1.0;
 }
 
 /** @brief Retrieves the next occurring event, or nullptr if none happens before #date */
@@ -171,7 +152,7 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, doub
   if (event_date > date)
     return nullptr;
 
-  tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(p_heap);
+  tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(heap_);
   if (trace_iterator == nullptr)
     return nullptr;
 
@@ -183,10 +164,10 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, doub
   *value = dateVal.value_;
 
   if (trace_iterator->idx < trace->event_list.size() - 1) {
-    xbt_heap_push(p_heap, trace_iterator, event_date + dateVal.date_);
+    xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
     trace_iterator->idx++;
   } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
-    xbt_heap_push(p_heap, trace_iterator, event_date + dateVal.date_);
+    xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
     trace_iterator->idx = 1; /* idx=0 is a placeholder to store when events really start */
   } else {                   /* If we don't loop, we don't need this trace_event anymore */
     trace_iterator->free_me = 1;
@@ -201,6 +182,7 @@ void tmgr_finalize()
     xbt_free((char*)kv.first);
     delete kv.second;
   }
+  trace_list.clear();
 }
 
 void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event)