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-2005 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.
44 #define TABLE_SIZE 1023 /**< Size of lookup table/array */
47 * An entry in the hash table.
49 * This struct is private to this file.
52 GLuint Key
; /**< the entry's key */
53 void *Data
; /**< the entry's data */
54 struct HashEntry
*Next
; /**< pointer to next entry */
58 * The hash table data structure.
60 * This is an opaque types (it's not defined in hash.h file).
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 */
70 * Create a new hash table.
72 * \return pointer to a new, empty hash table.
74 struct _mesa_HashTable
*
75 _mesa_NewHashTable(void)
77 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
79 _glthread_INIT_MUTEX(table
->Mutex
);
87 * Delete a hash table.
88 * Frees each entry on the hash table and then the hash table structure itself.
89 * Note that the caller should have already traversed the table and deleted
90 * the objects in the table (i.e. We don't free the entries' data pointer).
92 * \param table the hash table to delete.
95 _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
99 for (i
= 0; i
< TABLE_SIZE
; i
++) {
100 struct HashEntry
*entry
= table
->Table
[i
];
102 struct HashEntry
*next
= entry
->Next
;
107 _glthread_DESTROY_MUTEX(table
->Mutex
);
114 * Lookup an entry in the hash table.
116 * \param table the hash table.
117 * \param key the key.
119 * \return pointer to user's data or NULL if key not in table
122 _mesa_HashLookup(const struct _mesa_HashTable
*table
, GLuint key
)
125 const struct HashEntry
*entry
;
130 pos
= key
& (TABLE_SIZE
-1);
131 entry
= table
->Table
[pos
];
133 if (entry
->Key
== key
) {
144 * Insert a key/pointer pair into the hash table.
145 * If an entry with this key already exists we'll replace the existing entry.
147 * \param table the hash table.
148 * \param key the key (not zero).
149 * \param data pointer to user data.
152 _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
154 /* search for existing entry with this key */
156 struct HashEntry
*entry
;
161 _glthread_LOCK_MUTEX(table
->Mutex
);
163 if (key
> table
->MaxKey
)
166 pos
= key
& (TABLE_SIZE
-1);
167 entry
= table
->Table
[pos
];
169 if (entry
->Key
== key
) {
170 /* replace entry's data */
172 _glthread_UNLOCK_MUTEX(table
->Mutex
);
178 /* alloc and insert new table entry */
179 entry
= MALLOC_STRUCT(HashEntry
);
182 entry
->Next
= table
->Table
[pos
];
183 table
->Table
[pos
] = entry
;
185 _glthread_UNLOCK_MUTEX(table
->Mutex
);
191 * Remove an entry from the hash table.
193 * \param table the hash table.
194 * \param key key of entry to remove.
196 * While holding the hash table's lock, searches the entry with the matching
197 * key and unlinks it.
200 _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
203 struct HashEntry
*entry
, *prev
;
208 _glthread_LOCK_MUTEX(table
->Mutex
);
210 pos
= key
& (TABLE_SIZE
-1);
212 entry
= table
->Table
[pos
];
214 if (entry
->Key
== key
) {
217 prev
->Next
= entry
->Next
;
220 table
->Table
[pos
] = entry
->Next
;
223 _glthread_UNLOCK_MUTEX(table
->Mutex
);
230 _glthread_UNLOCK_MUTEX(table
->Mutex
);
236 * Get the key of the "first" entry in the hash table.
238 * This is used in the course of deleting all display lists when
239 * a context is destroyed.
241 * \param table the hash table
243 * \return key for the "first" entry in the hash table.
245 * While holding the lock, walks through all table positions until finding
246 * the first entry of the first non-empty one.
249 _mesa_HashFirstEntry(struct _mesa_HashTable
*table
)
253 _glthread_LOCK_MUTEX(table
->Mutex
);
254 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
255 if (table
->Table
[pos
]) {
256 _glthread_UNLOCK_MUTEX(table
->Mutex
);
257 return table
->Table
[pos
]->Key
;
260 _glthread_UNLOCK_MUTEX(table
->Mutex
);
266 * Given a hash table key, return the next key. This is used to walk
267 * over all entries in the table. Note that the keys returned during
268 * walking won't be in any particular order.
269 * \return next hash key or 0 if end of table.
272 _mesa_HashNextEntry(const struct _mesa_HashTable
*table
, GLuint key
)
274 const struct HashEntry
*entry
;
280 /* Find the entry with given key */
281 pos
= key
& (TABLE_SIZE
- 1);
282 entry
= table
->Table
[pos
];
284 if (entry
->Key
== key
) {
291 /* the key was not found, we can't find next entry */
296 /* return next in linked list */
297 return entry
->Next
->Key
;
300 /* look for next non-empty table slot */
302 while (pos
< TABLE_SIZE
) {
303 if (table
->Table
[pos
]) {
304 return table
->Table
[pos
]->Key
;
314 * Dump contents of hash table for debugging.
316 * \param table the hash table.
319 _mesa_HashPrint(const struct _mesa_HashTable
*table
)
323 for (i
=0;i
<TABLE_SIZE
;i
++) {
324 const struct HashEntry
*entry
= table
->Table
[i
];
326 _mesa_debug(NULL
, "%u %p\n", entry
->Key
, entry
->Data
);
335 * Find a block of adjacent unused hash keys.
337 * \param table the hash table.
338 * \param numKeys number of keys needed.
340 * \return Starting key of free block or 0 if failure.
342 * If there are enough free keys between the maximum key existing in the table
343 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
344 * the adjacent key. Otherwise do a full search for a free key block in the
345 * allowable key range.
348 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
350 GLuint maxKey
= ~((GLuint
) 0);
351 _glthread_LOCK_MUTEX(table
->Mutex
);
352 if (maxKey
- numKeys
> table
->MaxKey
) {
353 /* the quick solution */
354 _glthread_UNLOCK_MUTEX(table
->Mutex
);
355 return table
->MaxKey
+ 1;
358 /* the slow solution */
359 GLuint freeCount
= 0;
360 GLuint freeStart
= 1;
362 for (key
=1; key
!=maxKey
; key
++) {
363 if (_mesa_HashLookup(table
, key
)) {
364 /* darn, this key is already in use */
369 /* this key not in use, check if we've found enough */
371 if (freeCount
== numKeys
) {
372 _glthread_UNLOCK_MUTEX(table
->Mutex
);
377 /* cannot allocate a block of numKeys consecutive keys */
378 _glthread_UNLOCK_MUTEX(table
->Mutex
);
385 * Test walking over all the entries in a hash table.
388 test_hash_walking(void)
390 struct _mesa_HashTable
*t
= _mesa_NewHashTable();
391 const GLuint limit
= 50000;
394 /* create some entries */
395 for (i
= 0; i
< limit
; i
++) {
397 GLuint k
= (rand() % (limit
* 10)) + 1;
398 while (_mesa_HashLookup(t
, k
)) {
399 /* id already in use, try another */
400 k
= (rand() % (limit
* 10)) + 1;
402 _mesa_HashInsert(t
, k
, &dummy
);
405 /* walk over all entries */
407 GLuint k
= _mesa_HashFirstEntry(t
);
410 GLuint knext
= _mesa_HashNextEntry(t
, k
);
412 _mesa_HashRemove(t
, k
);
416 assert(count
== limit
);
417 k
= _mesa_HashFirstEntry(t
);
421 _mesa_DeleteHashTable(t
);
426 _mesa_test_hash_functions(void)
429 struct _mesa_HashTable
*t
;
431 t
= _mesa_NewHashTable();
432 _mesa_HashInsert(t
, 501, &a
);
433 _mesa_HashInsert(t
, 10, &c
);
434 _mesa_HashInsert(t
, 0xfffffff8, &b
);
435 /*_mesa_HashPrint(t);*/
437 assert(_mesa_HashLookup(t
,501));
438 assert(!_mesa_HashLookup(t
,1313));
439 assert(_mesa_HashFindFreeKeyBlock(t
, 100));
441 _mesa_DeleteHashTable(t
);