X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a21c6f99b54f0893807db95245ba0a05a5afc1c3..41c803093e7108f16bfb4cc7e2c41c52104c73c2:/src/xbt/lib.c diff --git a/src/xbt/lib.c b/src/xbt/lib.c index 1141f8d6a0..34fb42ea79 100644 --- a/src/xbt/lib.c +++ b/src/xbt/lib.c @@ -1,6 +1,6 @@ /* lib - a generic library, variation over dictionary */ -/* Copyright (c) 2011. 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 @@ -17,7 +17,7 @@ xbt_lib_t xbt_lib_new(void) { xbt_lib_t lib; lib = xbt_new(s_xbt_lib_t, 1); - lib->dict = xbt_dict_new(); + lib->dict = xbt_dict_new_homogeneous(xbt_free_f); lib->levels = 0; lib->free_f = NULL; return lib; @@ -60,7 +60,7 @@ void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj) void **elts = xbt_dict_get_or_null(lib->dict, key); if (!elts) { elts = xbt_new0(void *, lib->levels); - xbt_dict_set(lib->dict, key, elts, xbt_free); + xbt_dict_set(lib->dict, key, elts, NULL); } if (elts[level]) { XBT_DEBUG("Replace %p by %p element under key '%s:%d'", @@ -70,8 +70,55 @@ void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj) elts[level] = obj; } +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); return elts ? elts[level] : NULL; } + +xbt_dictelm_t xbt_lib_get_elm_or_null(xbt_lib_t lib, const char *key) +{ + return xbt_dict_get_elm_or_null(lib->dict, key); +} + +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); +}