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