LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_hash_set.c
Go to the documentation of this file.
1/**********************************************************************
2 *
3 * Name: tif_hash_set.c
4 * Purpose: Hash set functions.
5 * Author: Even Rouault, <even dot rouault at spatialys.com>
6 *
7 **********************************************************************
8 * Copyright (c) 2008-2009, Even Rouault <even dot rouault at spatialys.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
28
35#include "tiffiop.h"
36
37#include <assert.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <stdlib.h>
42
46typedef struct _TIFFList TIFFList;
47
56{
58 void *pData;
61};
62
116
123static const int anPrimes[] = {
124 53, 97, 193, 389, 769, 1543, 3079,
125 6151, 12289, 24593, 49157, 98317, 196613, 393241,
126 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
127 100663319, 201326611, 402653189, 805306457, 1610612741};
128
129/************************************************************************/
130/* TIFFHashSetHashPointer() */
131/************************************************************************/
132
145static unsigned long TIFFHashSetHashPointer(const void *elt)
146{
147 return (unsigned long)(uintptr_t)((void *)(elt));
148}
149
150/************************************************************************/
151/* TIFFHashSetEqualPointer() */
152/************************************************************************/
153
165static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2)
166{
167 return elt1 == elt2;
168}
169
170/************************************************************************/
171/* TIFFHashSetNew() */
172/************************************************************************/
173
175 TIFFHashSetEqualFunc fnEqualFunc,
176 TIFFHashSetFreeEltFunc fnFreeEltFunc)
177{
178 TIFFHashSet *set = (TIFFHashSet *)_TIFFmallocExt(tif, sizeof(TIFFHashSet));
179 if (set == NULL)
180 return NULL;
181 set->tif = tif;
182 set->fnHashFunc = fnHashFunc ? fnHashFunc : TIFFHashSetHashPointer;
183 set->fnEqualFunc = fnEqualFunc ? fnEqualFunc : TIFFHashSetEqualPointer;
184 set->fnFreeEltFunc = fnFreeEltFunc;
185 set->nSize = 0;
186 set->tabList = (TIFFList **)_TIFFcallocExt(tif, 53, sizeof(TIFFList *));
187 if (set->tabList == NULL)
188 {
189 _TIFFfreeExt(tif, set);
190 return NULL;
191 }
192 set->nIndiceAllocatedSize = 0;
193 set->nAllocatedSize = 53;
194 set->psRecyclingList = NULL;
195 set->nRecyclingListSize = 0;
196 set->bRehash = false;
197#ifdef HASH_DEBUG
198 set->nCollisions = 0;
199#endif
200 return set;
201}
202
203/************************************************************************/
204/* TIFFHashSetSize() */
205/************************************************************************/
206
208{
209 assert(set != NULL);
210 return set->nSize;
211}
212
213/************************************************************************/
214/* TIFFHashSetGetNewListElt() */
215/************************************************************************/
216
231{
232 if (set->psRecyclingList)
233 {
234 TIFFList *psRet = set->psRecyclingList;
235 psRet->pData = NULL;
236 set->nRecyclingListSize--;
237 set->psRecyclingList = psRet->psNext;
238 return psRet;
239 }
240
241 return (TIFFList *)_TIFFmallocExt(set->tif, sizeof(TIFFList));
242}
243
244/************************************************************************/
245/* TIFFHashSetReturnListElt() */
246/************************************************************************/
247
262{
263 if (set->nRecyclingListSize < 128)
264 {
265 psList->psNext = set->psRecyclingList;
266 set->psRecyclingList = psList;
267 set->nRecyclingListSize++;
268 }
269 else
270 {
271 _TIFFfreeExt(set->tif, psList);
272 }
273}
274
275/************************************************************************/
276/* TIFFHashSetClearInternal() */
277/************************************************************************/
278
292static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize)
293{
294 assert(set != NULL);
295 for (int i = 0; i < set->nAllocatedSize; i++)
296 {
297 TIFFList *cur = set->tabList[i];
298 while (cur)
299 {
300 if (set->fnFreeEltFunc)
301 set->fnFreeEltFunc(set->tif, cur->pData);
302 TIFFList *psNext = cur->psNext;
303 if (bFinalize)
304 _TIFFfreeExt(set->tif, cur);
305 else
306 TIFFHashSetReturnListElt(set, cur);
307 cur = psNext;
308 }
309 set->tabList[i] = NULL;
310 }
311 set->bRehash = false;
312}
313
314/************************************************************************/
315/* TIFFListDestroy() */
316/************************************************************************/
317
330static void TIFFListDestroy(TIFF *tif, TIFFList *psList)
331{
332 TIFFList *psCurrent = psList;
333
334 while (psCurrent)
335 {
336 TIFFList *const psNext = psCurrent->psNext;
337 _TIFFfreeExt(tif, psCurrent);
338 psCurrent = psNext;
339 }
340}
341
342/************************************************************************/
343/* TIFFHashSetDestroy() */
344/************************************************************************/
345
347{
348 if (set)
349 {
350 TIFFHashSetClearInternal(set, true);
351 _TIFFfreeExt(set->tif, set->tabList);
353 _TIFFfreeExt(set->tif, set);
354 }
355}
356
357#ifdef notused
358/************************************************************************/
359/* TIFFHashSetClear() */
360/************************************************************************/
361
362void TIFFHashSetClear(TIFFHashSet *set)
363{
364 TIFFHashSetClearInternal(set, false);
365 set->nIndiceAllocatedSize = 0;
366 set->nAllocatedSize = 53;
367#ifdef HASH_DEBUG
368 set->nCollisions = 0;
369#endif
370 set->nSize = 0;
371}
372
373/************************************************************************/
374/* TIFFHashSetForeach() */
375/************************************************************************/
376
377void TIFFHashSetForeach(TIFFHashSet *set, TIFFHashSetIterEltFunc fnIterFunc,
378 void *user_data)
379{
380 assert(set != NULL);
381 if (!fnIterFunc)
382 return;
383
384 for (int i = 0; i < set->nAllocatedSize; i++)
385 {
386 TIFFList *cur = set->tabList[i];
387 while (cur)
388 {
389 if (!fnIterFunc(cur->pData, user_data))
390 return;
391
392 cur = cur->psNext;
393 }
394 }
395}
396#endif
397
398/************************************************************************/
399/* TIFFHashSetRehash() */
400/************************************************************************/
401
419{
420 int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize];
421 TIFFList **newTabList = (TIFFList **)_TIFFcallocExt(
422 set->tif, nNewAllocatedSize, sizeof(TIFFList *));
423 if (newTabList == NULL)
424 return false;
425#ifdef HASH_DEBUG
426 TIFFDebug("TIFFHASH",
427 "hashSet=%p, nSize=%d, nCollisions=%d, "
428 "fCollisionRate=%.02f",
429 set, set->nSize, set->nCollisions,
430 set->nCollisions * 100.0 / set->nSize);
431 set->nCollisions = 0;
432#endif
433 for (int i = 0; i < set->nAllocatedSize; i++)
434 {
435 TIFFList *cur = set->tabList[i];
436 while (cur)
437 {
438 const unsigned long nNewHashVal =
439 set->fnHashFunc(cur->pData) % (unsigned long)nNewAllocatedSize;
440#ifdef HASH_DEBUG
441 if (newTabList[nNewHashVal])
442 set->nCollisions++;
443#endif
444 TIFFList *psNext = cur->psNext;
445 cur->psNext = newTabList[nNewHashVal];
446 newTabList[nNewHashVal] = cur;
447 cur = psNext;
448 }
449 }
450 _TIFFfreeExt(set->tif, set->tabList);
451 set->tabList = newTabList;
452 set->nAllocatedSize = nNewAllocatedSize;
453 set->bRehash = false;
454 return true;
455}
456
457/************************************************************************/
458/* TIFFHashSetFindPtr() */
459/************************************************************************/
460
475static void **TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
476{
477 const unsigned long nHashVal =
478 set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize;
479 TIFFList *cur = set->tabList[nHashVal];
480 while (cur)
481 {
482 if (set->fnEqualFunc(cur->pData, elt))
483 return &cur->pData;
484 cur = cur->psNext;
485 }
486 return NULL;
487}
488
489/************************************************************************/
490/* TIFFHashSetInsert() */
491/************************************************************************/
492
493bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
494{
495 assert(set != NULL);
496 void **pElt = TIFFHashSetFindPtr(set, elt);
497 if (pElt)
498 {
499 if (set->fnFreeEltFunc)
500 set->fnFreeEltFunc(set->tif, *pElt);
501
502 *pElt = elt;
503 return true;
504 }
505
506 if (set->nSize >= 2 * set->nAllocatedSize / 3 ||
507 (set->bRehash && set->nIndiceAllocatedSize > 0 &&
508 set->nSize <= set->nAllocatedSize / 2))
509 {
511 if (!TIFFHashSetRehash(set))
512 {
514 if (set->fnFreeEltFunc)
515 set->fnFreeEltFunc(set->tif, elt);
516 return false;
517 }
518 }
519
520 const unsigned long nHashVal =
521 set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize;
522#ifdef HASH_DEBUG
523 if (set->tabList[nHashVal])
524 set->nCollisions++;
525#endif
526
527 TIFFList *new_elt = TIFFHashSetGetNewListElt(set);
528 if (new_elt == NULL)
529 {
530 if (set->fnFreeEltFunc)
531 set->fnFreeEltFunc(set->tif, elt);
532 return false;
533 }
534 new_elt->pData = elt;
535 new_elt->psNext = set->tabList[nHashVal];
536 set->tabList[nHashVal] = new_elt;
537 set->nSize++;
538
539 return true;
540}
541
542/************************************************************************/
543/* TIFFHashSetLookup() */
544/************************************************************************/
545
546void *TIFFHashSetLookup(TIFFHashSet *set, const void *elt)
547{
548 assert(set != NULL);
549 void **pElt = TIFFHashSetFindPtr(set, elt);
550 if (pElt)
551 return *pElt;
552
553 return NULL;
554}
555
556/************************************************************************/
557/* TIFFHashSetRemoveInternal() */
558/************************************************************************/
559
579static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt,
580 bool bDeferRehash)
581{
582 assert(set != NULL);
583 if (set->nIndiceAllocatedSize > 0 && set->nSize <= set->nAllocatedSize / 2)
584 {
586 if (bDeferRehash)
587 set->bRehash = true;
588 else
589 {
590 if (!TIFFHashSetRehash(set))
591 {
592 /* Rehashing is an optimization. Allocation failure must not
593 * prevent removal, especially when another set shares
594 * ownership of the same element. */
596 }
597 }
598 }
599
600 int nHashVal =
601 (int)(set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize);
602 TIFFList *cur = set->tabList[nHashVal];
603 TIFFList *prev = NULL;
604 while (cur)
605 {
606 if (set->fnEqualFunc(cur->pData, elt))
607 {
608 if (prev)
609 prev->psNext = cur->psNext;
610 else
611 set->tabList[nHashVal] = cur->psNext;
612
613 if (set->fnFreeEltFunc)
614 set->fnFreeEltFunc(set->tif, cur->pData);
615
616 TIFFHashSetReturnListElt(set, cur);
617#ifdef HASH_DEBUG
618 if (set->tabList[nHashVal])
619 set->nCollisions--;
620#endif
621 set->nSize--;
622 return true;
623 }
624 prev = cur;
625 cur = cur->psNext;
626 }
627 return false;
628}
629
630/************************************************************************/
631/* TIFFHashSetRemove() */
632/************************************************************************/
633
634bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt)
635{
636 return TIFFHashSetRemoveInternal(set, elt, false);
637}
638
639#ifdef notused
640/************************************************************************/
641/* TIFFHashSetRemoveDeferRehash() */
642/************************************************************************/
643
644bool TIFFHashSetRemoveDeferRehash(TIFFHashSet *set, const void *elt)
645{
646 return TIFFHashSetRemoveInternal(set, elt, true);
647}
648#endif
bool(* TIFFHashSetEqualFunc)(const void *elt1, const void *elt2)
Test whether a stored element matches another element or key.
static void TIFFHashSetReturnListElt(TIFFHashSet *set, TIFFList *psList)
Recycle a detached node or free it when the cache is full.
static unsigned long TIFFHashSetHashPointer(const void *elt)
Hash a pointer value without dereferencing it.
void * TIFFHashSetLookup(TIFFHashSet *set, const void *elt)
Find a borrowed stored element matching a key.
bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
Insert an element or replace an equal element.
bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt)
Remove a matching element, disposing it if configured.
int TIFFHashSetSize(const TIFFHashSet *set)
Return the current live-element count.
void(* TIFFHashSetFreeEltFunc)(struct tiff *tif, void *elt)
Dispose of an element according to the set's ownership policy.
static TIFFList * TIFFHashSetGetNewListElt(TIFFHashSet *set)
Acquire a detached node from the cache or the set's TIFF allocator.
static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2)
Compare pointer values without dereferencing them.
void TIFFHashSetDestroy(TIFFHashSet *set)
Release a hash set and dispose its live elements if configured.
static const int anPrimes[]
Provide the fixed sequence of supported bucket capacities.
static void TIFFListDestroy(TIFF *tif, TIFFList *psList)
Free a node chain without disposing its payload fields.
static void ** TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
Find the mutable payload slot for a matching key.
static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt, bool bDeferRehash)
Remove a matching element with immediate or deferred shrink handling.
static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize)
Unlink live buckets and dispose their elements as configured.
unsigned long(* TIFFHashSetHashFunc)(const void *elt)
Compute an element hash without changing its key.
static bool TIFFHashSetRehash(TIFFHashSet *set)
Replace the bucket array using the selected prime-capacity index.
TIFFHashSet * TIFFHashSetNew(TIFF *tif, TIFFHashSetHashFunc fnHashFunc, TIFFHashSetEqualFunc fnEqualFunc, TIFFHashSetFreeEltFunc fnFreeEltFunc)
Allocate an empty hash set with optional ownership callbacks.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array under a handle's allocation policy.
Definition tif_open.c:224
Own bucket/node storage and record callback-defined element policy.
int nIndiceAllocatedSize
Index into anPrimes; callers must keep growth/shrink selection in range.
TIFF * tif
Borrowed handle used for every set allocation and passed to fnFreeEltFunc, or NULL for unaccounted al...
TIFFList * psRecyclingList
Owned spare-node list, whose payload fields are not live elements.
TIFFHashSetEqualFunc fnEqualFunc
Borrowed equality function, with a default installed at creation.
int nSize
Live-element count in normal externally visible state.
TIFFHashSetFreeEltFunc fnFreeEltFunc
Optional consuming disposer, including for failed incoming insertions.
TIFFList ** tabList
Owned array of bucket-head pointers.
TIFFHashSetHashFunc fnHashFunc
Borrowed hash function, with a default installed at creation.
int nAllocatedSize
Logical bucket count used for hashing and traversal.
bool bRehash
Deferred rehash state used by optional deferred removal.
int nRecyclingListSize
Number of nodes in the spare-node list, capped at 128 on return.
Link a live hash element or a recycled node.
struct _TIFFList * psNext
Next live-bucket or recycling-list node.
void * pData
Live payload pointer, or stale/unused recycled value.
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
Declare private library state, callbacks, and implementation helpers.