i965: Disable hardware blending if advanced blending is in use.
[mesa.git] / src / mesa / program / hash_table.h
1 /*
2 * Copyright © 2008 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file hash_table.h
26 * \brief Implementation of a generic, opaque hash table data type.
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
31 #ifndef HASH_TABLE_H
32 #define HASH_TABLE_H
33
34 #include <string.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <limits.h>
39 #include <assert.h>
40 #include "util/hash_table.h"
41
42 struct string_to_uint_map;
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 typedef unsigned (*hash_func_t)(const void *key);
49 typedef bool (*hash_compare_func_t)(const void *key1, const void *key2);
50
51 /**
52 * Hash table constructor
53 *
54 * Creates a hash table with the specified number of buckets. The supplied
55 * \c hash and \c compare routines are used when adding elements to the table
56 * and when searching for elements in the table.
57 *
58 * \param num_buckets Number of buckets (bins) in the hash table.
59 * \param hash Function used to compute hash value of input keys.
60 * \param compare Function used to compare keys.
61 */
62 static inline struct hash_table *hash_table_ctor(UNUSED unsigned num_buckets,
63 hash_func_t hash, hash_compare_func_t compare)
64 {
65 return _mesa_hash_table_create(NULL, hash, compare);
66 }
67
68 /**
69 * Release all memory associated with a hash table
70 *
71 * \warning
72 * This function does not release memory occupied either by keys or data.
73 */
74 static inline void hash_table_dtor(struct hash_table *ht)
75 {
76 return _mesa_hash_table_destroy(ht, NULL);
77 }
78
79 /**
80 * Flush all entries from a hash table
81 *
82 * \param ht Table to be cleared of its entries.
83 */
84 static inline void hash_table_clear(struct hash_table *ht)
85 {
86 return _mesa_hash_table_clear(ht, NULL);
87 }
88
89 /**
90 * Search a hash table for a specific element
91 *
92 * \param ht Table to be searched
93 * \param key Key of the desired element
94 *
95 * \return
96 * The \c data value supplied to \c hash_table_insert when the element with
97 * the matching key was added. If no matching key exists in the table,
98 * \c NULL is returned.
99 */
100 static inline void *hash_table_find(struct hash_table *ht, const void *key)
101 {
102 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
103 if (!entry)
104 return NULL;
105 return entry->data;
106 }
107
108 /**
109 * Add an element to a hash table
110 *
111 * \warning
112 * The value passed by \c key is kept in the hash table and is used by later
113 * calls to \c hash_table_find.
114 *
115 * \sa hash_table_replace
116 */
117 static inline void hash_table_insert(struct hash_table *ht, void *data,
118 const void *key)
119 {
120 _mesa_hash_table_insert(ht, key, data);
121 }
122
123 /**
124 * Add an element to a hash table with replacement
125 *
126 * \return
127 * 1 if it did replace the value (in which case the old key is kept), 0 if it
128 * did not replace the value (in which case the new key is kept).
129 *
130 * \warning
131 * If \c key is already in the hash table, \c data will \b replace the most
132 * recently inserted \c data (see the warning in \c hash_table_insert) for
133 * that key.
134 *
135 * \sa hash_table_insert
136 */
137 static inline bool hash_table_replace(struct hash_table *ht, void *data,
138 const void *key)
139 {
140 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
141 if (entry) {
142 entry->data = data;
143 return true;
144 } else {
145 _mesa_hash_table_insert(ht, key, data);
146 return false;
147 }
148 }
149
150 /**
151 * Remove a specific element from a hash table.
152 */
153 static inline void hash_table_remove(struct hash_table *ht, const void *key)
154 {
155 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
156
157 if (entry)
158 _mesa_hash_table_remove(ht, entry);
159 }
160
161 /**
162 * Compute hash value of a string
163 *
164 * Computes the hash value of a string using the DJB2 algorithm developed by
165 * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
166 * a time. I was unable to find the original posting in the archives.
167 *
168 * \param key Pointer to a NUL terminated string to be hashed.
169 *
170 * \sa hash_table_string_compare
171 */
172 extern unsigned hash_table_string_hash(const void *key);
173
174
175 /**
176 * Compare two strings used as keys
177 *
178 * This is just a wrapper around \c strcmp.
179 *
180 * \sa hash_table_string_hash
181 */
182 bool hash_table_string_compare(const void *a, const void *b);
183
184 /**
185 * Compute hash value of a pointer
186 *
187 * \param key Pointer to be used as a hash key
188 *
189 * \note
190 * The memory pointed to by \c key is \b never accessed. The value of \c key
191 * itself is used as the hash key
192 *
193 * \sa hash_table_pointer_compare
194 */
195 unsigned
196 hash_table_pointer_hash(const void *key);
197
198
199 /**
200 * Compare two pointers used as keys
201 *
202 * \sa hash_table_pointer_hash
203 */
204 bool
205 hash_table_pointer_compare(const void *key1, const void *key2);
206
207 static inline void
208 hash_table_call_foreach(struct hash_table *ht,
209 void (*callback)(const void *key,
210 void *data,
211 void *closure),
212 void *closure)
213 {
214 struct hash_entry *entry;
215
216 hash_table_foreach(ht, entry)
217 callback(entry->key, entry->data, closure);
218 }
219
220 struct string_to_uint_map *
221 string_to_uint_map_ctor();
222
223 void
224 string_to_uint_map_dtor(struct string_to_uint_map *);
225
226
227 #ifdef __cplusplus
228 }
229
230 struct string_map_iterate_wrapper_closure {
231 void (*callback)(const char *key, unsigned value, void *closure);
232 void *closure;
233 };
234
235 /**
236 * Map from a string (name) to an unsigned integer value
237 *
238 * \note
239 * Because of the way this class interacts with the \c hash_table
240 * implementation, values of \c UINT_MAX cannot be stored in the map.
241 */
242 struct string_to_uint_map {
243 public:
244 string_to_uint_map()
245 {
246 this->ht = hash_table_ctor(0, hash_table_string_hash,
247 hash_table_string_compare);
248 }
249
250 ~string_to_uint_map()
251 {
252 hash_table_call_foreach(this->ht, delete_key, NULL);
253 hash_table_dtor(this->ht);
254 }
255
256 /**
257 * Remove all mappings from this map.
258 */
259 void clear()
260 {
261 hash_table_call_foreach(this->ht, delete_key, NULL);
262 hash_table_clear(this->ht);
263 }
264
265 /**
266 * Runs a passed callback for the hash
267 */
268 void iterate(void (*func)(const char *, unsigned, void *), void *closure)
269 {
270 struct string_map_iterate_wrapper_closure *wrapper;
271
272 wrapper = (struct string_map_iterate_wrapper_closure *)
273 malloc(sizeof(struct string_map_iterate_wrapper_closure));
274 if (wrapper == NULL)
275 return;
276
277 wrapper->callback = func;
278 wrapper->closure = closure;
279
280 hash_table_call_foreach(this->ht, subtract_one_wrapper, wrapper);
281 free(wrapper);
282 }
283
284 /**
285 * Get the value associated with a particular key
286 *
287 * \return
288 * If \c key is found in the map, \c true is returned. Otherwise \c false
289 * is returned.
290 *
291 * \note
292 * If \c key is not found in the table, \c value is not modified.
293 */
294 bool get(unsigned &value, const char *key)
295 {
296 const intptr_t v =
297 (intptr_t) hash_table_find(this->ht, (const void *) key);
298
299 if (v == 0)
300 return false;
301
302 value = (unsigned)(v - 1);
303 return true;
304 }
305
306 void put(unsigned value, const char *key)
307 {
308 /* The low-level hash table structure returns NULL if key is not in the
309 * hash table. However, users of this map might want to store zero as a
310 * valid value in the table. Bias the value by +1 so that a
311 * user-specified zero is stored as 1. This enables ::get to tell the
312 * difference between a user-specified zero (returned as 1 by
313 * hash_table_find) and the key not in the table (returned as 0 by
314 * hash_table_find).
315 *
316 * The net effect is that we can't store UINT_MAX in the table. This is
317 * because UINT_MAX+1 = 0.
318 */
319 assert(value != UINT_MAX);
320 char *dup_key = strdup(key);
321 bool result = hash_table_replace(this->ht,
322 (void *) (intptr_t) (value + 1),
323 dup_key);
324 if (result)
325 free(dup_key);
326 }
327
328 private:
329 static void delete_key(const void *key, void *data, void *closure)
330 {
331 (void) data;
332 (void) closure;
333
334 free((char *)key);
335 }
336
337 static void subtract_one_wrapper(const void *key, void *data, void *closure)
338 {
339 struct string_map_iterate_wrapper_closure *wrapper =
340 (struct string_map_iterate_wrapper_closure *) closure;
341 unsigned value = (intptr_t) data;
342
343 value -= 1;
344
345 wrapper->callback((const char *) key, value, wrapper->closure);
346 }
347
348 struct hash_table *ht;
349 };
350
351 #endif /* __cplusplus */
352 #endif /* HASH_TABLE_H */