Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Key may be part of obj, remove it at the last.
[simgrid.git] / src / xbt / lib.c
index ccb1e0e..7fa4bc7 100644 (file)
@@ -1,6 +1,6 @@
 /* lib - a generic library, variation over dictionary                    */
 
-/* Copyright (c) 2011. The SimGrid Team.
+/* Copyright (c) 2011, 2013-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -70,6 +70,40 @@ void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj)
   elts[level] = obj;
 }
 
+/* for vm */
+void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callback)
+{
+  void **elts = xbt_dict_get_or_null(lib->dict, key);
+  if (!elts) {
+     XBT_WARN("no key %s", key);
+     return;
+  }
+
+  void *obj = elts[level];
+  if (!obj) {
+     XBT_WARN("no key %s at level %d", key, level);
+     return;
+  }
+
+  XBT_DEBUG("Remove %p of key %s at level %d", obj, key, level);
+  elts[level] = NULL;
+
+  /* check if there still remains any elements of this key */
+  int empty = 1;
+  int i;
+  for (i = 0; i < lib->levels && empty; i++) {
+     if (elts[i] != NULL)
+       empty = 0;
+  }
+  if (empty) {
+    /* there is no element at any level, so delete the key */
+    xbt_dict_remove(lib->dict, key);
+  }
+
+  if (invoke_callback)
+    lib->free_f[level](obj);
+}
+
 void *xbt_lib_get_or_null(xbt_lib_t lib, const char *key, int level)
 {
   void **elts = xbt_dict_get_or_null(lib->dict, key);
@@ -85,3 +119,7 @@ void *xbt_lib_get_level(xbt_dictelm_t elm, int level){
   void **elts = elm->content;
   return elts ? elts[level] : NULL;
 }
+
+void xbt_lib_remove(xbt_lib_t lib, const char *key){
+  xbt_dict_remove(lib->dict, key);
+}