Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups to the config stuff
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 6 Apr 2016 10:38:22 +0000 (12:38 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 6 Apr 2016 10:38:25 +0000 (12:38 +0200)
- Kill unused feature: minimal arity
- Hide some internal functions and symbols

include/xbt/config.h
src/simgrid/sg_config.cpp
src/xbt/config.c

index cf60d5e..2d0e359 100644 (file)
@@ -147,8 +147,6 @@ XBT_PUBLIC(void) xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cf
  *
  *  @{
  */
-XBT_PUBLIC(void) xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *description,
-    e_xbt_cfgelm_type_t type, int min, xbt_cfg_cb_t cb_set);
 XBT_PUBLIC(void) xbt_cfg_register_double (const char *name, const char *desc, double default_val,    xbt_cfg_cb_t cb_set);
 XBT_PUBLIC(void) xbt_cfg_register_int    (const char *name, const char *desc, int default_val,       xbt_cfg_cb_t cb_set);
 XBT_PUBLIC(void) xbt_cfg_register_string (const char *name, const char *desc, const char*default_val,xbt_cfg_cb_t cb_set);
@@ -156,9 +154,8 @@ XBT_PUBLIC(void) xbt_cfg_register_boolean(const char *name, const char *desc, co
 XBT_PUBLIC(void) xbt_cfg_register_alias(const char *newname, const char *oldname);
 XBT_PUBLIC(void) xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry);
 
-XBT_PUBLIC(void) xbt_cfg_aliases(xbt_cfg_t cfg);
-XBT_PUBLIC(void) xbt_cfg_help(xbt_cfg_t cfg);
-XBT_PUBLIC(void) xbt_cfg_check(void);
+XBT_PUBLIC(void) xbt_cfg_aliases(void);
+XBT_PUBLIC(void) xbt_cfg_help(void);
 XBT_PUBLIC(e_xbt_cfgelm_type_t) xbt_cfg_get_type(xbt_cfg_t cfg, const char *name);
 /*  @} */
 /** @defgroup XBT_cfg_get Getting the stored values
index 46c7d6e..4bee5be 100644 (file)
@@ -61,7 +61,7 @@ static void sg_config_cmd_line(int *argc, char **argv)
       shall_exit = 1;
     } else if (!strcmp(argv[i], "--cfg-help") || !strcmp(argv[i], "--help")) {
       printf("Description of the configuration accepted by this simulator:\n");
-      xbt_cfg_help(simgrid_config);
+      xbt_cfg_help();
       printf(
           "\n"
           "Each of these configurations can be used by adding\n"
@@ -79,7 +79,7 @@ static void sg_config_cmd_line(int *argc, char **argv)
       shall_exit = 1;
     } else if (!strcmp(argv[i], "--help-aliases")) {
       printf("Here is a list of all deprecated option names, with their replacement.\n");
-      xbt_cfg_aliases(simgrid_config);
+      xbt_cfg_aliases();
       printf("Please consider using the recent names\n");
       shall_exit = 1;
     } else if (!strcmp(argv[i], "--help-models")) {
@@ -118,6 +118,9 @@ static void _sg_cfg_cb__plugin(const char *name, int pos)
   xbt_assert(_sg_cfg_init_status < 2, "Cannot load a plugin after the initialization");
 
   char *val = xbt_cfg_get_string(name);
+  if (val==nullptr)
+    return;
+
   if (!strcmp(val, "help")) {
     model_help("plugin", surf_plugin_description);
     sg_cfg_exit_early();
@@ -472,7 +475,7 @@ void sg_config_init(int *argc, char **argv)
 
     /* Plugins configuration */
     describe_model(description, surf_plugin_description, "plugin", "The plugins");
-    xbt_cfg_register(&simgrid_config, "plugin", description, xbt_cfgelm_string, 0, &_sg_cfg_cb__plugin);
+    xbt_cfg_register_string("plugin", description, nullptr, &_sg_cfg_cb__plugin);
 
     describe_model(description, surf_cpu_model_description, "model", "The model to use for the CPU");
     xbt_cfg_register_string("cpu/model", description, "Cas01", &_sg_cfg_cb__cpu_model);
@@ -527,7 +530,7 @@ void sg_config_init(int *argc, char **argv)
         "Update the constraint set propagating recursively to others constraints (off by default when optim is set to lazy)",
         "no", NULL);
     /* Replay (this part is enabled even if MC it disabled) */
