Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Various cleanups and some more debug
[simgrid.git] / src / xbt / config.c
index 9e982a9..1b13546 100644 (file)
@@ -9,6 +9,7 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include <stdio.h>
 #include "xbt/misc.h"
 #include "xbt/sysdep.h"
 #include "xbt/log.h"
@@ -50,10 +51,6 @@ static void xbt_cfgelm_free(void *data);
 static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
                                   e_xbt_cfgelm_type_t type);
 
-static void xbt_cfg_str_free(void *d){
-  free(*(void**)d);
-}
-
 /*----[ Memory management ]-----------------------------------------------*/
 
 /** @brief Constructor
@@ -80,6 +77,7 @@ xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
   xbt_cfgelm_t variable=NULL;
   char *name=NULL;
   
+  DEBUG1("Copy cfg set %p",tocopy);
   *whereto=NULL;
   xbt_assert0(tocopy,"cannot copy NULL config");
 
@@ -91,6 +89,7 @@ xbt_cfg_cpy(xbt_cfg_t tocopy,xbt_cfg_t *whereto) {
 
 /** @brief Destructor */
 void xbt_cfg_free(xbt_cfg_t *cfg) {
+  DEBUG1("Frees cfg set %p",cfg);
   xbt_dict_free((xbt_dict_t*)cfg);
 }
 
@@ -174,6 +173,7 @@ void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
 void xbt_cfgelm_free(void *data) {
   xbt_cfgelm_t c=(xbt_cfgelm_t)data;
 
+  DEBUG1("Frees cfgelm %p",c);
   if (!c) return;
   xbt_dynar_free(&(c->content));
   free(c);
@@ -195,32 +195,21 @@ xbt_cfg_register(xbt_cfg_t cfg,
                 int min, int max,
                 xbt_cfg_cb_t cb_set,  xbt_cfg_cb_t cb_rm){
   xbt_cfgelm_t res;
-  xbt_ex_t e;
-  int found=0;
 
   xbt_assert(cfg);
   xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_peer,
               "type of %s not valid (%d should be between %d and %d)",
               name,type,xbt_cfgelm_int, xbt_cfgelm_peer);
-  DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",
-        name,min,max,xbt_cfgelm_type_name[type],type);
-  TRY {
-    res = xbt_dict_get((xbt_dict_t)cfg,name);
-  } CATCH(e) {
-    if (e.category == not_found_error) {
-      found = 1;
-      xbt_ex_free(e);
-    } else {
-      RETHROW;
-    }
-  }
+  res = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
 
