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