X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1a158b7a4db45818e8f62b7bdac7f0d1bf36d140..5e158336eb3abc9925d13e02d7851cbff00dd095:/src/xbt/dict.c diff --git a/src/xbt/dict.c b/src/xbt/dict.c index 7518075157..d4ed6ce906 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -1,6 +1,6 @@ /* dict - a generic dictionary, variation over hash table */ -/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2004-2011. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -19,20 +19,11 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities 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 ]###############################################################*/ /** * \brief Constructor * \return pointer to the destination - * \see xbt_dict_new_ext(), xbt_dict_free() + * \see xbt_dict_free() * * Creates and initialize a new dictionary with a default hashtable size. */ @@ -40,7 +31,7 @@ xbt_dict_t xbt_dict_new(void) { xbt_dict_t dict; - dict = xbt_mallocator_get(dict_mallocator); + dict = xbt_new(s_xbt_dict_t, 1); dict->table_size = 127; dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1); dict->count = 0; @@ -79,7 +70,7 @@ void xbt_dict_free(xbt_dict_t * dict) } } xbt_free(table); - xbt_mallocator_release(dict_mallocator, *dict); + xbt_free(*dict); *dict = NULL; } } @@ -223,8 +214,8 @@ static void xbt_dict_rehash(xbt_dict_t dict) * \param key the key to set the new data * \param key_len the size of the \a key * \param data the data to add in the dict - * \param free_ctn function to call with (\a key as argument) when - * \a key is removed from the dictionary + * \param free_ctn function to call with (\a data as argument) when + * \a data is removed from the dictionary * * Set the \a data in the structure under the \a key, which can be any kind * of data, as long as its length is provided in \a key_len. @@ -281,8 +272,8 @@ XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict, * \param dict the dict * \param key the key to set the new data * \param data the data to add in the dict - * \param free_ctn function to call with (\a key as argument) when - * \a key is removed from the dictionary + * \param free_ctn function to call with (\a data as argument) when + * \a data is removed from the dictionary * * set the \a data in the structure under the \a key, which is a * null terminated string. @@ -323,7 +314,7 @@ XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, } if (current == NULL) - THROW2(not_found_error, 0, "key %.*s not found", key_len, key); + THROWF(not_found_error, 0, "key %.*s not found", key_len, key); return current->content; } @@ -401,7 +392,7 @@ XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key) current = current->next; if (current == NULL) - THROW1(not_found_error, 0, "key %s not found", key); + THROWF(not_found_error, 0, "key %s not found", key); return current->content; } @@ -457,7 +448,7 @@ XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, } if (current == NULL) - THROW2(not_found_error, 0, "key %.*s not found", key_len, key); + THROWF(not_found_error, 0, "key %.*s not found", key_len, key); if (previous != NULL) { previous->next = current->next; @@ -499,45 +490,7 @@ XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key) XBT_INLINE void xbt_dicti_set(xbt_dict_t dict, uintptr_t key, uintptr_t data) { - - unsigned int hash_code = - xbt_dict_hash_ext((void *) &key, sizeof(uintptr_t)); - - xbt_dictelm_t current, previous = NULL; - xbt_assert(dict); - - XBT_DEBUG("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data, - hash_code, dict->table_size, hash_code & dict->table_size); - current = dict->table[hash_code & dict->table_size]; - while (current != NULL && - (hash_code != current->hash_code - || sizeof(uintptr_t) != current->key_len - || (((uintptr_t) key) != ((uintptr_t) current->key)))) { - previous = current; - current = current->next; - } - - if (current == NULL) { - /* this key doesn't exist yet */ - current = xbt_dictielm_new(key, hash_code, data); - dict->count++; - if (previous == NULL) { - dict->table[hash_code & dict->table_size] = current; - dict->fill++; - if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT) - xbt_dict_rehash(dict); - } else { - previous->next = current; - } - } else { - - /* there is already an element with the same key: overwrite it */ - if (current->content != NULL && current->free_f != NULL) { - current->free_f(current->content); - } - current->content = (void *) data; - current->free_f = NULL; - } + xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL); } /** @@ -551,59 +504,13 @@ XBT_INLINE void xbt_dicti_set(xbt_dict_t dict, */ XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key) { - - unsigned int hash_code = - xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t)); - xbt_dictelm_t current; - - xbt_assert(dict); - - current = dict->table[hash_code & dict->table_size]; - while (current != NULL && - (hash_code != current->hash_code - || sizeof(uintptr_t) != current->key_len - || (((uintptr_t) key) != ((uintptr_t) current->key)))) { - current = current->next; - } - - if (current == NULL) - return 0; - - return (uintptr_t) (current->content); + return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key); } /** Remove a uintptr_t key from the dict */ XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key) { - - unsigned int hash_code = - xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t)); - xbt_dictelm_t current, previous = NULL; - - - current = dict->table[hash_code & dict->table_size]; - while (current != NULL && - (hash_code != current->hash_code - || sizeof(uintptr_t) != current->key_len - || (((uintptr_t) key) != ((uintptr_t) current->key)))) { - previous = current; /* save the previous node */ - current = current->next; - } - - if (current == NULL) - THROW1(not_found_error, 0, "key %zu not found", key); - - if (previous != NULL) { - previous->next = current->next; - } else { - dict->table[hash_code & dict->table_size] = current->next; - } - - if (!dict->table[hash_code & dict->table_size]) - dict->fill--; - - xbt_dictelm_free(current); - dict->count--; + xbt_dict_remove_ext(dict, (void *)&key, sizeof key); } @@ -658,7 +565,7 @@ void xbt_dict_dump_output_string(void *s) */ XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict) { - return (xbt_dict_size(dict) == 0); + return !dict || (xbt_dict_length(dict) == 0); } /** @@ -684,7 +591,7 @@ void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output) while (element != NULL) { printf(" %s -> '", element->key); if (output != NULL) { - (*output) (element->content); + output(element->content); } printf("'\n"); element = element->next; @@ -764,16 +671,11 @@ void xbt_dict_dump_sizes(xbt_dict_t dict) */ void xbt_dict_preinit(void) { - if (dict_mallocator != NULL) { + 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_mallocator); xbt_mallocator_free(dict_elm_mallocator); } - 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, @@ -786,9 +688,7 @@ void xbt_dict_preinit(void) */ void xbt_dict_postexit(void) { - if (dict_mallocator != NULL) { - xbt_mallocator_free(dict_mallocator); - dict_mallocator = NULL; + if (dict_elm_mallocator != NULL) { xbt_mallocator_free(dict_elm_mallocator); dict_elm_mallocator = NULL; } @@ -811,23 +711,6 @@ void xbt_dict_postexit(void) } } -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" @@ -939,9 +822,10 @@ static void search_not_found(xbt_dict_t head, const char *data) TRY { data = xbt_dict_get(head, data); - THROW1(unknown_error, 0, + THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data); - } CATCH(e) { + } + CATCH(e) { if (e.category != not_found_error) xbt_test_exception(e); xbt_ex_free(e); @@ -974,7 +858,8 @@ static void count(xbt_dict_t dict, int length) static void count_check_get_key(xbt_dict_t dict, int length) { xbt_dict_cursor_t cursor; - char *key, *key2; + char *key; + _XBT_GNUC_UNUSED char *key2; void *data; int effective = 0; @@ -989,7 +874,7 @@ static void count_check_get_key(xbt_dict_t dict, int length) xbt_dict_foreach(dict, cursor, key, data) { effective++; key2 = xbt_dict_get_key(dict, data); - xbt_assert2(!strcmp(key, key2), + xbt_assert(!strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key); } @@ -1014,7 +899,8 @@ XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse traverse(head); TRY { debuged_remove(head, "12346"); - } CATCH(e) { + } + CATCH(e) { if (e.category != not_found_error) xbt_test_exception(e); xbt_ex_free(e); @@ -1248,7 +1134,7 @@ static int countelems(xbt_dict_t head) XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") { xbt_dict_t head = NULL; - int i, j, k, nb; + int i, j, k; char *key; void *data; @@ -1263,7 +1149,6 @@ XBT_TEST_UNIT("crash", test_dict_crash, "Crash test") SIZEOFKEY); head = xbt_dict_new(); /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */ - nb = 0; for (j = 0; j < 1000; j++) { char *data = NULL; key = xbt_malloc(SIZEOFKEY);