i965: Don't treat HW_REGs as barriers if they're immediates.
[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 "hash.h"
40 #include "hash_table.h"
41
42 /**
43 * Magic GLuint object name that gets stored outside of the struct hash_table.
44 *
45 * The hash table needs a particular pointer to be the marker for a key that
46 * was deleted from the table, along with NULL for the "never allocated in the
47 * table" marker. Legacy GL allows any GLuint to be used as a GL object name,
48 * and we use a 1:1 mapping from GLuints to key pointers, so we need to be
49 * able to track a GLuint that happens to match the deleted key outside of
50 * struct hash_table. We tell the hash table to use "1" as the deleted key
51 * value, so that we test the deleted-key-in-the-table path as best we can.
52 */
53 #define DELETED_KEY_VALUE 1
54
55 /**
56 * The hash table data structure.
57 */
58 struct _mesa_HashTable {
59 struct hash_table *ht;
60 GLuint MaxKey; /**< highest key inserted so far */
61 mtx_t Mutex; /**< mutual exclusion lock */
62 mtx_t WalkMutex; /**< for _mesa_HashWalk() */
63 GLboolean InDeleteAll; /**< Debug check */
64 /** Value that would be in the table for DELETED_KEY_VALUE. */
65 void *deleted_key_data;
66 };
67
68 /** @{
69 * Mapping from our use of GLuint as both the key and the hash value to the
70 * hash_table.h API
71 *
72 * There exist many integer hash functions, designed to avoid collisions when
73 * the integers are spread across key space with some patterns. In GL, the
74 * pattern (in the case of glGen*()ed object IDs) is that the keys are unique
75 * contiguous integers starting from 1. Because of that, we just use the key
76 * as the hash value, to minimize the cost of the hash function. If objects
77 * are never deleted, we will never see a collision in the table, because the
78 * table resizes itself when it approaches full, and thus key % table_size ==
79 * key.
80 *
81 * The case where we could have collisions for genned objects would be
82 * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50);
83 * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at
84 * the end of that sequence, instead of 1-150. So far it doesn't appear to be
85 * a problem.
86 */
87 static bool
88 uint_key_compare(const void *a, const void *b)
89 {
90 return a == b;
91 }
92
93 static uint32_t
94 uint_hash(GLuint id)
95 {
96 return id;
97 }
98
99 static void *
100 uint_key(GLuint id)
101 {
102 return (void *)(uintptr_t) id;
103 }
104 /** @} */
105
106 /**
107 * Create a new hash table.
108 *
109 * \return pointer to a new, empty hash table.
110 */
111 struct _mesa_HashTable *
112 _mesa_NewHashTable(void)
113 {
114 struct _mesa_HashTable *table = CALLOC_STRUCT(_mesa_HashTable);
115
116 if (table) {
117 table->ht = _mesa_hash_table_create(NULL, uint_key_compare);
118 _mesa_hash_table_set_deleted_key(table->ht, uint_key(DELETED_KEY_VALUE));
119 mtx_init(&table->Mutex, mtx_plain);
120 mtx_init(&table->WalkMutex, mtx_plain);
121 }
122 return table;
123 }
124
125
126
127 /**
128 * Delete a hash table.
129 * Frees each entry on the hash table and then the hash table structure itself.
130 * Note that the caller should have already traversed the table and deleted
131 * the objects in the table (i.e. We don't free the entries' data pointer).
132 *
133 * \param table the hash table to delete.
134 */
135 void
136 _mesa_DeleteHashTable(struct _mesa_HashTable *table)
137 {
138 assert(table);
139
140 if (_mesa_hash_table_next_entry(table->ht, NULL) != NULL) {
141 _mesa_problem(NULL, "In _mesa_DeleteHashTable, found non-freed data");
142 }
143
144 _mesa_hash_table_destroy(table->ht, NULL);
145
146 mtx_destroy(&table->Mutex);
147 mtx_destroy(&table->WalkMutex);
148 free(table);
149 }
150
151
152
153 /**
154 * Lookup an entry in the hash table, without locking.
155 * \sa _mesa_HashLookup
156 */
157 static inline void *
158 _mesa_HashLookup_unlocked(struct _mesa_HashTable *table, GLuint key)
159 {
160 const struct hash_entry *entry;
161
162 assert(table);
163 assert(key);
164
165 if (key == DELETED_KEY_VALUE)
166 return table->deleted_key_data;
167
168 entry = _mesa_hash_table_search(table->ht, uint_hash(key), uint_key(key));
169 if (!entry)
170 return NULL;
171
172 return entry->data;
173 }
174
175
176 /**
177 * Lookup an entry in the hash table.
178 *
179 * \param table the hash table.
180 * \param key the key.
181 *
182 * \return pointer to user's data or NULL if key not in table
183 */
184 void *
185 _mesa_HashLookup(struct _mesa_HashTable *table, GLuint key)
186 {
187 void *res;
188 assert(table);
189 mtx_lock(&table->Mutex);
190 res = _mesa_HashLookup_unlocked(table, key);
191 mtx_unlock(&table->Mutex);
192 return res;
193 }
194
195
196 /**
197 * Lookup an entry in the hash table without locking the mutex.
198 *
199 * The hash table mutex must be locked manually by calling
200 * _mesa_HashLockMutex() before calling this function.
201 *
202 * \param table the hash table.
203 * \param key the key.
204 *
205 * \return pointer to user's data or NULL if key not in table
206 */
207 void *
208 _mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key)
209 {
210 return _mesa_HashLookup_unlocked(table, key);
211 }
212
213
214 /**
215 * Lock the hash table mutex.
216 *
217 * This function should be used when multiple objects need
218 * to be looked up in the hash table, to avoid having to lock
219 * and unlock the mutex each time.
220 *
221 * \param table the hash table.
222 */
223 void
224 _mesa_HashLockMutex(struct _mesa_HashTable *table)
225 {
226 assert(table);
227 mtx_lock(&table->Mutex);
228 }
229
230
231 /**
232 * Unlock the hash table mutex.
233 *
234 * \param table the hash table.
235 */
236 void
237 _mesa_HashUnlockMutex(struct _mesa_HashTable *table)
238 {
239 assert(table);
240 mtx_unlock(&table->Mutex);
241 }
242
243
244 static inline void
245 _mesa_HashInsert_unlocked(struct _mesa_HashTable *table, GLuint key, void *data)
246 {
247 uint32_t hash = uint_hash(key);
248 struct hash_entry *entry;
249
250 assert(table);
251 assert(key);
252
253 if (key > table->MaxKey)
254 table->MaxKey = key;
255
256 if (key == DELETED_KEY_VALUE) {
257 table->deleted_key_data = data;
258 } else {
259 entry = _mesa_hash_table_search(table->ht, hash, uint_key(key));
260 if (entry) {
261 entry->data = data;
262 } else {
263 _mesa_hash_table_insert(table->ht, hash, uint_key(key), data);
264 }
265 }
266 }
267
268
269 /**
270 * Insert a key/pointer pair into the hash table without locking the mutex.
271 * If an entry with this key already exists we'll replace the existing entry.
272 *
273 * The hash table mutex must be locked manually by calling
274 * _mesa_HashLockMutex() before calling this function.
275 *
276 * \param table the hash table.
277 * \param key the key (not zero).
278 * \param data pointer to user data.
279 */
280 void
281 _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data)
282 {
283 _mesa_HashInsert_unlocked(table, key, data);
284 }
285
286
287 /**
288 * Insert a key/pointer pair into the hash table.
289 * If an entry with this key already exists we'll replace the existing entry.
290 *
291 * \param table the hash table.
292 * \param key the key (not zero).
293 * \param data pointer to user data.
294 */
295 void
296 _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
297 {
298 assert(table);
299 mtx_lock(&table->Mutex);
300 _mesa_HashInsert_unlocked(table, key, data);
301 mtx_unlock(&table->Mutex);
302 }
303
304
305 /**
306 * Remove an entry from the hash table.
307 *
308 * \param table the hash table.
309 * \param key key of entry to remove.
310 *
311 * While holding the hash table's lock, searches the entry with the matching
312 * key and unlinks it.
313 */
314 void
315 _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
316 {
317 struct hash_entry *entry;
318
319 assert(table);
320 assert(key);
321
322 /* have to check this outside of mutex lock */
323 if (table->InDeleteAll) {
324 _mesa_problem(NULL, "_mesa_HashRemove illegally called from "
325 "_mesa_HashDeleteAll callback function");
326 return;
327 }
328
329 mtx_lock(&table->Mutex);
330 if (key == DELETED_KEY_VALUE) {
331 table->deleted_key_data = NULL;
332 } else {
333 entry = _mesa_hash_table_search(table->ht, uint_hash(key), uint_key(key));
334 _mesa_hash_table_remove(table->ht, entry);
335 }
336 mtx_unlock(&table->Mutex);
337 }
338
339
340
341 /**
342 * Delete all entries in a hash table, but don't delete the table itself.
343 * Invoke the given callback function for each table entry.
344 *
345 * \param table the hash table to delete
346 * \param callback the callback function
347 * \param userData arbitrary pointer to pass along to the callback
348 * (this is typically a struct gl_context pointer)
349 */
350 void
351 _mesa_HashDeleteAll(struct _mesa_HashTable *table,
352 void (*callback)(GLuint key, void *data, void *userData),
353 void *userData)
354 {
355 struct hash_entry *entry;
356
357 ASSERT(table);
358 ASSERT(callback);
359 mtx_lock(&table->Mutex);
360 table->InDeleteAll = GL_TRUE;
361 hash_table_foreach(table->ht, entry) {
362 callback((uintptr_t)entry->key, entry->data, userData);
363 _mesa_hash_table_remove(table->ht, entry);
364 }
365 if (table->deleted_key_data) {
366 callback(DELETED_KEY_VALUE, table->deleted_key_data, userData);
367 table->deleted_key_data = NULL;
368 }
369 table->InDeleteAll = GL_FALSE;
370 mtx_unlock(&table->Mutex);
371 }
372
373
374 /**
375 * Clone all entries in a hash table, into a new table.
376 *
377 * \param table the hash table to clone
378 */
379 struct _mesa_HashTable *
380 _mesa_HashClone(const struct _mesa_HashTable *table)
381 {
382 /* cast-away const */
383 struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table;
384 struct hash_entry *entry;
385 struct _mesa_HashTable *clonetable;
386
387 ASSERT(table);
388 mtx_lock(&table2->Mutex);
389
390 clonetable = _mesa_NewHashTable();
391 assert(clonetable);
392 hash_table_foreach(table->ht, entry) {
393 _mesa_HashInsert(clonetable, (GLint)(uintptr_t)entry->key, entry->data);
394 }
395
396 mtx_unlock(&table2->Mutex);
397
398 return clonetable;
399 }
400
401
402 /**
403 * Walk over all entries in a hash table, calling callback function for each.
404 * Note: we use a separate mutex in this function to avoid a recursive
405 * locking deadlock (in case the callback calls _mesa_HashRemove()) and to
406 * prevent multiple threads/contexts from getting tangled up.
407 * A lock-less version of this function could be used when the table will
408 * not be modified.
409 * \param table the hash table to walk
410 * \param callback the callback function
411 * \param userData arbitrary pointer to pass along to the callback
412 * (this is typically a struct gl_context pointer)
413 */
414 void
415 _mesa_HashWalk(const struct _mesa_HashTable *table,
416 void (*callback)(GLuint key, void *data, void *userData),
417 void *userData)
418 {
419 /* cast-away const */
420 struct _mesa_HashTable *table2 = (struct _mesa_HashTable *) table;
421 struct hash_entry *entry;
422
423 ASSERT(table);
424 ASSERT(callback);
425 mtx_lock(&table2->WalkMutex);
426 hash_table_foreach(table->ht, entry) {
427 callback((uintptr_t)entry->key, entry->data, userData);
428 }
429 if (table->deleted_key_data)
430 callback(DELETED_KEY_VALUE, table->deleted_key_data, userData);
431 mtx_unlock(&table2->WalkMutex);
432 }
433
434 static void
435 debug_print_entry(GLuint key, void *data, void *userData)
436 {
437 _mesa_debug(NULL, "%u %p\n", key, data);
438 }
439
440 /**
441 * Dump contents of hash table for debugging.
442 *
443 * \param table the hash table.
444 */
445 void
446 _mesa_HashPrint(const struct _mesa_HashTable *table)
447 {
448 if (table->deleted_key_data)
449 debug_print_entry(DELETED_KEY_VALUE, table->deleted_key_data, NULL);
450 _mesa_HashWalk(table, debug_print_entry, NULL);
451 }
452
453
454 /**
455 * Find a block of adjacent unused hash keys.
456 *
457 * \param table the hash table.
458 * \param numKeys number of keys needed.
459 *
460 * \return Starting key of free block or 0 if failure.
461 *
462 * If there are enough free keys between the maximum key existing in the table
463 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
464 * the adjacent key. Otherwise do a full search for a free key block in the
465 * allowable key range.
466 */
467 GLuint
468 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
469 {
470 const GLuint maxKey = ~((GLuint) 0) - 1;
471 mtx_lock(&table->Mutex);
472 if (maxKey - numKeys > table->MaxKey) {
473 /* the quick solution */
474 mtx_unlock(&table->Mutex);
475 return table->MaxKey + 1;
476 }
477 else {
478 /* the slow solution */
479 GLuint freeCount = 0;
480 GLuint freeStart = 1;
481 GLuint key;
482 for (key = 1; key != maxKey; key++) {
483 if (_mesa_HashLookup_unlocked(table, key)) {
484 /* darn, this key is already in use */
485 freeCount = 0;
486 freeStart = key+1;
487 }
488 else {
489 /* this key not in use, check if we've found enough */
490 freeCount++;
491 if (freeCount == numKeys) {
492 mtx_unlock(&table->Mutex);
493 return freeStart;
494 }
495 }
496 }
497 /* cannot allocate a block of numKeys consecutive keys */
498 mtx_unlock(&table->Mutex);
499 return 0;
500 }
501 }
502
503
504 /**
505 * Return the number of entries in the hash table.
506 */
507 GLuint
508 _mesa_HashNumEntries(const struct _mesa_HashTable *table)
509 {
510 struct hash_entry *entry;
511 GLuint count = 0;
512
513 if (table->deleted_key_data)
514 count++;
515
516 hash_table_foreach(table->ht, entry)
517 count++;
518
519 return count;
520 }