From 2bda62604fa72db57d51fcd4f88e0039bed8ceb9 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Fri, 12 Jun 2020 13:23:16 -0700 Subject: [PATCH] gallium: add util_hash_table_u64 util_hash_table_u64 is a wrapper to util_hash_table with uint64_t keys. This is similar to hash_table_u64 in mesa. Signed-off-by: Chia-I Wu Reviewed-by: Ryan Neph Reviewed-by: Gert Wollny --- src/gallium/auxiliary/util/u_hash_table.c | 95 +++++++++++++++++++++++ src/gallium/auxiliary/util/u_hash_table.h | 21 +++++ 2 files changed, 116 insertions(+) diff --git a/src/gallium/auxiliary/util/u_hash_table.c b/src/gallium/auxiliary/util/u_hash_table.c index 3b1fa63..093277d 100644 --- a/src/gallium/auxiliary/util/u_hash_table.c +++ b/src/gallium/auxiliary/util/u_hash_table.c @@ -46,6 +46,8 @@ #include "util/u_memory.h" #include "util/u_hash_table.h" +#define XXH_INLINE_ALL +#include "xxhash.h" struct util_hash_table { @@ -297,3 +299,96 @@ util_hash_table_destroy(struct util_hash_table *ht) FREE(ht); } + +static unsigned hash_func_u64(void *key) +{ + return XXH32(key, sizeof(uint64_t), 0); +} + +static int compare_func_u64(void *key1, void *key2) +{ + return *(const uint64_t *)key1 != *(const uint64_t*)key2; +} + +struct util_hash_table_u64 * +util_hash_table_create_u64(void (*destroy)(void *value)) +{ + return (struct util_hash_table_u64 *) + util_hash_table_create(hash_func_u64, + compare_func_u64, + destroy); +} + +enum pipe_error +util_hash_table_set_u64(struct util_hash_table_u64 *ht_u64, + uint64_t key, + void *value) +{ + struct util_hash_table *ht = (struct util_hash_table *)ht_u64; + uint64_t *real_key; + enum pipe_error err; + + real_key = MALLOC(sizeof(*real_key)); + if (!real_key) + return PIPE_ERROR_OUT_OF_MEMORY; + *real_key = key; + + err = util_hash_table_set(ht, real_key, value); + if (err != PIPE_OK) + FREE(real_key); + + return err; +} + +void * +util_hash_table_get_u64(struct util_hash_table_u64 *ht_u64, + uint64_t key) +{ + struct util_hash_table *ht = (struct util_hash_table *)ht_u64; + + return util_hash_table_get(ht, &key); +} + +void +util_hash_table_remove_u64(struct util_hash_table_u64 *ht_u64, + uint64_t key) +{ + struct util_hash_table *ht = (struct util_hash_table *)ht_u64; + unsigned key_hash; + struct cso_hash_iter iter; + struct util_hash_table_item *item; + + key_hash = ht->hash(&key); + iter = util_hash_table_find_iter(ht, &key, key_hash); + + if (cso_hash_iter_is_null(iter)) + return; + + item = util_hash_table_item(iter); + ht->destroy(item->value); + FREE(item->key); + FREE(item); + + cso_hash_erase(ht->cso, iter); +} + +void +util_hash_table_destroy_u64(struct util_hash_table_u64 *ht_u64) +{ + struct util_hash_table *ht = (struct util_hash_table *)ht_u64; + struct cso_hash_iter iter; + struct util_hash_table_item *item; + + iter = cso_hash_first_node(ht->cso); + while (!cso_hash_iter_is_null(iter)) { + item = util_hash_table_item(iter); + ht->destroy(item->value); + FREE(item->key); + FREE(item); + iter = cso_hash_iter_next(iter); + } + + cso_hash_delete(ht->cso); + + FREE(ht); +} diff --git a/src/gallium/auxiliary/util/u_hash_table.h b/src/gallium/auxiliary/util/u_hash_table.h index b4a887b..c724420 100644 --- a/src/gallium/auxiliary/util/u_hash_table.h +++ b/src/gallium/auxiliary/util/u_hash_table.h @@ -47,6 +47,7 @@ extern "C" { * Generic purpose hash table. */ struct util_hash_table; +struct util_hash_table_u64; /** @@ -90,6 +91,26 @@ void util_hash_table_destroy(struct util_hash_table *ht); +struct util_hash_table_u64 * +util_hash_table_create_u64(void (*destroy)(void *value)); + +enum pipe_error +util_hash_table_set_u64(struct util_hash_table_u64 *ht, + uint64_t key, + void *value); + +void * +util_hash_table_get_u64(struct util_hash_table_u64 *ht, + uint64_t key); + + +void +util_hash_table_remove_u64(struct util_hash_table_u64 *ht, + uint64_t key); + +void +util_hash_table_destroy_u64(struct util_hash_table_u64 *ht); + #ifdef __cplusplus } #endif