-    xbt_cfg_register(&simgrid_config, "model-check/replay", "Enable replay mode with the given path", xbt_cfgelm_string, 0, _sg_cfg_cb_model_check_replay);
+    xbt_cfg_register_string("model-check/replay", "Enable replay mode with the given path", nullptr, _sg_cfg_cb_model_check_replay);
 
 #if HAVE_MC
     /* do model-checking-record */
@@ -687,7 +690,6 @@ void sg_config_init(int *argc, char **argv)
       xbt_cfg_setdefault_string("path", initial_path);
     }
 
-    xbt_cfg_check();
     _sg_cfg_init_status = 1;
 
     sg_config_cmd_line(argc, argv);
index b84c29e..647ec57 100644 (file)
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
 
 xbt_cfg_t simgrid_config = NULL;
+static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set);
 
-/* xbt_cfgelm_t: the typedef corresponding to a config variable.
-
-   Both data and DTD are mixed, but fixing it now would prevent me to ever defend my thesis. */
+/* xbt_cfgelm_t: the typedef corresponding to a config variable. */
 
 typedef struct {
   /* Description */
@@ -33,7 +32,6 @@ typedef struct {
 
   /* Allowed type of the variable */
   e_xbt_cfgelm_type_t type;
-  int min;
   unsigned isdefault:1;
 
   /* Callbacks */
@@ -43,7 +41,7 @@ typedef struct {
   xbt_dynar_t content;
 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
 
-static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any" };
+static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
 
 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
   { "yes",    "no"},
@@ -86,9 +84,8 @@ void xbt_cfg_cpy(xbt_cfg_t tocopy, xbt_cfg_t * whereto)
   *whereto = NULL;
   xbt_assert(tocopy, "cannot copy NULL config");
 
-  xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable) {
-    xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->min, variable->cb_set);
-  }
+  xbt_dict_foreach((xbt_dict_t) tocopy, cursor, name, variable)
+    xbt_cfg_register(whereto, name, variable->desc, variable->type, variable->cb_set);
 }
 
 /** @brief Destructor */
@@ -123,8 +120,8 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
     printf("%s  %s:", indent, key);
 
     size = xbt_dynar_length(variable->content);
-    printf ("%d_%s. Actual size=%d. postset=%p, List of values:\n",
-            variable->min, xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
+    printf ("%s. Actual size=%d. postset=%p, List of values:\n",
+            xbt_cfgelm_type_name[variable->type], size, variable->cb_set);
 
     switch (variable->type) {
     case xbt_cfgelm_int:
@@ -190,10 +187,9 @@ void xbt_cfgelm_free(void *data)
  *  @param name the name of the config element
  *  @param desc a description for this item (used by xbt_cfg_help())
  *  @param type the type of the config element
- *  @param min the minimum number of values for this config element (0 for optional elements)
  *  @param cb_set callback function called when a value is set
  */
-void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, int min, xbt_cfg_cb_t cb_set)
+static void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
 {
   if (*cfg == NULL)
     *cfg = xbt_cfg_new();
@@ -205,12 +201,11 @@ void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt
   xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
 
   res = xbt_new(s_xbt_cfgelm_t, 1);
-  XBT_DEBUG("Register cfg elm %s (%s) (%d %s (=%d) @%p in set %p)",
-            name, desc, min, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
+  XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
+            name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
 
   res->desc = xbt_strdup(desc);
   res->type = type;
-  res->min = min;
   res->cb_set = cb_set;
   res->isdefault = 1;
 
@@ -235,19 +230,19 @@ void xbt_cfg_register(xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt
 }
 
 void xbt_cfg_register_double(const char *name, const char *desc, double default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,1,cb_set);
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_double,cb_set);
   xbt_cfg_setdefault_double(name, default_value);
 }
 void xbt_cfg_register_int(const char *name, const char *desc, int default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,1,cb_set);
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_int,cb_set);
   xbt_cfg_setdefault_int(name, default_value);
 }
 void xbt_cfg_register_string(const char *name, const char *desc, const char *default_value, xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,1,cb_set);
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
   xbt_cfg_setdefault_string(name, default_value);
 }
 void xbt_cfg_register_boolean(const char *name, const char *desc, const char*default_value,xbt_cfg_cb_t cb_set){
-  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,1,cb_set);
+  xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
   xbt_cfg_setdefault_boolean(name, default_value);
 }
 