-  if (!found) {
+  if (res) {
     WARN1("Config elem %s registered twice.",name);
     /* Will be removed by the insertion of the new one */
   } 
 
   res=xbt_new(s_xbt_cfgelm_t,1);
+  DEBUG7("Register cfg elm %s (%d to %d %s (=%d) @%p in set %p)",
+        name,min,max,xbt_cfgelm_type_name[type],type,res,cfg);
 
   res->type=type;
   res->min=min;
@@ -238,11 +227,11 @@ xbt_cfg_register(xbt_cfg_t cfg,
     break;
 
   case xbt_cfgelm_string:
-   res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
+   res->content = xbt_dynar_new(sizeof(char*),xbt_free_ref);
    break;
 
   case xbt_cfgelm_peer:
-   res->content = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
+   res->content = xbt_dynar_new(sizeof(xbt_peer_t),xbt_peer_free_voidp);
    break;
 
   default:
@@ -263,6 +252,7 @@ xbt_cfg_register(xbt_cfg_t cfg,
 
 void
 xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
+  DEBUG2("Unregister elm '%s' from set %p",name,cfg);
   xbt_dict_remove((xbt_dict_t)cfg,name);
 }
 
@@ -284,6 +274,7 @@ xbt_cfg_register_str(xbt_cfg_t cfg,const char *entry) {
 
   int min,max;
   e_xbt_cfgelm_type_t type;
+  DEBUG1("Register string '%s'",entry);
 
   tok=strchr(entrycpy, ':');
   xbt_assert2(tok,"Invalid config element descriptor: %s%s",
@@ -329,6 +320,7 @@ xbt_cfg_check(xbt_cfg_t cfg) {
   int size;
 
   xbt_assert0(cfg,"NULL config set.");
+  DEBUG1("Check cfg set %p",cfg);
 
   xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
     size = xbt_dynar_length(variable->content);
@@ -360,18 +352,11 @@ static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t  cfg,
                                   const char *name,
                                   e_xbt_cfgelm_type_t type){
   xbt_cfgelm_t res=NULL;
-  xbt_ex_t e;
 
-  TRY {
-    res = xbt_dict_get((xbt_dict_t)cfg,name);
-  } CATCH(e) {
-    if (e.category == not_found_error) {
-      xbt_ex_free(e);
-      THROW1(not_found_error,0,
-            "No registered variable '%s' in this config set",name);
-    }
-    RETHROW;
-  }
+  res = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
+  if (!res)
+    THROW1(not_found_error,0,
+          "No registered variable '%s' in this config set",name);
 
   xbt_assert3(type == xbt_cfgelm_any || res->type == type,
               "You tried to access to the config element %s as an %s, but its type is %s.",
@@ -394,18 +379,11 @@ e_xbt_cfgelm_type_t
 xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
 
   xbt_cfgelm_t variable = NULL;
-  xbt_ex_t e;
 
-  TRY {
-    variable = xbt_dict_get((xbt_dict_t)cfg,name);
-  } CATCH(e) {
-    if (e.category == not_found_error) { 
-      xbt_ex_free(e);
-      THROW1(not_found_error,0,
-            "Can't get the type of '%s' since this variable does not exist",name);
-    }
-    RETHROW;
-  }
+  variable = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
+  if (!variable)
+    THROW1(not_found_error,0,
+          "Can't get the type of '%s' since this variable does not exist",name);
 
   INFO1("type in variable = %d",variable->type);
 
@@ -662,7 +640,7 @@ xbt_cfg_set_int(xbt_cfg_t cfg,const char*name, int val) {
           
     xbt_dynar_set(variable->content,0,&val);
   } else {
-    if (variable->max && xbt_dynar_length(variable->content) == variable->max)
+    if (variable->max && xbt_dynar_length(variable->content) == (unsigned long)variable->max)
       THROW3(mismatch_error,0,
              "Cannot add value %d to the config element %s since it's already full (size=%d)",
              val,name,variable->max); 
@@ -721,10 +699,18 @@ xbt_cfg_set_string(xbt_cfg_t cfg,const char*name, const char*val) {
 
   VERB2("Configuration setting: %s=%s",name,val);
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
+  DEBUG5("Variable: %d to %d %s (=%d) @%p",
+        variable->min,variable->max,xbt_cfgelm_type_name[variable->type],variable->type,variable);
 
   if (variable->max == 1) {
-    if (variable->cb_rm && xbt_dynar_length(variable->content))
-      (*variable->cb_rm)(name, 0);
+    if (xbt_dynar_length(variable->content)) {
+       if (variable->cb_rm)
+        (*variable->cb_rm)(name, 0);
+       else if (variable->type == xbt_cfgelm_string) {
+        char * sval=xbt_dynar_get_as(variable->content,0,char*);
+        free(sval);
+       }
+    }
           
     xbt_dynar_set(variable->content,0,&newval);
   } else {
@@ -789,7 +775,8 @@ xbt_cfg_set_peer(xbt_cfg_t cfg,const char*name,
 void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
 
   xbt_cfgelm_t variable;
-  int cpt,seen;
+  unsigned int cpt;
+  int seen;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
   
@@ -819,7 +806,7 @@ void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
 
 void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
   xbt_cfgelm_t variable;
-  int cpt;
+  unsigned int cpt;
   double seen;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
@@ -851,7 +838,7 @@ void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
 void
 xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
   xbt_cfgelm_t variable;
-  int cpt;
+  unsigned int cpt;
   char *seen;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
@@ -885,7 +872,7 @@ xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
 void
 xbt_cfg_rm_peer(xbt_cfg_t cfg,const char*name, const char *peer,int port) {
   xbt_cfgelm_t variable;
-  int cpt;
+  unsigned int cpt;
   xbt_peer_t seen;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
@@ -949,7 +936,7 @@ xbt_cfg_empty(xbt_cfg_t cfg,const char*name) {
 
   if (variable) {
     if (variable->cb_rm) {
-      int cpt;
+      unsigned int cpt;
       void *ignored;
       xbt_dynar_foreach(variable->content,cpt,ignored) {
         (*variable->cb_rm)(name, cpt);
@@ -1195,8 +1182,6 @@ XBT_TEST_UNIT("validation",test_config_validation,"Validation tests") {
 }
 
 XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
-  xbt_cfg_t set = set=make_set();
 
   xbt_test_add0("Get a single value");
   {    
@@ -1223,7 +1208,7 @@ XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
     dyn = xbt_cfg_get_dynar(myset,"user");
 
     if (xbt_dynar_length(dyn) != 3) 
-      xbt_test_fail1("Dynar length = %d, I expected 3", (int)xbt_dynar_length(dyn));
+      xbt_test_fail1("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
 
     if (strcmp(xbt_dynar_get_as(dyn,0,char*),"foo"))
       xbt_test_fail1("Dynar[0] = %s, I expected foo",   xbt_dynar_get_as(dyn,0,char*));