From: Gabriel Corona Date: Tue, 24 Nov 2015 16:11:21 +0000 (+0100) Subject: [surf] Don't free power_peak from the caller X-Git-Tag: v3_13~1551 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b0f7d03396e875f79a063b7a6eb8c5d78eac7c09 [surf] Don't free power_peak from the caller 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. --- diff --git a/examples/msg/token_ring/token_bypass.c b/examples/msg/token_ring/token_bypass.c index c25f43d2f7..eb1f655455 100644 --- a/examples/msg/token_ring/token_bypass.c +++ b/examples/msg/token_ring/token_bypass.c @@ -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"; diff --git a/src/bindings/java/org/simgrid/surf/surfJAVA_wrap.cxx b/src/bindings/java/org/simgrid/surf/surfJAVA_wrap.cxx index c0feca451e..1633eb0247 100644 --- a/src/bindings/java/org/simgrid/surf/surfJAVA_wrap.cxx +++ b/src/bindings/java/org/simgrid/surf/surfJAVA_wrap.cxx @@ -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; diff --git a/src/bindings/lua/lua_platf.c b/src/bindings/lua/lua_platf.c index 21be518cbc..79f81fbd77 100644 --- a/src/bindings/lua/lua_platf.c +++ b/src/bindings/lua/lua_platf.c @@ -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; } diff --git a/src/surf/cpu_cas01.cpp b/src/surf/cpu_cas01.cpp index a4579f2a64..3d94de988e 100644 --- a/src/surf/cpu_cas01.cpp +++ b/src/surf/cpu_cas01.cpp @@ -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); diff --git a/src/surf/cpu_ti.cpp b/src/surf/cpu_ti.cpp index 976a85c2d9..6d2ad3ea35 100644 --- a/src/surf/cpu_ti.cpp +++ b/src/surf/cpu_ti.cpp @@ -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) diff --git a/src/surf/host_ptask_L07.cpp b/src/surf/host_ptask_L07.cpp index 0361890c8b..d7ff6f39ed 100644 --- a/src/surf/host_ptask_L07.cpp +++ b/src/surf/host_ptask_L07.cpp @@ -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), diff --git a/src/surf/surf_routing.cpp b/src/surf/surf_routing.cpp index 1a63ab6389..97fa809299 100644 --- a/src/surf/surf_routing.cpp +++ b/src/surf/surf_routing.cpp @@ -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(""); XBT_DEBUG("", 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)); diff --git a/src/surf/surfxml_parse.c b/src/surf/surfxml_parse.c index 7904fcb05f..a7b0705553 100644 --- a/src/surf/surfxml_parse.c +++ b/src/surf/surfxml_parse.c @@ -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; }