LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions | Variables

Store unique elements with explicit comparison and disposal callbacks. More...

Collaboration diagram for Hash sets:

Data Structures

struct  _TIFFList
 Link a live hash element or a recycled node. More...
 
struct  _TIFFHashSet
 Own bucket/node storage and record callback-defined element policy. More...
 

Typedefs

typedef struct _TIFFList TIFFList
 Name a private live/recycled hash-list node.
 
typedef struct _TIFFHashSet TIFFHashSet
 Refer to a private hash set with callback-defined element ownership.
 
typedef unsigned long(* TIFFHashSetHashFunc) (const void *elt)
 Compute an element hash without changing its key.
 
typedef bool(* TIFFHashSetEqualFunc) (const void *elt1, const void *elt2)
 Test whether a stored element matches another element or key.
 
typedef void(* TIFFHashSetFreeEltFunc) (struct tiff *tif, void *elt)
 Dispose of an element according to the set's ownership policy.
 

Functions

static unsigned long TIFFHashSetHashPointer (const void *elt)
 Hash a pointer value without dereferencing it.
 
static bool TIFFHashSetEqualPointer (const void *elt1, const void *elt2)
 Compare pointer values without dereferencing them.
 
static TIFFList * TIFFHashSetGetNewListElt (TIFFHashSet *set)
 Acquire a detached node from the cache or the set's TIFF allocator.
 
static void TIFFHashSetReturnListElt (TIFFHashSet *set, TIFFList *psList)
 Recycle a detached node or free it when the cache is full.
 
static void TIFFHashSetClearInternal (TIFFHashSet *set, bool bFinalize)
 Unlink live buckets and dispose their elements as configured.
 
static void TIFFListDestroy (TIFF *tif, TIFFList *psList)
 Free a node chain without disposing its payload fields.
 
static bool TIFFHashSetRehash (TIFFHashSet *set)
 Replace the bucket array using the selected prime-capacity index.
 
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.
 
TIFFHashSet * TIFFHashSetNew (struct tiff *tif, TIFFHashSetHashFunc fnHashFunc, TIFFHashSetEqualFunc fnEqualFunc, TIFFHashSetFreeEltFunc fnFreeEltFunc)
 Allocate an empty hash set with optional ownership callbacks.
 
void TIFFHashSetDestroy (TIFFHashSet *set)
 Release a hash set and dispose its live elements if configured.
 
int TIFFHashSetSize (const TIFFHashSet *set)
 Return the current live-element count.
 
bool TIFFHashSetInsert (TIFFHashSet *set, void *elt)
 Insert an element or replace an equal element.
 
void * TIFFHashSetLookup (TIFFHashSet *set, const void *elt)
 Find a borrowed stored element matching a key.
 
bool TIFFHashSetRemove (TIFFHashSet *set, const void *elt)
 Remove a matching element, disposing it if configured.
 

Variables

static const int anPrimes []
 Provide the fixed sequence of supported bucket capacities.
 

Detailed Description

Store unique elements with explicit comparison and disposal callbacks.

Typedef Documentation

◆ TIFFHashSet

typedef struct _TIFFHashSet TIFFHashSet

Refer to a private hash set with callback-defined element ownership.

Owns its buckets and nodes. Element disposal depends on fnFreeEltFunc. Allocate with TIFFHashSetNew() and release with TIFFHashSetDestroy().

Definition at line 63 of file tif_hash_set.h.

◆ TIFFHashSetEqualFunc

typedef bool(* TIFFHashSetEqualFunc) (const void *elt1, const void *elt2)

Test whether a stored element matches another element or key.

Matching elements must have equal hashes. Both representations must be accepted by the callback, with stable key fields while stored. Callback code/state outlives the set; it must not structurally mutate or destroy the invoking set.

Parameters
[in]elt1Stored element.
[in]elt2Incoming element or lookup/removal key.
Returns
true for equal keys; false otherwise.

Definition at line 93 of file tif_hash_set.h.

◆ TIFFHashSetFreeEltFunc

typedef void(* TIFFHashSetFreeEltFunc) (struct tiff *tif, void *elt)

Dispose of an element according to the set's ownership policy.

