1 /*-------------------------------------------------------------------------
4 * for hash tables, particularly hash tables in shared memory
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/utils/hsearch.h,v 1.38 2005/06/18 20:51:30 tgl Exp $
12 *-------------------------------------------------------------------------
19 * Hash functions must have this signature.
21 typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
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.)
29 typedef int (*HashCompareFunc) (const void *key1, const void *key2,
33 * Key copying functions must have this signature. The return value is not
34 * used. (The definition is set up to allow memcpy() and strncpy() to be
37 typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
40 * Space allocation function for a hashtable --- designed to match malloc().
41 * Note: there is no free function API; can't destroy a hashtable unless you
42 * use the default allocator.
44 typedef void *(*HashAllocFunc) (Size request);
49 * A hash table has a top-level "directory", each of whose entries points
50 * to a "segment" of ssize bucket headers. The maximum number of hash
51 * buckets is thus dsize * ssize (but dsize may be expansible). Of course,
52 * the number of records in the table can be larger, but we don't want a
53 * whole lot of records per bucket or performance goes down.
55 * In a hash table allocated in shared memory, the directory cannot be
56 * expanded because it must stay at a fixed address. The directory size
57 * should be selected using hash_select_dirsize (and you'd better have
58 * a good idea of the maximum number of entries!). For non-shared hash
59 * tables, the initial directory size can be left at the default.
61 #define DEF_SEGSIZE 256
62 #define DEF_SEGSIZE_SHIFT 8 /* must be log2(DEF_SEGSIZE) */
63 #define DEF_DIRSIZE 256
64 #define DEF_FFACTOR 1 /* default fill factor */
68 * HASHELEMENT is the private part of a hashtable entry. The caller's data
69 * follows the HASHELEMENT structure (on a MAXALIGN'd boundary). The hash key
70 * is expected to be at the start of the caller's hash entry data structure.
72 typedef struct HASHELEMENT
74 struct HASHELEMENT *link; /* link to next entry in same bucket */
75 uint32 hashvalue; /* hash function result for this entry */
78 /* A hash bucket is a linked list of HASHELEMENTs */
79 typedef HASHELEMENT *HASHBUCKET;
81 /* A hash segment is an array of bucket headers */
82 typedef HASHBUCKET *HASHSEGMENT;
84 /* Header structure for a hash table --- contains all changeable info */
85 typedef struct HASHHDR
87 long dsize; /* Directory Size */
88 long ssize; /* Segment Size --- must be power of 2 */
89 int sshift; /* Segment shift = log2(ssize) */
90 uint32 max_bucket; /* ID of Maximum bucket in use */
91 uint32 high_mask; /* Mask to modulo into entire table */
92 uint32 low_mask; /* Mask to modulo into lower half of table */
93 long ffactor; /* Fill factor */
94 long nentries; /* Number of entries in hash table */
95 long nsegs; /* Number of allocated segments */
96 Size keysize; /* hash key length in bytes */
97 Size entrysize; /* total user element size in bytes */
98 long max_dsize; /* 'dsize' limit if directory is fixed
100 HASHELEMENT *freeList; /* linked list of free elements */
101 #ifdef HASH_STATISTICS
108 * Top control structure for a hashtable --- need not be shared, since
109 * no fields change at runtime
113 HASHHDR *hctl; /* shared control information */
114 HASHSEGMENT *dir; /* directory of segment starts */
115 HashValueFunc hash; /* hash function */
116 HashCompareFunc match; /* key comparison function */
117 HashCopyFunc keycopy; /* key copying function */
118 HashAllocFunc alloc; /* memory allocator */
119 MemoryContext hcxt; /* memory context if default allocator
121 char *tabname; /* table name (for error messages) */
122 bool isshared; /* true if table is in shared memory */
125 /* Parameter data structure for hash_create */
126 /* Only those fields indicated by hash_flags need be set */
127 typedef struct HASHCTL
129 long ssize; /* Segment Size */
130 long dsize; /* (initial) Directory Size */
131 long max_dsize; /* limit to dsize if directory size is
133 long ffactor; /* Fill factor */
134 Size keysize; /* hash key length in bytes */
135 Size entrysize; /* total user element size in bytes */
136 HashValueFunc hash; /* hash function */
137 HashCompareFunc match; /* key comparison function */
138 HashCopyFunc keycopy; /* key copying function */
139 HashAllocFunc alloc; /* memory allocator */
140 HASHSEGMENT *dir; /* directory of segment starts */
141 HASHHDR *hctl; /* location of header in shared mem */
142 MemoryContext hcxt; /* memory context to use for allocations */
145 /* Flags to indicate which parameters are supplied */
146 #define HASH_SEGMENT 0x002 /* Set segment size */
147 #define HASH_DIRSIZE 0x004 /* Set directory size */
148 #define HASH_FFACTOR 0x008 /* Set fill factor */
149 #define HASH_FUNCTION 0x010 /* Set user defined hash function */
150 #define HASH_ELEM 0x020 /* Set key/entry size */
151 #define HASH_SHARED_MEM 0x040 /* Hashtable is in shared memory */
152 #define HASH_ATTACH 0x080 /* Do not initialize hctl */
153 #define HASH_ALLOC 0x100 /* Set memory allocator */
154 #define HASH_CONTEXT 0x200 /* Set explicit memory context */
155 #define HASH_COMPARE 0x400 /* Set user defined comparison function */
156 #define HASH_KEYCOPY 0x800 /* Set user defined key-copying function */
159 /* max_dsize value to indicate expansible directory */
160 #define NO_MAX_DSIZE (-1)
161 /* number of hash elements allocated at once */
162 #define HASHELEMENT_ALLOC_INCR (32)
164 /* hash_search operations */
173 /* hash_seq status (should be considered an opaque type by callers) */
177 uint32 curBucket; /* index of current bucket */
178 HASHELEMENT *curEntry; /* current entry in bucket */
182 * prototypes for functions in dynahash.c
184 extern HTAB *hash_create(const char *tabname, long nelem,
185 HASHCTL *info, int flags);
186 extern void hash_destroy(HTAB *hashp);
187 extern void hash_stats(const char *where, HTAB *hashp);
188 extern void *hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action,
190 extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
191 extern void *hash_seq_search(HASH_SEQ_STATUS *status);
192 extern long hash_estimate_size(long num_entries, Size entrysize);
193 extern long hash_select_dirsize(long num_entries);
196 * prototypes for functions in hashfn.c
198 extern uint32 string_hash(const void *key, Size keysize);
199 extern uint32 tag_hash(const void *key, Size keysize);
200 extern uint32 oid_hash(const void *key, Size keysize);
201 extern uint32 bitmap_hash(const void *key, Size keysize);
202 extern int bitmap_match(const void *key1, const void *key2, Size keysize);
204 #endif /* HSEARCH_H */