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