Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New actions for the time independent trace replay framework:
[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_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict,
12                                 "Dictionaries internals");
13
14 xbt_mallocator_t dict_elm_mallocator = NULL;
15 xbt_mallocator_t dict_het_elm_mallocator = NULL;
16
17 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len,
18                               unsigned int hash_code, void *content,
19                               void_f_pvoid_t free_f)
20 {
21   xbt_dictelm_t element;
22
23   if (dict->homogeneous) {
24     xbt_assert(!free_f,
25                "Cannot set an individual free function in homogeneous dicts.");
26     element = xbt_mallocator_get(dict_elm_mallocator);
27   } else {
28     xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator);
29     het_element->free_f = free_f;
30     element = &het_element->element;
31   }
32   element->key = xbt_new(char, key_len + 1);
33   memcpy(element->key, key, key_len);
34   element->key[key_len] = '\0';
35
36   element->key_len = key_len;
37   element->hash_code = hash_code;
38
39   element->content = content;
40   element->next = NULL;
41
42   return element;
43 }
44
45 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
46 {
47   if (element) {
48     char *key = element->key;
49     void *content = element->content;
50     void_f_pvoid_t free_f;
51     if (dict->homogeneous) {
52       free_f = dict->free_f;
53       xbt_mallocator_release(dict_elm_mallocator, element);
54     } else {
55       xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
56       free_f = het_element->free_f;
57       xbt_mallocator_release(dict_het_elm_mallocator, het_element);
58     }
59
60     xbt_free(key);
61     if (free_f && content)
62       free_f(content);
63   }
64 }
65
66 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element,
67                           void *data, void_f_pvoid_t free_ctn)
68 {
69   void_f_pvoid_t free_f;
70   if (dict->homogeneous) {
71     free_f = dict->free_f;
72     xbt_assert(!free_ctn,
73                "Cannot set an individual free function in homogeneous dicts.");
74   } else {
75     xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
76     free_f  = het_element->free_f;
77     het_element->free_f = free_ctn;
78   }
79
80   if (free_f && element->content)
81     free_f(element->content);
82
83   element->content = data;
84 }
85
86 void *dict_elm_mallocator_new_f(void)
87 {
88   return xbt_new(s_xbt_dictelm_t, 1);
89 }
90
91 void *dict_het_elm_mallocator_new_f(void)
92 {
93   return xbt_new(s_xbt_het_dictelm_t, 1);
94 }