Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Don't free power_peak from the caller
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 24 Nov 2015 16:11:21 +0000 (17:11 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Wed, 25 Nov 2015 08:15:03 +0000 (09:15 +0100)
The code was giving the power_peak array to the callee. The callers
might still need the power_peak. Freeing it in the callee creates
dangling pointers in the callers.

Instead we consider power_peak as immutable in the callee and make a
private copy when needed instead of stealing it from the callers.

examples/msg/token_ring/token_bypass.c
src/bindings/java/org/simgrid/surf/surfJAVA_wrap.cxx
src/bindings/lua/lua_platf.c
src/surf/cpu_cas01.cpp
src/surf/cpu_ti.cpp
src/surf/host_ptask_L07.cpp
src/surf/surf_routing.cpp
src/surf/surfxml_parse.c

index c25f43d..eb1f655 100644 (file)
@@ -70,12 +70,14 @@ static int surf_parse_bypass_platform(void)
   bob.power_peak = xbt_dynar_new(sizeof(double), NULL);
   xbt_dynar_push_as(bob.power_peak, double, 98095000.0);
   sg_platf_new_host(&bob);
+  xbt_dynar_free(&bob.power_peak);
 
   s_sg_platf_host_cbarg_t alice = SG_PLATF_HOST_INITIALIZER;
   alice.id = "alice";
   alice.power_peak = xbt_dynar_new(sizeof(double), NULL);
   xbt_dynar_push_as(alice.power_peak, double, 98095000.0);
   sg_platf_new_host(&alice);
+  xbt_dynar_free(&alice.power_peak);
 
   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
   link.id = "link1";
index c0feca4..1633eb0 100644 (file)
@@ -1568,9 +1568,8 @@ Cpu *SwigDirector_CpuModel::createCpu(char const *name, DoubleDynar power_peak,
     
     long l = xbt_dynar_length(power_peak);
     jpower_peak = jenv->NewDoubleArray(l);
-    double *lout = (double *)xbt_dynar_to_array(power_peak);
+    double *lout = (double *) xbt_dynar_get_ptr(power_peak, 0);
     jenv->SetDoubleArrayRegion(jpower_peak, 0, l, (jdouble*)lout);
-    free(lout);
     
     jpstate = (jint) pstate;
     jpower_scale = (jdouble) power_scale;
index 21be518..79f81fb 100644 (file)
@@ -248,6 +248,7 @@ int console_add_host(lua_State *L) {
   lua_pop(L, 1);
 
   sg_platf_new_host(&host);
+  xbt_dynar_free(&host.power_peak);
 
   return 0;
 }
index a4579f2..3d94de9 100644 (file)
@@ -167,7 +167,15 @@ CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t powerPeak
          core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale,
     stateInitial) {
   p_powerEvent = NULL;
-  p_powerPeakList = powerPeak;
+
+  // Copy the power peak array:
+  p_powerPeakList = xbt_dynar_new(sizeof(double), nullptr);
+  unsigned long n = xbt_dynar_length(powerPeak);
+  for (unsigned long i = 0; i != n; ++i) {
+    double value = xbt_dynar_get_as(powerPeak, i, double);
+    xbt_dynar_push(p_powerPeakList, &value);
+  }
+
   m_pstate = pstate;
 
   XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
index 976a85c..6d2ad3e 100644 (file)
@@ -562,7 +562,6 @@ CpuTi::CpuTi(CpuTiModel *model, const char *name, xbt_dynar_t powerPeak,
   m_lastUpdate = 0;
 
   xbt_dynar_get_cpy(powerPeak, 0, &m_powerPeak);
-  xbt_dynar_free(&powerPeak);  /* kill memory leak */
   XBT_DEBUG("CPU create: peak=%f", m_powerPeak);
 
   if (stateTrace)
index 0361890..d7ff6f3 100644 (file)
@@ -317,7 +317,6 @@ Cpu *CpuL07Model::createCpu(const char *name,  xbt_dynar_t powerPeak,
                           xbt_dict_t cpu_properties)
 {
   double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
-  xbt_dynar_free(&powerPeak);   // kill memory leak
   sg_host_t sg_host = sg_host_by_name(name);
 
   xbt_assert(!surf_host_resource_priv(sg_host),
index 1a63ab6..97fa809 100644 (file)
@@ -770,10 +770,10 @@ static void routing_parse_cabinet(sg_platf_cabinet_cbarg_t cabinet)
       link_id                      = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
       host.id                      = host_id;
       link.id                      = link_id;
-      xbt_dynar_t power_state_list = xbt_dynar_new(sizeof(double), NULL);
-      xbt_dynar_push(power_state_list,&cabinet->power);
-      host.power_peak = power_state_list;
+      host.power_peak = xbt_dynar_new(sizeof(double), NULL);
+      xbt_dynar_push(host.power_peak,&cabinet->power);
       sg_platf_new_host(&host);
+      xbt_dynar_free(&host.power_peak);
       sg_platf_new_link(&link);
 
       char* link_up       = bprintf("%s_UP",link_id);
@@ -912,9 +912,8 @@ static void routing_parse_cluster(sg_platf_cluster_cbarg_t cluster)
         XBT_DEBUG("\tstate_file=\"\"");
       }
 
-      xbt_dynar_t power_state_list = xbt_dynar_new(sizeof(double), NULL);
-      xbt_dynar_push(power_state_list,&cluster->power);
-      host.power_peak = power_state_list;
+      host.power_peak = xbt_dynar_new(sizeof(double), NULL);
+      xbt_dynar_push(host.power_peak,&cluster->power);
       host.pstate = 0;
 
       //host.power_peak = cluster->power;
@@ -923,6 +922,7 @@ static void routing_parse_cluster(sg_platf_cluster_cbarg_t cluster)
       host.initial_state = SURF_RESOURCE_ON;
       host.coord = "";
       sg_platf_new_host(&host);
+      xbt_dynar_free(&host.power_peak);
       XBT_DEBUG("</host>");
 
       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,
@@ -1076,9 +1076,8 @@ static void routing_parse_peer(sg_platf_peer_cbarg_t peer)
   host.initial_state = SURF_RESOURCE_ON;
   host.id = host_id;
 
-  xbt_dynar_t power_state_list = xbt_dynar_new(sizeof(double), NULL);
-  xbt_dynar_push(power_state_list,&peer->power);
-  host.power_peak = power_state_list;
+  host.power_peak = xbt_dynar_new(sizeof(double), NULL);
+  xbt_dynar_push(host.power_peak,&peer->power);
   host.pstate = 0;
   //host.power_peak = peer->power;
   host.power_scale = 1.0;
@@ -1086,6 +1085,7 @@ static void routing_parse_peer(sg_platf_peer_cbarg_t peer)
   host.state_trace = peer->state_trace;
   host.core_amount = 1;
   sg_platf_new_host(&host);
+  xbt_dynar_free(&host.power_peak);
 
   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
   memset(&link, 0, sizeof(link));
index 7904fcb..a7b0705 100644 (file)
@@ -479,6 +479,7 @@ void ETag_surfxml_host(void)    {
   host.coord = A_surfxml_host_coordinates;
 
   sg_platf_new_host(&host);
+  xbt_dynar_free(&host.power_peak);
   current_property_set = NULL;
 }