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 <olvaffe@gmail.com>
Reviewed-by: Ryan Neph <ryanneph@google.com>
Reviewed-by: Gert Wollny <gert.wollny@collabora.com>
macos/master
Chia-I Wu 5 years ago
parent 02c447799f
commit 2bda62604f
  1. 95
      src/gallium/auxiliary/util/u_hash_table.c
  2. 21
      src/gallium/auxiliary/util/u_hash_table.h

@ -46,6 +46,8 @@
#include "util/u_memory.h" #include "util/u_memory.h"
#include "util/u_hash_table.h" #include "util/u_hash_table.h"
#define XXH_INLINE_ALL
#include "xxhash.h"
struct util_hash_table struct util_hash_table
{ {
@ -297,3 +299,96 @@ util_hash_table_destroy(struct util_hash_table *ht)
FREE(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);
}

@ -47,6 +47,7 @@ extern "C" {
* Generic purpose hash table. * Generic purpose hash table.
*/ */
struct util_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); 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 #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save