1 /* $Id: hash.c,v 1.7 2000/01/31 23:11:39 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
41 * This is used to implement display list and texture object lookup.
42 * NOTE: key=0 is illegal.
46 #define TABLE_SIZE 1024
51 struct HashEntry
*Next
;
54 struct _mesa_HashTable
{
55 struct HashEntry
*Table
[TABLE_SIZE
];
57 _glthread_Mutex Mutex
;
63 * Return pointer to a new, empty hash table.
65 struct _mesa_HashTable
*_mesa_NewHashTable(void)
67 return CALLOC_STRUCT(_mesa_HashTable
);
73 * Delete a hash table.
75 void _mesa_DeleteHashTable(struct _mesa_HashTable
*table
)
79 for (i
=0;i
<TABLE_SIZE
;i
++) {
80 struct HashEntry
*entry
= table
->Table
[i
];
82 struct HashEntry
*next
= entry
->Next
;
93 * Lookup an entry in the hash table.
94 * Input: table - the hash table
96 * Return: user data pointer or NULL if key not in table
98 void *_mesa_HashLookup(const struct _mesa_HashTable
*table
, GLuint key
)
101 const struct HashEntry
*entry
;
106 pos
= key
& (TABLE_SIZE
-1);
107 entry
= table
->Table
[pos
];
109 if (entry
->Key
== key
) {
120 * Insert into the hash table. If an entry with this key already exists
121 * we'll replace the existing entry.
122 * Input: table - the hash table
123 * key - the key (not zero)
124 * data - pointer to user data
126 void _mesa_HashInsert(struct _mesa_HashTable
*table
, GLuint key
, void *data
)
128 /* search for existing entry with this key */
130 struct HashEntry
*entry
;
135 _glthread_LOCK_MUTEX(table
->Mutex
);
137 if (key
> table
->MaxKey
)
140 pos
= key
& (TABLE_SIZE
-1);
141 entry
= table
->Table
[pos
];
143 if (entry
->Key
== key
) {
144 /* replace entry's data */
146 _glthread_UNLOCK_MUTEX(table
->Mutex
);
152 /* alloc and insert new table entry */
153 entry
= MALLOC_STRUCT(HashEntry
);
156 entry
->Next
= table
->Table
[pos
];
157 table
->Table
[pos
] = entry
;
159 _glthread_UNLOCK_MUTEX(table
->Mutex
);
165 * Remove an entry from the hash table.
166 * Input: table - the hash table
167 * key - key of entry to remove
169 void _mesa_HashRemove(struct _mesa_HashTable
*table
, GLuint key
)
172 struct HashEntry
*entry
, *prev
;
177 _glthread_LOCK_MUTEX(table
->Mutex
);
179 pos
= key
& (TABLE_SIZE
-1);
181 entry
= table
->Table
[pos
];
183 if (entry
->Key
== key
) {
186 prev
->Next
= entry
->Next
;
189 table
->Table
[pos
] = entry
->Next
;
192 _glthread_UNLOCK_MUTEX(table
->Mutex
);
199 _glthread_UNLOCK_MUTEX(table
->Mutex
);
205 * Return the key of the "first" entry in the hash table.
206 * By calling this function until zero is returned we can get
207 * the keys of all entries in the table.
209 GLuint
_mesa_HashFirstEntry(const struct _mesa_HashTable
*table
)
213 for (pos
=0; pos
< TABLE_SIZE
; pos
++) {
214 if (table
->Table
[pos
])
215 return table
->Table
[pos
]->Key
;
223 * Dump contents of hash table for debugging.
225 void _mesa_HashPrint(const struct _mesa_HashTable
*table
)
229 for (i
=0;i
<TABLE_SIZE
;i
++) {
230 const struct HashEntry
*entry
= table
->Table
[i
];
232 printf("%u %p\n", entry
->Key
, entry
->Data
);
241 * Find a block of 'numKeys' adjacent unused hash keys.
242 * Input: table - the hash table
243 * numKeys - number of keys needed
244 * Return: starting key of free block or 0 if failure
246 GLuint
_mesa_HashFindFreeKeyBlock(const struct _mesa_HashTable
*table
, GLuint numKeys
)
248 GLuint maxKey
= ~((GLuint
) 0);
249 if (maxKey
- numKeys
> table
->MaxKey
) {
250 /* the quick solution */
251 return table
->MaxKey
+ 1;
254 /* the slow solution */
255 GLuint freeCount
= 0;
256 GLuint freeStart
= 1;
258 for (key
=1; key
!=maxKey
; key
++) {
259 if (_mesa_HashLookup(table
, key
)) {
260 /* darn, this key is already in use */
265 /* this key not in use, check if we've found enough */
267 if (freeCount
== numKeys
) {
272 /* cannot allocate a block of numKeys consecutive keys */
279 #ifdef HASH_TEST_HARNESS
280 int main(int argc
, char *argv
[])
285 printf("&a = %p\n", &a
);
286 printf("&b = %p\n", &b
);
288 t
= _mesa_NewHashTable();
289 _mesa_HashInsert(t
, 501, &a
);
290 _mesa_HashInsert(t
, 10, &c
);
291 _mesa_HashInsert(t
, 0xfffffff8, &b
);
293 printf("Find 501: %p\n", _mesa_HashLookup(t
,501));
294 printf("Find 1313: %p\n", _mesa_HashLookup(t
,1313));
295 printf("Find block of 100: %d\n", _mesa_HashFindFreeKeyBlock(t
, 100));
296 _mesa_DeleteHashTable(t
);