gallium/hash_table: turn it into a wrapper around util/hash_table

Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3722>
This commit is contained in:
Marek Olšák
2020-02-05 14:47:36 -05:00
committed by Marge Bot
parent 10d235a843
commit 502840855a
29 changed files with 74 additions and 281 deletions
@@ -65,7 +65,7 @@ enum pipe_error
pb_validate_add_buffer(struct pb_validate *vl,
struct pb_buffer *buf,
enum pb_usage_flags flags,
struct util_hash_table *ht,
struct hash_table *ht,
boolean *already_present)
{
assert(buf);
@@ -46,7 +46,7 @@ extern "C" {
struct pb_buffer;
struct pipe_fence_handle;
struct util_hash_table;
struct hash_table;
/**
@@ -61,7 +61,7 @@ enum pipe_error
pb_validate_add_buffer(struct pb_validate *vl,
struct pb_buffer *buf,
enum pb_usage_flags flags,
struct util_hash_table *ht,
struct hash_table *ht,
boolean *already_present);
enum pipe_error
+1 -1
View File
@@ -82,7 +82,7 @@ struct debug_flush_ctx {
/* Contexts are used by a single thread at a time */
unsigned bt_depth;
boolean catch_map_of_referenced;
struct util_hash_table *ref_hash;
struct hash_table *ref_hash;
struct list_head head;
};
+1 -1
View File
@@ -54,7 +54,7 @@ static FILE *stream;
*/
static mtx_t serials_mutex = _MTX_INITIALIZER_NP;
static struct util_hash_table *serials_hash;
static struct hash_table *serials_hash;
static unsigned serials_last;
+1 -1
View File
@@ -46,7 +46,7 @@
#include "os/os_thread.h"
#include "u_hash_table.h"
struct util_hash_table* symbols_hash;
struct hash_table* symbols_hash;
static mtx_t symbols_mutex = _MTX_INITIALIZER_NP;
/* TODO with some refactoring we might be able to re-use debug_symbol_name_cached()
+1 -1
View File
@@ -270,7 +270,7 @@ debug_symbol_print(const void *addr)
debug_printf("\t%s\n", buf);
}
struct util_hash_table* symbols_hash;
struct hash_table* symbols_hash;
static mtx_t symbols_mutex = _MTX_INITIALIZER_NP;
const char*
+26 -227
View File
@@ -25,24 +25,10 @@
*
**************************************************************************/
/**
* @file
* General purpose hash table implementation.
*
* Just uses the cso_hash for now, but it might be better switch to a linear
* probing hash table implementation at some point -- as it is said they have
* better lookup and cache performance and it appears to be possible to write
* a lock-free implementation of such hash tables .
*
* @author José Fonseca <jfonseca@vmware.com>
*/
#include "pipe/p_compiler.h"
#include "util/u_debug.h"
#include "cso_cache/cso_hash.h"
#include "util/u_memory.h"
#include "util/u_hash_table.h"
#include "util/hash_table.h"
@@ -52,50 +38,11 @@
#endif
struct util_hash_table
{
struct cso_hash cso;
/** Hash function */
uint32_t (*hash)(const void *key);
/** Compare two keys */
bool (*equal)(const void *key1, const void *key2);
/* TODO: key, value destructors? */
};
struct util_hash_table_item
{
void *key;
void *value;
};
static inline struct util_hash_table_item *
util_hash_table_item(struct cso_hash_iter iter)
{
return (struct util_hash_table_item *)cso_hash_iter_data(iter);
}
struct util_hash_table *
struct hash_table *
util_hash_table_create(uint32_t (*hash)(const void *key),
bool (*equal)(const void *key1, const void *key2))
{
struct util_hash_table *ht;
ht = MALLOC_STRUCT(util_hash_table);
if (!ht)
return NULL;
cso_hash_init(&ht->cso);
ht->hash = hash;
ht->equal = equal;
return ht;
return _mesa_hash_table_create(NULL, hash, equal);
}
@@ -113,10 +60,10 @@ pointer_equal(const void *a, const void *b)
}
struct util_hash_table *
struct hash_table *
util_hash_table_create_ptr_keys(void)
{
return util_hash_table_create(pointer_hash, pointer_equal);
return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
}
@@ -154,220 +101,72 @@ static bool equal_fd(const void *key1, const void *key2)
}
struct util_hash_table *
struct hash_table *
util_hash_table_create_fd_keys(void)
{
return util_hash_table_create(hash_fd, equal_fd);
}
static inline struct cso_hash_iter
util_hash_table_find_iter(struct util_hash_table *ht,
void *key,
unsigned key_hash)
{
struct cso_hash_iter iter;
struct util_hash_table_item *item;
iter = cso_hash_find(&ht->cso, key_hash);
while (!cso_hash_iter_is_null(iter)) {
item = (struct util_hash_table_item *)cso_hash_iter_data(iter);
if (ht->equal(item->key, key))
break;
iter = cso_hash_iter_next(iter);
}
return iter;
}
static inline struct util_hash_table_item *
util_hash_table_find_item(struct util_hash_table *ht,
void *key,
unsigned key_hash)
{
struct cso_hash_iter iter;
struct util_hash_table_item *item;
iter = cso_hash_find(&ht->cso, key_hash);
while (!cso_hash_iter_is_null(iter)) {
item = (struct util_hash_table_item *)cso_hash_iter_data(iter);
if (ht->equal(item->key, key))
return item;
iter = cso_hash_iter_next(iter);
}
return NULL;
return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
}
enum pipe_error
util_hash_table_set(struct util_hash_table *ht,
util_hash_table_set(struct hash_table *ht,
void *key,
void *value)
{
unsigned key_hash;
struct util_hash_table_item *item;
struct cso_hash_iter iter;
assert(ht);
if (!ht)
return PIPE_ERROR_BAD_INPUT;
key_hash = ht->hash(key);
item = util_hash_table_find_item(ht, key, key_hash);
if (item) {
/* TODO: key/value destruction? */
item->value = value;
return PIPE_OK;
}
item = MALLOC_STRUCT(util_hash_table_item);
if (!item)
return PIPE_ERROR_OUT_OF_MEMORY;
item->key = key;
item->value = value;
iter = cso_hash_insert(&ht->cso, key_hash, item);
if(cso_hash_iter_is_null(iter)) {
FREE(item);
return PIPE_ERROR_OUT_OF_MEMORY;
}
_mesa_hash_table_insert(ht, key, value);
return PIPE_OK;
}
void *
util_hash_table_get(struct util_hash_table *ht,
util_hash_table_get(struct hash_table *ht,
void *key)
{
unsigned key_hash;
struct util_hash_table_item *item;
struct hash_entry *entry = _mesa_hash_table_search(ht, key);
assert(ht);
if (!ht)
return NULL;
key_hash = ht->hash(key);
item = util_hash_table_find_item(ht, key, key_hash);
if (!item)
return NULL;
return item->value;
return entry ? entry->data : NULL;
}
void
util_hash_table_remove(struct util_hash_table *ht,
util_hash_table_remove(struct hash_table *ht,
void *key)
{
unsigned key_hash;
struct cso_hash_iter iter;
struct util_hash_table_item *item;
assert(ht);
if (!ht)
return;
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);
assert(item);
FREE(item);
cso_hash_erase(&ht->cso, iter);
_mesa_hash_table_remove_key(ht, key);
}
void
util_hash_table_clear(struct util_hash_table *ht)
util_hash_table_clear(struct hash_table *ht)
{
struct cso_hash_iter iter;
struct util_hash_table_item *item;
assert(ht);
if (!ht)
return;
iter = cso_hash_first_node(&ht->cso);
while (!cso_hash_iter_is_null(iter)) {
item = (struct util_hash_table_item *)cso_hash_take(&ht->cso, cso_hash_iter_key(iter));
FREE(item);
iter = cso_hash_first_node(&ht->cso);
}
_mesa_hash_table_clear(ht, NULL);
}
enum pipe_error
util_hash_table_foreach(struct util_hash_table *ht,
enum pipe_error (*callback)
util_hash_table_foreach(struct hash_table *ht,
enum pipe_error (*callback)
(void *key, void *value, void *data),
void *data)
void *data)
{
struct cso_hash_iter iter;
struct util_hash_table_item *item;
enum pipe_error result;
assert(ht);
if (!ht)
return PIPE_ERROR_BAD_INPUT;
iter = cso_hash_first_node(&ht->cso);
while (!cso_hash_iter_is_null(iter)) {
item = (struct util_hash_table_item *)cso_hash_iter_data(iter);
result = callback(item->key, item->value, data);
if(result != PIPE_OK)
return result;
iter = cso_hash_iter_next(iter);
hash_table_foreach(ht, entry) {
enum pipe_error error = callback((void*)entry->key, entry->data, data);
if (error != PIPE_OK)
return error;
}
return PIPE_OK;
}
static enum pipe_error
util_hash_inc(UNUSED void *k, UNUSED void *v, void *d)
{
++*(size_t *)d;
return PIPE_OK;
}
size_t
util_hash_table_count(struct util_hash_table *ht)
util_hash_table_count(struct hash_table *ht)
{
size_t count = 0;
util_hash_table_foreach(ht, util_hash_inc, &count);
return count;
return _mesa_hash_table_num_entries(ht);
}
void
util_hash_table_destroy(struct util_hash_table *ht)
util_hash_table_destroy(struct hash_table *ht)
{
struct cso_hash_iter iter;
struct util_hash_table_item *item;
assert(ht);
if (!ht)
return;
iter = cso_hash_first_node(&ht->cso);
while (!cso_hash_iter_is_null(iter)) {
item = (struct util_hash_table_item *)cso_hash_iter_data(iter);
FREE(item);
iter = cso_hash_iter_next(iter);
}
cso_hash_deinit(&ht->cso);
FREE(ht);
_mesa_hash_table_destroy(ht, NULL);
}
+11 -17
View File
@@ -27,8 +27,6 @@
/**
* General purpose hash table.
*
* @author José Fonseca <jfonseca@vmware.com>
*/
#ifndef U_HASH_TABLE_H_
@@ -42,11 +40,7 @@
extern "C" {
#endif
/**
* Generic purpose hash table.
*/
struct util_hash_table;
struct hash_table;
/**
@@ -55,56 +49,56 @@ struct util_hash_table;
* @param hash hash function
* @param equal should return true for two equal keys.
*/
struct util_hash_table *
struct hash_table *
util_hash_table_create(uint32_t (*hash)(const void *key),
bool (*equal)(const void *key1, const void *key2));
/**
* Create a hash table where the keys are generic pointers.
*/
struct util_hash_table *
struct hash_table *
util_hash_table_create_ptr_keys(void);
/**
* Create a hash table where the keys are device FDs.
*/
struct util_hash_table *
struct hash_table *
util_hash_table_create_fd_keys(void);
enum pipe_error
util_hash_table_set(struct util_hash_table *ht,
util_hash_table_set(struct hash_table *ht,
void *key,
void *value);
void *
util_hash_table_get(struct util_hash_table *ht,
util_hash_table_get(struct hash_table *ht,
void *key);
void
util_hash_table_remove(struct util_hash_table *ht,
util_hash_table_remove(struct hash_table *ht,
void *key);
void
util_hash_table_clear(struct util_hash_table *ht);
util_hash_table_clear(struct hash_table *ht);
enum pipe_error
util_hash_table_foreach(struct util_hash_table *ht,
util_hash_table_foreach(struct hash_table *ht,
enum pipe_error (*callback)
(void *key, void *value, void *data),
void *data);
size_t
util_hash_table_count(struct util_hash_table *ht);
util_hash_table_count(struct hash_table *ht);
void
util_hash_table_destroy(struct util_hash_table *ht);
util_hash_table_destroy(struct hash_table *ht);
#ifdef __cplusplus
+2 -2
View File
@@ -71,8 +71,8 @@ struct lima_screen {
/* bo table */
mtx_t bo_table_lock;
mtx_t bo_cache_lock;
struct util_hash_table *bo_handles;
struct util_hash_table *bo_flink_names;
struct hash_table *bo_handles;
struct hash_table *bo_flink_names;
struct list_head bo_cache_buckets[NR_BO_CACHE_BUCKETS];
struct list_head bo_cache_time;
+1 -1
View File
@@ -71,7 +71,7 @@ struct v3d_screen {
const struct v3d_compiler *compiler;
struct util_hash_table *bo_handles;
struct hash_table *bo_handles;
mtx_t bo_handles_mutex;
uint32_t bo_size;
+1 -1
View File
@@ -87,7 +87,7 @@ struct vc4_screen {
uint32_t bo_count;
} bo_cache;
struct util_hash_table *bo_handles;
struct hash_table *bo_handles;
mtx_t bo_handles_mutex;
uint32_t bo_size;
+2 -2
View File
@@ -431,7 +431,7 @@ struct pipe_h264_enc_picture_desc
bool not_referenced;
bool enable_vui;
struct util_hash_table *frame_idx;
struct hash_table *frame_idx;
};
@@ -513,7 +513,7 @@ struct pipe_h265_enc_picture_desc
unsigned ref_idx_l0;
unsigned ref_idx_l1;
bool not_referenced;
struct util_hash_table *frame_idx;
struct hash_table *frame_idx;
};
struct pipe_h265_sps
+4 -4
View File
@@ -32,7 +32,7 @@
#include "nine_state.h"
struct gen_mipmap_state;
struct util_hash_table;
struct hash_table;
struct pipe_screen;
struct pipe_context;
struct cso_context;
@@ -102,8 +102,8 @@ struct NineDevice9
struct gen_mipmap_state *gen_mipmap;
struct {
struct util_hash_table *ht_vs;
struct util_hash_table *ht_ps;
struct hash_table *ht_vs;
struct hash_table *ht_ps;
struct NineVertexShader9 *vs;
struct NinePixelShader9 *ps;
unsigned num_vs;
@@ -111,7 +111,7 @@ struct NineDevice9
float *vs_const;
float *ps_const;
struct util_hash_table *ht_fvf;
struct hash_table *ht_fvf;
} ff;
struct {
+1 -1
View File
@@ -60,7 +60,7 @@ struct NineUnknown
const GUID **guids; /* for QueryInterface */
/* for [GS]etPrivateData/FreePrivateData */
struct util_hash_table *pdata;
struct hash_table *pdata;
void (*dtor)(void *data); /* top-level dtor */
};
+1 -1
View File
@@ -27,7 +27,7 @@
#include "pipe/p_state.h"
struct pipe_screen;
struct util_hash_table;
struct hash_table;
struct NineDevice9;
struct NineResource9
+1 -1
View File
@@ -28,7 +28,7 @@
#include "pipe/p_state.h"
#include "util/u_inlines.h"
struct util_hash_table;
struct hash_table;
struct NineDevice9;
@@ -155,7 +155,7 @@ struct h264d_prc
struct pipe_video_codec *codec;
struct pipe_video_buffer *target;
enum pipe_video_profile profile;
struct util_hash_table *video_buffer_map;
struct hash_table *video_buffer_map;
union {
struct {
unsigned nal_ref_idc;
@@ -48,7 +48,7 @@
#define AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS 0x1E
#endif
static struct util_hash_table *dev_tab = NULL;
static struct hash_table *dev_tab = NULL;
static simple_mtx_t dev_tab_mutex = _SIMPLE_MTX_INITIALIZER_NP;
DEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)
@@ -99,7 +99,7 @@ struct amdgpu_winsys {
/* For returning the same amdgpu_winsys_bo instance for exported
* and re-imported buffers. */
struct util_hash_table *bo_export_table;
struct hash_table *bo_export_table;
simple_mtx_t bo_export_table_lock;
};
@@ -67,7 +67,7 @@ screen_create(struct renderonly *ro)
return etna_screen_create(dev, gpu, ro);
}
static struct util_hash_table *etna_tab = NULL;
static struct hash_table *etna_tab = NULL;
static mtx_t etna_screen_mutex = _MTX_INITIALIZER_NP;
@@ -38,7 +38,7 @@
#include "freedreno/freedreno_screen.h"
static struct util_hash_table *fd_tab = NULL;
static struct hash_table *fd_tab = NULL;
static mtx_t fd_screen_mutex = _MTX_INITIALIZER_NP;
@@ -34,7 +34,7 @@
#include "lima/lima_screen.h"
static struct util_hash_table *fd_tab = NULL;
static struct hash_table *fd_tab = NULL;
static mtx_t lima_screen_mutex = _MTX_INITIALIZER_NP;
static void
@@ -17,7 +17,7 @@
#include <nvif/class.h>
#include <nvif/cl0080.h>
static struct util_hash_table *fd_tab = NULL;
static struct hash_table *fd_tab = NULL;
static mtx_t nouveau_screen_mutex = _MTX_INITIALIZER_NP;
@@ -41,7 +41,7 @@
#include <fcntl.h>
#include <radeon_surface.h>
static struct util_hash_table *fd_tab = NULL;
static struct hash_table *fd_tab = NULL;
static mtx_t fd_tab_mutex = _MTX_INITIALIZER_NP;
/* Enable/disable feature access for one command stream.
@@ -77,11 +77,11 @@ struct radeon_drm_winsys {
uint32_t accel_working2;
/* List of buffer GEM names. Protected by bo_handles_mutex. */
struct util_hash_table *bo_names;
struct hash_table *bo_names;
/* List of buffer handles. Protectded by bo_handles_mutex. */
struct util_hash_table *bo_handles;
struct hash_table *bo_handles;
/* List of buffer virtual memory ranges. Protectded by bo_handles_mutex. */
struct util_hash_table *bo_vas;
struct hash_table *bo_vas;
mtx_t bo_handles_mutex;
mtx_t bo_fence_lock;
+1 -1
View File
@@ -95,7 +95,7 @@ struct vmw_svga_winsys_context
struct svga_winsys_context base;
struct vmw_winsys_screen *vws;
struct util_hash_table *hash;
struct hash_table *hash;
#ifdef DEBUG
boolean must_flush;
+1 -1
View File
@@ -41,7 +41,7 @@
#include <unistd.h>
#include <fcntl.h>
static struct util_hash_table *dev_hash = NULL;
static struct hash_table *dev_hash = NULL;
static bool vmw_dev_compare(const void *key1, const void *key2)
{
@@ -1004,7 +1004,7 @@ virgl_drm_winsys_create(int drmFD)
}
static struct util_hash_table *fd_tab = NULL;
static struct hash_table *fd_tab = NULL;
static mtx_t virgl_screen_mutex = _MTX_INITIALIZER_NP;
static void
@@ -32,7 +32,7 @@
#include "virgl_resource_cache.h"
struct pipe_fence_handle;
struct util_hash_table;
struct hash_table;
struct virgl_hw_res {
struct pipe_reference reference;
@@ -60,8 +60,8 @@ struct virgl_drm_winsys
struct virgl_resource_cache cache;
mtx_t mutex;
struct util_hash_table *bo_handles;
struct util_hash_table *bo_names;
struct hash_table *bo_handles;
struct hash_table *bo_names;
mtx_t bo_handles_mutex;
bool has_capset_query_fix;
};