Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
497cf51baca140c78f8f5eb4d8cf638c57f3d0f0
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over the B-tree concept           */
2
3 /* Copyright (c) 2003-2009 The SimGrid team. All rights reserved.           */
4
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. */
7
8 #include "dict_private.h"       /* prototypes of this module */
9
10 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict,
12                                 "Dictionaries internals");
13
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");
22
23 xbt_mallocator_t dict_elm_mallocator = NULL;
24
25 xbt_dictelm_t xbt_dictelm_new(const char *key,
26                               int key_len,
27                               unsigned int hash_code,
28                               void *content, void_f_pvoid_t free_f)
29 {
30   xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
31
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';
36
37   element->key_len = key_len;
38   element->hash_code = hash_code;
39
40   element->content = content;
41   element->free_f = free_f;
42   element->next = NULL;
43
44   return element;
45 }
46
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);
49
50   element->key = (void*)key;
51
52   element->dictielem = 1; /* please DONT free the key on free */
53   element->key_len = sizeof(uintptr_t);
54   element->hash_code = hash_code;
55
56   element->content = (void*)content;
57   element->free_f = NULL;
58   element->next = NULL;
59
60   return element;
61 }
62
63 void xbt_dictelm_free(xbt_dictelm_t element)
64 {
65   if (element != NULL) {
66     if (!element->dictielem)
67       xbt_free(element->key);
68
69     if (element->free_f != NULL && element->content != NULL) {
70       element->free_f(element->content);
71     }
72
73     xbt_mallocator_release(dict_elm_mallocator, element);
74   }
75 }
76
77 void *dict_elm_mallocator_new_f(void)
78 {
79   return xbt_new(s_xbt_dictelm_t, 1);
80 }
81
82 void dict_elm_mallocator_free_f(void *elem)
83 {
84   xbt_free(elem);
85 }
86
87 void dict_elm_mallocator_reset_f(void *elem)
88 {
89
90 }