From: Martin Quinson Date: Tue, 18 Dec 2012 15:17:49 +0000 (+0100) Subject: Use xbt_malloc (and friends) instead of direct malloc() X-Git-Tag: v3_9_rc1~86^2~91 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/cc375ad134c19925e1e343bae932be21cf8a8caf Use xbt_malloc (and friends) instead of direct malloc() This is good practice as it asserts that the result is not null, and it actually help me right now when digging in the MC. --- diff --git a/examples/msg/masterslave/masterslave_failure_platfgen.c b/examples/msg/masterslave/masterslave_failure_platfgen.c index 3a7f226f51..bba483a7d9 100644 --- a/examples/msg/masterslave/masterslave_failure_platfgen.c +++ b/examples/msg/masterslave/masterslave_failure_platfgen.c @@ -328,7 +328,7 @@ int main(int argc, char *argv[]) msg_host_t host_master = NULL; msg_process_t process = NULL; xbt_dynar_t host_dynar = MSG_hosts_as_dynar(); - char** hostname_list = malloc(sizeof(char*) * xbt_dynar_length(host_dynar)); + char** hostname_list = xbt_malloc(sizeof(char*) * xbt_dynar_length(host_dynar)); xbt_dynar_foreach(host_dynar, i, host) { process = MSG_process_create("slave", slave, NULL, host); diff --git a/examples/msg/masterslave/masterslave_platfgen.c b/examples/msg/masterslave/masterslave_platfgen.c index d9d8131f44..88b1010fb3 100644 --- a/examples/msg/masterslave/masterslave_platfgen.c +++ b/examples/msg/masterslave/masterslave_platfgen.c @@ -210,7 +210,7 @@ int main(int argc, char **argv) { msg_host_t host = NULL; msg_host_t host_master = NULL; xbt_dynar_t host_dynar = MSG_hosts_as_dynar(); - char** hostname_list = malloc(sizeof(char*) * xbt_dynar_length(host_dynar)); + char** hostname_list = xbt_malloc(sizeof(char*) * xbt_dynar_length(host_dynar)); xbt_dynar_foreach(host_dynar, i, host) { MSG_process_create("slave", slave, NULL, host); diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 05e9584e2b..748a9bf8f8 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -194,7 +194,7 @@ int luaopen_simgrid(lua_State *L) XBT_DEBUG("luaopen_simgrid *****"); /* Get the command line arguments from the lua interpreter */ - char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT); + char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT); int argc = 1; argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */ diff --git a/src/instr/instr_routing.c b/src/instr/instr_routing.c index 17145b7ab7..0b4b84dd86 100644 --- a/src/instr/instr_routing.c +++ b/src/instr/instr_routing.c @@ -489,7 +489,7 @@ static xbt_edge_t new_xbt_graph_edge (xbt_graph_t graph, xbt_node_t s, xbt_node_ const char *sn = instr_node_name (s); const char *dn = instr_node_name (d); int len = strlen(sn)+strlen(dn)+1; - char *name = (char*)malloc(len * sizeof(char)); + char *name = (char*)xbt_malloc(len * sizeof(char)); snprintf (name, len, "%s%s", sn, dn); diff --git a/src/instr/jedule/jedule_events.c b/src/instr/jedule/jedule_events.c index e5175e9c0d..2edc2340f6 100644 --- a/src/instr/jedule/jedule_events.c +++ b/src/instr/jedule/jedule_events.c @@ -53,7 +53,7 @@ void jed_event_add_info(jed_event_t event, char *key, char *value) { void create_jed_event(jed_event_t *event, char *name, double start_time, double end_time, const char *type) { - *event = (jed_event_t) calloc(1, sizeof(s_jed_event_t)); + *event = xbt_new0(s_jed_event_t,1); (*event)->name = xbt_strdup(name); (*event)->start_time = start_time; diff --git a/src/instr/jedule/jedule_platform.c b/src/instr/jedule/jedule_platform.c index bd4ee3c6c4..b31fc7ab5b 100644 --- a/src/instr/jedule/jedule_platform.c +++ b/src/instr/jedule/jedule_platform.c @@ -63,7 +63,7 @@ static void jed_free_container(jed_simgrid_container_t container) { void jed_simgrid_create_container(jed_simgrid_container_t *container, char *name) { xbt_assert( name != NULL ); - *container = (jed_simgrid_container_t)calloc(1,sizeof(s_jed_simgrid_container_t)); + *container = xbt_new0(s_jed_simgrid_container_t,1); (*container)->name = xbt_strdup(name); (*container)->is_lowest = 0; (*container)->container_children = xbt_dynar_new(sizeof(jed_simgrid_container_t), NULL); @@ -117,7 +117,7 @@ static void add_subset_to(xbt_dynar_t subset_list, int start, int end, // printf(">>> start=%d end=%d\n", start, end); - subset = (jed_res_subset_t)calloc(1,sizeof(s_jed_res_subset_t)); + subset = xbt_new0(s_jed_res_subset_t,1); subset->start_idx = start; subset->nres = end-start+1; subset->parent = parent; @@ -157,7 +157,7 @@ static void add_subsets_to(xbt_dynar_t subset_list, xbt_dynar_t hostgroup, } nb_ids = xbt_dynar_length(id_list); - id_ar = (int*)calloc(nb_ids, sizeof(int)); + id_ar = xbt_new0(int,nb_ids); xbt_dynar_foreach(id_list, iter, id_str) { id_ar[iter] = atoi(id_str); } @@ -261,7 +261,7 @@ void jedule_add_meta_info(jedule_t jedule, char *key, char *value) { } void jed_create_jedule(jedule_t *jedule) { - *jedule = (jedule_t)calloc(1,sizeof(s_jedule_t)); + *jedule = xbt_new0(s_jedule_t,1); host2_simgrid_parent_container = xbt_dict_new_homogeneous(NULL); container_name2container = xbt_dict_new_homogeneous(NULL); (*jedule)->jedule_meta_info = xbt_dict_new_homogeneous(NULL); diff --git a/src/mc/mc_liveness.c b/src/mc/mc_liveness.c index b80b31954a..70560188b3 100644 --- a/src/mc/mc_liveness.c +++ b/src/mc/mc_liveness.c @@ -41,7 +41,7 @@ int create_dump(int pair) if(wait(&status) < 0) perror("wait"); if(WIFSIGNALED(status) && WCOREDUMP(status)){ - char *core_name = malloc(20); + char *core_name = xbt_malloc(20); sprintf(core_name,"core_%d", pair); rename("core", core_name); free(core_name); diff --git a/src/simdag/sd_dotloader.c b/src/simdag/sd_dotloader.c index 5faa6c4c2d..57d6d358d5 100644 --- a/src/simdag/sd_dotloader.c +++ b/src/simdag/sd_dotloader.c @@ -365,7 +365,7 @@ void dot_add_input_dependencies(SD_task_t current_job, Agedge_t * edge) SD_task_t file = NULL; char *name_tail=agnameof(agtail(edge)); char *name_head=agnameof(aghead(edge)); - char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char)); + char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char)); sprintf(name, "%s->%s", name_tail, name_head); double size = dot_parse_double(agget(edge, (char *) "size")); XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size")); @@ -403,7 +403,7 @@ void dot_add_output_dependencies(SD_task_t current_job, Agedge_t * edge) SD_task_t file; char *name_tail=agnameof(agtail(edge)); char *name_head=agnameof(aghead(edge)); - char *name = malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char)); + char *name = xbt_malloc((strlen(name_head)+strlen(name_tail)+6)*sizeof(char)); sprintf(name, "%s->%s", name_tail, name_head); double size = dot_parse_double(agget(edge, (char *) "size")); XBT_DEBUG("size : %e, get size : %s", size, agget(edge, (char *) "size")); diff --git a/src/smpi/smpi_base.c b/src/smpi/smpi_base.c index e064be615c..2c99064110 100644 --- a/src/smpi/smpi_base.c +++ b/src/smpi/smpi_base.c @@ -74,7 +74,7 @@ static MPI_Request build_request(void *buf, int count, if(datatype->has_subtype == 1){ // This part handles the problem of non-contiguous memory old_buf = buf; - buf = malloc(count*smpi_datatype_size(datatype)); + buf = xbt_malloc(count*smpi_datatype_size(datatype)); if (flags & SEND) { subtype->serialize(old_buf, buf, count, datatype->substruct); } @@ -212,7 +212,7 @@ void smpi_mpi_start(MPI_Request request) if(request->old_type->has_subtype == 0){ oldbuf = request->buf; if (oldbuf){ - request->buf = malloc(request->size); + request->buf = xbt_malloc(request->size); memcpy(request->buf,oldbuf,request->size); } } diff --git a/src/smpi/smpi_mpi_dt.c b/src/smpi/smpi_mpi_dt.c index d42e29f030..19f02289f3 100644 --- a/src/smpi/smpi_mpi_dt.c +++ b/src/smpi/smpi_mpi_dt.c @@ -181,7 +181,7 @@ int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype, s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)sendtype; - void * buf_tmp = malloc(count * type_c->size_oldtype); + void * buf_tmp = xbt_malloc(count * type_c->size_oldtype); subtype->serialize( sendbuf, buf_tmp,1, subtype); subtype = recvtype->substruct; diff --git a/src/surf/maxmin.c b/src/surf/maxmin.c index ea964d5af2..3d49f84d12 100644 --- a/src/surf/maxmin.c +++ b/src/surf/maxmin.c @@ -389,7 +389,7 @@ static XBT_INLINE void saturated_constraint_set_update(double usage, } else if (*min_usage == usage) { if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size saturated_constraint_set->size *= 2; - saturated_constraint_set->data = realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int)); + saturated_constraint_set->data = xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int)); } saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num; saturated_constraint_set->pos++; diff --git a/src/surf/network_gtnets.c b/src/surf/network_gtnets.c index b959f5557b..8207982393 100644 --- a/src/surf/network_gtnets.c +++ b/src/surf/network_gtnets.c @@ -62,7 +62,7 @@ static void route_new(int src_id, int dst_id, xbt_dynar_t links, src_id, dst_id, links, nb_link); /* Build the list of gtnets link IDs */ - gtnets_links = (int *) calloc(nb_link, sizeof(int)); + gtnets_links = xbt_new0(int, nb_link); i = 0; xbt_dynar_foreach(links, cursor, link) { gtnets_links[i++] = link->id; diff --git a/src/xbt/RngStream.c b/src/xbt/RngStream.c index a5a33134c6..01aa668c34 100644 --- a/src/xbt/RngStream.c +++ b/src/xbt/RngStream.c @@ -13,6 +13,7 @@ #include "xbt/RngStream.h" +#include "xbt/sysdep.h" #include #include #include @@ -311,14 +312,14 @@ RngStream RngStream_CreateStream (const char name[]) RngStream g; size_t len; - g = (RngStream) malloc (sizeof (struct RngStream_InfoState)); + g = (RngStream) xbt_malloc (sizeof (struct RngStream_InfoState)); if (g == NULL) { printf ("RngStream_CreateStream: No more memory\n\n"); exit (EXIT_FAILURE); } if (name) { len = strlen (name); - g->name = (char *) malloc ((len + 1) * sizeof (char)); + g->name = (char *) xbt_malloc ((len + 1) * sizeof (char)); strncpy (g->name, name, len + 1); } else g->name = 0; @@ -355,7 +356,7 @@ RngStream RngStream_CopyStream (const RngStream src) exit (EXIT_FAILURE); } - g = (RngStream) malloc (sizeof (struct RngStream_InfoState)); + g = (RngStream) xbt_malloc (sizeof (struct RngStream_InfoState)); if (g == NULL) { printf ("RngStream_CopyStream: No more memory\n\n"); exit (EXIT_FAILURE); diff --git a/src/xbt/ex.c b/src/xbt/ex.c index c6e74bddfd..ac2fa9b01b 100644 --- a/src/xbt/ex.c +++ b/src/xbt/ex.c @@ -409,7 +409,7 @@ typedef struct { static void good_example(void) { - global_context_t *global_context = malloc(sizeof(global_context_t)); + global_context_t *global_context = xbt_malloc(sizeof(global_context_t)); /* GOOD_EXAMPLE */ { /*01 */ diff --git a/src/xbt/heap.c b/src/xbt/heap.c index d26166b5fa..e124aab386 100644 --- a/src/xbt/heap.c +++ b/src/xbt/heap.c @@ -95,7 +95,7 @@ void xbt_heap_push(xbt_heap_t H, void *content, double key) if (count > size) { H->size = (size << 1) + 1; H->items = - (void *) realloc(H->items, + (void *) xbt_realloc(H->items, (H->size) * sizeof(struct xbt_heap_item)); } @@ -132,7 +132,7 @@ void *xbt_heap_pop(xbt_heap_t H) if (H->count < size >> 2 && size > 16) { size = (size >> 1) + 1; H->items = - (void *) realloc(items, + (void *) xbt_realloc(items, size * sizeof(struct xbt_heap_item)); H->size = size; } diff --git a/src/xbt/snprintf.c b/src/xbt/snprintf.c index 83c8181cc6..9098beba51 100644 --- a/src/xbt/snprintf.c +++ b/src/xbt/snprintf.c @@ -447,7 +447,7 @@ int asprintf(char **ptr, const char *fmt, /*args */ ...) str_l = portable_vsnprintf(NULL, (size_t) 0, fmt, ap); va_end(ap); assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ - *ptr = (char *) malloc(str_m = (size_t) str_l + 1); + *ptr = (char *) xbt_malloc(str_m = (size_t) str_l + 1); if (*ptr == NULL) { errno = ENOMEM; str_l = -1; @@ -476,7 +476,7 @@ int vasprintf(char **ptr, const char *fmt, va_list ap) va_end(ap2); } assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ - *ptr = (char *) malloc(str_m = (size_t) str_l + 1); + *ptr = (char *) xbt_malloc(str_m = (size_t) str_l + 1); if (*ptr == NULL) { errno = ENOMEM; str_l = -1; @@ -504,7 +504,7 @@ int asnprintf(char **ptr, size_t str_m, const char *fmt, /*args */ ...) /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */ if (str_m == 0) { /* not interested in resulting string, just return size */ } else { - *ptr = (char *) malloc(str_m); + *ptr = (char *) xbt_malloc(str_m); if (*ptr == NULL) { errno = ENOMEM; str_l = -1; @@ -538,7 +538,7 @@ int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap) /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */ if (str_m == 0) { /* not interested in resulting string, just return size */ } else { - *ptr = (char *) malloc(str_m); + *ptr = (char *) xbt_malloc(str_m); if (*ptr == NULL) { errno = ENOMEM; str_l = -1; diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.c index e4ea1849e6..4c361b594f 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.c @@ -305,14 +305,14 @@ xbt_dynar_t xbt_str_split_str(const char *s, const char *sep) //if substring was not found add the entire string if (NULL == q) { v = strlen(p); - to_push = malloc(v + 1); + to_push = xbt_malloc(v + 1); memcpy(to_push, p, v); to_push[v] = '\0'; xbt_dynar_push(res, &to_push); done = 1; } else { //get the appearance - to_push = malloc(q - p + 1); + to_push = xbt_malloc(q - p + 1); memcpy(to_push, p, q - p); //add string terminator to_push[q - p] = '\0'; diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index 688b4d9bd5..70a08ac711 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -29,8 +29,8 @@ XBT_INLINE void xbt_strbuff_empty(xbt_strbuff_t b) xbt_strbuff_t xbt_strbuff_new(void) { - xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t)); - res->data = malloc(512); + xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t)); + res->data = xbt_malloc(512); res->size = 512; xbt_strbuff_empty(res); return res; @@ -42,7 +42,7 @@ xbt_strbuff_t xbt_strbuff_new(void) */ XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(const char *ctn) { - xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t)); + xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t)); res->data = xbt_strdup(ctn); res->used = res->size = strlen(ctn); return res; @@ -76,7 +76,7 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) if (needed_space > b->size) { b->size = MAX(minimal_increment + b->used, needed_space); - b->data = realloc(b->data, b->size); + b->data = xbt_realloc(b->data, b->size); } strcpy(b->data + b->used, toadd); b->used += addlen; @@ -252,7 +252,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) // XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used); if (newused > b->size) { /* We have to realloc the data area before (because b->size is too small). We have to update our pointers, too */ - char *newdata = realloc(b->data, + char *newdata = xbt_realloc(b->data, b->used + MAX(minimal_increment, tooshort)); int offset = newdata - b->data;