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