- XBT_DEBUG("xbt_lib_add_level");
-
- lib->free_f = realloc(lib->free_f,sizeof(void_f_pvoid_t)*(lib->levels+1));
- lib->free_f[lib->levels]=free_f;
- return lib->levels++;
-}
-
-void xbt_lib_set(xbt_lib_t lib, const char *name, int level, void *obj)
-{
- XBT_DEBUG("xbt_lib_set key '%s' with object '%p'",name,obj);
- xbt_lib_set_ext(lib, name, strlen(name),level, obj);
-}
-
-void *xbt_lib_get_or_null(xbt_lib_t lib, const char *key, int level)
-{
- unsigned int hash_code = xbt_lib_hash(key);
- xbt_libelm_t current;
-
- if(!lib) xbt_die("Lib is NULL!");
-
- current = lib->table[hash_code & lib->table_size];
- while (current != NULL &&
- (hash_code != current->hash_code || strcmp(key, current->key)))
- current = current->next;
-
- if (current == NULL)
- return NULL;
-
- return (&(current->content))[level];
-}
-
-static void *lib_mallocator_new_f(void)
-{
- return xbt_new(s_xbt_lib_t, 1);
-}
-
-static void lib_mallocator_free_f(void *lib)
-{
- xbt_free(lib);
-}
-
-static void lib_mallocator_reset_f(void *lib)
-{
- /* nothing to do because all fields are
- * initialized in xbt_dict_new
- */
-}
-
-void xbt_lib_reset(xbt_lib_t *lib)
-{
- XBT_DEBUG("xbt_lib_reset");
- int i,j;
- xbt_libelm_t current, next;
- xbt_lib_t l = *lib;
- int levels = l->levels;
- if(!(*lib)) xbt_die("Lib is NULL!");
-
- if (l->count == 0)
- return;
-
- for (i = 0; i <= l->table_size; i++) {
- current = l->table[i];
- while (current) {
- next = current->next;
- xbt_free(current->key);
- for(j=0; j< levels; j++)
- if((&(current->content))[j] && (l->free_f[j]))
- l->free_f[j]((&(current->content))[j]);
- xbt_free(current);
- current = next;
- }
- l->table[i] = NULL;
- }
- xbt_free(l->free_f);
- l->count = 0;
- l->fill = 0;
- l->levels = 0;
- l->free_f = NULL;
-}
-
-int xbt_lib_length(xbt_lib_t lib)
-{
- if(!lib) xbt_die("Lib is NULL!");
- return lib->count;
-}
-
-static void xbt_lib_set_ext(xbt_lib_t lib,
- const char *key,
- int key_len,
- int level,
- void *data)
-{
-
- unsigned int hash_code = xbt_lib_hash_ext(key, key_len);
-
- xbt_libelm_t current, previous = NULL;
- if(!lib) xbt_die("Lib is NULL!");
-
- XBT_DEBUG("ADD '%.*s' hash = '%d', size = '%d', & = '%d' to level : %d",
- key_len,
- key,
- hash_code,
- lib->table_size,
- hash_code & lib->table_size,
- level);
-
- current = lib->table[hash_code & lib->table_size];
- while (current != NULL &&
- (hash_code != current->hash_code || key_len != current->key_len
- || memcmp(key, current->key, key_len))) {
- previous = current;
- current = current->next;
- }
-
- if (current == NULL) {/* this key doesn't exist yet */
- current = xbt_libelm_new(key, key_len, hash_code, data,level,lib->levels);
- lib->count++;
- if (previous == NULL) {
- lib->table[hash_code & lib->table_size] = current;
- lib->fill++;
- if ((lib->fill * 100) / (lib->table_size + 1) > MAX_FILL_PERCENT)
- xbt_lib_rehash(lib);
- } else {
- previous->next = current;
- }
- }
- else
- if( (&(current->content))[level] == NULL )/* this key exist but level is empty */
- {
- (&(current->content))[level] = data;
- }
- else
- {/* there is already an element with the same key: overwrite it */
- XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
- key_len, (char *) (&(current->content))[level],
- key_len, (char *) data, key_len, (char *) key);
- if (current->content != NULL) {
- free((&(current->content))[level]);
- }
- (&(current->content))[level] = data;
- }