1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * General purpose hash table implementation.
32 * Just uses the cso_hash for now, but it might be better switch to a linear
33 * probing hash table implementation at some point -- as it is said they have
34 * better lookup and cache performance and it appears to be possible to write
35 * a lock-free implementation of such hash tables .
37 * @author José Fonseca <jfonseca@vmware.com>
41 #include "pipe/p_compiler.h"
42 #include "util/u_debug.h"
44 #include "cso_cache/cso_hash.h"
46 #include "util/u_memory.h"
47 #include "util/u_hash_table.h"
50 struct util_hash_table
55 unsigned (*hash
)(void *key
);
57 /** Compare two keys */
58 int (*compare
)(void *key1
, void *key2
);
60 /* TODO: key, value destructors? */
64 struct util_hash_table_item
71 static inline struct util_hash_table_item
*
72 util_hash_table_item(struct cso_hash_iter iter
)
74 return (struct util_hash_table_item
*)cso_hash_iter_data(iter
);
78 struct util_hash_table
*
79 util_hash_table_create(unsigned (*hash
)(void *key
),
80 int (*compare
)(void *key1
, void *key2
))
82 struct util_hash_table
*ht
;
84 ht
= MALLOC_STRUCT(util_hash_table
);
88 cso_hash_init(&ht
->cso
);
91 ht
->compare
= compare
;
97 static inline struct cso_hash_iter
98 util_hash_table_find_iter(struct util_hash_table
*ht
,
102 struct cso_hash_iter iter
;
103 struct util_hash_table_item
*item
;
105 iter
= cso_hash_find(&ht
->cso
, key_hash
);
106 while (!cso_hash_iter_is_null(iter
)) {
107 item
= (struct util_hash_table_item
*)cso_hash_iter_data(iter
);
108 if (!ht
->compare(item
->key
, key
))
110 iter
= cso_hash_iter_next(iter
);
117 static inline struct util_hash_table_item
*
118 util_hash_table_find_item(struct util_hash_table
*ht
,
122 struct cso_hash_iter iter
;
123 struct util_hash_table_item
*item
;
125 iter
= cso_hash_find(&ht
->cso
, key_hash
);
126 while (!cso_hash_iter_is_null(iter
)) {
127 item
= (struct util_hash_table_item
*)cso_hash_iter_data(iter
);
128 if (!ht
->compare(item
->key
, key
))
130 iter
= cso_hash_iter_next(iter
);
138 util_hash_table_set(struct util_hash_table
*ht
,
143 struct util_hash_table_item
*item
;
144 struct cso_hash_iter iter
;
148 return PIPE_ERROR_BAD_INPUT
;
150 key_hash
= ht
->hash(key
);
152 item
= util_hash_table_find_item(ht
, key
, key_hash
);
154 /* TODO: key/value destruction? */
159 item
= MALLOC_STRUCT(util_hash_table_item
);
161 return PIPE_ERROR_OUT_OF_MEMORY
;
166 iter
= cso_hash_insert(&ht
->cso
, key_hash
, item
);
167 if(cso_hash_iter_is_null(iter
)) {
169 return PIPE_ERROR_OUT_OF_MEMORY
;
177 util_hash_table_get(struct util_hash_table
*ht
,
181 struct util_hash_table_item
*item
;
187 key_hash
= ht
->hash(key
);
189 item
= util_hash_table_find_item(ht
, key
, key_hash
);
198 util_hash_table_remove(struct util_hash_table
*ht
,
202 struct cso_hash_iter iter
;
203 struct util_hash_table_item
*item
;
209 key_hash
= ht
->hash(key
);
211 iter
= util_hash_table_find_iter(ht
, key
, key_hash
);
212 if(cso_hash_iter_is_null(iter
))
215 item
= util_hash_table_item(iter
);
219 cso_hash_erase(&ht
->cso
, iter
);
224 util_hash_table_clear(struct util_hash_table
*ht
)
226 struct cso_hash_iter iter
;
227 struct util_hash_table_item
*item
;
233 iter
= cso_hash_first_node(&ht
->cso
);
234 while (!cso_hash_iter_is_null(iter
)) {
235 item
= (struct util_hash_table_item
*)cso_hash_take(&ht
->cso
, cso_hash_iter_key(iter
));
237 iter
= cso_hash_first_node(&ht
->cso
);
243 util_hash_table_foreach(struct util_hash_table
*ht
,
244 enum pipe_error (*callback
)
245 (void *key
, void *value
, void *data
),
248 struct cso_hash_iter iter
;
249 struct util_hash_table_item
*item
;
250 enum pipe_error result
;
254 return PIPE_ERROR_BAD_INPUT
;
256 iter
= cso_hash_first_node(&ht
->cso
);
257 while (!cso_hash_iter_is_null(iter
)) {
258 item
= (struct util_hash_table_item
*)cso_hash_iter_data(iter
);
259 result
= callback(item
->key
, item
->value
, data
);
260 if(result
!= PIPE_OK
)
262 iter
= cso_hash_iter_next(iter
);
269 static enum pipe_error
270 util_hash_inc(UNUSED
void *k
, UNUSED
void *v
, void *d
)
278 util_hash_table_count(struct util_hash_table
*ht
)
281 util_hash_table_foreach(ht
, util_hash_inc
, &count
);
287 util_hash_table_destroy(struct util_hash_table
*ht
)
289 struct cso_hash_iter iter
;
290 struct util_hash_table_item
*item
;
296 iter
= cso_hash_first_node(&ht
->cso
);
297 while (!cso_hash_iter_is_null(iter
)) {
298 item
= (struct util_hash_table_item
*)cso_hash_iter_data(iter
);
300 iter
= cso_hash_iter_next(iter
);
303 cso_hash_deinit(&ht
->cso
);