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