]> git.saurik.com Git - apple/libc.git/blame - db/hash/hash.h
Libc-498.1.5.tar.gz
[apple/libc.git] / db / hash / hash.h
CommitLineData
224c7076
A
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Margo Seltzer.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)hash.h 8.3 (Berkeley) 5/31/94
37 * $FreeBSD: src/lib/libc/db/hash/hash.h,v 1.6 2002/03/21 22:46:26 obrien Exp $
38 */
39
40/* Operations */
41typedef enum {
42 HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
43} ACTION;
44
45/* Buffer Management structures */
46typedef struct _bufhead BUFHEAD;
47
48struct _bufhead {
49 BUFHEAD *prev; /* LRU links */
50 BUFHEAD *next; /* LRU links */
51 BUFHEAD *ovfl; /* Overflow page buffer header */
52 u_int32_t addr; /* Address of this page */
53 char *page; /* Actual page data */
54 char flags;
55#define BUF_MOD 0x0001
56#define BUF_DISK 0x0002
57#define BUF_BUCKET 0x0004
58#define BUF_PIN 0x0008
59};
60
61#define IS_BUCKET(X) ((X) & BUF_BUCKET)
62
63typedef BUFHEAD **SEGMENT;
64
65/* Hash Table Information */
66typedef struct hashhdr { /* Disk resident portion */
67 int magic; /* Magic NO for hash tables */
68 int version; /* Version ID */
69 u_int32_t lorder; /* Byte Order */
70 int bsize; /* Bucket/Page Size */
71 int bshift; /* Bucket shift */
72 int dsize; /* Directory Size */
73 int ssize; /* Segment Size */
74 int sshift; /* Segment shift */
75 int ovfl_point; /* Where overflow pages are being
76 * allocated */
77 int last_freed; /* Last overflow page freed */
78 int max_bucket; /* ID of Maximum bucket in use */
79 int high_mask; /* Mask to modulo into entire table */
80 int low_mask; /* Mask to modulo into lower half of
81 * table */
82 int ffactor; /* Fill factor */
83 int nkeys; /* Number of keys in hash table */
84 int hdrpages; /* Size of table header */
85 int h_charkey; /* value of hash(CHARKEY) */
86#define NCACHED 32 /* number of bit maps and spare
87 * points */
88 int spares[NCACHED];/* spare pages for overflow */
89 u_int16_t bitmaps[NCACHED]; /* address of overflow page
90 * bitmaps */
91} HASHHDR;
92
93typedef struct htab { /* Memory resident data structure */
94 HASHHDR hdr; /* Header */
95 int nsegs; /* Number of allocated segments */
96 int exsegs; /* Number of extra allocated
97 * segments */
98 u_int32_t /* Hash function */
99 (*hash)(const void *, size_t);
100 int flags; /* Flag values */
101 int fp; /* File pointer */
102 char *tmp_buf; /* Temporary Buffer for BIG data */
103 char *tmp_key; /* Temporary Buffer for BIG keys */
104 BUFHEAD *cpage; /* Current page */
105 int cbucket; /* Current bucket */
106 int cndx; /* Index of next item on cpage */
107 int error; /* Error Number -- for DBM
108 * compatibility */
109 int new_file; /* Indicates if fd is backing store
110 * or no */
111 int save_file; /* Indicates whether we need to flush
112 * file at
113 * exit */
114 u_int32_t *mapp[NCACHED]; /* Pointers to page maps */
115 int nmaps; /* Initial number of bitmaps */
116 int nbufs; /* Number of buffers left to
117 * allocate */
118 BUFHEAD bufhead; /* Header of buffer lru list */
119 SEGMENT *dir; /* Hash Bucket directory */
120 /* other flags */
121 int nextkey_eof :1; /* dbm_nextkey() reached EOF */
122} HTAB;
123
124/*
125 * Constants
126 */
127#define MAX_BSIZE 65536 /* 2^16 */
128#define MIN_BUFFERS 6
129#define MINHDRSIZE 512
130#define DEF_BUFSIZE 65536 /* 64 K */
131#define DEF_BUCKET_SIZE 4096
132#define DEF_BUCKET_SHIFT 12 /* log2(BUCKET) */
133#define DEF_SEGSIZE 256
134#define DEF_SEGSIZE_SHIFT 8 /* log2(SEGSIZE) */
135#define DEF_DIRSIZE 256
136#define DEF_FFACTOR 65536
137#define MIN_FFACTOR 4
138#define SPLTMAX 8
139#define CHARKEY "%$sniglet^&"
140#define NUMKEY 1038583
141#define BYTE_SHIFT 3
142#define INT_TO_BYTE 2
143#define INT_BYTE_SHIFT 5
144#define ALL_SET ((u_int32_t)0xFFFFFFFF)
145#define ALL_CLEAR 0
146
147#define PTROF(X) ((BUFHEAD *)((ptrdiff_t)(X)&~0x3))
148#define ISMOD(X) ((u_int32_t)(ptrdiff_t)(X)&0x1)
149#define DOMOD(X) ((X) = (char *)((ptrdiff_t)(X)|0x1))
150#define ISDISK(X) ((u_int32_t)(ptrdiff_t)(X)&0x2)
151#define DODISK(X) ((X) = (char *)((ptrdiff_t)(X)|0x2))
152
153#define BITS_PER_MAP 32
154
155/* Given the address of the beginning of a big map, clear/set the nth bit */
156#define CLRBIT(A, N) ((A)[(N)/BITS_PER_MAP] &= ~(1<<((N)%BITS_PER_MAP)))
157#define SETBIT(A, N) ((A)[(N)/BITS_PER_MAP] |= (1<<((N)%BITS_PER_MAP)))
158#define ISSET(A, N) ((A)[(N)/BITS_PER_MAP] & (1<<((N)%BITS_PER_MAP)))
159
160/* Overflow management */
161/*
162 * Overflow page numbers are allocated per split point. At each doubling of
163 * the table, we can allocate extra pages. So, an overflow page number has
164 * the top 5 bits indicate which split point and the lower 11 bits indicate
165 * which page at that split point is indicated (pages within split points are
166 * numberered starting with 1).
167 */
168
169#define SPLITSHIFT 11
170#define SPLITMASK 0x7FF
171#define SPLITNUM(N) (((u_int32_t)(N)) >> SPLITSHIFT)
172#define OPAGENUM(N) ((N) & SPLITMASK)
173#define OADDR_OF(S,O) ((u_int32_t)((u_int32_t)(S) << SPLITSHIFT) + (O))
174
175#define BUCKET_TO_PAGE(B) \
176 (B) + hashp->HDRPAGES + ((B) ? hashp->SPARES[__log2((B)+1)-1] : 0)
177#define OADDR_TO_PAGE(B) \
178 BUCKET_TO_PAGE ( (1 << SPLITNUM((B))) -1 ) + OPAGENUM((B));
179
180/*
181 * page.h contains a detailed description of the page format.
182 *
183 * Normally, keys and data are accessed from offset tables in the top of
184 * each page which point to the beginning of the key and data. There are
185 * four flag values which may be stored in these offset tables which indicate
186 * the following:
187 *
188 *
189 * OVFLPAGE Rather than a key data pair, this pair contains
190 * the address of an overflow page. The format of
191 * the pair is:
192 * OVERFLOW_PAGE_NUMBER OVFLPAGE
193 *
194 * PARTIAL_KEY This must be the first key/data pair on a page
195 * and implies that page contains only a partial key.
196 * That is, the key is too big to fit on a single page
197 * so it starts on this page and continues on the next.
198 * The format of the page is:
199 * KEY_OFF PARTIAL_KEY OVFL_PAGENO OVFLPAGE
200 *
201 * KEY_OFF -- offset of the beginning of the key
202 * PARTIAL_KEY -- 1
203 * OVFL_PAGENO - page number of the next overflow page
204 * OVFLPAGE -- 0
205 *
206 * FULL_KEY This must be the first key/data pair on the page. It
207 * is used in two cases.
208 *
209 * Case 1:
210 * There is a complete key on the page but no data
211 * (because it wouldn't fit). The next page contains
212 * the data.
213 *
214 * Page format it:
215 * KEY_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
216 *
217 * KEY_OFF -- offset of the beginning of the key
218 * FULL_KEY -- 2
219 * OVFL_PAGENO - page number of the next overflow page
220 * OVFLPAGE -- 0
221 *
222 * Case 2:
223 * This page contains no key, but part of a large
224 * data field, which is continued on the next page.
225 *
226 * Page format it:
227 * DATA_OFF FULL_KEY OVFL_PAGENO OVFL_PAGE
228 *
229 * KEY_OFF -- offset of the beginning of the data on
230 * this page
231 * FULL_KEY -- 2
232 * OVFL_PAGENO - page number of the next overflow page
233 * OVFLPAGE -- 0
234 *
235 * FULL_KEY_DATA
236 * This must be the first key/data pair on the page.
237 * There are two cases:
238 *
239 * Case 1:
240 * This page contains a key and the beginning of the
241 * data field, but the data field is continued on the
242 * next page.
243 *
244 * Page format is:
245 * KEY_OFF FULL_KEY_DATA OVFL_PAGENO DATA_OFF
246 *
247 * KEY_OFF -- offset of the beginning of the key
248 * FULL_KEY_DATA -- 3
249 * OVFL_PAGENO - page number of the next overflow page
250 * DATA_OFF -- offset of the beginning of the data
251 *
252 * Case 2:
253 * This page contains the last page of a big data pair.
254 * There is no key, only the tail end of the data
255 * on this page.
256 *
257 * Page format is:
258 * DATA_OFF FULL_KEY_DATA <OVFL_PAGENO> <OVFLPAGE>
259 *
260 * DATA_OFF -- offset of the beginning of the data on
261 * this page
262 * FULL_KEY_DATA -- 3
263 * OVFL_PAGENO - page number of the next overflow page
264 * OVFLPAGE -- 0
265 *
266 * OVFL_PAGENO and OVFLPAGE are optional (they are
267 * not present if there is no next page).
268 */
269
270#define OVFLPAGE 0
271#define PARTIAL_KEY 1
272#define FULL_KEY 2
273#define FULL_KEY_DATA 3
274#define REAL_KEY 4
275
276/* Short hands for accessing structure */
277#define BSIZE hdr.bsize
278#define BSHIFT hdr.bshift
279#define DSIZE hdr.dsize
280#define SGSIZE hdr.ssize
281#define SSHIFT hdr.sshift
282#define LORDER hdr.lorder
283#define OVFL_POINT hdr.ovfl_point
284#define LAST_FREED hdr.last_freed
285#define MAX_BUCKET hdr.max_bucket
286#define FFACTOR hdr.ffactor
287#define HIGH_MASK hdr.high_mask
288#define LOW_MASK hdr.low_mask
289#define NKEYS hdr.nkeys
290#define HDRPAGES hdr.hdrpages
291#define SPARES hdr.spares
292#define BITMAPS hdr.bitmaps
293#define VERSION hdr.version
294#define MAGIC hdr.magic
295#define NEXT_FREE hdr.next_free
296#define H_CHARKEY hdr.h_charkey