Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move the stack as field of SafetyChecker and CommDetChecker
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2014. 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, "Dictionaries internals");
12
13 xbt_mallocator_t dict_elm_mallocator = NULL;
14 xbt_mallocator_t dict_het_elm_mallocator = NULL;
15
16 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, unsigned int hash_code, void *content,
17                               void_f_pvoid_t free_f)
18 {
19   xbt_dictelm_t element;
20
21   if (dict->homogeneous) {
22     xbt_assert(!free_f, "Cannot set an individual free function in homogeneous dicts.");
23     element = xbt_mallocator_get(dict_elm_mallocator);
24   } else {
25     xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator);
26     het_element->free_f = free_f;
27     element = &het_element->element;
28   }
29   element->key = xbt_new(char, key_len + 1);
30   memcpy(element->key, key, key_len);
31   element->key[key_len] = '\0';
32
33   element->key_len = key_len;
34   element->hash_code = hash_code;
35
36   element->content = content;
37   element->next = NULL;
38
39   return element;
40 }
41
42 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
43 {
44   if (element) {
45     char *key = element->key;
46     void *content = element->content;
47     void_f_pvoid_t free_f;
48     if (dict->homogeneous) {
49       free_f = dict->free_f;
50       xbt_mallocator_release(dict_elm_mallocator, element);
51     } else {
52       xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
53       free_f = het_element->free_f;
54       xbt_mallocator_release(dict_het_elm_mallocator, het_element);
55     }
56
57     xbt_free(key);
58     if (free_f && content)
59       free_f(content);
60   }
61 }
62
63 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void *data, void_f_pvoid_t free_ctn)
64 {
65   void_f_pvoid_t free_f;
66   if (dict->homogeneous) {
67     free_f = dict->free_f;
68     xbt_assert(!free_ctn, "Cannot set an individual free function in homogeneous dicts.");
69   } else {
70     xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element;
71     free_f  = het_element->free_f;
72     het_element->free_f = free_ctn;
73   }
74
75   if (free_f && element->content)
76     free_f(element->content);
77
78   element->content = data;
79 }
80
81 void *dict_elm_mallocator_new_f(void)
82 {
83   return xbt_new(s_xbt_dictelm_t, 1);
84 }
85
86 void *dict_het_elm_mallocator_new_f(void)
87 {
88   return xbt_new(s_xbt_het_dictelm_t, 1);
89 }