Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
random generator arguments must be double to avoid stupid overflows
[simgrid.git] / src / surf / surfxml_parse.c
index c1dbedb..3c38422 100644 (file)
@@ -14,7 +14,6 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
                                "Logging specific to the SURF parsing module");
-
 #undef CLEANUP
 #include "simgrid_dtd.c"
 
@@ -29,9 +28,31 @@ xbt_dict_t trace_connect_list_bandwidth = NULL;
 xbt_dict_t trace_connect_list_latency = NULL;
 
 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
-char* old_buff;
+static xbt_dynar_t surfxml_bufferstack_stack = NULL;
+static int surfxml_bufferstack_size = 2048;
+static char *old_buff=NULL;
+static void surf_parse_error(char *msg);
+
+static void push_surfxml_bufferstack(int new)
+{
+  if(!new) old_buff = surfxml_bufferstack;
+  else {
+    xbt_dynar_push(surfxml_bufferstack_stack, &surfxml_bufferstack);
+    surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
+  }
+}
+
+static void pop_surfxml_bufferstack(int new)
+{
+  if(!new) surfxml_bufferstack = old_buff;
+  else {
+    free(surfxml_bufferstack);
+    xbt_dynar_pop(surfxml_bufferstack_stack, &surfxml_bufferstack);
+  }
+}
+
 /* Stores the set name reffered to by the foreach tag */
-static const char* foreach_set_name;
+static char* foreach_set_name;
 static xbt_dynar_t main_STag_surfxml_host_cb_list = NULL;
 static xbt_dynar_t main_ETag_surfxml_host_cb_list = NULL;
 static xbt_dynar_t main_STag_surfxml_link_cb_list = NULL;
@@ -96,6 +117,16 @@ FILE *surf_file_to_parse = NULL;
 
 static void convert_route_multi_to_routes(void);
 static void parse_route_elem(void);
+static void parse_foreach(void);
+static void parse_sets(void);
+static void parse_route_multi_set_endpoints(void);
+static void parse_route_multi_set_route(void);
+static void parse_cluster(void);
+static void parse_trace_init(void);
+static void parse_trace_finalize(void);
+static void parse_trace_c_connect(void);
+static void init_randomness(void);
+static void add_randomness(void);
 
 void surf_parse_free_callbacks(void)
 {
@@ -218,8 +249,6 @@ void STag_surfxml_platform(void)
              "is available in the contrib/platform_generation directory "
              "of the simgrid repository.");
 
-  if (set_list == NULL) set_list = xbt_dict_new(); 
-
   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
 
 }
@@ -231,7 +260,6 @@ void ETag_surfxml_platform(void)
   surfxml_call_cb_functions(ETag_surfxml_platform_cb_list);
 
   xbt_dict_free(&random_data_list);
-  xbt_dict_free(&set_list);
 }
 
 void STag_surfxml_host(void)
@@ -326,7 +354,7 @@ void ETag_surfxml_set(void)
 void STag_surfxml_foreach(void)
 {
   /* Save the current buffer */
-  old_buff = surfxml_bufferstack;
+  push_surfxml_bufferstack(0);
   surfxml_call_cb_functions(STag_surfxml_foreach_cb_list);
 }
 
@@ -444,13 +472,20 @@ static int _surf_parse(void)
 
 int_f_void_t surf_parse = _surf_parse;
 
+void surf_parse_error(char *msg)
+{
+   fprintf(stderr,"Parse error on line %d: %s\n",surf_parse_lineno,msg);
+   abort();
+}
+
+
 void surf_parse_get_double(double *value, const char *string)
 {
   int ret = 0;
 
   ret = sscanf(string, "%lg", value);
-  xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
-             surf_parse_lineno, string);
+  if (ret != 1)
+     surf_parse_error(bprintf("%s is not a double", string));
 }
 
 void surf_parse_get_int(int *value, const char *string)
@@ -458,8 +493,8 @@ void surf_parse_get_int(int *value, const char *string)
   int ret = 0;
 
   ret = sscanf(string, "%d", value);
-  xbt_assert2((ret == 1), "Parse error line %d : %s not a number",
-             surf_parse_lineno, string);
+  if (ret != 1) 
+     surf_parse_error(bprintf("%s is not an integer", string));
 }
 
 void surf_parse_get_trace(tmgr_trace_t * trace, const char *string)
