Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use xbt_malloc (and friends) instead of direct malloc()
authorMartin Quinson <martin.quinson@loria.fr>
Tue, 18 Dec 2012 15:17:49 +0000 (16:17 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Tue, 18 Dec 2012 21:16:28 +0000 (22:16 +0100)
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.

18 files changed:
examples/msg/masterslave/masterslave_failure_platfgen.c
examples/msg/masterslave/masterslave_platfgen.c
src/bindings/lua/simgrid_lua.c
src/instr/instr_routing.c
src/instr/jedule/jedule_events.c
src/instr/jedule/jedule_platform.c
src/mc/mc_liveness.c
src/simdag/sd_dotloader.c
src/smpi/smpi_base.c
src/smpi/smpi_mpi_dt.c
src/surf/maxmin.c
src/surf/network_gtnets.c
src/xbt/RngStream.c
src/xbt/ex.c
src/xbt/heap.c
src/xbt/snprintf.c
src/xbt/xbt_str.c
src/xbt/xbt_strbuff.c

index 3a7f226..bba483a 100644 (file)
@@ -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);
index d9d8131..88b1010 100644 (file)
@@ -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);
index 05e9584..748a9bf 100644 (file)
@@ -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? */
 
index 17145b7..0b4b84d 100644 (file)
@@ -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);
index e5175e9..2edc234 100644 (file)
@@ -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;
index bd4ee3c..b31fc7a 100644 (file)
@@ -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);
index b80b319..7056018 100644 (file)
@@ -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);
index 5faa6c4..57d6d35 100644 (file)
@@ -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"));
index e064be6..2c99064 100644 (file)
@@ -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);
         }
       }
index d42e29f..19f0228 100644 (file)
@@ -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;
index ea964d5..3d49f84 100644 (file)
@@ -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++;
index b959f55..8207982 100644 (file)
@@ -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;
index a5a3313..01aa668 100644 (file)
@@ -13,6 +13,7 @@
 
 
 #include "xbt/RngStream.h"
+#include "xbt/sysdep.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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);
index c6e74bd..ac2fa9b 100644 (file)
@@ -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 */
index d26166b..e124aab 100644 (file)
@@ -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;
   }
index 83c8181..9098beb 100644 (file)
@@ -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;
index e4ea184..4c361b5 100644 (file)
@@ -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';
index 688b4d9..70a08ac 100644 (file)
@@ -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;