From 7c4a9640028b6bf4ff635caeea75089cdad1a779 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sat, 31 Mar 2012 17:33:58 -0500 Subject: [PATCH] Define a LH_LOAD_FACTOR constant and note the range that it can be set to. Change the resize check from "count > size" to "count >= size" to avoid a potential infinite loop with high load factors and a full hash table. --- linkhash.c | 2 +- linkhash.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/linkhash.c b/linkhash.c index 3a9ba0e..88c0a7c 100644 --- a/linkhash.c +++ b/linkhash.c @@ -125,7 +125,7 @@ int lh_table_insert(struct lh_table *t, void *k, const void *v) unsigned long h, n; t->inserts++; - if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2); + if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2); h = t->hash_fn(k); n = h % t->size; diff --git a/linkhash.h b/linkhash.h index 90f219d..9d89460 100644 --- a/linkhash.h +++ b/linkhash.h @@ -22,6 +22,13 @@ extern "C" { #define LH_PRIME 0x9e370001UL /** + * The fraction of filled hash buckets until an insert will cause the table + * to be resized. + * This can range from just above 0 up to 1.0. + */ +#define LH_LOAD_FACTOR 0.66 + +/** * sentinel pointer value for empty slots */ #define LH_EMPTY (void*)-1 -- 2.7.4