i965: Unindent the body of intelDestroyContext.
[mesa.git] / src / mesa / main / hash.c
1 /**
2 * \file hash.c
3 * Generic hash table.
4 *
5 * Used for display lists, texture objects, vertex/fragment programs,
6 * buffer objects, etc. The hash functions are thread-safe.
7 *
8 * \note key=0 is illegal.
9 *
10 * \author Brian Paul
11 */
12
13 /*
14 * Mesa 3-D graphics library
15 *
16 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included
26 * in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 */
36
37 #include "glheader.h"
38 #include "imports.h"
39 #include "glapi/glthread.h"
40 #include "hash.h"
41 #include "hash_table.h"
42
43 /**
44 * Magic GLuint object name that gets stored outside of the struct hash_table.
45 *
46 * The hash table needs a particular pointer to be the marker for a key that
47 * was deleted from the table, along with NULL for the "never allocated in the
48 * table" marker. Legacy GL allows any GLuint to be used as a GL object name,
49 * and we use a 1:1 mapping from GLuints to key pointers, so we need to be
50 * able to track a GLuint that happens to match the deleted key outside of
51 * struct hash_table. We tell the hash table to use "1" as the deleted key
52 * value, so that we test the deleted-key-in-the-table path as best we can.
53 */
54 #define DELETED_KEY_VALUE 1
55
56 /**
57 * The hash table data structure.
58 */
59 struct _mesa_HashTable {
60 struct hash_table *ht;
61 GLuint MaxKey; /**< highest key inserted so far */
62 _glthread_Mutex Mutex; /**< mutual exclusion lock */
63 _glthread_Mutex WalkMutex; /**< for _mesa_HashWalk() */
64 GLboolean InDeleteAll; /**< Debug check */
65 /** Value that would be in the table for DELETED_KEY_VALUE. */
66 void *deleted_key_data;
67 };
68
69 /** @{
70 * Mapping from our use of GLuint as both the key and the hash value to the
71 * hash_table.h API
72 *
73 * There exist many integer hash functions, designed to avoid collisions when
74 * the integers are spread across key space with some patterns. In GL, the
75 * pattern (in the case of glGen*()ed object IDs) is that the keys are unique
76 * contiguous integers starting from 1. Because of that, we just use the key
77 * as the hash value, to minimize the cost of the hash function. If objects
78 * are never deleted, we will never see a collision in the table, because the
79 * table resizes itself when it approaches full, and thus key % table_size ==
80 * key.
81 *
82 * The case where we could have collisions for genned objects would be
83 * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50);
84 * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at
85 * the end of that sequence, instead of 1-150. So far it doesn't appear to be
86 * a problem.
87 */
88 static bool
89 uint_key_compare(const void *a, const void *b)
90 {
91 return a == b;
92 }
93
94 static uint32_t
95 uint_hash(GLuint id)
96 {
97 return id;
98 }
99
100 static void *
101 uint_key(GLuint id)
102 {
103 return (void *)(uintptr_t) id;
104 }
105 /** @} */
106
107 /**
108 * Create a new hash table.
109 *
110 * \return pointer to a new, empty hash table.
111 */
112 struct _mesa_HashTable *
113 _mesa_NewHashTable(void)
114 {
115 struct _mesa_HashTable *table = CALLOC_STRUCT(_mesa_HashTable);
116
117 if (table) {
118 table->ht = _mesa_hash_table_create(NULL, uint_key_compare);
119 _mesa_hash_table_set_deleted_key(table->ht, uint_key(DELETED_KEY_VALUE));
120 _glthread_INIT_MUTEX(table->Mutex);
121 _glthread_INIT_MUTEX(table->WalkMutex);
122 }
123 return table;
124 }
125
126
127
128 /**
129 * Delete a hash table.
130 * Frees each entry on the hash table and then the hash table structure itself.
131 * Note that the caller should have already traversed the table and deleted
132 * the objects in the table (i.e. We don't free the entries' data pointer).
133 *
134 * \param table the hash table to delete.
135 */
136 void
137 _mesa_DeleteHashTable(struct _mesa_HashTable *table)
138 {
139 assert(table);
140
141 if (_mesa_hash_table_next_entry(table->ht, NULL) != NULL) {
142 _mesa_problem(NULL, "In _mesa_DeleteHashTable, found non-freed data");
143 }
144
145 _mesa_hash_table_destroy(table->ht, NULL);
146
147 _glthread_DESTROY_MUTEX(table->Mutex);
148 _glthread_DESTROY_MUTEX(table->WalkMutex);
149 free(table);
150 }
151
152
153
154 /**
155 * Lookup an entry in the hash table, without locking.
156 * \sa _mesa_HashLookup
157 */
158 static inline void *
159 _mesa_HashLookup_unlocked(struct _mesa_HashTable *table, GLuint key)
160 {
161 const struct hash_entry *entry;
162
163 assert(table);
164 assert(key);
165
166 if (key == DELETED_KEY_VALUE)
167 return table->deleted_key_data;
168
169 entry = _mesa_hash_table_search(table->ht, uint_hash(key), uint_key(key));
170 if (!entry)
171 return NULL;
172
173 return entry->data;
174 }
175
176
177 /**
178 * Lookup an entry in the hash table.
179 *
180 * \param table the hash table.
181 * \param key the key.
182 *
183 * \return pointer to user's data or NULL if key not in table
184 */
185 void *
186 _mesa_HashLookup(struct _mesa_HashTable *table, GLuint key)
187 {
188 void *res;
189 assert(table);
190 _glthread_LOCK_MUTEX(table->Mutex);
191 res = _mesa_HashLookup_unlocked(table, key);
192 _glthread_UNLOCK_MUTEX(table->Mutex);
193 return res;
194 }
195
196
197 /**
198 * Insert a key/pointer pair into the hash table.
199 * If an entry with this key already exists we'll replace the existing entry.
200 *
201 * \param table the hash table.
202 * \param key the key (not zero).
203 * \param data pointer to user data.
204 */
205 void
206 _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
207 {
208 uint32_t hash = uint_hash(key);
209 struct hash_entry *entry;
210
211 assert(table);
212 assert(key);
213
214 _glthread_LOCK_MUTEX(table->Mutex);
215
216 if (key > table->MaxKey)
217 table->MaxKey = key;
218
219 if (key == DELETED_KEY_VALUE) {
220 table->deleted_key_data = data;
221 } else {
222 entry = _mesa_hash_table_search(table->ht, hash, uint_key(key));
223 if (entry) {
224 entry->data = data;
225 } else {
226 _mesa_hash_table_insert(table->ht, hash, uint_key(key), data);
227 }
228 }
229
230 _glthread_UNLOCK_MUTEX(table->Mutex);
231 }
232
233
234
235 /**
236 * Remove an entry from the hash table.
237 *
238 * \param table the hash table.
239 * \param key key of entry to remove.
240 *
241 * While holding the hash table's lock, searches the entry with the matching
242 * key and unlinks it.
243 */
244 void
245 _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
246 {
247 struct hash_entry *entry;
248
249 assert(table);
250 assert(key);
251
252 /* have to check this outside of mutex lock */
253 if (table->InDeleteAll) {
254 _mesa_problem(NULL, "_mesa_HashRemove illegally called from "
255 "_mesa_HashDeleteAll callback function");
256 return;
257 }
258
259 _glthread_LOCK_MUTEX(table->Mutex);
260 if (key == DELETED_KEY_VALUE) {
261 table->deleted_key_data = NULL;
262 } else {
263 entry = _mesa_hash_table_search(table->ht, uint_hash(key), uint_key(key));
264 _mesa_hash_table_remove(table->ht, entry);
265 }
266 _glthread_UNLOCK_MUTEX(table->Mutex);
267 }
268
269
270
271 /**
272 * Delete all entries in a hash table, but don't delete the table itself.
273 * Invoke the given callback function for each table entry.
274 *
275 * \param table the hash table to delete
276 * \param callback the callback function
277 * \param userData arbitrary pointer to pass along to the callback
278 * (this is typically a struct gl_context pointer)
279 */
280 void
281 _mesa_HashDeleteAll(struct _mesa_HashTable *table,
282 void (*callback)(GLuint key, void *data, void *userData),
283 void *userData)
284 {
285 struct hash_entry *entry;
286
287 ASSERT(table);
288 ASSERT(callback);
289 _glthread_LOCK_MUTEX(table->Mutex);
290 table->InDeleteAll = GL_TRUE;
291 hash_table_foreach(table->ht, entry) {
292 callback((uintptr_t)entry->key, entry->data, userData);
293 _mesa_hash_table_remove(table->ht, entry);
294 }
295 if (table->deleted_key_data) {
296 callback(DELETED_KEY_VALUE, table->deleted_key_data, userData);
297 table->deleted_key_data = NULL;
298 }
299 table->InDeleteAll = GL_FALSE;
300 _glthread_UNLOCK_MUTEX(table->Mutex);
301 }
302
303
304 /**
305 * Clone all entries in a hash table, into a new table.
306 *
307 * \param table the hash table to clone
308 */
309 struct _mesa_HashTable *
310 _mesa_HashClone(const struct _mesa_HashTable *table)
311 {
312 /* cast-away const */
313 struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table;
314 struct hash_entry *entry;
315 struct _mesa_HashTable *clonetable;
316
317 ASSERT(table);
318 _glthread_LOCK_MUTEX(table2->Mutex);
319
320 clonetable = _mesa_NewHashTable();
321 assert(clonetable);
322 hash_table_foreach(table->ht, entry) {
323 _mesa_HashInsert(clonetable, (GLint)(uintptr_t)entry->key, entry->data);
324 }
325
326 _glthread_UNLOCK_MUTEX(table2->Mutex);
327
328 return clonetable;
329 }
330
331
332 /**
333 * Walk over all entries in a hash table, calling callback function for each.
334 * Note: we use a separate mutex in this function to avoid a recursive
335 * locking deadlock (in case the callback calls _mesa_HashRemove()) and to
336 * prevent multiple threads/contexts from getting tangled up.
337 * A lock-less version of this function could be used when the table will
338 * not be modified.
339 * \param table the hash table to walk
340 * \param callback the callback function
341 * \param userData arbitrary pointer to pass along to the callback
342 * (this is typically a struct gl_context pointer)
343 */
344 void
345 _mesa_HashWalk(const struct _mesa_HashTable *table,
346 void (*callback)(GLuint key, void *data, void *userData),
347 void *userData)
348 {
349 /* cast-away const */
350 struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table;
351 struct hash_entry *entry;
352
353 ASSERT(table);
354 ASSERT(callback);
355 _glthread_LOCK_MUTEX(table2->WalkMutex);
356 hash_table_foreach(table->ht, entry) {
357 callback((uintptr_t)entry->key, entry->data, userData);
358 }
359 if (table->deleted_key_data)
360 callback(DELETED_KEY_VALUE, table->deleted_key_data, userData);
361 _glthread_UNLOCK_MUTEX(table2->WalkMutex);
362 }
363
364 static void
365 debug_print_entry(GLuint key, void *data, void *userData)
366 {
367 _mesa_debug(NULL, "%u %p\n", key, data);
368 }
369
370 /**
371 * Dump contents of hash table for debugging.
372 *
373 * \param table the hash table.
374 */
375 void
376 _mesa_HashPrint(const struct _mesa_HashTable *table)
377 {
378 if (table->deleted_key_data)
379 debug_print_entry(DELETED_KEY_VALUE, table->deleted_key_data, NULL);
380 _mesa_HashWalk(table, debug_print_entry, NULL);
381 }
382
383
384 /**
385 * Find a block of adjacent unused hash keys.
386 *
387 * \param table the hash table.
388 * \param numKeys number of keys needed.
389 *
390 * \return Starting key of free block or 0 if failure.
391 *
392 * If there are enough free keys between the maximum key existing in the table
393 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
394 * the adjacent key. Otherwise do a full search for a free key block in the
395 * allowable key range.
396 */
397 GLuint
398 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
399 {
400 const GLuint maxKey = ~((GLuint) 0) - 1;
401 _glthread_LOCK_MUTEX(table->Mutex);
402 if (maxKey - numKeys > table->MaxKey) {
403 /* the quick solution */
404 _glthread_UNLOCK_MUTEX(table->Mutex);
405 return table->MaxKey + 1;
406 }
407 else {
408 /* the slow solution */
409 GLuint freeCount = 0;
410 GLuint freeStart = 1;
411 GLuint key;
412 for (key = 1; key != maxKey; key++) {
413 if (_mesa_HashLookup_unlocked(table, key)) {
414 /* darn, this key is already in use */
415 freeCount = 0;
416 freeStart = key+1;
417 }
418 else {
419 /* this key not in use, check if we've found enough */
420 freeCount++;
421 if (freeCount == numKeys) {
422 _glthread_UNLOCK_MUTEX(table->Mutex);
423 return freeStart;
424 }
425 }
426 }
427 /* cannot allocate a block of numKeys consecutive keys */
428 _glthread_UNLOCK_MUTEX(table->Mutex);
429 return 0;
430 }
431 }
432
433
434 /**
435 * Return the number of entries in the hash table.
436 */
437 GLuint
438 _mesa_HashNumEntries(const struct _mesa_HashTable *table)
439 {
440 struct hash_entry *entry;
441 GLuint count = 0;
442
443 if (table->deleted_key_data)
444 count++;
445
446 hash_table_foreach(table->ht, entry)
447 count++;
448
449 return count;
450 }