Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix compilation error on windows.
[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_mallocator_t dict_elm_mallocator = NULL;
16 xbt_mallocator_t dict_het_elm_mallocator = NULL;
17
18 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
19                               unsigned int hash_code, void *content,
20                               void_f_pvoid_t free_f)
21 {
22   xbt_dictelm_t element;
23
24   if (dict->homogeneous) {
25     xbt_assert(!free_f,
26                "Cannot set an individual free function in homogeneous dicts.");
27     element = xbt_mallocator_get(dict_elm_mallocator);
28   } else {
29     xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator);
30     het_element->free_f = free_f;
31     element = &het_element->element;
32   }
33   element->key = xbt_new(char, key_len + 1);
34   memcpy(element->key, 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->next = NULL;
42
43   return element;
44 }
45
46 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
47 {
48   if (element) {
49     char *key = element->key;
50     void *content = element->content;
51     void_f_pvoid_t free_f;
52     if (dict->homogeneous) {
53       free_f = dict->free_f;
54       xbt_mallocator_release(dict_elm_mallocator, element);
55     } else {
56       xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
57       free_f = het_element->free_f;
58       xbt_mallocator_release(dict_het_elm_mallocator, het_element);
59     }
60
61     xbt_free(key);
62     if (free_f && content)
63       free_f(content);
64   }
65 }
66
67 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element,
68                           void *data, void_f_pvoid_t free_ctn)
69 {
70   void_f_pvoid_t free_f;
71   if (dict->homogeneous) {
72     free_f = dict->free_f;
73     xbt_assert(!free_ctn,
74                "Cannot set an individual free function in homogeneous dicts.");
75   } else {
76     xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
77     free_f  = het_element->free_f;
78     het_element->free_f = free_ctn;
79   }
80
81   if (free_f && element->content)
82     free_f(element->content);
83
84   element->content = data;
85 }
86
87 void *dict_elm_mallocator_new_f(void)
88 {
89   return xbt_new(s_xbt_dictelm_t, 1);
90 }
91
92 void *dict_het_elm_mallocator_new_f(void)
93 {
94   return xbt_new(s_xbt_het_dictelm_t, 1);
95 }