LCOV - code coverage report
Current view: top level - frmts/gtiff/libtiff - tif_hash_set.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 111 160 69.4 %
Date: 2026-12-05 06:39:39 Functions: 12 15 80.0 %

          Line data    Source code
       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             : 
      29             : /**
      30             :  * @file tif_hash_set.c
      31             :  * @brief Implement callback-driven buckets, recycled nodes, and element
      32             :  * disposal.
      33             :  */
      34             : 
      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             : 
      43             : /** @brief Name a private live/recycled hash-list node.
      44             :  * @ingroup libtiff_private_hash
      45             :  */
      46             : typedef struct _TIFFList TIFFList;
      47             : 
      48             : /**
      49             :  * @brief Link a live hash element or a recycled node.
      50             :  * @ingroup libtiff_private_hash
      51             :  *
      52             :  * The set owns nodes. Payload ownership follows the optional disposer;
      53             :  * recycled nodes can contain stale payload values that must not be used.
      54             :  */
      55             : struct _TIFFList
      56             : {
      57             :     /** @brief Live payload pointer, or stale/unused recycled value. */
      58             :     void *pData;
      59             :     /** @brief Next live-bucket or recycling-list node. */
      60             :     struct _TIFFList *psNext;
      61             : };
      62             : 
      63             : /**
      64             :  * @brief Own bucket/node storage and record callback-defined element policy.
      65             :  * @ingroup libtiff_private_hash
      66             :  *
      67             :  * All set storage is allocated through the TIFF allocators with the
      68             :  * borrowed tif handle, so it counts against that handle's memory limits.
      69             :  * Access and callback lifetime need external coordination; no internal lock
      70             :  * or iteration mutation guard exists.
      71             :  */
      72             : struct _TIFFHashSet
      73             : {
      74             :     /**
      75             :      * @brief Borrowed handle used for every set allocation and passed to
      76             :      * fnFreeEltFunc, or NULL for unaccounted allocation.
      77             :      */
      78             :     TIFF *tif;
      79             :     /** @brief Borrowed hash function, with a default installed at creation. */
      80             :     TIFFHashSetHashFunc fnHashFunc;
      81             :     /**
      82             :      * @brief Borrowed equality function, with a default installed at creation.
      83             :      */
      84             :     TIFFHashSetEqualFunc fnEqualFunc;
      85             :     /**
      86             :      * @brief Optional consuming disposer, including for failed incoming
      87             :      * insertions.
      88             :      */
      89             :     TIFFHashSetFreeEltFunc fnFreeEltFunc;
      90             :     /** @brief Owned array of bucket-head pointers. */
      91             :     TIFFList **tabList;
      92             :     /** @brief Live-element count in normal externally visible state. */
      93             :     int nSize;
      94             :     /**
      95             :      * @brief Index into anPrimes; callers must keep growth/shrink selection in
      96             :      * range.
      97             :      */
      98             :     int nIndiceAllocatedSize;
      99             :     /** @brief Logical bucket count used for hashing and traversal. */
     100             :     int nAllocatedSize;
     101             :     /**
     102             :      * @brief Owned spare-node list, whose payload fields are not live elements.
     103             :      */
     104             :     TIFFList *psRecyclingList;
     105             :     /**
     106             :      * @brief Number of nodes in the spare-node list, capped at 128 on return.
     107             :      */
     108             :     int nRecyclingListSize;
     109             :     /** @brief Deferred rehash state used by optional deferred removal. */
     110             :     bool bRehash;
     111             : #ifdef HASH_DEBUG
     112             :     /** @brief Optional HASH_DEBUG collision counter. */
     113             :     int nCollisions;
     114             : #endif
     115             : };
     116             : 
     117             : /**
     118             :  * @brief Provide the fixed sequence of supported bucket capacities.
     119             :  * @ingroup libtiff_private_hash
     120             :  *
     121             :  * Growth indexes this array without an explicit end-of-table guard.
     122             :  */
     123             : static 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             : 
     133             : /**
     134             :  * @brief Hash a pointer value without dereferencing it.
     135             :  * @ingroup libtiff_private_hash
     136             :  *
     137             :  * Does not allocate, mutate, dispose, or emit diagnostics. Collisions are
     138             :  * resolved by equality; the result is not a persistent identifier or
     139             :  * cryptographic hash.
     140             :  *
     141             :  * @param[in] elt Arbitrary pointer, including NULL.
     142             :  * @return The uintptr_t pointer representation converted to unsigned long;
     143             :  * high bits can be truncated when that type is narrower.
     144             :  */
     145           0 : static unsigned long TIFFHashSetHashPointer(const void *elt)
     146             : {
     147           0 :     return (unsigned long)(uintptr_t)((void *)(elt));
     148             : }
     149             : 
     150             : /************************************************************************/
     151             : /*                      TIFFHashSetEqualPointer()                       */
     152             : /************************************************************************/
     153             : 
     154             : /**
     155             :  * @brief Compare pointer values without dereferencing them.
     156             :  * @ingroup libtiff_private_hash
     157             :  *
     158             :  * Does not allocate, mutate, dispose, transfer ownership, or emit
     159             :  * diagnostics.
     160             :  *
     161             :  * @param[in] elt1 First pointer, possibly NULL.
     162             :  * @param[in] elt2 Second pointer, possibly NULL.
     163             :  * @return true if the pointers compare equal; false otherwise.
     164             :  */
     165           0 : static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2)
     166             : {
     167           0 :     return elt1 == elt2;
     168             : }
     169             : 
     170             : /************************************************************************/
     171             : /*                           TIFFHashSetNew()                           */
     172             : /************************************************************************/
     173             : 
     174      144758 : TIFFHashSet *TIFFHashSetNew(TIFF *tif, TIFFHashSetHashFunc fnHashFunc,
     175             :                             TIFFHashSetEqualFunc fnEqualFunc,
     176             :                             TIFFHashSetFreeEltFunc fnFreeEltFunc)
     177             : {
     178      144758 :     TIFFHashSet *set = (TIFFHashSet *)_TIFFmallocExt(tif, sizeof(TIFFHashSet));
     179      144899 :     if (set == NULL)
     180           0 :         return NULL;
     181      144899 :     set->tif = tif;
     182      144899 :     set->fnHashFunc = fnHashFunc ? fnHashFunc : TIFFHashSetHashPointer;
     183      144899 :     set->fnEqualFunc = fnEqualFunc ? fnEqualFunc : TIFFHashSetEqualPointer;
     184      144899 :     set->fnFreeEltFunc = fnFreeEltFunc;
     185      144899 :     set->nSize = 0;
     186      144899 :     set->tabList = (TIFFList **)_TIFFcallocExt(tif, 53, sizeof(TIFFList *));
     187      144908 :     if (set->tabList == NULL)
     188             :     {
     189           0 :         _TIFFfreeExt(tif, set);
     190           0 :         return NULL;
     191             :     }
     192      144931 :     set->nIndiceAllocatedSize = 0;
     193      144931 :     set->nAllocatedSize = 53;
     194      144931 :     set->psRecyclingList = NULL;
     195      144931 :     set->nRecyclingListSize = 0;
     196      144931 :     set->bRehash = false;
     197             : #ifdef HASH_DEBUG
     198             :     set->nCollisions = 0;
     199             : #endif
     200      144931 :     return set;
     201             : }
     202             : 
     203             : /************************************************************************/
     204             : /*                          TIFFHashSetSize()                           */
     205             : /************************************************************************/
     206             : 
     207       80227 : int TIFFHashSetSize(const TIFFHashSet *set)
     208             : {
     209       80227 :     assert(set != NULL);
     210       80227 :     return set->nSize;
     211             : }
     212             : 
     213             : /************************************************************************/
     214             : /*                      TIFFHashSetGetNewListElt()                      */
     215             : /************************************************************************/
     216             : 
     217             : /**
     218             :  * @brief Acquire a detached node from the cache or the set's TIFF allocator.
     219             :  * @ingroup libtiff_private_hash
     220             :  *
     221             :  * Pops and decrements the recycling list when available, clearing its stale
     222             :  * payload pointer; a freshly allocated node is uninitialised. The caller
     223             :  * must initialise payload/link fields before linking. Does not invoke the
     224             :  * disposer or emit diagnostics.
     225             :  *
     226             :  * @param[in,out] set Non-NULL live set with a consistent recycling
     227             :  * list/count.
     228             :  * @return A detached node, or NULL if fresh-node allocation fails.
     229             :  */
     230      160702 : static TIFFList *TIFFHashSetGetNewListElt(TIFFHashSet *set)
     231             : {
     232      160702 :     if (set->psRecyclingList)
     233             :     {
     234        2978 :         TIFFList *psRet = set->psRecyclingList;
     235        2978 :         psRet->pData = NULL;
     236        2978 :         set->nRecyclingListSize--;
     237        2978 :         set->psRecyclingList = psRet->psNext;
     238        2978 :         return psRet;
     239             :     }
     240             : 
     241      157724 :     return (TIFFList *)_TIFFmallocExt(set->tif, sizeof(TIFFList));
     242             : }
     243             : 
     244             : /************************************************************************/
     245             : /*                      TIFFHashSetReturnListElt()                      */
     246             : /************************************************************************/
     247             : 
     248             : /**
     249             :  * @brief Recycle a detached node or free it when the cache is full.
     250             :  * @ingroup libtiff_private_hash
     251             :  *
     252             :  * Pushes the node onto the recycling list while fewer than 128 nodes are
     253             :  * cached, otherwise calls _TIFFfreeExt(). Does not dispose or dereference its
     254             :  * payload; the caller must have completed element disposal/ownership
     255             :  * handling first. Returns no status and emits no diagnostic.
     256             :  *
     257             :  * @param[in,out] set Non-NULL live set.
     258             :  * @param[in] psList Detached owned node; must not be used by the caller
     259             :  * afterward.
     260             :  */
     261        3000 : static void TIFFHashSetReturnListElt(TIFFHashSet *set, TIFFList *psList)
     262             : {
     263        3000 :     if (set->nRecyclingListSize < 128)
     264             :     {
     265        3000 :         psList->psNext = set->psRecyclingList;
     266        3000 :         set->psRecyclingList = psList;
     267        3000 :         set->nRecyclingListSize++;
     268             :     }
     269             :     else
     270             :     {
     271           0 :         _TIFFfreeExt(set->tif, psList);
     272             :     }
     273        3000 : }
     274             : 
     275             : /************************************************************************/
     276             : /*                      TIFFHashSetClearInternal()                      */
     277             : /************************************************************************/
     278             : 
     279             : /**
     280             :  * @brief Unlink live buckets and dispose their elements as configured.
     281             :  * @ingroup libtiff_private_hash
     282             :  *
     283             :  * Calls the disposer when installed, empties each bucket, and clears
     284             :  * bRehash. Does not reset nSize or capacity metadata; Clear/Destroy callers
     285             :  * finish that bookkeeping. Does not release a pre-existing recycling list or
     286             :  * the bucket array. No status or direct diagnostic is provided.
     287             :  *
     288             :  * @param[in,out] set Non-NULL live set.
     289             :  * @param[in] bFinalize true to free live nodes; false to recycle/free them
     290             :  * through the cache.
     291             :  */
     292      145000 : static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize)
     293             : {
     294      145000 :     assert(set != NULL);
     295     7826970 :     for (int i = 0; i < set->nAllocatedSize; i++)
     296             :     {
     297     7681960 :         TIFFList *cur = set->tabList[i];
     298     7839790 :         while (cur)
     299             :         {
     300      157818 :             if (set->fnFreeEltFunc)
     301       78912 :                 set->fnFreeEltFunc(set->tif, cur->pData);
     302      157821 :             TIFFList *psNext = cur->psNext;
     303      157821 :             if (bFinalize)
     304      157821 :                 _TIFFfreeExt(set->tif, cur);
     305             :             else
     306           0 :                 TIFFHashSetReturnListElt(set, cur);
     307      157825 :             cur = psNext;
     308             :         }
     309     7681970 :         set->tabList[i] = NULL;
     310             :     }
     311      145007 :     set->bRehash = false;
     312      145007 : }
     313             : 
     314             : /************************************************************************/
     315             : /*                          TIFFListDestroy()                           */
     316             : /************************************************************************/
     317             : 
     318             : /**
     319             :  * @brief Free a node chain without disposing its payload fields.
     320             :  * @ingroup libtiff_private_hash
     321             :  *
     322             :  * Walks psNext and frees nodes only. Appropriate for recycled nodes
     323             :  * containing stale payload values. Does not free payload objects, invoke
     324             :  * callbacks, or emit diagnostics.
     325             :  *
     326             :  * @param[in] tif Handle the nodes were allocated with, or NULL.
     327             :  *
     328             :  * @param[in] psList Owned list head, or NULL; chain nodes become invalid.
     329             :  */
     330      145003 : static void TIFFListDestroy(TIFF *tif, TIFFList *psList)
     331             : {
     332      145003 :     TIFFList *psCurrent = psList;
     333             : 
     334      145025 :     while (psCurrent)
     335             :     {
     336          22 :         TIFFList *const psNext = psCurrent->psNext;
     337          22 :         _TIFFfreeExt(tif, psCurrent);
     338          22 :         psCurrent = psNext;
     339             :     }
     340      145003 : }
     341             : 
     342             : /************************************************************************/
     343             : /*                         TIFFHashSetDestroy()                         */
     344             : /************************************************************************/
     345             : 
     346      144998 : void TIFFHashSetDestroy(TIFFHashSet *set)
     347             : {
     348      144998 :     if (set)
     349             :     {
     350      145003 :         TIFFHashSetClearInternal(set, true);
     351      145004 :         _TIFFfreeExt(set->tif, set->tabList);
     352      145005 :         TIFFListDestroy(set->tif, set->psRecyclingList);
     353      145005 :         _TIFFfreeExt(set->tif, set);
     354             :     }
     355      145000 : }
     356             : 
     357             : #ifdef notused
     358             : /************************************************************************/
     359             : /*                          TIFFHashSetClear()                          */
     360             : /************************************************************************/
     361             : 
     362             : void 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             : 
     377             : void 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             : 
     402             : /**
     403             :  * @brief Replace the bucket array using the selected prime-capacity index.
     404             :  * @ingroup libtiff_private_hash
     405             :  *
     406             :  * Allocation failure leaves the current array/nodes/elements unchanged; the
     407             :  * caller owns any capacity-index rollback. Success invokes hash callbacks,
     408             :  * relinks existing nodes, frees the old bucket array, updates capacity, and
     409             :  * clears bRehash. Payload objects and list nodes do not move, but bucket
     410             :  * views and traversal order change. HASH_DEBUG builds also update/log
     411             :  * collision statistics. No direct allocation-failure diagnostic is emitted.
     412             :  *
     413             :  * @param[in,out] set Non-NULL set with nIndiceAllocatedSize within anPrimes
     414             :  * bounds.
     415             :  * @return true after relinking into the new bucket array; false if its
     416             :  * _TIFFcallocExt() fails or exceeds a memory limit.
     417             :  */
     418           0 : static bool TIFFHashSetRehash(TIFFHashSet *set)
     419             : {
     420           0 :     int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize];
     421           0 :     TIFFList **newTabList = (TIFFList **)_TIFFcallocExt(
     422             :         set->tif, nNewAllocatedSize, sizeof(TIFFList *));
     423           0 :     if (newTabList == NULL)
     424           0 :         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           0 :     for (int i = 0; i < set->nAllocatedSize; i++)
     434             :     {
     435           0 :         TIFFList *cur = set->tabList[i];
     436           0 :         while (cur)
     437             :         {
     438           0 :             const unsigned long nNewHashVal =
     439           0 :                 set->fnHashFunc(cur->pData) % (unsigned long)nNewAllocatedSize;
     440             : #ifdef HASH_DEBUG
     441             :             if (newTabList[nNewHashVal])
     442             :                 set->nCollisions++;
     443             : #endif
     444           0 :             TIFFList *psNext = cur->psNext;
     445           0 :             cur->psNext = newTabList[nNewHashVal];
     446           0 :             newTabList[nNewHashVal] = cur;
     447           0 :             cur = psNext;
     448             :         }
     449             :     }
     450           0 :     _TIFFfreeExt(set->tif, set->tabList);
     451           0 :     set->tabList = newTabList;
     452           0 :     set->nAllocatedSize = nNewAllocatedSize;
     453           0 :     set->bRehash = false;
     454           0 :     return true;
     455             : }
     456             : 
     457             : /************************************************************************/
     458             : /*                         TIFFHashSetFindPtr()                         */
     459             : /************************************************************************/
     460             : 
     461             : /**
     462             :  * @brief Find the mutable payload slot for a matching key.
     463             :  * @ingroup libtiff_private_hash
     464             :  *
     465             :  * Invokes callbacks without allocation or container mutation. The slot
     466             :  * remains part of its node across rehash, but removal/recycling/destruction
     467             :  * invalidates or repurposes it. Does not extend payload lifetime, take
     468             :  * ownership, or emit diagnostics.
     469             :  *
     470             :  * @param[in] set Non-NULL live set.
     471             :  * @param[in] elt Borrowed key supported by the hash/equality callbacks.
     472             :  * @return A borrowed address of the matching node's payload slot, or NULL if
     473             :  * absent.
     474             :  */
     475      391343 : static void **TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
     476             : {
     477      391333 :     const unsigned long nHashVal =
     478      391343 :         set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize;
     479      391333 :     TIFFList *cur = set->tabList[nHashVal];
     480      392044 :     while (cur)
     481             :     {
     482       68697 :         if (set->fnEqualFunc(cur->pData, elt))
     483       67986 :             return &cur->pData;
     484         711 :         cur = cur->psNext;
     485             :     }
     486      323347 :     return NULL;
     487             : }
     488             : 
     489             : /************************************************************************/
     490             : /*                         TIFFHashSetInsert()                          */
     491             : /************************************************************************/
     492             : 
     493      160702 : bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
     494             : {
     495      160702 :     assert(set != NULL);
     496      160702 :     void **pElt = TIFFHashSetFindPtr(set, elt);
     497      160774 :     if (pElt)
     498             :     {
     499           0 :         if (set->fnFreeEltFunc)
     500           0 :             set->fnFreeEltFunc(set->tif, *pElt);
     501             : 
     502           0 :         *pElt = elt;
     503           0 :         return true;
     504             :     }
     505             : 
     506      160774 :     if (set->nSize >= 2 * set->nAllocatedSize / 3 ||
     507      160748 :         (set->bRehash && set->nIndiceAllocatedSize > 0 &&
     508           0 :          set->nSize <= set->nAllocatedSize / 2))
     509             :     {
     510          26 :         set->nIndiceAllocatedSize++;
     511          26 :         if (!TIFFHashSetRehash(set))
     512             :         {
     513           0 :             set->nIndiceAllocatedSize--;
     514           0 :             if (set->fnFreeEltFunc)
     515           0 :                 set->fnFreeEltFunc(set->tif, elt);
     516           0 :             return false;
     517             :         }
     518             :     }
     519             : 
     520      160763 :     const unsigned long nHashVal =
     521      160748 :         set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize;
     522             : #ifdef HASH_DEBUG
     523             :     if (set->tabList[nHashVal])
     524             :         set->nCollisions++;
     525             : #endif
     526             : 
     527      160763 :     TIFFList *new_elt = TIFFHashSetGetNewListElt(set);
     528      160764 :     if (new_elt == NULL)
     529             :     {
     530           0 :         if (set->fnFreeEltFunc)
     531           0 :             set->fnFreeEltFunc(set->tif, elt);
     532           0 :         return false;
     533             :     }
     534      160764 :     new_elt->pData = elt;
     535      160764 :     new_elt->psNext = set->tabList[nHashVal];
     536      160764 :     set->tabList[nHashVal] = new_elt;
     537      160764 :     set->nSize++;
     538             : 
     539      160764 :     return true;
     540             : }
     541             : 
     542             : /************************************************************************/
     543             : /*                         TIFFHashSetLookup()                          */
     544             : /************************************************************************/
     545             : 
     546      230562 : void *TIFFHashSetLookup(TIFFHashSet *set, const void *elt)
     547             : {
     548      230562 :     assert(set != NULL);
     549      230562 :     void **pElt = TIFFHashSetFindPtr(set, elt);
     550      230591 :     if (pElt)
     551       67987 :         return *pElt;
     552             : 
     553      162604 :     return NULL;
     554             : }
     555             : 
     556             : /************************************************************************/
     557             : /*                     TIFFHashSetRemoveInternal()                      */
     558             : /************************************************************************/
     559             : 
     560             : /**
     561             :  * @brief Remove a matching element with immediate or deferred shrink handling.
     562             :  * @ingroup libtiff_private_hash
     563             :  *
     564             :  * Selects shrink handling before looking up the key. Immediate rehash
     565             :  * failure restores the capacity index and the removal continues with the
     566             :  * current table, since rehashing is only an optimisation.
     567             :  * Deferred handling can change index/rehash state even when the key is
     568             :  * absent. A found node is unlinked, its payload is disposed if configured,
     569             :  * its node is returned to the cache, and nSize decreases. No direct
     570             :  * diagnostic is emitted.
     571             :  *
     572             :  * @param[in,out] set Non-NULL live set.
     573             :  * @param[in] elt Borrowed removal key; may become invalid if it is the
     574             :  * disposed element.
     575             :  * @param[in] bDeferRehash true to mark a possible shrink for later rather
     576             :  * than allocating now.
     577             :  * @return true when removed; false if absent.
     578             :  */
     579        3000 : static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt,
     580             :                                       bool bDeferRehash)
     581             : {
     582        3000 :     assert(set != NULL);
     583        3000 :     if (set->nIndiceAllocatedSize > 0 && set->nSize <= set->nAllocatedSize / 2)
     584             :     {
     585           0 :         set->nIndiceAllocatedSize--;
     586           0 :         if (bDeferRehash)
     587           0 :             set->bRehash = true;
     588             :         else
     589             :         {
     590           0 :             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. */
     595           0 :                 set->nIndiceAllocatedSize++;
     596             :             }
     597             :         }
     598             :     }
     599             : 
     600        3000 :     int nHashVal =
     601        3000 :         (int)(set->fnHashFunc(elt) % (unsigned long)set->nAllocatedSize);
     602        3000 :     TIFFList *cur = set->tabList[nHashVal];
     603        3000 :     TIFFList *prev = NULL;
     604        3000 :     while (cur)
     605             :     {
     606        3000 :         if (set->fnEqualFunc(cur->pData, elt))
     607             :         {
     608        3000 :             if (prev)
     609           0 :                 prev->psNext = cur->psNext;
     610             :             else
     611        3000 :                 set->tabList[nHashVal] = cur->psNext;
     612             : 
     613        3000 :             if (set->fnFreeEltFunc)
     614        1500 :                 set->fnFreeEltFunc(set->tif, cur->pData);
     615             : 
     616        3000 :             TIFFHashSetReturnListElt(set, cur);
     617             : #ifdef HASH_DEBUG
     618             :             if (set->tabList[nHashVal])
     619             :                 set->nCollisions--;
     620             : #endif
     621        3000 :             set->nSize--;
     622        3000 :             return true;
     623             :         }
     624           0 :         prev = cur;
     625           0 :         cur = cur->psNext;
     626             :     }
     627           0 :     return false;
     628             : }
     629             : 
     630             : /************************************************************************/
     631             : /*                         TIFFHashSetRemove()                          */
     632             : /************************************************************************/
     633             : 
     634        3000 : bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt)
     635             : {
     636        3000 :     return TIFFHashSetRemoveInternal(set, elt, false);
     637             : }
     638             : 
     639             : #ifdef notused
     640             : /************************************************************************/
     641             : /*                    TIFFHashSetRemoveDeferRehash()                    */
     642             : /************************************************************************/
     643             : 
     644             : bool TIFFHashSetRemoveDeferRehash(TIFFHashSet *set, const void *elt)
     645             : {
     646             :     return TIFFHashSetRemoveInternal(set, elt, true);
     647             : }
     648             : #endif

Generated by: LCOV version 1.14