Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++11 <random> instead of rand().
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "dict_private.h"       /* prototypes of this module */
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict, "Dictionaries internals");
11
12 xbt_mallocator_t dict_elm_mallocator = NULL;
13
14 xbt_dictelm_t xbt_dictelm_new(const char* key, int key_len, unsigned int hash_code, void* content)
15 {
16   xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
17   element->key = xbt_new(char, key_len + 1);
18   memcpy(element->key, key, key_len);
19   element->key[key_len] = '\0';
20
21   element->key_len = key_len;
22   element->hash_code = hash_code;
23
24   element->content = content;
25   element->next = NULL;
26
27   return element;
28 }
29
30 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element)
31 {
32   if (element) {
33     char *key = element->key;
34     void *content = element->content;
35     void_f_pvoid_t free_f;
36     free_f = dict->free_f;
37     xbt_mallocator_release(dict_elm_mallocator, element);
38
39     xbt_free(key);
40     if (free_f && content)
41       free_f(content);
42   }
43 }
44
45 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void* data)
46 {
47   void_f_pvoid_t free_f = dict->free_f;
48   if (free_f && element->content)
49     free_f(element->content);
50
51   element->content = data;
52 }
53
54 void *dict_elm_mallocator_new_f(void)
55 {
56   return xbt_new(s_xbt_dictelm_t, 1);
57 }