Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5564 | serge | 1 | /** |
2 | * \file hash.c |
||
3 | * Generic hash table. |
||
4 | * |
||
5 | * Used for display lists, texture objects, vertex/fragment programs, |
||
6 | * buffer objects, etc. The hash functions are thread-safe. |
||
7 | * |
||
8 | * \note key=0 is illegal. |
||
9 | * |
||
10 | * \author Brian Paul |
||
11 | */ |
||
12 | |||
13 | /* |
||
14 | * Mesa 3-D graphics library |
||
15 | * |
||
16 | * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. |
||
17 | * |
||
18 | * Permission is hereby granted, free of charge, to any person obtaining a |
||
19 | * copy of this software and associated documentation files (the "Software"), |
||
20 | * to deal in the Software without restriction, including without limitation |
||
21 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||
22 | * and/or sell copies of the Software, and to permit persons to whom the |
||
23 | * Software is furnished to do so, subject to the following conditions: |
||
24 | * |
||
25 | * The above copyright notice and this permission notice shall be included |
||
26 | * in all copies or substantial portions of the Software. |
||
27 | * |
||
28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
||
29 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
30 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||
31 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
||
32 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
||
33 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
||
34 | * OTHER DEALINGS IN THE SOFTWARE. |
||
35 | */ |
||
36 | |||
37 | #include "glheader.h" |
||
38 | #include "imports.h" |
||
39 | #include "hash.h" |
||
40 | #include "util/hash_table.h" |
||
41 | |||
42 | /** |
||
43 | * Magic GLuint object name that gets stored outside of the struct hash_table. |
||
44 | * |
||
45 | * The hash table needs a particular pointer to be the marker for a key that |
||
46 | * was deleted from the table, along with NULL for the "never allocated in the |
||
47 | * table" marker. Legacy GL allows any GLuint to be used as a GL object name, |
||
48 | * and we use a 1:1 mapping from GLuints to key pointers, so we need to be |
||
49 | * able to track a GLuint that happens to match the deleted key outside of |
||
50 | * struct hash_table. We tell the hash table to use "1" as the deleted key |
||
51 | * value, so that we test the deleted-key-in-the-table path as best we can. |
||
52 | */ |
||
53 | #define DELETED_KEY_VALUE 1 |
||
54 | |||
55 | /** |
||
56 | * The hash table data structure. |
||
57 | */ |
||
58 | struct _mesa_HashTable { |
||
59 | struct hash_table *ht; |
||
60 | GLuint MaxKey; /**< highest key inserted so far */ |
||
61 | mtx_t Mutex; /**< mutual exclusion lock */ |
||
62 | mtx_t WalkMutex; /**< for _mesa_HashWalk() */ |
||
63 | GLboolean InDeleteAll; /**< Debug check */ |
||
64 | /** Value that would be in the table for DELETED_KEY_VALUE. */ |
||
65 | void *deleted_key_data; |
||
66 | }; |
||
67 | |||
68 | /** @{ |
||
69 | * Mapping from our use of GLuint as both the key and the hash value to the |
||
70 | * hash_table.h API |
||
71 | * |
||
72 | * There exist many integer hash functions, designed to avoid collisions when |
||
73 | * the integers are spread across key space with some patterns. In GL, the |
||
74 | * pattern (in the case of glGen*()ed object IDs) is that the keys are unique |
||
75 | * contiguous integers starting from 1. Because of that, we just use the key |
||
76 | * as the hash value, to minimize the cost of the hash function. If objects |
||
77 | * are never deleted, we will never see a collision in the table, because the |
||
78 | * table resizes itself when it approaches full, and thus key % table_size == |
||
79 | * key. |
||
80 | * |
||
81 | * The case where we could have collisions for genned objects would be |
||
82 | * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50); |
||
83 | * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at |
||
84 | * the end of that sequence, instead of 1-150. So far it doesn't appear to be |
||
85 | * a problem. |
||
86 | */ |
||
87 | static bool |
||
88 | uint_key_compare(const void *a, const void *b) |
||
89 | { |
||
90 | return a == b; |
||
91 | } |
||
92 | |||
93 | static uint32_t |
||
94 | uint_hash(GLuint id) |
||
95 | { |
||
96 | return id; |
||
97 | } |
||
98 | |||
99 | static uint32_t |
||
100 | uint_key_hash(const void *key) |
||
101 | { |
||
102 | return uint_hash((uintptr_t)key); |
||
103 | } |
||
104 | |||
105 | static void * |
||
106 | uint_key(GLuint id) |
||
107 | { |
||
108 | return (void *)(uintptr_t) id; |
||
109 | } |
||
110 | /** @} */ |
||
111 | |||
112 | /** |
||
113 | * Create a new hash table. |
||
114 | * |
||
115 | * \return pointer to a new, empty hash table. |
||
116 | */ |
||
117 | struct _mesa_HashTable * |
||
118 | _mesa_NewHashTable(void) |
||
119 | { |
||
120 | struct _mesa_HashTable *table = CALLOC_STRUCT(_mesa_HashTable); |
||
121 | |||
122 | if (table) { |
||
123 | table->ht = _mesa_hash_table_create(NULL, uint_key_hash, |
||
124 | uint_key_compare); |
||
125 | if (table->ht == NULL) { |
||
126 | free(table); |
||
127 | _mesa_error_no_memory(__func__); |
||
128 | return NULL; |
||
129 | } |
||
130 | |||
131 | _mesa_hash_table_set_deleted_key(table->ht, uint_key(DELETED_KEY_VALUE)); |
||
132 | mtx_init(&table->Mutex, mtx_plain); |
||
133 | mtx_init(&table->WalkMutex, mtx_plain); |
||
134 | } |
||
135 | else { |
||
136 | _mesa_error_no_memory(__func__); |
||
137 | } |
||
138 | |||
139 | return table; |
||
140 | } |
||
141 | |||
142 | |||
143 | |||
144 | /** |
||
145 | * Delete a hash table. |
||
146 | * Frees each entry on the hash table and then the hash table structure itself. |
||
147 | * Note that the caller should have already traversed the table and deleted |
||
148 | * the objects in the table (i.e. We don't free the entries' data pointer). |
||
149 | * |
||
150 | * \param table the hash table to delete. |
||
151 | */ |
||
152 | void |
||
153 | _mesa_DeleteHashTable(struct _mesa_HashTable *table) |
||
154 | { |
||
155 | assert(table); |
||
156 | |||
157 | if (_mesa_hash_table_next_entry(table->ht, NULL) != NULL) { |
||
158 | _mesa_problem(NULL, "In _mesa_DeleteHashTable, found non-freed data"); |
||
159 | } |
||
160 | |||
161 | _mesa_hash_table_destroy(table->ht, NULL); |
||
162 | |||
163 | mtx_destroy(&table->Mutex); |
||
164 | mtx_destroy(&table->WalkMutex); |
||
165 | free(table); |
||
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * Lookup an entry in the hash table, without locking. |
||
172 | * \sa _mesa_HashLookup |
||
173 | */ |
||
174 | static inline void * |
||
175 | _mesa_HashLookup_unlocked(struct _mesa_HashTable *table, GLuint key) |
||
176 | { |
||
177 | const struct hash_entry *entry; |
||
178 | |||
179 | assert(table); |
||
180 | assert(key); |
||
181 | |||
182 | if (key == DELETED_KEY_VALUE) |
||
183 | return table->deleted_key_data; |
||
184 | |||
185 | entry = _mesa_hash_table_search(table->ht, uint_key(key)); |
||
186 | if (!entry) |
||
187 | return NULL; |
||
188 | |||
189 | return entry->data; |
||
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Lookup an entry in the hash table. |
||
195 | * |
||
196 | * \param table the hash table. |
||
197 | * \param key the key. |
||
198 | * |
||
199 | * \return pointer to user's data or NULL if key not in table |
||
200 | */ |
||
201 | void * |
||
202 | _mesa_HashLookup(struct _mesa_HashTable *table, GLuint key) |
||
203 | { |
||
204 | void *res; |
||
205 | assert(table); |
||
206 | mtx_lock(&table->Mutex); |
||
207 | res = _mesa_HashLookup_unlocked(table, key); |
||
208 | mtx_unlock(&table->Mutex); |
||
209 | return res; |
||
210 | } |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Lookup an entry in the hash table without locking the mutex. |
||
215 | * |
||
216 | * The hash table mutex must be locked manually by calling |
||
217 | * _mesa_HashLockMutex() before calling this function. |
||
218 | * |
||
219 | * \param table the hash table. |
||
220 | * \param key the key. |
||
221 | * |
||
222 | * \return pointer to user's data or NULL if key not in table |
||
223 | */ |
||
224 | void * |
||
225 | _mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key) |
||
226 | { |
||
227 | return _mesa_HashLookup_unlocked(table, key); |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Lock the hash table mutex. |
||
233 | * |
||
234 | * This function should be used when multiple objects need |
||
235 | * to be looked up in the hash table, to avoid having to lock |
||
236 | * and unlock the mutex each time. |
||
237 | * |
||
238 | * \param table the hash table. |
||
239 | */ |
||
240 | void |
||
241 | _mesa_HashLockMutex(struct _mesa_HashTable *table) |
||
242 | { |
||
243 | assert(table); |
||
244 | mtx_lock(&table->Mutex); |
||
245 | } |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Unlock the hash table mutex. |
||
250 | * |
||
251 | * \param table the hash table. |
||
252 | */ |
||
253 | void |
||
254 | _mesa_HashUnlockMutex(struct _mesa_HashTable *table) |
||
255 | { |
||
256 | assert(table); |
||
257 | mtx_unlock(&table->Mutex); |
||
258 | } |
||
259 | |||
260 | |||
261 | static inline void |
||
262 | _mesa_HashInsert_unlocked(struct _mesa_HashTable *table, GLuint key, void *data) |
||
263 | { |
||
264 | uint32_t hash = uint_hash(key); |
||
265 | struct hash_entry *entry; |
||
266 | |||
267 | assert(table); |
||
268 | assert(key); |
||
269 | |||
270 | if (key > table->MaxKey) |
||
271 | table->MaxKey = key; |
||
272 | |||
273 | if (key == DELETED_KEY_VALUE) { |
||
274 | table->deleted_key_data = data; |
||
275 | } else { |
||
276 | entry = _mesa_hash_table_search_pre_hashed(table->ht, hash, uint_key(key)); |
||
277 | if (entry) { |
||
278 | entry->data = data; |
||
279 | } else { |
||
280 | _mesa_hash_table_insert_pre_hashed(table->ht, hash, uint_key(key), data); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | |||
286 | /** |
||
287 | * Insert a key/pointer pair into the hash table without locking the mutex. |
||
288 | * If an entry with this key already exists we'll replace the existing entry. |
||
289 | * |
||
290 | * The hash table mutex must be locked manually by calling |
||
291 | * _mesa_HashLockMutex() before calling this function. |
||
292 | * |
||
293 | * \param table the hash table. |
||
294 | * \param key the key (not zero). |
||
295 | * \param data pointer to user data. |
||
296 | */ |
||
297 | void |
||
298 | _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data) |
||
299 | { |
||
300 | _mesa_HashInsert_unlocked(table, key, data); |
||
301 | } |
||
302 | |||
303 | |||
304 | /** |
||
305 | * Insert a key/pointer pair into the hash table. |
||
306 | * If an entry with this key already exists we'll replace the existing entry. |
||
307 | * |
||
308 | * \param table the hash table. |
||
309 | * \param key the key (not zero). |
||
310 | * \param data pointer to user data. |
||
311 | */ |
||
312 | void |
||
313 | _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data) |
||
314 | { |
||
315 | assert(table); |
||
316 | mtx_lock(&table->Mutex); |
||
317 | _mesa_HashInsert_unlocked(table, key, data); |
||
318 | mtx_unlock(&table->Mutex); |
||
319 | } |
||
320 | |||
321 | |||
322 | /** |
||
323 | * Remove an entry from the hash table. |
||
324 | * |
||
325 | * \param table the hash table. |
||
326 | * \param key key of entry to remove. |
||
327 | * |
||
328 | * While holding the hash table's lock, searches the entry with the matching |
||
329 | * key and unlinks it. |
||
330 | */ |
||
331 | void |
||
332 | _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key) |
||
333 | { |
||
334 | struct hash_entry *entry; |
||
335 | |||
336 | assert(table); |
||
337 | assert(key); |
||
338 | |||
339 | /* have to check this outside of mutex lock */ |
||
340 | if (table->InDeleteAll) { |
||
341 | _mesa_problem(NULL, "_mesa_HashRemove illegally called from " |
||
342 | "_mesa_HashDeleteAll callback function"); |
||
343 | return; |
||
344 | } |
||
345 | |||
346 | mtx_lock(&table->Mutex); |
||
347 | if (key == DELETED_KEY_VALUE) { |
||
348 | table->deleted_key_data = NULL; |
||
349 | } else { |
||
350 | entry = _mesa_hash_table_search(table->ht, uint_key(key)); |
||
351 | _mesa_hash_table_remove(table->ht, entry); |
||
352 | } |
||
353 | mtx_unlock(&table->Mutex); |
||
354 | } |
||
355 | |||
356 | |||
357 | |||
358 | /** |
||
359 | * Delete all entries in a hash table, but don't delete the table itself. |
||
360 | * Invoke the given callback function for each table entry. |
||
361 | * |
||
362 | * \param table the hash table to delete |
||
363 | * \param callback the callback function |
||
364 | * \param userData arbitrary pointer to pass along to the callback |
||
365 | * (this is typically a struct gl_context pointer) |
||
366 | */ |
||
367 | void |
||
368 | _mesa_HashDeleteAll(struct _mesa_HashTable *table, |
||
369 | void (*callback)(GLuint key, void *data, void *userData), |
||
370 | void *userData) |
||
371 | { |
||
372 | struct hash_entry *entry; |
||
373 | |||
374 | assert(table); |
||
375 | assert(callback); |
||
376 | mtx_lock(&table->Mutex); |
||
377 | table->InDeleteAll = GL_TRUE; |
||
378 | hash_table_foreach(table->ht, entry) { |
||
379 | callback((uintptr_t)entry->key, entry->data, userData); |
||
380 | _mesa_hash_table_remove(table->ht, entry); |
||
381 | } |
||
382 | if (table->deleted_key_data) { |
||
383 | callback(DELETED_KEY_VALUE, table->deleted_key_data, userData); |
||
384 | table->deleted_key_data = NULL; |
||
385 | } |
||
386 | table->InDeleteAll = GL_FALSE; |
||
387 | mtx_unlock(&table->Mutex); |
||
388 | } |
||
389 | |||
390 | |||
391 | /** |
||
392 | * Clone all entries in a hash table, into a new table. |
||
393 | * |
||
394 | * \param table the hash table to clone |
||
395 | */ |
||
396 | struct _mesa_HashTable * |
||
397 | _mesa_HashClone(const struct _mesa_HashTable *table) |
||
398 | { |
||
399 | /* cast-away const */ |
||
400 | struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table; |
||
401 | struct hash_entry *entry; |
||
402 | struct _mesa_HashTable *clonetable; |
||
403 | |||
404 | assert(table); |
||
405 | mtx_lock(&table2->Mutex); |
||
406 | |||
407 | clonetable = _mesa_NewHashTable(); |
||
408 | assert(clonetable); |
||
409 | hash_table_foreach(table->ht, entry) { |
||
410 | _mesa_HashInsert(clonetable, (GLint)(uintptr_t)entry->key, entry->data); |
||
411 | } |
||
412 | |||
413 | mtx_unlock(&table2->Mutex); |
||
414 | |||
415 | return clonetable; |
||
416 | } |
||
417 | |||
418 | |||
419 | /** |
||
420 | * Walk over all entries in a hash table, calling callback function for each. |
||
421 | * Note: we use a separate mutex in this function to avoid a recursive |
||
422 | * locking deadlock (in case the callback calls _mesa_HashRemove()) and to |
||
423 | * prevent multiple threads/contexts from getting tangled up. |
||
424 | * A lock-less version of this function could be used when the table will |
||
425 | * not be modified. |
||
426 | * \param table the hash table to walk |
||
427 | * \param callback the callback function |
||
428 | * \param userData arbitrary pointer to pass along to the callback |
||
429 | * (this is typically a struct gl_context pointer) |
||
430 | */ |
||
431 | void |
||
432 | _mesa_HashWalk(const struct _mesa_HashTable *table, |
||
433 | void (*callback)(GLuint key, void *data, void *userData), |
||
434 | void *userData) |
||
435 | { |
||
436 | /* cast-away const */ |
||
437 | struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table; |
||
438 | struct hash_entry *entry; |
||
439 | |||
440 | assert(table); |
||
441 | assert(callback); |
||
442 | mtx_lock(&table2->WalkMutex); |
||
443 | hash_table_foreach(table->ht, entry) { |
||
444 | callback((uintptr_t)entry->key, entry->data, userData); |
||
445 | } |
||
446 | if (table->deleted_key_data) |
||
447 | callback(DELETED_KEY_VALUE, table->deleted_key_data, userData); |
||
448 | mtx_unlock(&table2->WalkMutex); |
||
449 | } |
||
450 | |||
451 | static void |
||
452 | debug_print_entry(GLuint key, void *data, void *userData) |
||
453 | { |
||
454 | _mesa_debug(NULL, "%u %p\n", key, data); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Dump contents of hash table for debugging. |
||
459 | * |
||
460 | * \param table the hash table. |
||
461 | */ |
||
462 | void |
||
463 | _mesa_HashPrint(const struct _mesa_HashTable *table) |
||
464 | { |
||
465 | if (table->deleted_key_data) |
||
466 | debug_print_entry(DELETED_KEY_VALUE, table->deleted_key_data, NULL); |
||
467 | _mesa_HashWalk(table, debug_print_entry, NULL); |
||
468 | } |
||
469 | |||
470 | |||
471 | /** |
||
472 | * Find a block of adjacent unused hash keys. |
||
473 | * |
||
474 | * \param table the hash table. |
||
475 | * \param numKeys number of keys needed. |
||
476 | * |
||
477 | * \return Starting key of free block or 0 if failure. |
||
478 | * |
||
479 | * If there are enough free keys between the maximum key existing in the table |
||
480 | * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return |
||
481 | * the adjacent key. Otherwise do a full search for a free key block in the |
||
482 | * allowable key range. |
||
483 | */ |
||
484 | GLuint |
||
485 | _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys) |
||
486 | { |
||
487 | const GLuint maxKey = ~((GLuint) 0) - 1; |
||
488 | mtx_lock(&table->Mutex); |
||
489 | if (maxKey - numKeys > table->MaxKey) { |
||
490 | /* the quick solution */ |
||
491 | mtx_unlock(&table->Mutex); |
||
492 | return table->MaxKey + 1; |
||
493 | } |
||
494 | else { |
||
495 | /* the slow solution */ |
||
496 | GLuint freeCount = 0; |
||
497 | GLuint freeStart = 1; |
||
498 | GLuint key; |
||
499 | for (key = 1; key != maxKey; key++) { |
||
500 | if (_mesa_HashLookup_unlocked(table, key)) { |
||
501 | /* darn, this key is already in use */ |
||
502 | freeCount = 0; |
||
503 | freeStart = key+1; |
||
504 | } |
||
505 | else { |
||
506 | /* this key not in use, check if we've found enough */ |
||
507 | freeCount++; |
||
508 | if (freeCount == numKeys) { |
||
509 | mtx_unlock(&table->Mutex); |
||
510 | return freeStart; |
||
511 | } |
||
512 | } |
||
513 | } |
||
514 | /* cannot allocate a block of numKeys consecutive keys */ |
||
515 | mtx_unlock(&table->Mutex); |
||
516 | return 0; |
||
517 | } |
||
518 | } |
||
519 | |||
520 | |||
521 | /** |
||
522 | * Return the number of entries in the hash table. |
||
523 | */ |
||
524 | GLuint |
||
525 | _mesa_HashNumEntries(const struct _mesa_HashTable *table) |
||
526 | { |
||
527 | struct hash_entry *entry; |
||
528 | GLuint count = 0; |
||
529 | |||
530 | if (table->deleted_key_data) |
||
531 | count++; |
||
532 | |||
533 | hash_table_foreach(table->ht, entry) |
||
534 | count++; |
||
535 | |||
536 | return count; |
||
537 | }>>>> |