Invoked on replacement, removal, clear, destruction, and failed insertion of an incoming element. A failed insertion therefore does not return ownership to a caller that configured a consuming disposer. The callback must accept every inserted representation and must not mutate or destroy the invoking set. The set adds no status/error handling around disposal.

Parameters
[in]tifHandle given to TIFFHashSetNew(), possibly NULL, for releasing the element through the matching allocator.
[in,out]eltElement being consumed; may become invalid after disposal.

Definition at line 111 of file tif_hash_set.h.

◆ TIFFHashSetHashFunc

typedef unsigned long(* TIFFHashSetHashFunc) (const void *elt)

Compute an element hash without changing its key.

The callback and any referenced external state must outlive the set. It must support stored elements and lookup-key representations consistently and must not mutate or destroy the set. Hash bits need not be a pointer address or stable across processes.

Parameters
[in]eltStored element or lookup key, including NULL only if supported by the callback.
Returns
An unsigned hash; equal elements must produce equal hashes.

Definition at line 78 of file tif_hash_set.h.

◆ TIFFList

typedef struct _TIFFList TIFFList

Name a private live/recycled hash-list node.

Definition at line 46 of file tif_hash_set.c.

Function Documentation

◆ TIFFHashSetClearInternal()

static void TIFFHashSetClearInternal ( TIFFHashSet *  set,
bool  bFinalize 
)
static

Unlink live buckets and dispose their elements as configured.

Calls the disposer when installed, empties each bucket, and clears bRehash. Does not reset nSize or capacity metadata; Clear/Destroy callers finish that bookkeeping. Does not release a pre-existing recycling list or the bucket array. No status or direct diagnostic is provided.

Parameters
[in,out]setNon-NULL live set.
[in]bFinalizetrue to free live nodes; false to recycle/free them through the cache.

Definition at line 292 of file tif_hash_set.c.

References _TIFFfreeExt(), _TIFFHashSet::bRehash, _TIFFHashSet::fnFreeEltFunc, _TIFFHashSet::nAllocatedSize, _TIFFList::pData, _TIFFList::psNext, _TIFFHashSet::tabList, _TIFFHashSet::tif, and TIFFHashSetReturnListElt().

Referenced by TIFFHashSetDestroy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetDestroy()

void TIFFHashSetDestroy ( TIFFHashSet *  set)

Release a hash set and dispose its live elements if configured.

NULL is accepted. Calls the disposer for each live element when installed, frees buckets/live/recycled nodes, then frees the set. The set and all container views become invalid. Without a disposer, element allocations remain caller-owned. No status or direct diagnostic is provided; disposer effects follow its contract.

Parameters
[in,out]setOwned set to destroy, or NULL.

Definition at line 346 of file tif_hash_set.c.

References _TIFFfreeExt(), _TIFFHashSet::psRecyclingList, _TIFFHashSet::tabList, _TIFFHashSet::tif, TIFFHashSetClearInternal(), and TIFFListDestroy().

Referenced by _TIFFCleanupIFDOffsetAndNumberMaps().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetEqualPointer()

static bool TIFFHashSetEqualPointer ( const void *  elt1,
const void *  elt2 
)
static

Compare pointer values without dereferencing them.

Does not allocate, mutate, dispose, transfer ownership, or emit diagnostics.

Parameters
[in]elt1First pointer, possibly NULL.
[in]elt2Second pointer, possibly NULL.
Returns
true if the pointers compare equal; false otherwise.

Definition at line 165 of file tif_hash_set.c.

Referenced by TIFFHashSetNew().

Here is the caller graph for this function:

◆ TIFFHashSetFindPtr()

static void ** TIFFHashSetFindPtr ( TIFFHashSet *  set,
const void *  elt 
)
static

Find the mutable payload slot for a matching key.

Invokes callbacks without allocation or container mutation. The slot remains part of its node across rehash, but removal/recycling/destruction invalidates or repurposes it. Does not extend payload lifetime, take ownership, or emit diagnostics.

Parameters
[in]setNon-NULL live set.
[in]eltBorrowed key supported by the hash/equality callbacks.
Returns
A borrowed address of the matching node's payload slot, or NULL if absent.

Definition at line 475 of file tif_hash_set.c.

References _TIFFHashSet::fnEqualFunc, _TIFFHashSet::fnHashFunc, _TIFFHashSet::nAllocatedSize, _TIFFList::pData, _TIFFList::psNext, and _TIFFHashSet::tabList.

