Skip to content

Commit

Permalink
remove globals for hash size
Browse files Browse the repository at this point in the history
  • Loading branch information
yrutschle committed Apr 10, 2022
1 parent cd7afaa commit 21d00bd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
19 changes: 9 additions & 10 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}


Expand All @@ -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);
Expand Down Expand Up @@ -208,7 +207,7 @@ void hash_dump(hash* h, char* filename)
}

fprintf(out, "<hash elem=%d>\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;

Expand Down
2 changes: 1 addition & 1 deletion hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Binary file modified hashtest/htest
Binary file not shown.
4 changes: 3 additions & 1 deletion hashtest/htest.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <string.h>


/* tests have been written for a hash that holds 32 items */
#define HASH_SIZE 32

#define STR_LENGTH 16
struct hash_item {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion udp-listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit 21d00bd

Please sign in to comment.