• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #include "util/u_pointer.h"
30 #include "util/u_hash_table.h"
31 
32 #if DETECT_OS_UNIX
33 #include <sys/stat.h>
34 #endif
35 
36 
37 static uint32_t
pointer_hash(const void * key)38 pointer_hash(const void *key)
39 {
40    return _mesa_hash_pointer(key);
41 }
42 
43 
44 static bool
pointer_equal(const void * a,const void * b)45 pointer_equal(const void *a, const void *b)
46 {
47    return a == b;
48 }
49 
50 
51 struct hash_table *
util_hash_table_create_ptr_keys(void)52 util_hash_table_create_ptr_keys(void)
53 {
54    return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
55 }
56 
57 
hash_fd(const void * key)58 static uint32_t hash_fd(const void *key)
59 {
60 #if DETECT_OS_UNIX
61    int fd = pointer_to_intptr(key);
62    struct stat stat;
63 
64    fstat(fd, &stat);
65 
66    return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
67 #else
68    return 0;
69 #endif
70 }
71 
72 
equal_fd(const void * key1,const void * key2)73 static bool equal_fd(const void *key1, const void *key2)
74 {
75 #if DETECT_OS_UNIX
76    int fd1 = pointer_to_intptr(key1);
77    int fd2 = pointer_to_intptr(key2);
78    struct stat stat1, stat2;
79 
80    fstat(fd1, &stat1);
81    fstat(fd2, &stat2);
82 
83    return stat1.st_dev == stat2.st_dev &&
84           stat1.st_ino == stat2.st_ino &&
85           stat1.st_rdev == stat2.st_rdev;
86 #else
87    return 0;
88 #endif
89 }
90 
91 
92 struct hash_table *
util_hash_table_create_fd_keys(void)93 util_hash_table_create_fd_keys(void)
94 {
95    return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
96 }
97 
98 
99 void *
util_hash_table_get(struct hash_table * ht,void * key)100 util_hash_table_get(struct hash_table *ht,
101                     void *key)
102 {
103    struct hash_entry *entry = _mesa_hash_table_search(ht, key);
104 
105    return entry ? entry->data : NULL;
106 }
107 
108 
109 enum pipe_error
util_hash_table_foreach(struct hash_table * ht,enum pipe_error (* callback)(void * key,void * value,void * data),void * data)110 util_hash_table_foreach(struct hash_table *ht,
111                         enum pipe_error (*callback)
112                         (void *key, void *value, void *data),
113                         void *data)
114 {
115    hash_table_foreach(ht, entry) {
116       enum pipe_error error = callback((void*)entry->key, entry->data, data);
117       if (error != PIPE_OK)
118          return error;
119    }
120    return PIPE_OK;
121 }
122