From 21d00bd29d46795bbe752ca3a674d410f3fc7f1a Mon Sep 17 00:00:00 2001 From: yrutschle Date: Sun, 10 Apr 2022 08:45:01 +0200 Subject: [PATCH] remove globals for hash size --- hash.c | 19 +++++++++---------- hash.h | 2 +- hashtest/htest | Bin 24992 -> 24992 bytes hashtest/htest.c | 4 +++- udp-listener.c | 4 +++- 5 files changed, 16 insertions(+), 13 deletions(-) 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 aa2447526547353ea416ba87418fc12ae95593e5..90a62e30ef31bb95ef126b32291644f1d5a8ab1e 100755 GIT binary patch delta 2416 zcmZWreN0qW7Qc6xkq4GHgC9G5B{P)iI-^CHq;z4ymIpKXW*HC}q1gap*NC>ewwo+$ zWfccA#qQ1PwAI^eZTd$|+Qe=)?b>3ptx>@YO4;-ew5vfE(N!8Be%2x)Mf>)5XF%QT zOW>S)?m54Ee&?KfVPA~yi_rrTO}UXu;@=IPP76|;mc+{06QF;ii#oRztl|f69gUqm z5E*>y^knj$?tk`3NvU#JjV^v6g%G7dsaMw4t87w@7Cj3?h|TmmfuTmOyLEpOA(5G+ zwk4{T_Ui&d4lTjXQ7X2hD~4r4bZ!_%uQ2Ex&{VBi3a}HZ=C=Xv0vuF!L1jLdq%XyG zfE`!6`>d}AD0zZ zc!=8q+-tE-UPFHtAc>MZ4J{scIsZ}MVG;PnqrfQkhl+o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eb(UjEMf<)nC3a~n%y=2ir<~HUj3<6W2syK zlurJhKV)x!cd##e*+&Ymlil1{RZ6=13ReF?CO&?oxRm%_yIGt`9^(`DZzm!C@Y@Ot{^{C#NGVs!qvVggseA=l$e$^%BxU?mc}TwC;#m~|;_JJwVwDZ%VU1@3 zdFp{fB-fjlVlpSig-2OZrD8s~euC`h@6;~G>R;5lfId}QL~J~;;V9|iBO4ws*tsax zTbVMFxGXv7{d_k5KHpdOL(s+~^+*FaZxp2Rpt?lLisVgFtykKLIP*cBg3#h-j zaVunI*SnAi)UV6vU7Tv} +/* 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); }