1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2014. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "dict_private.h" /* prototypes of this module */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_elm, xbt_dict, "Dictionaries internals");
13 xbt_mallocator_t dict_elm_mallocator = NULL;
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)
18 xbt_dictelm_t element;
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';
26 element->key_len = key_len;
27 element->hash_code = hash_code;
29 element->content = content;
35 void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t 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);
45 if (free_f && content)
50 void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void *data, void_f_pvoid_t free_ctn)
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.");
56 if (free_f && element->content)
57 free_f(element->content);
59 element->content = data;
62 void *dict_elm_mallocator_new_f(void)
64 return xbt_new(s_xbt_dictelm_t, 1);