diff --git a/include/lib/hashchain.h b/include/lib/hashchain.h new file mode 100644 index 0000000..eff0108 --- /dev/null +++ b/include/lib/hashchain.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ht_H_ +#define _ht_H_ + +#include + +typedef struct ht_entry { + char *key; + union { + void *ptr; + u64 val; + }; + struct ht_entry *next; +} ht_entry_t; + +typedef struct { + ht_entry_t **buckets; + size_t capacity; + size_t size; +} ht_t; + +ht_t *ht_create(size_t capacity); +void ht_destroy(ht_t *ht); +int ht_insert(ht_t *ht, const char *key, void *value); +void *ht_lookup(ht_t *ht, const char *key); +int ht_delete(ht_t *ht, const char *key); + +#endif /* _ht_H_ */ diff --git a/lib/Makefile b/lib/Makefile index a238d4e..c19dae5 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -8,3 +8,4 @@ obj-y += string.o obj-y += list.o obj-y += sem.o obj-y += rwlock.o +obj-y += hashchain.o diff --git a/lib/hashchain.c b/lib/hashchain.c new file mode 100644 index 0000000..f7b95a4 --- /dev/null +++ b/lib/hashchain.c @@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include + +#define ht_MIN 32 + +static u64 hash_str(const char *str) +{ + u64 hash = 5381; + for (const char *p = str; *p; p++) + hash = ((hash << 5) + hash) + (u64)*p; + return hash; +} + +ht_t *ht_create(size_t capacity) +{ + size_t cap = npow2(capacity); + if (cap < ht_MIN) + cap = ht_MIN; + ht_t *ht = kzalloc(sizeof(ht_t), ALLOC_KERN); + if (!ht) + return NULL; + + ht->buckets = kzalloc(sizeof(ht_entry_t *) * cap, ALLOC_KERN); + if (!ht->buckets) { + kfree(ht); + return NULL; + } + + ht->capacity = cap; + ht->size = 0; + return ht; +} + +void ht_destroy(ht_t *ht) +{ + if (!ht) + return; + + for (size_t i = 0; i < ht->capacity; i++) { + ht_entry_t *entry = ht->buckets[i]; + while (entry) { + ht_entry_t *next = entry->next; + kfree(entry->key); + kfree(entry); + entry = next; + } + } + + kfree(ht->buckets); + kfree(ht); +} + +int ht_insert(ht_t *ht, const char *key, void *value) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + while (entry) { + if (strcmp(entry->key, key) == 0) { + entry->ptr = value; + return 0; + } + entry = entry->next; + } + + entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN); + if (!entry) + return -1; + + entry->key = strdup(key); + if (!entry->key) { + kfree(entry); + return -1; + } + + entry->ptr = value; + entry->next = ht->buckets[idx]; + ht->buckets[idx] = entry; + ht->size++; + return 0; +} + +void *ht_lookup(ht_t *ht, const char *key) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + while (entry) { + if (strcmp(entry->key, key) == 0) + return entry->ptr; + entry = entry->next; + } + + return NULL; +} + +int ht_delete(ht_t *ht, const char *key) +{ + u64 hash = hash_str(key); + size_t idx = hash & (ht->capacity - 1); + + ht_entry_t *entry = ht->buckets[idx]; + ht_entry_t *prev = NULL; + + while (entry) { + if (strcmp(entry->key, key) == 0) { + if (prev) + prev->next = entry->next; + else + ht->buckets[idx] = entry->next; + + kfree(entry->key); + kfree(entry); + ht->size--; + return 0; + } + prev = entry; + entry = entry->next; + } + + return -1; +}