@@ -267,7 +262,6 @@ void xbt_cfg_register_alias(const char *newname, const char *oldname)
 
   res->desc = bprintf("Deprecated alias for %s",newname);
   res->type = xbt_cfgelm_alias;
-  res->min = 1;
   res->isdefault = 1;
   res->content = (xbt_dynar_t)newname;
 
@@ -281,8 +275,8 @@ void xbt_cfg_register_alias(const char *newname, const char *oldname)
  * @param entry a string describing the element to register
  *
  * The string may consist in several variable descriptions separated by a space.
- * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
- * with type being one of  'string','int' or 'double'.
+ * Each of them must use the following syntax: \<name\>:\<type\>
+ * with type being one of  'string','int','bool' or 'double'.
  *
  * Note that this does not allow to set the description, so you should prefer the other interface
  */
@@ -291,32 +285,24 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
   char *entrycpy = xbt_strdup(entry);
   char *tok;
 
-  int min;
   e_xbt_cfgelm_type_t type;
   XBT_DEBUG("Register string '%s'", entry);
 
   tok = strchr(entrycpy, ':');
-  xbt_assert(tok, "Invalid config element descriptor: %s%s", entry, "; Should be <name>:<min nb>_to_<max nb>_<type>");
+  xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
   *(tok++) = '\0';
 
-  min = strtol(tok, &tok, 10);
-  xbt_assert(tok, "Invalid minimum in config element descriptor %s", entry);
-
-  xbt_assert(*tok == '_', "Invalid config element descriptor: %s%s", entry,
-              "; Should be <name>:<min nb>_to_<max nb>_<type>");
-  tok++;
-
   for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); type++);
-  xbt_assert(type < xbt_cfgelm_type_count, "Invalid type in config element descriptor: %s%s", entry,
-              "; Should be one of 'string', 'int' or 'double'.");
+  xbt_assert(type < xbt_cfgelm_type_count,
+      "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
 
-  xbt_cfg_register(cfg, entrycpy, NULL, type, min, NULL);
+  xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
 
   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
 }
 
 /** @brief Displays the declared aliases and their description */