@@ -496,7 +531,7 @@ static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
 }
 
 static void parse_route_set_endpoints(void) {
-  route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+  route_link_list = xbt_dynar_new(sizeof(char *), NULL);
 }
 
 static void init_data(void)
@@ -505,10 +540,13 @@ static void init_data(void)
   xbt_dynar_free(&route_link_list);
   route_table = xbt_dict_new();
 
+  if(!surfxml_bufferstack_stack) 
+    surfxml_bufferstack_stack=xbt_dynar_new(sizeof(char*),NULL);
   route_multi_table = xbt_dict_new();
   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
   traces_set_list = xbt_dict_new();
-   
+  if (set_list == NULL) set_list = xbt_dict_new();
+
   trace_connect_list_host_avail = xbt_dict_new();
   trace_connect_list_power = xbt_dict_new();
   trace_connect_list_link_avail = xbt_dict_new();
@@ -516,22 +554,54 @@ static void init_data(void)
   trace_connect_list_latency = xbt_dict_new();
 
   random_data_list = xbt_dict_new();
+  surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
+  surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
+  surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
+  surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
+  surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
+  surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
+  surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
+  surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
+  surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
+  surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
+  surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
 }
 
 static void free_data(void) 
 {
   char *key,*data;
   xbt_dict_cursor_t cursor = NULL;
+  char *name;
+  unsigned int cpt = 0;
 
   xbt_dict_foreach(route_table, cursor, key, data) {
     xbt_dynar_t links = (xbt_dynar_t)data;
+    char *name;
+    unsigned int cpt = 0;
+
+    xbt_dynar_foreach (links, cpt, name)  free(name);
     xbt_dynar_free(&links);
   }
   xbt_dict_free(&route_table);
   route_link_list = NULL;
 
+  xbt_dict_free(&route_multi_table);
+
+  xbt_dynar_foreach (route_multi_elements, cpt, name)  free(name);
+  xbt_dynar_free(&route_multi_elements);
+
+  xbt_dict_foreach(set_list, cursor, key, data) {
+    xbt_dynar_t set = (xbt_dynar_t)data;
+
+    xbt_dynar_foreach (set, cpt, name)  free(name);
+    xbt_dynar_free(&set);
+  }
+  xbt_dict_free(&set_list);
+
+  xbt_dynar_free(&surfxml_bufferstack_stack);
+
   xbt_dict_free(&trace_connect_list_host_avail);
   xbt_dict_free(&trace_connect_list_power);
   xbt_dict_free(&trace_connect_list_link_avail);
@@ -554,7 +624,6 @@ void parse_platform_file(const char* file)
 static void parse_make_temporary_route(const char *src, const char *dst, int action)
 {
   int AX_ptr = 0;
-  surfxml_bufferstack = xbt_new0(char, 2048);
   
   A_surfxml_route_action = action;
   SURFXML_BUFFER_SET(route_src,                     src);
@@ -565,7 +634,6 @@ static void parse_change_cpu_data(const char* hostName, const char* surfxml_host
                                        const char* surfxml_host_availability_file, const char* surfxml_host_state_file)
 {
   int AX_ptr = 0;
-  surfxml_bufferstack = xbt_new0(char, 2048);
  
   SURFXML_BUFFER_SET(host_id,                     hostName);
   SURFXML_BUFFER_SET(host_power,                  surfxml_host_power /*hostPower*/);
@@ -578,7 +646,6 @@ static void parse_change_link_data(const char* linkName, const char* surfxml_lin
                                        const char* surfxml_link_latency, const char* surfxml_link_latency_file, const char* surfxml_link_state_file)
 {
   int AX_ptr = 0;
-  surfxml_bufferstack = xbt_new0(char, 2048);
  
   SURFXML_BUFFER_SET(link_id,                linkName);
   SURFXML_BUFFER_SET(link_bandwidth,         surfxml_link_bandwidth);
@@ -588,48 +655,66 @@ static void parse_change_link_data(const char* linkName, const char* surfxml_lin
   SURFXML_BUFFER_SET(link_state_file,        surfxml_link_state_file);
 }
 
-/**
-* \brief Restores the original surfxml buffer
-*/
-static void parse_restore_original_buffer(void)
-{
-  free(surfxml_bufferstack);
-  surfxml_bufferstack = old_buff;
-}
-
 /* Functions for the sets and foreach tags */
 
-void parse_sets(void)
+static void parse_sets(void)
 {
   char *id, *suffix, *prefix, *radical;
   int start, end;
+  xbt_dynar_t radical_elements;
   xbt_dynar_t radical_ends;
   xbt_dynar_t current_set;
-  char *value;
+  char *value,*groups;
   int i;
+  unsigned int iter;
 
   id = xbt_strdup(A_surfxml_set_id);
   prefix = xbt_strdup(A_surfxml_set_prefix);
   suffix = xbt_strdup(A_surfxml_set_suffix);
   radical = xbt_strdup(A_surfxml_set_radical);
   
-  xbt_assert1(!xbt_dict_get_or_null(set_list, id),
-             "Set '%s' declared several times in the platform file.",id);  
-  radical_ends = xbt_str_split(radical, "-");
-  xbt_assert1((xbt_dynar_length(radical_ends)==2), "Radical must be in the form lvalue-rvalue! Provided value: %s", radical);
+  if (xbt_dict_get_or_null(set_list, id))
+     surf_parse_error(bprintf("Set '%s' declared several times in the platform file.",id));
+   
+  current_set = xbt_dynar_new(sizeof(char*), NULL);
 
-  surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
-  surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
+  radical_elements = xbt_str_split(radical,",");
+  xbt_dynar_foreach(radical_elements,iter, groups) {
+       
+     radical_ends = xbt_str_split(groups, "-");
+     switch (xbt_dynar_length(radical_ends)) {
+      case 1:
+       surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
+       value = bprintf("%s%d%s", prefix, start, suffix);
+       xbt_dynar_push(current_set, &value);
+       break;
+       
+      case 2:
 
-  current_set = xbt_dynar_new(sizeof(char*), NULL);
+       surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char*));
+       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char*));
 
-  
-  for (i=start; i<=end; i++) {
-     value = bprintf("%s%d%s", prefix, i, suffix);
-     xbt_dynar_push(current_set, &value);
-  } 
-  
+
+       for (i=start; i<=end; i++) {
+          value = bprintf("%s%d%s", prefix, i, suffix);
+          xbt_dynar_push(current_set, &value);
+       } 
+       break;
+       
+      default:
+       surf_parse_error(xbt_strdup("Malformed radical"));
+     } 
+     
+     xbt_dynar_free(&radical_ends);
+  }
+   
   xbt_dict_set(set_list, id, current_set, NULL);
+
+  xbt_dynar_free(&radical_elements);
+  free(radical);
+  free(suffix);
+  free(prefix);
+  free(id);
 }
 
 static const char* surfxml_host_power;
