1*84d9c625SLionel Sambuc /* $NetBSD: hash_page.c,v 1.26 2013/12/01 00:22:48 christos Exp $ */
22639ae9bSBen Gras
32639ae9bSBen Gras /*-
42639ae9bSBen Gras * Copyright (c) 1990, 1993, 1994
52639ae9bSBen Gras * The Regents of the University of California. All rights reserved.
62639ae9bSBen Gras *
72639ae9bSBen Gras * This code is derived from software contributed to Berkeley by
82639ae9bSBen Gras * Margo Seltzer.
92639ae9bSBen Gras *
102639ae9bSBen Gras * Redistribution and use in source and binary forms, with or without
112639ae9bSBen Gras * modification, are permitted provided that the following conditions
122639ae9bSBen Gras * are met:
132639ae9bSBen Gras * 1. Redistributions of source code must retain the above copyright
142639ae9bSBen Gras * notice, this list of conditions and the following disclaimer.
152639ae9bSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162639ae9bSBen Gras * notice, this list of conditions and the following disclaimer in the
172639ae9bSBen Gras * documentation and/or other materials provided with the distribution.
182639ae9bSBen Gras * 3. Neither the name of the University nor the names of its contributors
192639ae9bSBen Gras * may be used to endorse or promote products derived from this software
202639ae9bSBen Gras * without specific prior written permission.
212639ae9bSBen Gras *
222639ae9bSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232639ae9bSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242639ae9bSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252639ae9bSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262639ae9bSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272639ae9bSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282639ae9bSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292639ae9bSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302639ae9bSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312639ae9bSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322639ae9bSBen Gras * SUCH DAMAGE.
332639ae9bSBen Gras */
342639ae9bSBen Gras
352639ae9bSBen Gras #if HAVE_NBTOOL_CONFIG_H
362639ae9bSBen Gras #include "nbtool_config.h"
372639ae9bSBen Gras #endif
382639ae9bSBen Gras
392639ae9bSBen Gras #include <sys/cdefs.h>
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: hash_page.c,v 1.26 2013/12/01 00:22:48 christos Exp $");
412639ae9bSBen Gras
422639ae9bSBen Gras /*
432639ae9bSBen Gras * PACKAGE: hashing
442639ae9bSBen Gras *
452639ae9bSBen Gras * DESCRIPTION:
462639ae9bSBen Gras * Page manipulation for hashing package.
472639ae9bSBen Gras *
482639ae9bSBen Gras * ROUTINES:
492639ae9bSBen Gras *
502639ae9bSBen Gras * External
512639ae9bSBen Gras * __get_page
522639ae9bSBen Gras * __add_ovflpage
532639ae9bSBen Gras * Internal
542639ae9bSBen Gras * overflow_page
552639ae9bSBen Gras */
562639ae9bSBen Gras
572639ae9bSBen Gras #include "namespace.h"
582639ae9bSBen Gras
592639ae9bSBen Gras #include <sys/types.h>
602639ae9bSBen Gras
612639ae9bSBen Gras #include <errno.h>
622639ae9bSBen Gras #include <fcntl.h>
632639ae9bSBen Gras #include <signal.h>
642639ae9bSBen Gras #include <stdio.h>
652639ae9bSBen Gras #include <stdlib.h>
662639ae9bSBen Gras #include <string.h>
672639ae9bSBen Gras #include <unistd.h>
682639ae9bSBen Gras #include <paths.h>
692639ae9bSBen Gras #include <assert.h>
702639ae9bSBen Gras
712639ae9bSBen Gras #include <db.h>
722639ae9bSBen Gras #include "hash.h"
732639ae9bSBen Gras #include "page.h"
742639ae9bSBen Gras #include "extern.h"
752639ae9bSBen Gras
762639ae9bSBen Gras static uint32_t *fetch_bitmap(HTAB *, int);
772639ae9bSBen Gras static uint32_t first_free(uint32_t);
782639ae9bSBen Gras static uint16_t overflow_page(HTAB *);
792639ae9bSBen Gras static void putpair(char *, const DBT *, const DBT *);
802639ae9bSBen Gras static void squeeze_key(uint16_t *, const DBT *, const DBT *);
812639ae9bSBen Gras static int ugly_split(HTAB *, uint32_t, BUFHEAD *, BUFHEAD *, int, int);
822639ae9bSBen Gras
832639ae9bSBen Gras #define PAGE_INIT(P) { \
842639ae9bSBen Gras ((uint16_t *)(void *)(P))[0] = 0; \
852639ae9bSBen Gras temp = 3 * sizeof(uint16_t); \
86f14fb602SLionel Sambuc _DIAGASSERT((size_t)hashp->BSIZE >= temp); \
872639ae9bSBen Gras ((uint16_t *)(void *)(P))[1] = (uint16_t)(hashp->BSIZE - temp); \
882639ae9bSBen Gras ((uint16_t *)(void *)(P))[2] = hashp->BSIZE; \
892639ae9bSBen Gras }
902639ae9bSBen Gras
912639ae9bSBen Gras /*
922639ae9bSBen Gras * This is called AFTER we have verified that there is room on the page for
932639ae9bSBen Gras * the pair (PAIRFITS has returned true) so we go right ahead and start moving
942639ae9bSBen Gras * stuff on.
952639ae9bSBen Gras */
962639ae9bSBen Gras static void
putpair(char * p,const DBT * key,const DBT * val)972639ae9bSBen Gras putpair(char *p, const DBT *key, const DBT *val)
982639ae9bSBen Gras {
992639ae9bSBen Gras uint16_t *bp, n, off;
1002639ae9bSBen Gras size_t temp;
1012639ae9bSBen Gras
1022639ae9bSBen Gras bp = (uint16_t *)(void *)p;
1032639ae9bSBen Gras
1042639ae9bSBen Gras /* Enter the key first. */
1052639ae9bSBen Gras n = bp[0];
1062639ae9bSBen Gras
1072639ae9bSBen Gras temp = OFFSET(bp);
1082639ae9bSBen Gras _DIAGASSERT(temp >= key->size);
1092639ae9bSBen Gras off = (uint16_t)(temp - key->size);
1102639ae9bSBen Gras memmove(p + off, key->data, key->size);
1112639ae9bSBen Gras bp[++n] = off;
1122639ae9bSBen Gras
1132639ae9bSBen Gras /* Now the data. */
1142639ae9bSBen Gras _DIAGASSERT(off >= val->size);
1152639ae9bSBen Gras off -= (uint16_t)val->size;
1162639ae9bSBen Gras memmove(p + off, val->data, val->size);
1172639ae9bSBen Gras bp[++n] = off;
1182639ae9bSBen Gras
1192639ae9bSBen Gras /* Adjust page info. */
1202639ae9bSBen Gras bp[0] = n;
1212639ae9bSBen Gras temp = (n + 3) * sizeof(uint16_t);
1222639ae9bSBen Gras _DIAGASSERT(off >= temp);
1232639ae9bSBen Gras bp[n + 1] = (uint16_t)(off - temp);
1242639ae9bSBen Gras bp[n + 2] = off;
1252639ae9bSBen Gras }
1262639ae9bSBen Gras
1272639ae9bSBen Gras /*
1282639ae9bSBen Gras * Returns:
1292639ae9bSBen Gras * 0 OK
1302639ae9bSBen Gras * -1 error
1312639ae9bSBen Gras */
1322639ae9bSBen Gras int
__delpair(HTAB * hashp,BUFHEAD * bufp,int ndx)1332639ae9bSBen Gras __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
1342639ae9bSBen Gras {
1352639ae9bSBen Gras uint16_t *bp, newoff;
1362639ae9bSBen Gras int n;
1372639ae9bSBen Gras uint16_t pairlen;
1382639ae9bSBen Gras size_t temp;
1392639ae9bSBen Gras
1402639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
1412639ae9bSBen Gras n = bp[0];
1422639ae9bSBen Gras
1432639ae9bSBen Gras if (bp[ndx + 1] < REAL_KEY)
1442639ae9bSBen Gras return (__big_delete(hashp, bufp));
1452639ae9bSBen Gras if (ndx != 1)
1462639ae9bSBen Gras newoff = bp[ndx - 1];
1472639ae9bSBen Gras else
1482639ae9bSBen Gras newoff = hashp->BSIZE;
1492639ae9bSBen Gras pairlen = newoff - bp[ndx + 1];
1502639ae9bSBen Gras
1512639ae9bSBen Gras if (ndx != (n - 1)) {
1522639ae9bSBen Gras /* Hard Case -- need to shuffle keys */
1532639ae9bSBen Gras int i;
1542639ae9bSBen Gras char *src = bufp->page + (int)OFFSET(bp);
1552639ae9bSBen Gras char *dst = src + (int)pairlen;
1562639ae9bSBen Gras memmove(dst, src, (size_t)(bp[ndx + 1] - OFFSET(bp)));
1572639ae9bSBen Gras
1582639ae9bSBen Gras /* Now adjust the pointers */
1592639ae9bSBen Gras for (i = ndx + 2; i <= n; i += 2) {
1602639ae9bSBen Gras if (bp[i + 1] == OVFLPAGE) {
1612639ae9bSBen Gras bp[i - 2] = bp[i];
1622639ae9bSBen Gras bp[i - 1] = bp[i + 1];
1632639ae9bSBen Gras } else {
1642639ae9bSBen Gras bp[i - 2] = bp[i] + pairlen;
1652639ae9bSBen Gras bp[i - 1] = bp[i + 1] + pairlen;
1662639ae9bSBen Gras }
1672639ae9bSBen Gras }
1682639ae9bSBen Gras }
1692639ae9bSBen Gras /* Finally adjust the page data */
1702639ae9bSBen Gras bp[n] = OFFSET(bp) + pairlen;
1712639ae9bSBen Gras temp = bp[n + 1] + pairlen + 2 * sizeof(uint16_t);
1722639ae9bSBen Gras _DIAGASSERT(temp <= 0xffff);
1732639ae9bSBen Gras bp[n - 1] = (uint16_t)temp;
1742639ae9bSBen Gras bp[0] = n - 2;
1752639ae9bSBen Gras hashp->NKEYS--;
1762639ae9bSBen Gras
1772639ae9bSBen Gras bufp->flags |= BUF_MOD;
1782639ae9bSBen Gras return (0);
1792639ae9bSBen Gras }
1802639ae9bSBen Gras /*
1812639ae9bSBen Gras * Returns:
1822639ae9bSBen Gras * 0 ==> OK
1832639ae9bSBen Gras * -1 ==> Error
1842639ae9bSBen Gras */
1852639ae9bSBen Gras int
__split_page(HTAB * hashp,uint32_t obucket,uint32_t nbucket)1862639ae9bSBen Gras __split_page(HTAB *hashp, uint32_t obucket, uint32_t nbucket)
1872639ae9bSBen Gras {
1882639ae9bSBen Gras BUFHEAD *new_bufp, *old_bufp;
1892639ae9bSBen Gras uint16_t *ino;
1902639ae9bSBen Gras char *np;
1912639ae9bSBen Gras DBT key, val;
1922639ae9bSBen Gras int n, ndx, retval;
1932639ae9bSBen Gras uint16_t copyto, diff, off, moved;
1942639ae9bSBen Gras char *op;
1952639ae9bSBen Gras size_t temp;
1962639ae9bSBen Gras
1972639ae9bSBen Gras copyto = (uint16_t)hashp->BSIZE;
1982639ae9bSBen Gras off = (uint16_t)hashp->BSIZE;
1992639ae9bSBen Gras old_bufp = __get_buf(hashp, obucket, NULL, 0);
2002639ae9bSBen Gras if (old_bufp == NULL)
2012639ae9bSBen Gras return (-1);
2022639ae9bSBen Gras new_bufp = __get_buf(hashp, nbucket, NULL, 0);
2032639ae9bSBen Gras if (new_bufp == NULL)
2042639ae9bSBen Gras return (-1);
2052639ae9bSBen Gras
2062639ae9bSBen Gras old_bufp->flags |= (BUF_MOD | BUF_PIN);
2072639ae9bSBen Gras new_bufp->flags |= (BUF_MOD | BUF_PIN);
2082639ae9bSBen Gras
2092639ae9bSBen Gras ino = (uint16_t *)(void *)(op = old_bufp->page);
2102639ae9bSBen Gras np = new_bufp->page;
2112639ae9bSBen Gras
2122639ae9bSBen Gras moved = 0;
2132639ae9bSBen Gras
2142639ae9bSBen Gras for (n = 1, ndx = 1; n < ino[0]; n += 2) {
2152639ae9bSBen Gras if (ino[n + 1] < REAL_KEY) {
2162639ae9bSBen Gras retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
2172639ae9bSBen Gras (int)copyto, (int)moved);
2182639ae9bSBen Gras old_bufp->flags &= ~BUF_PIN;
2192639ae9bSBen Gras new_bufp->flags &= ~BUF_PIN;
2202639ae9bSBen Gras return (retval);
2212639ae9bSBen Gras
2222639ae9bSBen Gras }
2232639ae9bSBen Gras key.data = (uint8_t *)op + ino[n];
2242639ae9bSBen Gras key.size = off - ino[n];
2252639ae9bSBen Gras
2262639ae9bSBen Gras if (__call_hash(hashp, key.data, (int)key.size) == obucket) {
2272639ae9bSBen Gras /* Don't switch page */
2282639ae9bSBen Gras diff = copyto - off;
2292639ae9bSBen Gras if (diff) {
2302639ae9bSBen Gras copyto = ino[n + 1] + diff;
2312639ae9bSBen Gras memmove(op + copyto, op + ino[n + 1],
2322639ae9bSBen Gras (size_t)(off - ino[n + 1]));
2332639ae9bSBen Gras ino[ndx] = copyto + ino[n] - ino[n + 1];
2342639ae9bSBen Gras ino[ndx + 1] = copyto;
2352639ae9bSBen Gras } else
2362639ae9bSBen Gras copyto = ino[n + 1];
2372639ae9bSBen Gras ndx += 2;
2382639ae9bSBen Gras } else {
2392639ae9bSBen Gras /* Switch page */
2402639ae9bSBen Gras val.data = (uint8_t *)op + ino[n + 1];
2412639ae9bSBen Gras val.size = ino[n] - ino[n + 1];
2422639ae9bSBen Gras putpair(np, &key, &val);
2432639ae9bSBen Gras moved += 2;
2442639ae9bSBen Gras }
2452639ae9bSBen Gras
2462639ae9bSBen Gras off = ino[n + 1];
2472639ae9bSBen Gras }
2482639ae9bSBen Gras
2492639ae9bSBen Gras /* Now clean up the page */
2502639ae9bSBen Gras ino[0] -= moved;
2512639ae9bSBen Gras temp = sizeof(uint16_t) * (ino[0] + 3);
2522639ae9bSBen Gras _DIAGASSERT(copyto >= temp);
2532639ae9bSBen Gras FREESPACE(ino) = (uint16_t)(copyto - temp);
2542639ae9bSBen Gras OFFSET(ino) = copyto;
2552639ae9bSBen Gras
2562639ae9bSBen Gras #ifdef DEBUG3
2572639ae9bSBen Gras (void)fprintf(stderr, "split %d/%d\n",
2582639ae9bSBen Gras ((uint16_t *)np)[0] / 2,
2592639ae9bSBen Gras ((uint16_t *)op)[0] / 2);
2602639ae9bSBen Gras #endif
2612639ae9bSBen Gras /* unpin both pages */
2622639ae9bSBen Gras old_bufp->flags &= ~BUF_PIN;
2632639ae9bSBen Gras new_bufp->flags &= ~BUF_PIN;
2642639ae9bSBen Gras return (0);
2652639ae9bSBen Gras }
2662639ae9bSBen Gras
2672639ae9bSBen Gras /*
2682639ae9bSBen Gras * Called when we encounter an overflow or big key/data page during split
2692639ae9bSBen Gras * handling. This is special cased since we have to begin checking whether
2702639ae9bSBen Gras * the key/data pairs fit on their respective pages and because we may need
2712639ae9bSBen Gras * overflow pages for both the old and new pages.
2722639ae9bSBen Gras *
2732639ae9bSBen Gras * The first page might be a page with regular key/data pairs in which case
2742639ae9bSBen Gras * we have a regular overflow condition and just need to go on to the next
2752639ae9bSBen Gras * page or it might be a big key/data pair in which case we need to fix the
2762639ae9bSBen Gras * big key/data pair.
2772639ae9bSBen Gras *
2782639ae9bSBen Gras * Returns:
2792639ae9bSBen Gras * 0 ==> success
2802639ae9bSBen Gras * -1 ==> failure
2812639ae9bSBen Gras */
2822639ae9bSBen Gras static int
ugly_split(HTAB * hashp,uint32_t obucket,BUFHEAD * old_bufp,BUFHEAD * new_bufp,int copyto,int moved)2832639ae9bSBen Gras ugly_split(
2842639ae9bSBen Gras HTAB *hashp,
2852639ae9bSBen Gras uint32_t obucket, /* Same as __split_page. */
2862639ae9bSBen Gras BUFHEAD *old_bufp,
2872639ae9bSBen Gras BUFHEAD *new_bufp,
2882639ae9bSBen Gras int copyto, /* First byte on page which contains key/data values. */
2892639ae9bSBen Gras int moved /* Number of pairs moved to new page. */
2902639ae9bSBen Gras )
2912639ae9bSBen Gras {
2922639ae9bSBen Gras BUFHEAD *bufp; /* Buffer header for ino */
2932639ae9bSBen Gras uint16_t *ino; /* Page keys come off of */
2942639ae9bSBen Gras uint16_t *np; /* New page */
2952639ae9bSBen Gras uint16_t *op; /* Page keys go on to if they aren't moving */
2962639ae9bSBen Gras size_t temp;
2972639ae9bSBen Gras
2982639ae9bSBen Gras BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
2992639ae9bSBen Gras DBT key, val;
3002639ae9bSBen Gras SPLIT_RETURN ret;
3012639ae9bSBen Gras uint16_t n, off, ov_addr, scopyto;
3022639ae9bSBen Gras char *cino; /* Character value of ino */
3032639ae9bSBen Gras
3042639ae9bSBen Gras bufp = old_bufp;
3052639ae9bSBen Gras ino = (uint16_t *)(void *)old_bufp->page;
3062639ae9bSBen Gras np = (uint16_t *)(void *)new_bufp->page;
3072639ae9bSBen Gras op = (uint16_t *)(void *)old_bufp->page;
3082639ae9bSBen Gras last_bfp = NULL;
3092639ae9bSBen Gras scopyto = (uint16_t)copyto; /* ANSI */
3102639ae9bSBen Gras
3112639ae9bSBen Gras n = ino[0] - 1;
3122639ae9bSBen Gras while (n < ino[0]) {
3132639ae9bSBen Gras if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
3142639ae9bSBen Gras if (__big_split(hashp, old_bufp,
3152639ae9bSBen Gras new_bufp, bufp, (int)bufp->addr, obucket, &ret))
3162639ae9bSBen Gras return (-1);
3172639ae9bSBen Gras old_bufp = ret.oldp;
3182639ae9bSBen Gras if (!old_bufp)
3192639ae9bSBen Gras return (-1);
3202639ae9bSBen Gras op = (uint16_t *)(void *)old_bufp->page;
3212639ae9bSBen Gras new_bufp = ret.newp;
3222639ae9bSBen Gras if (!new_bufp)
3232639ae9bSBen Gras return (-1);
3242639ae9bSBen Gras np = (uint16_t *)(void *)new_bufp->page;
3252639ae9bSBen Gras bufp = ret.nextp;
3262639ae9bSBen Gras if (!bufp)
3272639ae9bSBen Gras return (0);
3282639ae9bSBen Gras cino = (char *)bufp->page;
3292639ae9bSBen Gras ino = (uint16_t *)(void *)cino;
3302639ae9bSBen Gras last_bfp = ret.nextp;
3312639ae9bSBen Gras } else if (ino[n + 1] == OVFLPAGE) {
3322639ae9bSBen Gras ov_addr = ino[n];
3332639ae9bSBen Gras /*
3342639ae9bSBen Gras * Fix up the old page -- the extra 2 are the fields
3352639ae9bSBen Gras * which contained the overflow information.
3362639ae9bSBen Gras */
3372639ae9bSBen Gras ino[0] -= (moved + 2);
3382639ae9bSBen Gras temp = sizeof(uint16_t) * (ino[0] + 3);
3392639ae9bSBen Gras _DIAGASSERT(scopyto >= temp);
3402639ae9bSBen Gras FREESPACE(ino) = (uint16_t)(scopyto - temp);
3412639ae9bSBen Gras OFFSET(ino) = scopyto;
3422639ae9bSBen Gras
3432639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)ov_addr, bufp, 0);
3442639ae9bSBen Gras if (!bufp)
3452639ae9bSBen Gras return (-1);
3462639ae9bSBen Gras
3472639ae9bSBen Gras ino = (uint16_t *)(void *)bufp->page;
3482639ae9bSBen Gras n = 1;
3492639ae9bSBen Gras scopyto = hashp->BSIZE;
3502639ae9bSBen Gras moved = 0;
3512639ae9bSBen Gras
3522639ae9bSBen Gras if (last_bfp)
3532639ae9bSBen Gras __free_ovflpage(hashp, last_bfp);
3542639ae9bSBen Gras last_bfp = bufp;
3552639ae9bSBen Gras }
3562639ae9bSBen Gras /* Move regular sized pairs of there are any */
3572639ae9bSBen Gras off = hashp->BSIZE;
3582639ae9bSBen Gras for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
3592639ae9bSBen Gras cino = (char *)(void *)ino;
3602639ae9bSBen Gras key.data = (uint8_t *)cino + ino[n];
3612639ae9bSBen Gras key.size = off - ino[n];
3622639ae9bSBen Gras val.data = (uint8_t *)cino + ino[n + 1];
3632639ae9bSBen Gras val.size = ino[n] - ino[n + 1];
3642639ae9bSBen Gras off = ino[n + 1];
3652639ae9bSBen Gras
3662639ae9bSBen Gras if (__call_hash(hashp, key.data, (int)key.size) == obucket) {
3672639ae9bSBen Gras /* Keep on old page */
3682639ae9bSBen Gras if (PAIRFITS(op, (&key), (&val)))
3692639ae9bSBen Gras putpair((char *)(void *)op, &key, &val);
3702639ae9bSBen Gras else {
3712639ae9bSBen Gras old_bufp =
3722639ae9bSBen Gras __add_ovflpage(hashp, old_bufp);
3732639ae9bSBen Gras if (!old_bufp)
3742639ae9bSBen Gras return (-1);
3752639ae9bSBen Gras op = (uint16_t *)(void *)old_bufp->page;
3762639ae9bSBen Gras putpair((char *)(void *)op, &key, &val);
3772639ae9bSBen Gras }
3782639ae9bSBen Gras old_bufp->flags |= BUF_MOD;
3792639ae9bSBen Gras } else {
3802639ae9bSBen Gras /* Move to new page */
3812639ae9bSBen Gras if (PAIRFITS(np, (&key), (&val)))
3822639ae9bSBen Gras putpair((char *)(void *)np, &key, &val);
3832639ae9bSBen Gras else {
3842639ae9bSBen Gras new_bufp =
3852639ae9bSBen Gras __add_ovflpage(hashp, new_bufp);
3862639ae9bSBen Gras if (!new_bufp)
3872639ae9bSBen Gras return (-1);
3882639ae9bSBen Gras np = (uint16_t *)(void *)new_bufp->page;
3892639ae9bSBen Gras putpair((char *)(void *)np, &key, &val);
3902639ae9bSBen Gras }
3912639ae9bSBen Gras new_bufp->flags |= BUF_MOD;
3922639ae9bSBen Gras }
3932639ae9bSBen Gras }
3942639ae9bSBen Gras }
3952639ae9bSBen Gras if (last_bfp)
3962639ae9bSBen Gras __free_ovflpage(hashp, last_bfp);
3972639ae9bSBen Gras return (0);
3982639ae9bSBen Gras }
3992639ae9bSBen Gras
4002639ae9bSBen Gras /*
4012639ae9bSBen Gras * Add the given pair to the page
4022639ae9bSBen Gras *
4032639ae9bSBen Gras * Returns:
4042639ae9bSBen Gras * 0 ==> OK
4052639ae9bSBen Gras * 1 ==> failure
4062639ae9bSBen Gras */
4072639ae9bSBen Gras int
__addel(HTAB * hashp,BUFHEAD * bufp,const DBT * key,const DBT * val)4082639ae9bSBen Gras __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
4092639ae9bSBen Gras {
4102639ae9bSBen Gras uint16_t *bp, *sop;
4112639ae9bSBen Gras int do_expand;
4122639ae9bSBen Gras
4132639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
4142639ae9bSBen Gras do_expand = 0;
4152639ae9bSBen Gras while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
4162639ae9bSBen Gras /* Exception case */
4172639ae9bSBen Gras if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
4182639ae9bSBen Gras /* This is the last page of a big key/data pair
4192639ae9bSBen Gras and we need to add another page */
4202639ae9bSBen Gras break;
4212639ae9bSBen Gras else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
4222639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)bp[bp[0] - 1], bufp,
4232639ae9bSBen Gras 0);
4242639ae9bSBen Gras if (!bufp)
4252639ae9bSBen Gras return (-1);
4262639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
4272639ae9bSBen Gras } else if (bp[bp[0]] != OVFLPAGE) {
4282639ae9bSBen Gras /* Short key/data pairs, no more pages */
4292639ae9bSBen Gras break;
4302639ae9bSBen Gras } else {
4312639ae9bSBen Gras /* Try to squeeze key on this page */
4322639ae9bSBen Gras if (bp[2] >= REAL_KEY &&
4332639ae9bSBen Gras FREESPACE(bp) >= PAIRSIZE(key, val)) {
4342639ae9bSBen Gras squeeze_key(bp, key, val);
4352639ae9bSBen Gras goto stats;
4362639ae9bSBen Gras } else {
4372639ae9bSBen Gras bufp = __get_buf(hashp,
4382639ae9bSBen Gras (uint32_t)bp[bp[0] - 1], bufp, 0);
4392639ae9bSBen Gras if (!bufp)
4402639ae9bSBen Gras return (-1);
4412639ae9bSBen Gras bp = (uint16_t *)(void *)bufp->page;
4422639ae9bSBen Gras }
4432639ae9bSBen Gras }
4442639ae9bSBen Gras
4452639ae9bSBen Gras if (PAIRFITS(bp, key, val))
4462639ae9bSBen Gras putpair(bufp->page, key, val);
4472639ae9bSBen Gras else {
4482639ae9bSBen Gras do_expand = 1;
4492639ae9bSBen Gras bufp = __add_ovflpage(hashp, bufp);
4502639ae9bSBen Gras if (!bufp)
4512639ae9bSBen Gras return (-1);
4522639ae9bSBen Gras sop = (uint16_t *)(void *)bufp->page;
4532639ae9bSBen Gras
4542639ae9bSBen Gras if (PAIRFITS(sop, key, val))
4552639ae9bSBen Gras putpair((char *)(void *)sop, key, val);
4562639ae9bSBen Gras else
4572639ae9bSBen Gras if (__big_insert(hashp, bufp, key, val))
4582639ae9bSBen Gras return (-1);
4592639ae9bSBen Gras }
4602639ae9bSBen Gras stats:
4612639ae9bSBen Gras bufp->flags |= BUF_MOD;
4622639ae9bSBen Gras /*
4632639ae9bSBen Gras * If the average number of keys per bucket exceeds the fill factor,
4642639ae9bSBen Gras * expand the table.
4652639ae9bSBen Gras */
4662639ae9bSBen Gras hashp->NKEYS++;
4672639ae9bSBen Gras if (do_expand ||
4682639ae9bSBen Gras (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
4692639ae9bSBen Gras return (__expand_table(hashp));
4702639ae9bSBen Gras return (0);
4712639ae9bSBen Gras }
4722639ae9bSBen Gras
4732639ae9bSBen Gras /*
4742639ae9bSBen Gras *
4752639ae9bSBen Gras * Returns:
4762639ae9bSBen Gras * pointer on success
4772639ae9bSBen Gras * NULL on error
4782639ae9bSBen Gras */
4792639ae9bSBen Gras BUFHEAD *
__add_ovflpage(HTAB * hashp,BUFHEAD * bufp)4802639ae9bSBen Gras __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
4812639ae9bSBen Gras {
4822639ae9bSBen Gras uint16_t *sp;
4832639ae9bSBen Gras uint16_t ndx, ovfl_num;
4842639ae9bSBen Gras size_t temp;
4852639ae9bSBen Gras #ifdef DEBUG1
4862639ae9bSBen Gras int tmp1, tmp2;
4872639ae9bSBen Gras #endif
4882639ae9bSBen Gras sp = (uint16_t *)(void *)bufp->page;
4892639ae9bSBen Gras
4902639ae9bSBen Gras /* Check if we are dynamically determining the fill factor */
4912639ae9bSBen Gras if (hashp->FFACTOR == DEF_FFACTOR) {
4922639ae9bSBen Gras hashp->FFACTOR = (uint32_t)sp[0] >> 1;
4932639ae9bSBen Gras if (hashp->FFACTOR < MIN_FFACTOR)
4942639ae9bSBen Gras hashp->FFACTOR = MIN_FFACTOR;
4952639ae9bSBen Gras }
4962639ae9bSBen Gras bufp->flags |= BUF_MOD;
4972639ae9bSBen Gras ovfl_num = overflow_page(hashp);
4982639ae9bSBen Gras #ifdef DEBUG1
4992639ae9bSBen Gras tmp1 = bufp->addr;
5002639ae9bSBen Gras tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
5012639ae9bSBen Gras #endif
5022639ae9bSBen Gras if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, (uint32_t)ovfl_num,
5032639ae9bSBen Gras bufp, 1)))
5042639ae9bSBen Gras return (NULL);
5052639ae9bSBen Gras bufp->ovfl->flags |= BUF_MOD;
5062639ae9bSBen Gras #ifdef DEBUG1
5072639ae9bSBen Gras (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
5082639ae9bSBen Gras tmp1, tmp2, bufp->ovfl->addr);
5092639ae9bSBen Gras #endif
5102639ae9bSBen Gras ndx = sp[0];
5112639ae9bSBen Gras /*
5122639ae9bSBen Gras * Since a pair is allocated on a page only if there's room to add
5132639ae9bSBen Gras * an overflow page, we know that the OVFL information will fit on
5142639ae9bSBen Gras * the page.
5152639ae9bSBen Gras */
5162639ae9bSBen Gras sp[ndx + 4] = OFFSET(sp);
5172639ae9bSBen Gras temp = FREESPACE(sp);
5182639ae9bSBen Gras _DIAGASSERT(temp >= OVFLSIZE);
5192639ae9bSBen Gras sp[ndx + 3] = (uint16_t)(temp - OVFLSIZE);
5202639ae9bSBen Gras sp[ndx + 1] = ovfl_num;
5212639ae9bSBen Gras sp[ndx + 2] = OVFLPAGE;
5222639ae9bSBen Gras sp[0] = ndx + 2;
5232639ae9bSBen Gras #ifdef HASH_STATISTICS
5242639ae9bSBen Gras hash_overflows++;
5252639ae9bSBen Gras #endif
5262639ae9bSBen Gras return (bufp->ovfl);
5272639ae9bSBen Gras }
5282639ae9bSBen Gras
5292639ae9bSBen Gras /*
5302639ae9bSBen Gras * Returns:
5312639ae9bSBen Gras * 0 indicates SUCCESS
5322639ae9bSBen Gras * -1 indicates FAILURE
5332639ae9bSBen Gras */
5342639ae9bSBen Gras int
__get_page(HTAB * hashp,char * p,uint32_t bucket,int is_bucket,int is_disk,int is_bitmap)5352639ae9bSBen Gras __get_page(HTAB *hashp, char *p, uint32_t bucket, int is_bucket, int is_disk,
5362639ae9bSBen Gras int is_bitmap)
5372639ae9bSBen Gras {
5382639ae9bSBen Gras int fd, page, size;
5392639ae9bSBen Gras ssize_t rsize;
5402639ae9bSBen Gras uint16_t *bp;
5412639ae9bSBen Gras size_t temp;
5422639ae9bSBen Gras
5432639ae9bSBen Gras fd = hashp->fp;
5442639ae9bSBen Gras size = hashp->BSIZE;
5452639ae9bSBen Gras
5462639ae9bSBen Gras if ((fd == -1) || !is_disk) {
5472639ae9bSBen Gras PAGE_INIT(p);
5482639ae9bSBen Gras return (0);
5492639ae9bSBen Gras }
5502639ae9bSBen Gras if (is_bucket)
5512639ae9bSBen Gras page = BUCKET_TO_PAGE(bucket);
5522639ae9bSBen Gras else
5532639ae9bSBen Gras page = OADDR_TO_PAGE(bucket);
5542639ae9bSBen Gras if ((rsize = pread(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1)
5552639ae9bSBen Gras return (-1);
5562639ae9bSBen Gras bp = (uint16_t *)(void *)p;
5572639ae9bSBen Gras if (!rsize)
5582639ae9bSBen Gras bp[0] = 0; /* We hit the EOF, so initialize a new page */
5592639ae9bSBen Gras else
5602639ae9bSBen Gras if (rsize != size) {
5612639ae9bSBen Gras errno = EFTYPE;
5622639ae9bSBen Gras return (-1);
5632639ae9bSBen Gras }
5642639ae9bSBen Gras if (!is_bitmap && !bp[0]) {
5652639ae9bSBen Gras PAGE_INIT(p);
5662639ae9bSBen Gras } else
5672639ae9bSBen Gras if (hashp->LORDER != BYTE_ORDER) {
5682639ae9bSBen Gras int i, max;
5692639ae9bSBen Gras
5702639ae9bSBen Gras if (is_bitmap) {
5712639ae9bSBen Gras max = (uint32_t)hashp->BSIZE >> 2; /* divide by 4 */
5722639ae9bSBen Gras for (i = 0; i < max; i++)
5732639ae9bSBen Gras M_32_SWAP(((int *)(void *)p)[i]);
5742639ae9bSBen Gras } else {
5752639ae9bSBen Gras M_16_SWAP(bp[0]);
5762639ae9bSBen Gras max = bp[0] + 2;
5772639ae9bSBen Gras for (i = 1; i <= max; i++)
5782639ae9bSBen Gras M_16_SWAP(bp[i]);
5792639ae9bSBen Gras }
5802639ae9bSBen Gras }
5812639ae9bSBen Gras return (0);
5822639ae9bSBen Gras }
5832639ae9bSBen Gras
5842639ae9bSBen Gras /*
5852639ae9bSBen Gras * Write page p to disk
5862639ae9bSBen Gras *
5872639ae9bSBen Gras * Returns:
5882639ae9bSBen Gras * 0 ==> OK
5892639ae9bSBen Gras * -1 ==>failure
5902639ae9bSBen Gras */
5912639ae9bSBen Gras int
__put_page(HTAB * hashp,char * p,uint32_t bucket,int is_bucket,int is_bitmap)5922639ae9bSBen Gras __put_page(HTAB *hashp, char *p, uint32_t bucket, int is_bucket, int is_bitmap)
5932639ae9bSBen Gras {
5942639ae9bSBen Gras int fd, page, size;
5952639ae9bSBen Gras ssize_t wsize;
5962639ae9bSBen Gras
5972639ae9bSBen Gras size = hashp->BSIZE;
598*84d9c625SLionel Sambuc if ((hashp->fp == -1) && (hashp->fp = __dbtemp("_hash", NULL)) == -1)
5992639ae9bSBen Gras return (-1);
6002639ae9bSBen Gras fd = hashp->fp;
6012639ae9bSBen Gras
6022639ae9bSBen Gras if (hashp->LORDER != BYTE_ORDER) {
6032639ae9bSBen Gras int i;
6042639ae9bSBen Gras int max;
6052639ae9bSBen Gras
6062639ae9bSBen Gras if (is_bitmap) {
6072639ae9bSBen Gras max = (uint32_t)hashp->BSIZE >> 2; /* divide by 4 */
6082639ae9bSBen Gras for (i = 0; i < max; i++)
6092639ae9bSBen Gras M_32_SWAP(((int *)(void *)p)[i]);
6102639ae9bSBen Gras } else {
6112639ae9bSBen Gras max = ((uint16_t *)(void *)p)[0] + 2;
6122639ae9bSBen Gras for (i = 0; i <= max; i++)
6132639ae9bSBen Gras M_16_SWAP(((uint16_t *)(void *)p)[i]);
6142639ae9bSBen Gras }
6152639ae9bSBen Gras }
6162639ae9bSBen Gras if (is_bucket)
6172639ae9bSBen Gras page = BUCKET_TO_PAGE(bucket);
6182639ae9bSBen Gras else
6192639ae9bSBen Gras page = OADDR_TO_PAGE(bucket);
6202639ae9bSBen Gras if ((wsize = pwrite(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1)
6212639ae9bSBen Gras /* Errno is set */
6222639ae9bSBen Gras return (-1);
6232639ae9bSBen Gras if (wsize != size) {
6242639ae9bSBen Gras errno = EFTYPE;
6252639ae9bSBen Gras return (-1);
6262639ae9bSBen Gras }
6272639ae9bSBen Gras return (0);
6282639ae9bSBen Gras }
6292639ae9bSBen Gras
6302639ae9bSBen Gras #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
6312639ae9bSBen Gras /*
6322639ae9bSBen Gras * Initialize a new bitmap page. Bitmap pages are left in memory
6332639ae9bSBen Gras * once they are read in.
6342639ae9bSBen Gras */
6352639ae9bSBen Gras int
__ibitmap(HTAB * hashp,int pnum,int nbits,int ndx)6362639ae9bSBen Gras __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
6372639ae9bSBen Gras {
6382639ae9bSBen Gras uint32_t *ip;
6392639ae9bSBen Gras int clearbytes, clearints;
6402639ae9bSBen Gras
6412639ae9bSBen Gras if ((ip = malloc((size_t)hashp->BSIZE)) == NULL)
6422639ae9bSBen Gras return (1);
6432639ae9bSBen Gras hashp->nmaps++;
6442639ae9bSBen Gras clearints = ((uint32_t)(nbits - 1) >> INT_BYTE_SHIFT) + 1;
6452639ae9bSBen Gras clearbytes = clearints << INT_TO_BYTE;
6462639ae9bSBen Gras (void)memset(ip, 0, (size_t)clearbytes);
6472639ae9bSBen Gras (void)memset(((char *)(void *)ip) + clearbytes, 0xFF,
6482639ae9bSBen Gras (size_t)(hashp->BSIZE - clearbytes));
6492639ae9bSBen Gras ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
6502639ae9bSBen Gras SETBIT(ip, 0);
6512639ae9bSBen Gras hashp->BITMAPS[ndx] = (uint16_t)pnum;
6522639ae9bSBen Gras hashp->mapp[ndx] = ip;
6532639ae9bSBen Gras return (0);
6542639ae9bSBen Gras }
6552639ae9bSBen Gras
6562639ae9bSBen Gras static uint32_t
first_free(uint32_t map)6572639ae9bSBen Gras first_free(uint32_t map)
6582639ae9bSBen Gras {
6592639ae9bSBen Gras uint32_t i, mask;
6602639ae9bSBen Gras
6612639ae9bSBen Gras mask = 0x1;
6622639ae9bSBen Gras for (i = 0; i < BITS_PER_MAP; i++) {
6632639ae9bSBen Gras if (!(mask & map))
6642639ae9bSBen Gras return (i);
6652639ae9bSBen Gras mask = mask << 1;
6662639ae9bSBen Gras }
6672639ae9bSBen Gras return (i);
6682639ae9bSBen Gras }
6692639ae9bSBen Gras
6702639ae9bSBen Gras static uint16_t
overflow_page(HTAB * hashp)6712639ae9bSBen Gras overflow_page(HTAB *hashp)
6722639ae9bSBen Gras {
6732639ae9bSBen Gras uint32_t *freep = NULL;
6742639ae9bSBen Gras int max_free, offset, splitnum;
6752639ae9bSBen Gras uint16_t addr;
6762639ae9bSBen Gras int bit, first_page, free_bit, free_page, i, in_use_bits, j;
6772639ae9bSBen Gras #ifdef DEBUG2
6782639ae9bSBen Gras int tmp1, tmp2;
6792639ae9bSBen Gras #endif
6802639ae9bSBen Gras splitnum = hashp->OVFL_POINT;
6812639ae9bSBen Gras max_free = hashp->SPARES[splitnum];
6822639ae9bSBen Gras
6832639ae9bSBen Gras free_page = (uint32_t)(max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
6842639ae9bSBen Gras free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
6852639ae9bSBen Gras
6862639ae9bSBen Gras /* Look through all the free maps to find the first free block */
6872639ae9bSBen Gras first_page = (uint32_t)hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
6882639ae9bSBen Gras for ( i = first_page; i <= free_page; i++ ) {
6892639ae9bSBen Gras if (!(freep = (uint32_t *)hashp->mapp[i]) &&
6902639ae9bSBen Gras !(freep = fetch_bitmap(hashp, i)))
6912639ae9bSBen Gras return (0);
6922639ae9bSBen Gras if (i == free_page)
6932639ae9bSBen Gras in_use_bits = free_bit;
6942639ae9bSBen Gras else
6952639ae9bSBen Gras in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
6962639ae9bSBen Gras
6972639ae9bSBen Gras if (i == first_page) {
6982639ae9bSBen Gras bit = hashp->LAST_FREED &
6992639ae9bSBen Gras ((hashp->BSIZE << BYTE_SHIFT) - 1);
7002639ae9bSBen Gras j = bit / BITS_PER_MAP;
7012639ae9bSBen Gras bit = bit & ~(BITS_PER_MAP - 1);
7022639ae9bSBen Gras } else {
7032639ae9bSBen Gras bit = 0;
7042639ae9bSBen Gras j = 0;
7052639ae9bSBen Gras }
7062639ae9bSBen Gras for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
7072639ae9bSBen Gras if (freep[j] != ALL_SET)
7082639ae9bSBen Gras goto found;
7092639ae9bSBen Gras }
7102639ae9bSBen Gras
7112639ae9bSBen Gras /* No Free Page Found */
7122639ae9bSBen Gras hashp->LAST_FREED = hashp->SPARES[splitnum];
7132639ae9bSBen Gras hashp->SPARES[splitnum]++;
7142639ae9bSBen Gras offset = hashp->SPARES[splitnum] -
7152639ae9bSBen Gras (splitnum ? hashp->SPARES[splitnum - 1] : 0);
7162639ae9bSBen Gras
7172639ae9bSBen Gras #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
7182639ae9bSBen Gras if (offset > SPLITMASK) {
7192639ae9bSBen Gras if (++splitnum >= NCACHED) {
7202639ae9bSBen Gras (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
7212639ae9bSBen Gras errno = EFBIG;
7222639ae9bSBen Gras return (0);
7232639ae9bSBen Gras }
7242639ae9bSBen Gras hashp->OVFL_POINT = splitnum;
7252639ae9bSBen Gras hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
7262639ae9bSBen Gras hashp->SPARES[splitnum-1]--;
7272639ae9bSBen Gras offset = 1;
7282639ae9bSBen Gras }
7292639ae9bSBen Gras
7302639ae9bSBen Gras /* Check if we need to allocate a new bitmap page */
7312639ae9bSBen Gras if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
7322639ae9bSBen Gras free_page++;
7332639ae9bSBen Gras if (free_page >= NCACHED) {
7342639ae9bSBen Gras (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
7352639ae9bSBen Gras errno = EFBIG;
7362639ae9bSBen Gras return (0);
7372639ae9bSBen Gras }
7382639ae9bSBen Gras /*
7392639ae9bSBen Gras * This is tricky. The 1 indicates that you want the new page
7402639ae9bSBen Gras * allocated with 1 clear bit. Actually, you are going to
7412639ae9bSBen Gras * allocate 2 pages from this map. The first is going to be
7422639ae9bSBen Gras * the map page, the second is the overflow page we were
7432639ae9bSBen Gras * looking for. The init_bitmap routine automatically, sets
7442639ae9bSBen Gras * the first bit of itself to indicate that the bitmap itself
7452639ae9bSBen Gras * is in use. We would explicitly set the second bit, but
7462639ae9bSBen Gras * don't have to if we tell init_bitmap not to leave it clear
7472639ae9bSBen Gras * in the first place.
7482639ae9bSBen Gras */
7492639ae9bSBen Gras if (__ibitmap(hashp,
7502639ae9bSBen Gras (int)OADDR_OF(splitnum, offset), 1, free_page))
7512639ae9bSBen Gras return (0);
7522639ae9bSBen Gras hashp->SPARES[splitnum]++;
7532639ae9bSBen Gras #ifdef DEBUG2
7542639ae9bSBen Gras free_bit = 2;
7552639ae9bSBen Gras #endif
7562639ae9bSBen Gras offset++;
7572639ae9bSBen Gras if (offset > SPLITMASK) {
7582639ae9bSBen Gras if (++splitnum >= NCACHED) {
7592639ae9bSBen Gras (void)write(STDERR_FILENO, OVMSG,
7602639ae9bSBen Gras sizeof(OVMSG) - 1);
7612639ae9bSBen Gras errno = EFBIG;
7622639ae9bSBen Gras return (0);
7632639ae9bSBen Gras }
7642639ae9bSBen Gras hashp->OVFL_POINT = splitnum;
7652639ae9bSBen Gras hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
7662639ae9bSBen Gras hashp->SPARES[splitnum-1]--;
7672639ae9bSBen Gras offset = 0;
7682639ae9bSBen Gras }
7692639ae9bSBen Gras } else {
7702639ae9bSBen Gras /*
7712639ae9bSBen Gras * Free_bit addresses the last used bit. Bump it to address
7722639ae9bSBen Gras * the first available bit.
7732639ae9bSBen Gras */
7742639ae9bSBen Gras free_bit++;
7752639ae9bSBen Gras SETBIT(freep, free_bit);
7762639ae9bSBen Gras }
7772639ae9bSBen Gras
7782639ae9bSBen Gras /* Calculate address of the new overflow page */
7792639ae9bSBen Gras addr = OADDR_OF(splitnum, offset);
7802639ae9bSBen Gras #ifdef DEBUG2
7812639ae9bSBen Gras (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
7822639ae9bSBen Gras addr, free_bit, free_page);
7832639ae9bSBen Gras #endif
7842639ae9bSBen Gras return (addr);
7852639ae9bSBen Gras
7862639ae9bSBen Gras found:
7872639ae9bSBen Gras bit = bit + first_free(freep[j]);
7882639ae9bSBen Gras SETBIT(freep, bit);
7892639ae9bSBen Gras #ifdef DEBUG2
7902639ae9bSBen Gras tmp1 = bit;
7912639ae9bSBen Gras tmp2 = i;
7922639ae9bSBen Gras #endif
7932639ae9bSBen Gras /*
7942639ae9bSBen Gras * Bits are addressed starting with 0, but overflow pages are addressed
7952639ae9bSBen Gras * beginning at 1. Bit is a bit addressnumber, so we need to increment
7962639ae9bSBen Gras * it to convert it to a page number.
7972639ae9bSBen Gras */
7982639ae9bSBen Gras bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
7992639ae9bSBen Gras if (bit >= hashp->LAST_FREED)
8002639ae9bSBen Gras hashp->LAST_FREED = bit - 1;
8012639ae9bSBen Gras
8022639ae9bSBen Gras /* Calculate the split number for this page */
8032639ae9bSBen Gras for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
8042639ae9bSBen Gras offset = (i ? bit - hashp->SPARES[i - 1] : bit);
8052639ae9bSBen Gras if (offset >= SPLITMASK) {
8062639ae9bSBen Gras (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
8072639ae9bSBen Gras errno = EFBIG;
8082639ae9bSBen Gras return (0); /* Out of overflow pages */
8092639ae9bSBen Gras }
8102639ae9bSBen Gras addr = OADDR_OF(i, offset);
8112639ae9bSBen Gras #ifdef DEBUG2
8122639ae9bSBen Gras (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
8132639ae9bSBen Gras addr, tmp1, tmp2);
8142639ae9bSBen Gras #endif
8152639ae9bSBen Gras
8162639ae9bSBen Gras /* Allocate and return the overflow page */
8172639ae9bSBen Gras return (addr);
8182639ae9bSBen Gras }
8192639ae9bSBen Gras
8202639ae9bSBen Gras /*
8212639ae9bSBen Gras * Mark this overflow page as free.
8222639ae9bSBen Gras */
8232639ae9bSBen Gras void
__free_ovflpage(HTAB * hashp,BUFHEAD * obufp)8242639ae9bSBen Gras __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
8252639ae9bSBen Gras {
8262639ae9bSBen Gras uint16_t addr;
8272639ae9bSBen Gras uint32_t *freep;
8282639ae9bSBen Gras int bit_address, free_page, free_bit;
8292639ae9bSBen Gras uint16_t ndx;
8302639ae9bSBen Gras
8312639ae9bSBen Gras addr = obufp->addr;
8322639ae9bSBen Gras #ifdef DEBUG1
8332639ae9bSBen Gras (void)fprintf(stderr, "Freeing %d\n", addr);
8342639ae9bSBen Gras #endif
8352639ae9bSBen Gras ndx = (((uint32_t)addr) >> SPLITSHIFT);
8362639ae9bSBen Gras bit_address =
8372639ae9bSBen Gras (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
8382639ae9bSBen Gras if (bit_address < hashp->LAST_FREED)
8392639ae9bSBen Gras hashp->LAST_FREED = bit_address;
8402639ae9bSBen Gras free_page = ((uint32_t)bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
8412639ae9bSBen Gras free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
8422639ae9bSBen Gras
8432639ae9bSBen Gras if (!(freep = hashp->mapp[free_page]))
8442639ae9bSBen Gras freep = fetch_bitmap(hashp, free_page);
8452639ae9bSBen Gras /*
8462639ae9bSBen Gras * This had better never happen. It means we tried to read a bitmap
8472639ae9bSBen Gras * that has already had overflow pages allocated off it, and we
8482639ae9bSBen Gras * failed to read it from the file.
8492639ae9bSBen Gras */
8502639ae9bSBen Gras _DIAGASSERT(freep != NULL);
8512639ae9bSBen Gras CLRBIT(freep, free_bit);
8522639ae9bSBen Gras #ifdef DEBUG2
8532639ae9bSBen Gras (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
8542639ae9bSBen Gras obufp->addr, free_bit, free_page);
8552639ae9bSBen Gras #endif
8562639ae9bSBen Gras __reclaim_buf(hashp, obufp);
8572639ae9bSBen Gras }
8582639ae9bSBen Gras
8592639ae9bSBen Gras /*
8602639ae9bSBen Gras * We have to know that the key will fit, but the last entry on the page is
8612639ae9bSBen Gras * an overflow pair, so we need to shift things.
8622639ae9bSBen Gras */
8632639ae9bSBen Gras static void
squeeze_key(uint16_t * sp,const DBT * key,const DBT * val)8642639ae9bSBen Gras squeeze_key(uint16_t *sp, const DBT *key, const DBT *val)
8652639ae9bSBen Gras {
8662639ae9bSBen Gras char *p;
8672639ae9bSBen Gras uint16_t free_space, n, off, pageno;
8682639ae9bSBen Gras size_t temp;
8692639ae9bSBen Gras
8702639ae9bSBen Gras p = (char *)(void *)sp;
8712639ae9bSBen Gras n = sp[0];
8722639ae9bSBen Gras free_space = FREESPACE(sp);
8732639ae9bSBen Gras off = OFFSET(sp);
8742639ae9bSBen Gras
8752639ae9bSBen Gras pageno = sp[n - 1];
8762639ae9bSBen Gras _DIAGASSERT(off >= key->size);
8772639ae9bSBen Gras off -= (uint16_t)key->size;
8782639ae9bSBen Gras sp[n - 1] = off;
8792639ae9bSBen Gras memmove(p + off, key->data, key->size);
8802639ae9bSBen Gras _DIAGASSERT(off >= val->size);
8812639ae9bSBen Gras off -= (uint16_t)val->size;
8822639ae9bSBen Gras sp[n] = off;
8832639ae9bSBen Gras memmove(p + off, val->data, val->size);
8842639ae9bSBen Gras sp[0] = n + 2;
8852639ae9bSBen Gras sp[n + 1] = pageno;
8862639ae9bSBen Gras sp[n + 2] = OVFLPAGE;
8872639ae9bSBen Gras temp = PAIRSIZE(key, val);
8882639ae9bSBen Gras _DIAGASSERT(free_space >= temp);
8892639ae9bSBen Gras FREESPACE(sp) = (uint16_t)(free_space - temp);
8902639ae9bSBen Gras OFFSET(sp) = off;
8912639ae9bSBen Gras }
8922639ae9bSBen Gras
8932639ae9bSBen Gras static uint32_t *
fetch_bitmap(HTAB * hashp,int ndx)8942639ae9bSBen Gras fetch_bitmap(HTAB *hashp, int ndx)
8952639ae9bSBen Gras {
8962639ae9bSBen Gras if (ndx >= hashp->nmaps)
8972639ae9bSBen Gras return (NULL);
8982639ae9bSBen Gras if ((hashp->mapp[ndx] = malloc((size_t)hashp->BSIZE)) == NULL)
8992639ae9bSBen Gras return (NULL);
9002639ae9bSBen Gras if (__get_page(hashp,
9012639ae9bSBen Gras (char *)(void *)hashp->mapp[ndx], (uint32_t)hashp->BITMAPS[ndx], 0, 1, 1)) {
9022639ae9bSBen Gras free(hashp->mapp[ndx]);
9032639ae9bSBen Gras return (NULL);
9042639ae9bSBen Gras }
9052639ae9bSBen Gras return (hashp->mapp[ndx]);
9062639ae9bSBen Gras }
9072639ae9bSBen Gras
9082639ae9bSBen Gras #ifdef DEBUG4
9092639ae9bSBen Gras void print_chain(HTAB *, uint32_t);
9102639ae9bSBen Gras void
print_chain(HTAB * hashp,uint32_t addr)9112639ae9bSBen Gras print_chain(HTAB *hashp, uint32_t addr)
9122639ae9bSBen Gras {
9132639ae9bSBen Gras BUFHEAD *bufp;
9142639ae9bSBen Gras uint16_t *bp, oaddr;
9152639ae9bSBen Gras
9162639ae9bSBen Gras (void)fprintf(stderr, "%d ", addr);
9172639ae9bSBen Gras bufp = __get_buf(hashp, addr, NULL, 0);
9182639ae9bSBen Gras bp = (uint16_t *)bufp->page;
9192639ae9bSBen Gras while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
9202639ae9bSBen Gras ((bp[0] > 2) && bp[2] < REAL_KEY))) {
9212639ae9bSBen Gras oaddr = bp[bp[0] - 1];
9222639ae9bSBen Gras (void)fprintf(stderr, "%d ", (int)oaddr);
9232639ae9bSBen Gras bufp = __get_buf(hashp, (uint32_t)oaddr, bufp, 0);
9242639ae9bSBen Gras bp = (uint16_t *)bufp->page;
9252639ae9bSBen Gras }
9262639ae9bSBen Gras (void)fprintf(stderr, "\n");
9272639ae9bSBen Gras }
9282639ae9bSBen Gras #endif
929