-void xbt_cfg_aliases(xbt_cfg_t cfg)
+void xbt_cfg_aliases(void)
 {
   xbt_dict_cursor_t dict_cursor;
   unsigned int dynar_cursor;
@@ -324,12 +310,12 @@ void xbt_cfg_aliases(xbt_cfg_t cfg)
   char *name;
   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
 
-  xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
+  xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
     xbt_dynar_push(names, &name);
   xbt_dynar_sort_strings(names);
 
   xbt_dynar_foreach(names, dynar_cursor, name) {
-    variable = xbt_dict_get((xbt_dict_t )cfg, name);
+    variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
 
     if (variable->type == xbt_cfgelm_alias)
       printf("   %s: %s\n", name, variable->desc);
@@ -337,7 +323,7 @@ void xbt_cfg_aliases(xbt_cfg_t cfg)
 }
 
 /** @brief Displays the declared options and their description */
-void xbt_cfg_help(xbt_cfg_t cfg)
+void xbt_cfg_help(void)
 {
   xbt_dict_cursor_t dict_cursor;
   unsigned int dynar_cursor;
@@ -345,27 +331,24 @@ void xbt_cfg_help(xbt_cfg_t cfg)
   char *name;
   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
 
-  xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
+  xbt_dict_foreach((xbt_dict_t )simgrid_config, dict_cursor, name, variable)
     xbt_dynar_push(names, &name);
   xbt_dynar_sort_strings(names);
 
   xbt_dynar_foreach(names, dynar_cursor, name) {
-    int i;
     int size;
-    variable = xbt_dict_get((xbt_dict_t )cfg, name);
+    variable = xbt_dict_get((xbt_dict_t )simgrid_config, name);
     if (variable->type == xbt_cfgelm_alias)
       continue;
 
     printf("   %s: %s\n", name, variable->desc);
     printf("       Type: %s; ", xbt_cfgelm_type_name[variable->type]);
-    if (variable->min != 1)
-      printf("Arity: min:%d", variable->min);
     size = xbt_dynar_length(variable->content);
-    printf("Current value%s: ", (size <= 1 ? "" : "s"));
+    printf("Current value: ");
 
     if (size != 1)
       printf(size == 0 ? "n/a\n" : "{ ");
-    for (i = 0; i < size; i++) {
+    for (int i = 0; i < size; i++) {
       const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
 
       switch (variable->type) {
@@ -396,33 +379,6 @@ void xbt_cfg_help(xbt_cfg_t cfg)
   xbt_dynar_free(&names);
 }
 
-/** @brief Check that each variable have the right amount of values */
-void xbt_cfg_check(void)
-{
-  xbt_dict_cursor_t cursor;
-  xbt_cfgelm_t variable;
-  char *name;
-
-  xbt_dict_foreach((xbt_dict_t) simgrid_config, cursor, name, variable) {
-    if (variable->type == xbt_cfgelm_alias)
-      continue;
-
-    int size = xbt_dynar_length(variable->content);
-    if (variable->min > size) {
-      xbt_dict_cursor_free(&cursor);
-      THROWF(mismatch_error, 0, "Config elem %s needs at least %d %s, but there is only %d values.",
-             name, variable->min, xbt_cfgelm_type_name[variable->type], size);
-    }
-
-    if (variable->isdefault && size > variable->min) {
-      xbt_dict_cursor_free(&cursor);
-      THROWF(mismatch_error, 0, "Config elem %s theoretically accepts %d %s, but has a default of %d values.",
-             name, variable->min, xbt_cfgelm_type_name[variable->type], size);
-    }
-  }
-  xbt_dict_cursor_free(&cursor);
-}
-
 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
 {
   xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
@@ -435,7 +391,7 @@ static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm
   }
 
   if (!res) {
-    xbt_cfg_help(cfg);
+    xbt_cfg_help();
     fflush(stdout);
     THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
   }
@@ -479,7 +435,7 @@ void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
   char *str;
   int i;
   double d;
-  e_xbt_cfgelm_type_t type = xbt_cfgelm_any; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
+  e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
 
   xbt_ex_t e;
 
@@ -977,9 +933,9 @@ static xbt_cfg_t make_set()
   xbt_cfg_t set = NULL;
 
   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
-  xbt_cfg_register_str(&set, "speed:1_int");
-  xbt_cfg_register_str(&set, "peername:1_string");
-  xbt_cfg_register_str(&set, "user:1_string");
+  xbt_cfg_register_str(&set, "speed:int");
+  xbt_cfg_register_str(&set, "peername:string");
+  xbt_cfg_register_str(&set, "user:string");
 
   return set;
 }                               /* end_of_make_set */
@@ -994,35 +950,6 @@ XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
   xbt_cfg_free(&simgrid_config);
 }
 
-XBT_TEST_UNIT("validation", test_config_validation, "Validation tests")
-{
-  xbt_ex_t e;
-
-  simgrid_config = make_set();
-  xbt_test_add("Having too few elements for speed");
-  xbt_cfg_set_parse("peername:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
-  TRY {
-    xbt_cfg_check();
-  } CATCH(e) {
-    if (e.category != mismatch_error || strncmp(e.msg, "Config elem speed needs", strlen("Config elem speed needs")))
-      xbt_test_fail("Got an exception. msg=%s", e.msg);
-    xbt_ex_free(e);
-  }
-
-  xbt_test_add("Having too much values of 'speed'");
-  xbt_cfg_set_parse("peername:toto:42 user:machin");
-  TRY {
-    xbt_cfg_set_parse("speed:42 speed:24");
-  } CATCH(e) {
-    if (e.category != mismatch_error ||
-        strncmp(e.msg, "Cannot add value 34 to the config elem speed", strlen("Config elem speed needs")))
-      xbt_test_fail("Got an exception. msg=%s", e.msg);
-    xbt_ex_free(e);
-  }
-  xbt_cfg_check();
-  xbt_cfg_free(&simgrid_config);
-}
-
 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
 {
   simgrid_config = make_set();