Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_hash_table.c
4561 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728#include "util/u_pointer.h"29#include "util/u_hash_table.h"3031#if DETECT_OS_UNIX32#include <sys/stat.h>33#endif343536static uint32_t37pointer_hash(const void *key)38{39return _mesa_hash_pointer(key);40}414243static bool44pointer_equal(const void *a, const void *b)45{46return a == b;47}484950struct hash_table *51util_hash_table_create_ptr_keys(void)52{53return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);54}555657static uint32_t hash_fd(const void *key)58{59#if DETECT_OS_UNIX60int fd = pointer_to_intptr(key);61struct stat stat;6263fstat(fd, &stat);6465return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;66#else67return 0;68#endif69}707172static bool equal_fd(const void *key1, const void *key2)73{74#if DETECT_OS_UNIX75int fd1 = pointer_to_intptr(key1);76int fd2 = pointer_to_intptr(key2);77struct stat stat1, stat2;7879fstat(fd1, &stat1);80fstat(fd2, &stat2);8182return stat1.st_dev == stat2.st_dev &&83stat1.st_ino == stat2.st_ino &&84stat1.st_rdev == stat2.st_rdev;85#else86return 0;87#endif88}899091struct hash_table *92util_hash_table_create_fd_keys(void)93{94return _mesa_hash_table_create(NULL, hash_fd, equal_fd);95}969798void *99util_hash_table_get(struct hash_table *ht,100void *key)101{102struct hash_entry *entry = _mesa_hash_table_search(ht, key);103104return entry ? entry->data : NULL;105}106107108enum pipe_error109util_hash_table_foreach(struct hash_table *ht,110enum pipe_error (*callback)111(void *key, void *value, void *data),112void *data)113{114hash_table_foreach(ht, entry) {115enum pipe_error error = callback((void*)entry->key, entry->data, data);116if (error != PIPE_OK)117return error;118}119return PIPE_OK;120}121122123