Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I think I just killed a simcall
[simgrid.git] / src / xbt / lib.c
1 /* lib - a generic library, variation over dictionary                    */
2
3 /* Copyright (c) 2011, 2013-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <xbt/asserts.h>
10 #include <xbt/sysdep.h>
11 #include <xbt/log.h>
12 #include <xbt/lib.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_lib, xbt, "A dict with keys of type (name, level)");
15
16 xbt_lib_t xbt_lib_new(void)
17 {
18   xbt_lib_t lib  = xbt_new(s_xbt_lib_t, 1);
19   lib->dict = xbt_dict_new_homogeneous(xbt_free_f);
20   lib->levels = 0;
21   lib->free_f = NULL;
22   return lib;
23 }
24
25 void xbt_lib_free(xbt_lib_t *plib)
26 {
27   xbt_lib_t lib = *plib;
28   if (lib) {
29     xbt_dict_cursor_t cursor;
30     char *key;
31     void **elts;
32     xbt_dict_foreach (lib->dict, cursor, key, elts) {
33       int i;
34       for (i = 0 ; i < lib->levels ; i++)
35         if (elts[i] && lib->free_f[i])
36           lib->free_f[i](elts[i]);
37     }
38     xbt_dict_free(&lib->dict);
39     xbt_free(lib->free_f);
40     xbt_free(lib);
41     *plib = NULL;
42   }
43 }
44
45 int xbt_lib_add_level(xbt_lib_t lib, void_f_pvoid_t free_f)
46 {
47   XBT_DEBUG("xbt_lib_add_level");
48   xbt_assert(xbt_dict_is_empty(lib->dict), "Lib is not empty (size: %u), cannot add a level", xbt_dict_size(lib->dict));
49   lib->free_f = xbt_realloc(lib->free_f, sizeof(void_f_pvoid_t) * (lib->levels + 1));
50   lib->free_f[lib->levels] = free_f;
51   return lib->levels++;
52 }
53
54 void xbt_lib_set(xbt_lib_t lib, const char *key, int level, void *obj)
55 {
56   XBT_DEBUG("xbt_lib_set key '%s:%d' with object %p", key, level, obj);
57   xbt_assert(level >= 0);
58   void **elts = xbt_dict_get_or_null(lib->dict, key);
59   if (!elts) {
60     elts = xbt_new0(void *, lib->levels);
61     xbt_dict_set(lib->dict, key, elts, NULL);
62   }
63   if (elts[level]) {
64     XBT_DEBUG("Replace %p by %p element under key '%s:%d'", elts[level], obj, key, level);
65     if (lib->free_f[level])
66       lib->free_f[level](elts[level]);
67   }
68   elts[level] = obj;
69 }
70
71 void xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callback)
72 {
73   xbt_assert(level >= 0);
74   void **elts = xbt_dict_get_or_null(lib->dict, key);
75   if (!elts) {
76      XBT_WARN("no key %s", key);
77      return;
78   }
79
80   void *obj = elts[level];
81   if (!obj) {
82      XBT_WARN("no key %s at level %d", key, level);
83      return;
84   }
85
86   XBT_DEBUG("Remove %p of key %s at level %d", obj, key, level);
87   elts[level] = NULL;
88
89   /* check if there still remains any elements of this key */
90   int empty = 1;
91   for (int i = 0; i < lib->levels && empty; i++) {
92      if (elts[i] != NULL)
93        empty = 0;
94   }
95   if (empty) {
96     /* there is no element at any level, so delete the key */
97     xbt_dict_remove(lib->dict, key);
98   }
99
100   if (invoke_callback && lib->free_f[level])
101     lib->free_f[level](obj);
102 }
103
104 void *xbt_lib_get_or_null(xbt_lib_t lib, const char *key, int level)
105 {
106   xbt_assert(level >= 0);
107   void **elts = xbt_dict_get_or_null(lib->dict, key);
108   return elts ? elts[level] : NULL;
109 }
110
111 xbt_dictelm_t xbt_lib_get_elm_or_null(xbt_lib_t lib, const char *key)
112 {
113   return xbt_dict_get_elm_or_null(lib->dict, key);
114 }
115
116 void *xbt_lib_get_level(xbt_dictelm_t elm, int level)
117 {
118   xbt_assert(level >= 0);
119   void **elts = elm->content;
120   return elts ? elts[level] : NULL;
121 }
122
123 void xbt_lib_remove(xbt_lib_t lib, const char *key){
124   xbt_dict_remove(lib->dict, key);
125 }