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