+eina_hash_free_buckets to free buckets without freeing a hash
authordiscomfitor <discomfitor@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sun, 18 Jul 2010 04:24:24 +0000 (04:24 +0000)
committerdiscomfitor <discomfitor@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sun, 18 Jul 2010 04:24:24 +0000 (04:24 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@50315 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_hash.h
src/lib/eina_hash.c

index f0c299c..218784c 100644 (file)
@@ -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,
index 8623d4e..ccc10fa 100644 (file)
@@ -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
@@ -838,6 +839,32 @@ eina_hash_free(Eina_Hash *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.
  *
  * @p key is expected to be a unique string within the hash table.