From 102eb8b994c4b3bf55b476b16a46a652bf8ebb28 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 28 Nov 2011 13:56:53 +0100 Subject: [PATCH] Define a struct s_xbt_het_dictelm for holding the free function of a dict element. Create a mallocator for these structs, and use them. --- src/xbt/dict.c | 7 +++++++ src/xbt/dict_elm.c | 25 +++++++++++++++++-------- src/xbt/dict_private.h | 11 ++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/xbt/dict.c b/src/xbt/dict.c index e678a99f37..2bbf177ef7 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -671,12 +671,17 @@ void xbt_dict_preinit(void) if (dict_elm_mallocator != NULL) { /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */ xbt_mallocator_free(dict_elm_mallocator); + xbt_mallocator_free(dict_het_elm_mallocator); } dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f, dict_elm_mallocator_reset_f); + dict_het_elm_mallocator = xbt_mallocator_new(256, + dict_het_elm_mallocator_new_f, + dict_het_elm_mallocator_free_f, + dict_het_elm_mallocator_reset_f); } /** @@ -688,6 +693,8 @@ void xbt_dict_postexit(void) if (dict_elm_mallocator != NULL) { xbt_mallocator_free(dict_elm_mallocator); dict_elm_mallocator = NULL; + xbt_mallocator_free(dict_het_elm_mallocator); + dict_het_elm_mallocator = NULL; } if (all_sizes) { unsigned int count; diff --git a/src/xbt/dict_elm.c b/src/xbt/dict_elm.c index f737b3318e..db60d220eb 100644 --- a/src/xbt/dict_elm.c +++ b/src/xbt/dict_elm.c @@ -22,12 +22,14 @@ XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse, xbt_dict, "Dictionaries internals: post-removal cleanup"); xbt_mallocator_t dict_elm_mallocator = NULL; +xbt_mallocator_t dict_het_elm_mallocator = NULL; xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, unsigned int hash_code, void *content, void_f_pvoid_t free_f) { - xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator); + xbt_het_dictelm_t het_element = xbt_mallocator_get(dict_het_elm_mallocator); + xbt_dictelm_t element = &het_element->element; element->key = xbt_new(char, key_len + 1); memcpy((void *) element->key, (void *) key, key_len); @@ -37,7 +39,7 @@ xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, element->hash_code = hash_code; element->content = content; - element->free_f = free_f; + het_element->free_f = free_f; element->next = NULL; return element; @@ -46,27 +48,34 @@ xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element) { if (element != NULL) { + xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element; xbt_free(element->key); - if (element->free_f != NULL && element->content != NULL) { - element->free_f(element->content); + if (het_element->free_f != NULL && element->content != NULL) { + het_element->free_f(element->content); } - xbt_mallocator_release(dict_elm_mallocator, element); + xbt_mallocator_release(dict_het_elm_mallocator, het_element); } } void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void *data, void_f_pvoid_t free_ctn) { - if (element->free_f && element->content) - element->free_f(element->content); + xbt_het_dictelm_t het_element = (xbt_het_dictelm_t)element; + if (het_element->free_f && element->content) + het_element->free_f(element->content); element->content = data; - element->free_f = free_ctn; + het_element->free_f = free_ctn; } void *dict_elm_mallocator_new_f(void) { return xbt_new(s_xbt_dictelm_t, 1); } + +void *dict_het_elm_mallocator_new_f(void) +{ + return xbt_new(s_xbt_het_dictelm_t, 1); +} diff --git a/src/xbt/dict_private.h b/src/xbt/dict_private.h index 4c5dcc7390..490fe38cbb 100644 --- a/src/xbt/dict_private.h +++ b/src/xbt/dict_private.h @@ -27,11 +27,15 @@ typedef struct s_xbt_dictelm { unsigned int hash_code; void *content; - void_f_pvoid_t free_f; xbt_dictelm_t next; } s_xbt_dictelm_t; +typedef struct s_xbt_het_dictelm { + s_xbt_dictelm_t element; + void_f_pvoid_t free_f; +} s_xbt_het_dictelm_t, *xbt_het_dictelm_t; + typedef struct s_xbt_dict { void_f_pvoid_t free_f; xbt_dictelm_t *table; @@ -48,6 +52,11 @@ extern void *dict_elm_mallocator_new_f(void); #define dict_elm_mallocator_free_f xbt_free_f #define dict_elm_mallocator_reset_f ((void_f_pvoid_t)NULL) +extern xbt_mallocator_t dict_het_elm_mallocator; +extern void *dict_het_elm_mallocator_new_f(void); +#define dict_het_elm_mallocator_free_f xbt_free_f +#define dict_het_elm_mallocator_reset_f ((void_f_pvoid_t)NULL) + /*####[ Function prototypes ]################################################*/ xbt_dictelm_t xbt_dictelm_new(xbt_dict_t dict, const char *key, int key_len, unsigned int hash_code, void *content, -- 2.20.1