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.
39 #include "util/hash_table.h"
43 * Create a new hash table.
45 * \return pointer to a new, empty hash table.
47 struct _mesa_HashTable
*
48 _mesa_NewHashTable(void)
50 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
53 table
->ht
= _mesa_hash_table_create(NULL
, uint_key_hash
,
55 if (table
->ht
== NULL
) {
57 _mesa_error_no_memory(__func__
);
61 _mesa_hash_table_set_deleted_key(table
->ht
, uint_key(DELETED_KEY_VALUE
));
63 * Needs to be recursive, since the callback in _mesa_HashWalk()
64 * is allowed to call _mesa_HashRemove().
66 mtx_init(&table
->Mutex
, mtx_recursive
);
69 _mesa_error_no_memory(__func__
);
78 * Delete a hash table.
79 * Frees each entry on the hash table and then the hash table structure itself.
80 * Note that the caller should have already traversed the table and deleted
81 * the objects in the table (i.e. We don't free the entries' data pointer).
83 * \param table the hash table to delete.
86 _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
90 if (_mesa_hash_table_next_entry(table
->ht
, NULL
) != NULL
) {
91 _mesa_problem(NULL
, "In _mesa_DeleteHashTable, found non-freed data");
94 _mesa_hash_table_destroy(table
->ht
, NULL
);
96 mtx_destroy(&table
->Mutex
);
103 * Lookup an entry in the hash table, without locking.
104 * \sa _mesa_HashLookup
107 _mesa_HashLookup_unlocked(struct _mesa_HashTable
*table
, GLuint key
)
109 const struct hash_entry
*entry
;
114 if (key
== DELETED_KEY_VALUE
)
115 return table
->deleted_key_data
;
117 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
,
128 * Lookup an entry in the hash table.
130 * \param table the hash table.
131 * \param key the key.
133 * \return pointer to user's data or NULL if key not in table
136 _mesa_HashLookup(struct _mesa_HashTable
*table
, GLuint key
)
139 _mesa_HashLockMutex(table
);
140 res
= _mesa_HashLookup_unlocked(table
, key
);
141 _mesa_HashUnlockMutex(table
);
147 * Lookup an entry in the hash table without locking the mutex.
149 * The hash table mutex must be locked manually by calling
150 * _mesa_HashLockMutex() before calling this function.
152 * \param table the hash table.
153 * \param key the key.
155 * \return pointer to user's data or NULL if key not in table
158 _mesa_HashLookupLocked(struct _mesa_HashTable
*table
, GLuint key
)
160 return _mesa_HashLookup_unlocked(table
, key
);
165 _mesa_HashInsert_unlocked(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
167 uint32_t hash
= uint_hash(key
);
168 struct hash_entry
*entry
;
173 if (key
> table
->MaxKey
)
176 if (key
== DELETED_KEY_VALUE
) {
177 table
->deleted_key_data
= data
;
179 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
, hash
, uint_key(key
));
183 _mesa_hash_table_insert_pre_hashed(table
->ht
, hash
, uint_key(key
), data
);
190 * Insert a key/pointer pair into the hash table without locking the mutex.
191 * If an entry with this key already exists we'll replace the existing entry.
193 * The hash table mutex must be locked manually by calling
194 * _mesa_HashLockMutex() before calling this function.
196 * \param table the hash table.
197 * \param key the key (not zero).
198 * \param data pointer to user data.
201 _mesa_HashInsertLocked(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
203 _mesa_HashInsert_unlocked(table
, key
, data
);
208 * Insert a key/pointer pair into the hash table.
209 * If an entry with this key already exists we'll replace the existing entry.
211 * \param table the hash table.
212 * \param key the key (not zero).
213 * \param data pointer to user data.
216 _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
218 _mesa_HashLockMutex(table
);
219 _mesa_HashInsert_unlocked(table
, key
, data
);
220 _mesa_HashUnlockMutex(table
);
225 * Remove an entry from the hash table.
227 * \param table the hash table.
228 * \param key key of entry to remove.
230 * While holding the hash table's lock, searches the entry with the matching
231 * key and unlinks it.
234 _mesa_HashRemove_unlocked(struct _mesa_HashTable
*table
, GLuint key
)
236 struct hash_entry
*entry
;
241 /* assert if _mesa_HashRemove illegally called from _mesa_HashDeleteAll
242 * callback function. Have to check this outside of mutex lock.
244 assert(!table
->InDeleteAll
);
246 if (key
== DELETED_KEY_VALUE
) {
247 table
->deleted_key_data
= NULL
;
249 entry
= _mesa_hash_table_search_pre_hashed(table
->ht
,
252 _mesa_hash_table_remove(table
->ht
, entry
);
258 _mesa_HashRemoveLocked(struct _mesa_HashTable
*table
, GLuint key
)
260 _mesa_HashRemove_unlocked(table
, key
);
264 _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
266 _mesa_HashLockMutex(table
);
267 _mesa_HashRemove_unlocked(table
, key
);
268 _mesa_HashUnlockMutex(table
);
272 * Delete all entries in a hash table, but don't delete the table itself.
273 * Invoke the given callback function for each table entry.
275 * \param table the hash table to delete
276 * \param callback the callback function
277 * \param userData arbitrary pointer to pass along to the callback
278 * (this is typically a struct gl_context pointer)
281 _mesa_HashDeleteAll(struct _mesa_HashTable
*table
,
282 void (*callback
)(GLuint key
, void *data
, void *userData
),
285 struct hash_entry
*entry
;
288 _mesa_HashLockMutex(table
);
289 table
->InDeleteAll
= GL_TRUE
;
290 hash_table_foreach(table
->ht
, entry
) {
291 callback((uintptr_t)entry
->key
, entry
->data
, userData
);
292 _mesa_hash_table_remove(table
->ht
, entry
);
294 if (table
->deleted_key_data
) {
295 callback(DELETED_KEY_VALUE
, table
->deleted_key_data
, userData
);
296 table
->deleted_key_data
= NULL
;
298 table
->InDeleteAll
= GL_FALSE
;
299 _mesa_HashUnlockMutex(table
);
304 * Walk over all entries in a hash table, calling callback function for each.
305 * \param table the hash table to walk
306 * \param callback the callback function
307 * \param userData arbitrary pointer to pass along to the callback
308 * (this is typically a struct gl_context pointer)
311 hash_walk_unlocked(const struct _mesa_HashTable
*table
,
312 void (*callback
)(GLuint key
, void *data
, void *userData
),
318 struct hash_entry
*entry
;
319 hash_table_foreach(table
->ht
, entry
) {
320 callback((uintptr_t)entry
->key
, entry
->data
, userData
);
322 if (table
->deleted_key_data
)
323 callback(DELETED_KEY_VALUE
, table
->deleted_key_data
, userData
);
328 _mesa_HashWalk(const struct _mesa_HashTable
*table
,
329 void (*callback
)(GLuint key
, void *data
, void *userData
),
332 /* cast-away const */
333 struct _mesa_HashTable
*table2
= (struct _mesa_HashTable
*) table
;
335 _mesa_HashLockMutex(table2
);
336 hash_walk_unlocked(table
, callback
, userData
);
337 _mesa_HashUnlockMutex(table2
);
341 _mesa_HashWalkLocked(const struct _mesa_HashTable
*table
,
342 void (*callback
)(GLuint key
, void *data
, void *userData
),
345 hash_walk_unlocked(table
, callback
, userData
);
349 debug_print_entry(GLuint key
, void *data
, void *userData
)
351 _mesa_debug(NULL
, "%u %p\n", key
, data
);
355 * Dump contents of hash table for debugging.
357 * \param table the hash table.
360 _mesa_HashPrint(const struct _mesa_HashTable
*table
)
362 if (table
->deleted_key_data
)
363 debug_print_entry(DELETED_KEY_VALUE
, table
->deleted_key_data
, NULL
);
364 _mesa_HashWalk(table
, debug_print_entry
, NULL
);
369 * Find a block of adjacent unused hash keys.
371 * \param table the hash table.
372 * \param numKeys number of keys needed.
374 * \return Starting key of free block or 0 if failure.
376 * If there are enough free keys between the maximum key existing in the table
377 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
378 * the adjacent key. Otherwise do a full search for a free key block in the
379 * allowable key range.
382 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
384 const GLuint maxKey
= ~((GLuint
) 0) - 1;
385 if (maxKey
- numKeys
> table
->MaxKey
) {
386 /* the quick solution */
387 return table
->MaxKey
+ 1;
390 /* the slow solution */
391 GLuint freeCount
= 0;
392 GLuint freeStart
= 1;
394 for (key
= 1; key
!= maxKey
; key
++) {
395 if (_mesa_HashLookup_unlocked(table
, key
)) {
396 /* darn, this key is already in use */
401 /* this key not in use, check if we've found enough */
403 if (freeCount
== numKeys
) {
408 /* cannot allocate a block of numKeys consecutive keys */
415 * Return the number of entries in the hash table.
418 _mesa_HashNumEntries(const struct _mesa_HashTable
*table
)
422 if (table
->deleted_key_data
)
425 count
+= _mesa_hash_table_num_entries(table
->ht
);