1 /* $Id: hash.c,v 1.14 2002/10/24 23:57:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 * \brief Generic hash table. Used for display lists and texture objects.
37 * The hash functions are thread-safe.
39 * \note key=0 is illegal
43 #define TABLE_SIZE 1023 /**< Size of lookup table/array */
46 * An entry in the hash table. This struct is private to this file.
49 GLuint Key
; /**< the entry's key */
50 void *Data
; /**< the entry's data */
51 struct HashEntry
*Next
; /**< pointer to next entry */
55 * The hashtable data structure. This is an opaque types (it's not
56 * defined in the .h file).
58 struct _mesa_HashTable
{
59 struct HashEntry
*Table
[TABLE_SIZE
]; /**< the lookup table */
60 GLuint MaxKey
; /**< highest key inserted so far */
61 _glthread_Mutex Mutex
; /**< mutual exclusion lock */
67 * Create a new hash table.
68 * \return pointer to a new, empty hash table.
70 struct _mesa_HashTable
*_mesa_NewHashTable(void)
72 struct _mesa_HashTable
*table
= CALLOC_STRUCT(_mesa_HashTable
);
74 _glthread_INIT_MUTEX(table
->Mutex
);
82 * Delete a hash table.
83 * \param table - the hash table to delete
85 void _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
89 for (i
=0;i
<TABLE_SIZE
;i
++) {
90 struct HashEntry
*entry
= table
->Table
[i
];
92 struct HashEntry
*next
= entry
->Next
;
103 * Lookup an entry in the hash table.
104 * \param table - the hash table
105 * \param key - the key
106 * \return pointer to user's data or NULL if key not in table
108 void *_mesa_HashLookup(const struct _mesa_HashTable
*table
, GLuint key
)
111 const struct HashEntry
*entry
;
116 pos
= key
& (TABLE_SIZE
-1);
117 entry
= table
->Table
[pos
];
119 if (entry
->Key
== key
) {
130 * Insert into the hash table. If an entry with this key already exists
131 * we'll replace the existing entry.
132 * \param table - the hash table
133 * \param key - the key (not zero)
134 * \param data - pointer to user data
136 void _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
138 /* search for existing entry with this key */
140 struct HashEntry
*entry
;
145 _glthread_LOCK_MUTEX(table
->Mutex
);
147 if (key
> table
->MaxKey
)
150 pos
= key
& (TABLE_SIZE
-1);
151 entry
= table
->Table
[pos
];
153 if (entry
->Key
== key
) {
154 /* replace entry's data */
156 _glthread_UNLOCK_MUTEX(table
->Mutex
);
162 /* alloc and insert new table entry */
163 entry
= MALLOC_STRUCT(HashEntry
);
166 entry
->Next
= table
->Table
[pos
];
167 table
->Table
[pos
] = entry
;
169 _glthread_UNLOCK_MUTEX(table
->Mutex
);
175 * Remove an entry from the hash table.
176 * \param table - the hash table
177 * \param key - key of entry to remove
179 void _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
182 struct HashEntry
*entry
, *prev
;
187 _glthread_LOCK_MUTEX(table
->Mutex
);
189 pos
= key
& (TABLE_SIZE
-1);
191 entry
= table
->Table
[pos
];
193 if (entry
->Key
== key
) {
196 prev
->Next
= entry
->Next
;
199 table
->Table
[pos
] = entry
->Next
;
202 _glthread_UNLOCK_MUTEX(table
->Mutex
);
209 _glthread_UNLOCK_MUTEX(table
->Mutex
);
215 * Get the key of the "first" entry in the hash table.
216 * This is used in the course of deleting all display lists when
217 * a context is destroyed.
218 * \param table - the hash table
219 * \return key for the "first" entry in the hash table.
221 GLuint
_mesa_HashFirstEntry(struct _mesa_HashTable
*table
)
225 _glthread_LOCK_MUTEX(table
->Mutex
);
226 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
227 if (table
->Table
[pos
]) {
228 _glthread_UNLOCK_MUTEX(table
->Mutex
);
229 return table
->Table
[pos
]->Key
;
232 _glthread_UNLOCK_MUTEX(table
->Mutex
);
239 * Dump contents of hash table for debugging.
240 * \param table - the hash table
242 void _mesa_HashPrint(const struct _mesa_HashTable
*table
)
246 for (i
=0;i
<TABLE_SIZE
;i
++) {
247 const struct HashEntry
*entry
= table
->Table
[i
];
249 _mesa_debug(NULL
, "%u %p\n", entry
->Key
, entry
->Data
);
258 * Find a block of 'numKeys' adjacent unused hash keys.
259 * \param table - the hash table
260 * \param numKeys - number of keys needed
261 * \return Starting key of free block or 0 if failure
263 GLuint
_mesa_HashFindFreeKeyBlock(struct _mesa_HashTable
*table
, GLuint numKeys
)
265 GLuint maxKey
= ~((GLuint
) 0);
266 _glthread_LOCK_MUTEX(table
->Mutex
);
267 if (maxKey
- numKeys
> table
->MaxKey
) {
268 /* the quick solution */
269 _glthread_UNLOCK_MUTEX(table
->Mutex
);
270 return table
->MaxKey
+ 1;
273 /* the slow solution */
274 GLuint freeCount
= 0;
275 GLuint freeStart
= 1;
277 for (key
=1; key
!=maxKey
; key
++) {
278 if (_mesa_HashLookup(table
, key
)) {
279 /* darn, this key is already in use */
284 /* this key not in use, check if we've found enough */
286 if (freeCount
== numKeys
) {
287 _glthread_UNLOCK_MUTEX(table
->Mutex
);
292 /* cannot allocate a block of numKeys consecutive keys */
293 _glthread_UNLOCK_MUTEX(table
->Mutex
);
300 #ifdef HASH_TEST_HARNESS
301 int main(int argc
, char *argv
[])
306 _mesa_printf("&a = %p\n", &a
);
307 _mesa_printf("&b = %p\n", &b
);
309 t
= _mesa_NewHashTable();
310 _mesa_HashInsert(t
, 501, &a
);
311 _mesa_HashInsert(t
, 10, &c
);
312 _mesa_HashInsert(t
, 0xfffffff8, &b
);
315 _mesa_printf("Find 501: %p\n", _mesa_HashLookup(t
,501));
316 _mesa_printf("Find 1313: %p\n", _mesa_HashLookup(t
,1313));
317 _mesa_printf("Find block of 100: %d\n", _mesa_HashFindFreeKeyBlock(t
, 100));
319 _mesa_DeleteHashTable(t
);