diff --git a/kernel-src/include/util.h b/kernel-src/include/util.h index 13915c65..a44491d8 100644 --- a/kernel-src/include/util.h +++ b/kernel-src/include/util.h @@ -2,6 +2,7 @@ #define _UTIL_H #include +#include #define UTIL_ZEROBUFFERSIZE (1024 * 64) @@ -20,6 +21,23 @@ static inline long min(long x, long y) { return x > y ? y : x; } +#define FNV1PRIME 0x100000001b3ull +#define FNV1OFFSET 0xcbf29ce484222325ull + +// FNV-1a hash implementation +static inline uint64_t fnv1ahash(void *buffer, size_t size) { + uint8_t *ptr = buffer; + uint8_t *top = ptr + size; + uint64_t h = FNV1OFFSET; + + while (ptr < top) { + h ^= *ptr++; + h *= FNV1PRIME; + } + + return h; +} + extern uint8_t util_zerobuffer[]; #endif diff --git a/kernel-src/lib/hashtable.c b/kernel-src/lib/hashtable.c index bce6f534..ab1632c0 100644 --- a/kernel-src/lib/hashtable.c +++ b/kernel-src/lib/hashtable.c @@ -3,25 +3,10 @@ #include #include #include - -#define FNV1PRIME 0x100000001b3ull -#define FNV1OFFSET 0xcbf29ce484222325ull +#include static scache_t *hashentrycache; -// FNV-1a hash implementation -static uint64_t hashbuffer(void *buffer, size_t size) { - uint8_t *ptr = buffer; - uint8_t *top = ptr + size; - uint64_t h = FNV1OFFSET; - - while (ptr < top) { - h ^= *ptr++; - h *= FNV1PRIME; - } - - return h; -} static hashentry_t *getentry(hashtable_t *table, void *key, size_t keysize, uintmax_t hash) { uintmax_t tableoffset = hash % table->capacity; @@ -37,7 +22,7 @@ static hashentry_t *getentry(hashtable_t *table, void *key, size_t keysize, uint } int hashtable_set(hashtable_t *table, void *value, void *key, size_t keysize, bool allocate) { - uintmax_t hash = hashbuffer(key, keysize); + uintmax_t hash = fnv1ahash(key, keysize); hashentry_t *entry = getentry(table, key, keysize, hash); @@ -71,7 +56,7 @@ int hashtable_set(hashtable_t *table, void *value, void *key, size_t keysize, bo } int hashtable_get(hashtable_t *table, void **value, void *key, size_t keysize) { - uintmax_t hash = hashbuffer(key, keysize); + uintmax_t hash = fnv1ahash(key, keysize); hashentry_t *entry = getentry(table, key, keysize, hash); @@ -83,7 +68,7 @@ int hashtable_get(hashtable_t *table, void **value, void *key, size_t keysize) { } int hashtable_remove(hashtable_t *table, void *key, size_t keysize) { - uintmax_t hash = hashbuffer(key, keysize); + uintmax_t hash = fnv1ahash(key, keysize); uintmax_t tableoffset = hash % table->capacity; hashentry_t *entry = getentry(table, key, keysize, hash);