andre@3: /*-
andre@3:  * Copyright (c) 1990, 1993, 1994
andre@3:  *	The Regents of the University of California.  All rights reserved.
andre@3:  *
andre@3:  * This code is derived from software contributed to Berkeley by
andre@3:  * Margo Seltzer.
andre@3:  *
andre@3:  * Redistribution and use in source and binary forms, with or without
andre@3:  * modification, are permitted provided that the following conditions
andre@3:  * are met:
andre@3:  * 1. Redistributions of source code must retain the above copyright
andre@3:  *    notice, this list of conditions and the following disclaimer.
andre@3:  * 2. Redistributions in binary form must reproduce the above copyright
andre@3:  *    notice, this list of conditions and the following disclaimer in the
andre@3:  *    documentation and/or other materials provided with the distribution.
andre@3:  * 3. ***REMOVED*** - see 
andre@3:  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
andre@3:  * 4. Neither the name of the University nor the names of its contributors
andre@3:  *    may be used to endorse or promote products derived from this software
andre@3:  *    without specific prior written permission.
andre@3:  *
andre@3:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
andre@3:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
andre@3:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
andre@3:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
andre@3:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
andre@3:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
andre@3:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
andre@3:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
andre@3:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
andre@3:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
andre@3:  * SUCH DAMAGE.
andre@3:  */
andre@3: 
andre@3: #if defined(unix)
andre@3: #define MY_LSEEK lseek
andre@3: #else
andre@3: #define MY_LSEEK new_lseek
andre@3: extern long new_lseek(int fd, long pos, int start);
andre@3: #endif
andre@3: 
andre@3: #if defined(LIBC_SCCS) && !defined(lint)
andre@3: static char sccsid[] = "@(#)hash_page.c	8.7 (Berkeley) 8/16/94";
andre@3: #endif /* LIBC_SCCS and not lint */
andre@3: 
andre@3: /*
andre@3:  * PACKAGE:  hashing
andre@3:  *
andre@3:  * DESCRIPTION:
andre@3:  *	Page manipulation for hashing package.
andre@3:  *
andre@3:  * ROUTINES:
andre@3:  *
andre@3:  * External
andre@3:  *	__get_page
andre@3:  *	__add_ovflpage
andre@3:  * Internal
andre@3:  *	overflow_page
andre@3:  *	open_temp
andre@3:  */
andre@3: #ifndef macintosh
andre@3: #include <sys/types.h>
andre@3: #endif
andre@3: 
andre@3: #if defined(macintosh)
andre@3: #include <unistd.h>
andre@3: #endif
andre@3: 
andre@3: #include <errno.h>
andre@3: #include <fcntl.h>
andre@3: #if defined(_WIN32) || defined(_WINDOWS) 
andre@3: #include <io.h>
andre@3: #endif
andre@3: #include <signal.h>
andre@3: #include <stdio.h>
andre@3: #include <stdlib.h>
andre@3: #include <string.h>
andre@3: 
andre@3: #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
andre@3: #include <unistd.h>
andre@3: #endif
andre@3: 
andre@3: #include <assert.h>
andre@3: 
andre@3: #include "mcom_db.h"
andre@3: #include "hash.h"
andre@3: #include "page.h"
andre@3: /* #include "extern.h" */
andre@3: 
andre@3: extern int mkstempflags(char *path, int extraFlags);
andre@3: 
andre@3: static uint32	*fetch_bitmap __P((HTAB *, uint32));
andre@3: static uint32	 first_free __P((uint32));
andre@3: static int	 open_temp __P((HTAB *));
andre@3: static uint16	 overflow_page __P((HTAB *));
andre@3: static void	 squeeze_key __P((uint16 *, const DBT *, const DBT *));
andre@3: static int	 ugly_split
andre@3: 		    __P((HTAB *, uint32, BUFHEAD *, BUFHEAD *, int, int));
andre@3: 
andre@3: #define	PAGE_INIT(P) { \
andre@3: 	((uint16 *)(P))[0] = 0; \
andre@3: 	((uint16 *)(P))[1] = hashp->BSIZE - 3 * sizeof(uint16); \
andre@3: 	((uint16 *)(P))[2] = hashp->BSIZE; \
andre@3: }
andre@3: 
andre@3: /* implement a new lseek using lseek that
andre@3:  * writes zero's when extending a file
andre@3:  * beyond the end.
andre@3:  */
andre@3: long new_lseek(int fd, long offset, int origin)
andre@3: {
andre@3:  	long cur_pos=0;
andre@3: 	long end_pos=0;
andre@3: 	long seek_pos=0;
andre@3: 
andre@3: 	if(origin == SEEK_CUR)
andre@3:       {	
andre@3:       	if(offset < 1)							  
andre@3: 	    	return(lseek(fd, offset, SEEK_CUR));
andre@3: 
andre@3: 		cur_pos = lseek(fd, 0, SEEK_CUR);
andre@3: 
andre@3: 		if(cur_pos < 0)
andre@3: 			return(cur_pos);
andre@3: 	  }
andre@3: 										 
andre@3: 	end_pos = lseek(fd, 0, SEEK_END);
andre@3: 	if(end_pos < 0)
andre@3: 		return(end_pos);
andre@3: 
andre@3: 	if(origin == SEEK_SET)
andre@3: 		seek_pos = offset;
andre@3: 	else if(origin == SEEK_CUR)
andre@3: 		seek_pos = cur_pos + offset;
andre@3: 	else if(origin == SEEK_END)
andre@3: 		seek_pos = end_pos + offset;
andre@3:  	else
andre@3: 	  {
andre@3: 	  	assert(0);
andre@3: 		return(-1);
andre@3: 	  }
andre@3: 
andre@3:  	/* the seek position desired is before the
andre@3: 	 * end of the file.  We don't need
andre@3: 	 * to do anything special except the seek.
andre@3: 	 */
andre@3:  	if(seek_pos <= end_pos)
andre@3:  		return(lseek(fd, seek_pos, SEEK_SET));
andre@3:  		
andre@3:  	  /* the seek position is beyond the end of the
andre@3:  	   * file.  Write zero's to the end.
andre@3:  	   *
andre@3: 	   * we are already at the end of the file so
andre@3: 	   * we just need to "write()" zeros for the
andre@3: 	   * difference between seek_pos-end_pos and
andre@3: 	   * then seek to the position to finish
andre@3: 	   * the call
andre@3:  	   */
andre@3:  	  { 
andre@3:  	 	char buffer[1024];
andre@3: 	   	long len = seek_pos-end_pos;
andre@3: 	   	memset(&buffer, 0, 1024);
andre@3: 	   	while(len > 0)
andre@3: 	      {
andre@3: 	        write(fd, (char*)&buffer, (size_t)(1024 > len ? len : 1024));
andre@3: 		    len -= 1024;
andre@3: 		  }
andre@3: 		return(lseek(fd, seek_pos, SEEK_SET));
andre@3: 	  }		
andre@3: 
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * This is called AFTER we have verified that there is room on the page for
andre@3:  * the pair (PAIRFITS has returned true) so we go right ahead and start moving
andre@3:  * stuff on.
andre@3:  */
andre@3: static void
andre@3: putpair(char *p, const DBT *key, DBT * val)
andre@3: {
andre@3: 	register uint16 *bp, n, off;
andre@3: 
andre@3: 	bp = (uint16 *)p;
andre@3: 
andre@3: 	/* Enter the key first. */
andre@3: 	n = bp[0];
andre@3: 
andre@3: 	off = OFFSET(bp) - key->size;
andre@3: 	memmove(p + off, key->data, key->size);
andre@3: 	bp[++n] = off;
andre@3: 
andre@3: 	/* Now the data. */
andre@3: 	off -= val->size;
andre@3: 	memmove(p + off, val->data, val->size);
andre@3: 	bp[++n] = off;
andre@3: 
andre@3: 	/* Adjust page info. */
andre@3: 	bp[0] = n;
andre@3: 	bp[n + 1] = off - ((n + 3) * sizeof(uint16));
andre@3: 	bp[n + 2] = off;
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Returns:
andre@3:  *	 0 OK
andre@3:  *	-1 error
andre@3:  */
andre@3: extern int
andre@3: __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
andre@3: {
andre@3: 	register uint16 *bp, newoff;
andre@3: 	register int n;
andre@3: 	uint16 pairlen;
andre@3: 
andre@3: 	bp = (uint16 *)bufp->page;
andre@3: 	n = bp[0];
andre@3: 
andre@3: 	if (bp[ndx + 1] < REAL_KEY)
andre@3: 		return (__big_delete(hashp, bufp));
andre@3: 	if (ndx != 1)
andre@3: 		newoff = bp[ndx - 1];
andre@3: 	else
andre@3: 		newoff = hashp->BSIZE;
andre@3: 	pairlen = newoff - bp[ndx + 1];
andre@3: 
andre@3: 	if (ndx != (n - 1)) {
andre@3: 		/* Hard Case -- need to shuffle keys */
andre@3: 		register int i;
andre@3: 		register char *src = bufp->page + (int)OFFSET(bp);
andre@3: 		uint32 dst_offset = (uint32)OFFSET(bp) + (uint32)pairlen;
andre@3: 		register char *dst = bufp->page + dst_offset;
andre@3: 		uint32 length = bp[ndx + 1] - OFFSET(bp);
andre@3: 
andre@3: 		/*
andre@3: 		 * +-----------+XXX+---------+XXX+---------+---------> +infinity
andre@3: 		 * |           |             |             |
andre@3: 		 * 0           src_offset    dst_offset    BSIZE
andre@3: 		 *
andre@3: 		 * Dst_offset is > src_offset, so if src_offset were bad, dst_offset
andre@3: 		 * would be too, therefore we check only dst_offset.
andre@3: 		 *
andre@3: 		 * If dst_offset is >= BSIZE, either OFFSET(bp), or pairlen, or both
andre@3: 		 * is corrupted.
andre@3: 		 *
andre@3: 		 * Once we know dst_offset is < BSIZE, we can subtract it from BSIZE
andre@3: 		 * to get an upper bound on length.
andre@3: 		 */
andre@3: 		if(dst_offset > (uint32)hashp->BSIZE)
andre@3: 			return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 		if(length > (uint32)(hashp->BSIZE - dst_offset))
andre@3: 			return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 		memmove(dst, src, length);
andre@3: 
andre@3: 		/* Now adjust the pointers */
andre@3: 		for (i = ndx + 2; i <= n; i += 2) {
andre@3: 			if (bp[i + 1] == OVFLPAGE) {
andre@3: 				bp[i - 2] = bp[i];
andre@3: 				bp[i - 1] = bp[i + 1];
andre@3: 			} else {
andre@3: 				bp[i - 2] = bp[i] + pairlen;
andre@3: 				bp[i - 1] = bp[i + 1] + pairlen;
andre@3: 			}
andre@3: 		}
andre@3: 	}
andre@3: 	/* Finally adjust the page data */
andre@3: 	bp[n] = OFFSET(bp) + pairlen;
andre@3: 	bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(uint16);
andre@3: 	bp[0] = n - 2;
andre@3: 	hashp->NKEYS--;
andre@3: 
andre@3: 	bufp->flags |= BUF_MOD;
andre@3: 	return (0);
andre@3: }
andre@3: /*
andre@3:  * Returns:
andre@3:  *	 0 ==> OK
andre@3:  *	-1 ==> Error
andre@3:  */
andre@3: extern int
andre@3: __split_page(HTAB *hashp, uint32 obucket, uint32 nbucket)
andre@3: {
andre@3: 	register BUFHEAD *new_bufp, *old_bufp;
andre@3: 	register uint16 *ino;
andre@3: 	register uint16 *tmp_uint16_array;
andre@3: 	register char *np;
andre@3: 	DBT key, val;
andre@3:     uint16 n, ndx;
andre@3: 	int retval;
andre@3: 	uint16 copyto, diff, moved;
andre@3: 	size_t off;
andre@3: 	char *op;
andre@3: 
andre@3: 	copyto = (uint16)hashp->BSIZE;
andre@3: 	off = (uint16)hashp->BSIZE;
andre@3: 	old_bufp = __get_buf(hashp, obucket, NULL, 0);
andre@3: 	if (old_bufp == NULL)
andre@3: 		return (-1);
andre@3: 	new_bufp = __get_buf(hashp, nbucket, NULL, 0);
andre@3: 	if (new_bufp == NULL)
andre@3: 		return (-1);
andre@3: 
andre@3: 	old_bufp->flags |= (BUF_MOD | BUF_PIN);
andre@3: 	new_bufp->flags |= (BUF_MOD | BUF_PIN);
andre@3: 
andre@3: 	ino = (uint16 *)(op = old_bufp->page);
andre@3: 	np = new_bufp->page;
andre@3: 
andre@3: 	moved = 0;
andre@3: 
andre@3: 	for (n = 1, ndx = 1; n < ino[0]; n += 2) {
andre@3: 		if (ino[n + 1] < REAL_KEY) {
andre@3: 			retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
andre@3: 			    (int)copyto, (int)moved);
andre@3: 			old_bufp->flags &= ~BUF_PIN;
andre@3: 			new_bufp->flags &= ~BUF_PIN;
andre@3: 			return (retval);
andre@3: 
andre@3: 		}
andre@3: 		key.data = (uint8 *)op + ino[n];
andre@3: 
andre@3: 		/* check here for ino[n] being greater than
andre@3: 		 * off.  If it is then the database has
andre@3: 		 * been corrupted.
andre@3: 		 */
andre@3: 		if(ino[n] > off)
andre@3: 			return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 		key.size = off - ino[n];
andre@3: 
andre@3: #ifdef DEBUG
andre@3: 		/* make sure the size is positive */
andre@3: 		assert(((int)key.size) > -1);
andre@3: #endif
andre@3: 
andre@3: 		if (__call_hash(hashp, (char *)key.data, key.size) == obucket) {
andre@3: 			/* Don't switch page */
andre@3: 			diff = copyto - off;
andre@3: 			if (diff) {
andre@3: 				copyto = ino[n + 1] + diff;
andre@3: 				memmove(op + copyto, op + ino[n + 1],
andre@3: 				    off - ino[n + 1]);
andre@3: 				ino[ndx] = copyto + ino[n] - ino[n + 1];
andre@3: 				ino[ndx + 1] = copyto;
andre@3: 			} else
andre@3: 				copyto = ino[n + 1];
andre@3: 			ndx += 2;
andre@3: 		} else {
andre@3: 			/* Switch page */
andre@3: 			val.data = (uint8 *)op + ino[n + 1];
andre@3: 			val.size = ino[n] - ino[n + 1];
andre@3: 
andre@3: 			/* if the pair doesn't fit something is horribly
andre@3: 			 * wrong.  LJM
andre@3: 			 */
andre@3: 			tmp_uint16_array = (uint16*)np;
andre@3: 			if(!PAIRFITS(tmp_uint16_array, &key, &val))
andre@3: 				return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 			putpair(np, &key, &val);
andre@3: 			moved += 2;
andre@3: 		}
andre@3: 
andre@3: 		off = ino[n + 1];
andre@3: 	}
andre@3: 
andre@3: 	/* Now clean up the page */
andre@3: 	ino[0] -= moved;
andre@3: 	FREESPACE(ino) = copyto - sizeof(uint16) * (ino[0] + 3);
andre@3: 	OFFSET(ino) = copyto;
andre@3: 
andre@3: #ifdef DEBUG3
andre@3: 	(void)fprintf(stderr, "split %d/%d\n",
andre@3: 	    ((uint16 *)np)[0] / 2,
andre@3: 	    ((uint16 *)op)[0] / 2);
andre@3: #endif
andre@3: 	/* unpin both pages */
andre@3: 	old_bufp->flags &= ~BUF_PIN;
andre@3: 	new_bufp->flags &= ~BUF_PIN;
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Called when we encounter an overflow or big key/data page during split
andre@3:  * handling.  This is special cased since we have to begin checking whether
andre@3:  * the key/data pairs fit on their respective pages and because we may need
andre@3:  * overflow pages for both the old and new pages.
andre@3:  *
andre@3:  * The first page might be a page with regular key/data pairs in which case
andre@3:  * we have a regular overflow condition and just need to go on to the next
andre@3:  * page or it might be a big key/data pair in which case we need to fix the
andre@3:  * big key/data pair.
andre@3:  *
andre@3:  * Returns:
andre@3:  *	 0 ==> success
andre@3:  *	-1 ==> failure
andre@3:  */
andre@3: 
andre@3: /* the maximum number of loops we will allow UGLY split to chew
andre@3:  * on before we assume the database is corrupted and throw it
andre@3:  * away.
andre@3:  */
andre@3: #define MAX_UGLY_SPLIT_LOOPS 10000
andre@3: 
andre@3: static int
andre@3: ugly_split(HTAB *hashp, uint32 obucket, BUFHEAD *old_bufp,
andre@3:  BUFHEAD *new_bufp,/* Same as __split_page. */ int copyto, int moved)
andre@3: 	/* int copyto;	 First byte on page which contains key/data values. */
andre@3: 	/* int moved;	 Number of pairs moved to new page. */
andre@3: {
andre@3: 	register BUFHEAD *bufp;	/* Buffer header for ino */
andre@3: 	register uint16 *ino;	/* Page keys come off of */
andre@3: 	register uint16 *np;	/* New page */
andre@3: 	register uint16 *op;	/* Page keys go on to if they aren't moving */
andre@3:     uint32 loop_detection=0;
andre@3: 
andre@3: 	BUFHEAD *last_bfp;	/* Last buf header OVFL needing to be freed */
andre@3: 	DBT key, val;
andre@3: 	SPLIT_RETURN ret;
andre@3: 	uint16 n, off, ov_addr, scopyto;
andre@3: 	char *cino;		/* Character value of ino */
andre@3: 	int status;
andre@3: 
andre@3: 	bufp = old_bufp;
andre@3: 	ino = (uint16 *)old_bufp->page;
andre@3: 	np = (uint16 *)new_bufp->page;
andre@3: 	op = (uint16 *)old_bufp->page;
andre@3: 	last_bfp = NULL;
andre@3: 	scopyto = (uint16)copyto;	/* ANSI */
andre@3: 
andre@3: 	n = ino[0] - 1;
andre@3: 	while (n < ino[0]) {
andre@3: 
andre@3: 
andre@3:         /* this function goes nuts sometimes and never returns. 
andre@3:          * I havent found the problem yet but I need a solution
andre@3:          * so if we loop too often we assume a database curruption error
andre@3:          * :LJM
andre@3:          */
andre@3:         loop_detection++;
andre@3: 
andre@3:         if(loop_detection > MAX_UGLY_SPLIT_LOOPS)
andre@3:             return DATABASE_CORRUPTED_ERROR;
andre@3: 
andre@3: 		if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
andre@3: 			if ((status = __big_split(hashp, old_bufp,
andre@3: 			    new_bufp, bufp, bufp->addr, obucket, &ret)))
andre@3: 				return (status);
andre@3: 			old_bufp = ret.oldp;
andre@3: 			if (!old_bufp)
andre@3: 				return (-1);
andre@3: 			op = (uint16 *)old_bufp->page;
andre@3: 			new_bufp = ret.newp;
andre@3: 			if (!new_bufp)
andre@3: 				return (-1);
andre@3: 			np = (uint16 *)new_bufp->page;
andre@3: 			bufp = ret.nextp;
andre@3: 			if (!bufp)
andre@3: 				return (0);
andre@3: 			cino = (char *)bufp->page;
andre@3: 			ino = (uint16 *)cino;
andre@3: 			last_bfp = ret.nextp;
andre@3: 		} else if (ino[n + 1] == OVFLPAGE) {
andre@3: 			ov_addr = ino[n];
andre@3: 			/*
andre@3: 			 * Fix up the old page -- the extra 2 are the fields
andre@3: 			 * which contained the overflow information.
andre@3: 			 */
andre@3: 			ino[0] -= (moved + 2);
andre@3: 			FREESPACE(ino) =
andre@3: 			    scopyto - sizeof(uint16) * (ino[0] + 3);
andre@3: 			OFFSET(ino) = scopyto;
andre@3: 
andre@3: 			bufp = __get_buf(hashp, ov_addr, bufp, 0);
andre@3: 			if (!bufp)
andre@3: 				return (-1);
andre@3: 
andre@3: 			ino = (uint16 *)bufp->page;
andre@3: 			n = 1;
andre@3: 			scopyto = hashp->BSIZE;
andre@3: 			moved = 0;
andre@3: 
andre@3: 			if (last_bfp)
andre@3: 				__free_ovflpage(hashp, last_bfp);
andre@3: 			last_bfp = bufp;
andre@3: 		}
andre@3: 		/* Move regular sized pairs of there are any */
andre@3: 		off = hashp->BSIZE;
andre@3: 		for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
andre@3: 			cino = (char *)ino;
andre@3: 			key.data = (uint8 *)cino + ino[n];
andre@3: 			key.size = off - ino[n];
andre@3: 			val.data = (uint8 *)cino + ino[n + 1];
andre@3: 			val.size = ino[n] - ino[n + 1];
andre@3: 			off = ino[n + 1];
andre@3: 
andre@3: 			if (__call_hash(hashp, (char*)key.data, key.size) == obucket) {
andre@3: 				/* Keep on old page */
andre@3: 				if (PAIRFITS(op, (&key), (&val)))
andre@3: 					putpair((char *)op, &key, &val);
andre@3: 				else {
andre@3: 					old_bufp =
andre@3: 					    __add_ovflpage(hashp, old_bufp);
andre@3: 					if (!old_bufp)
andre@3: 						return (-1);
andre@3: 					op = (uint16 *)old_bufp->page;
andre@3: 					putpair((char *)op, &key, &val);
andre@3: 				}
andre@3: 				old_bufp->flags |= BUF_MOD;
andre@3: 			} else {
andre@3: 				/* Move to new page */
andre@3: 				if (PAIRFITS(np, (&key), (&val)))
andre@3: 					putpair((char *)np, &key, &val);
andre@3: 				else {
andre@3: 					new_bufp =
andre@3: 					    __add_ovflpage(hashp, new_bufp);
andre@3: 					if (!new_bufp)
andre@3: 						return (-1);
andre@3: 					np = (uint16 *)new_bufp->page;
andre@3: 					putpair((char *)np, &key, &val);
andre@3: 				}
andre@3: 				new_bufp->flags |= BUF_MOD;
andre@3: 			}
andre@3: 		}
andre@3: 	}
andre@3: 	if (last_bfp)
andre@3: 		__free_ovflpage(hashp, last_bfp);
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Add the given pair to the page
andre@3:  *
andre@3:  * Returns:
andre@3:  *	0 ==> OK
andre@3:  *	1 ==> failure
andre@3:  */
andre@3: extern int
andre@3: __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT * val)
andre@3: {
andre@3: 	register uint16 *bp, *sop;
andre@3: 	int do_expand;
andre@3: 
andre@3: 	bp = (uint16 *)bufp->page;
andre@3: 	do_expand = 0;
andre@3: 	while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
andre@3: 		/* Exception case */
andre@3: 		if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
andre@3: 			/* This is the last page of a big key/data pair
andre@3: 			   and we need to add another page */
andre@3: 			break;
andre@3: 		else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
andre@3: 			bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
andre@3: 			if (!bufp)
andre@3: 			  {
andre@3: #ifdef DEBUG
andre@3: 				assert(0);
andre@3: #endif
andre@3: 				return (-1);
andre@3: 			  }
andre@3: 			bp = (uint16 *)bufp->page;
andre@3: 		} else
andre@3: 			/* Try to squeeze key on this page */
andre@3: 			if (FREESPACE(bp) > PAIRSIZE(key, val)) {
andre@3: 			  {
andre@3: 				squeeze_key(bp, key, val);
andre@3: 
andre@3: 				/* LJM: I added this because I think it was
andre@3: 				 * left out on accident.
andre@3: 				 * if this isn't incremented nkeys will not
andre@3: 				 * be the actual number of keys in the db.
andre@3: 				 */
andre@3: 				hashp->NKEYS++;
andre@3: 				return (0);
andre@3: 			  }
andre@3: 			} else {
andre@3: 				bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
andre@3: 				if (!bufp)
andre@3: 			      {
andre@3: #ifdef DEBUG
andre@3: 				    assert(0);
andre@3: #endif
andre@3: 					return (-1);
andre@3: 				  }
andre@3: 				bp = (uint16 *)bufp->page;
andre@3: 			}
andre@3: 
andre@3: 	if (PAIRFITS(bp, key, val))
andre@3: 		putpair(bufp->page, key, (DBT *)val);
andre@3: 	else {
andre@3: 		do_expand = 1;
andre@3: 		bufp = __add_ovflpage(hashp, bufp);
andre@3: 		if (!bufp)
andre@3: 	      {
andre@3: #ifdef DEBUG
andre@3: 		    assert(0);
andre@3: #endif
andre@3: 			return (-1);
andre@3: 		  }
andre@3: 		sop = (uint16 *)bufp->page;
andre@3: 
andre@3: 		if (PAIRFITS(sop, key, val))
andre@3: 			putpair((char *)sop, key, (DBT *)val);
andre@3: 		else
andre@3: 			if (__big_insert(hashp, bufp, key, val))
andre@3: 	          {
andre@3: #ifdef DEBUG
andre@3: 		        assert(0);
andre@3: #endif
andre@3: 			    return (-1);
andre@3: 		      }
andre@3: 	}
andre@3: 	bufp->flags |= BUF_MOD;
andre@3: 	/*
andre@3: 	 * If the average number of keys per bucket exceeds the fill factor,
andre@3: 	 * expand the table.
andre@3: 	 */
andre@3: 	hashp->NKEYS++;
andre@3: 	if (do_expand ||
andre@3: 	    (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
andre@3: 		return (__expand_table(hashp));
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  *
andre@3:  * Returns:
andre@3:  *	pointer on success
andre@3:  *	NULL on error
andre@3:  */
andre@3: extern BUFHEAD *
andre@3: __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
andre@3: {
andre@3: 	register uint16 *sp;
andre@3: 	uint16 ndx, ovfl_num;
andre@3: #ifdef DEBUG1
andre@3: 	int tmp1, tmp2;
andre@3: #endif
andre@3: 	sp = (uint16 *)bufp->page;
andre@3: 
andre@3: 	/* Check if we are dynamically determining the fill factor */
andre@3: 	if (hashp->FFACTOR == DEF_FFACTOR) {
andre@3: 		hashp->FFACTOR = sp[0] >> 1;
andre@3: 		if (hashp->FFACTOR < MIN_FFACTOR)
andre@3: 			hashp->FFACTOR = MIN_FFACTOR;
andre@3: 	}
andre@3: 	bufp->flags |= BUF_MOD;
andre@3: 	ovfl_num = overflow_page(hashp);
andre@3: #ifdef DEBUG1
andre@3: 	tmp1 = bufp->addr;
andre@3: 	tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
andre@3: #endif
andre@3: 	if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
andre@3: 		return (NULL);
andre@3: 	bufp->ovfl->flags |= BUF_MOD;
andre@3: #ifdef DEBUG1
andre@3: 	(void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
andre@3: 	    tmp1, tmp2, bufp->ovfl->addr);
andre@3: #endif
andre@3: 	ndx = sp[0];
andre@3: 	/*
andre@3: 	 * Since a pair is allocated on a page only if there's room to add
andre@3: 	 * an overflow page, we know that the OVFL information will fit on
andre@3: 	 * the page.
andre@3: 	 */
andre@3: 	sp[ndx + 4] = OFFSET(sp);
andre@3: 	sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
andre@3: 	sp[ndx + 1] = ovfl_num;
andre@3: 	sp[ndx + 2] = OVFLPAGE;
andre@3: 	sp[0] = ndx + 2;
andre@3: #ifdef HASH_STATISTICS
andre@3: 	hash_overflows++;
andre@3: #endif
andre@3: 	return (bufp->ovfl);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Returns:
andre@3:  *	 0 indicates SUCCESS
andre@3:  *	-1 indicates FAILURE
andre@3:  */
andre@3: extern int
andre@3: __get_page(HTAB *hashp,
andre@3: 	char * p,
andre@3: 	uint32 bucket, 
andre@3: 	int is_bucket, 
andre@3: 	int is_disk, 
andre@3: 	int is_bitmap)
andre@3: {
andre@3: 	register int fd, page;
andre@3: 	size_t size;
andre@3: 	int rsize;
andre@3: 	uint16 *bp;
andre@3: 
andre@3: 	fd = hashp->fp;
andre@3: 	size = hashp->BSIZE;
andre@3: 
andre@3: 	if ((fd == -1) || !is_disk) {
andre@3: 		PAGE_INIT(p);
andre@3: 		return (0);
andre@3: 	}
andre@3: 	if (is_bucket)
andre@3: 		page = BUCKET_TO_PAGE(bucket);
andre@3: 	else
andre@3: 		page = OADDR_TO_PAGE(bucket);
andre@3: 	if ((MY_LSEEK(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
andre@3: 	    ((rsize = read(fd, p, size)) == -1))
andre@3: 		return (-1);
andre@3: 
andre@3: 	bp = (uint16 *)p;
andre@3: 	if (!rsize)
andre@3: 		bp[0] = 0;	/* We hit the EOF, so initialize a new page */
andre@3: 	else
andre@3: 		if ((unsigned)rsize != size) {
andre@3: 			errno = EFTYPE;
andre@3: 			return (-1);
andre@3: 		}
andre@3: 
andre@3: 	if (!is_bitmap && !bp[0]) {
andre@3: 		PAGE_INIT(p);
andre@3: 	} else {
andre@3: 
andre@3: #ifdef DEBUG
andre@3: 		if(BYTE_ORDER == LITTLE_ENDIAN)
andre@3: 		  {
andre@3: 			int is_little_endian;
andre@3: 			is_little_endian = BYTE_ORDER;
andre@3: 		  }
andre@3: 		else if(BYTE_ORDER == BIG_ENDIAN)
andre@3: 		  {
andre@3: 			int is_big_endian;
andre@3: 			is_big_endian = BYTE_ORDER;
andre@3: 		  }
andre@3: 		else
andre@3: 		  {
andre@3: 			assert(0);
andre@3: 		  }
andre@3: #endif
andre@3: 
andre@3: 		if (hashp->LORDER != BYTE_ORDER) {
andre@3: 			register int i, max;
andre@3: 
andre@3: 			if (is_bitmap) {
andre@3: 				max = hashp->BSIZE >> 2; /* divide by 4 */
andre@3: 				for (i = 0; i < max; i++)
andre@3: 					M_32_SWAP(((int *)p)[i]);
andre@3: 			} else {
andre@3: 				M_16_SWAP(bp[0]);
andre@3: 				max = bp[0] + 2;
andre@3: 
andre@3: 	    		/* bound the size of max by
andre@3: 	     		 * the maximum number of entries
andre@3: 	     		 * in the array
andre@3: 	     		 */
andre@3: 				if((unsigned)max > (size / sizeof(uint16)))
andre@3: 					return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 				/* do the byte order swap
andre@3: 				 */
andre@3: 				for (i = 1; i <= max; i++)
andre@3: 					M_16_SWAP(bp[i]);
andre@3: 			}
andre@3: 		}
andre@3: 
andre@3: 		/* check the validity of the page here
andre@3: 		 * (after doing byte order swaping if necessary)
andre@3: 		 */
andre@3: 		if(!is_bitmap && bp[0] != 0)
andre@3: 		  {
andre@3: 			uint16 num_keys = bp[0];
andre@3: 			uint16 offset;
andre@3: 			uint16 i;
andre@3: 
andre@3: 			/* bp[0] is supposed to be the number of
andre@3: 			 * entries currently in the page.  If
andre@3: 			 * bp[0] is too large (larger than the whole
andre@3: 			 * page) then the page is corrupted
andre@3: 			 */
andre@3: 			if(bp[0] > (size / sizeof(uint16)))
andre@3: 				return(DATABASE_CORRUPTED_ERROR);
andre@3: 			
andre@3: 			/* bound free space */
andre@3: 			if(FREESPACE(bp) > size)
andre@3: 				return(DATABASE_CORRUPTED_ERROR);
andre@3: 		
andre@3: 			/* check each key and data offset to make
andre@3:  			 * sure they are all within bounds they
andre@3:  			 * should all be less than the previous
andre@3:  			 * offset as well.
andre@3:  			 */
andre@3: 			offset = size;
andre@3: 			for(i=1 ; i <= num_keys; i+=2)
andre@3:   			  {
andre@3: 				/* ignore overflow pages etc. */
andre@3: 				if(bp[i+1] >= REAL_KEY)
andre@3: 	  			  {
andre@3: 						
andre@3: 					if(bp[i] > offset || bp[i+1] > bp[i])			
andre@3: 						return(DATABASE_CORRUPTED_ERROR);
andre@3: 			
andre@3: 					offset = bp[i+1];
andre@3: 	  			  }
andre@3: 				else
andre@3: 	  			  {
andre@3: 					/* there are no other valid keys after
andre@3: 		 			 * seeing a non REAL_KEY
andre@3: 		 			 */
andre@3: 					break;
andre@3: 	  			  }
andre@3:   			  }
andre@3: 		}
andre@3: 	}
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Write page p to disk
andre@3:  *
andre@3:  * Returns:
andre@3:  *	 0 ==> OK
andre@3:  *	-1 ==>failure
andre@3:  */
andre@3: extern int
andre@3: __put_page(HTAB *hashp, char *p, uint32 bucket, int is_bucket, int is_bitmap)
andre@3: {
andre@3: 	register int fd, page;
andre@3: 	size_t size;
andre@3: 	int wsize;
andre@3: 	off_t offset;
andre@3: 
andre@3: 	size = hashp->BSIZE;
andre@3: 	if ((hashp->fp == -1) && open_temp(hashp))
andre@3: 		return (-1);
andre@3: 	fd = hashp->fp;
andre@3: 
andre@3: 	if (hashp->LORDER != BYTE_ORDER) {
andre@3: 		register int i;
andre@3: 		register int max;
andre@3: 
andre@3: 		if (is_bitmap) {
andre@3: 			max = hashp->BSIZE >> 2;	/* divide by 4 */
andre@3: 			for (i = 0; i < max; i++)
andre@3: 				M_32_SWAP(((int *)p)[i]);
andre@3: 		} else {
andre@3: 			max = ((uint16 *)p)[0] + 2;
andre@3: 
andre@3:             /* bound the size of max by
andre@3:              * the maximum number of entries
andre@3:              * in the array
andre@3:              */
andre@3:             if((unsigned)max > (size / sizeof(uint16)))
andre@3:                 return(DATABASE_CORRUPTED_ERROR);
andre@3: 
andre@3: 			for (i = 0; i <= max; i++)
andre@3: 				M_16_SWAP(((uint16 *)p)[i]);
andre@3: 
andre@3: 		}
andre@3: 	}
andre@3: 
andre@3: 	if (is_bucket)
andre@3: 		page = BUCKET_TO_PAGE(bucket);
andre@3: 	else
andre@3: 		page = OADDR_TO_PAGE(bucket);
andre@3: 	offset = (off_t)page << hashp->BSHIFT;
andre@3: 	if ((MY_LSEEK(fd, offset, SEEK_SET) == -1) ||
andre@3: 	    ((wsize = write(fd, p, size)) == -1))
andre@3: 		/* Errno is set */
andre@3: 		return (-1);
andre@3: 	if ((unsigned)wsize != size) {
andre@3: 		errno = EFTYPE;
andre@3: 		return (-1);
andre@3: 	}
andre@3: #if defined(_WIN32) || defined(_WINDOWS) 
andre@3: 	if (offset + size > hashp->file_size) {
andre@3: 		hashp->updateEOF = 1;
andre@3: 	}
andre@3: #endif
andre@3: 	/* put the page back the way it was so that it isn't byteswapped
andre@3: 	 * if it remains in memory - LJM
andre@3: 	 */
andre@3: 	if (hashp->LORDER != BYTE_ORDER) {
andre@3: 		register int i;
andre@3: 		register int max;
andre@3: 
andre@3: 		if (is_bitmap) {
andre@3: 			max = hashp->BSIZE >> 2;	/* divide by 4 */
andre@3: 			for (i = 0; i < max; i++)
andre@3: 				M_32_SWAP(((int *)p)[i]);
andre@3: 		} else {
andre@3:     		uint16 *bp = (uint16 *)p;
andre@3: 
andre@3: 			M_16_SWAP(bp[0]);
andre@3: 			max = bp[0] + 2;
andre@3: 
andre@3: 			/* no need to bound the size if max again
andre@3: 			 * since it was done already above
andre@3: 			 */
andre@3: 
andre@3: 			/* do the byte order re-swap
andre@3: 			 */
andre@3: 			for (i = 1; i <= max; i++)
andre@3: 				M_16_SWAP(bp[i]);
andre@3: 		}
andre@3: 	}
andre@3: 
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: #define BYTE_MASK	((1 << INT_BYTE_SHIFT) -1)
andre@3: /*
andre@3:  * Initialize a new bitmap page.  Bitmap pages are left in memory
andre@3:  * once they are read in.
andre@3:  */
andre@3: extern int
andre@3: __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
andre@3: {
andre@3: 	uint32 *ip;
andre@3: 	size_t clearbytes, clearints;
andre@3: 
andre@3: 	if ((ip = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
andre@3: 		return (1);
andre@3: 	hashp->nmaps++;
andre@3: 	clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
andre@3: 	clearbytes = clearints << INT_TO_BYTE;
andre@3: 	(void)memset((char *)ip, 0, clearbytes);
andre@3: 	(void)memset(((char *)ip) + clearbytes, 0xFF,
andre@3: 	    hashp->BSIZE - clearbytes);
andre@3: 	ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
andre@3: 	SETBIT(ip, 0);
andre@3: 	hashp->BITMAPS[ndx] = (uint16)pnum;
andre@3: 	hashp->mapp[ndx] = ip;
andre@3: 	return (0);
andre@3: }
andre@3: 
andre@3: static uint32
andre@3: first_free(uint32 map)
andre@3: {
andre@3: 	register uint32 i, mask;
andre@3: 
andre@3: 	mask = 0x1;
andre@3: 	for (i = 0; i < BITS_PER_MAP; i++) {
andre@3: 		if (!(mask & map))
andre@3: 			return (i);
andre@3: 		mask = mask << 1;
andre@3: 	}
andre@3: 	return (i);
andre@3: }
andre@3: 
andre@3: static uint16
andre@3: overflow_page(HTAB *hashp)
andre@3: {
andre@3: 	register uint32 *freep=NULL;
andre@3: 	register int max_free, offset, splitnum;
andre@3: 	uint16 addr;
andre@3: 	uint32 i;
andre@3: 	int bit, first_page, free_bit, free_page, in_use_bits, j;
andre@3: #ifdef DEBUG2
andre@3: 	int tmp1, tmp2;
andre@3: #endif
andre@3: 	splitnum = hashp->OVFL_POINT;
andre@3: 	max_free = hashp->SPARES[splitnum];
andre@3: 
andre@3: 	free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
andre@3: 	free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
andre@3: 
andre@3: 	/* Look through all the free maps to find the first free block */
andre@3: 	first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
andre@3: 	for ( i = first_page; i <= (unsigned)free_page; i++ ) {
andre@3: 		if (!(freep = (uint32 *)hashp->mapp[i]) &&
andre@3: 		    !(freep = fetch_bitmap(hashp, i)))
andre@3: 			return (0);
andre@3: 		if (i == (unsigned)free_page)
andre@3: 			in_use_bits = free_bit;
andre@3: 		else
andre@3: 			in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
andre@3: 		
andre@3: 		if (i == (unsigned)first_page) {
andre@3: 			bit = hashp->LAST_FREED &
andre@3: 			    ((hashp->BSIZE << BYTE_SHIFT) - 1);
andre@3: 			j = bit / BITS_PER_MAP;
andre@3: 			bit = bit & ~(BITS_PER_MAP - 1);
andre@3: 		} else {
andre@3: 			bit = 0;
andre@3: 			j = 0;
andre@3: 		}
andre@3: 		for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
andre@3: 			if (freep[j] != ALL_SET)
andre@3: 				goto found;
andre@3: 	}
andre@3: 
andre@3: 	/* No Free Page Found */
andre@3: 	hashp->LAST_FREED = hashp->SPARES[splitnum];
andre@3: 	hashp->SPARES[splitnum]++;
andre@3: 	offset = hashp->SPARES[splitnum] -
andre@3: 	    (splitnum ? hashp->SPARES[splitnum - 1] : 0);
andre@3: 
andre@3: #define	OVMSG	"HASH: Out of overflow pages.  Increase page size\n"
andre@3: 	if (offset > SPLITMASK) {
andre@3: 		if (++splitnum >= NCACHED) {
andre@3: #ifndef macintosh
andre@3: 			(void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
andre@3: #endif
andre@3: 			return (0);
andre@3: 		}
andre@3: 		hashp->OVFL_POINT = splitnum;
andre@3: 		hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
andre@3: 		hashp->SPARES[splitnum-1]--;
andre@3: 		offset = 1;
andre@3: 	}
andre@3: 
andre@3: 	/* Check if we need to allocate a new bitmap page */
andre@3: 	if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
andre@3: 		free_page++;
andre@3: 		if (free_page >= NCACHED) {
andre@3: #ifndef macintosh
andre@3: 			(void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
andre@3: #endif
andre@3: 			return (0);
andre@3: 		}
andre@3: 		/*
andre@3: 		 * This is tricky.  The 1 indicates that you want the new page
andre@3: 		 * allocated with 1 clear bit.  Actually, you are going to
andre@3: 		 * allocate 2 pages from this map.  The first is going to be
andre@3: 		 * the map page, the second is the overflow page we were
andre@3: 		 * looking for.  The init_bitmap routine automatically, sets
andre@3: 		 * the first bit of itself to indicate that the bitmap itself
andre@3: 		 * is in use.  We would explicitly set the second bit, but
andre@3: 		 * don't have to if we tell init_bitmap not to leave it clear
andre@3: 		 * in the first place.
andre@3: 		 */
andre@3: 		if (__ibitmap(hashp,
andre@3: 		    (int)OADDR_OF(splitnum, offset), 1, free_page))
andre@3: 			return (0);
andre@3: 		hashp->SPARES[splitnum]++;
andre@3: #ifdef DEBUG2
andre@3: 		free_bit = 2;
andre@3: #endif
andre@3: 		offset++;
andre@3: 		if (offset > SPLITMASK) {
andre@3: 			if (++splitnum >= NCACHED) {
andre@3: #ifndef macintosh
andre@3: 				(void)write(STDERR_FILENO, OVMSG,
andre@3: 				    sizeof(OVMSG) - 1);
andre@3: #endif
andre@3: 				return (0);
andre@3: 			}
andre@3: 			hashp->OVFL_POINT = splitnum;
andre@3: 			hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
andre@3: 			hashp->SPARES[splitnum-1]--;
andre@3: 			offset = 0;
andre@3: 		}
andre@3: 	} else {
andre@3: 		/*
andre@3: 		 * Free_bit addresses the last used bit.  Bump it to address
andre@3: 		 * the first available bit.
andre@3: 		 */
andre@3: 		free_bit++;
andre@3: 		SETBIT(freep, free_bit);
andre@3: 	}
andre@3: 
andre@3: 	/* Calculate address of the new overflow page */
andre@3: 	addr = OADDR_OF(splitnum, offset);
andre@3: #ifdef DEBUG2
andre@3: 	(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
andre@3: 	    addr, free_bit, free_page);
andre@3: #endif
andre@3: 	return (addr);
andre@3: 
andre@3: found:
andre@3: 	bit = bit + first_free(freep[j]);
andre@3: 	SETBIT(freep, bit);
andre@3: #ifdef DEBUG2
andre@3: 	tmp1 = bit;
andre@3: 	tmp2 = i;
andre@3: #endif
andre@3: 	/*
andre@3: 	 * Bits are addressed starting with 0, but overflow pages are addressed
andre@3: 	 * beginning at 1. Bit is a bit addressnumber, so we need to increment
andre@3: 	 * it to convert it to a page number.
andre@3: 	 */
andre@3: 	bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
andre@3: 	if (bit >= hashp->LAST_FREED)
andre@3: 		hashp->LAST_FREED = bit - 1;
andre@3: 
andre@3: 	/* Calculate the split number for this page */
andre@3: 	for (i = 0; (i < (unsigned)splitnum) && (bit > hashp->SPARES[i]); i++) {}
andre@3: 	offset = (i ? bit - hashp->SPARES[i - 1] : bit);
andre@3: 	if (offset >= SPLITMASK)
andre@3: 		return (0);	/* Out of overflow pages */
andre@3: 	addr = OADDR_OF(i, offset);
andre@3: #ifdef DEBUG2
andre@3: 	(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
andre@3: 	    addr, tmp1, tmp2);
andre@3: #endif
andre@3: 
andre@3: 	/* Allocate and return the overflow page */
andre@3: 	return (addr);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Mark this overflow page as free.
andre@3:  */
andre@3: extern void
andre@3: __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
andre@3: {
andre@3: 	uint16 addr;
andre@3: 	uint32 *freep;
andre@3: 	uint32 bit_address, free_page, free_bit;
andre@3: 	uint16 ndx;
andre@3: 
andre@3: 	if(!obufp || !obufp->addr)
andre@3: 	    return;
andre@3: 
andre@3: 	addr = obufp->addr;
andre@3: #ifdef DEBUG1
andre@3: 	(void)fprintf(stderr, "Freeing %d\n", addr);
andre@3: #endif
andre@3: 	ndx = (((uint16)addr) >> SPLITSHIFT);
andre@3: 	bit_address =
andre@3: 	    (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
andre@3: 	if (bit_address < (uint32)hashp->LAST_FREED)
andre@3: 		hashp->LAST_FREED = bit_address;
andre@3: 	free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
andre@3: 	free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
andre@3: 
andre@3: 	if (!(freep = hashp->mapp[free_page])) 
andre@3: 		freep = fetch_bitmap(hashp, free_page);
andre@3: 
andre@3: #ifdef DEBUG
andre@3: 	/*
andre@3: 	 * This had better never happen.  It means we tried to read a bitmap
andre@3: 	 * that has already had overflow pages allocated off it, and we
andre@3: 	 * failed to read it from the file.
andre@3: 	 */
andre@3: 	if (!freep)
andre@3: 	  {
andre@3: 		assert(0);
andre@3: 		return;
andre@3: 	  }
andre@3: #endif
andre@3: 	CLRBIT(freep, free_bit);
andre@3: #ifdef DEBUG2
andre@3: 	(void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
andre@3: 	    obufp->addr, free_bit, free_page);
andre@3: #endif
andre@3: 	__reclaim_buf(hashp, obufp);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Returns:
andre@3:  *	 0 success
andre@3:  *	-1 failure
andre@3:  */
andre@3: static int
andre@3: open_temp(HTAB *hashp)
andre@3: {
andre@3: #ifdef XP_OS2
andre@3:  	hashp->fp = mkstemp(NULL);
andre@3: #else
andre@3: #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
andre@3: 	sigset_t set, oset;
andre@3: #endif
andre@3: #if !defined(macintosh)
andre@3: 	char * tmpdir;
andre@3: 	size_t len;
andre@3: 	char last;
andre@3: #endif
andre@3: 	static const char namestr[] = "/_hashXXXXXX";
andre@3: 	char filename[1024];
andre@3: 
andre@3: #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
andre@3: 	/* Block signals; make sure file goes away at process exit. */
andre@3: 	(void)sigfillset(&set);
andre@3: 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
andre@3: #endif
andre@3: 
andre@3: 	filename[0] = 0;
andre@3: #if defined(macintosh)
andre@3: 	strcat(filename, namestr + 1);
andre@3: #else
andre@3: 	tmpdir = getenv("TMP");
andre@3: 	if (!tmpdir)
andre@3: 		tmpdir = getenv("TMPDIR");
andre@3: 	if (!tmpdir)
andre@3: 		tmpdir = getenv("TEMP");
andre@3: 	if (!tmpdir)
andre@3: 		tmpdir = ".";
andre@3: 	len = strlen(tmpdir);
andre@3: 	if (len && len < (sizeof filename - sizeof namestr)) {
andre@3: 		strcpy(filename, tmpdir);
andre@3: 	}
andre@3: 	len = strlen(filename);
andre@3: 	last = tmpdir[len - 1];
andre@3: 	strcat(filename, (last == '/' || last == '\\') ? namestr + 1 : namestr);
andre@3: #endif
andre@3: 
andre@3: #if defined(_WIN32) || defined(_WINDOWS)
andre@3: 	if ((hashp->fp = mkstempflags(filename, _O_BINARY|_O_TEMPORARY)) != -1) {
andre@3: 		if (hashp->filename) {
andre@3: 			free(hashp->filename);
andre@3: 		}
andre@3: 		hashp->filename = strdup(filename);
andre@3: 		hashp->is_temp = 1;
andre@3: 	}
andre@3: #else
andre@3: 	if ((hashp->fp = mkstemp(filename)) != -1) {
andre@3: 		(void)unlink(filename);
andre@3: #if !defined(macintosh)
andre@3: 		(void)fcntl(hashp->fp, F_SETFD, 1);
andre@3: #endif									  
andre@3: 	}
andre@3: #endif
andre@3: 
andre@3: #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
andre@3: 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
andre@3: #endif
andre@3: #endif  /* !OS2 */
andre@3: 	return (hashp->fp != -1 ? 0 : -1);
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * We have to know that the key will fit, but the last entry on the page is
andre@3:  * an overflow pair, so we need to shift things.
andre@3:  */
andre@3: static void
andre@3: squeeze_key(uint16 *sp, const DBT * key, const DBT * val)
andre@3: {
andre@3: 	register char *p;
andre@3: 	uint16 free_space, n, off, pageno;
andre@3: 
andre@3: 	p = (char *)sp;
andre@3: 	n = sp[0];
andre@3: 	free_space = FREESPACE(sp);
andre@3: 	off = OFFSET(sp);
andre@3: 
andre@3: 	pageno = sp[n - 1];
andre@3: 	off -= key->size;
andre@3: 	sp[n - 1] = off;
andre@3: 	memmove(p + off, key->data, key->size);
andre@3: 	off -= val->size;
andre@3: 	sp[n] = off;
andre@3: 	memmove(p + off, val->data, val->size);
andre@3: 	sp[0] = n + 2;
andre@3: 	sp[n + 1] = pageno;
andre@3: 	sp[n + 2] = OVFLPAGE;
andre@3: 	FREESPACE(sp) = free_space - PAIRSIZE(key, val);
andre@3: 	OFFSET(sp) = off;
andre@3: }
andre@3: 
andre@3: static uint32 *
andre@3: fetch_bitmap(HTAB *hashp, uint32 ndx)
andre@3: {
andre@3: 	if (ndx >= (unsigned)hashp->nmaps)
andre@3: 		return (NULL);
andre@3: 	if ((hashp->mapp[ndx] = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
andre@3: 		return (NULL);
andre@3: 	if (__get_page(hashp,
andre@3: 	    (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
andre@3: 		free(hashp->mapp[ndx]);
andre@3: 		hashp->mapp[ndx] = NULL; /* NEW: 9-11-95 */
andre@3: 		return (NULL);
andre@3: 	}                 
andre@3: 	return (hashp->mapp[ndx]);
andre@3: }
andre@3: 
andre@3: #ifdef DEBUG4
andre@3: int
andre@3: print_chain(int addr)
andre@3: {
andre@3: 	BUFHEAD *bufp;
andre@3: 	short *bp, oaddr;
andre@3: 
andre@3: 	(void)fprintf(stderr, "%d ", addr);
andre@3: 	bufp = __get_buf(hashp, addr, NULL, 0);
andre@3: 	bp = (short *)bufp->page;
andre@3: 	while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
andre@3: 		((bp[0] > 2) && bp[2] < REAL_KEY))) {
andre@3: 		oaddr = bp[bp[0] - 1];
andre@3: 		(void)fprintf(stderr, "%d ", (int)oaddr);
andre@3: 		bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
andre@3: 		bp = (short *)bufp->page;
andre@3: 	}
andre@3: 	(void)fprintf(stderr, "\n");
andre@3: }
andre@3: #endif