]> git.saurik.com Git - apple/libc.git/blob - db/hash/hash_page.c
a48fe48228f761a567d9389788d00b70b3f4b5b1
[apple/libc.git] / db / hash / hash_page.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*-
24 * Copyright (c) 1990, 1993, 1994
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.
57 */
58
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
63
64 /*
65 * PACKAGE: hashing
66 *
67 * DESCRIPTION:
68 * Page manipulation for hashing package.
69 *
70 * ROUTINES:
71 *
72 * External
73 * __get_page
74 * __add_ovflpage
75 * Internal
76 * overflow_page
77 * open_temp
78 */
79
80 #include <sys/types.h>
81
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <signal.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <unistd.h>
89 #ifdef DEBUG
90 #include <assert.h>
91 #endif
92
93 #include <db.h>
94 #include "hash.h"
95 #include "page.h"
96 #include "extern.h"
97
98 static u_int32_t *fetch_bitmap(HTAB *, int);
99 static u_int32_t first_free(u_int32_t);
100 static int open_temp(HTAB *);
101 static u_int16_t overflow_page(HTAB *);
102 static void putpair(char *, const DBT *, const DBT *);
103 static void squeeze_key(u_int16_t *, const DBT *, const DBT *);
104 static int ugly_split
105 (HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int);
106
107 #define PAGE_INIT(P) { \
108 ((u_int16_t *)(P))[0] = 0; \
109 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
110 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
111 }
112
113 /*
114 * This is called AFTER we have verified that there is room on the page for
115 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
116 * stuff on.
117 */
118 static void
119 putpair(p, key, val)
120 char *p;
121 const DBT *key, *val;
122 {
123 u_int16_t *bp, n, off;
124
125 bp = (u_int16_t *)p;
126
127 /* Enter the key first. */
128 n = bp[0];
129
130 off = OFFSET(bp) - key->size;
131 memmove(p + off, key->data, key->size);
132 bp[++n] = off;
133
134 /* Now the data. */
135 off -= val->size;
136 memmove(p + off, val->data, val->size);
137 bp[++n] = off;
138
139 /* Adjust page info. */
140 bp[0] = n;
141 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
142 bp[n + 2] = off;
143 }
144
145 /*
146 * Returns:
147 * 0 OK
148 * -1 error
149 */
150 extern int
151 __delpair(hashp, bufp, ndx)
152 HTAB *hashp;
153 BUFHEAD *bufp;
154 int ndx;
155 {
156 u_int16_t *bp, newoff;
157 int n;
158 u_int16_t pairlen;
159
160 bp = (u_int16_t *)bufp->page;
161 n = bp[0];
162
163 if (bp[ndx + 1] < REAL_KEY)
164 return (__big_delete(hashp, bufp));
165 if (ndx != 1)
166 newoff = bp[ndx - 1];
167 else
168 newoff = hashp->BSIZE;
169 pairlen = newoff - bp[ndx + 1];
170
171 if (ndx != (n - 1)) {
172 /* Hard Case -- need to shuffle keys */
173 int i;
174 char *src = bufp->page + (int)OFFSET(bp);
175 char *dst = src + (int)pairlen;
176 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
177
178 /* Now adjust the pointers */
179 for (i = ndx + 2; i <= n; i += 2) {
180 if (bp[i + 1] == OVFLPAGE) {
181 bp[i - 2] = bp[i];
182 bp[i - 1] = bp[i + 1];
183 } else {
184 bp[i - 2] = bp[i] + pairlen;
185 bp[i - 1] = bp[i + 1] + pairlen;
186 }
187 }
188 }
189 /* Finally adjust the page data */
190 bp[n] = OFFSET(bp) + pairlen;
191 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
192 bp[0] = n - 2;
193 hashp->NKEYS--;
194
195 bufp->flags |= BUF_MOD;
196 return (0);
197 }
198 /*
199 * Returns:
200 * 0 ==> OK
201 * -1 ==> Error
202 */
203 extern int
204 __split_page(hashp, obucket, nbucket)
205 HTAB *hashp;
206 u_int32_t obucket, nbucket;
207 {
208 BUFHEAD *new_bufp, *old_bufp;
209 u_int16_t *ino;
210 char *np;
211 DBT key, val;
212 int n, ndx, retval;
213 u_int16_t copyto, diff, off, moved;
214 char *op;
215
216 copyto = (u_int16_t)hashp->BSIZE;
217 off = (u_int16_t)hashp->BSIZE;
218 old_bufp = __get_buf(hashp, obucket, NULL, 0);
219 if (old_bufp == NULL)
220 return (-1);
221 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
222 if (new_bufp == NULL)
223 return (-1);
224
225 old_bufp->flags |= (BUF_MOD | BUF_PIN);
226 new_bufp->flags |= (BUF_MOD | BUF_PIN);
227
228 ino = (u_int16_t *)(op = old_bufp->page);
229 np = new_bufp->page;
230
231 moved = 0;
232
233 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
234 if (ino[n + 1] < REAL_KEY) {
235 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
236 (int)copyto, (int)moved);
237 old_bufp->flags &= ~BUF_PIN;
238 new_bufp->flags &= ~BUF_PIN;
239 return (retval);
240
241 }
242 key.data = (u_char *)op + ino[n];
243 key.size = off - ino[n];
244
245 if (__call_hash(hashp, key.data, key.size) == obucket) {
246 /* Don't switch page */
247 diff = copyto - off;
248 if (diff) {
249 copyto = ino[n + 1] + diff;
250 memmove(op + copyto, op + ino[n + 1],
251 off - ino[n + 1]);
252 ino[ndx] = copyto + ino[n] - ino[n + 1];
253 ino[ndx + 1] = copyto;
254 } else
255 copyto = ino[n + 1];
256 ndx += 2;
257 } else {
258 /* Switch page */
259 val.data = (u_char *)op + ino[n + 1];
260 val.size = ino[n] - ino[n + 1];
261 putpair(np, &key, &val);
262 moved += 2;
263 }
264
265 off = ino[n + 1];
266 }
267
268 /* Now clean up the page */
269 ino[0] -= moved;
270 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
271 OFFSET(ino) = copyto;
272
273 #ifdef DEBUG3
274 (void)fprintf(stderr, "split %d/%d\n",
275 ((u_int16_t *)np)[0] / 2,
276 ((u_int16_t *)op)[0] / 2);
277 #endif
278 /* unpin both pages */
279 old_bufp->flags &= ~BUF_PIN;
280 new_bufp->flags &= ~BUF_PIN;
281 return (0);
282 }
283
284 /*
285 * Called when we encounter an overflow or big key/data page during split
286 * handling. This is special cased since we have to begin checking whether
287 * the key/data pairs fit on their respective pages and because we may need
288 * overflow pages for both the old and new pages.
289 *
290 * The first page might be a page with regular key/data pairs in which case
291 * we have a regular overflow condition and just need to go on to the next
292 * page or it might be a big key/data pair in which case we need to fix the
293 * big key/data pair.
294 *
295 * Returns:
296 * 0 ==> success
297 * -1 ==> failure
298 */
299 static int
300 ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
301 HTAB *hashp;
302 u_int32_t obucket; /* Same as __split_page. */
303 BUFHEAD *old_bufp, *new_bufp;
304 int copyto; /* First byte on page which contains key/data values. */
305 int moved; /* Number of pairs moved to new page. */
306 {
307 BUFHEAD *bufp; /* Buffer header for ino */
308 u_int16_t *ino; /* Page keys come off of */
309 u_int16_t *np; /* New page */
310 u_int16_t *op; /* Page keys go on to if they aren't moving */
311
312 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
313 DBT key, val;
314 SPLIT_RETURN ret;
315 u_int16_t n, off, ov_addr, scopyto;
316 char *cino; /* Character value of ino */
317
318 bufp = old_bufp;
319 ino = (u_int16_t *)old_bufp->page;
320 np = (u_int16_t *)new_bufp->page;
321 op = (u_int16_t *)old_bufp->page;
322 last_bfp = NULL;
323 scopyto = (u_int16_t)copyto; /* ANSI */
324
325 n = ino[0] - 1;
326 while (n < ino[0]) {
327 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
328 if (__big_split(hashp, old_bufp,
329 new_bufp, bufp, bufp->addr, obucket, &ret))
330 return (-1);
331 old_bufp = ret.oldp;
332 if (!old_bufp)
333 return (-1);
334 op = (u_int16_t *)old_bufp->page;
335 new_bufp = ret.newp;
336 if (!new_bufp)
337 return (-1);
338 np = (u_int16_t *)new_bufp->page;
339 bufp = ret.nextp;
340 if (!bufp)
341 return (0);
342 cino = (char *)bufp->page;
343 ino = (u_int16_t *)cino;
344 last_bfp = ret.nextp;
345 } else if (ino[n + 1] == OVFLPAGE) {
346 ov_addr = ino[n];
347 /*
348 * Fix up the old page -- the extra 2 are the fields
349 * which contained the overflow information.
350 */
351 ino[0] -= (moved + 2);
352 FREESPACE(ino) =
353 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
354 OFFSET(ino) = scopyto;
355
356 bufp = __get_buf(hashp, ov_addr, bufp, 0);
357 if (!bufp)
358 return (-1);
359
360 ino = (u_int16_t *)bufp->page;
361 n = 1;
362 scopyto = hashp->BSIZE;
363 moved = 0;
364
365 if (last_bfp)
366 __free_ovflpage(hashp, last_bfp);
367 last_bfp = bufp;
368 }
369 /* Move regular sized pairs of there are any */
370 off = hashp->BSIZE;
371 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
372 cino = (char *)ino;
373 key.data = (u_char *)cino + ino[n];
374 key.size = off - ino[n];
375 val.data = (u_char *)cino + ino[n + 1];
376 val.size = ino[n] - ino[n + 1];
377 off = ino[n + 1];
378
379 if (__call_hash(hashp, key.data, key.size) == obucket) {
380 /* Keep on old page */
381 if (PAIRFITS(op, (&key), (&val)))
382 putpair((char *)op, &key, &val);
383 else {
384 old_bufp =
385 __add_ovflpage(hashp, old_bufp);
386 if (!old_bufp)
387 return (-1);
388 op = (u_int16_t *)old_bufp->page;
389 putpair((char *)op, &key, &val);
390 }
391 old_bufp->flags |= BUF_MOD;
392 } else {
393 /* Move to new page */
394 if (PAIRFITS(np, (&key), (&val)))
395 putpair((char *)np, &key, &val);
396 else {
397 new_bufp =
398 __add_ovflpage(hashp, new_bufp);
399 if (!new_bufp)
400 return (-1);
401 np = (u_int16_t *)new_bufp->page;
402 putpair((char *)np, &key, &val);
403 }
404 new_bufp->flags |= BUF_MOD;
405 }
406 }
407 }
408 if (last_bfp)
409 __free_ovflpage(hashp, last_bfp);
410 return (0);
411 }
412
413 /*
414 * Add the given pair to the page
415 *
416 * Returns:
417 * 0 ==> OK
418 * 1 ==> failure
419 */
420 extern int
421 __addel(hashp, bufp, key, val)
422 HTAB *hashp;
423 BUFHEAD *bufp;
424 const DBT *key, *val;
425 {
426 u_int16_t *bp, *sop;
427 int do_expand;
428
429 bp = (u_int16_t *)bufp->page;
430 do_expand = 0;
431 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
432 /* Exception case */
433 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
434 /* This is the last page of a big key/data pair
435 and we need to add another page */
436 break;
437 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
438 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
439 if (!bufp)
440 return (-1);
441 bp = (u_int16_t *)bufp->page;
442 } else
443 /* Try to squeeze key on this page */
444 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
445 squeeze_key(bp, key, val);
446 return (0);
447 } else {
448 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
449 if (!bufp)
450 return (-1);
451 bp = (u_int16_t *)bufp->page;
452 }
453
454 if (PAIRFITS(bp, key, val))
455 putpair(bufp->page, key, val);
456 else {
457 do_expand = 1;
458 bufp = __add_ovflpage(hashp, bufp);
459 if (!bufp)
460 return (-1);
461 sop = (u_int16_t *)bufp->page;
462
463 if (PAIRFITS(sop, key, val))
464 putpair((char *)sop, key, val);
465 else
466 if (__big_insert(hashp, bufp, key, val))
467 return (-1);
468 }
469 bufp->flags |= BUF_MOD;
470 /*
471 * If the average number of keys per bucket exceeds the fill factor,
472 * expand the table.
473 */
474 hashp->NKEYS++;
475 if (do_expand ||
476 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
477 return (__expand_table(hashp));
478 return (0);
479 }
480
481 /*
482 *
483 * Returns:
484 * pointer on success
485 * NULL on error
486 */
487 extern BUFHEAD *
488 __add_ovflpage(hashp, bufp)
489 HTAB *hashp;
490 BUFHEAD *bufp;
491 {
492 u_int16_t *sp;
493 u_int16_t ndx, ovfl_num;
494 #ifdef DEBUG1
495 int tmp1, tmp2;
496 #endif
497 sp = (u_int16_t *)bufp->page;
498
499 /* Check if we are dynamically determining the fill factor */
500 if (hashp->FFACTOR == DEF_FFACTOR) {
501 hashp->FFACTOR = sp[0] >> 1;
502 if (hashp->FFACTOR < MIN_FFACTOR)
503 hashp->FFACTOR = MIN_FFACTOR;
504 }
505 bufp->flags |= BUF_MOD;
506 ovfl_num = overflow_page(hashp);
507 #ifdef DEBUG1
508 tmp1 = bufp->addr;
509 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
510 #endif
511 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
512 return (NULL);
513 bufp->ovfl->flags |= BUF_MOD;
514 #ifdef DEBUG1
515 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
516 tmp1, tmp2, bufp->ovfl->addr);
517 #endif
518 ndx = sp[0];
519 /*
520 * Since a pair is allocated on a page only if there's room to add
521 * an overflow page, we know that the OVFL information will fit on
522 * the page.
523 */
524 sp[ndx + 4] = OFFSET(sp);
525 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
526 sp[ndx + 1] = ovfl_num;
527 sp[ndx + 2] = OVFLPAGE;
528 sp[0] = ndx + 2;
529 #ifdef HASH_STATISTICS
530 hash_overflows++;
531 #endif
532 return (bufp->ovfl);
533 }
534
535 /*
536 * Returns:
537 * 0 indicates SUCCESS
538 * -1 indicates FAILURE
539 */
540 extern int
541 __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
542 HTAB *hashp;
543 char *p;
544 u_int32_t bucket;
545 int is_bucket, is_disk, is_bitmap;
546 {
547 int fd, page, size;
548 int rsize;
549 u_int16_t *bp;
550
551 fd = hashp->fp;
552 size = hashp->BSIZE;
553
554 if ((fd == -1) || !is_disk) {
555 PAGE_INIT(p);
556 return (0);
557 }
558 if (is_bucket)
559 page = BUCKET_TO_PAGE(bucket);
560 else
561 page = OADDR_TO_PAGE(bucket);
562 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
563 ((rsize = read(fd, p, size)) == -1))
564 return (-1);
565 bp = (u_int16_t *)p;
566 if (!rsize)
567 bp[0] = 0; /* We hit the EOF, so initialize a new page */
568 else
569 if (rsize != size) {
570 errno = EFTYPE;
571 return (-1);
572 }
573 if (!is_bitmap && !bp[0]) {
574 PAGE_INIT(p);
575 } else
576 if (hashp->LORDER != BYTE_ORDER) {
577 int i, max;
578
579 if (is_bitmap) {
580 max = hashp->BSIZE >> 2; /* divide by 4 */
581 for (i = 0; i < max; i++)
582 M_32_SWAP(((int *)p)[i]);
583 } else {
584 M_16_SWAP(bp[0]);
585 max = bp[0] + 2;
586 for (i = 1; i <= max; i++)
587 M_16_SWAP(bp[i]);
588 }
589 }
590 return (0);
591 }
592
593 /*
594 * Write page p to disk
595 *
596 * Returns:
597 * 0 ==> OK
598 * -1 ==>failure
599 */
600 extern int
601 __put_page(hashp, p, bucket, is_bucket, is_bitmap)
602 HTAB *hashp;
603 char *p;
604 u_int32_t bucket;
605 int is_bucket, is_bitmap;
606 {
607 int fd, page, size;
608 int wsize;
609
610 size = hashp->BSIZE;
611 if ((hashp->fp == -1) && open_temp(hashp))
612 return (-1);
613 fd = hashp->fp;
614
615 if (hashp->LORDER != BYTE_ORDER) {
616 int i;
617 int max;
618
619 if (is_bitmap) {
620 max = hashp->BSIZE >> 2; /* divide by 4 */
621 for (i = 0; i < max; i++)
622 M_32_SWAP(((int *)p)[i]);
623 } else {
624 max = ((u_int16_t *)p)[0] + 2;
625 for (i = 0; i <= max; i++)
626 M_16_SWAP(((u_int16_t *)p)[i]);
627 }
628 }
629 if (is_bucket)
630 page = BUCKET_TO_PAGE(bucket);
631 else
632 page = OADDR_TO_PAGE(bucket);
633 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
634 ((wsize = write(fd, p, size)) == -1))
635 /* Errno is set */
636 return (-1);
637 if (wsize != size) {
638 errno = EFTYPE;
639 return (-1);
640 }
641 return (0);
642 }
643
644 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
645 /*
646 * Initialize a new bitmap page. Bitmap pages are left in memory
647 * once they are read in.
648 */
649 extern int
650 __ibitmap(hashp, pnum, nbits, ndx)
651 HTAB *hashp;
652 int pnum, nbits, ndx;
653 {
654 u_int32_t *ip;
655 int clearbytes, clearints;
656
657 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
658 return (1);
659 hashp->nmaps++;
660 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
661 clearbytes = clearints << INT_TO_BYTE;
662 (void)memset((char *)ip, 0, clearbytes);
663 (void)memset(((char *)ip) + clearbytes, 0xFF,
664 hashp->BSIZE - clearbytes);
665 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
666 SETBIT(ip, 0);
667 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
668 hashp->mapp[ndx] = ip;
669 return (0);
670 }
671
672 static u_int32_t
673 first_free(map)
674 u_int32_t map;
675 {
676 u_int32_t i, mask;
677
678 mask = 0x1;
679 for (i = 0; i < BITS_PER_MAP; i++) {
680 if (!(mask & map))
681 return (i);
682 mask = mask << 1;
683 }
684 return (i);
685 }
686
687 static u_int16_t
688 overflow_page(hashp)
689 HTAB *hashp;
690 {
691 u_int32_t *freep;
692 int max_free, offset, splitnum;
693 u_int16_t addr;
694 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
695 #ifdef DEBUG2
696 int tmp1, tmp2;
697 #endif
698 splitnum = hashp->OVFL_POINT;
699 max_free = hashp->SPARES[splitnum];
700
701 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
702 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
703
704 /* Look through all the free maps to find the first free block */
705 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
706 for ( i = first_page; i <= free_page; i++ ) {
707 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
708 !(freep = fetch_bitmap(hashp, i)))
709 return (0);
710 if (i == free_page)
711 in_use_bits = free_bit;
712 else
713 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
714
715 if (i == first_page) {
716 bit = hashp->LAST_FREED &
717 ((hashp->BSIZE << BYTE_SHIFT) - 1);
718 j = bit / BITS_PER_MAP;
719 bit = bit & ~(BITS_PER_MAP - 1);
720 } else {
721 bit = 0;
722 j = 0;
723 }
724 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
725 if (freep[j] != ALL_SET)
726 goto found;
727 }
728
729 /* No Free Page Found */
730 hashp->LAST_FREED = hashp->SPARES[splitnum];
731 hashp->SPARES[splitnum]++;
732 offset = hashp->SPARES[splitnum] -
733 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
734
735 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
736 if (offset > SPLITMASK) {
737 if (++splitnum >= NCACHED) {
738 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
739 return (0);
740 }
741 hashp->OVFL_POINT = splitnum;
742 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
743 hashp->SPARES[splitnum-1]--;
744 offset = 1;
745 }
746
747 /* Check if we need to allocate a new bitmap page */
748 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
749 free_page++;
750 if (free_page >= NCACHED) {
751 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
752 return (0);
753 }
754 /*
755 * This is tricky. The 1 indicates that you want the new page
756 * allocated with 1 clear bit. Actually, you are going to
757 * allocate 2 pages from this map. The first is going to be
758 * the map page, the second is the overflow page we were
759 * looking for. The init_bitmap routine automatically, sets
760 * the first bit of itself to indicate that the bitmap itself
761 * is in use. We would explicitly set the second bit, but
762 * don't have to if we tell init_bitmap not to leave it clear
763 * in the first place.
764 */
765 if (__ibitmap(hashp,
766 (int)OADDR_OF(splitnum, offset), 1, free_page))
767 return (0);
768 hashp->SPARES[splitnum]++;
769 #ifdef DEBUG2
770 free_bit = 2;
771 #endif
772 offset++;
773 if (offset > SPLITMASK) {
774 if (++splitnum >= NCACHED) {
775 (void)write(STDERR_FILENO, OVMSG,
776 sizeof(OVMSG) - 1);
777 return (0);
778 }
779 hashp->OVFL_POINT = splitnum;
780 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
781 hashp->SPARES[splitnum-1]--;
782 offset = 0;
783 }
784 } else {
785 /*
786 * Free_bit addresses the last used bit. Bump it to address
787 * the first available bit.
788 */
789 free_bit++;
790 SETBIT(freep, free_bit);
791 }
792
793 /* Calculate address of the new overflow page */
794 addr = OADDR_OF(splitnum, offset);
795 #ifdef DEBUG2
796 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
797 addr, free_bit, free_page);
798 #endif
799 return (addr);
800
801 found:
802 bit = bit + first_free(freep[j]);
803 SETBIT(freep, bit);
804 #ifdef DEBUG2
805 tmp1 = bit;
806 tmp2 = i;
807 #endif
808 /*
809 * Bits are addressed starting with 0, but overflow pages are addressed
810 * beginning at 1. Bit is a bit addressnumber, so we need to increment
811 * it to convert it to a page number.
812 */
813 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
814 if (bit >= hashp->LAST_FREED)
815 hashp->LAST_FREED = bit - 1;
816
817 /* Calculate the split number for this page */
818 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
819 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
820 if (offset >= SPLITMASK)
821 return (0); /* Out of overflow pages */
822 addr = OADDR_OF(i, offset);
823 #ifdef DEBUG2
824 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
825 addr, tmp1, tmp2);
826 #endif
827
828 /* Allocate and return the overflow page */
829 return (addr);
830 }
831
832 /*
833 * Mark this overflow page as free.
834 */
835 extern void
836 __free_ovflpage(hashp, obufp)
837 HTAB *hashp;
838 BUFHEAD *obufp;
839 {
840 u_int16_t addr;
841 u_int32_t *freep;
842 int bit_address, free_page, free_bit;
843 u_int16_t ndx;
844
845 addr = obufp->addr;
846 #ifdef DEBUG1
847 (void)fprintf(stderr, "Freeing %d\n", addr);
848 #endif
849 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
850 bit_address =
851 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
852 if (bit_address < hashp->LAST_FREED)
853 hashp->LAST_FREED = bit_address;
854 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
855 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
856
857 if (!(freep = hashp->mapp[free_page]))
858 freep = fetch_bitmap(hashp, free_page);
859 #ifdef DEBUG
860 /*
861 * This had better never happen. It means we tried to read a bitmap
862 * that has already had overflow pages allocated off it, and we
863 * failed to read it from the file.
864 */
865 if (!freep)
866 assert(0);
867 #endif
868 CLRBIT(freep, free_bit);
869 #ifdef DEBUG2
870 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
871 obufp->addr, free_bit, free_page);
872 #endif
873 __reclaim_buf(hashp, obufp);
874 }
875
876 /*
877 * Returns:
878 * 0 success
879 * -1 failure
880 */
881 static int
882 open_temp(hashp)
883 HTAB *hashp;
884 {
885 sigset_t set, oset;
886 static char namestr[] = "_hashXXXXXX";
887
888 /* Block signals; make sure file goes away at process exit. */
889 (void)sigfillset(&set);
890 (void)sigprocmask(SIG_BLOCK, &set, &oset);
891 if ((hashp->fp = mkstemp(namestr)) != -1) {
892 (void)unlink(namestr);
893 (void)fcntl(hashp->fp, F_SETFD, 1);
894 }
895 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
896 return (hashp->fp != -1 ? 0 : -1);
897 }
898
899 /*
900 * We have to know that the key will fit, but the last entry on the page is
901 * an overflow pair, so we need to shift things.
902 */
903 static void
904 squeeze_key(sp, key, val)
905 u_int16_t *sp;
906 const DBT *key, *val;
907 {
908 char *p;
909 u_int16_t free_space, n, off, pageno;
910
911 p = (char *)sp;
912 n = sp[0];
913 free_space = FREESPACE(sp);
914 off = OFFSET(sp);
915
916 pageno = sp[n - 1];
917 off -= key->size;
918 sp[n - 1] = off;
919 memmove(p + off, key->data, key->size);
920 off -= val->size;
921 sp[n] = off;
922 memmove(p + off, val->data, val->size);
923 sp[0] = n + 2;
924 sp[n + 1] = pageno;
925 sp[n + 2] = OVFLPAGE;
926 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
927 OFFSET(sp) = off;
928 }
929
930 static u_int32_t *
931 fetch_bitmap(hashp, ndx)
932 HTAB *hashp;
933 int ndx;
934 {
935 if (ndx >= hashp->nmaps)
936 return (NULL);
937 if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
938 return (NULL);
939 if (__get_page(hashp,
940 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
941 free(hashp->mapp[ndx]);
942 return (NULL);
943 }
944 return (hashp->mapp[ndx]);
945 }
946
947 #ifdef DEBUG4
948 int
949 print_chain(addr)
950 int addr;
951 {
952 BUFHEAD *bufp;
953 short *bp, oaddr;
954
955 (void)fprintf(stderr, "%d ", addr);
956 bufp = __get_buf(hashp, addr, NULL, 0);
957 bp = (short *)bufp->page;
958 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
959 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
960 oaddr = bp[bp[0] - 1];
961 (void)fprintf(stderr, "%d ", (int)oaddr);
962 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
963 bp = (short *)bufp->page;
964 }
965 (void)fprintf(stderr, "\n");
966 }
967 #endif