Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make dicts ready to accept homogeneous dictionaries.
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2011. 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 "dict_private.h"       /* prototypes of this module */
10
11 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict,
13                                 "Dictionaries internals");
14
15 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_add, xbt_dict,
16                         "Dictionaries internals: elements addition");
17 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_search, xbt_dict,
18                         "Dictionaries internals: searching");
19 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_remove, xbt_dict,
20                         "Dictionaries internals: elements removal");
21 XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse, xbt_dict,
22                         "Dictionaries internals: post-removal cleanup");
23
24 xbt_mallocator_t dict_elm_mallocator = NULL;
25 xbt_mallocator_t dict_het_elm_mallocator = NULL;
26
27 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
28                               unsigned int hash_code, void *content,
29                               void_f_pvoid_t free_f)
30 {
31   xbt_dictelm_t element;
32
33   if (dict->homogeneous) {
34     xbt_assert(!free_f,
35                "Cannot set an individual free function in homogeneous dicts.");
36     element = xbt_mallocator_get(dict_elm_mallocator);
37   } else {
38     xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator);
39     het_element->free_f = free_f;
40     element = &het_element->element;
41   }
42   element->key = xbt_new(char, key_len + 1);
43   memcpy(element->key, key, key_len);
44   element->key[key_len] = '\0';
45
46   element->key_len = key_len;
47   element->hash_code = hash_code;
48
49   element->content = content;
50   element->next = NULL;
51
52   return element;
53 }
54
55 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
56 {
57   if (element) {
58     char *key = element->key;
59     void *content = element->content;
60     void_f_pvoid_t free_f;
61     if (dict->homogeneous) {
62       free_f = dict->free_f;
63       xbt_mallocator_release(dict_elm_mallocator, element);
64     } else {
65       xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
66       free_f = het_element->free_f;
67       xbt_mallocator_release(dict_het_elm_mallocator, het_element);
68     }
69
70     xbt_free(key);
71     if (free_f && content)
72       free_f(content);
73   }
74 }
75
76 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element,
77                           void *data, void_f_pvoid_t free_ctn)
78 {
79   void_f_pvoid_t free_f;
80   if (dict->homogeneous) {
81     free_f = dict->free_f;
82     xbt_assert(!free_ctn,
83                "Cannot set an individual free function in homogeneous dicts.");
84   } else {
85     xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
86     free_f  = het_element->free_f;
87     het_element->free_f = free_ctn;
88   }
89
90   if (free_f && element->content)
91     free_f(element->content);
92
93   element->content = data;
94 }
95
96 void *dict_elm_mallocator_new_f(void)
97 {
98   return xbt_new(s_xbt_dictelm_t, 1);
99 }
100
101 void *dict_het_elm_mallocator_new_f(void)
102 {
103   return xbt_new(s_xbt_het_dictelm_t, 1);
104 }