b7a7bd9f64804498c4fd716ee362a38addf15bef
5 * Used for display lists, texture objects, vertex/fragment programs,
6 * buffer objects, etc. The hash functions are thread-safe.
8 * \note key=0 is illegal.
14 * Mesa 3-D graphics library
16 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
25 * The above copyright notice and this permission notice shall be included
26 * in all copies or substantial portions of the Software.
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.
40 #include "util/hash_table.h"
43 * Magic GLuint object name that gets stored outside of the struct hash_table.
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.
53 #define DELETED_KEY_VALUE 1
56 * The hash table data structure.
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 GLboolean InDeleteAll
; /**< Debug check */
63 /** Value that would be in the table for DELETED_KEY_VALUE. */
64 void *deleted_key_data
;
68 * Mapping from our use of GLuint as both the key and the hash value to the
71 * There exist many integer hash functions, designed to avoid collisions when
72 * the integers are spread across key space with some patterns. In GL, the
73 * pattern (in the case of glGen*()ed object IDs) is that the keys are unique
74 * contiguous integers starting from 1. Because of that, we just use the key
75 * as the hash value, to minimize the cost of the hash function. If objects
76 * are never deleted, we will never see a collision in the table, because the
77 * table resizes itself when it approaches full, and thus key % table_size ==
80 * The case where we could have collisions for genned objects would be
81 * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50);
82 * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at
83 * the end of that sequence, instead of 1-150. So far it doesn't appear to be
87 uint_key_compare(const void *a
, const void *b
)
99 uint_key_hash(const void *key
)
101 return uint_hash((uintptr_t)key
);
107 return (void *)(uintptr_t) id
;
112 * Create a new hash table.
114 * \return pointer to a new, empty hash table.
116 struct _mesa_HashTable
*
117 _mesa_NewHashTable(void)
119 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
122 table
->ht
= _mesa_hash_table_create(NULL
, uint_key_hash
,
124 if (table
->ht
== NULL
) {
126 _mesa_error_no_memory(__func__
);
130 _mesa_hash_table_set_deleted_key(table
->ht
, uint_key(DELETED_KEY_VALUE
));
132 * Needs to be recursive, since the callback in _mesa_HashWalk()
133 * is allowed to call _mesa_HashRemove().
135 mtx_init(&table
->Mutex
, mtx_recursive
);
138 _mesa_error_no_memory(__func__
);
147 * Delete a hash table.
148 * Frees each entry on the hash table and then the hash table structure itself.
149 * Note that the caller should have already traversed the table and deleted
150 * the objects in the table (i.e. We don't free the entries' data pointer).
152 * \param table the hash table to delete.
155 _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
159 if (_mesa_hash_table_next_entry(table
->ht
, NULL
) != NULL
) {
160 _mesa_problem(NULL
, "In _mesa_DeleteHashTable, found non-freed data");
163 _mesa_hash_table_destroy(table
->ht
, NULL
);
165 mtx_destroy(&table
->Mutex
);
172 * Lookup an entry in the hash table, without locking.
173 * \sa _mesa_HashLookup
176 _mesa_HashLookup_unlocked(struct _mesa_HashTable
*table
, GLuint key
)
178 const struct hash_entry
*entry
;
183 if (key
== DELETED_KEY_VALUE
)
184 return table
->deleted_key_data
;
186 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
,
197 * Lookup an entry in the hash table.
199 * \param table the hash table.
200 * \param key the key.
202 * \return pointer to user's data or NULL if key not in table
205 _mesa_HashLookup(struct _mesa_HashTable
*table
, GLuint key
)
209 mtx_lock(&table
->Mutex
);
210 res
= _mesa_HashLookup_unlocked(table
, key
);
211 mtx_unlock(&table
->Mutex
);
217 * Lookup an entry in the hash table without locking the mutex.
219 * The hash table mutex must be locked manually by calling
220 * _mesa_HashLockMutex() before calling this function.
222 * \param table the hash table.
223 * \param key the key.
225 * \return pointer to user's data or NULL if key not in table
228 _mesa_HashLookupLocked(struct _mesa_HashTable
*table
, GLuint key
)
230 return _mesa_HashLookup_unlocked(table
, key
);
235 * Lock the hash table mutex.
237 * This function should be used when multiple objects need
238 * to be looked up in the hash table, to avoid having to lock
239 * and unlock the mutex each time.
241 * \param table the hash table.
244 _mesa_HashLockMutex(struct _mesa_HashTable
*table
)
247 mtx_lock(&table
->Mutex
);
252 * Unlock the hash table mutex.
254 * \param table the hash table.
257 _mesa_HashUnlockMutex(struct _mesa_HashTable
*table
)
260 mtx_unlock(&table
->Mutex
);
265 _mesa_HashInsert_unlocked(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
267 uint32_t hash
= uint_hash(key
);
268 struct hash_entry
*entry
;
273 if (key
> table
->MaxKey
)
276 if (key
== DELETED_KEY_VALUE
) {
277 table
->deleted_key_data
= data
;
279 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
, hash
, uint_key(key
));
283 _mesa_hash_table_insert_pre_hashed(table
->ht
, hash
, uint_key(key
), data
);
290 * Insert a key/pointer pair into the hash table without locking the mutex.
291 * If an entry with this key already exists we'll replace the existing entry.
293 * The hash table mutex must be locked manually by calling
294 * _mesa_HashLockMutex() before calling this function.
296 * \param table the hash table.
297 * \param key the key (not zero).
298 * \param data pointer to user data.
301 _mesa_HashInsertLocked(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
303 _mesa_HashInsert_unlocked(table
, key
, data
);
308 * Insert a key/pointer pair into the hash table.
309 * If an entry with this key already exists we'll replace the existing entry.
311 * \param table the hash table.
312 * \param key the key (not zero).
313 * \param data pointer to user data.
316 _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
319 mtx_lock(&table
->Mutex
);
320 _mesa_HashInsert_unlocked(table
, key
, data
);
321 mtx_unlock(&table
->Mutex
);
326 * Remove an entry from the hash table.
328 * \param table the hash table.
329 * \param key key of entry to remove.
331 * While holding the hash table's lock, searches the entry with the matching
332 * key and unlinks it.
335 _mesa_HashRemove_unlocked(struct _mesa_HashTable
*table
, GLuint key
)
337 struct hash_entry
*entry
;
342 /* have to check this outside of mutex lock */
343 if (table
->InDeleteAll
) {
344 _mesa_problem(NULL
, "_mesa_HashRemove illegally called from "
345 "_mesa_HashDeleteAll callback function");
349 if (key
== DELETED_KEY_VALUE
) {
350 table
->deleted_key_data
= NULL
;
352 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
,
355 _mesa_hash_table_remove(table
->ht
, entry
);
361 _mesa_HashRemoveLocked(struct _mesa_HashTable
*table
, GLuint key
)
363 _mesa_HashRemove_unlocked(table
, key
);
367 _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
369 mtx_lock(&table
->Mutex
);
370 _mesa_HashRemove_unlocked(table
, key
);
371 mtx_unlock(&table
->Mutex
);
375 * Delete all entries in a hash table, but don't delete the table itself.
376 * Invoke the given callback function for each table entry.
378 * \param table the hash table to delete
379 * \param callback the callback function
380 * \param userData arbitrary pointer to pass along to the callback
381 * (this is typically a struct gl_context pointer)
384 _mesa_HashDeleteAll(struct _mesa_HashTable
*table
,
385 void (*callback
)(GLuint key
, void *data
, void *userData
),
388 struct hash_entry
*entry
;
392 mtx_lock(&table
->Mutex
);
393 table
->InDeleteAll
= GL_TRUE
;
394 hash_table_foreach(table
->ht
, entry
) {
395 callback((uintptr_t)entry
->key
, entry
->data
, userData
);
396 _mesa_hash_table_remove(table
->ht
, entry
);
398 if (table
->deleted_key_data
) {
399 callback(DELETED_KEY_VALUE
, table
->deleted_key_data
, userData
);
400 table
->deleted_key_data
= NULL
;
402 table
->InDeleteAll
= GL_FALSE
;
403 mtx_unlock(&table
->Mutex
);
408 * Walk over all entries in a hash table, calling callback function for each.
409 * \param table the hash table to walk
410 * \param callback the callback function
411 * \param userData arbitrary pointer to pass along to the callback
412 * (this is typically a struct gl_context pointer)
415 _mesa_HashWalk(const struct _mesa_HashTable
*table
,
416 void (*callback
)(GLuint key
, void *data
, void *userData
),
419 /* cast-away const */
420 struct _mesa_HashTable
*table2
= (struct _mesa_HashTable
*) table
;
421 struct hash_entry
*entry
;
425 mtx_lock(&table2
->Mutex
);
426 hash_table_foreach(table
->ht
, entry
) {
427 callback((uintptr_t)entry
->key
, entry
->data
, userData
);
429 if (table
->deleted_key_data
)
430 callback(DELETED_KEY_VALUE
, table
->deleted_key_data
, userData
);
431 mtx_unlock(&table2
->Mutex
);
435 debug_print_entry(GLuint key
, void *data
, void *userData
)
437 _mesa_debug(NULL
, "%u %p\n", key
, data
);
441 * Dump contents of hash table for debugging.
443 * \param table the hash table.
446 _mesa_HashPrint(const struct _mesa_HashTable
*table
)
448 if (table
->deleted_key_data
)
449 debug_print_entry(DELETED_KEY_VALUE
, table
->deleted_key_data
, NULL
);
450 _mesa_HashWalk(table
, debug_print_entry
, NULL
);
455 * Find a block of adjacent unused hash keys.
457 * \param table the hash table.
458 * \param numKeys number of keys needed.
460 * \return Starting key of free block or 0 if failure.
462 * If there are enough free keys between the maximum key existing in the table
463 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
464 * the adjacent key. Otherwise do a full search for a free key block in the
465 * allowable key range.
468 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
470 const GLuint maxKey
= ~((GLuint
) 0) - 1;
471 if (maxKey
- numKeys
> table
->MaxKey
) {
472 /* the quick solution */
473 return table
->MaxKey
+ 1;
476 /* the slow solution */
477 GLuint freeCount
= 0;
478 GLuint freeStart
= 1;
480 for (key
= 1; key
!= maxKey
; key
++) {
481 if (_mesa_HashLookup_unlocked(table
, key
)) {
482 /* darn, this key is already in use */
487 /* this key not in use, check if we've found enough */
489 if (freeCount
== numKeys
) {
494 /* cannot allocate a block of numKeys consecutive keys */
501 * Return the number of entries in the hash table.
504 _mesa_HashNumEntries(const struct _mesa_HashTable
*table
)
508 if (table
->deleted_key_data
)
511 count
+= _mesa_hash_table_num_entries(table
->ht
);