summaryrefslogtreecommitdiffstats
path: root/dhash/dhash.h
diff options
context:
space:
mode:
authorJohn Dennis <jdennis@redhat.com>2011-09-06 14:36:59 -0400
committerStephen Gallagher <sgallagh@redhat.com>2012-04-25 07:43:22 -0400
commitadd869cb76e6ba1e3a16923faf83b68148e9278b (patch)
treeec3901426dcadd3b1add34232b1cd776cde03b13 /dhash/dhash.h
parentb597c0e70474f1ee40fd78d006fc1da1a04d4bba (diff)
downloadding-libs2-add869cb76e6ba1e3a16923faf83b68148e9278b.tar.gz
ding-libs2-add869cb76e6ba1e3a16923faf83b68148e9278b.tar.xz
ding-libs2-add869cb76e6ba1e3a16923faf83b68148e9278b.zip
* Resolves: bug #735464 Fix the loop limit used to initialize the table directory, was based on count, now limited to segment_count.
* Do not pre-allocate all buckets based on requested table size. This defeats the point of a dynamic hash table which adjusts it's memory usage depending upon the number of items it holds. Pre-allocation also runs afoul of the table contraction logic, if the table is pre-allocated to a large size it will subsequently try to compact it negating the pre-allocation. Now the initial allocation is restricted to one directory segment (i.e. table->segment_count == 1) * If the caller did not specify the directory_bits and segment_bits then compute them from the requested table size. The directory_size and segment_size must be powers of 2 to accmodate fast arithmetic. An optimal maximum number of buckets would be equal to the anticipated table size. If there were no collisions that would mean each item would be located in it's own bucket whose chain length is 1, the item would be immediatly found upon indexing into the bucket table. * There is a requirment there be at least one directory segment, however contract_table() failed to enforce this requirement. Add a check to enforce the requirement. * If hash_create() or hash_create_ex() failed it left the tbl parameter uninitialized but returned an error code. Now upon failure tbl is initialized to NULL as well as returning an error code. * Clean up how the directory and segment sizes were computed. It had been using a loop and shifting 1 bit at a time, now it shifts all the bits in one operation and assures at least one directory and segment are allocated. * In the event of an early exit from hash_create_ex() due to an error make sure all previously allocated resources are cleaned up by calling hash_destroy(). There was only one place this was missing. hash_destroy() blindly assumed the table had a directory, normally it would unless hash_destroy was called due to an early exit in hash_create_ex(). Modify hash_destroy() to check for the existence of the directory before cleaning up the directory contents. * Update the function documentation in dhash.h to explain each parameter of hash_create() and hash_create_ex(). * Enhance dhash_test.c - To output the random seed used to intialize the random number generator and add command line option to set the random seed. This permits recreating a failed test run. - Add command line parameter to set the initial table size. - Use proper string to long conversion routines when reading command line parameters (e.g. strtoul() instead of atoi()) - Add logic to make sure each test value is unique. * Add additional information to the debug output to show the computed directory_bits, segment_bits, sizes, etc. * Adjust the debug_level conditionals to be less verbose.
Diffstat (limited to 'dhash/dhash.h')
-rw-r--r--dhash/dhash.h79
1 files changed, 58 insertions, 21 deletions
diff --git a/dhash/dhash.h b/dhash/dhash.h
index a10ed24..baa0d6a 100644
--- a/dhash/dhash.h
+++ b/dhash/dhash.h
@@ -203,13 +203,40 @@ const char* hash_error_string(int error);
* Create a new hash table with room for n initial entries. hash_create returns
* an opaque pointer to the new hash table in the table parameter. Functions
* operating on the hash table pass in this hash table pointer. This means you
- * may have as many concurrent hash tables as you want. The delete_callback
- * parameter is a pointer to a function which will be called just prior to a
- * hash entry being deleted. This is useful when the hash value has items which
- * may need to be disposed of. The delete_callback may be NULL.
- * The delete_private_data is data passed to the delete_callback, this way
- * custom callbacks can have a private context to reach data they need to use
- * before performning their operations. delete_private_data may be NULL.
+ * may have as many concurrent hash tables as you want.
+
+ * If the table creation is successful tbl is set to the new table and
+ * HASH_SUCCESS is returned, otherwise tbl will be set to NULL and the
+ * return value will be a HASH error code.
+ *
+ * count
+ * Expected number of entries in the table. This is a tuning
+ * parameter because a dynamic hash table dynamically adjusts it's
+ * internal data strucuture to optimize for the actual number of
+ * entries. The count parameter allows for some more optimization,
+ * however it's not critical to get it right, the table will still
+ * perform well. If you have no idea of how many entries to expect
+ * then pass 0, a reasonable default will be selected.
+ * tbl
+ * Pointer to a hash_table_t variable which is initialized to the
+ * new hash table (i.e. the returned table). If the table fails to
+ * be created due to errors the tbl parameter will be set to NULL
+ * and the return value will be a HASH error code.
+ * delete_callback
+ * Pointer to a function which will be called just prior to a hash
+ * entry being deleted. This is useful when the hash value has
+ * items which may need to be disposed of. The delete_callback may
+ * be NULL.
+ * delete_private_data
+ * Void pointer passed to the delete_callback, this way delete
+ * callbacks can have a private context to reach data they need to
+ * use before performning their operations. delete_private_data
+ * may be NULL.
+ *
+ * hash_create is identical to calling:
+ *
+ * hash_create_ex(count, tbl, 0, 0, 0, 0, NULL, NULL, NULL,
+ * delete_callback, delete_private_data);
*/
int hash_create(unsigned long count, hash_table_t **tbl,
hash_delete_callback *delete_callback,
@@ -217,22 +244,32 @@ int hash_create(unsigned long count, hash_table_t **tbl,
/*
* Create a new hash table and fine tune it's configurable parameters.
- * Refer to CACM article for explanation of parameters.
+ * Refer to CACM article for explanation of parameters. See
+ * hash_create for other parameters and return values.
*
- * directory_bits: number of address bits allocated to top level directory array.
- * segment_bits: number of address bits allocated to segment array.
- * min_load_factor: table contracted when the ratio of entry count to bucket count
- * is less than the min_load_factor the table is contracted.
- * max_load_factor: table expanded when the ratio of entry count to bucket count
- * is greater than the max_load_factor the table is expanded.
- * alloc_func: function pointer for allocation
- * free_func: funciton pointer for freeing memory allocated with alloc_func
- * alloc_private data: data passed to the alloc and free functions so that
- * custom functions can refernce other private data they may
- * need during their execution without having to use global
- * variables.
+ * directory_bits
+ * Number of address bits allocated to top level directory array.
+ * If directory_bits or segment_bits is zero then directory_bits
+ * and segment_bits will be computed based on the count parameter.
+ * segment_bits
+ * number of address bits allocated to segment array.
+ * min_load_factor
+ * The table contracted when the ratio of entry count to bucket count
+ * is less than the min_load_factor the table is contracted.
+ * max_load_factor
+ * The table expanded when the ratio of entry count to bucket count
+ * is greater than the max_load_factor the table is expanded.
+ * alloc_func
+ * Function pointer for allocation.
+ * free_func
+ * Function pointer for freeing memory allocated with alloc_func.
+ * alloc_private_data
+ * Data pointer passed to the alloc and free functions so that
+ * custom functions can refernce other private data they may need
+ * during their execution without having to use global variables.
*
- * Note directory_bits + segment_bits must be <= number of bits in unsigned long
+ * Note directory_bits + segment_bits must be <= number of bits in
+ * unsigned long
*/
int hash_create_ex(unsigned long count, hash_table_t **tbl,
unsigned int directory_bits,