X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/347996b4a10c4e8579080692afa60e0afb88b60a..6d1fc1c31cb2152b6d20742081118524dbb78d14:/src/xbt/lib.c diff --git a/src/xbt/lib.c b/src/xbt/lib.c index 8e2f0f65fd..068652e41a 100644 --- a/src/xbt/lib.c +++ b/src/xbt/lib.c @@ -6,17 +6,16 @@ /* 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 #include #include #include -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_lib, xbt, - "A dict with keys of type (name, level)"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_lib, xbt, "A dict with keys of type (name, level)"); 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; @@ -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 (size: %u), cannot add a level", xbt_dict_size(lib->dict)); + 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++; } @@ -57,14 +54,14 @@ int xbt_lib_add_level(xbt_lib_t lib, void_f_pvoid_t free_f) void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj) { XBT_DEBUG("xbt_lib_set key '%s:%d' with object %p", key, level, obj); + xbt_assert(level >= 0); 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, NULL); } if (elts[level]) { - XBT_DEBUG("Replace %p by %p element under key '%s:%d'", - elts[level], obj, key, 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]); } @@ -73,6 +70,7 @@ void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj) void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callback) { + xbt_assert(level >= 0); void **elts = xbt_dict_get_or_null(lib->dict, key); if (!elts) { XBT_WARN("no key %s", key); @@ -90,8 +88,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; } @@ -106,6 +103,7 @@ void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callbac void *xbt_lib_get_or_null(xbt_lib_t lib, const char *key, int level) { + xbt_assert(level >= 0); void **elts = xbt_dict_get_or_null(lib->dict, key); return elts ? elts[level] : NULL; } @@ -115,7 +113,9 @@ 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 *xbt_lib_get_level(xbt_dictelm_t elm, int level) +{ + xbt_assert(level >= 0); void **elts = elm->content; return elts ? elts[level] : NULL; }