From: Marek Olšák Date: Wed, 5 Feb 2020 19:27:21 +0000 (-0500) Subject: gallium/hash_table: consolidate hash tables with FD keys X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=76dff2fabe065b71f0d336cb43853335dd3eb82a;p=mesa.git gallium/hash_table: consolidate hash tables with FD keys Reviewed-by: Kristian H. Kristensen Part-of: --- diff --git a/src/gallium/auxiliary/util/u_hash_table.c b/src/gallium/auxiliary/util/u_hash_table.c index 0a2863dcc0c..e58c7823be2 100644 --- a/src/gallium/auxiliary/util/u_hash_table.c +++ b/src/gallium/auxiliary/util/u_hash_table.c @@ -47,6 +47,10 @@ #include "util/u_hash_table.h" #include "util/hash_table.h" +#if DETECT_OS_UNIX +#include +#endif + struct util_hash_table { @@ -116,6 +120,47 @@ util_hash_table_create_ptr_keys(void) } +static unsigned hash_fd(void *key) +{ +#if DETECT_OS_UNIX + int fd = pointer_to_intptr(key); + struct stat stat; + + fstat(fd, &stat); + + return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; +#else + return 0; +#endif +} + + +static int compare_fd(void *key1, void *key2) +{ +#if DETECT_OS_UNIX + int fd1 = pointer_to_intptr(key1); + int fd2 = pointer_to_intptr(key2); + struct stat stat1, stat2; + + fstat(fd1, &stat1); + fstat(fd2, &stat2); + + return stat1.st_dev != stat2.st_dev || + stat1.st_ino != stat2.st_ino || + stat1.st_rdev != stat2.st_rdev; +#else + return 0; +#endif +} + + +struct util_hash_table * +util_hash_table_create_fd_keys(void) +{ + return util_hash_table_create(hash_fd, compare_fd); +} + + static inline struct cso_hash_iter util_hash_table_find_iter(struct util_hash_table *ht, void *key, diff --git a/src/gallium/auxiliary/util/u_hash_table.h b/src/gallium/auxiliary/util/u_hash_table.h index cf0c58cd287..7db87c4367b 100644 --- a/src/gallium/auxiliary/util/u_hash_table.h +++ b/src/gallium/auxiliary/util/u_hash_table.h @@ -66,6 +66,13 @@ struct util_hash_table * util_hash_table_create_ptr_keys(void); +/** + * Create a hash table where the keys are device FDs. + */ +struct util_hash_table * +util_hash_table_create_fd_keys(void); + + enum pipe_error util_hash_table_set(struct util_hash_table *ht, void *key, diff --git a/src/gallium/winsys/etnaviv/drm/etnaviv_drm_winsys.c b/src/gallium/winsys/etnaviv/drm/etnaviv_drm_winsys.c index 8e3f7a06a9a..bfeeb429be5 100644 --- a/src/gallium/winsys/etnaviv/drm/etnaviv_drm_winsys.c +++ b/src/gallium/winsys/etnaviv/drm/etnaviv_drm_winsys.c @@ -91,30 +91,6 @@ etna_drm_screen_destroy(struct pipe_screen *pscreen) } } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - struct pipe_screen * etna_drm_screen_create_renderonly(struct renderonly *ro) { @@ -122,7 +98,7 @@ etna_drm_screen_create_renderonly(struct renderonly *ro) mtx_lock(&etna_screen_mutex); if (!etna_tab) { - etna_tab = util_hash_table_create(hash_fd, compare_fd); + etna_tab = util_hash_table_create_fd_keys(); if (!etna_tab) goto unlock; } diff --git a/src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c b/src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c index 719c52a80c7..d09208bcb26 100644 --- a/src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c +++ b/src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c @@ -62,28 +62,6 @@ fd_drm_screen_destroy(struct pipe_screen *pscreen) } } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - struct pipe_screen * fd_drm_screen_create(int fd, struct renderonly *ro) { @@ -91,7 +69,7 @@ fd_drm_screen_create(int fd, struct renderonly *ro) mtx_lock(&fd_screen_mutex); if (!fd_tab) { - fd_tab = util_hash_table_create(hash_fd, compare_fd); + fd_tab = util_hash_table_create_fd_keys(); if (!fd_tab) goto unlock; } diff --git a/src/gallium/winsys/lima/drm/lima_drm_winsys.c b/src/gallium/winsys/lima/drm/lima_drm_winsys.c index 07690307c64..c9ef9406598 100644 --- a/src/gallium/winsys/lima/drm/lima_drm_winsys.c +++ b/src/gallium/winsys/lima/drm/lima_drm_winsys.c @@ -57,30 +57,6 @@ lima_drm_screen_destroy(struct pipe_screen *pscreen) } } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - struct pipe_screen * lima_drm_screen_create(int fd) { @@ -88,7 +64,7 @@ lima_drm_screen_create(int fd) mtx_lock(&lima_screen_mutex); if (!fd_tab) { - fd_tab = util_hash_table_create(hash_fd, compare_fd); + fd_tab = util_hash_table_create_fd_keys(); if (!fd_tab) goto unlock; } diff --git a/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c b/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c index 3e790aa65d5..57f3702a3e0 100644 --- a/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c +++ b/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c @@ -36,28 +36,6 @@ bool nouveau_drm_screen_unref(struct nouveau_screen *screen) return ret == 0; } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - PUBLIC struct pipe_screen * nouveau_drm_screen_create(int fd) { @@ -69,7 +47,7 @@ nouveau_drm_screen_create(int fd) mtx_lock(&nouveau_screen_mutex); if (!fd_tab) { - fd_tab = util_hash_table_create(hash_fd, compare_fd); + fd_tab = util_hash_table_create_fd_keys(); if (!fd_tab) { mtx_unlock(&nouveau_screen_mutex); return NULL; diff --git a/src/gallium/winsys/radeon/drm/radeon_drm_winsys.c b/src/gallium/winsys/radeon/drm/radeon_drm_winsys.c index 0ea43c1959b..c38d238bfcd 100644 --- a/src/gallium/winsys/radeon/drm/radeon_drm_winsys.c +++ b/src/gallium/winsys/radeon/drm/radeon_drm_winsys.c @@ -761,28 +761,6 @@ static bool radeon_read_registers(struct radeon_winsys *rws, return true; } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - DEBUG_GET_ONCE_BOOL_OPTION(thread, "RADEON_THREAD", true) static bool radeon_winsys_unref(struct radeon_winsys *ws) @@ -828,7 +806,7 @@ radeon_drm_winsys_create(int fd, const struct pipe_screen_config *config, mtx_lock(&fd_tab_mutex); if (!fd_tab) { - fd_tab = util_hash_table_create(hash_fd, compare_fd); + fd_tab = util_hash_table_create_fd_keys(); } ws = util_hash_table_get(fd_tab, intptr_to_pointer(fd)); diff --git a/src/gallium/winsys/virgl/drm/virgl_drm_winsys.c b/src/gallium/winsys/virgl/drm/virgl_drm_winsys.c index 53d8fdf3eda..959af78493b 100644 --- a/src/gallium/winsys/virgl/drm/virgl_drm_winsys.c +++ b/src/gallium/winsys/virgl/drm/virgl_drm_winsys.c @@ -1028,28 +1028,6 @@ virgl_drm_screen_destroy(struct pipe_screen *pscreen) } } -static unsigned hash_fd(void *key) -{ - int fd = pointer_to_intptr(key); - struct stat stat; - fstat(fd, &stat); - - return stat.st_dev ^ stat.st_ino ^ stat.st_rdev; -} - -static int compare_fd(void *key1, void *key2) -{ - int fd1 = pointer_to_intptr(key1); - int fd2 = pointer_to_intptr(key2); - struct stat stat1, stat2; - fstat(fd1, &stat1); - fstat(fd2, &stat2); - - return stat1.st_dev != stat2.st_dev || - stat1.st_ino != stat2.st_ino || - stat1.st_rdev != stat2.st_rdev; -} - struct pipe_screen * virgl_drm_screen_create(int fd, const struct pipe_screen_config *config) { @@ -1057,7 +1035,7 @@ virgl_drm_screen_create(int fd, const struct pipe_screen_config *config) mtx_lock(&virgl_screen_mutex); if (!fd_tab) { - fd_tab = util_hash_table_create(hash_fd, compare_fd); + fd_tab = util_hash_table_create_fd_keys(); if (!fd_tab) goto unlock; }