Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / xbt / dict_elm.c
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2022. 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_mallocator_t dict_elm_mallocator = NULL;
11
12 xbt_dictelm_t xbt_dictelm_new(const char* key, int key_len, unsigned int hash_code, void* content)
13 {
14   xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator);
15   element->key = xbt_new(char, key_len + 1);
16   memcpy(element->key, key, key_len);
17   element->key[key_len] = '\0';
18
19   element->key_len = key_len;
20   element->hash_code = hash_code;
21
22   element->content = content;
23   element->next = NULL;
24
25   return element;
26 }
27
28 void xbt_dictelm_free(const_xbt_dict_t dict, xbt_dictelm_t element)
29 {
30   if (element) {
31     char *key = element->key;
32     void *content = element->content;
33     void_f_pvoid_t free_f;
34     free_f = dict->free_f;
35     xbt_mallocator_release(dict_elm_mallocator, element);
36
37     xbt_free(key);
38     if (free_f && content)
39       free_f(content);
40   }
41 }
42
43 void xbt_dictelm_set_data(const_xbt_dict_t dict, xbt_dictelm_t element, void* data)
44 {
45   void_f_pvoid_t free_f = dict->free_f;
46   if (free_f && element->content)
47     free_f(element->content);
48
49   element->content = data;
50 }
51
52 void *dict_elm_mallocator_new_f(void)
53 {
54   return xbt_new(s_xbt_dictelm_t, 1);
55 }