Referenced by TIFFHashSetInsert(), and TIFFHashSetLookup().

Here is the caller graph for this function:

◆ TIFFHashSetGetNewListElt()

static TIFFList * TIFFHashSetGetNewListElt ( TIFFHashSet *  set)
static

Acquire a detached node from the cache or the set's TIFF allocator.

Pops and decrements the recycling list when available, clearing its stale payload pointer; a freshly allocated node is uninitialised. The caller must initialise payload/link fields before linking. Does not invoke the disposer or emit diagnostics.

Parameters
[in,out]setNon-NULL live set with a consistent recycling list/count.
Returns
A detached node, or NULL if fresh-node allocation fails.

Definition at line 230 of file tif_hash_set.c.

References _TIFFmallocExt(), _TIFFHashSet::nRecyclingListSize, _TIFFList::pData, _TIFFList::psNext, _TIFFHashSet::psRecyclingList, and _TIFFHashSet::tif.

Referenced by TIFFHashSetInsert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetHashPointer()

static unsigned long TIFFHashSetHashPointer ( const void *  elt)
static

Hash a pointer value without dereferencing it.

Does not allocate, mutate, dispose, or emit diagnostics. Collisions are resolved by equality; the result is not a persistent identifier or cryptographic hash.

Parameters
[in]eltArbitrary pointer, including NULL.
Returns
The uintptr_t pointer representation converted to unsigned long; high bits can be truncated when that type is narrower.

Definition at line 145 of file tif_hash_set.c.

Referenced by TIFFHashSetNew().

Here is the caller graph for this function:

◆ TIFFHashSetInsert()

bool TIFFHashSetInsert ( TIFFHashSet *  set,
void *  elt 
)

Insert an element or replace an equal element.

For an equal key, disposes the previous stored element before replacing its pointer; the count is unchanged. With a consuming disposer, reinserting the same owned pointer as its own replacement leaves a dangling pointer and is not supported. Without a disposer, the caller remains responsible for replaced element storage.

A new key can allocate/rehash buckets and allocate or recycle a node. A failed insertion leaves existing membership/payloads intact, but a successful rehash before a node-allocation failure may change bucket layout/order. Without a disposer, a failed incoming element remains caller-owned. Emits no direct diagnostic. Growth assumes an in-range index into the fixed prime-capacity table; it is not an unbounded- capacity contract.

Parameters
[in,out]setNon-NULL live set with compatible callbacks.
[in,out]eltIncoming element; consumed by the disposer on failure when installed.
Returns
true when inserted/replaced; false on a required bucket/node allocation failure. On false, elt has already been passed to the disposer when installed.

Definition at line 493 of file tif_hash_set.c.

References _TIFFHashSet::bRehash, _TIFFHashSet::fnFreeEltFunc, _TIFFHashSet::fnHashFunc, _TIFFHashSet::nAllocatedSize, _TIFFHashSet::nIndiceAllocatedSize, _TIFFHashSet::nSize, _TIFFList::pData, _TIFFList::psNext, _TIFFHashSet::tabList, _TIFFHashSet::tif, TIFFHashSetFindPtr(), TIFFHashSetGetNewListElt(), and TIFFHashSetRehash().

Referenced by insertOffsetAndDirNumber().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetLookup()

void * TIFFHashSetLookup ( TIFFHashSet *  set,
const void *  elt 
)

Find a borrowed stored element matching a key.

Invokes hash/equality callbacks without allocation or container mutation. Does not transfer element ownership. Rehashing does not move payload objects, but replacement/removal/clear/destruction may dispose the returned object. Do not alter stored key fields or assume the view extends lifetime. Emits no direct diagnostic.

Parameters
[in]setNon-NULL live set.
[in]eltBorrowed lookup key accepted by the hash/equality callbacks.
Returns
The matching stored pointer, or NULL if absent; a stored NULL pointer cannot be distinguished from absence.

Definition at line 546 of file tif_hash_set.c.

References TIFFHashSetFindPtr().

Referenced by _TIFFCheckDirNumberAndOffset(), _TIFFGetDirNumberFromOffset(), _TIFFGetOffsetFromDirNumber(), and _TIFFRemoveEntryFromDirectoryListByOffset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetNew()

TIFFHashSet * TIFFHashSetNew ( struct tiff *  tif,
TIFFHashSetHashFunc  fnHashFunc,
TIFFHashSetEqualFunc  fnEqualFunc,
TIFFHashSetFreeEltFunc  fnFreeEltFunc 
)

Allocate an empty hash set with optional ownership callbacks.

The set, its bucket array and its list nodes are allocated and freed through the TIFF allocators with tif, so they count against that handle's single and cumulative memory limits; with a NULL tif they are not counted against any handle. A non-NULL tif must outlive the set, and its cumulative memory limit must be neither enabled nor disabled until the set is destroyed. Failure releases partial container allocations and emits no library diagnostic. Hash/equality callbacks must agree and remain valid until destruction; elements' hash/equality keys must remain stable while stored. A configured disposer consumes elements on removal/replacement/destruction and also on failed insertion. No external element is owned by creation itself.

Parameters
[in]tifBorrowed handle used for allocation and passed to fnFreeEltFunc, or NULL.
[in]fnHashFuncBorrowed hash callback, or NULL for pointer hashing.
[in]fnEqualFuncBorrowed equality callback, or NULL for pointer equality.
[in]fnFreeEltFuncOptional element disposer; NULL leaves elements caller-owned.
Returns
An owned empty set for TIFFHashSetDestroy(), or NULL if set/bucket allocation fails or a memory limit is exceeded.

Definition at line 174 of file tif_hash_set.c.

References _TIFFcallocExt(), _TIFFfreeExt(), _TIFFmallocExt(), _TIFFHashSet::bRehash, _TIFFHashSet::fnEqualFunc, _TIFFHashSet::fnFreeEltFunc, _TIFFHashSet::fnHashFunc, _TIFFHashSet::nAllocatedSize, _TIFFHashSet::nIndiceAllocatedSize, _TIFFHashSet::nRecyclingListSize, _TIFFHashSet::nSize, _TIFFHashSet::psRecyclingList, _TIFFHashSet::tabList, _TIFFHashSet::tif, TIFFHashSetEqualPointer(), and TIFFHashSetHashPointer().

Referenced by _TIFFCheckDirNumberAndOffset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetRehash()

static bool TIFFHashSetRehash ( TIFFHashSet *  set)
static

Replace the bucket array using the selected prime-capacity index.

Allocation failure leaves the current array/nodes/elements unchanged; the caller owns any capacity-index rollback. Success invokes hash callbacks, relinks existing nodes, frees the old bucket array, updates capacity, and clears bRehash. Payload objects and list nodes do not move, but bucket views and traversal order change. HASH_DEBUG builds also update/log collision statistics. No direct allocation-failure diagnostic is emitted.

Parameters
[in,out]setNon-NULL set with nIndiceAllocatedSize within anPrimes bounds.
Returns
true after relinking into the new bucket array; false if its _TIFFcallocExt() fails or exceeds a memory limit.

Definition at line 418 of file tif_hash_set.c.

References _TIFFcallocExt(), _TIFFfreeExt(), anPrimes, _TIFFHashSet::bRehash, _TIFFHashSet::fnHashFunc, _TIFFHashSet::nAllocatedSize, _TIFFHashSet::nIndiceAllocatedSize, _TIFFHashSet::nSize, _TIFFList::pData, _TIFFList::psNext, _TIFFHashSet::tabList, and _TIFFHashSet::tif.

Referenced by TIFFHashSetInsert(), and TIFFHashSetRemoveInternal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetRemove()

bool TIFFHashSetRemove ( TIFFHashSet *  set,
const void *  elt 
)

Remove a matching element, disposing it if configured.

May allocate a smaller bucket table before looking for the key, even if the key is absent. A failed shrink allocation keeps the current table and the removal proceeds, so a set sharing ownership of the element with another set is never left holding a disposed element. Successful removal unlinks the node, calls the disposer if installed, recycles/frees the node, and decrements the count. Without a disposer, payload storage stays caller-owned. No direct diagnostic or ownership of an unmatched key is taken.

Parameters
[in,out]setNon-NULL live set.
[in]eltBorrowed removal key; may alias a stored object that disposal invalidates.
Returns
true when removed; false when no stored element matches.

