diff --git a/hash.c b/hash.c index caaeab0..608a552 100644 --- a/hash.c +++ b/hash.c @@ -42,12 +42,10 @@ typedef void* hash_item; #include "hash.h" -static const int h_keylen = 5; /* How many bits in the hash key? (hash is 2^h_keylen big) */ -static const int hash_size = (1 << h_keylen); /* 8 => 256 */ -static const int keymask = hash_size - 1; /* 8 => 11111111 */ static void* const FREE = NULL; struct hash { + int hash_size; /* Max number of items in the hash */ int item_cnt; /* Number of items in the hash */ gap_array* data; @@ -60,15 +58,16 @@ typedef struct hash hash; static int hash_make_key(hash* h, hash_item item) { - return h->hash_make_key(item) & keymask; + return h->hash_make_key(item) % h->hash_size; } -hash* hash_init(hash_make_key_fn make_key, hash_cmp_item_fn cmp_item) +hash* hash_init(int hash_size, hash_make_key_fn make_key, hash_cmp_item_fn cmp_item) { hash* h = malloc(sizeof(*h)); if (!h) return NULL; + h->hash_size = hash_size; h->item_cnt = 0; h->data = gap_init(hash_size); h->hash_make_key = make_key; @@ -80,7 +79,7 @@ hash* hash_init(hash_make_key_fn make_key, hash_cmp_item_fn cmp_item) /* Return the index following i in h */ static int hash_next_index(hash* h, int i) { - return (i + 1) % hash_size; + return (i + 1) % h->hash_size; } /* Returns the index in h of specified address, -1 if not found @@ -98,7 +97,7 @@ static int hash_find_index(hash* h, hash_item item) fprintf(stderr, "searching %d\n", index); #endif while (cnx != FREE) { - if (cnt++ > hash_size) return -1; + if (cnt++ > h->hash_size) return -1; if (!h->cmp_item(cnx, item)) break; @@ -129,7 +128,7 @@ static int distance(int current_index, hash* h, hash_item item) if (wanted_index <= current_index) return current_index - wanted_index; else - return current_index - wanted_index + hash_size; + return current_index - wanted_index + h->hash_size; } @@ -139,7 +138,7 @@ int hash_insert(hash* h, hash_item new) int index = bubble_wanted_index; gap_array* hash = h->data; - if (h->item_cnt == hash_size) + if (h->item_cnt == h->hash_size) return -1; hash_item curr_item = gap_get(hash, index); @@ -208,7 +207,7 @@ void hash_dump(hash* h, char* filename) } fprintf(out, "\n", h->item_cnt); - for (int i = 0; i < hash_size; i++) { + for (int i = 0; i < h->hash_size; i++) { hash_item item = gap_get(h->data, i); int idx = 0; diff --git a/hash.h b/hash.h index e774186..66d79ab 100644 --- a/hash.h +++ b/hash.h @@ -13,7 +13,7 @@ typedef int (*hash_make_key_fn)(hash_item item); /* Function that compares two items: returns 0 if they are the same */ typedef int (*hash_cmp_item_fn)(hash_item item1, hash_item item2); -hash* hash_init(hash_make_key_fn make_key, hash_cmp_item_fn cmp_item); +hash* hash_init(int hash_size, hash_make_key_fn make_key, hash_cmp_item_fn cmp_item); int hash_insert(hash* h, hash_item new); int hash_remove(hash* h, hash_item item); diff --git a/hashtest/htest b/hashtest/htest index aa24475..90a62e3 100755 Binary files a/hashtest/htest and b/hashtest/htest differ diff --git a/hashtest/htest.c b/hashtest/htest.c index 2c5e688..dacb27e 100644 --- a/hashtest/htest.c +++ b/hashtest/htest.c @@ -19,6 +19,8 @@ #include +/* tests have been written for a hash that holds 32 items */ +#define HASH_SIZE 32 #define STR_LENGTH 16 struct hash_item { @@ -55,7 +57,7 @@ static void htest_next_key(FILE* f, char* action, int* key, char str[STR_LENGTH] int main(int argc, char* argv[]) { - hash* h = hash_init(&hash_make_key, &cmp_item); + hash* h = hash_init(HASH_SIZE, &hash_make_key, &cmp_item); char action; hash_item item; int line = 0; diff --git a/udp-listener.c b/udp-listener.c index 7c45892..0f72193 100644 --- a/udp-listener.c +++ b/udp-listener.c @@ -27,6 +27,8 @@ #include "sslh-conf.h" #include "udp-listener.h" +/* How many concurrent connections we manage */ +#define HASH_SIZE 1024 /* returns date at which this socket times out. */ static int udp_timeout(struct connection* cnx) @@ -91,7 +93,7 @@ static int hash_make_key(hash_item new) * */ void udp_init(struct loop_info* fd_info) { - fd_info->hash_sources = hash_init(&hash_make_key, &cnx_cmp); + fd_info->hash_sources = hash_init(HASH_SIZE, &hash_make_key, &cnx_cmp); }