5 * Used for display lists and texture objects. The hash functions are
8 * \note key=0 is illegal.
14 * Mesa 3-D graphics library
17 * Copyright (C) 1999-2002 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.
45 #define TABLE_SIZE 1023 /**< Size of lookup table/array */
48 * An entry in the hash table.
50 * This struct is private to this file.
53 GLuint Key
; /**< the entry's key */
54 void *Data
; /**< the entry's data */
55 struct HashEntry
*Next
; /**< pointer to next entry */
59 * The hash table data structure.
61 * This is an opaque types (it's not defined in hash.h file).
63 struct _mesa_HashTable
{
64 struct HashEntry
*Table
[TABLE_SIZE
]; /**< the lookup table */
65 GLuint MaxKey
; /**< highest key inserted so far */
66 _glthread_Mutex Mutex
; /**< mutual exclusion lock */
72 * Create a new hash table.
74 * \return pointer to a new, empty hash table.
76 struct _mesa_HashTable
*_mesa_NewHashTable(void)
78 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
80 _glthread_INIT_MUTEX(table
->Mutex
);
88 * Delete a hash table.
90 * \param table the hash table to delete.
92 * Frees each entry on the hash table and then the hash table structure itself.
94 void _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
98 for (i
=0;i
<TABLE_SIZE
;i
++) {
99 struct HashEntry
*entry
= table
->Table
[i
];
101 struct HashEntry
*next
= entry
->Next
;
106 _glthread_DESTROY_MUTEX(table
->Mutex
);
113 * Lookup an entry in the hash table.
115 * \param table the hash table.
116 * \param key the key.
118 * \return pointer to user's data or NULL if key not in table
120 * Walks through the hash entry until finding the matching key.
122 void *_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 into the hash table.
146 * If an entry with this key already exists we'll replace the existing entry.
148 * \param table the hash table.
149 * \param key the key (not zero).
150 * \param data pointer to user data.
152 * While holding the hash table's lock, walk through the hash entry list replacing the data if a
153 * matching key is found, or inserts a new table entry otherwise.
155 void _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
157 /* search for existing entry with this key */
159 struct HashEntry
*entry
;
164 _glthread_LOCK_MUTEX(table
->Mutex
);
166 if (key
> table
->MaxKey
)
169 pos
= key
& (TABLE_SIZE
-1);
170 entry
= table
->Table
[pos
];
172 if (entry
->Key
== key
) {
173 /* replace entry's data */
175 _glthread_UNLOCK_MUTEX(table
->Mutex
);
181 /* alloc and insert new table entry */
182 entry
= MALLOC_STRUCT(HashEntry
);
185 entry
->Next
= table
->Table
[pos
];
186 table
->Table
[pos
] = entry
;
188 _glthread_UNLOCK_MUTEX(table
->Mutex
);
194 * Remove an entry from the hash table.
196 * \param table the hash table.
197 * \param key key of entry to remove.
199 * While holding the hash table's lock, searches the entry with the matching
200 * key and unlinks it.
202 void _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
205 struct HashEntry
*entry
, *prev
;
210 _glthread_LOCK_MUTEX(table
->Mutex
);
212 pos
= key
& (TABLE_SIZE
-1);
214 entry
= table
->Table
[pos
];
216 if (entry
->Key
== key
) {
219 prev
->Next
= entry
->Next
;
222 table
->Table
[pos
] = entry
->Next
;
225 _glthread_UNLOCK_MUTEX(table
->Mutex
);
232 _glthread_UNLOCK_MUTEX(table
->Mutex
);
238 * Get the key of the "first" entry in the hash table.
240 * This is used in the course of deleting all display lists when
241 * a context is destroyed.
243 * \param table the hash table
245 * \return key for the "first" entry in the hash table.
247 * While holding the lock, walks through all table positions until finding
248 * the first entry of the first non-empty one.
250 GLuint
_mesa_HashFirstEntry(struct _mesa_HashTable
*table
)
254 _glthread_LOCK_MUTEX(table
->Mutex
);
255 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
256 if (table
->Table
[pos
]) {
257 _glthread_UNLOCK_MUTEX(table
->Mutex
);
258 return table
->Table
[pos
]->Key
;
261 _glthread_UNLOCK_MUTEX(table
->Mutex
);
268 * Dump contents of hash table for debugging.
270 * \param table the hash table.
272 void _mesa_HashPrint(const struct _mesa_HashTable
*table
)
276 for (i
=0;i
<TABLE_SIZE
;i
++) {
277 const struct HashEntry
*entry
= table
->Table
[i
];
279 _mesa_debug(NULL
, "%u %p\n", entry
->Key
, entry
->Data
);
288 * Find a block of adjacent unused hash keys.
290 * \param table the hash table.
291 * \param numKeys number of keys needed.
293 * \return Starting key of free block or 0 if failure.
295 * If there are enough free keys between the maximum key existing in the table
296 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
297 * the adjacent key. Otherwise do a full search for a free key block in the
298 * allowable key range.
300 GLuint
_mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
302 GLuint maxKey
= ~((GLuint
) 0);
303 _glthread_LOCK_MUTEX(table
->Mutex
);
304 if (maxKey
- numKeys
> table
->MaxKey
) {
305 /* the quick solution */
306 _glthread_UNLOCK_MUTEX(table
->Mutex
);
307 return table
->MaxKey
+ 1;
310 /* the slow solution */
311 GLuint freeCount
= 0;
312 GLuint freeStart
= 1;
314 for (key
=1; key
!=maxKey
; key
++) {
315 if (_mesa_HashLookup(table
, key
)) {
316 /* darn, this key is already in use */
321 /* this key not in use, check if we've found enough */
323 if (freeCount
== numKeys
) {
324 _glthread_UNLOCK_MUTEX(table
->Mutex
);
329 /* cannot allocate a block of numKeys consecutive keys */
330 _glthread_UNLOCK_MUTEX(table
->Mutex
);
337 #ifdef HASH_TEST_HARNESS
338 int main(int argc
, char *argv
[])
343 _mesa_printf("&a = %p\n", &a
);
344 _mesa_printf("&b = %p\n", &b
);
346 t
= _mesa_NewHashTable();
347 _mesa_HashInsert(t
, 501, &a
);
348 _mesa_HashInsert(t
, 10, &c
);
349 _mesa_HashInsert(t
, 0xfffffff8, &b
);
352 _mesa_printf("Find 501: %p\n", _mesa_HashLookup(t
,501));
353 _mesa_printf("Find 1313: %p\n", _mesa_HashLookup(t
,1313));
354 _mesa_printf("Find block of 100: %d\n", _mesa_HashFindFreeKeyBlock(t
, 100));
356 _mesa_DeleteHashTable(t
);