VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/hash.c@ 55617

最後變更 在這個檔案從55617是 55617,由 vboxsync 提交於 10 年 前

3D: simplify hash tables debugging a bit.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.5 KB
 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_threads.h"
8#include "cr_hash.h"
9#include "cr_mem.h"
10#include "cr_error.h"
11
12#include <iprt/list.h>
13
14#define CR_MAXUINT ((GLuint) 0xFFFFFFFF)
15#define CR_HASH_ID_MIN ((GLuint)1)
16#define CR_HASH_ID_MAX CR_MAXUINT
17
18#define CR_NUM_BUCKETS 1047
19
20typedef struct FreeElemRec {
21 RTLISTNODE Node;
22 GLuint min;
23 GLuint max;
24} FreeElem;
25
26struct CRHashIdPool {
27 RTLISTNODE freeList;
28 GLuint min;
29 GLuint max;
30};
31
32typedef struct CRHashNode {
33 unsigned long key;
34 void *data;
35 struct CRHashNode *next;
36} CRHashNode;
37
38struct CRHashTable {
39 unsigned int num_elements;
40 CRHashNode *buckets[CR_NUM_BUCKETS];
41 CRHashIdPool *idPool;
42#ifdef CHROMIUM_THREADSAFE
43 CRmutex mutex;
44#endif
45};
46
47
48CRHashIdPool *crAllocHashIdPoolEx( GLuint min, GLuint max )
49{
50 CRHashIdPool *pool;
51 FreeElem *elem;
52 if (min < CR_HASH_ID_MIN || max > CR_HASH_ID_MAX || min >= max)
53 {
54 crWarning("invalid min man vals");
55 return NULL;
56 }
57 pool = (CRHashIdPool *) crCalloc(sizeof(CRHashIdPool));
58 elem = (FreeElem *) crCalloc(sizeof(FreeElem));
59 RTListInit(&pool->freeList);
60 elem->min = min;
61 elem->max = max;
62 RTListAppend(&pool->freeList, &elem->Node);
63 pool->min = min;
64 pool->max = max;
65 return pool;
66}
67
68CRHashIdPool *crAllocHashIdPool( void )
69{
70 return crAllocHashIdPoolEx( CR_HASH_ID_MIN, CR_HASH_ID_MAX );
71}
72
73void crFreeHashIdPool( CRHashIdPool *pool )
74{
75 FreeElem *i, *next;
76 RTListForEachSafe(&pool->freeList, i, next, FreeElem, Node)
77 {
78 crFree(i);
79 }
80
81 crFree(pool);
82}
83
84#ifdef DEBUG_misha
85static void crHashIdPoolDbgCheckConsistency(CRHashIdPool *pool)
86{
87 FreeElem *i;
88 GLuint min = 0;
89
90 /* null is a special case, it is always treated as allocated */
91 Assert(!crHashIdPoolIsIdFree(pool, 0));
92
93 /* first ensure entries have correct values */
94 RTListForEach(&pool->freeList, i, FreeElem, Node)
95 {
96 Assert(i->min >= pool->min);
97 Assert(i->max <= pool->max);
98 Assert(i->min < i->max);
99 }
100
101 /* now ensure entries do not intersect */
102 /* and that they are sorted */
103 RTListForEach(&pool->freeList, i, FreeElem, Node)
104 {
105 Assert(min < i->min);
106 min = i->max;
107 }
108}
109
110static void crHashIdPoolDbgCheckUsed( const CRHashIdPool *pool, GLuint start, GLuint count, GLboolean fUsed )
111{
112 GLuint i;
113 CRASSERT(count);
114 CRASSERT(start >= pool->min);
115 CRASSERT(start + count <= pool->max);
116 CRASSERT(start + count > start);
117 for (i = 0; i < count; ++i)
118 {
119 Assert(!fUsed == !!crHashIdPoolIsIdFree( pool, start + i ));
120 }
121}
122
123# define CR_HASH_IDPOOL_DBG_CHECK_USED(_p, _start, _count, _used) do { \
124 crHashIdPoolDbgCheckConsistency((_p)); \
125 crHashIdPoolDbgCheckUsed( (_p), (_start), (_count), (_used) ); \
126 } while (0)
127
128# define CR_HASH_IDPOOL_DBG_CHECK_CONSISTENCY(_p) do { crHashIdPoolDbgCheckConsistency((_p)); } while (0)
129#else
130# define CR_HASH_IDPOOL_DBG_CHECK_USED(_p, _start, _count, _used) do { } while (0)
131# define CR_HASH_IDPOOL_DBG_CHECK_CONSISTENCY(_p) do { } while (0)
132#endif
133
134/*
135 * Allocate a block of <count> IDs. Return index of first one.
136 * Return 0 if we fail.
137 */
138GLuint crHashIdPoolAllocBlock( CRHashIdPool *pool, GLuint count )
139{
140 FreeElem *f, *next;
141 GLuint ret;
142
143 CRASSERT(count > 0);
144 RTListForEachSafe(&pool->freeList, f, next, FreeElem, Node)
145 {
146 Assert(f->max > f->min);
147 if (f->max - f->min >= (GLuint) count)
148 {
149 /* found a sufficiently large enough block */
150 ret = f->min;
151 f->min += count;
152 if (f->min == f->max)
153 {
154 RTListNodeRemove(&f->Node);
155 crFree(f);
156 }
157
158 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, ret, count, GL_TRUE);
159 return ret;
160 }
161 }
162
163 /* failed to find free block */
164 crWarning("crHashIdPoolAllocBlock failed");
165 CR_HASH_IDPOOL_DBG_CHECK_CONSISTENCY(pool);
166 return 0;
167}
168
169
170/*
171 * Free a block of <count> IDs starting at <first>.
172 */
173void crHashIdPoolFreeBlock( CRHashIdPool *pool, GLuint first, GLuint count )
174{
175 FreeElem *f;
176 GLuint last;
177 GLuint newMax;
178 FreeElem *cur, *curNext;
179
180 /* null is a special case, it is always treated as allocated */
181 if (!first)
182 {
183 Assert(!crHashIdPoolIsIdFree(pool, 0));
184 ++first;
185 --count;
186 if (!count)
187 return;
188 }
189
190 last = first + count;
191 CRASSERT(count > 0);
192 CRASSERT(last > first);
193 CRASSERT(first >= pool->min);
194 CRASSERT(last <= pool->max);
195
196 /* the id list is sorted, first find a place to insert */
197 RTListForEach(&pool->freeList, f, FreeElem, Node)
198 {
199 Assert(f->max > f->min);
200
201 if (f->max < first)
202 continue;
203
204 if (f->min > last)
205 {
206 /* we are here because first is > than prevEntry->max
207 * otherwise the previous loop iterations should handle that */
208 FreeElem *elem = (FreeElem *) crCalloc(sizeof(FreeElem));
209 elem->min = first;
210 elem->max = last;
211 RTListNodeInsertBefore(&f->Node, &elem->Node);
212 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, first, count, GL_FALSE);
213 return;
214 }
215
216 /* now we have f->min <= last and f->max >= first,
217 * so we have either intersection */
218
219 if (f->min > first)
220 f->min = first; /* first is guaranteed not to touch any prev regions */
221
222 newMax = last;
223
224 if (f->max >= last)
225 {
226 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, first, count, GL_FALSE);
227 return;
228 }
229
230 for (cur = RTListNodeGetNext(&f->Node, FreeElem, Node),
231 curNext = RT_FROM_MEMBER(cur->Node.pNext, FreeElem, Node);
232 !RTListNodeIsDummy(&pool->freeList, cur, FreeElem, Node);
233 cur = curNext,
234 curNext = RT_FROM_MEMBER((cur)->Node.pNext, FreeElem, Node) )
235 {
236 if (cur->min > last)
237 break;
238
239 newMax = cur->max;
240 RTListNodeRemove(&cur->Node);
241 crFree(cur);
242
243 if (newMax >= last)
244 break;
245 }
246
247 f->max = newMax;
248 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, first, count, GL_FALSE);
249 return;
250 }
251
252 /* we are here because either the list is empty or because all list rande elements have smaller values */
253 f = (FreeElem *) crCalloc(sizeof(FreeElem));
254 f->min = first;
255 f->max = last;
256 RTListAppend(&pool->freeList, &f->Node);
257 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, first, count, GL_FALSE);
258}
259
260
261
262/*
263 * Mark the given Id as being allocated.
264 */
265GLboolean crHashIdPoolAllocId( CRHashIdPool *pool, GLuint id )
266{
267 FreeElem *f, *next;
268
269 if (!id)
270 {
271 /* null is a special case, it is always treated as allocated */
272 Assert(!crHashIdPoolIsIdFree(pool, 0));
273 return GL_FALSE;
274 }
275
276// Assert(id != 2);
277
278 RTListForEachSafe(&pool->freeList, f, next, FreeElem, Node)
279 {
280 if (f->max <= id)
281 continue;
282 if (f->min > id)
283 {
284 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
285 return GL_FALSE;
286 }
287
288 /* f->min <= id && f->max > id */
289 if (id > f->min)
290 {
291 if (id + 1 < f->max)
292 {
293 FreeElem *elem = (FreeElem *) crCalloc(sizeof(FreeElem));
294 elem->min = id + 1;
295 elem->max = f->max;
296 RTListNodeInsertAfter(&f->Node, &elem->Node);
297 }
298 f->max = id;
299
300 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
301 }
302 else
303 {
304 Assert(id == f->min);
305 if (id + 1 < f->max)
306 {
307 f->min = id + 1;
308 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
309 }
310 else
311 {
312 RTListNodeRemove(&f->Node);
313 crFree(f);
314 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
315 }
316 }
317
318 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
319 return GL_TRUE;
320 }
321
322 /* if we get here, the ID was already allocated - that's OK */
323 CR_HASH_IDPOOL_DBG_CHECK_USED(pool, id, 1, GL_TRUE);
324 return GL_FALSE;
325}
326
327
328/*
329 * Determine if the given id is free. Return GL_TRUE if so.
330 */
331GLboolean crHashIdPoolIsIdFree( const CRHashIdPool *pool, GLuint id )
332{
333 FreeElem *f;
334 CRASSERT(id <= pool->max);
335
336 RTListForEach(&pool->freeList, f, FreeElem, Node)
337 {
338 if (f->max <= id)
339 continue;
340 if (f->min > id)
341 return GL_FALSE;
342 return GL_TRUE;
343 }
344 return GL_FALSE;
345}
346
347void crHashIdWalkKeys( CRHashIdPool *pool, CRHashIdWalkKeys walkFunc , void *data)
348{
349 FreeElem *prev = NULL, *f;
350
351 RTListForEach(&pool->freeList, f, FreeElem, Node)
352 {
353 if (prev)
354 {
355 Assert(prev->max < f->min);
356 walkFunc(prev->max+1, f->min - prev->max, data);
357 }
358 else if (f->min > pool->min)
359 {
360 walkFunc(pool->min, f->min - pool->min, data);
361 }
362
363 prev = f;
364 }
365
366 Assert(prev->max <= pool->max);
367
368 if (prev->max < pool->max)
369 {
370 walkFunc(prev->max+1, pool->max - prev->max, data);
371 }
372}
373
374CRHashTable *crAllocHashtableEx( GLuint min, GLuint max )
375{
376 int i;
377 CRHashTable *hash = (CRHashTable *) crCalloc( sizeof( CRHashTable )) ;
378 hash->num_elements = 0;
379 for (i = 0 ; i < CR_NUM_BUCKETS ; i++)
380 {
381 hash->buckets[i] = NULL;
382 }
383 hash->idPool = crAllocHashIdPoolEx( min, max );
384#ifdef CHROMIUM_THREADSAFE
385 crInitMutex(&hash->mutex);
386#endif
387 return hash;
388}
389
390CRHashTable *crAllocHashtable( void )
391{
392 return crAllocHashtableEx(CR_HASH_ID_MIN, CR_HASH_ID_MAX);
393}
394
395void crFreeHashtable( CRHashTable *hash, CRHashtableCallback deleteFunc )
396{
397 int i;
398 CRHashNode *entry, *next;
399
400 if ( !hash) return;
401
402#ifdef CHROMIUM_THREADSAFE
403 crLockMutex(&hash->mutex);
404#endif
405
406 for ( i = 0; i < CR_NUM_BUCKETS; i++ )
407 {
408 entry = hash->buckets[i];
409 while (entry)
410 {
411 next = entry->next;
412 /* Clear the key in case crHashtableDelete() is called
413 * from this callback.
414 */
415 entry->key = 0;
416 if (deleteFunc && entry->data)
417 {
418 (*deleteFunc)(entry->data);
419 }
420 crFree(entry);
421 entry = next;
422
423 }
424 }
425 crFreeHashIdPool( hash->idPool );
426
427#ifdef CHROMIUM_THREADSAFE
428 crUnlockMutex(&hash->mutex);
429 crFreeMutex(&hash->mutex);
430#endif
431
432 crFree( hash );
433}
434
435void crHashtableLock(CRHashTable *h)
436{
437#ifdef CHROMIUM_THREADSAFE
438 crLockMutex(&h->mutex);
439#endif
440}
441
442void crHashtableUnlock(CRHashTable *h)
443{
444#ifdef CHROMIUM_THREADSAFE
445 crUnlockMutex(&h->mutex);
446#endif
447}
448
449void crHashtableWalkUnlocked( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
450{
451 int i;
452 CRHashNode *entry, *next;
453
454 for (i = 0; i < CR_NUM_BUCKETS; i++)
455 {
456 entry = hash->buckets[i];
457 while (entry)
458 {
459 /* save next ptr here, in case walkFunc deletes this entry */
460 next = entry->next;
461 if (entry->data && walkFunc) {
462 (*walkFunc)( entry->key, entry->data, dataPtr2 );
463 }
464 entry = next;
465 }
466 }
467}
468
469void crHashtableWalk( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
470{
471 if (!hash)
472 return;
473
474#ifdef CHROMIUM_THREADSAFE
475 crLockMutex(&hash->mutex);
476#endif
477 crHashtableWalkUnlocked(hash, walkFunc , dataPtr2);
478#ifdef CHROMIUM_THREADSAFE
479 crUnlockMutex(&hash->mutex);
480#endif
481}
482
483static unsigned int crHash( unsigned long key )
484{
485 return key % CR_NUM_BUCKETS;
486}
487
488void crHashtableAdd( CRHashTable *h, unsigned long key, void *data )
489{
490 unsigned int index = crHash(key);
491 CRHashNode *node = (CRHashNode *) crCalloc( sizeof( CRHashNode ) );
492#ifdef CHROMIUM_THREADSAFE
493 crLockMutex(&h->mutex);
494#endif
495 node->key = key;
496 node->data = data;
497 node->next = h->buckets[index];
498 h->buckets[index] = node;
499 h->num_elements++;
500 crHashIdPoolAllocId (h->idPool, key);
501#ifdef CHROMIUM_THREADSAFE
502 crUnlockMutex(&h->mutex);
503#endif
504}
505
506GLboolean crHashtableAllocRegisterKey( CRHashTable *h, GLuint key)
507{
508 GLboolean fAllocated;
509#ifdef CHROMIUM_THREADSAFE
510 crLockMutex(&h->mutex);
511#endif
512 fAllocated = crHashIdPoolAllocId (h->idPool, key);
513#ifdef CHROMIUM_THREADSAFE
514 crUnlockMutex(&h->mutex);
515#endif
516 return fAllocated;
517}
518
519void crHashtableWalkKeys( CRHashTable *h, CRHashIdWalkKeys walkFunc , void *data)
520{
521#ifdef CHROMIUM_THREADSAFE
522 crLockMutex(&h->mutex);
523#endif
524 crHashIdWalkKeys(h->idPool, walkFunc , data);
525#ifdef CHROMIUM_THREADSAFE
526 crUnlockMutex(&h->mutex);
527#endif
528}
529
530GLuint crHashtableAllocKeys( CRHashTable *h, GLsizei range)
531{
532 GLuint res;
533 int i;
534
535#ifdef CHROMIUM_THREADSAFE
536 crLockMutex(&h->mutex);
537#endif
538 res = crHashIdPoolAllocBlock (h->idPool, range);
539#ifdef DEBUG_misha
540 Assert(res);
541 for (i = 0; i < range; ++i)
542 {
543 void *search = crHashtableSearch( h, res+i );
544 Assert(!search);
545 }
546#endif
547#ifdef CHROMIUM_THREADSAFE
548 crUnlockMutex(&h->mutex);
549#endif
550 return res;
551}
552
553void crHashtableDelete( CRHashTable *h, unsigned long key, CRHashtableCallback deleteFunc )
554{
555 unsigned int index = crHash( key );
556 CRHashNode *temp, *beftemp = NULL;
557
558#ifdef CHROMIUM_THREADSAFE
559 crLockMutex(&h->mutex);
560#endif
561 for ( temp = h->buckets[index]; temp; temp = temp->next )
562 {
563 if ( temp->key == key )
564 break;
565 beftemp = temp;
566 }
567 if ( temp )
568 {
569 if ( beftemp )
570 beftemp->next = temp->next;
571 else
572 h->buckets[index] = temp->next;
573 h->num_elements--;
574 if (temp->data && deleteFunc) {
575 (*deleteFunc)( temp->data );
576 }
577
578 crFree( temp );
579 }
580
581 crHashIdPoolFreeBlock( h->idPool, key, 1 );
582#ifdef CHROMIUM_THREADSAFE
583 crUnlockMutex(&h->mutex);
584#endif
585}
586
587void crHashtableDeleteBlock( CRHashTable *h, unsigned long key, GLsizei range, CRHashtableCallback deleteFunc )
588{
589 /* XXX optimize someday */
590 GLuint i;
591 for (i = 0; i < (GLuint)range; i++) {
592 crHashtableDelete( h, key, deleteFunc );
593 }
594}
595
596void *crHashtableSearch( const CRHashTable *h, unsigned long key )
597{
598 unsigned int index = crHash( key );
599 CRHashNode *temp;
600#ifdef CHROMIUM_THREADSAFE
601 crLockMutex((CRmutex *)&h->mutex);
602#endif
603 for ( temp = h->buckets[index]; temp; temp = temp->next )
604 {
605 if ( temp->key == key )
606 break;
607 }
608#ifdef CHROMIUM_THREADSAFE
609 crUnlockMutex((CRmutex *)&h->mutex);
610#endif
611 if ( !temp )
612 {
613 return NULL;
614 }
615 return temp->data;
616}
617
618void crHashtableReplace( CRHashTable *h, unsigned long key, void *data,
619 CRHashtableCallback deleteFunc)
620{
621 unsigned int index = crHash( key );
622 CRHashNode *temp;
623#ifdef CHROMIUM_THREADSAFE
624 crLockMutex(&h->mutex);
625#endif
626 for ( temp = h->buckets[index]; temp; temp = temp->next )
627 {
628 if ( temp->key == key )
629 break;
630 }
631#ifdef CHROMIUM_THREADSAFE
632 crUnlockMutex(&h->mutex);
633#endif
634 if ( !temp )
635 {
636 crHashtableAdd( h, key, data );
637 return;
638 }
639#ifdef CHROMIUM_THREADSAFE
640 crLockMutex(&h->mutex);
641#endif
642 if ( temp->data && deleteFunc )
643 {
644 (*deleteFunc)( temp->data );
645 }
646 temp->data = data;
647#ifdef CHROMIUM_THREADSAFE
648 crUnlockMutex(&h->mutex);
649#endif
650}
651
652unsigned int crHashtableNumElements( const CRHashTable *h)
653{
654 if (h)
655 return h->num_elements;
656 else
657 return 0;
658}
659
660/*
661 * Determine if the given key is used. Return GL_TRUE if so.
662 */
663GLboolean crHashtableIsKeyUsed( const CRHashTable *h, GLuint id )
664{
665 return (GLboolean) !crHashIdPoolIsIdFree( h->idPool, id);
666}
667
668GLboolean crHashtableGetDataKey(CRHashTable *pHash, void *pData, unsigned long *pKey)
669{
670 int i;
671 CRHashNode *entry;
672 GLboolean rc = GL_FALSE;
673
674 if (!pHash)
675 return rc;
676
677#ifdef CHROMIUM_THREADSAFE
678 crLockMutex(&pHash->mutex);
679#endif
680 for (i = 0; i<CR_NUM_BUCKETS && !rc; i++)
681 {
682 entry = pHash->buckets[i];
683 while (entry)
684 {
685 if (entry->data == pData) {
686 if (pKey)
687 *pKey = entry->key;
688 rc = GL_TRUE;
689 break;
690 }
691 entry = entry->next;
692 }
693 }
694#ifdef CHROMIUM_THREADSAFE
695 crUnlockMutex(&pHash->mutex);
696#endif
697
698 return rc;
699}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette