PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hsearch.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * hsearch.h
4 * exported definitions for utils/hash/dynahash.c; see notes therein
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/utils/hsearch.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef HSEARCH_H
15#define HSEARCH_H
16
17
18/*
19 * Hash functions must have this signature.
20 */
21typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
22
23/*
24 * Key comparison functions must have this signature. Comparison functions
25 * return zero for match, nonzero for no match. (The comparison function
26 * definition is designed to allow memcmp() and strncmp() to be used directly
27 * as key comparison functions.)
28 */
29typedef int (*HashCompareFunc) (const void *key1, const void *key2,
30 Size keysize);
31
32/*
33 * Key copying functions must have this signature. The return value is not
34 * used. (The definition is set up to allow memcpy() and strlcpy() to be
35 * used directly.)
36 */
37typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
38
39/*
40 * Space allocation function for a hashtable. Note: there is no free function
41 * API; can't destroy a hashtable unless you use the default allocator.
42 */
43typedef void *(*HashAllocFunc) (Size request, void *alloc_arg);
44
45/*
46 * HASHELEMENT is the private part of a hashtable entry. The caller's data
47 * follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key
48 * is expected to be at the start of the caller's hash entry data structure.
49 */
50typedef struct HASHELEMENT
51{
52 struct HASHELEMENT *link; /* link to next entry in same bucket */
53 uint32 hashvalue; /* hash function result for this entry */
55
56/* Hash table header struct is an opaque type known only within dynahash.c */
57typedef struct HASHHDR HASHHDR;
58
59/* Hash table control struct is an opaque type known only within dynahash.c */
60typedef struct HTAB HTAB;
61
62/* Parameter data structure for hash_create */
63/* Only those fields indicated by hash_flags need be set */
64typedef struct HASHCTL
65{
66 /* Used if HASH_PARTITION flag is set: */
67 int64 num_partitions; /* # partitions (must be power of 2) */
68 /* Used if HASH_ELEM flag is set (which is now required): */
69 Size keysize; /* hash key length in bytes */
70 Size entrysize; /* total user element size in bytes */
71 /* Used if HASH_FUNCTION flag is set: */
72 HashValueFunc hash; /* hash function */
73 /* Used if HASH_COMPARE flag is set: */
74 HashCompareFunc match; /* key comparison function */
75 /* Used if HASH_KEYCOPY flag is set: */
76 HashCopyFunc keycopy; /* key copying function */
77 /* Used if HASH_ALLOC flag is set: */
78 HashAllocFunc alloc; /* memory allocator */
79 void *alloc_arg; /* opaque argument passed to allocator */
80 /* Used if HASH_CONTEXT flag is set: */
81 MemoryContext hcxt; /* memory context to use for allocations */
82 /* Used if HASH_ATTACH flag is set: */
83 HASHHDR *hctl; /* location of header in shared mem */
85
86/* Flag bits for hash_create; most indicate which parameters are supplied */
87#define HASH_PARTITION 0x0001 /* Hashtable is used w/partitioned locking */
88/* 0x0002 is unused */
89/* 0x0004 is unused */
90#define HASH_ELEM 0x0008 /* Set keysize and entrysize (now required!) */
91#define HASH_STRINGS 0x0010 /* Select support functions for string keys */
92#define HASH_BLOBS 0x0020 /* Select support functions for binary keys */
93#define HASH_FUNCTION 0x0040 /* Set user defined hash function */
94#define HASH_COMPARE 0x0080 /* Set user defined comparison function */
95#define HASH_KEYCOPY 0x0100 /* Set user defined key-copying function */
96#define HASH_ALLOC 0x0200 /* Set memory allocator */
97#define HASH_CONTEXT 0x0400 /* Set memory allocation context */
98#define HASH_SHARED_MEM 0x0800 /* Hashtable is in shared memory */
99#define HASH_ATTACH 0x1000 /* Do not initialize hctl */
100#define HASH_FIXED_SIZE 0x2000 /* Initial size is a hard limit */
101
102/* max_dsize value to indicate expansible directory */
103#define NO_MAX_DSIZE (-1)
104
105/* hash_search operations */
113
114/* hash_seq status (should be considered an opaque type by callers) */
115typedef struct
116{
118 uint32 curBucket; /* index of current bucket */
119 HASHELEMENT *curEntry; /* current entry in bucket */
120 bool hasHashvalue; /* true if hashvalue was provided */
121 uint32 hashvalue; /* hashvalue to start seqscan over hash */
123
124/*
125 * prototypes for functions in dynahash.c
126 */
127extern HTAB *hash_create(const char *tabname, int64 nelem,
128 const HASHCTL *info, int flags);
129extern void hash_destroy(HTAB *hashp);
130extern void hash_stats(const char *caller, HTAB *hashp);
131extern void *hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action,
132 bool *foundPtr);
133extern uint32 get_hash_value(HTAB *hashp, const void *keyPtr);
134extern void *hash_search_with_hash_value(HTAB *hashp, const void *keyPtr,
135 uint32 hashvalue, HASHACTION action,
136 bool *foundPtr);
137extern bool hash_update_hash_key(HTAB *hashp, void *existingEntry,
138 const void *newKeyPtr);
139extern int64 hash_get_num_entries(HTAB *hashp);
140extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
142 HTAB *hashp,
143 uint32 hashvalue);
144extern void *hash_seq_search(HASH_SEQ_STATUS *status);
145extern void hash_seq_term(HASH_SEQ_STATUS *status);
146extern void hash_freeze(HTAB *hashp);
147extern Size hash_estimate_size(int64 num_entries, Size entrysize);
148extern void AtEOXact_HashTables(bool isCommit);
149extern void AtEOSubXact_HashTables(bool isCommit, int nestDepth);
150
151#endif /* HSEARCH_H */
int64_t int64
Definition c.h:621
uint32_t uint32
Definition c.h:624
size_t Size
Definition c.h:689
void hash_seq_init_with_hash_value(HASH_SEQ_STATUS *status, HTAB *hashp, uint32 hashvalue)
Definition dynahash.c:1337
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1864
Size hash_estimate_size(int64 num_entries, Size entrysize)
Definition dynahash.c:763
int(* HashCompareFunc)(const void *key1, const void *key2, Size keysize)
Definition hsearch.h:29
HASHACTION
Definition hsearch.h:107
@ HASH_FIND
Definition hsearch.h:108
@ HASH_REMOVE
Definition hsearch.h:110
@ HASH_ENTER
Definition hsearch.h:109
@ HASH_ENTER_NULL
Definition hsearch.h:111
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:360
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition dynahash.c:1890
void hash_destroy(HTAB *hashp)
Definition dynahash.c:802
void * hash_search_with_hash_value(HTAB *hashp, const void *keyPtr, uint32 hashvalue, HASHACTION action, bool *foundPtr)
Definition dynahash.c:902
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
uint32(* HashValueFunc)(const void *key, Size keysize)
Definition hsearch.h:21
void hash_seq_term(HASH_SEQ_STATUS *status)
Definition dynahash.c:1444
int64 hash_get_num_entries(HTAB *hashp)
Definition dynahash.c:1273
void hash_stats(const char *caller, HTAB *hashp)
Definition dynahash.c:821
void hash_freeze(HTAB *hashp)
Definition dynahash.c:1464
void *(* HashAllocFunc)(Size request, void *alloc_arg)
Definition hsearch.h:43
bool hash_update_hash_key(HTAB *hashp, void *existingEntry, const void *newKeyPtr)
Definition dynahash.c:1077
uint32 get_hash_value(HTAB *hashp, const void *keyPtr)
Definition dynahash.c:845
void *(* HashCopyFunc)(void *dest, const void *src, Size keysize)
Definition hsearch.h:37
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
static int fb(int x)
HashAllocFunc alloc
Definition hsearch.h:78
Size keysize
Definition hsearch.h:69
HashValueFunc hash
Definition hsearch.h:72
Size entrysize
Definition hsearch.h:70
void * alloc_arg
Definition hsearch.h:79
HashCompareFunc match
Definition hsearch.h:74
HASHHDR * hctl
Definition hsearch.h:83
MemoryContext hcxt
Definition hsearch.h:81
int64 num_partitions
Definition hsearch.h:67
HashCopyFunc keycopy
Definition hsearch.h:76
struct HASHELEMENT * link
Definition hsearch.h:52
uint32 hashvalue
Definition hsearch.h:53
uint32 hashvalue
Definition hsearch.h:121
HASHELEMENT * curEntry
Definition hsearch.h:119
uint32 curBucket
Definition hsearch.h:118
bool hasHashvalue
Definition hsearch.h:120