VirtualBox

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

最後變更 在這個檔案從53038是 50100,由 vboxsync 提交於 11 年 前

crOpenGL: bugfixes

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 16.4 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 CRHashNode *node = (CRHashNode *) crCalloc( sizeof( CRHashNode ) );
491#ifdef CHROMIUM_THREADSAFE
492 crLockMutex(&h->mutex);
493#endif
494 node->key = key;
495 node->data = data;
496 node->next = h->buckets[crHash( key )];
497 h->buckets[ crHash( key ) ] = node;
498 h->num_elements++;
499 crHashIdPoolAllocId (h->idPool, key);
500#ifdef CHROMIUM_THREADSAFE
501 crUnlockMutex(&h->mutex);
502#endif
503}
504
505GLboolean crHashtableAllocRegisterKey( CRHashTable *h, GLuint key)
506{
507 GLboolean fAllocated;
508#ifdef CHROMIUM_THREADSAFE
509 crLockMutex(&h->mutex);
510#endif
511 fAllocated = crHashIdPoolAllocId (h->idPool, key);
512#ifdef CHROMIUM_THREADSAFE
513 crUnlockMutex(&h->mutex);
514#endif
515 return fAllocated;
516}
517
518void crHashtableWalkKeys( CRHashTable *h, CRHashIdWalkKeys walkFunc , void *data)
519{
520#ifdef CHROMIUM_THREADSAFE
521 crLockMutex(&h->mutex);
522#endif
523 crHashIdWalkKeys(h->idPool, walkFunc , data);
524#ifdef CHROMIUM_THREADSAFE
525 crUnlockMutex(&h->mutex);
526#endif
527}
528
529GLuint crHashtableAllocKeys( CRHashTable *h, GLsizei range)
530{
531 GLuint res;
532 int i;
533
534#ifdef CHROMIUM_THREADSAFE
535 crLockMutex(&h->mutex);
536#endif
537 res = crHashIdPoolAllocBlock (h->idPool, range);
538#ifdef DEBUG_misha
539 Assert(res);
540 for (i = 0; i < range; ++i)
541 {
542 void *search = crHashtableSearch( h, res+i );
543 Assert(!search);
544 }
545#endif
546#ifdef CHROMIUM_THREADSAFE
547 crUnlockMutex(&h->mutex);
548#endif
549 return res;
550}
551
552void crHashtableDelete( CRHashTable *h, unsigned long key, CRHashtableCallback deleteFunc )
553{
554 unsigned int index = crHash( key );
555 CRHashNode *temp, *beftemp = NULL;
556
557#ifdef CHROMIUM_THREADSAFE
558 crLockMutex(&h->mutex);
559#endif
560 for ( temp = h->buckets[index]; temp; temp = temp->next )
561 {
562 if ( temp->key == key )
563 break;
564 beftemp = temp;
565 }
566 if ( temp )
567 {
568 if ( beftemp )
569 beftemp->next = temp->next;
570 else
571 h->buckets[index] = temp->next;
572 h->num_elements--;
573 if (temp->data && deleteFunc) {
574 (*deleteFunc)( temp->data );
575 }
576
577 crFree( temp );
578 }
579
580 crHashIdPoolFreeBlock( h->idPool, key, 1 );
581#ifdef CHROMIUM_THREADSAFE
582 crUnlockMutex(&h->mutex);
583#endif
584}
585
586void crHashtableDeleteBlock( CRHashTable *h, unsigned long key, GLsizei range, CRHashtableCallback deleteFunc )
587{
588 /* XXX optimize someday */
589 GLuint i;
590 for (i = 0; i < (GLuint)range; i++) {
591 crHashtableDelete( h, key, deleteFunc );
592 }
593}
594
595void *crHashtableSearch( const CRHashTable *h, unsigned long key )
596{
597 unsigned int index = crHash( key );
598 CRHashNode *temp;
599#ifdef CHROMIUM_THREADSAFE
600 crLockMutex((CRmutex *)&h->mutex);
601#endif
602 for ( temp = h->buckets[index]; temp; temp = temp->next )
603 {
604 if ( temp->key == key )
605 break;
606 }
607#ifdef CHROMIUM_THREADSAFE
608 crUnlockMutex((CRmutex *)&h->mutex);
609#endif
610 if ( !temp )
611 {
612 return NULL;
613 }
614 return temp->data;
615}
616
617void crHashtableReplace( CRHashTable *h, unsigned long key, void *data,
618 CRHashtableCallback deleteFunc)
619{
620 unsigned int index = crHash( key );
621 CRHashNode *temp;
622#ifdef CHROMIUM_THREADSAFE
623 crLockMutex(&h->mutex);
624#endif
625 for ( temp = h->buckets[index]; temp; temp = temp->next )
626 {
627 if ( temp->key == key )
628 break;
629 }
630#ifdef CHROMIUM_THREADSAFE
631 crUnlockMutex(&h->mutex);
632#endif
633 if ( !temp )
634 {
635 crHashtableAdd( h, key, data );
636 return;
637 }
638#ifdef CHROMIUM_THREADSAFE
639 crLockMutex(&h->mutex);
640#endif
641 if ( temp->data && deleteFunc )
642 {
643 (*deleteFunc)( temp->data );
644 }
645 temp->data = data;
646#ifdef CHROMIUM_THREADSAFE
647 crUnlockMutex(&h->mutex);
648#endif
649}
650
651unsigned int crHashtableNumElements( const CRHashTable *h)
652{
653 if (h)
654 return h->num_elements;
655 else
656 return 0;
657}
658
659/*
660 * Determine if the given key is used. Return GL_TRUE if so.
661 */
662GLboolean crHashtableIsKeyUsed( const CRHashTable *h, GLuint id )
663{
664 return (GLboolean) !crHashIdPoolIsIdFree( h->idPool, id);
665}
666
667GLboolean crHashtableGetDataKey(CRHashTable *pHash, void *pData, unsigned long *pKey)
668{
669 int i;
670 CRHashNode *entry;
671 GLboolean rc = GL_FALSE;
672
673 if (!pHash)
674 return rc;
675
676#ifdef CHROMIUM_THREADSAFE
677 crLockMutex(&pHash->mutex);
678#endif
679 for (i = 0; i<CR_NUM_BUCKETS && !rc; i++)
680 {
681 entry = pHash->buckets[i];
682 while (entry)
683 {
684 if (entry->data == pData) {
685 if (pKey)
686 *pKey = entry->key;
687 rc = GL_TRUE;
688 break;
689 }
690 entry = entry->next;
691 }
692 }
693#ifdef CHROMIUM_THREADSAFE
694 crUnlockMutex(&pHash->mutex);
695#endif
696
697 return rc;
698}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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