Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various little cleanups
[simgrid.git] / src / xbt / lib.c
index 4f9313e..2bd8520 100644 (file)
@@ -1,6 +1,6 @@
 /* lib - a generic library, variation over dictionary                    */
 
-/* Copyright (c) 2011, 2013-2014. The SimGrid Team.
+/* Copyright (c) 2011, 2013-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -15,8 +15,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_lib, xbt,
 
 xbt_lib_t xbt_lib_new(void)
 {
-  xbt_lib_t lib;
-  lib = xbt_new(s_xbt_lib_t, 1);
+  xbt_lib_t lib  = xbt_new(s_xbt_lib_t, 1);
   lib->dict = xbt_dict_new_homogeneous(xbt_free_f);
   lib->levels = 0;
   lib->free_f = NULL;
@@ -33,7 +32,7 @@ void xbt_lib_free(xbt_lib_t *plib)
     xbt_dict_foreach (lib->dict, cursor, key, elts) {
       int i;
       for (i = 0 ; i < lib->levels ; i++)
-        if (elts[i])
+        if (elts[i] && lib->free_f[i])
           lib->free_f[i](elts[i]);
     }
     xbt_dict_free(&lib->dict);
@@ -46,10 +45,8 @@ void xbt_lib_free(xbt_lib_t *plib)
 int xbt_lib_add_level(xbt_lib_t lib, void_f_pvoid_t free_f)
 {
   XBT_DEBUG("xbt_lib_add_level");
-  xbt_assert(xbt_dict_is_empty(lib->dict),
-             "Lib is not empty, cannot add a level");
-  lib->free_f = xbt_realloc(lib->free_f,
-                            sizeof(void_f_pvoid_t) * (lib->levels + 1));
+  xbt_assert(xbt_dict_is_empty(lib->dict), "Lib is not empty, cannot add a level");
+  lib->free_f = xbt_realloc(lib->free_f, sizeof(void_f_pvoid_t) * (lib->levels + 1));
   lib->free_f[lib->levels] = free_f;
   return lib->levels++;
 }
@@ -63,9 +60,9 @@ void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj)
     xbt_dict_set(lib->dict, key, elts, NULL);
   }
   if (elts[level]) {
-    XBT_DEBUG("Replace %p by %p element under key '%s:%d'",
-              elts[level], obj, key, level);
-    lib->free_f[level](elts[level]);
+    XBT_DEBUG("Replace %p by %p element under key '%s:%d'", elts[level], obj, key, level);
+    if (lib->free_f[level])
+      lib->free_f[level](elts[level]);
   }
   elts[level] = obj;
 }
@@ -89,8 +86,7 @@ void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callbac
 
   /* check if there still remains any elements of this key */
   int empty = 1;
-  int i;
-  for (i = 0; i < lib->levels && empty; i++) {
+  for (int i = 0; i < lib->levels && empty; i++) {
      if (elts[i] != NULL)
        empty = 0;
   }
@@ -99,7 +95,7 @@ void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callbac
     xbt_dict_remove(lib->dict, key);
   }
 
-  if (invoke_callback)
+  if (invoke_callback && lib->free_f[level])
     lib->free_f[level](obj);
 }