From: thiery Date: Mon, 14 Aug 2006 11:17:40 +0000 (+0000) Subject: Use mallocators for dictionaries X-Git-Tag: v3.3~2645 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/81b2c8f6358e3f5c0e1d480a45e8d1b574210177 Use mallocators for dictionaries git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2714 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/xbt/dict.c b/src/xbt/dict.c index c28171f432..6d788f0897 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -10,12 +10,20 @@ #include #include "xbt/ex.h" #include "xbt/log.h" +#include "xbt/mallocator.h" +#include "xbt_modinter.h" #include "dict_private.h" XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict,xbt, "Dictionaries provide the same functionnalities than hash tables"); /*####[ Private prototypes ]#################################################*/ +static xbt_mallocator_t dict_mallocator = NULL; +static void* dict_mallocator_new_f(void); +static void dict_mallocator_free_f(void* dict); +static void dict_mallocator_reset_f(void* dict); + + /*####[ Code ]###############################################################*/ /** @@ -36,14 +44,23 @@ xbt_dict_t xbt_dict_new(void) { * \see xbt_dict_new(), xbt_dict_free() */ xbt_dict_t xbt_dict_new_ext(int hashsize) { - int i; - xbt_dict_t dict = xbt_new0(s_xbt_dict_t, 1); + xbt_dict_t dict; + + if (dict_mallocator == NULL) { + /* first run */ + dict_mallocator = xbt_mallocator_new(256, + dict_mallocator_new_f, + dict_mallocator_free_f, + dict_mallocator_reset_f); + dict_elm_mallocator = xbt_mallocator_new(256, + dict_elm_mallocator_new_f, + dict_elm_mallocator_free_f, + dict_elm_mallocator_reset_f); + } + + dict = xbt_mallocator_get(dict_mallocator); dict->table_size = hashsize; dict->table = xbt_new0(xbt_dictelm_t, dict->table_size); - - for (i = 0; i < hashsize; i++) { - dict->table[i] = NULL; - } dict->count = 0; return dict; @@ -58,17 +75,22 @@ xbt_dict_t xbt_dict_new_ext(int hashsize) { void xbt_dict_free(xbt_dict_t *dict) { int i; xbt_dictelm_t current, previous; + int table_size; + xbt_dictelm_t *table; + if (dict != NULL && *dict != NULL) { - for (i = 0; i < (*dict)->table_size; i++) { - current = (*dict)->table[i]; + table_size = (*dict)->table_size; + table = (*dict)->table; + for (i = 0; i < table_size; i++) { + current = table[i]; while (current != NULL) { previous = current; current = current->next; xbt_dictelm_free(previous); } } - xbt_free((*dict)->table); - xbt_free(*dict); + xbt_free(table); + xbt_mallocator_release(dict_mallocator, *dict); *dict = NULL; } } @@ -397,6 +419,31 @@ void xbt_dict_dump(xbt_dict_t dict, } } +/** + * Destroy the dict mallocators. + * This is an internal XBT function called by xbt_exit(). + */ +void xbt_dict_exit(void) { + if (dict_mallocator != NULL) { + xbt_mallocator_free(dict_mallocator); + xbt_mallocator_free(dict_elm_mallocator); + } +} + +static void* dict_mallocator_new_f(void) { + return xbt_new(s_xbt_dict_t, 1); +} + +static void dict_mallocator_free_f(void* dict) { + xbt_free(dict); +} + +static void dict_mallocator_reset_f(void* dict) { + /* nothing to do because all fields are + * initialized in xbt_dict_new + */ +} + #ifdef SIMGRID_TEST #include "xbt.h" #include "xbt/ex.h" diff --git a/src/xbt/dict_elm.c b/src/xbt/dict_elm.c index 529ea65a66..e08190d46f 100644 --- a/src/xbt/dict_elm.c +++ b/src/xbt/dict_elm.c @@ -17,12 +17,14 @@ XBT_LOG_NEW_SUBCATEGORY(xbt_dict_search,xbt_dict,"Dictionaries internals: search XBT_LOG_NEW_SUBCATEGORY(xbt_dict_remove,xbt_dict,"Dictionaries internals: elements removal"); XBT_LOG_NEW_SUBCATEGORY(xbt_dict_collapse,xbt_dict,"Dictionaries internals: post-removal cleanup"); +xbt_mallocator_t dict_elm_mallocator = NULL; + xbt_dictelm_t xbt_dictelm_new(const char *key, int key_len, void *content, void_f_pvoid_t free_f, xbt_dictelm_t next) { - xbt_dictelm_t element = xbt_new(s_xbt_dictelm_t, 1); + xbt_dictelm_t element = xbt_mallocator_get(dict_elm_mallocator); element->key = xbt_new(char, key_len + 1); strncpy(element->key, key, key_len); @@ -44,6 +46,18 @@ void xbt_dictelm_free(xbt_dictelm_t element) { element->free_f(element->content); } - xbt_free(element); + xbt_mallocator_release(dict_elm_mallocator, element); } } + +void* dict_elm_mallocator_new_f(void) { + return xbt_new(s_xbt_dictelm_t, 1); +} + +void dict_elm_mallocator_free_f(void* elem) { + xbt_free(elem); +} + +void dict_elm_mallocator_reset_f(void* elem) { + +} diff --git a/src/xbt/dict_private.h b/src/xbt/dict_private.h index 2aae85850a..17856d2a4c 100644 --- a/src/xbt/dict_private.h +++ b/src/xbt/dict_private.h @@ -16,6 +16,7 @@ #include "xbt/ex.h" #include "xbt/dynar.h" #include "xbt/dict.h" +#include "xbt/mallocator.h" typedef struct xbt_dictelm_ *xbt_dictelm_t; @@ -37,6 +38,11 @@ typedef struct xbt_dict_cursor_ s_xbt_dict_cursor_t; unsigned int xbt_dict_hash(const char *str); +extern xbt_mallocator_t dict_elm_mallocator; +extern void* dict_elm_mallocator_new_f(void); +extern void dict_elm_mallocator_free_f(void* elem); +extern void dict_elm_mallocator_reset_f(void* elem); + /*####[ Function prototypes ]################################################*/ xbt_dictelm_t xbt_dictelm_new(const char *key, int key_len, diff --git a/src/xbt/xbt_main.c b/src/xbt/xbt_main.c index 927c613022..92765f10c3 100644 --- a/src/xbt/xbt_main.c +++ b/src/xbt/xbt_main.c @@ -45,6 +45,7 @@ xbt_exit(){ if (xbt_initialized == 0) { free(xbt_binary_name); xbt_fifo_exit(); + xbt_dict_exit(); } xbt_log_exit(); } diff --git a/src/xbt_modinter.h b/src/xbt_modinter.h index 628f1ee6ea..f54aef28e0 100644 --- a/src/xbt_modinter.h +++ b/src/xbt_modinter.h @@ -14,5 +14,6 @@ void xbt_log_init(int *argc,char **argv); void xbt_log_exit(void); void xbt_fifo_exit(void); +void xbt_dict_exit(void); #endif /* XBT_MODINTER_H */