Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Due to recent "improvements" in the smpi mpich rma tests, netbsd timeouts. Avoid...
[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
15 xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, unsigned int hash_code, void *content,
16                               void_f_pvoid_t free_f)
17 {
18   xbt_dictelm_t element;
19
20   xbt_assert(!free_f, "Cannot set an individual free function in homogeneous dicts.");
21   element      = xbt_mallocator_get(dict_elm_mallocator);
22   element->key = xbt_new(char, key_len + 1);
23   memcpy(element->key, key, key_len);
24   element->key[key_len] = '\0';
25
26   element->key_len = key_len;
27   element->hash_code = hash_code;
28
29   element->content = content;
30   element->next = NULL;
31
32   return element;
33 }
34
35 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
36 {
37   if (element) {
38     char *key = element->key;
39     void *content = element->content;
40     void_f_pvoid_t free_f;
41     free_f = dict->free_f;
42     xbt_mallocator_release(dict_elm_mallocator, element);
43
44     xbt_free(key);
45     if (free_f && content)
46       free_f(content);
47   }
48 }
49
50 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void *data, void_f_pvoid_t free_ctn)
51 {
52   void_f_pvoid_t free_f;
53   free_f = dict->free_f;
54   xbt_assert(!free_ctn, "Cannot set an individual free function in homogeneous dicts.");
55
56   if (free_f && element->content)
57     free_f(element->content);
58
59   element->content = data;
60 }
61
62 void *dict_elm_mallocator_new_f(void)
63 {
64   return xbt_new(s_xbt_dictelm_t, 1);
65 }