From b4ab767c827a58dc5efaa5ee22ab6050d4b6172c Mon Sep 17 00:00:00 2001 From: Augustin Degomme Date: Mon, 23 Sep 2013 17:57:44 +0200 Subject: [PATCH 1/1] add a cleanup callback to surf_resource_t, useful to free data allocated in cpu models Removes a few leaks --- src/include/surf/surf.h | 1 + src/include/surf/surf_resource.h | 5 ++++- src/include/surf/surf_resource_lmm.h | 2 +- src/surf/cpu_cas01.c | 15 ++++++++++++++- src/surf/cpu_ti.c | 2 +- src/surf/storage.c | 2 +- src/surf/workstation_ptask_L07.c | 2 +- 7 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/include/surf/surf.h b/src/include/surf/surf.h index e93f43f723..afeb5f79ab 100644 --- a/src/include/surf/surf.h +++ b/src/include/surf/surf.h @@ -377,6 +377,7 @@ typedef struct surf_resource { surf_model_t model; char *name; xbt_dict_t properties; + void_f_pvoid_t free_f; } s_surf_resource_t, *surf_resource_t; /** diff --git a/src/include/surf/surf_resource.h b/src/include/surf/surf_resource.h index dc66869fdd..1723e8574e 100644 --- a/src/include/surf/surf_resource.h +++ b/src/include/surf/surf_resource.h @@ -12,18 +12,21 @@ static XBT_INLINE surf_resource_t surf_resource_new(size_t childsize, surf_model_t model, const char *name, - xbt_dict_t props) + xbt_dict_t props, void_f_pvoid_t free_f) { surf_resource_t res = xbt_malloc0(childsize); res->model = model; res->name = xbt_strdup(name); res->properties = props; + res->free_f=free_f; return res; } static XBT_INLINE void surf_resource_free(void *r) { surf_resource_t resource = r; + if(resource->free_f) + resource->free_f(r); free(resource->name); xbt_dict_free(&resource->properties); free(resource); diff --git a/src/include/surf/surf_resource_lmm.h b/src/include/surf/surf_resource_lmm.h index 992f70db07..1f507860a6 100644 --- a/src/include/surf/surf_resource_lmm.h +++ b/src/include/surf/surf_resource_lmm.h @@ -28,7 +28,7 @@ static XBT_INLINE surf_resource_lmm_t res = (surf_resource_lmm_t) surf_resource_new(childsize, model, name, - props); + props, NULL); res->constraint = lmm_constraint_new(system, res, constraint_value); res->state_current = state_init; diff --git a/src/surf/cpu_cas01.c b/src/surf/cpu_cas01.c index b3aeec02ec..671f363936 100644 --- a/src/surf/cpu_cas01.c +++ b/src/surf/cpu_cas01.c @@ -22,6 +22,19 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, static xbt_swag_t cpu_running_action_set_that_does_not_need_being_checked = NULL; +/* Additionnal callback function to cleanup some data, called from surf_resource_free */ + +static void cpu_cas1_cleanup(void* r){ + cpu_Cas01_t cpu = (cpu_Cas01_t)r; + unsigned int iter; + xbt_dynar_t power_tuple = NULL; + xbt_dynar_foreach(cpu->energy->power_range_watts_list, iter, power_tuple) + xbt_dynar_free(&power_tuple); + xbt_dynar_free(&cpu->energy->power_range_watts_list); + xbt_dynar_free(&cpu->power_peak_list); + xbt_free(cpu->energy); + return; +} /* This function is registered as a callback to sg_platf_new_host() and never called directly */ static void *cpu_create_resource(const char *name, xbt_dynar_t power_peak, @@ -40,7 +53,7 @@ static void *cpu_create_resource(const char *name, xbt_dynar_t power_peak, name); cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t), surf_cpu_model, name, - cpu_properties); + cpu_properties, &cpu_cas1_cleanup); cpu->power_peak = xbt_dynar_get_as(power_peak, pstate, double); cpu->power_peak_list = power_peak; cpu->pstate = pstate; diff --git a/src/surf/cpu_ti.c b/src/surf/cpu_ti.c index 127d1eaeb2..629faaf7bf 100644 --- a/src/surf/cpu_ti.c +++ b/src/surf/cpu_ti.c @@ -161,7 +161,7 @@ static void* cpu_ti_create_resource(const char *name, xbt_dynar_t power_peak, name); xbt_assert(core==1,"Multi-core not handled with this model yet"); cpu = (cpu_ti_t) surf_resource_new(sizeof(s_cpu_ti_t), - surf_cpu_model, name,cpu_properties); + surf_cpu_model, name,cpu_properties, NULL); cpu->action_set = xbt_swag_new(xbt_swag_offset(ti_action, cpu_list_hookup)); diff --git a/src/surf/storage.c b/src/surf/storage.c index e00a72151d..e5d8d02a79 100644 --- a/src/surf/storage.c +++ b/src/surf/storage.c @@ -205,7 +205,7 @@ static void* storage_create_resource(const char* id, const char* model, "Storage '%s' declared several times in the platform file", id); storage = (storage_t) surf_resource_new(sizeof(s_storage_t), - surf_storage_model, id, properties); + surf_storage_model, id, properties, NULL); storage->generic_resource.name = xbt_strdup(id); storage->state_current = SURF_RESOURCE_ON; diff --git a/src/surf/workstation_ptask_L07.c b/src/surf/workstation_ptask_L07.c index 9d46e741c4..eb62dcf9e1 100644 --- a/src/surf/workstation_ptask_L07.c +++ b/src/surf/workstation_ptask_L07.c @@ -668,7 +668,7 @@ static void* ptask_cpu_create_resource(const char *name, double power_scale, name); cpu = (cpu_L07_t) surf_resource_new(sizeof(s_cpu_L07_t), - surf_workstation_model, name,cpu_properties); + surf_workstation_model, name,cpu_properties, NULL); cpu->type = SURF_WORKSTATION_RESOURCE_CPU; cpu->info = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL); -- 2.20.1