@@ -655,14 +740,18 @@ static void finalize_host_foreach(void)
 
   xbt_dict_t cluster_host_props = current_property_set;
   
-  xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
-             "Set name '%s' reffered by foreach tag not found.", foreach_set_name);  
-
-  xbt_assert1((strcmp(A_surfxml_host_id, "$1") == 0), "The id of the host within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_host_id);
+  names = xbt_dict_get_or_null(set_list, foreach_set_name);
+  if (!names)
+     surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
+                              foreach_set_name));
+  if (strcmp(A_surfxml_host_id, "$1"))
+     surf_parse_error(bprintf("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)", 
+                             A_surfxml_host_id));
 
        
   /* foreach name in set call the main host callback */
   xbt_dynar_foreach (names, cpt, name) {
+    push_surfxml_bufferstack(1);
     parse_change_cpu_data(name, surfxml_host_power, surfxml_host_availability,
                                        surfxml_host_availability_file, surfxml_host_state_file);
     surfxml_call_cb_functions(main_STag_surfxml_host_cb_list);
@@ -672,13 +761,12 @@ static void finalize_host_foreach(void)
     }
 
     surfxml_call_cb_functions(main_ETag_surfxml_host_cb_list);
-    free(surfxml_bufferstack);
+    pop_surfxml_bufferstack(1);
   }
 
   current_property_set = xbt_dict_new();
 