Definition at line 634 of file tif_hash_set.c.

References TIFFHashSetRemoveInternal().

Referenced by _TIFFCheckDirNumberAndOffset(), _TIFFRemoveEntryFromDirectoryListByOffset(), and insertOffsetAndDirNumber().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetRemoveInternal()

static bool TIFFHashSetRemoveInternal ( TIFFHashSet *  set,
const void *  elt,
bool  bDeferRehash 
)
static

Remove a matching element with immediate or deferred shrink handling.

Selects shrink handling before looking up the key. Immediate rehash failure restores the capacity index and the removal continues with the current table, since rehashing is only an optimisation. Deferred handling can change index/rehash state even when the key is absent. A found node is unlinked, its payload is disposed if configured, its node is returned to the cache, and nSize decreases. No direct diagnostic is emitted.

Parameters
[in,out]setNon-NULL live set.
[in]eltBorrowed removal key; may become invalid if it is the disposed element.
[in]bDeferRehashtrue to mark a possible shrink for later rather than allocating now.
Returns
true when removed; false if absent.

Definition at line 579 of file tif_hash_set.c.

References _TIFFHashSet::bRehash, _TIFFHashSet::fnEqualFunc, _TIFFHashSet::fnFreeEltFunc, _TIFFHashSet::fnHashFunc, _TIFFHashSet::nAllocatedSize, _TIFFHashSet::nIndiceAllocatedSize, _TIFFHashSet::nSize, _TIFFList::pData, _TIFFList::psNext, _TIFFHashSet::tabList, _TIFFHashSet::tif, TIFFHashSetRehash(), and TIFFHashSetReturnListElt().

Referenced by TIFFHashSetRemove().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetReturnListElt()

static void TIFFHashSetReturnListElt ( TIFFHashSet *  set,
TIFFList *  psList 
)
static

Recycle a detached node or free it when the cache is full.

Pushes the node onto the recycling list while fewer than 128 nodes are cached, otherwise calls _TIFFfreeExt(). Does not dispose or dereference its payload; the caller must have completed element disposal/ownership handling first. Returns no status and emits no diagnostic.

Parameters
[in,out]setNon-NULL live set.
[in]psListDetached owned node; must not be used by the caller afterward.

Definition at line 261 of file tif_hash_set.c.

References _TIFFfreeExt(), _TIFFHashSet::nRecyclingListSize, _TIFFList::psNext, _TIFFHashSet::psRecyclingList, and _TIFFHashSet::tif.

Referenced by TIFFHashSetClearInternal(), and TIFFHashSetRemoveInternal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIFFHashSetSize()

int TIFFHashSetSize ( const TIFFHashSet *  set)

Return the current live-element count.

Reads state without allocation, callback invocation, ownership transfer, or diagnostics. Debug builds assert that set is non-NULL.

Parameters
[in]setNon-NULL live set.
Returns
The live-element count, not the bucket capacity; no distinct error result.

Definition at line 207 of file tif_hash_set.c.

References _TIFFHashSet::nSize.

Referenced by _TIFFCheckDirNumberAndOffset().

Here is the caller graph for this function:

◆ TIFFListDestroy()

static void TIFFListDestroy ( TIFF *  tif,
TIFFList *  psList 
)
static

Free a node chain without disposing its payload fields.

Walks psNext and frees nodes only. Appropriate for recycled nodes containing stale payload values. Does not free payload objects, invoke callbacks, or emit diagnostics.

Parameters
[in]tifHandle the nodes were allocated with, or NULL.
[in]psListOwned list head, or NULL; chain nodes become invalid.

Definition at line 330 of file tif_hash_set.c.

References _TIFFfreeExt(), and _TIFFList::psNext.

Referenced by TIFFHashSetDestroy().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ anPrimes

const int anPrimes[]
static
Initial value:
= {
53, 97, 193, 389, 769, 1543, 3079,
6151, 12289, 24593, 49157, 98317, 196613, 393241,
786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
100663319, 201326611, 402653189, 805306457, 1610612741}

Provide the fixed sequence of supported bucket capacities.

Growth indexes this array without an explicit end-of-table guard.

Definition at line 123 of file tif_hash_set.c.

Referenced by TIFFHashSetRehash().