aboutsummaryrefslogtreecommitdiff
path: root/src/hash.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-07-14 21:02:05 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2016-07-14 21:02:05 +0300
commit5580c3248794803a8e45dd58b45933a32637954d (patch)
tree3067db40ccf49fab472d7fe3df31bae17d24df5b /src/hash.c
parent66eb071ce6202a33dbf53dfc46e52b25d683290c (diff)
downloadgdbm-5580c3248794803a8e45dd58b45933a32637954d.tar.gz
gdbm-5580c3248794803a8e45dd58b45933a32637954d.tar.bz2
Don't use hardcoded constant
* src/gdbmconst.h (GDBM_HASH_BITS): New constant. * src/bucket.c: Use GDBM_HASH_BITS instead of the hardcoded value. * src/findkey.c (_gdbm_findkey): Use _gdbm_hash_key. * src/gdbmtool.c (hash_handler): Use _gdbm_hash_key if the database is open. * src/hash.c (_gdbm_bucket_dir, _gdbm_hash_key): New functions. * src/proto.h (_gdbm_bucket_dir, _gdbm_hash_key): New protos. * src/systems.h (STATBLKSIZE): Take a struct stat as argument. * src/gdbmopen.c (STATBLKSIZE): Takes argument now.
Diffstat (limited to 'src/hash.c')
-rw-r--r--src/hash.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/hash.c b/src/hash.c
index 316785d..1c6cb8e 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -22,11 +22,10 @@
#include "gdbmdefs.h"
-
-/* This hash function computes a 31 bit value. The value is used to index
- the hash directory using the top n bits. It is also used in a hash bucket
- to find the home position of the element by taking the value modulo the
- bucket hash table size. */
+/* This hash function computes a GDBM_HASH_BITS-bit value. The value is used
+ to index the hash directory using the top n bits. It is also used in a
+ hash bucket to find the home position of the element by taking the value
+ modulo the bucket hash table size. */
int
_gdbm_hash (datum key)
@@ -34,7 +33,6 @@ _gdbm_hash (datum key)
unsigned int value; /* Used to compute the hash value. */
int index; /* Used to cycle through random values. */
-
/* Set the initial value from key. */
value = 0x238F13AF * key.dsize;
for (index = 0; index < key.dsize; index++)
@@ -45,3 +43,18 @@ _gdbm_hash (datum key)
/* Return the value. */
return((int) value);
}
+
+int
+_gdbm_bucket_dir (GDBM_FILE dbf, int hash)
+{
+ return hash >> (GDBM_HASH_BITS - dbf->header->dir_bits);
+}
+
+void
+_gdbm_hash_key (GDBM_FILE dbf, datum key, int *hash, int *bucket, int *offset)
+{
+ int hashval = _gdbm_hash (key);
+ *hash = hashval;
+ *bucket = _gdbm_bucket_dir (dbf, hashval);
+ *offset = hashval % dbf->header->bucket_elems;
+}

Return to:

Send suggestions and report system problems to the System administrator.