VirtualBox

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

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

crOpenGL: proper fix

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 15.6 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 guarantied 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
347CRHashTable *crAllocHashtableEx( GLuint min, GLuint max )
348{
349 int i;
350 CRHashTable *hash = (CRHashTable *) crCalloc( sizeof( CRHashTable )) ;
351 hash->num_elements = 0;
352 for (i = 0 ; i < CR_NUM_BUCKETS ; i++)
353 {
354 hash->buckets[i] = NULL;
355 }
356 hash->idPool = crAllocHashIdPoolEx( min, max );
357#ifdef CHROMIUM_THREADSAFE
358 crInitMutex(&hash->mutex);
359#endif
360 return hash;
361}
362
363CRHashTable *crAllocHashtable( void )
364{
365 return crAllocHashtableEx(CR_HASH_ID_MIN, CR_HASH_ID_MAX);
366}
367
368void crFreeHashtable( CRHashTable *hash, CRHashtableCallback deleteFunc )
369{
370 int i;
371 CRHashNode *entry, *next;
372
373 if ( !hash) return;
374
375#ifdef CHROMIUM_THREADSAFE
376 crLockMutex(&hash->mutex);
377#endif
378
379 for ( i = 0; i < CR_NUM_BUCKETS; i++ )
380 {
381 entry = hash->buckets[i];
382 while (entry)
383 {
384 next = entry->next;
385 /* Clear the key in case crHashtableDelete() is called
386 * from this callback.
387 */
388 entry->key = 0;
389 if (deleteFunc && entry->data)
390 {
391 (*deleteFunc)(entry->data);
392 }
393 crFree(entry);
394 entry = next;
395
396 }
397 }
398 crFreeHashIdPool( hash->idPool );
399
400#ifdef CHROMIUM_THREADSAFE
401 crUnlockMutex(&hash->mutex);
402 crFreeMutex(&hash->mutex);
403#endif
404
405 crFree( hash );
406}
407
408void crHashtableLock(CRHashTable *h)
409{
410#ifdef CHROMIUM_THREADSAFE
411 crLockMutex(&h->mutex);
412#endif
413}
414
415void crHashtableUnlock(CRHashTable *h)
416{
417#ifdef CHROMIUM_THREADSAFE
418 crUnlockMutex(&h->mutex);
419#endif
420}
421
422void crHashtableWalkUnlocked( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
423{
424 int i;
425 CRHashNode *entry, *next;
426
427 for (i = 0; i < CR_NUM_BUCKETS; i++)
428 {
429 entry = hash->buckets[i];
430 while (entry)
431 {
432 /* save next ptr here, in case walkFunc deletes this entry */
433 next = entry->next;
434 if (entry->data && walkFunc) {
435 (*walkFunc)( entry->key, entry->data, dataPtr2 );
436 }
437 entry = next;
438 }
439 }
440}
441
442void crHashtableWalk( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
443{
444 if (!hash)
445 return;
446
447#ifdef CHROMIUM_THREADSAFE
448 crLockMutex(&hash->mutex);
449#endif
450 crHashtableWalkUnlocked(hash, walkFunc , dataPtr2);
451#ifdef CHROMIUM_THREADSAFE
452 crUnlockMutex(&hash->mutex);
453#endif
454}
455
456static unsigned int crHash( unsigned long key )
457{
458 return key % CR_NUM_BUCKETS;
459}
460
461void crHashtableAdd( CRHashTable *h, unsigned long key, void *data )
462{
463 CRHashNode *node = (CRHashNode *) crCalloc( sizeof( CRHashNode ) );
464#ifdef CHROMIUM_THREADSAFE
465 crLockMutex(&h->mutex);
466#endif
467 node->key = key;
468 node->data = data;
469 node->next = h->buckets[crHash( key )];
470 h->buckets[ crHash( key ) ] = node;
471 h->num_elements++;
472 crHashIdPoolAllocId (h->idPool, key);
473#ifdef CHROMIUM_THREADSAFE
474 crUnlockMutex(&h->mutex);
475#endif
476}
477
478GLboolean crHashtableAllocRegisterKey( CRHashTable *h, GLuint key)
479{
480 GLboolean fAllocated;
481#ifdef CHROMIUM_THREADSAFE
482 crLockMutex(&h->mutex);
483#endif
484 fAllocated = crHashIdPoolAllocId (h->idPool, key);
485#ifdef CHROMIUM_THREADSAFE
486 crUnlockMutex(&h->mutex);
487#endif
488 return fAllocated;
489}
490
491GLuint crHashtableAllocKeys( CRHashTable *h, GLsizei range)
492{
493 GLuint res;
494 int i;
495
496#ifdef CHROMIUM_THREADSAFE
497 crLockMutex(&h->mutex);
498#endif
499 res = crHashIdPoolAllocBlock (h->idPool, range);
500#ifdef DEBUG_misha
501 Assert(res);
502 for (i = 0; i < range; ++i)
503 {
504 void *search = crHashtableSearch( h, res+i );
505 Assert(!search);
506 }
507#endif
508#ifdef CHROMIUM_THREADSAFE
509 crUnlockMutex(&h->mutex);
510#endif
511 return res;
512}
513
514void crHashtableDelete( CRHashTable *h, unsigned long key, CRHashtableCallback deleteFunc )
515{
516 unsigned int index = crHash( key );
517 CRHashNode *temp, *beftemp = NULL;
518
519#ifdef CHROMIUM_THREADSAFE
520 crLockMutex(&h->mutex);
521#endif
522 for ( temp = h->buckets[index]; temp; temp = temp->next )
523 {
524 if ( temp->key == key )
525 break;
526 beftemp = temp;
527 }
528 if ( temp )
529 {
530 if ( beftemp )
531 beftemp->next = temp->next;
532 else
533 h->buckets[index] = temp->next;
534 h->num_elements--;
535 if (temp->data && deleteFunc) {
536 (*deleteFunc)( temp->data );
537 }
538
539 crFree( temp );
540 }
541
542 crHashIdPoolFreeBlock( h->idPool, key, 1 );
543#ifdef CHROMIUM_THREADSAFE
544 crUnlockMutex(&h->mutex);
545#endif
546}
547
548void crHashtableDeleteBlock( CRHashTable *h, unsigned long key, GLsizei range, CRHashtableCallback deleteFunc )
549{
550 /* XXX optimize someday */
551 GLuint i;
552 for (i = 0; i < (GLuint)range; i++) {
553 crHashtableDelete( h, key, deleteFunc );
554 }
555}
556
557void *crHashtableSearch( const CRHashTable *h, unsigned long key )
558{
559 unsigned int index = crHash( key );
560 CRHashNode *temp;
561#ifdef CHROMIUM_THREADSAFE
562 crLockMutex((CRmutex *)&h->mutex);
563#endif
564 for ( temp = h->buckets[index]; temp; temp = temp->next )
565 {
566 if ( temp->key == key )
567 break;
568 }
569#ifdef CHROMIUM_THREADSAFE
570 crUnlockMutex((CRmutex *)&h->mutex);
571#endif
572 if ( !temp )
573 {
574 return NULL;
575 }
576 return temp->data;
577}
578
579void crHashtableReplace( CRHashTable *h, unsigned long key, void *data,
580 CRHashtableCallback deleteFunc)
581{
582 unsigned int index = crHash( key );
583 CRHashNode *temp;
584#ifdef CHROMIUM_THREADSAFE
585 crLockMutex(&h->mutex);
586#endif
587 for ( temp = h->buckets[index]; temp; temp = temp->next )
588 {
589 if ( temp->key == key )
590 break;
591 }
592#ifdef CHROMIUM_THREADSAFE
593 crUnlockMutex(&h->mutex);
594#endif
595 if ( !temp )
596 {
597 crHashtableAdd( h, key, data );
598 return;
599 }
600#ifdef CHROMIUM_THREADSAFE
601 crLockMutex(&h->mutex);
602#endif
603 if ( temp->data && deleteFunc )
604 {
605 (*deleteFunc)( temp->data );
606 }
607 temp->data = data;
608#ifdef CHROMIUM_THREADSAFE
609 crUnlockMutex(&h->mutex);
610#endif
611}
612
613unsigned int crHashtableNumElements( const CRHashTable *h)
614{
615 if (h)
616 return h->num_elements;
617 else
618 return 0;
619}
620
621/*
622 * Determine if the given key is used. Return GL_TRUE if so.
623 */
624GLboolean crHashtableIsKeyUsed( const CRHashTable *h, GLuint id )
625{
626 return (GLboolean) !crHashIdPoolIsIdFree( h->idPool, id);
627}
628
629GLboolean crHashtableGetDataKey(CRHashTable *pHash, void *pData, unsigned long *pKey)
630{
631 int i;
632 CRHashNode *entry;
633 GLboolean rc = GL_FALSE;
634
635 if (!pHash)
636 return rc;
637
638#ifdef CHROMIUM_THREADSAFE
639 crLockMutex(&pHash->mutex);
640#endif
641 for (i = 0; i<CR_NUM_BUCKETS && !rc; i++)
642 {
643 entry = pHash->buckets[i];
644 while (entry)
645 {
646 if (entry->data == pData) {
647 if (pKey)
648 *pKey = entry->key;
649 rc = GL_TRUE;
650 break;
651 }
652 entry = entry->next;
653 }
654 }
655#ifdef CHROMIUM_THREADSAFE
656 crUnlockMutex(&pHash->mutex);
657#endif
658
659 return rc;
660}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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