59c8c6cf98dc07c1935e434ac53fa877e9c82bd7
[mesa.git] / src / gallium / auxiliary / util / u_hash_table.c
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 "pipe/p_compiler.h"
30 #include "util/u_debug.h"
31
32 #include "util/u_memory.h"
33 #include "util/u_hash_table.h"
34 #include "util/hash_table.h"
35
36 #if DETECT_OS_UNIX
37 #include <sys/stat.h>
38 #endif
39
40
41 struct hash_table *
42 util_hash_table_create(uint32_t (*hash)(const void *key),
43 bool (*equal)(const void *key1, const void *key2))
44 {
45 return _mesa_hash_table_create(NULL, hash, equal);
46 }
47
48
49 static uint32_t
50 pointer_hash(const void *key)
51 {
52 return _mesa_hash_pointer(key);
53 }
54
55
56 static bool
57 pointer_equal(const void *a, const void *b)
58 {
59 return a == b;
60 }
61
62
63 struct hash_table *
64 util_hash_table_create_ptr_keys(void)
65 {
66 return _mesa_hash_table_create(NULL, pointer_hash, pointer_equal);
67 }
68
69
70 static uint32_t hash_fd(const void *key)
71 {
72 #if DETECT_OS_UNIX
73 int fd = pointer_to_intptr(key);
74 struct stat stat;
75
76 fstat(fd, &stat);
77
78 return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
79 #else
80 return 0;
81 #endif
82 }
83
84
85 static bool equal_fd(const void *key1, const void *key2)
86 {
87 #if DETECT_OS_UNIX
88 int fd1 = pointer_to_intptr(key1);
89 int fd2 = pointer_to_intptr(key2);
90 struct stat stat1, stat2;
91
92 fstat(fd1, &stat1);
93 fstat(fd2, &stat2);
94
95 return stat1.st_dev == stat2.st_dev &&
96 stat1.st_ino == stat2.st_ino &&
97 stat1.st_rdev == stat2.st_rdev;
98 #else
99 return 0;
100 #endif
101 }
102
103
104 struct hash_table *
105 util_hash_table_create_fd_keys(void)
106 {
107 return _mesa_hash_table_create(NULL, hash_fd, equal_fd);
108 }
109
110
111 enum pipe_error
112 util_hash_table_set(struct hash_table *ht,
113 void *key,
114 void *value)
115 {
116 _mesa_hash_table_insert(ht, key, value);
117 return PIPE_OK;
118 }
119
120
121 void *
122 util_hash_table_get(struct hash_table *ht,
123 void *key)
124 {
125 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
126
127 return entry ? entry->data : NULL;
128 }
129
130
131 void
132 util_hash_table_remove(struct hash_table *ht,
133 void *key)
134 {
135 _mesa_hash_table_remove_key(ht, key);
136 }
137
138
139 void
140 util_hash_table_clear(struct hash_table *ht)
141 {
142 _mesa_hash_table_clear(ht, NULL);
143 }
144
145
146 enum pipe_error
147 util_hash_table_foreach(struct hash_table *ht,
148 enum pipe_error (*callback)
149 (void *key, void *value, void *data),
150 void *data)
151 {
152 hash_table_foreach(ht, entry) {
153 enum pipe_error error = callback((void*)entry->key, entry->data, data);
154 if (error != PIPE_OK)
155 return error;
156 }
157 return PIPE_OK;
158 }
159
160
161 size_t
162 util_hash_table_count(struct hash_table *ht)
163 {
164 return _mesa_hash_table_num_entries(ht);
165 }
166
167
168 void
169 util_hash_table_destroy(struct hash_table *ht)
170 {
171 _mesa_hash_table_destroy(ht, NULL);
172 }