From d3982d4b5c137078043ac6d091d70710dcb7c054 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Sun, 18 Jul 2010 04:24:24 +0000 Subject: [PATCH] +eina_hash_free_buckets to free buckets without freeing a hash SVN revision: 50315 --- legacy/eina/src/include/eina_hash.h | 1 + legacy/eina/src/lib/eina_hash.c | 35 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/legacy/eina/src/include/eina_hash.h b/legacy/eina/src/include/eina_hash.h index f0c299c1cd..218784c78b 100644 --- a/legacy/eina/src/include/eina_hash.h +++ b/legacy/eina/src/include/eina_hash.h @@ -79,6 +79,7 @@ EAPI Eina_Bool eina_hash_del(Eina_Hash *hash, const void *key, const void *dat EAPI void * eina_hash_find(const Eina_Hash *hash, const void *key) EINA_ARG_NONNULL(1, 2); EAPI void * eina_hash_modify(Eina_Hash *hash, const void *key, const void *data) EINA_ARG_NONNULL(1, 2, 3); EAPI void eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1); +EAPI void eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1); EAPI int eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1); EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash *hash, diff --git a/legacy/eina/src/lib/eina_hash.c b/legacy/eina/src/lib/eina_hash.c index 8623d4e907..ccc10fa87b 100644 --- a/legacy/eina/src/lib/eina_hash.c +++ b/legacy/eina/src/lib/eina_hash.c @@ -802,15 +802,16 @@ eina_hash_population(const Eina_Hash *hash) } /** - * Calls @ref Eina_Free_Cb if one was specified at time of creation, then frees an entire hash table + * Calls @ref Eina_Free_Cb (if one was specified at time of creation) on all hash table + * buckets, then frees the hash table * @param hash The hash table to be freed * * This function frees up all the memory allocated to storing the specified - * hash tale pointed to by @p hash. If no data_free_cb has been passed to the + * hash table pointed to by @p hash. If no data_free_cb has been passed to the * hash at creation time, any entries in the table that the program * has no more pointers for elsewhere may now be lost, so this should only be - * called if the program has lready freed any allocated data in the hash table - * or has the pointers for data in teh table stored elswehere as well. + * called if the program has already freed any allocated data in the hash table + * or has the pointers for data in the table stored elsewhere as well. * * Example: * @code @@ -837,6 +838,32 @@ eina_hash_free(Eina_Hash *hash) free(hash); } +/** + * Calls @ref Eina_Free_Cb (if one was specified at time of creation) on all hash table buckets + * @param hash The hash table to free buckets on + * + * Frees all memory allocated for hash table buckets. Note that the bucket value is not freed + * unless an @ref Eina_Free_Cb was specified at creation time. + * @see Noooo they be stealin' my bucket! + */ +EAPI void +eina_hash_free_buckets(Eina_Hash *hash) +{ + int i; + + EINA_MAGIC_CHECK_HASH(hash); + EINA_SAFETY_ON_NULL_RETURN(hash); + + if (hash->buckets) + { + for (i = 0; i < hash->size; i++) + eina_rbtree_delete(hash->buckets[i], EINA_RBTREE_FREE_CB(_eina_hash_head_free), hash); + free(hash->buckets); + hash->buckets = NULL; + hash->population = 0; + } +} + /** * Adds an entry to the given hash table. *