Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
There was too many reallocs
[simgrid.git] / src / surf / network_dassf.c
index 8046397..3d31515 100644 (file)
@@ -129,7 +129,8 @@ static void parse_network_link(void)
                   lat_initial, lat_trace, state_initial, state_trace);
 }
 
-static int nb_link = 0;
+static int nb_link;
+static int link_name_capacity;
 static char **link_name = NULL;
 static int src_id = -1;
 static int dst_id = -1;
@@ -139,14 +140,17 @@ static void parse_route_set_endpoints(void)
   src_id = network_card_new(A_surfxml_route_src);
   dst_id = network_card_new(A_surfxml_route_dst);
   nb_link = 0;
-  link_name = NULL;
+  link_name_capacity = 16;
+  link_name = xbt_new(char*, link_name_capacity);
 }
 
 static void parse_route_elem(void)
 {
-  nb_link++;
-  link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
-  link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
+  if (nb_link == link_name_capacity) {
+    link_name_capacity *= 2;
+    link_name = xbt_realloc(link_name, (link_name_capacity) * sizeof(char *));
+  }
+  link_name[nb_link++] = xbt_strdup(A_surfxml_route_element_name);
 }
 
 static void parse_route_set_route(void)