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
17 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
19 * Permission is hereby granted, free of charge, to any person obtaining a
20 * copy of this software and associated documentation files (the "Software"),
21 * to deal in the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
26 * The above copyright notice and this permission notice shall be included
27 * in all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
33 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include "glapi/glthread.h"
44 #define TABLE_SIZE 1023 /**< Size of lookup table/array */
46 #define HASH_FUNC(K) ((K) % TABLE_SIZE)
50 * An entry in the hash table.
53 GLuint Key
; /**< the entry's key */
54 void *Data
; /**< the entry's data */
55 struct HashEntry
*Next
; /**< pointer to next entry */
60 * The hash table data structure.
62 struct _mesa_HashTable
{
63 struct HashEntry
*Table
[TABLE_SIZE
]; /**< the lookup table */
64 GLuint MaxKey
; /**< highest key inserted so far */
65 _glthread_Mutex Mutex
; /**< mutual exclusion lock */
66 _glthread_Mutex WalkMutex
; /**< for _mesa_HashWalk() */
67 GLboolean InDeleteAll
; /**< Debug check */
73 * Create a new hash table.
75 * \return pointer to a new, empty hash table.
77 struct _mesa_HashTable
*
78 _mesa_NewHashTable(void)
80 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
82 _glthread_INIT_MUTEX(table
->Mutex
);
83 _glthread_INIT_MUTEX(table
->WalkMutex
);
91 * Delete a hash table.
92 * Frees each entry on the hash table and then the hash table structure itself.
93 * Note that the caller should have already traversed the table and deleted
94 * the objects in the table (i.e. We don't free the entries' data pointer).
96 * \param table the hash table to delete.
99 _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
103 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
104 struct HashEntry
*entry
= table
->Table
[pos
];
106 struct HashEntry
*next
= entry
->Next
;
109 "In _mesa_DeleteHashTable, found non-freed data");
115 _glthread_DESTROY_MUTEX(table
->Mutex
);
116 _glthread_DESTROY_MUTEX(table
->WalkMutex
);
123 * Lookup an entry in the hash table.
125 * \param table the hash table.
126 * \param key the key.
128 * \return pointer to user's data or NULL if key not in table
131 _mesa_HashLookup(struct _mesa_HashTable
*table
, GLuint key
)
134 const struct HashEntry
*entry
;
139 pos
= HASH_FUNC(key
);
140 _glthread_LOCK_MUTEX(table
->Mutex
);
141 entry
= table
->Table
[pos
];
143 if (entry
->Key
== key
) {
144 _glthread_UNLOCK_MUTEX(table
->Mutex
);
149 _glthread_UNLOCK_MUTEX(table
->Mutex
);
156 * Insert a key/pointer pair into the hash table.
157 * If an entry with this key already exists we'll replace the existing entry.
159 * \param table the hash table.
160 * \param key the key (not zero).
161 * \param data pointer to user data.
164 _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
166 /* search for existing entry with this key */
168 struct HashEntry
*entry
;
173 _glthread_LOCK_MUTEX(table
->Mutex
);
175 if (key
> table
->MaxKey
)
178 pos
= HASH_FUNC(key
);
180 /* check if replacing an existing entry with same key */
181 for (entry
= table
->Table
[pos
]; entry
; entry
= entry
->Next
) {
182 if (entry
->Key
== key
) {
183 /* replace entry's data */
184 #if 0 /* not sure this check is always valid */
186 _mesa_problem(NULL
, "Memory leak detected in _mesa_HashInsert");
190 _glthread_UNLOCK_MUTEX(table
->Mutex
);
195 /* alloc and insert new table entry */
196 entry
= MALLOC_STRUCT(HashEntry
);
200 entry
->Next
= table
->Table
[pos
];
201 table
->Table
[pos
] = entry
;
204 _glthread_UNLOCK_MUTEX(table
->Mutex
);
210 * Remove an entry from the hash table.
212 * \param table the hash table.
213 * \param key key of entry to remove.
215 * While holding the hash table's lock, searches the entry with the matching
216 * key and unlinks it.
219 _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
222 struct HashEntry
*entry
, *prev
;
227 /* have to check this outside of mutex lock */
228 if (table
->InDeleteAll
) {
229 _mesa_problem(NULL
, "_mesa_HashRemove illegally called from "
230 "_mesa_HashDeleteAll callback function");
234 _glthread_LOCK_MUTEX(table
->Mutex
);
236 pos
= HASH_FUNC(key
);
238 entry
= table
->Table
[pos
];
240 if (entry
->Key
== key
) {
243 prev
->Next
= entry
->Next
;
246 table
->Table
[pos
] = entry
->Next
;
249 _glthread_UNLOCK_MUTEX(table
->Mutex
);
256 _glthread_UNLOCK_MUTEX(table
->Mutex
);
262 * Delete all entries in a hash table, but don't delete the table itself.
263 * Invoke the given callback function for each table entry.
265 * \param table the hash table to delete
266 * \param callback the callback function
267 * \param userData arbitrary pointer to pass along to the callback
268 * (this is typically a GLcontext pointer)
271 _mesa_HashDeleteAll(struct _mesa_HashTable
*table
,
272 void (*callback
)(GLuint key
, void *data
, void *userData
),
278 _glthread_LOCK_MUTEX(table
->Mutex
);
279 table
->InDeleteAll
= GL_TRUE
;
280 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
281 struct HashEntry
*entry
, *next
;
282 for (entry
= table
->Table
[pos
]; entry
; entry
= next
) {
283 callback(entry
->Key
, entry
->Data
, userData
);
287 table
->Table
[pos
] = NULL
;
289 table
->InDeleteAll
= GL_FALSE
;
290 _glthread_UNLOCK_MUTEX(table
->Mutex
);
295 * Walk over all entries in a hash table, calling callback function for each.
296 * Note: we use a separate mutex in this function to avoid a recursive
297 * locking deadlock (in case the callback calls _mesa_HashRemove()) and to
298 * prevent multiple threads/contexts from getting tangled up.
299 * A lock-less version of this function could be used when the table will
301 * \param table the hash table to walk
302 * \param callback the callback function
303 * \param userData arbitrary pointer to pass along to the callback
304 * (this is typically a GLcontext pointer)
307 _mesa_HashWalk(const struct _mesa_HashTable
*table
,
308 void (*callback
)(GLuint key
, void *data
, void *userData
),
311 /* cast-away const */
312 struct _mesa_HashTable
*table2
= (struct _mesa_HashTable
*) table
;
316 _glthread_LOCK_MUTEX(table2
->WalkMutex
);
317 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
318 struct HashEntry
*entry
, *next
;
319 for (entry
= table
->Table
[pos
]; entry
; entry
= next
) {
320 /* save 'next' pointer now in case the callback deletes the entry */
322 callback(entry
->Key
, entry
->Data
, userData
);
325 _glthread_UNLOCK_MUTEX(table2
->WalkMutex
);
330 * Return the key of the "first" entry in the hash table.
331 * While holding the lock, walks through all table positions until finding
332 * the first entry of the first non-empty one.
334 * \param table the hash table
335 * \return key for the "first" entry in the hash table.
338 _mesa_HashFirstEntry(struct _mesa_HashTable
*table
)
342 _glthread_LOCK_MUTEX(table
->Mutex
);
343 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
344 if (table
->Table
[pos
]) {
345 _glthread_UNLOCK_MUTEX(table
->Mutex
);
346 return table
->Table
[pos
]->Key
;
349 _glthread_UNLOCK_MUTEX(table
->Mutex
);
355 * Given a hash table key, return the next key. This is used to walk
356 * over all entries in the table. Note that the keys returned during
357 * walking won't be in any particular order.
358 * \return next hash key or 0 if end of table.
361 _mesa_HashNextEntry(const struct _mesa_HashTable
*table
, GLuint key
)
363 const struct HashEntry
*entry
;
369 /* Find the entry with given key */
370 pos
= HASH_FUNC(key
);
371 for (entry
= table
->Table
[pos
]; entry
; entry
= entry
->Next
) {
372 if (entry
->Key
== key
) {
378 /* the given key was not found, so we can't find the next entry */
383 /* return next in linked list */
384 return entry
->Next
->Key
;
387 /* look for next non-empty table slot */
389 while (pos
< TABLE_SIZE
) {
390 if (table
->Table
[pos
]) {
391 return table
->Table
[pos
]->Key
;
401 * Dump contents of hash table for debugging.
403 * \param table the hash table.
406 _mesa_HashPrint(const struct _mesa_HashTable
*table
)
410 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
411 const struct HashEntry
*entry
= table
->Table
[pos
];
413 _mesa_debug(NULL
, "%u %p\n", entry
->Key
, entry
->Data
);
422 * Find a block of adjacent unused hash keys.
424 * \param table the hash table.
425 * \param numKeys number of keys needed.
427 * \return Starting key of free block or 0 if failure.
429 * If there are enough free keys between the maximum key existing in the table
430 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
431 * the adjacent key. Otherwise do a full search for a free key block in the
432 * allowable key range.
435 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
437 const GLuint maxKey
= ~((GLuint
) 0);
438 _glthread_LOCK_MUTEX(table
->Mutex
);
439 if (maxKey
- numKeys
> table
->MaxKey
) {
440 /* the quick solution */
441 _glthread_UNLOCK_MUTEX(table
->Mutex
);
442 return table
->MaxKey
+ 1;
445 /* the slow solution */
446 GLuint freeCount
= 0;
447 GLuint freeStart
= 1;
449 for (key
= 1; key
!= maxKey
; key
++) {
450 if (_mesa_HashLookup(table
, key
)) {
451 /* darn, this key is already in use */
456 /* this key not in use, check if we've found enough */
458 if (freeCount
== numKeys
) {
459 _glthread_UNLOCK_MUTEX(table
->Mutex
);
464 /* cannot allocate a block of numKeys consecutive keys */
465 _glthread_UNLOCK_MUTEX(table
->Mutex
);
471 #if 0 /* debug only */
474 * Test walking over all the entries in a hash table.
477 test_hash_walking(void)
479 struct _mesa_HashTable
*t
= _mesa_NewHashTable();
480 const GLuint limit
= 50000;
483 /* create some entries */
484 for (i
= 0; i
< limit
; i
++) {
486 GLuint k
= (rand() % (limit
* 10)) + 1;
487 while (_mesa_HashLookup(t
, k
)) {
488 /* id already in use, try another */
489 k
= (rand() % (limit
* 10)) + 1;
491 _mesa_HashInsert(t
, k
, &dummy
);
494 /* walk over all entries */
496 GLuint k
= _mesa_HashFirstEntry(t
);
499 GLuint knext
= _mesa_HashNextEntry(t
, k
);
501 _mesa_HashRemove(t
, k
);
505 assert(count
== limit
);
506 k
= _mesa_HashFirstEntry(t
);
510 _mesa_DeleteHashTable(t
);
515 _mesa_test_hash_functions(void)
518 struct _mesa_HashTable
*t
;
520 t
= _mesa_NewHashTable();
521 _mesa_HashInsert(t
, 501, &a
);
522 _mesa_HashInsert(t
, 10, &c
);
523 _mesa_HashInsert(t
, 0xfffffff8, &b
);
524 /*_mesa_HashPrint(t);*/
526 assert(_mesa_HashLookup(t
,501));
527 assert(!_mesa_HashLookup(t
,1313));
528 assert(_mesa_HashFindFreeKeyBlock(t
, 100));
530 _mesa_DeleteHashTable(t
);