1 /* dict - a generic dictionary, variation over the B-tree concept */
3 /* Copyright (c) 2003-2009 The SimGrid team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "dict_private.h" /* prototypes of this module */
10 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict,
12 "Dictionaries internals");
14 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_add, xbt_dict,
15 "Dictionaries internals: elements addition");
16 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_search, xbt_dict,
17 "Dictionaries internals: searching");
18 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_remove, xbt_dict,
19 "Dictionaries internals: elements removal");
20 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse, xbt_dict,
21 "Dictionaries internals: post-removal cleanup");
23 xbt_mallocator_t dict_elm_mallocator = NULL;
25 xbt_dictelm_t xbt_dictelm_new(const char *key,
27 unsigned int hash_code,
28 void *content, void_f_pvoid_t free_f)
30 xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
32 element->dictielem = 0; /* please free the key on free */
33 element->key = xbt_new(char, key_len + 1);
34 memcpy((void *)element->key, (void *)key, key_len);
35 element->key[key_len] = '\0';
37 element->key_len = key_len;
38 element->hash_code = hash_code;
40 element->content = content;
41 element->free_f = free_f;
47 xbt_dictelm_t xbt_dictielm_new(uintptr_t key, unsigned int hash_code, uintptr_t content) {
48 xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
50 element->key = (void*)key;
52 element->dictielem = 1; /* please DONT free the key on free */
53 element->key_len = sizeof(uintptr_t);
54 element->hash_code = hash_code;
56 element->content = (void*)content;
57 element->free_f = NULL;
63 void xbt_dictelm_free(xbt_dictelm_t element)
65 if (element != NULL) {
66 if (!element->dictielem)
67 xbt_free(element->key);
69 if (element->free_f != NULL && element->content != NULL) {
70 element->free_f(element->content);
73 xbt_mallocator_release(dict_elm_mallocator, element);
77 void *dict_elm_mallocator_new_f(void)
79 return xbt_new(s_xbt_dictelm_t, 1);
82 void dict_elm_mallocator_free_f(void *elem)
87 void dict_elm_mallocator_reset_f(void *elem)