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 |
|
---|
20 | typedef struct FreeElemRec {
|
---|
21 | RTLISTNODE Node;
|
---|
22 | GLuint min;
|
---|
23 | GLuint max;
|
---|
24 | } FreeElem;
|
---|
25 |
|
---|
26 | struct CRHashIdPool {
|
---|
27 | RTLISTNODE freeList;
|
---|
28 | GLuint min;
|
---|
29 | GLuint max;
|
---|
30 | };
|
---|
31 |
|
---|
32 | typedef struct CRHashNode {
|
---|
33 | unsigned long key;
|
---|
34 | void *data;
|
---|
35 | struct CRHashNode *next;
|
---|
36 | } CRHashNode;
|
---|
37 |
|
---|
38 | struct 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 |
|
---|
48 | CRHashIdPool *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 |
|
---|
68 | CRHashIdPool *crAllocHashIdPool( void )
|
---|
69 | {
|
---|
70 | return crAllocHashIdPoolEx( CR_HASH_ID_MIN, CR_HASH_ID_MAX );
|
---|
71 | }
|
---|
72 |
|
---|
73 | void 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
|
---|
85 | static 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 |
|
---|
110 | static 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 | */
|
---|
138 | GLuint 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 | */
|
---|
173 | void 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 | */
|
---|
265 | GLboolean 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 | */
|
---|
331 | GLboolean 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 |
|
---|
347 | void 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 - 1));
|
---|
356 | walkFunc(prev->max+1, f->min - 1, data);
|
---|
357 | }
|
---|
358 |
|
---|
359 | prev = f;
|
---|
360 | }
|
---|
361 |
|
---|
362 | Assert(prev->max <= pool->max);
|
---|
363 |
|
---|
364 | if (prev->max < pool->max)
|
---|
365 | {
|
---|
366 | walkFunc(prev->max+1, pool->max, data);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | CRHashTable *crAllocHashtableEx( GLuint min, GLuint max )
|
---|
371 | {
|
---|
372 | int i;
|
---|
373 | CRHashTable *hash = (CRHashTable *) crCalloc( sizeof( CRHashTable )) ;
|
---|
374 | hash->num_elements = 0;
|
---|
375 | for (i = 0 ; i < CR_NUM_BUCKETS ; i++)
|
---|
376 | {
|
---|
377 | hash->buckets[i] = NULL;
|
---|
378 | }
|
---|
379 | hash->idPool = crAllocHashIdPoolEx( min, max );
|
---|
380 | #ifdef CHROMIUM_THREADSAFE
|
---|
381 | crInitMutex(&hash->mutex);
|
---|
382 | #endif
|
---|
383 | return hash;
|
---|
384 | }
|
---|
385 |
|
---|
386 | CRHashTable *crAllocHashtable( void )
|
---|
387 | {
|
---|
388 | return crAllocHashtableEx(CR_HASH_ID_MIN, CR_HASH_ID_MAX);
|
---|
389 | }
|
---|
390 |
|
---|
391 | void crFreeHashtable( CRHashTable *hash, CRHashtableCallback deleteFunc )
|
---|
392 | {
|
---|
393 | int i;
|
---|
394 | CRHashNode *entry, *next;
|
---|
395 |
|
---|
396 | if ( !hash) return;
|
---|
397 |
|
---|
398 | #ifdef CHROMIUM_THREADSAFE
|
---|
399 | crLockMutex(&hash->mutex);
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | for ( i = 0; i < CR_NUM_BUCKETS; i++ )
|
---|
403 | {
|
---|
404 | entry = hash->buckets[i];
|
---|
405 | while (entry)
|
---|
406 | {
|
---|
407 | next = entry->next;
|
---|
408 | /* Clear the key in case crHashtableDelete() is called
|
---|
409 | * from this callback.
|
---|
410 | */
|
---|
411 | entry->key = 0;
|
---|
412 | if (deleteFunc && entry->data)
|
---|
413 | {
|
---|
414 | (*deleteFunc)(entry->data);
|
---|
415 | }
|
---|
416 | crFree(entry);
|
---|
417 | entry = next;
|
---|
418 |
|
---|
419 | }
|
---|
420 | }
|
---|
421 | crFreeHashIdPool( hash->idPool );
|
---|
422 |
|
---|
423 | #ifdef CHROMIUM_THREADSAFE
|
---|
424 | crUnlockMutex(&hash->mutex);
|
---|
425 | crFreeMutex(&hash->mutex);
|
---|
426 | #endif
|
---|
427 |
|
---|
428 | crFree( hash );
|
---|
429 | }
|
---|
430 |
|
---|
431 | void crHashtableLock(CRHashTable *h)
|
---|
432 | {
|
---|
433 | #ifdef CHROMIUM_THREADSAFE
|
---|
434 | crLockMutex(&h->mutex);
|
---|
435 | #endif
|
---|
436 | }
|
---|
437 |
|
---|
438 | void crHashtableUnlock(CRHashTable *h)
|
---|
439 | {
|
---|
440 | #ifdef CHROMIUM_THREADSAFE
|
---|
441 | crUnlockMutex(&h->mutex);
|
---|
442 | #endif
|
---|
443 | }
|
---|
444 |
|
---|
445 | void crHashtableWalkUnlocked( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
|
---|
446 | {
|
---|
447 | int i;
|
---|
448 | CRHashNode *entry, *next;
|
---|
449 |
|
---|
450 | for (i = 0; i < CR_NUM_BUCKETS; i++)
|
---|
451 | {
|
---|
452 | entry = hash->buckets[i];
|
---|
453 | while (entry)
|
---|
454 | {
|
---|
455 | /* save next ptr here, in case walkFunc deletes this entry */
|
---|
456 | next = entry->next;
|
---|
457 | if (entry->data && walkFunc) {
|
---|
458 | (*walkFunc)( entry->key, entry->data, dataPtr2 );
|
---|
459 | }
|
---|
460 | entry = next;
|
---|
461 | }
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | void crHashtableWalk( CRHashTable *hash, CRHashtableWalkCallback walkFunc , void *dataPtr2)
|
---|
466 | {
|
---|
467 | if (!hash)
|
---|
468 | return;
|
---|
469 |
|
---|
470 | #ifdef CHROMIUM_THREADSAFE
|
---|
471 | crLockMutex(&hash->mutex);
|
---|
472 | #endif
|
---|
473 | crHashtableWalkUnlocked(hash, walkFunc , dataPtr2);
|
---|
474 | #ifdef CHROMIUM_THREADSAFE
|
---|
475 | crUnlockMutex(&hash->mutex);
|
---|
476 | #endif
|
---|
477 | }
|
---|
478 |
|
---|
479 | static unsigned int crHash( unsigned long key )
|
---|
480 | {
|
---|
481 | return key % CR_NUM_BUCKETS;
|
---|
482 | }
|
---|
483 |
|
---|
484 | void crHashtableAdd( CRHashTable *h, unsigned long key, void *data )
|
---|
485 | {
|
---|
486 | CRHashNode *node = (CRHashNode *) crCalloc( sizeof( CRHashNode ) );
|
---|
487 | #ifdef CHROMIUM_THREADSAFE
|
---|
488 | crLockMutex(&h->mutex);
|
---|
489 | #endif
|
---|
490 | node->key = key;
|
---|
491 | node->data = data;
|
---|
492 | node->next = h->buckets[crHash( key )];
|
---|
493 | h->buckets[ crHash( key ) ] = node;
|
---|
494 | h->num_elements++;
|
---|
495 | crHashIdPoolAllocId (h->idPool, key);
|
---|
496 | #ifdef CHROMIUM_THREADSAFE
|
---|
497 | crUnlockMutex(&h->mutex);
|
---|
498 | #endif
|
---|
499 | }
|
---|
500 |
|
---|
501 | GLboolean crHashtableAllocRegisterKey( CRHashTable *h, GLuint key)
|
---|
502 | {
|
---|
503 | GLboolean fAllocated;
|
---|
504 | #ifdef CHROMIUM_THREADSAFE
|
---|
505 | crLockMutex(&h->mutex);
|
---|
506 | #endif
|
---|
507 | fAllocated = crHashIdPoolAllocId (h->idPool, key);
|
---|
508 | #ifdef CHROMIUM_THREADSAFE
|
---|
509 | crUnlockMutex(&h->mutex);
|
---|
510 | #endif
|
---|
511 | return fAllocated;
|
---|
512 | }
|
---|
513 |
|
---|
514 | void crHashtableWalkKeys( CRHashTable *h, CRHashIdWalkKeys walkFunc , void *data)
|
---|
515 | {
|
---|
516 | #ifdef CHROMIUM_THREADSAFE
|
---|
517 | crLockMutex(&h->mutex);
|
---|
518 | #endif
|
---|
519 | crHashIdWalkKeys(h->idPool, walkFunc , data);
|
---|
520 | #ifdef CHROMIUM_THREADSAFE
|
---|
521 | crUnlockMutex(&h->mutex);
|
---|
522 | #endif
|
---|
523 | }
|
---|
524 |
|
---|
525 | GLuint crHashtableAllocKeys( CRHashTable *h, GLsizei range)
|
---|
526 | {
|
---|
527 | GLuint res;
|
---|
528 | int i;
|
---|
529 |
|
---|
530 | #ifdef CHROMIUM_THREADSAFE
|
---|
531 | crLockMutex(&h->mutex);
|
---|
532 | #endif
|
---|
533 | res = crHashIdPoolAllocBlock (h->idPool, range);
|
---|
534 | #ifdef DEBUG_misha
|
---|
535 | Assert(res);
|
---|
536 | for (i = 0; i < range; ++i)
|
---|
537 | {
|
---|
538 | void *search = crHashtableSearch( h, res+i );
|
---|
539 | Assert(!search);
|
---|
540 | }
|
---|
541 | #endif
|
---|
542 | #ifdef CHROMIUM_THREADSAFE
|
---|
543 | crUnlockMutex(&h->mutex);
|
---|
544 | #endif
|
---|
545 | return res;
|
---|
546 | }
|
---|
547 |
|
---|
548 | void crHashtableDelete( CRHashTable *h, unsigned long key, CRHashtableCallback deleteFunc )
|
---|
549 | {
|
---|
550 | unsigned int index = crHash( key );
|
---|
551 | CRHashNode *temp, *beftemp = NULL;
|
---|
552 |
|
---|
553 | #ifdef CHROMIUM_THREADSAFE
|
---|
554 | crLockMutex(&h->mutex);
|
---|
555 | #endif
|
---|
556 | for ( temp = h->buckets[index]; temp; temp = temp->next )
|
---|
557 | {
|
---|
558 | if ( temp->key == key )
|
---|
559 | break;
|
---|
560 | beftemp = temp;
|
---|
561 | }
|
---|
562 | if ( temp )
|
---|
563 | {
|
---|
564 | if ( beftemp )
|
---|
565 | beftemp->next = temp->next;
|
---|
566 | else
|
---|
567 | h->buckets[index] = temp->next;
|
---|
568 | h->num_elements--;
|
---|
569 | if (temp->data && deleteFunc) {
|
---|
570 | (*deleteFunc)( temp->data );
|
---|
571 | }
|
---|
572 |
|
---|
573 | crFree( temp );
|
---|
574 | }
|
---|
575 |
|
---|
576 | crHashIdPoolFreeBlock( h->idPool, key, 1 );
|
---|
577 | #ifdef CHROMIUM_THREADSAFE
|
---|
578 | crUnlockMutex(&h->mutex);
|
---|
579 | #endif
|
---|
580 | }
|
---|
581 |
|
---|
582 | void crHashtableDeleteBlock( CRHashTable *h, unsigned long key, GLsizei range, CRHashtableCallback deleteFunc )
|
---|
583 | {
|
---|
584 | /* XXX optimize someday */
|
---|
585 | GLuint i;
|
---|
586 | for (i = 0; i < (GLuint)range; i++) {
|
---|
587 | crHashtableDelete( h, key, deleteFunc );
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | void *crHashtableSearch( const CRHashTable *h, unsigned long key )
|
---|
592 | {
|
---|
593 | unsigned int index = crHash( key );
|
---|
594 | CRHashNode *temp;
|
---|
595 | #ifdef CHROMIUM_THREADSAFE
|
---|
596 | crLockMutex((CRmutex *)&h->mutex);
|
---|
597 | #endif
|
---|
598 | for ( temp = h->buckets[index]; temp; temp = temp->next )
|
---|
599 | {
|
---|
600 | if ( temp->key == key )
|
---|
601 | break;
|
---|
602 | }
|
---|
603 | #ifdef CHROMIUM_THREADSAFE
|
---|
604 | crUnlockMutex((CRmutex *)&h->mutex);
|
---|
605 | #endif
|
---|
606 | if ( !temp )
|
---|
607 | {
|
---|
608 | return NULL;
|
---|
609 | }
|
---|
610 | return temp->data;
|
---|
611 | }
|
---|
612 |
|
---|
613 | void crHashtableReplace( CRHashTable *h, unsigned long key, void *data,
|
---|
614 | CRHashtableCallback deleteFunc)
|
---|
615 | {
|
---|
616 | unsigned int index = crHash( key );
|
---|
617 | CRHashNode *temp;
|
---|
618 | #ifdef CHROMIUM_THREADSAFE
|
---|
619 | crLockMutex(&h->mutex);
|
---|
620 | #endif
|
---|
621 | for ( temp = h->buckets[index]; temp; temp = temp->next )
|
---|
622 | {
|
---|
623 | if ( temp->key == key )
|
---|
624 | break;
|
---|
625 | }
|
---|
626 | #ifdef CHROMIUM_THREADSAFE
|
---|
627 | crUnlockMutex(&h->mutex);
|
---|
628 | #endif
|
---|
629 | if ( !temp )
|
---|
630 | {
|
---|
631 | crHashtableAdd( h, key, data );
|
---|
632 | return;
|
---|
633 | }
|
---|
634 | #ifdef CHROMIUM_THREADSAFE
|
---|
635 | crLockMutex(&h->mutex);
|
---|
636 | #endif
|
---|
637 | if ( temp->data && deleteFunc )
|
---|
638 | {
|
---|
639 | (*deleteFunc)( temp->data );
|
---|
640 | }
|
---|
641 | temp->data = data;
|
---|
642 | #ifdef CHROMIUM_THREADSAFE
|
---|
643 | crUnlockMutex(&h->mutex);
|
---|
644 | #endif
|
---|
645 | }
|
---|
646 |
|
---|
647 | unsigned int crHashtableNumElements( const CRHashTable *h)
|
---|
648 | {
|
---|
649 | if (h)
|
---|
650 | return h->num_elements;
|
---|
651 | else
|
---|
652 | return 0;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /*
|
---|
656 | * Determine if the given key is used. Return GL_TRUE if so.
|
---|
657 | */
|
---|
658 | GLboolean crHashtableIsKeyUsed( const CRHashTable *h, GLuint id )
|
---|
659 | {
|
---|
660 | return (GLboolean) !crHashIdPoolIsIdFree( h->idPool, id);
|
---|
661 | }
|
---|
662 |
|
---|
663 | GLboolean crHashtableGetDataKey(CRHashTable *pHash, void *pData, unsigned long *pKey)
|
---|
664 | {
|
---|
665 | int i;
|
---|
666 | CRHashNode *entry;
|
---|
667 | GLboolean rc = GL_FALSE;
|
---|
668 |
|
---|
669 | if (!pHash)
|
---|
670 | return rc;
|
---|
671 |
|
---|
672 | #ifdef CHROMIUM_THREADSAFE
|
---|
673 | crLockMutex(&pHash->mutex);
|
---|
674 | #endif
|
---|
675 | for (i = 0; i<CR_NUM_BUCKETS && !rc; i++)
|
---|
676 | {
|
---|
677 | entry = pHash->buckets[i];
|
---|
678 | while (entry)
|
---|
679 | {
|
---|
680 | if (entry->data == pData) {
|
---|
681 | if (pKey)
|
---|
682 | *pKey = entry->key;
|
---|
683 | rc = GL_TRUE;
|
---|
684 | break;
|
---|
685 | }
|
---|
686 | entry = entry->next;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | #ifdef CHROMIUM_THREADSAFE
|
---|
690 | crUnlockMutex(&pHash->mutex);
|
---|
691 | #endif
|
---|
692 |
|
---|
693 | return rc;
|
---|
694 | }
|
---|