Home | History | Annotate | Line # | Download | only in hash
      1  1.38  christos /*	$NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $	*/
      2   1.8       cgd 
      3   1.1       cgd /*-
      4   1.7       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.18       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.30     joerg #if HAVE_NBTOOL_CONFIG_H
     36  1.30     joerg #include "nbtool_config.h"
     37  1.30     joerg #endif
     38  1.30     joerg 
     39  1.11  christos #include <sys/cdefs.h>
     40  1.38  christos __RCSID("$NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $");
     41   1.1       cgd 
     42  1.14    kleink #include "namespace.h"
     43   1.1       cgd #include <sys/param.h>
     44   1.1       cgd #include <sys/stat.h>
     45   1.1       cgd 
     46   1.1       cgd #include <errno.h>
     47   1.1       cgd #include <fcntl.h>
     48   1.1       cgd #include <stdio.h>
     49   1.1       cgd #include <stdlib.h>
     50   1.1       cgd #include <string.h>
     51   1.1       cgd #include <unistd.h>
     52   1.1       cgd #include <assert.h>
     53   1.1       cgd 
     54   1.1       cgd #include <db.h>
     55   1.1       cgd #include "hash.h"
     56   1.1       cgd #include "page.h"
     57   1.1       cgd #include "extern.h"
     58   1.1       cgd 
     59  1.27  christos static int   alloc_segs(HTAB *, int);
     60  1.27  christos static int   flush_meta(HTAB *);
     61  1.27  christos static int   hash_access(HTAB *, ACTION, DBT *, DBT *);
     62  1.27  christos static int   hash_close(DB *);
     63  1.28     joerg static int   hash_delete(const DB *, const DBT *, uint32_t);
     64  1.27  christos static int   hash_fd(const DB *);
     65  1.28     joerg static int   hash_get(const DB *, const DBT *, DBT *, uint32_t);
     66  1.28     joerg static int   hash_put(const DB *, DBT *, const DBT *, uint32_t);
     67  1.27  christos static void *hash_realloc(SEGMENT **, size_t, size_t);
     68  1.28     joerg static int   hash_seq(const DB *, DBT *, DBT *, uint32_t);
     69  1.28     joerg static int   hash_sync(const DB *, uint32_t);
     70  1.27  christos static int   hdestroy(HTAB *);
     71  1.27  christos static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
     72  1.27  christos static int   init_htab(HTAB *, size_t);
     73   1.1       cgd #if BYTE_ORDER == LITTLE_ENDIAN
     74  1.27  christos static void  swap_header(HTAB *);
     75  1.27  christos static void  swap_header_copy(HASHHDR *, HASHHDR *);
     76   1.1       cgd #endif
     77   1.1       cgd 
     78   1.1       cgd /* Fast arithmetic, relying on powers of 2, */
     79   1.1       cgd #define MOD(x, y)		((x) & ((y) - 1))
     80   1.1       cgd 
     81   1.1       cgd #define RETURN_ERROR(ERR, LOC)	{ save_errno = ERR; goto LOC; }
     82   1.1       cgd 
     83   1.1       cgd /* Return values */
     84   1.1       cgd #define	SUCCESS	 (0)
     85   1.1       cgd #define	ERROR	(-1)
     86   1.1       cgd #define	ABNORMAL (1)
     87   1.1       cgd 
     88   1.1       cgd #ifdef HASH_STATISTICS
     89   1.7       cgd int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
     90   1.1       cgd #endif
     91   1.1       cgd 
     92   1.1       cgd /************************** INTERFACE ROUTINES ***************************/
     93   1.1       cgd /* OPEN/CLOSE */
     94   1.1       cgd 
     95  1.15  christos /* ARGSUSED */
     96  1.27  christos DB *
     97  1.27  christos __hash_open(const char *file, int flags, mode_t mode, const HASHINFO *info,
     98  1.27  christos     int dflags)
     99   1.1       cgd {
    100   1.1       cgd 	HTAB *hashp;
    101   1.1       cgd 	struct stat statbuf;
    102   1.1       cgd 	DB *dbp;
    103  1.27  christos 	int bpages, new_table, nsegs, save_errno;
    104  1.27  christos 	ssize_t hdrsize;
    105   1.1       cgd 
    106   1.1       cgd 	if ((flags & O_ACCMODE) == O_WRONLY) {
    107   1.1       cgd 		errno = EINVAL;
    108   1.1       cgd 		return (NULL);
    109   1.1       cgd 	}
    110   1.1       cgd 
    111  1.27  christos 	if (!(hashp = calloc(1, sizeof(HTAB))))
    112   1.1       cgd 		return (NULL);
    113   1.1       cgd 	hashp->fp = -1;
    114   1.4       cgd 
    115   1.1       cgd 	/*
    116   1.4       cgd 	 * Even if user wants write only, we need to be able to read
    117   1.4       cgd 	 * the actual file, so we need to open it read/write. But, the
    118   1.4       cgd 	 * field in the hashp structure needs to be accurate so that
    119   1.1       cgd 	 * we can check accesses.
    120   1.1       cgd 	 */
    121   1.4       cgd 	hashp->flags = flags;
    122   1.1       cgd 
    123   1.1       cgd 	new_table = 0;
    124   1.1       cgd 	if (!file || (flags & O_TRUNC) ||
    125   1.1       cgd 	    (stat(file, &statbuf) && (errno == ENOENT))) {
    126   1.1       cgd 		if (errno == ENOENT)
    127   1.1       cgd 			errno = 0; /* Just in case someone looks at errno */
    128   1.1       cgd 		new_table = 1;
    129   1.1       cgd 	}
    130   1.1       cgd 	if (file) {
    131  1.33  christos 		if ((hashp->fp = __dbopen(file, flags, mode, &statbuf)) == -1)
    132   1.1       cgd 			RETURN_ERROR(errno, error0);
    133  1.21  christos 		new_table |= statbuf.st_size == 0;
    134   1.1       cgd 	}
    135   1.1       cgd 	if (new_table) {
    136  1.15  christos 		if (!(hashp = init_hash(hashp, file, info)))
    137   1.1       cgd 			RETURN_ERROR(errno, error1);
    138   1.1       cgd 	} else {
    139   1.1       cgd 		/* Table already exists */
    140   1.1       cgd 		if (info && info->hash)
    141   1.1       cgd 			hashp->hash = info->hash;
    142   1.1       cgd 		else
    143   1.1       cgd 			hashp->hash = __default_hash;
    144   1.1       cgd 
    145   1.1       cgd 		hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
    146   1.1       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    147   1.1       cgd 		swap_header(hashp);
    148   1.1       cgd #endif
    149   1.1       cgd 		if (hdrsize == -1)
    150   1.1       cgd 			RETURN_ERROR(errno, error1);
    151   1.1       cgd 		if (hdrsize != sizeof(HASHHDR))
    152   1.1       cgd 			RETURN_ERROR(EFTYPE, error1);
    153   1.1       cgd 		/* Verify file type, versions and hash function */
    154   1.1       cgd 		if (hashp->MAGIC != HASHMAGIC)
    155   1.1       cgd 			RETURN_ERROR(EFTYPE, error1);
    156   1.5       cgd #define	OLDHASHVERSION	1
    157   1.5       cgd 		if (hashp->VERSION != HASHVERSION &&
    158   1.5       cgd 		    hashp->VERSION != OLDHASHVERSION)
    159   1.1       cgd 			RETURN_ERROR(EFTYPE, error1);
    160  1.31     lukem 		if (hashp->hash(CHARKEY, sizeof(CHARKEY)) !=
    161  1.31     lukem 		    (uint32_t)hashp->H_CHARKEY)
    162   1.1       cgd 			RETURN_ERROR(EFTYPE, error1);
    163   1.1       cgd 		/*
    164   1.1       cgd 		 * Figure out how many segments we need.  Max_Bucket is the
    165   1.1       cgd 		 * maximum bucket number, so the number of buckets is
    166   1.1       cgd 		 * max_bucket + 1.
    167   1.1       cgd 		 */
    168   1.1       cgd 		nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
    169   1.1       cgd 			 hashp->SGSIZE;
    170   1.1       cgd 		hashp->nsegs = 0;
    171   1.1       cgd 		if (alloc_segs(hashp, nsegs))
    172   1.1       cgd 			/*
    173   1.1       cgd 			 * If alloc_segs fails, table will have been destroyed
    174   1.1       cgd 			 * and errno will have been set.
    175   1.1       cgd 			 */
    176   1.1       cgd 			return (NULL);
    177   1.1       cgd 		/* Read in bitmaps */
    178   1.1       cgd 		bpages = (hashp->SPARES[hashp->OVFL_POINT] +
    179  1.15  christos 		    (unsigned int)(hashp->BSIZE << BYTE_SHIFT) - 1) >>
    180   1.1       cgd 		    (hashp->BSHIFT + BYTE_SHIFT);
    181   1.1       cgd 
    182   1.1       cgd 		hashp->nmaps = bpages;
    183  1.28     joerg 		(void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32_t *));
    184   1.1       cgd 	}
    185   1.1       cgd 
    186   1.1       cgd 	/* Initialize Buffer Manager */
    187   1.1       cgd 	if (info && info->cachesize)
    188   1.1       cgd 		__buf_init(hashp, info->cachesize);
    189   1.1       cgd 	else
    190   1.1       cgd 		__buf_init(hashp, DEF_BUFSIZE);
    191   1.1       cgd 
    192   1.1       cgd 	hashp->new_file = new_table;
    193   1.1       cgd 	hashp->save_file = file && (hashp->flags & O_RDWR);
    194   1.1       cgd 	hashp->cbucket = -1;
    195  1.33  christos 	if (!(dbp = malloc(sizeof(*dbp)))) {
    196   1.1       cgd 		save_errno = errno;
    197   1.1       cgd 		hdestroy(hashp);
    198   1.1       cgd 		errno = save_errno;
    199   1.1       cgd 		return (NULL);
    200   1.1       cgd 	}
    201   1.1       cgd 	dbp->internal = hashp;
    202   1.1       cgd 	dbp->close = hash_close;
    203   1.1       cgd 	dbp->del = hash_delete;
    204   1.1       cgd 	dbp->fd = hash_fd;
    205   1.1       cgd 	dbp->get = hash_get;
    206   1.1       cgd 	dbp->put = hash_put;
    207   1.1       cgd 	dbp->seq = hash_seq;
    208   1.1       cgd 	dbp->sync = hash_sync;
    209   1.1       cgd 	dbp->type = DB_HASH;
    210   1.1       cgd 
    211  1.32  christos #ifdef DEBUG1
    212   1.1       cgd 	(void)fprintf(stderr,
    213  1.16   aymeric "%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
    214   1.1       cgd 	    "init_htab:",
    215   1.1       cgd 	    "TABLE POINTER   ", hashp,
    216   1.1       cgd 	    "BUCKET SIZE     ", hashp->BSIZE,
    217   1.1       cgd 	    "BUCKET SHIFT    ", hashp->BSHIFT,
    218   1.1       cgd 	    "DIRECTORY SIZE  ", hashp->DSIZE,
    219   1.1       cgd 	    "SEGMENT SIZE    ", hashp->SGSIZE,
    220   1.1       cgd 	    "SEGMENT SHIFT   ", hashp->SSHIFT,
    221   1.1       cgd 	    "FILL FACTOR     ", hashp->FFACTOR,
    222   1.1       cgd 	    "MAX BUCKET      ", hashp->MAX_BUCKET,
    223   1.1       cgd 	    "OVFL POINT	     ", hashp->OVFL_POINT,
    224   1.1       cgd 	    "LAST FREED      ", hashp->LAST_FREED,
    225   1.1       cgd 	    "HIGH MASK       ", hashp->HIGH_MASK,
    226   1.1       cgd 	    "LOW  MASK       ", hashp->LOW_MASK,
    227   1.1       cgd 	    "NSEGS           ", hashp->nsegs,
    228   1.1       cgd 	    "NKEYS           ", hashp->NKEYS);
    229   1.1       cgd #endif
    230   1.1       cgd #ifdef HASH_STATISTICS
    231   1.1       cgd 	hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
    232   1.1       cgd #endif
    233   1.1       cgd 	return (dbp);
    234   1.1       cgd 
    235   1.1       cgd error1:
    236   1.1       cgd 	if (hashp != NULL)
    237   1.1       cgd 		(void)close(hashp->fp);
    238   1.1       cgd 
    239   1.1       cgd error0:
    240   1.1       cgd 	free(hashp);
    241   1.1       cgd 	errno = save_errno;
    242   1.1       cgd 	return (NULL);
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.1       cgd static int
    246  1.27  christos hash_close(DB *dbp)
    247   1.1       cgd {
    248   1.1       cgd 	HTAB *hashp;
    249   1.1       cgd 	int retval;
    250   1.1       cgd 
    251   1.1       cgd 	if (!dbp)
    252   1.1       cgd 		return (ERROR);
    253   1.1       cgd 
    254  1.27  christos 	hashp = dbp->internal;
    255   1.1       cgd 	retval = hdestroy(hashp);
    256   1.1       cgd 	free(dbp);
    257   1.1       cgd 	return (retval);
    258   1.1       cgd }
    259   1.1       cgd 
    260   1.1       cgd static int
    261  1.27  christos hash_fd(const DB *dbp)
    262   1.1       cgd {
    263   1.1       cgd 	HTAB *hashp;
    264   1.1       cgd 
    265   1.1       cgd 	if (!dbp)
    266   1.1       cgd 		return (ERROR);
    267   1.1       cgd 
    268  1.27  christos 	hashp = dbp->internal;
    269   1.1       cgd 	if (hashp->fp == -1) {
    270   1.1       cgd 		errno = ENOENT;
    271   1.1       cgd 		return (-1);
    272   1.1       cgd 	}
    273   1.1       cgd 	return (hashp->fp);
    274   1.1       cgd }
    275   1.1       cgd 
    276   1.1       cgd /************************** LOCAL CREATION ROUTINES **********************/
    277   1.1       cgd static HTAB *
    278  1.27  christos init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
    279   1.1       cgd {
    280   1.1       cgd 	struct stat statbuf;
    281   1.1       cgd 	int nelem;
    282   1.1       cgd 
    283   1.1       cgd 	nelem = 1;
    284   1.1       cgd 	hashp->NKEYS = 0;
    285   1.1       cgd 	hashp->LORDER = BYTE_ORDER;
    286   1.1       cgd 	hashp->BSIZE = DEF_BUCKET_SIZE;
    287   1.1       cgd 	hashp->BSHIFT = DEF_BUCKET_SHIFT;
    288   1.1       cgd 	hashp->SGSIZE = DEF_SEGSIZE;
    289   1.1       cgd 	hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
    290   1.1       cgd 	hashp->DSIZE = DEF_DIRSIZE;
    291   1.1       cgd 	hashp->FFACTOR = DEF_FFACTOR;
    292   1.1       cgd 	hashp->hash = __default_hash;
    293   1.1       cgd 	memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
    294   1.1       cgd 	memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
    295   1.1       cgd 
    296   1.1       cgd 	/* Fix bucket size to be optimal for file system */
    297   1.1       cgd 	if (file != NULL) {
    298   1.1       cgd 		if (stat(file, &statbuf))
    299   1.1       cgd 			return (NULL);
    300  1.20  christos 		hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE);
    301  1.28     joerg 		hashp->BSHIFT = __log2((uint32_t)hashp->BSIZE);
    302   1.1       cgd 	}
    303   1.1       cgd 
    304   1.1       cgd 	if (info) {
    305   1.1       cgd 		if (info->bsize) {
    306   1.1       cgd 			/* Round pagesize up to power of 2 */
    307   1.1       cgd 			hashp->BSHIFT = __log2(info->bsize);
    308   1.1       cgd 			hashp->BSIZE = 1 << hashp->BSHIFT;
    309  1.20  christos 			if (hashp->BSIZE > MAX_BSIZE) {
    310   1.1       cgd 				errno = EINVAL;
    311   1.1       cgd 				return (NULL);
    312   1.1       cgd 			}
    313   1.1       cgd 		}
    314   1.1       cgd 		if (info->ffactor)
    315   1.1       cgd 			hashp->FFACTOR = info->ffactor;
    316   1.1       cgd 		if (info->hash)
    317   1.1       cgd 			hashp->hash = info->hash;
    318   1.1       cgd 		if (info->nelem)
    319   1.1       cgd 			nelem = info->nelem;
    320   1.1       cgd 		if (info->lorder) {
    321   1.1       cgd 			if (info->lorder != BIG_ENDIAN &&
    322   1.1       cgd 			    info->lorder != LITTLE_ENDIAN) {
    323   1.1       cgd 				errno = EINVAL;
    324   1.1       cgd 				return (NULL);
    325   1.1       cgd 			}
    326   1.1       cgd 			hashp->LORDER = info->lorder;
    327   1.1       cgd 		}
    328   1.1       cgd 	}
    329   1.1       cgd 	/* init_htab should destroy the table and set errno if it fails */
    330  1.15  christos 	if (init_htab(hashp, (size_t)nelem))
    331   1.1       cgd 		return (NULL);
    332   1.1       cgd 	else
    333   1.1       cgd 		return (hashp);
    334   1.1       cgd }
    335   1.1       cgd /*
    336   1.1       cgd  * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
    337   1.1       cgd  * the table and set errno, so we just pass the error information along.
    338   1.1       cgd  *
    339   1.1       cgd  * Returns 0 on No Error
    340   1.1       cgd  */
    341   1.1       cgd static int
    342  1.27  christos init_htab(HTAB *hashp, size_t nelem)
    343   1.1       cgd {
    344  1.27  christos 	int nbuckets;
    345  1.28     joerg 	uint32_t nsegs;
    346   1.1       cgd 	int l2;
    347   1.1       cgd 
    348   1.1       cgd 	/*
    349   1.1       cgd 	 * Divide number of elements by the fill factor and determine a
    350   1.1       cgd 	 * desired number of buckets.  Allocate space for the next greater
    351   1.1       cgd 	 * power of two number of buckets.
    352   1.1       cgd 	 */
    353   1.1       cgd 	nelem = (nelem - 1) / hashp->FFACTOR + 1;
    354   1.1       cgd 
    355  1.28     joerg 	_DBFIT(nelem, uint32_t);
    356  1.28     joerg 	l2 = __log2(MAX((uint32_t)nelem, 2));
    357   1.1       cgd 	nbuckets = 1 << l2;
    358   1.1       cgd 
    359   1.1       cgd 	hashp->SPARES[l2] = l2 + 1;
    360   1.1       cgd 	hashp->SPARES[l2 + 1] = l2 + 1;
    361   1.1       cgd 	hashp->OVFL_POINT = l2;
    362   1.1       cgd 	hashp->LAST_FREED = 2;
    363   1.1       cgd 
    364   1.1       cgd 	/* First bitmap page is at: splitpoint l2 page offset 1 */
    365  1.15  christos 	if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0))
    366   1.1       cgd 		return (-1);
    367   1.1       cgd 
    368   1.1       cgd 	hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
    369   1.1       cgd 	hashp->HIGH_MASK = (nbuckets << 1) - 1;
    370  1.15  christos 	/* LINTED constant in conditional context */
    371   1.1       cgd 	hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
    372   1.1       cgd 	    hashp->BSHIFT) + 1;
    373   1.1       cgd 
    374   1.1       cgd 	nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
    375   1.1       cgd 	nsegs = 1 << __log2(nsegs);
    376   1.1       cgd 
    377  1.31     lukem 	if (nsegs > (uint32_t)hashp->DSIZE)
    378   1.1       cgd 		hashp->DSIZE = nsegs;
    379  1.15  christos 	return (alloc_segs(hashp, (int)nsegs));
    380   1.1       cgd }
    381   1.1       cgd 
    382   1.1       cgd /********************** DESTROY/CLOSE ROUTINES ************************/
    383   1.1       cgd 
    384   1.1       cgd /*
    385   1.1       cgd  * Flushes any changes to the file if necessary and destroys the hashp
    386   1.1       cgd  * structure, freeing all allocated space.
    387   1.1       cgd  */
    388   1.1       cgd static int
    389  1.27  christos hdestroy(HTAB *hashp)
    390   1.1       cgd {
    391   1.1       cgd 	int i, save_errno;
    392   1.1       cgd 
    393   1.1       cgd 	save_errno = 0;
    394   1.1       cgd 
    395   1.1       cgd #ifdef HASH_STATISTICS
    396  1.27  christos 	(void)fprintf(stderr, "hdestroy: accesses %d collisions %d\n",
    397   1.1       cgd 	    hash_accesses, hash_collisions);
    398  1.27  christos 	(void)fprintf(stderr, "hdestroy: expansions %d\n",
    399   1.1       cgd 	    hash_expansions);
    400  1.27  christos 	(void)fprintf(stderr, "hdestroy: overflows %d\n",
    401   1.1       cgd 	    hash_overflows);
    402  1.27  christos 	(void)fprintf(stderr, "keys %d maxp %d segmentcount %d\n",
    403   1.1       cgd 	    hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
    404   1.1       cgd 
    405   1.1       cgd 	for (i = 0; i < NCACHED; i++)
    406   1.1       cgd 		(void)fprintf(stderr,
    407   1.1       cgd 		    "spares[%d] = %d\n", i, hashp->SPARES[i]);
    408   1.1       cgd #endif
    409   1.1       cgd 	/*
    410   1.1       cgd 	 * Call on buffer manager to free buffers, and if required,
    411   1.1       cgd 	 * write them to disk.
    412   1.1       cgd 	 */
    413   1.1       cgd 	if (__buf_free(hashp, 1, hashp->save_file))
    414   1.1       cgd 		save_errno = errno;
    415   1.1       cgd 	if (hashp->dir) {
    416   1.1       cgd 		free(*hashp->dir);	/* Free initial segments */
    417   1.1       cgd 		/* Free extra segments */
    418   1.1       cgd 		while (hashp->exsegs--)
    419   1.1       cgd 			free(hashp->dir[--hashp->nsegs]);
    420   1.1       cgd 		free(hashp->dir);
    421   1.1       cgd 	}
    422   1.1       cgd 	if (flush_meta(hashp) && !save_errno)
    423   1.1       cgd 		save_errno = errno;
    424   1.1       cgd 	/* Free Bigmaps */
    425   1.1       cgd 	for (i = 0; i < hashp->nmaps; i++)
    426   1.1       cgd 		if (hashp->mapp[i])
    427   1.1       cgd 			free(hashp->mapp[i]);
    428   1.1       cgd 
    429   1.1       cgd 	if (hashp->fp != -1)
    430   1.1       cgd 		(void)close(hashp->fp);
    431   1.6       cgd 
    432   1.6       cgd 	free(hashp);
    433   1.1       cgd 
    434   1.1       cgd 	if (save_errno) {
    435   1.1       cgd 		errno = save_errno;
    436   1.1       cgd 		return (ERROR);
    437   1.1       cgd 	}
    438   1.1       cgd 	return (SUCCESS);
    439   1.1       cgd }
    440   1.1       cgd /*
    441   1.1       cgd  * Write modified pages to disk
    442   1.1       cgd  *
    443   1.1       cgd  * Returns:
    444   1.1       cgd  *	 0 == OK
    445   1.1       cgd  *	-1 ERROR
    446   1.1       cgd  */
    447   1.1       cgd static int
    448  1.28     joerg hash_sync(const DB *dbp, uint32_t flags)
    449   1.1       cgd {
    450   1.1       cgd 	HTAB *hashp;
    451   1.1       cgd 
    452   1.1       cgd 	if (flags != 0) {
    453   1.1       cgd 		errno = EINVAL;
    454   1.1       cgd 		return (ERROR);
    455   1.1       cgd 	}
    456   1.1       cgd 
    457   1.1       cgd 	if (!dbp)
    458   1.1       cgd 		return (ERROR);
    459   1.1       cgd 
    460  1.27  christos 	hashp = dbp->internal;
    461   1.1       cgd 	if (!hashp->save_file)
    462   1.1       cgd 		return (0);
    463   1.1       cgd 	if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
    464   1.1       cgd 		return (ERROR);
    465   1.1       cgd 	hashp->new_file = 0;
    466   1.1       cgd 	return (0);
    467   1.1       cgd }
    468   1.1       cgd 
    469   1.1       cgd /*
    470   1.1       cgd  * Returns:
    471   1.1       cgd  *	 0 == OK
    472   1.1       cgd  *	-1 indicates that errno should be set
    473   1.1       cgd  */
    474   1.1       cgd static int
    475  1.27  christos flush_meta(HTAB *hashp)
    476   1.1       cgd {
    477   1.1       cgd 	HASHHDR *whdrp;
    478   1.1       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    479   1.1       cgd 	HASHHDR whdr;
    480   1.1       cgd #endif
    481  1.27  christos 	int fp, i;
    482  1.27  christos 	ssize_t wsize;
    483   1.1       cgd 
    484   1.1       cgd 	if (!hashp->save_file)
    485   1.1       cgd 		return (0);
    486   1.1       cgd 	hashp->MAGIC = HASHMAGIC;
    487   1.1       cgd 	hashp->VERSION = HASHVERSION;
    488   1.1       cgd 	hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
    489   1.1       cgd 
    490   1.1       cgd 	fp = hashp->fp;
    491   1.1       cgd 	whdrp = &hashp->hdr;
    492   1.1       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    493   1.1       cgd 	whdrp = &whdr;
    494   1.1       cgd 	swap_header_copy(&hashp->hdr, whdrp);
    495   1.1       cgd #endif
    496  1.13   thorpej 	if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
    497   1.1       cgd 		return (-1);
    498   1.1       cgd 	else
    499   1.1       cgd 		if (wsize != sizeof(HASHHDR)) {
    500   1.1       cgd 			errno = EFTYPE;
    501  1.10       jtc 			hashp->err = errno;
    502   1.1       cgd 			return (-1);
    503   1.1       cgd 		}
    504   1.1       cgd 	for (i = 0; i < NCACHED; i++)
    505   1.1       cgd 		if (hashp->mapp[i])
    506  1.15  christos 			if (__put_page(hashp, (char *)(void *)hashp->mapp[i],
    507  1.15  christos 				(u_int)hashp->BITMAPS[i], 0, 1))
    508   1.1       cgd 				return (-1);
    509   1.1       cgd 	return (0);
    510   1.1       cgd }
    511   1.1       cgd 
    512   1.1       cgd /*******************************SEARCH ROUTINES *****************************/
    513   1.1       cgd /*
    514   1.1       cgd  * All the access routines return
    515   1.1       cgd  *
    516   1.1       cgd  * Returns:
    517   1.1       cgd  *	 0 on SUCCESS
    518   1.1       cgd  *	 1 to indicate an external ERROR (i.e. key not found, etc)
    519   1.1       cgd  *	-1 to indicate an internal ERROR (i.e. out of memory, etc)
    520   1.1       cgd  */
    521   1.1       cgd static int
    522  1.28     joerg hash_get(const DB *dbp, const DBT *key, DBT *data, uint32_t flag)
    523   1.1       cgd {
    524   1.1       cgd 	HTAB *hashp;
    525   1.1       cgd 
    526  1.27  christos 	hashp = dbp->internal;
    527   1.1       cgd 	if (flag) {
    528  1.10       jtc 		hashp->err = errno = EINVAL;
    529   1.1       cgd 		return (ERROR);
    530   1.1       cgd 	}
    531  1.23  christos 	return (hash_access(hashp, HASH_GET, __UNCONST(key), data));
    532   1.1       cgd }
    533   1.1       cgd 
    534   1.1       cgd static int
    535  1.28     joerg hash_put(const DB *dbp, DBT *key, const DBT *data, uint32_t flag)
    536   1.1       cgd {
    537   1.1       cgd 	HTAB *hashp;
    538   1.1       cgd 
    539  1.27  christos 	hashp = dbp->internal;
    540   1.1       cgd 	if (flag && flag != R_NOOVERWRITE) {
    541  1.10       jtc 		hashp->err = errno = EINVAL;
    542   1.1       cgd 		return (ERROR);
    543   1.1       cgd 	}
    544   1.1       cgd 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
    545  1.10       jtc 		hashp->err = errno = EPERM;
    546   1.1       cgd 		return (ERROR);
    547   1.1       cgd 	}
    548  1.15  christos 	/* LINTED const castaway */
    549   1.1       cgd 	return (hash_access(hashp, flag == R_NOOVERWRITE ?
    550  1.23  christos 	    HASH_PUTNEW : HASH_PUT, __UNCONST(key), __UNCONST(data)));
    551   1.1       cgd }
    552   1.1       cgd 
    553   1.1       cgd static int
    554  1.28     joerg hash_delete(const DB *dbp, const DBT *key, uint32_t flag)
    555   1.1       cgd {
    556   1.1       cgd 	HTAB *hashp;
    557   1.1       cgd 
    558  1.27  christos 	hashp = dbp->internal;
    559   1.1       cgd 	if (flag && flag != R_CURSOR) {
    560  1.10       jtc 		hashp->err = errno = EINVAL;
    561   1.1       cgd 		return (ERROR);
    562   1.1       cgd 	}
    563   1.1       cgd 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
    564  1.10       jtc 		hashp->err = errno = EPERM;
    565   1.1       cgd 		return (ERROR);
    566   1.1       cgd 	}
    567  1.23  christos 	return hash_access(hashp, HASH_DELETE, __UNCONST(key), NULL);
    568   1.1       cgd }
    569   1.1       cgd 
    570   1.1       cgd /*
    571   1.1       cgd  * Assume that hashp has been set in wrapper routine.
    572   1.1       cgd  */
    573   1.1       cgd static int
    574  1.27  christos hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
    575   1.1       cgd {
    576  1.27  christos 	BUFHEAD *rbufp;
    577   1.1       cgd 	BUFHEAD *bufp, *save_bufp;
    578  1.28     joerg 	uint16_t *bp;
    579  1.27  christos 	int n, ndx, off;
    580  1.15  christos 	size_t size;
    581  1.27  christos 	char *kp;
    582  1.28     joerg 	uint16_t pageno;
    583   1.1       cgd 
    584   1.1       cgd #ifdef HASH_STATISTICS
    585   1.1       cgd 	hash_accesses++;
    586   1.1       cgd #endif
    587   1.1       cgd 
    588  1.38  christos 	off = HASH_BSIZE(hashp);
    589   1.1       cgd 	size = key->size;
    590   1.1       cgd 	kp = (char *)key->data;
    591  1.15  christos 	rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0);
    592   1.1       cgd 	if (!rbufp)
    593   1.1       cgd 		return (ERROR);
    594   1.1       cgd 	save_bufp = rbufp;
    595   1.1       cgd 
    596   1.1       cgd 	/* Pin the bucket chain */
    597   1.1       cgd 	rbufp->flags |= BUF_PIN;
    598  1.28     joerg 	for (bp = (uint16_t *)(void *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
    599   1.1       cgd 		if (bp[1] >= REAL_KEY) {
    600   1.1       cgd 			/* Real key/data pair */
    601  1.31     lukem 			if (size == (size_t)(off - *bp) &&
    602   1.1       cgd 			    memcmp(kp, rbufp->page + *bp, size) == 0)
    603   1.1       cgd 				goto found;
    604   1.1       cgd 			off = bp[1];
    605   1.1       cgd #ifdef HASH_STATISTICS
    606   1.1       cgd 			hash_collisions++;
    607   1.1       cgd #endif
    608   1.1       cgd 			bp += 2;
    609   1.1       cgd 			ndx += 2;
    610   1.1       cgd 		} else if (bp[1] == OVFLPAGE) {
    611  1.28     joerg 			rbufp = __get_buf(hashp, (uint32_t)*bp, rbufp, 0);
    612   1.1       cgd 			if (!rbufp) {
    613   1.1       cgd 				save_bufp->flags &= ~BUF_PIN;
    614   1.1       cgd 				return (ERROR);
    615   1.1       cgd 			}
    616   1.1       cgd 			/* FOR LOOP INIT */
    617  1.28     joerg 			bp = (uint16_t *)(void *)rbufp->page;
    618   1.1       cgd 			n = *bp++;
    619   1.1       cgd 			ndx = 1;
    620  1.38  christos 			off = HASH_BSIZE(hashp);
    621   1.1       cgd 		} else if (bp[1] < REAL_KEY) {
    622   1.1       cgd 			if ((ndx =
    623  1.15  christos 			    __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
    624   1.1       cgd 				goto found;
    625   1.1       cgd 			if (ndx == -2) {
    626   1.1       cgd 				bufp = rbufp;
    627   1.1       cgd 				if (!(pageno =
    628   1.1       cgd 				    __find_last_page(hashp, &bufp))) {
    629   1.1       cgd 					ndx = 0;
    630   1.1       cgd 					rbufp = bufp;
    631   1.1       cgd 					break;	/* FOR */
    632   1.1       cgd 				}
    633  1.28     joerg 				rbufp = __get_buf(hashp, (uint32_t)pageno,
    634  1.15  christos 				    bufp, 0);
    635   1.1       cgd 				if (!rbufp) {
    636   1.1       cgd 					save_bufp->flags &= ~BUF_PIN;
    637   1.1       cgd 					return (ERROR);
    638   1.1       cgd 				}
    639   1.1       cgd 				/* FOR LOOP INIT */
    640  1.28     joerg 				bp = (uint16_t *)(void *)rbufp->page;
    641   1.1       cgd 				n = *bp++;
    642   1.1       cgd 				ndx = 1;
    643  1.38  christos 				off = HASH_BSIZE(hashp);
    644   1.1       cgd 			} else {
    645   1.1       cgd 				save_bufp->flags &= ~BUF_PIN;
    646   1.1       cgd 				return (ERROR);
    647   1.1       cgd 			}
    648   1.1       cgd 		}
    649   1.1       cgd 
    650   1.1       cgd 	/* Not found */
    651   1.1       cgd 	switch (action) {
    652   1.1       cgd 	case HASH_PUT:
    653   1.1       cgd 	case HASH_PUTNEW:
    654   1.1       cgd 		if (__addel(hashp, rbufp, key, val)) {
    655   1.1       cgd 			save_bufp->flags &= ~BUF_PIN;
    656   1.1       cgd 			return (ERROR);
    657   1.1       cgd 		} else {
    658   1.1       cgd 			save_bufp->flags &= ~BUF_PIN;
    659   1.1       cgd 			return (SUCCESS);
    660   1.1       cgd 		}
    661   1.1       cgd 	case HASH_GET:
    662   1.1       cgd 	case HASH_DELETE:
    663   1.1       cgd 	default:
    664   1.1       cgd 		save_bufp->flags &= ~BUF_PIN;
    665   1.1       cgd 		return (ABNORMAL);
    666   1.1       cgd 	}
    667   1.1       cgd 
    668   1.1       cgd found:
    669   1.1       cgd 	switch (action) {
    670   1.1       cgd 	case HASH_PUTNEW:
    671   1.1       cgd 		save_bufp->flags &= ~BUF_PIN;
    672   1.1       cgd 		return (ABNORMAL);
    673   1.1       cgd 	case HASH_GET:
    674  1.28     joerg 		bp = (uint16_t *)(void *)rbufp->page;
    675   1.1       cgd 		if (bp[ndx + 1] < REAL_KEY) {
    676   1.1       cgd 			if (__big_return(hashp, rbufp, ndx, val, 0))
    677   1.1       cgd 				return (ERROR);
    678   1.1       cgd 		} else {
    679  1.28     joerg 			val->data = (uint8_t *)rbufp->page + (int)bp[ndx + 1];
    680   1.1       cgd 			val->size = bp[ndx] - bp[ndx + 1];
    681   1.1       cgd 		}
    682   1.1       cgd 		break;
    683   1.1       cgd 	case HASH_PUT:
    684   1.1       cgd 		if ((__delpair(hashp, rbufp, ndx)) ||
    685   1.1       cgd 		    (__addel(hashp, rbufp, key, val))) {
    686   1.1       cgd 			save_bufp->flags &= ~BUF_PIN;
    687   1.1       cgd 			return (ERROR);
    688   1.1       cgd 		}
    689   1.1       cgd 		break;
    690   1.1       cgd 	case HASH_DELETE:
    691   1.1       cgd 		if (__delpair(hashp, rbufp, ndx))
    692   1.1       cgd 			return (ERROR);
    693  1.35  christos 		/*
    694  1.35  christos 		 * Our index lags 2 behind on the same page when we are
    695  1.35  christos 		 * deleting the element pointed to by the index; otherwise
    696  1.35  christos 		 * deleting randomly from an iterated hash produces undefined
    697  1.35  christos 		 * results.
    698  1.35  christos 		 */
    699  1.35  christos 		if (ndx != hashp->cndx - 2 || rbufp != hashp->cpage)
    700  1.35  christos 			break;
    701  1.35  christos 
    702  1.35  christos 		if (hashp->cndx > 1) {
    703  1.35  christos 			/* Move back one element */
    704  1.35  christos 			hashp->cndx -= 2;
    705  1.35  christos 		} else {
    706  1.35  christos 			/*
    707  1.35  christos 			 * Move back one page, and indicate to go to the last
    708  1.35  christos 			 * element of the previous page by setting cndx to -1
    709  1.35  christos 			 */
    710  1.35  christos 			hashp->cbucket--;
    711  1.35  christos 			hashp->cpage = NULL;
    712  1.35  christos 			hashp->cndx = -1;
    713  1.35  christos 		}
    714   1.1       cgd 		break;
    715   1.1       cgd 	default:
    716   1.1       cgd 		abort();
    717   1.1       cgd 	}
    718   1.1       cgd 	save_bufp->flags &= ~BUF_PIN;
    719   1.1       cgd 	return (SUCCESS);
    720   1.1       cgd }
    721   1.1       cgd 
    722   1.1       cgd static int
    723  1.28     joerg hash_seq(const DB *dbp, DBT *key, DBT *data, uint32_t flag)
    724   1.1       cgd {
    725  1.28     joerg 	uint32_t bucket;
    726  1.27  christos 	BUFHEAD *bufp = NULL; /* XXX: gcc */
    727   1.1       cgd 	HTAB *hashp;
    728  1.28     joerg 	uint16_t *bp, ndx;
    729   1.1       cgd 
    730  1.27  christos 	hashp = dbp->internal;
    731   1.1       cgd 	if (flag && flag != R_FIRST && flag != R_NEXT) {
    732  1.10       jtc 		hashp->err = errno = EINVAL;
    733   1.1       cgd 		return (ERROR);
    734   1.1       cgd 	}
    735   1.1       cgd #ifdef HASH_STATISTICS
    736   1.1       cgd 	hash_accesses++;
    737   1.1       cgd #endif
    738   1.1       cgd 	if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
    739   1.1       cgd 		hashp->cbucket = 0;
    740   1.1       cgd 		hashp->cndx = 1;
    741   1.1       cgd 		hashp->cpage = NULL;
    742   1.1       cgd 	}
    743   1.1       cgd 
    744  1.34  christos next_bucket:
    745   1.1       cgd 	for (bp = NULL; !bp || !bp[0]; ) {
    746   1.1       cgd 		if (!(bufp = hashp->cpage)) {
    747   1.1       cgd 			for (bucket = hashp->cbucket;
    748  1.31     lukem 			    bucket <= (uint32_t)hashp->MAX_BUCKET;
    749  1.35  christos 			    bucket++) {
    750   1.1       cgd 				bufp = __get_buf(hashp, bucket, NULL, 0);
    751   1.1       cgd 				if (!bufp)
    752   1.1       cgd 					return (ERROR);
    753   1.1       cgd 				hashp->cpage = bufp;
    754  1.28     joerg 				bp = (uint16_t *)(void *)bufp->page;
    755   1.1       cgd 				if (bp[0])
    756   1.1       cgd 					break;
    757   1.1       cgd 			}
    758   1.1       cgd 			hashp->cbucket = bucket;
    759   1.1       cgd 			if (hashp->cbucket > hashp->MAX_BUCKET) {
    760   1.1       cgd 				hashp->cbucket = -1;
    761   1.1       cgd 				return (ABNORMAL);
    762   1.1       cgd 			}
    763  1.35  christos 			if (hashp->cndx == -1) {
    764  1.35  christos 				/* move to the last element of the page */
    765  1.35  christos 				hashp->cndx = 1;
    766  1.35  christos 				while (bp[hashp->cndx - 1] != 0)
    767  1.35  christos 					hashp->cndx += 2;
    768  1.35  christos 			} else {
    769  1.35  christos 				/* start on the first element */
    770  1.35  christos 				hashp->cndx = 1;
    771  1.35  christos 			}
    772  1.34  christos 		} else {
    773  1.36  christos 			bp = (uint16_t *)(void *)bufp->page;
    774  1.34  christos 			if (flag == R_NEXT || flag == 0) {
    775  1.34  christos 				if (hashp->cndx > bp[0]) {
    776  1.34  christos 					hashp->cpage = NULL;
    777  1.34  christos 					hashp->cbucket++;
    778  1.34  christos 					hashp->cndx = 1;
    779  1.34  christos 					goto next_bucket;
    780  1.34  christos 				}
    781  1.34  christos 			}
    782  1.34  christos 		}
    783  1.34  christos 
    784   1.1       cgd 
    785  1.27  christos 		_DIAGASSERT(bp != NULL);
    786  1.27  christos 		_DIAGASSERT(bufp != NULL);
    787   1.1       cgd 		while (bp[hashp->cndx + 1] == OVFLPAGE) {
    788   1.1       cgd 			bufp = hashp->cpage =
    789  1.28     joerg 			    __get_buf(hashp, (uint32_t)bp[hashp->cndx], bufp,
    790  1.15  christos 				0);
    791   1.1       cgd 			if (!bufp)
    792   1.1       cgd 				return (ERROR);
    793  1.28     joerg 			bp = (uint16_t *)(void *)(bufp->page);
    794   1.1       cgd 			hashp->cndx = 1;
    795   1.1       cgd 		}
    796   1.1       cgd 		if (!bp[0]) {
    797   1.1       cgd 			hashp->cpage = NULL;
    798   1.1       cgd 			++hashp->cbucket;
    799   1.1       cgd 		}
    800   1.1       cgd 	}
    801   1.1       cgd 	ndx = hashp->cndx;
    802   1.1       cgd 	if (bp[ndx + 1] < REAL_KEY) {
    803   1.1       cgd 		if (__big_keydata(hashp, bufp, key, data, 1))
    804   1.1       cgd 			return (ERROR);
    805  1.36  christos 		hashp->cndx = 1;
    806   1.1       cgd 	} else {
    807  1.25  christos 		if (hashp->cpage == NULL)
    808  1.26       rtr 			return (ERROR);
    809  1.28     joerg 		key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
    810  1.38  christos 		key->size = (ndx > 1 ? bp[ndx - 1] : HASH_BSIZE(hashp)) - bp[ndx];
    811  1.28     joerg 		data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
    812   1.1       cgd 		data->size = bp[ndx] - bp[ndx + 1];
    813  1.36  christos 		hashp->cndx += 2;
    814   1.1       cgd 	}
    815   1.1       cgd 	return (SUCCESS);
    816   1.1       cgd }
    817   1.1       cgd 
    818   1.1       cgd /********************************* UTILITIES ************************/
    819   1.1       cgd 
    820   1.1       cgd /*
    821   1.1       cgd  * Returns:
    822   1.1       cgd  *	 0 ==> OK
    823   1.1       cgd  *	-1 ==> Error
    824   1.1       cgd  */
    825  1.27  christos int
    826  1.27  christos __expand_table(HTAB *hashp)
    827   1.1       cgd {
    828  1.28     joerg 	uint32_t old_bucket, new_bucket;
    829  1.27  christos 	int new_segnum, spare_ndx;
    830  1.27  christos 	size_t dirsize;
    831   1.1       cgd 
    832   1.1       cgd #ifdef HASH_STATISTICS
    833   1.1       cgd 	hash_expansions++;
    834   1.1       cgd #endif
    835   1.1       cgd 	new_bucket = ++hashp->MAX_BUCKET;
    836   1.1       cgd 	old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
    837   1.1       cgd 
    838   1.1       cgd 	new_segnum = new_bucket >> hashp->SSHIFT;
    839   1.1       cgd 
    840   1.1       cgd 	/* Check if we need a new segment */
    841   1.1       cgd 	if (new_segnum >= hashp->nsegs) {
    842   1.1       cgd 		/* Check if we need to expand directory */
    843   1.1       cgd 		if (new_segnum >= hashp->DSIZE) {
    844   1.1       cgd 			/* Reallocate directory */
    845   1.1       cgd 			dirsize = hashp->DSIZE * sizeof(SEGMENT *);
    846   1.1       cgd 			if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
    847   1.1       cgd 				return (-1);
    848  1.32  christos 			dirsize <<= 1;
    849  1.32  christos 			_DBFIT(dirsize, uint32_t);
    850  1.32  christos 			hashp->DSIZE = (uint32_t)dirsize;
    851   1.1       cgd 		}
    852   1.7       cgd 		if ((hashp->dir[new_segnum] =
    853  1.27  christos 		    calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
    854   1.1       cgd 			return (-1);
    855   1.1       cgd 		hashp->exsegs++;
    856   1.1       cgd 		hashp->nsegs++;
    857   1.1       cgd 	}
    858   1.1       cgd 	/*
    859   1.1       cgd 	 * If the split point is increasing (MAX_BUCKET's log base 2
    860   1.1       cgd 	 * * increases), we need to copy the current contents of the spare
    861   1.1       cgd 	 * split bucket to the next bucket.
    862   1.1       cgd 	 */
    863  1.28     joerg 	spare_ndx = __log2((uint32_t)(hashp->MAX_BUCKET + 1));
    864   1.1       cgd 	if (spare_ndx > hashp->OVFL_POINT) {
    865   1.1       cgd 		hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
    866   1.1       cgd 		hashp->OVFL_POINT = spare_ndx;
    867   1.1       cgd 	}
    868   1.1       cgd 
    869  1.31     lukem 	if (new_bucket > (uint32_t)hashp->HIGH_MASK) {
    870   1.1       cgd 		/* Starting a new doubling */
    871   1.1       cgd 		hashp->LOW_MASK = hashp->HIGH_MASK;
    872   1.1       cgd 		hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
    873   1.1       cgd 	}
    874   1.1       cgd 	/* Relocate records to the new bucket */
    875   1.1       cgd 	return (__split_page(hashp, old_bucket, new_bucket));
    876   1.1       cgd }
    877   1.1       cgd 
    878   1.1       cgd /*
    879   1.1       cgd  * If realloc guarantees that the pointer is not destroyed if the realloc
    880   1.1       cgd  * fails, then this routine can go away.
    881   1.1       cgd  */
    882   1.1       cgd static void *
    883  1.27  christos hash_realloc(SEGMENT **p_ptr, size_t oldsize, size_t newsize)
    884  1.27  christos {
    885  1.27  christos 	void *p;
    886  1.27  christos 
    887  1.27  christos 	if ((p = malloc(newsize)) != NULL) {
    888  1.27  christos 		memmove(p, *p_ptr, oldsize);
    889  1.27  christos 		memset((char *)p + oldsize, 0, newsize - oldsize);
    890   1.1       cgd 		free(*p_ptr);
    891   1.1       cgd 		*p_ptr = p;
    892   1.1       cgd 	}
    893   1.1       cgd 	return (p);
    894   1.1       cgd }
    895   1.1       cgd 
    896  1.28     joerg uint32_t
    897  1.27  christos __call_hash(HTAB *hashp, char *k, int len)
    898   1.1       cgd {
    899   1.1       cgd 	int n, bucket;
    900   1.1       cgd 
    901  1.15  christos 	n = hashp->hash(k, (size_t)len);
    902   1.1       cgd 	bucket = n & hashp->HIGH_MASK;
    903   1.1       cgd 	if (bucket > hashp->MAX_BUCKET)
    904   1.1       cgd 		bucket = bucket & hashp->LOW_MASK;
    905   1.1       cgd 	return (bucket);
    906   1.1       cgd }
    907   1.1       cgd 
    908   1.1       cgd /*
    909   1.1       cgd  * Allocate segment table.  On error, destroy the table and set errno.
    910   1.1       cgd  *
    911   1.1       cgd  * Returns 0 on success
    912   1.1       cgd  */
    913   1.1       cgd static int
    914  1.27  christos alloc_segs(HTAB *hashp, int nsegs)
    915   1.1       cgd {
    916  1.27  christos 	int i;
    917  1.27  christos 	SEGMENT store;
    918   1.1       cgd 
    919   1.1       cgd 	int save_errno;
    920   1.1       cgd 
    921  1.27  christos 	hashp->dir = calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *));
    922  1.27  christos 	if (hashp->dir == NULL) {
    923   1.1       cgd 		save_errno = errno;
    924   1.1       cgd 		(void)hdestroy(hashp);
    925   1.1       cgd 		errno = save_errno;
    926   1.1       cgd 		return (-1);
    927   1.1       cgd 	}
    928  1.24  christos 	hashp->nsegs = nsegs;
    929  1.24  christos 	if (nsegs == 0)
    930  1.24  christos 		return 0;
    931   1.1       cgd 	/* Allocate segments */
    932  1.27  christos 	store = calloc((size_t)(nsegs << hashp->SSHIFT), sizeof(SEGMENT));
    933  1.27  christos 	if (store == NULL) {
    934   1.1       cgd 		save_errno = errno;
    935   1.1       cgd 		(void)hdestroy(hashp);
    936   1.1       cgd 		errno = save_errno;
    937   1.1       cgd 		return (-1);
    938   1.1       cgd 	}
    939  1.24  christos 	for (i = 0; i < nsegs; i++)
    940   1.1       cgd 		hashp->dir[i] = &store[i << hashp->SSHIFT];
    941   1.1       cgd 	return (0);
    942   1.1       cgd }
    943   1.1       cgd 
    944   1.1       cgd #if BYTE_ORDER == LITTLE_ENDIAN
    945   1.1       cgd /*
    946   1.1       cgd  * Hashp->hdr needs to be byteswapped.
    947   1.1       cgd  */
    948   1.1       cgd static void
    949  1.27  christos swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
    950   1.1       cgd {
    951  1.27  christos 	size_t i;
    952   1.1       cgd 
    953   1.7       cgd 	P_32_COPY(srcp->magic, destp->magic);
    954   1.7       cgd 	P_32_COPY(srcp->version, destp->version);
    955   1.7       cgd 	P_32_COPY(srcp->lorder, destp->lorder);
    956   1.7       cgd 	P_32_COPY(srcp->bsize, destp->bsize);
    957   1.7       cgd 	P_32_COPY(srcp->bshift, destp->bshift);
    958   1.7       cgd 	P_32_COPY(srcp->dsize, destp->dsize);
    959   1.7       cgd 	P_32_COPY(srcp->ssize, destp->ssize);
    960   1.7       cgd 	P_32_COPY(srcp->sshift, destp->sshift);
    961   1.7       cgd 	P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
    962   1.7       cgd 	P_32_COPY(srcp->last_freed, destp->last_freed);
    963   1.7       cgd 	P_32_COPY(srcp->max_bucket, destp->max_bucket);
    964   1.7       cgd 	P_32_COPY(srcp->high_mask, destp->high_mask);
    965   1.7       cgd 	P_32_COPY(srcp->low_mask, destp->low_mask);
    966   1.7       cgd 	P_32_COPY(srcp->ffactor, destp->ffactor);
    967   1.7       cgd 	P_32_COPY(srcp->nkeys, destp->nkeys);
    968   1.7       cgd 	P_32_COPY(srcp->hdrpages, destp->hdrpages);
    969   1.7       cgd 	P_32_COPY(srcp->h_charkey, destp->h_charkey);
    970   1.1       cgd 	for (i = 0; i < NCACHED; i++) {
    971   1.7       cgd 		P_32_COPY(srcp->spares[i], destp->spares[i]);
    972   1.7       cgd 		P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
    973   1.1       cgd 	}
    974   1.1       cgd }
    975   1.1       cgd 
    976   1.1       cgd static void
    977  1.27  christos swap_header(HTAB *hashp)
    978   1.1       cgd {
    979   1.1       cgd 	HASHHDR *hdrp;
    980  1.27  christos 	size_t i;
    981   1.1       cgd 
    982   1.1       cgd 	hdrp = &hashp->hdr;
    983   1.1       cgd 
    984   1.7       cgd 	M_32_SWAP(hdrp->magic);
    985   1.7       cgd 	M_32_SWAP(hdrp->version);
    986   1.7       cgd 	M_32_SWAP(hdrp->lorder);
    987   1.7       cgd 	M_32_SWAP(hdrp->bsize);
    988   1.7       cgd 	M_32_SWAP(hdrp->bshift);
    989   1.7       cgd 	M_32_SWAP(hdrp->dsize);
    990   1.7       cgd 	M_32_SWAP(hdrp->ssize);
    991   1.7       cgd 	M_32_SWAP(hdrp->sshift);
    992   1.7       cgd 	M_32_SWAP(hdrp->ovfl_point);
    993   1.7       cgd 	M_32_SWAP(hdrp->last_freed);
    994   1.7       cgd 	M_32_SWAP(hdrp->max_bucket);
    995   1.7       cgd 	M_32_SWAP(hdrp->high_mask);
    996   1.7       cgd 	M_32_SWAP(hdrp->low_mask);
    997   1.7       cgd 	M_32_SWAP(hdrp->ffactor);
    998   1.7       cgd 	M_32_SWAP(hdrp->nkeys);
    999   1.7       cgd 	M_32_SWAP(hdrp->hdrpages);
   1000   1.7       cgd 	M_32_SWAP(hdrp->h_charkey);
   1001   1.1       cgd 	for (i = 0; i < NCACHED; i++) {
   1002   1.7       cgd 		M_32_SWAP(hdrp->spares[i]);
   1003   1.7       cgd 		M_16_SWAP(hdrp->bitmaps[i]);
   1004   1.1       cgd 	}
   1005   1.1       cgd }
   1006   1.1       cgd #endif
   1007