mirror of
https://github.com/yrutschle/sslh.git
synced 2025-04-12 23:27:15 +03:00
remove globals for hash size
This commit is contained in:
parent
cd7afaa00d
commit
21d00bd29d
19
hash.c
19
hash.c
@ -42,12 +42,10 @@
|
|||||||
typedef void* hash_item;
|
typedef void* hash_item;
|
||||||
#include "hash.h"
|
#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;
|
static void* const FREE = NULL;
|
||||||
|
|
||||||
struct hash {
|
struct hash {
|
||||||
|
int hash_size; /* Max number of items in the hash */
|
||||||
int item_cnt; /* Number of items in the hash */
|
int item_cnt; /* Number of items in the hash */
|
||||||
gap_array* data;
|
gap_array* data;
|
||||||
|
|
||||||
@ -60,15 +58,16 @@ typedef struct hash hash;
|
|||||||
|
|
||||||
static int hash_make_key(hash* h, hash_item item)
|
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));
|
hash* h = malloc(sizeof(*h));
|
||||||
if (!h) return NULL;
|
if (!h) return NULL;
|
||||||
|
|
||||||
|
h->hash_size = hash_size;
|
||||||
h->item_cnt = 0;
|
h->item_cnt = 0;
|
||||||
h->data = gap_init(hash_size);
|
h->data = gap_init(hash_size);
|
||||||
h->hash_make_key = make_key;
|
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 */
|
/* Return the index following i in h */
|
||||||
static int hash_next_index(hash* h, int i)
|
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
|
/* 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);
|
fprintf(stderr, "searching %d\n", index);
|
||||||
#endif
|
#endif
|
||||||
while (cnx != FREE) {
|
while (cnx != FREE) {
|
||||||
if (cnt++ > hash_size) return -1;
|
if (cnt++ > h->hash_size) return -1;
|
||||||
|
|
||||||
if (!h->cmp_item(cnx, item))
|
if (!h->cmp_item(cnx, item))
|
||||||
break;
|
break;
|
||||||
@ -129,7 +128,7 @@ static int distance(int current_index, hash* h, hash_item item)
|
|||||||
if (wanted_index <= current_index)
|
if (wanted_index <= current_index)
|
||||||
return current_index - wanted_index;
|
return current_index - wanted_index;
|
||||||
else
|
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;
|
int index = bubble_wanted_index;
|
||||||
gap_array* hash = h->data;
|
gap_array* hash = h->data;
|
||||||
|
|
||||||
if (h->item_cnt == hash_size)
|
if (h->item_cnt == h->hash_size)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
hash_item curr_item = gap_get(hash, index);
|
hash_item curr_item = gap_get(hash, index);
|
||||||
@ -208,7 +207,7 @@ void hash_dump(hash* h, char* filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fprintf(out, "<hash elem=%d>\n", h->item_cnt);
|
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);
|
hash_item item = gap_get(h->data, i);
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
|
2
hash.h
2
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 */
|
/* Function that compares two items: returns 0 if they are the same */
|
||||||
typedef int (*hash_cmp_item_fn)(hash_item item1, hash_item item2);
|
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_insert(hash* h, hash_item new);
|
||||||
int hash_remove(hash* h, hash_item item);
|
int hash_remove(hash* h, hash_item item);
|
||||||
|
BIN
hashtest/htest
BIN
hashtest/htest
Binary file not shown.
@ -19,6 +19,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
/* tests have been written for a hash that holds 32 items */
|
||||||
|
#define HASH_SIZE 32
|
||||||
|
|
||||||
#define STR_LENGTH 16
|
#define STR_LENGTH 16
|
||||||
struct hash_item {
|
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[])
|
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;
|
char action;
|
||||||
hash_item item;
|
hash_item item;
|
||||||
int line = 0;
|
int line = 0;
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#include "sslh-conf.h"
|
#include "sslh-conf.h"
|
||||||
#include "udp-listener.h"
|
#include "udp-listener.h"
|
||||||
|
|
||||||
|
/* How many concurrent connections we manage */
|
||||||
|
#define HASH_SIZE 1024
|
||||||
|
|
||||||
/* returns date at which this socket times out. */
|
/* returns date at which this socket times out. */
|
||||||
static int udp_timeout(struct connection* cnx)
|
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)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user