summaryrefslogtreecommitdiff
path: root/code/core/htable
diff options
context:
space:
mode:
Diffstat (limited to 'code/core/htable')
-rwxr-xr-xcode/core/htable/hashtable.c31
-rwxr-xr-xcode/core/htable/hashtable.h7
-rw-r--r--code/core/htable/htable.c19
3 files changed, 26 insertions, 31 deletions
diff --git a/code/core/htable/hashtable.c b/code/core/htable/hashtable.c
index 8174a5d..de15827 100755
--- a/code/core/htable/hashtable.c
+++ b/code/core/htable/hashtable.c
@@ -24,34 +24,44 @@ const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
const float max_load_factor = 0.65;
/*****************************************************************************/
-int
-create_hashtable(struct hashtable *h, unsigned int minsize,
+struct hashtable *
+create_hashtable(unsigned int minsize,
unsigned int (*hash_fn) (void*),
int (*eq_fn) (void*,void*),
void *(*malloc_fn) (size_t),
void *(*realloc_fn) (void*,size_t),
void (*free_fn) (void*))
{
+ struct hashtable *h = NULL;
unsigned int pindex, size = primes[0];
+
/* Check requested hashtable isn't too large */
- if (minsize > (1u << 30)) return 0;
+ if (minsize > (1u << 30)) return NULL;
+
+ malloc_fn = malloc_fn ? malloc_fn : malloc;
+ realloc_fn = realloc_fn ? realloc_fn : realloc;
+ free_fn = free_fn ? free_fn : free;
+ h = malloc_fn(sizeof(struct hashtable));
+ if (NULL == h) return NULL;
+ memset(h, 0, sizeof(struct hashtable));
+
/* Enforce size as prime */
for (pindex=0; pindex < prime_table_length; pindex++) {
if (primes[pindex] > minsize) { size = primes[pindex]; break; }
}
- h->fn_malloc = malloc_fn ? malloc_fn : malloc;
- h->fn_realloc = realloc_fn ? realloc_fn : realloc;
- h->fn_free = free_fn ? free_fn : free;
+ h->fn_malloc = malloc_fn;
+ h->fn_realloc = realloc_fn;
+ h->fn_free = free_fn;
h->tablelength = size;
h->primeindex = pindex;
h->entrycount = 0;
h->fn_hash = hash_fn;
h->fn_eq = eq_fn;
h->loadlimit = (unsigned int) ceil(size * max_load_factor);
- h->table = h->fn_malloc(sizeof(struct entry *) * size);
- if (NULL == h->table) return 0; /*oom*/
+ h->table = malloc_fn(sizeof(struct entry *) * size);
+ if (NULL == h->table) { free_fn(h); return NULL; } /*oom*/
memset(h->table, 0, size * sizeof(struct entry *));
- return -1;
+ return h;
}
/*****************************************************************************/
@@ -190,7 +200,7 @@ hashtable_remove(struct hashtable *h, void *k)
unsigned int hashvalue, index;
hashvalue = hashtable_hash(h,k);
- index = indexFor(h->tablelength,hashtable_hash(h,k));
+ index = indexFor(h->tablelength,hashvalue);
pE = &(h->table[index]);
e = *pE;
while (NULL != e)
@@ -227,6 +237,7 @@ hashtable_destroy(struct hashtable *h)
{ f = e; e = e->next; if (h->fn_free_k) h->fn_free_k(f->k); if (h->fn_free_v) h->fn_free_v(f->v); h->fn_free(f); }
}
h->fn_free(h->table);
+ h->fn_free(h);
}
/*
diff --git a/code/core/htable/hashtable.h b/code/core/htable/hashtable.h
index d99f1c4..046f740 100755
--- a/code/core/htable/hashtable.h
+++ b/code/core/htable/hashtable.h
@@ -67,18 +67,17 @@ struct hashtable;
* create_hashtable
* @name create_hashtable
- * @name h hashtable to create
* @param minsize minimum initial size of hashtable
* @param hash_fn function for hashing keys
* @param eq_fn function for determining key equality
* @param malloc_fn function malloc
* @param realloc_fn function realloc
* @param free_fn function free
- * @return non-zero on success
+ * @return hashtable created
*/
-int
-create_hashtable(struct hashtable *h, unsigned int minsize,
+struct hashtable *
+create_hashtable(unsigned int minsize,
unsigned int (*hash_fn) (void*),
int (*eq_fn) (void*,void*),
void *(*malloc_fn) (size_t),
diff --git a/code/core/htable/htable.c b/code/core/htable/htable.c
index 4a9ee11..552a03b 100644
--- a/code/core/htable/htable.c
+++ b/code/core/htable/htable.c
@@ -3,33 +3,17 @@
#include <string.h>
#include "hashtable.h"
-#include "hashtable_private.h"
-
-static unsigned int hash_fn(void *k) {
- return *((unsigned int *)k);
-}
-
-static int eq_fn(void *k1, void *k2) {
- return !memcmp(k1, k2, ECP_ECDH_SIZE_KEY);
-}
static void *h_create(ECPContext *ctx) {
int rv;
- struct hashtable *h = malloc(sizeof(struct hashtable));
+ struct hashtable *h = create_hashtable(1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL);
if (h == NULL) return NULL;
- rv = create_hashtable(h, 1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL);
- if (!rv) {
- free(h);
- return NULL;
- }
-
return h;
}
static void h_destroy(void *h) {
hashtable_destroy(h);
- free(h);
}
static int h_insert(void *h, unsigned char *k, ECPConnection *v) {
@@ -39,6 +23,7 @@ static int h_insert(void *h, unsigned char *k, ECPConnection *v) {
}
static ECPConnection *h_remove(void *h, unsigned char *k) {
+ printf("REMOVE!!!\n");
return hashtable_remove(h, k);
}