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 GLboolean InDeleteAll
; /**< Debug check */
72 * Create a new hash table.
74 * \return pointer to a new, empty hash table.
76 struct _mesa_HashTable
*
77 _mesa_NewHashTable(void)
79 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
81 _glthread_INIT_MUTEX(table
->Mutex
);
89 * Delete a hash table.
90 * Frees each entry on the hash table and then the hash table structure itself.
91 * Note that the caller should have already traversed the table and deleted
92 * the objects in the table (i.e. We don't free the entries' data pointer).
94 * \param table the hash table to delete.
97 _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
101 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
102 struct HashEntry
*entry
= table
->Table
[pos
];
104 struct HashEntry
*next
= entry
->Next
;
107 "In _mesa_DeleteHashTable, found non-freed data");
113 _glthread_DESTROY_MUTEX(table
->Mutex
);
120 * Lookup an entry in the hash table.
122 * \param table the hash table.
123 * \param key the key.
125 * \return pointer to user's data or NULL if key not in table
128 _mesa_HashLookup(const struct _mesa_HashTable
*table
, GLuint key
)
131 const struct HashEntry
*entry
;
136 pos
= HASH_FUNC(key
);
137 entry
= table
->Table
[pos
];
139 if (entry
->Key
== key
) {
150 * Insert a key/pointer pair into the hash table.
151 * If an entry with this key already exists we'll replace the existing entry.
153 * \param table the hash table.
154 * \param key the key (not zero).
155 * \param data pointer to user data.
158 _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
160 /* search for existing entry with this key */
162 struct HashEntry
*entry
;
167 _glthread_LOCK_MUTEX(table
->Mutex
);
169 if (key
> table
->MaxKey
)
172 pos
= HASH_FUNC(key
);
174 /* check if replacing an existing entry with same key */
175 for (entry
= table
->Table
[pos
]; entry
; entry
= entry
->Next
) {
176 if (entry
->Key
== key
) {
177 /* replace entry's data */
178 #if 0 /* not sure this check is always valid */
180 _mesa_problem(NULL
, "Memory leak detected in _mesa_HashInsert");
184 _glthread_UNLOCK_MUTEX(table
->Mutex
);
189 /* alloc and insert new table entry */
190 entry
= MALLOC_STRUCT(HashEntry
);
193 entry
->Next
= table
->Table
[pos
];
194 table
->Table
[pos
] = entry
;
196 _glthread_UNLOCK_MUTEX(table
->Mutex
);
202 * Remove an entry from the hash table.
204 * \param table the hash table.
205 * \param key key of entry to remove.
207 * While holding the hash table's lock, searches the entry with the matching
208 * key and unlinks it.
211 _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
214 struct HashEntry
*entry
, *prev
;
219 /* have to check this outside of mutex lock */
220 if (table
->InDeleteAll
) {
221 _mesa_problem(NULL
, "_mesa_HashRemove illegally called from "
222 "_mesa_HashDeleteAll callback function");
226 _glthread_LOCK_MUTEX(table
->Mutex
);
228 pos
= HASH_FUNC(key
);
230 entry
= table
->Table
[pos
];
232 if (entry
->Key
== key
) {
235 prev
->Next
= entry
->Next
;
238 table
->Table
[pos
] = entry
->Next
;
241 _glthread_UNLOCK_MUTEX(table
->Mutex
);
248 _glthread_UNLOCK_MUTEX(table
->Mutex
);
254 * Delete all entries in a hash table, but don't delete the table itself.
255 * Invoke the given callback function for each table entry.
257 * \param table the hash table to delete
258 * \param callback the callback function
259 * \param userData arbitrary pointer to pass along to the callback
260 * (this is typically a GLcontext pointer)
263 _mesa_HashDeleteAll(struct _mesa_HashTable
*table
,
264 void (*callback
)(GLuint key
, void *data
, void *userData
),
270 _glthread_LOCK_MUTEX(table
->Mutex
);
271 table
->InDeleteAll
= GL_TRUE
;
272 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
273 struct HashEntry
*entry
, *next
;
274 for (entry
= table
->Table
[pos
]; entry
; entry
= next
) {
275 callback(entry
->Key
, entry
->Data
, userData
);
279 table
->Table
[pos
] = NULL
;
281 table
->InDeleteAll
= GL_FALSE
;
282 _glthread_UNLOCK_MUTEX(table
->Mutex
);
287 * Walk over all entries in a hash table, calling callback function for each.
288 * \param table the hash table to walk
289 * \param callback the callback function
290 * \param userData arbitrary pointer to pass along to the callback
291 * (this is typically a GLcontext pointer)
294 _mesa_HashWalk(const struct _mesa_HashTable
*table
,
295 void (*callback
)(GLuint key
, void *data
, void *userData
),
298 /* cast-away const */
299 struct _mesa_HashTable
*table2
= (struct _mesa_HashTable
*) table
;
303 _glthread_UNLOCK_MUTEX(table2
->Mutex
);
304 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
305 struct HashEntry
*entry
;
306 for (entry
= table
->Table
[pos
]; entry
; entry
= entry
->Next
) {
307 callback(entry
->Key
, entry
->Data
, userData
);
310 _glthread_UNLOCK_MUTEX(table2
->Mutex
);
315 * Return the key of the "first" entry in the hash table.
316 * While holding the lock, walks through all table positions until finding
317 * the first entry of the first non-empty one.
319 * \param table the hash table
320 * \return key for the "first" entry in the hash table.
323 _mesa_HashFirstEntry(struct _mesa_HashTable
*table
)
327 _glthread_LOCK_MUTEX(table
->Mutex
);
328 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
329 if (table
->Table
[pos
]) {
330 _glthread_UNLOCK_MUTEX(table
->Mutex
);
331 return table
->Table
[pos
]->Key
;
334 _glthread_UNLOCK_MUTEX(table
->Mutex
);
340 * Given a hash table key, return the next key. This is used to walk
341 * over all entries in the table. Note that the keys returned during
342 * walking won't be in any particular order.
343 * \return next hash key or 0 if end of table.
346 _mesa_HashNextEntry(const struct _mesa_HashTable
*table
, GLuint key
)
348 const struct HashEntry
*entry
;
354 /* Find the entry with given key */
355 pos
= HASH_FUNC(key
);
356 for (entry
= table
->Table
[pos
]; entry
; entry
= entry
->Next
) {
357 if (entry
->Key
== key
) {
363 /* the given key was not found, so we can't find the next entry */
368 /* return next in linked list */
369 return entry
->Next
->Key
;
372 /* look for next non-empty table slot */
374 while (pos
< TABLE_SIZE
) {
375 if (table
->Table
[pos
]) {
376 return table
->Table
[pos
]->Key
;
386 * Dump contents of hash table for debugging.
388 * \param table the hash table.
391 _mesa_HashPrint(const struct _mesa_HashTable
*table
)
395 for (pos
= 0; pos
< TABLE_SIZE
; pos
++) {
396 const struct HashEntry
*entry
= table
->Table
[pos
];
398 _mesa_debug(NULL
, "%u %p\n", entry
->Key
, entry
->Data
);
407 * Find a block of adjacent unused hash keys.
409 * \param table the hash table.
410 * \param numKeys number of keys needed.
412 * \return Starting key of free block or 0 if failure.
414 * If there are enough free keys between the maximum key existing in the table
415 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
416 * the adjacent key. Otherwise do a full search for a free key block in the
417 * allowable key range.
420 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
422 const GLuint maxKey
= ~((GLuint
) 0);
423 _glthread_LOCK_MUTEX(table
->Mutex
);
424 if (maxKey
- numKeys
> table
->MaxKey
) {
425 /* the quick solution */
426 _glthread_UNLOCK_MUTEX(table
->Mutex
);
427 return table
->MaxKey
+ 1;
430 /* the slow solution */
431 GLuint freeCount
= 0;
432 GLuint freeStart
= 1;
434 for (key
= 1; key
!= maxKey
; key
++) {
435 if (_mesa_HashLookup(table
, key
)) {
436 /* darn, this key is already in use */
441 /* this key not in use, check if we've found enough */
443 if (freeCount
== numKeys
) {
444 _glthread_UNLOCK_MUTEX(table
->Mutex
);
449 /* cannot allocate a block of numKeys consecutive keys */
450 _glthread_UNLOCK_MUTEX(table
->Mutex
);
456 #if 0 /* debug only */
459 * Test walking over all the entries in a hash table.
462 test_hash_walking(void)
464 struct _mesa_HashTable
*t
= _mesa_NewHashTable();
465 const GLuint limit
= 50000;
468 /* create some entries */
469 for (i
= 0; i
< limit
; i
++) {
471 GLuint k
= (rand() % (limit
* 10)) + 1;
472 while (_mesa_HashLookup(t
, k
)) {
473 /* id already in use, try another */
474 k
= (rand() % (limit
* 10)) + 1;
476 _mesa_HashInsert(t
, k
, &dummy
);
479 /* walk over all entries */
481 GLuint k
= _mesa_HashFirstEntry(t
);
484 GLuint knext
= _mesa_HashNextEntry(t
, k
);
486 _mesa_HashRemove(t
, k
);
490 assert(count
== limit
);
491 k
= _mesa_HashFirstEntry(t
);
495 _mesa_DeleteHashTable(t
);
500 _mesa_test_hash_functions(void)
503 struct _mesa_HashTable
*t
;
505 t
= _mesa_NewHashTable();
506 _mesa_HashInsert(t
, 501, &a
);
507 _mesa_HashInsert(t
, 10, &c
);
508 _mesa_HashInsert(t
, 0xfffffff8, &b
);
509 /*_mesa_HashPrint(t);*/
511 assert(_mesa_HashLookup(t
,501));
512 assert(!_mesa_HashLookup(t
,1313));
513 assert(_mesa_HashFindFreeKeyBlock(t
, 100));
515 _mesa_DeleteHashTable(t
);