• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2009,2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #ifndef _HASH_TABLE_H
29 #define _HASH_TABLE_H
30 
31 #include <stdlib.h>
32 #include <inttypes.h>
33 #include <stdbool.h>
34 #include "macros.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 struct hash_entry {
41    uint32_t hash;
42    const void *key;
43    void *data;
44 };
45 
46 struct hash_table {
47    struct hash_entry *table;
48    uint32_t (*key_hash_function)(const void *key);
49    bool (*key_equals_function)(const void *a, const void *b);
50    const void *deleted_key;
51    uint32_t size;
52    uint32_t rehash;
53    uint64_t size_magic;
54    uint64_t rehash_magic;
55    uint32_t max_entries;
56    uint32_t size_index;
57    uint32_t entries;
58    uint32_t deleted_entries;
59 };
60 
61 struct hash_table *
62 _mesa_hash_table_create(void *mem_ctx,
63                         uint32_t (*key_hash_function)(const void *key),
64                         bool (*key_equals_function)(const void *a,
65                                                     const void *b));
66 
67 bool
68 _mesa_hash_table_init(struct hash_table *ht,
69                       void *mem_ctx,
70                       uint32_t (*key_hash_function)(const void *key),
71                       bool (*key_equals_function)(const void *a,
72                                                   const void *b));
73 
74 struct hash_table *
75 _mesa_hash_table_create_u32_keys(void *mem_ctx);
76 
77 struct hash_table *
78 _mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx);
79 void _mesa_hash_table_destroy(struct hash_table *ht,
80                               void (*delete_function)(struct hash_entry *entry));
81 void _mesa_hash_table_clear(struct hash_table *ht,
82                             void (*delete_function)(struct hash_entry *entry));
83 void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
84                                       const void *deleted_key);
85 
_mesa_hash_table_num_entries(const struct hash_table * ht)86 static inline uint32_t _mesa_hash_table_num_entries(const struct hash_table *ht)
87 {
88    return ht->entries;
89 }
90 
91 struct hash_entry *
92 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data);
93 struct hash_entry *
94 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
95                                    const void *key, void *data);
96 struct hash_entry *
97 _mesa_hash_table_search(const struct hash_table *ht, const void *key);
98 struct hash_entry *
99 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
100                                   const void *key);
101 void _mesa_hash_table_remove(struct hash_table *ht,
102                              struct hash_entry *entry);
103 void _mesa_hash_table_remove_key(struct hash_table *ht,
104                                  const void *key);
105 
106 struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
107                                                struct hash_entry *entry);
108 struct hash_entry *_mesa_hash_table_next_entry_unsafe(const struct hash_table *ht,
109                                                struct hash_entry *entry);
110 struct hash_entry *
111 _mesa_hash_table_random_entry(struct hash_table *ht,
112                               bool (*predicate)(struct hash_entry *entry));
113 
114 uint32_t _mesa_hash_data(const void *data, size_t size);
115 uint32_t _mesa_hash_data_with_seed(const void *data, size_t size, uint32_t seed);
116 
117 uint32_t _mesa_hash_int(const void *key);
118 uint32_t _mesa_hash_uint(const void *key);
119 uint32_t _mesa_hash_u32(const void *key);
120 uint32_t _mesa_hash_string(const void *key);
121 uint32_t _mesa_hash_string_with_length(const void *_key, unsigned length);
122 uint32_t _mesa_hash_pointer(const void *pointer);
123 
124 bool _mesa_key_int_equal(const void *a, const void *b);
125 bool _mesa_key_uint_equal(const void *a, const void *b);
126 bool _mesa_key_u32_equal(const void *a, const void *b);
127 bool _mesa_key_string_equal(const void *a, const void *b);
128 bool _mesa_key_pointer_equal(const void *a, const void *b);
129 
130 struct hash_table *
131 _mesa_pointer_hash_table_create(void *mem_ctx);
132 
133 static inline struct hash_table *
_mesa_string_hash_table_create(void * mem_ctx)134 _mesa_string_hash_table_create(void *mem_ctx)
135 {
136    return _mesa_hash_table_create(mem_ctx, _mesa_hash_string,
137                                   _mesa_key_string_equal);
138 }
139 
140 bool
141 _mesa_hash_table_reserve(struct hash_table *ht, unsigned size);
142 /**
143  * This foreach function is safe against deletion (which just replaces
144  * an entry's data with the deleted marker), but not against insertion
145  * (which may rehash the table, making entry a dangling pointer).
146  */
147 #define hash_table_foreach(ht, entry)                                      \
148    for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL);  \
149         entry != NULL;                                                     \
150         entry = _mesa_hash_table_next_entry(ht, entry))
151 /**
152  * This foreach function destroys the table as it iterates.
153  * It is not safe to use when inserting or removing entries.
154  */
155 #define hash_table_foreach_remove(ht, entry)                                      \
156    for (struct hash_entry *entry = _mesa_hash_table_next_entry_unsafe(ht, NULL);  \
157         (ht)->entries;                                                     \
158         entry->hash = 0, entry->key = (void*)NULL, entry->data = NULL,      \
159         (ht)->entries--, entry = _mesa_hash_table_next_entry_unsafe(ht, entry))
160 
161 static inline void
hash_table_call_foreach(struct hash_table * ht,void (* callback)(const void * key,void * data,void * closure),void * closure)162 hash_table_call_foreach(struct hash_table *ht,
163                         void (*callback)(const void *key,
164                                          void *data,
165                                          void *closure),
166                         void *closure)
167 {
168    hash_table_foreach(ht, entry)
169       callback(entry->key, entry->data, closure);
170 }
171 
172 /**
173  * This helper macro generates the boilerplate required to use a hash table with
174  * a fixed-size struct as the key.
175  */
176 #define DERIVE_HASH_TABLE(T)                                                   \
177    static uint32_t T##_hash(const void *key)                                   \
178    {                                                                           \
179       return _mesa_hash_data(key, sizeof(struct T));                           \
180    }                                                                           \
181                                                                                \
182    static bool T##_equal(const void *a, const void *b)                         \
183    {                                                                           \
184       return memcmp(a, b, sizeof(struct T)) == 0;                              \
185    }                                                                           \
186                                                                                \
187    static struct hash_table *T##_table_create(void *memctx)                    \
188    {                                                                           \
189       return _mesa_hash_table_create(memctx, T##_hash, T##_equal);             \
190    }
191 
192 /**
193  * Hash table wrapper which supports 64-bit keys.
194  */
195 struct hash_table_u64 {
196    struct hash_table *table;
197    void *freed_key_data;
198    void *deleted_key_data;
199 };
200 
201 struct hash_entry_u64 {
202    uint64_t key;
203    void *data;
204    struct hash_entry *_entry;
205 };
206 
207 struct hash_table_u64 *
208 _mesa_hash_table_u64_create(void *mem_ctx);
209 
210 void
211 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht);
212 
213 void
214 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
215                             void *data);
216 
217 void *
218 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key);
219 
220 void
221 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key);
222 
223 void
224 _mesa_hash_table_u64_clear(struct hash_table_u64 *ht);
225 
226 struct hash_entry_u64
227 _mesa_hash_table_u64_next_entry(struct hash_table_u64 *ht,
228                                 struct hash_entry_u64 *ent);
229 
230 static inline uint32_t
_mesa_hash_table_u64_num_entries(struct hash_table_u64 * ht)231 _mesa_hash_table_u64_num_entries(struct hash_table_u64 *ht)
232 {
233    return (!!ht->freed_key_data) + (!!ht->deleted_key_data) +
234           _mesa_hash_table_num_entries(ht->table);
235 }
236 
237 /**
238  * This foreach function is safe against deletion (which just replaces
239  * an entry's data with the deleted marker), but not against insertion
240  * (which may rehash the table, making entry a dangling pointer).
241  */
242 #define hash_table_u64_foreach(ht, entry)                                      \
243    for (struct hash_entry_u64 entry =                                          \
244          _mesa_hash_table_u64_next_entry(ht, NULL);                            \
245         entry.data != NULL;                                                    \
246         entry = _mesa_hash_table_u64_next_entry(ht, &entry))
247 
248 
249 #ifdef __cplusplus
250 } /* extern C */
251 #endif
252 
253 #endif /* _HASH_TABLE_H */
254