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