-  surfxml_bufferstack = old_buff;
-  
+  pop_surfxml_bufferstack(0);
 }
 
 static const char* surfxml_link_bandwidth;
@@ -706,14 +794,17 @@ static void finalize_link_foreach(void)
 
   xbt_dict_t cluster_link_props = current_property_set;
 
-  xbt_assert1((names = xbt_dict_get_or_null(set_list, foreach_set_name)),
-             "Set name '%s' reffered by foreach tag not found.", foreach_set_name); 
-
-  xbt_assert1((strcmp(A_surfxml_link_id, "$1") == 0), "The id of the link within the foreach should point to the foreach set_id (use $1). Your value: %s", A_surfxml_link_id);
-
+  names = xbt_dict_get_or_null(set_list, foreach_set_name);
+  if (!names)
+     surf_parse_error(bprintf("Set name '%s' used in <foreach> not found.",
+                              foreach_set_name));
+  if (strcmp(A_surfxml_link_id, "$1"))
+     surf_parse_error(bprintf("The host id within <foreach> should point to the foreach set_id (use $1 instead of %s)", 
+                             A_surfxml_link_id));
 
   /* for each name in set call the main link callback */
   xbt_dynar_foreach (names, cpt, name) {
+    push_surfxml_bufferstack(1);
     parse_change_link_data(name, surfxml_link_bandwidth, surfxml_link_bandwidth_file,
                                        surfxml_link_latency, surfxml_link_latency_file, surfxml_link_state_file);
     surfxml_call_cb_functions(main_STag_surfxml_link_cb_list);
@@ -723,16 +814,17 @@ static void finalize_link_foreach(void)
     }
 
     surfxml_call_cb_functions(main_ETag_surfxml_link_cb_list);
-   free(surfxml_bufferstack);
-
+    pop_surfxml_bufferstack(1);
   }
 
   current_property_set = xbt_dict_new();
 
-  surfxml_bufferstack = old_buff;
+  pop_surfxml_bufferstack(0);
+  free(foreach_set_name);
+  foreach_set_name=NULL;
 }
 
-void parse_foreach(void)
+static void parse_foreach(void)
 {
   /* save the host & link callbacks */
   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
@@ -770,7 +862,7 @@ static void parse_route_elem(void)
   xbt_dynar_push(route_link_list, &val);
 }
 
-void parse_route_multi_set_endpoints(void)
+static void parse_route_multi_set_endpoints(void)
 {
   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
@@ -793,7 +885,7 @@ static int contains(xbt_dynar_t list, const char* value)
 }
 
 /* 
-   This function is used to append or override the contents of an alread existing route in the case a new one with its name is found.
+   This function is used to append or override the contents of an already existing route in the case a new one with its name is found.
    The decision is based upon the value of action specified in the xml route:multi attribute action
  */
 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
@@ -811,6 +903,7 @@ void manage_route(xbt_dict_t routing_table, const char *route_name, int action,
                                    xbt_dynar_foreach(links, cpt, value) {      
                                        xbt_dynar_push(route_link_list,&value);
                                    }
+                                   xbt_dynar_free(&links);
                                     break;
         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
                                    xbt_dynar_foreach(route_link_list, cpt, value) {
@@ -820,6 +913,7 @@ void manage_route(xbt_dict_t routing_table, const char *route_name, int action,
                                     route_link_list = links;
                                    break;
         case A_surfxml_route_action_OVERRIDE:
+                                   xbt_dynar_free(&links);
                                     break;
         default:break;
      }
@@ -830,7 +924,7 @@ void manage_route(xbt_dict_t routing_table, const char *route_name, int action,
   }
 }
 
-void parse_route_multi_set_route(void)
+static void parse_route_multi_set_route(void)
 {
   char* route_name;
 
@@ -856,6 +950,9 @@ static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links,
 {
   unsigned int cpt;
   char* value, *val;
+
+  push_surfxml_bufferstack(1);
+
    parse_make_temporary_route(src_name, dst_name, route_action);
    surfxml_call_cb_functions(STag_surfxml_route_cb_list);
    DEBUG2("\tADDING ROUTE: %s -> %s", src_name, dst_name);
@@ -875,7 +972,7 @@ static void add_multi_links(const char* src, const char* dst, xbt_dynar_t links,
      xbt_dynar_push(route_link_list, &val);
    }    
    surfxml_call_cb_functions(ETag_surfxml_route_cb_list);
-   free(surfxml_bufferstack);
+   pop_surfxml_bufferstack(1);
 }
 
 static void convert_route_multi_to_routes(void)
@@ -896,7 +993,7 @@ static void convert_route_multi_to_routes(void)
      set = workstation_set;
   
 
-  old_buff = surfxml_bufferstack;
+  push_surfxml_bufferstack(0);
   /* Get all routes in the exact order they were entered in the platform file */
   xbt_dynar_foreach(route_multi_elements, cursor, key) {
      /* Get links for the route */     
@@ -952,18 +1049,16 @@ static void convert_route_multi_to_routes(void)
        }     
       }
     }
