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