Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not trim in split_quoted, that's expensive, and the caller can do it if his input...
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over the B-tree concept           */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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
26 xbt_dictelm_t xbt_dictelm_new(const char *key,
27                               int key_len,
28                               unsigned int hash_code,
29                               void *content, void_f_pvoid_t free_f)
30 {
31   xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
32
33   element->dictielem = 0;       /* please free the key on free */
34   element->key = xbt_new(char, key_len + 1);
35   memcpy((void *) element->key, (void *) key, key_len);
36   element->key[key_len] = '\0';
37
38   element->key_len = key_len;
39   element->hash_code = hash_code;
40
41   element->content = content;
42   element->free_f = free_f;
43   element->next = NULL;
44
45   return element;
46 }
47
48 xbt_dictelm_t xbt_dictielm_new(uintptr_t key, unsigned int hash_code,
49                                uintptr_t content)
50 {
51   xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
52
53   element->key = (void *) key;
54
55   element->dictielem = 1;       /* please DONT free the key on free */
56   element->key_len = sizeof(uintptr_t);
57   element->hash_code = hash_code;
58
59   element->content = (void *) content;
60   element->free_f = NULL;
61   element->next = NULL;
62
63   return element;
64 }
65
66 void xbt_dictelm_free(xbt_dictelm_t element)
67 {
68   if (element != NULL) {
69     if (!element->dictielem)
70       xbt_free(element->key);
71
72     if (element->free_f != NULL && element->content != NULL) {
73       element->free_f(element->content);
74     }
75
76     xbt_mallocator_release(dict_elm_mallocator, element);
77   }
78 }
79
80 void *dict_elm_mallocator_new_f(void)
81 {
82   return xbt_new(s_xbt_dictelm_t, 1);
83 }
84
85 void dict_elm_mallocator_free_f(void *elem)
86 {
87   xbt_free(elem);
88 }
89
90 void dict_elm_mallocator_reset_f(void *elem)
91 {
92
93 }