+    xbt_dynar_free(&keys);
   }  
-  surfxml_bufferstack = old_buff;
-  xbt_dict_free(&route_multi_table);
-  xbt_dynar_free(&route_multi_elements);
+  pop_surfxml_bufferstack(0);
 }
 
 /* Cluster tag functions */
 
-void parse_cluster(void)
+static void parse_cluster(void)
 {  
    static int AX_ptr = 0;
-   static int surfxml_bufferstack_size = 2048;
  
    char* cluster_id = A_surfxml_cluster_id;
    char* cluster_prefix = A_surfxml_cluster_prefix;
@@ -974,12 +1069,9 @@ void parse_cluster(void)
    char* cluster_lat = A_surfxml_cluster_lat;
    char* cluster_bb_bw = A_surfxml_cluster_bb_bw;
    char* cluster_bb_lat = A_surfxml_cluster_bb_lat;
-   char* saved_buff = surfxml_bufferstack;
-
-   char * backbone_name;
+   char* backbone_name;
 
-   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
+   push_surfxml_bufferstack(1);
 
    /* Make set */
    SURFXML_BUFFER_SET(set_id, cluster_id);
@@ -1052,9 +1144,10 @@ void parse_cluster(void)
    SURFXML_END_TAG(route_c_multi);
 
 
+   free(backbone_name);
+
    /* Restore buff */
-   free(surfxml_bufferstack);
-   surfxml_bufferstack = saved_buff;
+   pop_surfxml_bufferstack(1);
 }
 
 /* Trace management functions */
@@ -1063,14 +1156,14 @@ static double trace_periodicity = -1.0;
 static char* trace_file = NULL;
 static char* trace_id;
 
-void parse_trace_init(void)
+static void parse_trace_init(void)
 {
    trace_id = strdup(A_surfxml_trace_id);
    trace_file = strdup(A_surfxml_trace_file);
    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
 }
 
-void parse_trace_finalize(void)
+static void parse_trace_finalize(void)
 {
   tmgr_trace_t trace;
   if (!trace_file || strcmp(trace_file,"") != 0) {
@@ -1084,7 +1177,7 @@ void parse_trace_finalize(void)
   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
 }
 
-void parse_trace_c_connect(void)
+static void parse_trace_c_connect(void)
 {
    xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
              "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
@@ -1135,20 +1228,20 @@ double get_cpu_power(const char *power)
   return power_scale;
 }
 
-int random_min, random_max, random_mean, random_std_deviation, random_generator;
+double random_min, random_max, random_mean, random_std_deviation, random_generator;
 char *random_id;
 
-void init_randomness(void)
+static void init_randomness(void)
 {
   random_id = A_surfxml_random_id;
-  surf_parse_get_int(&random_min, A_surfxml_random_min);
-  surf_parse_get_int(&random_max, A_surfxml_random_max);
-  surf_parse_get_int(&random_mean, A_surfxml_random_mean);
-  surf_parse_get_int(&random_std_deviation, A_surfxml_random_std_deviation);
+  surf_parse_get_double(&random_min, A_surfxml_random_min);
+  surf_parse_get_double(&random_max, A_surfxml_random_max);
+  surf_parse_get_double(&random_mean, A_surfxml_random_mean);
+  surf_parse_get_double(&random_std_deviation, A_surfxml_random_std_deviation);
   random_generator = A_surfxml_random_generator;
 }
 
-void add_randomness(void)
+static void add_randomness(void)
 {
    /* If needed aditional properties can be added by using the prop tag */
   random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);