1*14fcefd5Schristos /* $NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $ */
2402f19d1Scgd
39f0aa214Scgd /*-
4a6d14e36Scgd * Copyright (c) 1990, 1993, 1994
59f0aa214Scgd * The Regents of the University of California. All rights reserved.
69f0aa214Scgd *
79f0aa214Scgd * This code is derived from software contributed to Berkeley by
89f0aa214Scgd * Margo Seltzer.
99f0aa214Scgd *
109f0aa214Scgd * Redistribution and use in source and binary forms, with or without
119f0aa214Scgd * modification, are permitted provided that the following conditions
129f0aa214Scgd * are met:
139f0aa214Scgd * 1. Redistributions of source code must retain the above copyright
149f0aa214Scgd * notice, this list of conditions and the following disclaimer.
159f0aa214Scgd * 2. Redistributions in binary form must reproduce the above copyright
169f0aa214Scgd * notice, this list of conditions and the following disclaimer in the
179f0aa214Scgd * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
199f0aa214Scgd * may be used to endorse or promote products derived from this software
209f0aa214Scgd * without specific prior written permission.
219f0aa214Scgd *
229f0aa214Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239f0aa214Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249f0aa214Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259f0aa214Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269f0aa214Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279f0aa214Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289f0aa214Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299f0aa214Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309f0aa214Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319f0aa214Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329f0aa214Scgd * SUCH DAMAGE.
339f0aa214Scgd */
349f0aa214Scgd
35d3595ddfSjoerg #if HAVE_NBTOOL_CONFIG_H
36d3595ddfSjoerg #include "nbtool_config.h"
37d3595ddfSjoerg #endif
38d3595ddfSjoerg
3900ae392dSchristos #include <sys/cdefs.h>
40*14fcefd5Schristos __RCSID("$NetBSD: hash.c,v 1.38 2015/11/18 18:22:42 christos Exp $");
419f0aa214Scgd
42abc6d45eSkleink #include "namespace.h"
439f0aa214Scgd #include <sys/param.h>
449f0aa214Scgd #include <sys/stat.h>
459f0aa214Scgd
469f0aa214Scgd #include <errno.h>
479f0aa214Scgd #include <fcntl.h>
489f0aa214Scgd #include <stdio.h>
499f0aa214Scgd #include <stdlib.h>
509f0aa214Scgd #include <string.h>
519f0aa214Scgd #include <unistd.h>
529f0aa214Scgd #include <assert.h>
539f0aa214Scgd
549f0aa214Scgd #include <db.h>
559f0aa214Scgd #include "hash.h"
569f0aa214Scgd #include "page.h"
579f0aa214Scgd #include "extern.h"
589f0aa214Scgd
59cb9daf8fSchristos static int alloc_segs(HTAB *, int);
60cb9daf8fSchristos static int flush_meta(HTAB *);
61cb9daf8fSchristos static int hash_access(HTAB *, ACTION, DBT *, DBT *);
62cb9daf8fSchristos static int hash_close(DB *);
6340b37a3bSjoerg static int hash_delete(const DB *, const DBT *, uint32_t);
64cb9daf8fSchristos static int hash_fd(const DB *);
6540b37a3bSjoerg static int hash_get(const DB *, const DBT *, DBT *, uint32_t);
6640b37a3bSjoerg static int hash_put(const DB *, DBT *, const DBT *, uint32_t);
67cb9daf8fSchristos static void *hash_realloc(SEGMENT **, size_t, size_t);
6840b37a3bSjoerg static int hash_seq(const DB *, DBT *, DBT *, uint32_t);
6940b37a3bSjoerg static int hash_sync(const DB *, uint32_t);
70cb9daf8fSchristos static int hdestroy(HTAB *);
71cb9daf8fSchristos static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
72cb9daf8fSchristos static int init_htab(HTAB *, size_t);
739f0aa214Scgd #if BYTE_ORDER == LITTLE_ENDIAN
74cb9daf8fSchristos static void swap_header(HTAB *);
75cb9daf8fSchristos static void swap_header_copy(HASHHDR *, HASHHDR *);
769f0aa214Scgd #endif
779f0aa214Scgd
789f0aa214Scgd /* Fast arithmetic, relying on powers of 2, */
799f0aa214Scgd #define MOD(x, y) ((x) & ((y) - 1))
809f0aa214Scgd
819f0aa214Scgd #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
829f0aa214Scgd
839f0aa214Scgd /* Return values */
849f0aa214Scgd #define SUCCESS (0)
859f0aa214Scgd #define ERROR (-1)
869f0aa214Scgd #define ABNORMAL (1)
879f0aa214Scgd
889f0aa214Scgd #ifdef HASH_STATISTICS
89a6d14e36Scgd int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
909f0aa214Scgd #endif
919f0aa214Scgd
929f0aa214Scgd /************************** INTERFACE ROUTINES ***************************/
939f0aa214Scgd /* OPEN/CLOSE */
949f0aa214Scgd
9561238e71Schristos /* ARGSUSED */
96cb9daf8fSchristos DB *
__hash_open(const char * file,int flags,mode_t mode,const HASHINFO * info,int dflags)97cb9daf8fSchristos __hash_open(const char *file, int flags, mode_t mode, const HASHINFO *info,
98cb9daf8fSchristos int dflags)
999f0aa214Scgd {
1009f0aa214Scgd HTAB *hashp;
1019f0aa214Scgd struct stat statbuf;
1029f0aa214Scgd DB *dbp;
103cb9daf8fSchristos int bpages, new_table, nsegs, save_errno;
104cb9daf8fSchristos ssize_t hdrsize;
1059f0aa214Scgd
1069f0aa214Scgd if ((flags & O_ACCMODE) == O_WRONLY) {
1079f0aa214Scgd errno = EINVAL;
1089f0aa214Scgd return (NULL);
1099f0aa214Scgd }
1109f0aa214Scgd
111cb9daf8fSchristos if (!(hashp = calloc(1, sizeof(HTAB))))
1129f0aa214Scgd return (NULL);
1139f0aa214Scgd hashp->fp = -1;
11445e27c80Scgd
1159f0aa214Scgd /*
11645e27c80Scgd * Even if user wants write only, we need to be able to read
11745e27c80Scgd * the actual file, so we need to open it read/write. But, the
11845e27c80Scgd * field in the hashp structure needs to be accurate so that
1199f0aa214Scgd * we can check accesses.
1209f0aa214Scgd */
12145e27c80Scgd hashp->flags = flags;
1229f0aa214Scgd
1239f0aa214Scgd new_table = 0;
1249f0aa214Scgd if (!file || (flags & O_TRUNC) ||
1259f0aa214Scgd (stat(file, &statbuf) && (errno == ENOENT))) {
1269f0aa214Scgd if (errno == ENOENT)
1279f0aa214Scgd errno = 0; /* Just in case someone looks at errno */
1289f0aa214Scgd new_table = 1;
1299f0aa214Scgd }
1309f0aa214Scgd if (file) {
131b605a13bSchristos if ((hashp->fp = __dbopen(file, flags, mode, &statbuf)) == -1)
1329f0aa214Scgd RETURN_ERROR(errno, error0);
133c4e3c4d4Schristos new_table |= statbuf.st_size == 0;
1349f0aa214Scgd }
1359f0aa214Scgd if (new_table) {
13661238e71Schristos if (!(hashp = init_hash(hashp, file, info)))
1379f0aa214Scgd RETURN_ERROR(errno, error1);
1389f0aa214Scgd } else {
1399f0aa214Scgd /* Table already exists */
1409f0aa214Scgd if (info && info->hash)
1419f0aa214Scgd hashp->hash = info->hash;
1429f0aa214Scgd else
1439f0aa214Scgd hashp->hash = __default_hash;
1449f0aa214Scgd
1459f0aa214Scgd hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
1469f0aa214Scgd #if BYTE_ORDER == LITTLE_ENDIAN
1479f0aa214Scgd swap_header(hashp);
1489f0aa214Scgd #endif
1499f0aa214Scgd if (hdrsize == -1)
1509f0aa214Scgd RETURN_ERROR(errno, error1);
1519f0aa214Scgd if (hdrsize != sizeof(HASHHDR))
1529f0aa214Scgd RETURN_ERROR(EFTYPE, error1);
1539f0aa214Scgd /* Verify file type, versions and hash function */
1549f0aa214Scgd if (hashp->MAGIC != HASHMAGIC)
1559f0aa214Scgd RETURN_ERROR(EFTYPE, error1);
15632661c1fScgd #define OLDHASHVERSION 1
15732661c1fScgd if (hashp->VERSION != HASHVERSION &&
15832661c1fScgd hashp->VERSION != OLDHASHVERSION)
1599f0aa214Scgd RETURN_ERROR(EFTYPE, error1);
160c7201a0fSlukem if (hashp->hash(CHARKEY, sizeof(CHARKEY)) !=
161c7201a0fSlukem (uint32_t)hashp->H_CHARKEY)
1629f0aa214Scgd RETURN_ERROR(EFTYPE, error1);
1639f0aa214Scgd /*
1649f0aa214Scgd * Figure out how many segments we need. Max_Bucket is the
1659f0aa214Scgd * maximum bucket number, so the number of buckets is
1669f0aa214Scgd * max_bucket + 1.
1679f0aa214Scgd */
1689f0aa214Scgd nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
1699f0aa214Scgd hashp->SGSIZE;
1709f0aa214Scgd hashp->nsegs = 0;
1719f0aa214Scgd if (alloc_segs(hashp, nsegs))
1729f0aa214Scgd /*
1739f0aa214Scgd * If alloc_segs fails, table will have been destroyed
1749f0aa214Scgd * and errno will have been set.
1759f0aa214Scgd */
1769f0aa214Scgd return (NULL);
1779f0aa214Scgd /* Read in bitmaps */
1789f0aa214Scgd bpages = (hashp->SPARES[hashp->OVFL_POINT] +
17961238e71Schristos (unsigned int)(hashp->BSIZE << BYTE_SHIFT) - 1) >>
1809f0aa214Scgd (hashp->BSHIFT + BYTE_SHIFT);
1819f0aa214Scgd
1829f0aa214Scgd hashp->nmaps = bpages;
18340b37a3bSjoerg (void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32_t *));
1849f0aa214Scgd }
1859f0aa214Scgd
1869f0aa214Scgd /* Initialize Buffer Manager */
1879f0aa214Scgd if (info && info->cachesize)
1889f0aa214Scgd __buf_init(hashp, info->cachesize);
1899f0aa214Scgd else
1909f0aa214Scgd __buf_init(hashp, DEF_BUFSIZE);
1919f0aa214Scgd
1929f0aa214Scgd hashp->new_file = new_table;
1939f0aa214Scgd hashp->save_file = file && (hashp->flags & O_RDWR);
1949f0aa214Scgd hashp->cbucket = -1;
195b605a13bSchristos if (!(dbp = malloc(sizeof(*dbp)))) {
1969f0aa214Scgd save_errno = errno;
1979f0aa214Scgd hdestroy(hashp);
1989f0aa214Scgd errno = save_errno;
1999f0aa214Scgd return (NULL);
2009f0aa214Scgd }
2019f0aa214Scgd dbp->internal = hashp;
2029f0aa214Scgd dbp->close = hash_close;
2039f0aa214Scgd dbp->del = hash_delete;
2049f0aa214Scgd dbp->fd = hash_fd;
2059f0aa214Scgd dbp->get = hash_get;
2069f0aa214Scgd dbp->put = hash_put;
2079f0aa214Scgd dbp->seq = hash_seq;
2089f0aa214Scgd dbp->sync = hash_sync;
2099f0aa214Scgd dbp->type = DB_HASH;
2109f0aa214Scgd
211c5e820caSchristos #ifdef DEBUG1
2129f0aa214Scgd (void)fprintf(stderr,
2139e09072fSaymeric "%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",
2149f0aa214Scgd "init_htab:",
2159f0aa214Scgd "TABLE POINTER ", hashp,
2169f0aa214Scgd "BUCKET SIZE ", hashp->BSIZE,
2179f0aa214Scgd "BUCKET SHIFT ", hashp->BSHIFT,
2189f0aa214Scgd "DIRECTORY SIZE ", hashp->DSIZE,
2199f0aa214Scgd "SEGMENT SIZE ", hashp->SGSIZE,
2209f0aa214Scgd "SEGMENT SHIFT ", hashp->SSHIFT,
2219f0aa214Scgd "FILL FACTOR ", hashp->FFACTOR,
2229f0aa214Scgd "MAX BUCKET ", hashp->MAX_BUCKET,
2239f0aa214Scgd "OVFL POINT ", hashp->OVFL_POINT,
2249f0aa214Scgd "LAST FREED ", hashp->LAST_FREED,
2259f0aa214Scgd "HIGH MASK ", hashp->HIGH_MASK,
2269f0aa214Scgd "LOW MASK ", hashp->LOW_MASK,
2279f0aa214Scgd "NSEGS ", hashp->nsegs,
2289f0aa214Scgd "NKEYS ", hashp->NKEYS);
2299f0aa214Scgd #endif
2309f0aa214Scgd #ifdef HASH_STATISTICS
2319f0aa214Scgd hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
2329f0aa214Scgd #endif
2339f0aa214Scgd return (dbp);
2349f0aa214Scgd
2359f0aa214Scgd error1:
2369f0aa214Scgd if (hashp != NULL)
2379f0aa214Scgd (void)close(hashp->fp);
2389f0aa214Scgd
2399f0aa214Scgd error0:
2409f0aa214Scgd free(hashp);
2419f0aa214Scgd errno = save_errno;
2429f0aa214Scgd return (NULL);
2439f0aa214Scgd }
2449f0aa214Scgd
2459f0aa214Scgd static int
hash_close(DB * dbp)246cb9daf8fSchristos hash_close(DB *dbp)
2479f0aa214Scgd {
2489f0aa214Scgd HTAB *hashp;
2499f0aa214Scgd int retval;
2509f0aa214Scgd
2519f0aa214Scgd if (!dbp)
2529f0aa214Scgd return (ERROR);
2539f0aa214Scgd
254cb9daf8fSchristos hashp = dbp->internal;
2559f0aa214Scgd retval = hdestroy(hashp);
2569f0aa214Scgd free(dbp);
2579f0aa214Scgd return (retval);
2589f0aa214Scgd }
2599f0aa214Scgd
2609f0aa214Scgd static int
hash_fd(const DB * dbp)261cb9daf8fSchristos hash_fd(const DB *dbp)
2629f0aa214Scgd {
2639f0aa214Scgd HTAB *hashp;
2649f0aa214Scgd
2659f0aa214Scgd if (!dbp)
2669f0aa214Scgd return (ERROR);
2679f0aa214Scgd
268cb9daf8fSchristos hashp = dbp->internal;
2699f0aa214Scgd if (hashp->fp == -1) {
2709f0aa214Scgd errno = ENOENT;
2719f0aa214Scgd return (-1);
2729f0aa214Scgd }
2739f0aa214Scgd return (hashp->fp);
2749f0aa214Scgd }
2759f0aa214Scgd
2769f0aa214Scgd /************************** LOCAL CREATION ROUTINES **********************/
2779f0aa214Scgd static HTAB *
init_hash(HTAB * hashp,const char * file,const HASHINFO * info)278cb9daf8fSchristos init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
2799f0aa214Scgd {
2809f0aa214Scgd struct stat statbuf;
2819f0aa214Scgd int nelem;
2829f0aa214Scgd
2839f0aa214Scgd nelem = 1;
2849f0aa214Scgd hashp->NKEYS = 0;
2859f0aa214Scgd hashp->LORDER = BYTE_ORDER;
2869f0aa214Scgd hashp->BSIZE = DEF_BUCKET_SIZE;
2879f0aa214Scgd hashp->BSHIFT = DEF_BUCKET_SHIFT;
2889f0aa214Scgd hashp->SGSIZE = DEF_SEGSIZE;
2899f0aa214Scgd hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
2909f0aa214Scgd hashp->DSIZE = DEF_DIRSIZE;
2919f0aa214Scgd hashp->FFACTOR = DEF_FFACTOR;
2929f0aa214Scgd hashp->hash = __default_hash;
2939f0aa214Scgd memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
2949f0aa214Scgd memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
2959f0aa214Scgd
2969f0aa214Scgd /* Fix bucket size to be optimal for file system */
2979f0aa214Scgd if (file != NULL) {
2989f0aa214Scgd if (stat(file, &statbuf))
2999f0aa214Scgd return (NULL);
300bab13a9dSchristos hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE);
30140b37a3bSjoerg hashp->BSHIFT = __log2((uint32_t)hashp->BSIZE);
3029f0aa214Scgd }
3039f0aa214Scgd
3049f0aa214Scgd if (info) {
3059f0aa214Scgd if (info->bsize) {
3069f0aa214Scgd /* Round pagesize up to power of 2 */
3079f0aa214Scgd hashp->BSHIFT = __log2(info->bsize);
3089f0aa214Scgd hashp->BSIZE = 1 << hashp->BSHIFT;
309bab13a9dSchristos if (hashp->BSIZE > MAX_BSIZE) {
3109f0aa214Scgd errno = EINVAL;
3119f0aa214Scgd return (NULL);
3129f0aa214Scgd }
3139f0aa214Scgd }
3149f0aa214Scgd if (info->ffactor)
3159f0aa214Scgd hashp->FFACTOR = info->ffactor;
3169f0aa214Scgd if (info->hash)
3179f0aa214Scgd hashp->hash = info->hash;
3189f0aa214Scgd if (info->nelem)
3199f0aa214Scgd nelem = info->nelem;
3209f0aa214Scgd if (info->lorder) {
3219f0aa214Scgd if (info->lorder != BIG_ENDIAN &&
3229f0aa214Scgd info->lorder != LITTLE_ENDIAN) {
3239f0aa214Scgd errno = EINVAL;
3249f0aa214Scgd return (NULL);
3259f0aa214Scgd }
3269f0aa214Scgd hashp->LORDER = info->lorder;
3279f0aa214Scgd }
3289f0aa214Scgd }
3299f0aa214Scgd /* init_htab should destroy the table and set errno if it fails */
33061238e71Schristos if (init_htab(hashp, (size_t)nelem))
3319f0aa214Scgd return (NULL);
3329f0aa214Scgd else
3339f0aa214Scgd return (hashp);
3349f0aa214Scgd }
3359f0aa214Scgd /*
3369f0aa214Scgd * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
3379f0aa214Scgd * the table and set errno, so we just pass the error information along.
3389f0aa214Scgd *
3399f0aa214Scgd * Returns 0 on No Error
3409f0aa214Scgd */
3419f0aa214Scgd static int
init_htab(HTAB * hashp,size_t nelem)342cb9daf8fSchristos init_htab(HTAB *hashp, size_t nelem)
3439f0aa214Scgd {
344cb9daf8fSchristos int nbuckets;
34540b37a3bSjoerg uint32_t nsegs;
3469f0aa214Scgd int l2;
3479f0aa214Scgd
3489f0aa214Scgd /*
3499f0aa214Scgd * Divide number of elements by the fill factor and determine a
3509f0aa214Scgd * desired number of buckets. Allocate space for the next greater
3519f0aa214Scgd * power of two number of buckets.
3529f0aa214Scgd */
3539f0aa214Scgd nelem = (nelem - 1) / hashp->FFACTOR + 1;
3549f0aa214Scgd
35540b37a3bSjoerg _DBFIT(nelem, uint32_t);
35640b37a3bSjoerg l2 = __log2(MAX((uint32_t)nelem, 2));
3579f0aa214Scgd nbuckets = 1 << l2;
3589f0aa214Scgd
3599f0aa214Scgd hashp->SPARES[l2] = l2 + 1;
3609f0aa214Scgd hashp->SPARES[l2 + 1] = l2 + 1;
3619f0aa214Scgd hashp->OVFL_POINT = l2;
3629f0aa214Scgd hashp->LAST_FREED = 2;
3639f0aa214Scgd
3649f0aa214Scgd /* First bitmap page is at: splitpoint l2 page offset 1 */
36561238e71Schristos if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0))
3669f0aa214Scgd return (-1);
3679f0aa214Scgd
3689f0aa214Scgd hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
3699f0aa214Scgd hashp->HIGH_MASK = (nbuckets << 1) - 1;
37061238e71Schristos /* LINTED constant in conditional context */
3719f0aa214Scgd hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
3729f0aa214Scgd hashp->BSHIFT) + 1;
3739f0aa214Scgd
3749f0aa214Scgd nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
3759f0aa214Scgd nsegs = 1 << __log2(nsegs);
3769f0aa214Scgd
377c7201a0fSlukem if (nsegs > (uint32_t)hashp->DSIZE)
3789f0aa214Scgd hashp->DSIZE = nsegs;
37961238e71Schristos return (alloc_segs(hashp, (int)nsegs));
3809f0aa214Scgd }
3819f0aa214Scgd
3829f0aa214Scgd /********************** DESTROY/CLOSE ROUTINES ************************/
3839f0aa214Scgd
3849f0aa214Scgd /*
3859f0aa214Scgd * Flushes any changes to the file if necessary and destroys the hashp
3869f0aa214Scgd * structure, freeing all allocated space.
3879f0aa214Scgd */
3889f0aa214Scgd static int
hdestroy(HTAB * hashp)389cb9daf8fSchristos hdestroy(HTAB *hashp)
3909f0aa214Scgd {
3919f0aa214Scgd int i, save_errno;
3929f0aa214Scgd
3939f0aa214Scgd save_errno = 0;
3949f0aa214Scgd
3959f0aa214Scgd #ifdef HASH_STATISTICS
396cb9daf8fSchristos (void)fprintf(stderr, "hdestroy: accesses %d collisions %d\n",
3979f0aa214Scgd hash_accesses, hash_collisions);
398cb9daf8fSchristos (void)fprintf(stderr, "hdestroy: expansions %d\n",
3999f0aa214Scgd hash_expansions);
400cb9daf8fSchristos (void)fprintf(stderr, "hdestroy: overflows %d\n",
4019f0aa214Scgd hash_overflows);
402cb9daf8fSchristos (void)fprintf(stderr, "keys %d maxp %d segmentcount %d\n",
4039f0aa214Scgd hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
4049f0aa214Scgd
4059f0aa214Scgd for (i = 0; i < NCACHED; i++)
4069f0aa214Scgd (void)fprintf(stderr,
4079f0aa214Scgd "spares[%d] = %d\n", i, hashp->SPARES[i]);
4089f0aa214Scgd #endif
4099f0aa214Scgd /*
4109f0aa214Scgd * Call on buffer manager to free buffers, and if required,
4119f0aa214Scgd * write them to disk.
4129f0aa214Scgd */
4139f0aa214Scgd if (__buf_free(hashp, 1, hashp->save_file))
4149f0aa214Scgd save_errno = errno;
4159f0aa214Scgd if (hashp->dir) {
4169f0aa214Scgd free(*hashp->dir); /* Free initial segments */
4179f0aa214Scgd /* Free extra segments */
4189f0aa214Scgd while (hashp->exsegs--)
4199f0aa214Scgd free(hashp->dir[--hashp->nsegs]);
4209f0aa214Scgd free(hashp->dir);
4219f0aa214Scgd }
4229f0aa214Scgd if (flush_meta(hashp) && !save_errno)
4239f0aa214Scgd save_errno = errno;
4249f0aa214Scgd /* Free Bigmaps */
4259f0aa214Scgd for (i = 0; i < hashp->nmaps; i++)
4269f0aa214Scgd if (hashp->mapp[i])
4279f0aa214Scgd free(hashp->mapp[i]);
4289f0aa214Scgd
4299f0aa214Scgd if (hashp->fp != -1)
4309f0aa214Scgd (void)close(hashp->fp);
4319f0aa214Scgd
43298a60eb8Scgd free(hashp);
43398a60eb8Scgd
4349f0aa214Scgd if (save_errno) {
4359f0aa214Scgd errno = save_errno;
4369f0aa214Scgd return (ERROR);
4379f0aa214Scgd }
4389f0aa214Scgd return (SUCCESS);
4399f0aa214Scgd }
4409f0aa214Scgd /*
4419f0aa214Scgd * Write modified pages to disk
4429f0aa214Scgd *
4439f0aa214Scgd * Returns:
4449f0aa214Scgd * 0 == OK
4459f0aa214Scgd * -1 ERROR
4469f0aa214Scgd */
4479f0aa214Scgd static int
hash_sync(const DB * dbp,uint32_t flags)44840b37a3bSjoerg hash_sync(const DB *dbp, uint32_t flags)
4499f0aa214Scgd {
4509f0aa214Scgd HTAB *hashp;
4519f0aa214Scgd
4529f0aa214Scgd if (flags != 0) {
4539f0aa214Scgd errno = EINVAL;
4549f0aa214Scgd return (ERROR);
4559f0aa214Scgd }
4569f0aa214Scgd
4579f0aa214Scgd if (!dbp)
4589f0aa214Scgd return (ERROR);
4599f0aa214Scgd
460cb9daf8fSchristos hashp = dbp->internal;
4619f0aa214Scgd if (!hashp->save_file)
4629f0aa214Scgd return (0);
4639f0aa214Scgd if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
4649f0aa214Scgd return (ERROR);
4659f0aa214Scgd hashp->new_file = 0;
4669f0aa214Scgd return (0);
4679f0aa214Scgd }
4689f0aa214Scgd
4699f0aa214Scgd /*
4709f0aa214Scgd * Returns:
4719f0aa214Scgd * 0 == OK
4729f0aa214Scgd * -1 indicates that errno should be set
4739f0aa214Scgd */
4749f0aa214Scgd static int
flush_meta(HTAB * hashp)475cb9daf8fSchristos flush_meta(HTAB *hashp)
4769f0aa214Scgd {
4779f0aa214Scgd HASHHDR *whdrp;
4789f0aa214Scgd #if BYTE_ORDER == LITTLE_ENDIAN
4799f0aa214Scgd HASHHDR whdr;
4809f0aa214Scgd #endif
481cb9daf8fSchristos int fp, i;
482cb9daf8fSchristos ssize_t wsize;
4839f0aa214Scgd
4849f0aa214Scgd if (!hashp->save_file)
4859f0aa214Scgd return (0);
4869f0aa214Scgd hashp->MAGIC = HASHMAGIC;
4879f0aa214Scgd hashp->VERSION = HASHVERSION;
4889f0aa214Scgd hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
4899f0aa214Scgd
4909f0aa214Scgd fp = hashp->fp;
4919f0aa214Scgd whdrp = &hashp->hdr;
4929f0aa214Scgd #if BYTE_ORDER == LITTLE_ENDIAN
4939f0aa214Scgd whdrp = &whdr;
4949f0aa214Scgd swap_header_copy(&hashp->hdr, whdrp);
4959f0aa214Scgd #endif
496317d4e84Sthorpej if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
4979f0aa214Scgd return (-1);
4989f0aa214Scgd else
4999f0aa214Scgd if (wsize != sizeof(HASHHDR)) {
5009f0aa214Scgd errno = EFTYPE;
501135c9d24Sjtc hashp->err = errno;
5029f0aa214Scgd return (-1);
5039f0aa214Scgd }
5049f0aa214Scgd for (i = 0; i < NCACHED; i++)
5059f0aa214Scgd if (hashp->mapp[i])
50661238e71Schristos if (__put_page(hashp, (char *)(void *)hashp->mapp[i],
50761238e71Schristos (u_int)hashp->BITMAPS[i], 0, 1))
5089f0aa214Scgd return (-1);
5099f0aa214Scgd return (0);
5109f0aa214Scgd }
5119f0aa214Scgd
5129f0aa214Scgd /*******************************SEARCH ROUTINES *****************************/
5139f0aa214Scgd /*
5149f0aa214Scgd * All the access routines return
5159f0aa214Scgd *
5169f0aa214Scgd * Returns:
5179f0aa214Scgd * 0 on SUCCESS
5189f0aa214Scgd * 1 to indicate an external ERROR (i.e. key not found, etc)
5199f0aa214Scgd * -1 to indicate an internal ERROR (i.e. out of memory, etc)
5209f0aa214Scgd */
5219f0aa214Scgd static int
hash_get(const DB * dbp,const DBT * key,DBT * data,uint32_t flag)52240b37a3bSjoerg hash_get(const DB *dbp, const DBT *key, DBT *data, uint32_t flag)
5239f0aa214Scgd {
5249f0aa214Scgd HTAB *hashp;
5259f0aa214Scgd
526cb9daf8fSchristos hashp = dbp->internal;
5279f0aa214Scgd if (flag) {
528135c9d24Sjtc hashp->err = errno = EINVAL;
5299f0aa214Scgd return (ERROR);
5309f0aa214Scgd }
53103256c6eSchristos return (hash_access(hashp, HASH_GET, __UNCONST(key), data));
5329f0aa214Scgd }
5339f0aa214Scgd
5349f0aa214Scgd static int
hash_put(const DB * dbp,DBT * key,const DBT * data,uint32_t flag)53540b37a3bSjoerg hash_put(const DB *dbp, DBT *key, const DBT *data, uint32_t flag)
5369f0aa214Scgd {
5379f0aa214Scgd HTAB *hashp;
5389f0aa214Scgd
539cb9daf8fSchristos hashp = dbp->internal;
5409f0aa214Scgd if (flag && flag != R_NOOVERWRITE) {
541135c9d24Sjtc hashp->err = errno = EINVAL;
5429f0aa214Scgd return (ERROR);
5439f0aa214Scgd }
5449f0aa214Scgd if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
545135c9d24Sjtc hashp->err = errno = EPERM;
5469f0aa214Scgd return (ERROR);
5479f0aa214Scgd }
54861238e71Schristos /* LINTED const castaway */
5499f0aa214Scgd return (hash_access(hashp, flag == R_NOOVERWRITE ?
55003256c6eSchristos HASH_PUTNEW : HASH_PUT, __UNCONST(key), __UNCONST(data)));
5519f0aa214Scgd }
5529f0aa214Scgd
5539f0aa214Scgd static int
hash_delete(const DB * dbp,const DBT * key,uint32_t flag)55440b37a3bSjoerg hash_delete(const DB *dbp, const DBT *key, uint32_t flag)
5559f0aa214Scgd {
5569f0aa214Scgd HTAB *hashp;
5579f0aa214Scgd
558cb9daf8fSchristos hashp = dbp->internal;
5599f0aa214Scgd if (flag && flag != R_CURSOR) {
560135c9d24Sjtc hashp->err = errno = EINVAL;
5619f0aa214Scgd return (ERROR);
5629f0aa214Scgd }
5639f0aa214Scgd if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
564135c9d24Sjtc hashp->err = errno = EPERM;
5659f0aa214Scgd return (ERROR);
5669f0aa214Scgd }
56703256c6eSchristos return hash_access(hashp, HASH_DELETE, __UNCONST(key), NULL);
5689f0aa214Scgd }
5699f0aa214Scgd
5709f0aa214Scgd /*
5719f0aa214Scgd * Assume that hashp has been set in wrapper routine.
5729f0aa214Scgd */
5739f0aa214Scgd static int
hash_access(HTAB * hashp,ACTION action,DBT * key,DBT * val)574cb9daf8fSchristos hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
5759f0aa214Scgd {
576cb9daf8fSchristos BUFHEAD *rbufp;
5779f0aa214Scgd BUFHEAD *bufp, *save_bufp;
57840b37a3bSjoerg uint16_t *bp;
579cb9daf8fSchristos int n, ndx, off;
58061238e71Schristos size_t size;
581cb9daf8fSchristos char *kp;
58240b37a3bSjoerg uint16_t pageno;
5839f0aa214Scgd
5849f0aa214Scgd #ifdef HASH_STATISTICS
5859f0aa214Scgd hash_accesses++;
5869f0aa214Scgd #endif
5879f0aa214Scgd
588*14fcefd5Schristos off = HASH_BSIZE(hashp);
5899f0aa214Scgd size = key->size;
5909f0aa214Scgd kp = (char *)key->data;
59161238e71Schristos rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0);
5929f0aa214Scgd if (!rbufp)
5939f0aa214Scgd return (ERROR);
5949f0aa214Scgd save_bufp = rbufp;
5959f0aa214Scgd
5969f0aa214Scgd /* Pin the bucket chain */
5979f0aa214Scgd rbufp->flags |= BUF_PIN;
59840b37a3bSjoerg for (bp = (uint16_t *)(void *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
5999f0aa214Scgd if (bp[1] >= REAL_KEY) {
6009f0aa214Scgd /* Real key/data pair */
601c7201a0fSlukem if (size == (size_t)(off - *bp) &&
6029f0aa214Scgd memcmp(kp, rbufp->page + *bp, size) == 0)
6039f0aa214Scgd goto found;
6049f0aa214Scgd off = bp[1];
6059f0aa214Scgd #ifdef HASH_STATISTICS
6069f0aa214Scgd hash_collisions++;
6079f0aa214Scgd #endif
6089f0aa214Scgd bp += 2;
6099f0aa214Scgd ndx += 2;
6109f0aa214Scgd } else if (bp[1] == OVFLPAGE) {
61140b37a3bSjoerg rbufp = __get_buf(hashp, (uint32_t)*bp, rbufp, 0);
6129f0aa214Scgd if (!rbufp) {
6139f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6149f0aa214Scgd return (ERROR);
6159f0aa214Scgd }
6169f0aa214Scgd /* FOR LOOP INIT */
61740b37a3bSjoerg bp = (uint16_t *)(void *)rbufp->page;
6189f0aa214Scgd n = *bp++;
6199f0aa214Scgd ndx = 1;
620*14fcefd5Schristos off = HASH_BSIZE(hashp);
6219f0aa214Scgd } else if (bp[1] < REAL_KEY) {
6229f0aa214Scgd if ((ndx =
62361238e71Schristos __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
6249f0aa214Scgd goto found;
6259f0aa214Scgd if (ndx == -2) {
6269f0aa214Scgd bufp = rbufp;
6279f0aa214Scgd if (!(pageno =
6289f0aa214Scgd __find_last_page(hashp, &bufp))) {
6299f0aa214Scgd ndx = 0;
6309f0aa214Scgd rbufp = bufp;
6319f0aa214Scgd break; /* FOR */
6329f0aa214Scgd }
63340b37a3bSjoerg rbufp = __get_buf(hashp, (uint32_t)pageno,
63461238e71Schristos bufp, 0);
6359f0aa214Scgd if (!rbufp) {
6369f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6379f0aa214Scgd return (ERROR);
6389f0aa214Scgd }
6399f0aa214Scgd /* FOR LOOP INIT */
64040b37a3bSjoerg bp = (uint16_t *)(void *)rbufp->page;
6419f0aa214Scgd n = *bp++;
6429f0aa214Scgd ndx = 1;
643*14fcefd5Schristos off = HASH_BSIZE(hashp);
6449f0aa214Scgd } else {
6459f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6469f0aa214Scgd return (ERROR);
6479f0aa214Scgd }
6489f0aa214Scgd }
6499f0aa214Scgd
6509f0aa214Scgd /* Not found */
6519f0aa214Scgd switch (action) {
6529f0aa214Scgd case HASH_PUT:
6539f0aa214Scgd case HASH_PUTNEW:
6549f0aa214Scgd if (__addel(hashp, rbufp, key, val)) {
6559f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6569f0aa214Scgd return (ERROR);
6579f0aa214Scgd } else {
6589f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6599f0aa214Scgd return (SUCCESS);
6609f0aa214Scgd }
6619f0aa214Scgd case HASH_GET:
6629f0aa214Scgd case HASH_DELETE:
6639f0aa214Scgd default:
6649f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6659f0aa214Scgd return (ABNORMAL);
6669f0aa214Scgd }
6679f0aa214Scgd
6689f0aa214Scgd found:
6699f0aa214Scgd switch (action) {
6709f0aa214Scgd case HASH_PUTNEW:
6719f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6729f0aa214Scgd return (ABNORMAL);
6739f0aa214Scgd case HASH_GET:
67440b37a3bSjoerg bp = (uint16_t *)(void *)rbufp->page;
6759f0aa214Scgd if (bp[ndx + 1] < REAL_KEY) {
6769f0aa214Scgd if (__big_return(hashp, rbufp, ndx, val, 0))
6779f0aa214Scgd return (ERROR);
6789f0aa214Scgd } else {
67940b37a3bSjoerg val->data = (uint8_t *)rbufp->page + (int)bp[ndx + 1];
6809f0aa214Scgd val->size = bp[ndx] - bp[ndx + 1];
6819f0aa214Scgd }
6829f0aa214Scgd break;
6839f0aa214Scgd case HASH_PUT:
6849f0aa214Scgd if ((__delpair(hashp, rbufp, ndx)) ||
6859f0aa214Scgd (__addel(hashp, rbufp, key, val))) {
6869f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
6879f0aa214Scgd return (ERROR);
6889f0aa214Scgd }
6899f0aa214Scgd break;
6909f0aa214Scgd case HASH_DELETE:
6919f0aa214Scgd if (__delpair(hashp, rbufp, ndx))
6929f0aa214Scgd return (ERROR);
69325bda9e7Schristos /*
69425bda9e7Schristos * Our index lags 2 behind on the same page when we are
69525bda9e7Schristos * deleting the element pointed to by the index; otherwise
69625bda9e7Schristos * deleting randomly from an iterated hash produces undefined
69725bda9e7Schristos * results.
69825bda9e7Schristos */
69925bda9e7Schristos if (ndx != hashp->cndx - 2 || rbufp != hashp->cpage)
70025bda9e7Schristos break;
70125bda9e7Schristos
70225bda9e7Schristos if (hashp->cndx > 1) {
70325bda9e7Schristos /* Move back one element */
70425bda9e7Schristos hashp->cndx -= 2;
70525bda9e7Schristos } else {
70625bda9e7Schristos /*
70725bda9e7Schristos * Move back one page, and indicate to go to the last
70825bda9e7Schristos * element of the previous page by setting cndx to -1
70925bda9e7Schristos */
71025bda9e7Schristos hashp->cbucket--;
71125bda9e7Schristos hashp->cpage = NULL;
71225bda9e7Schristos hashp->cndx = -1;
71325bda9e7Schristos }
7149f0aa214Scgd break;
7159f0aa214Scgd default:
7169f0aa214Scgd abort();
7179f0aa214Scgd }
7189f0aa214Scgd save_bufp->flags &= ~BUF_PIN;
7199f0aa214Scgd return (SUCCESS);
7209f0aa214Scgd }
7219f0aa214Scgd
7229f0aa214Scgd static int
hash_seq(const DB * dbp,DBT * key,DBT * data,uint32_t flag)72340b37a3bSjoerg hash_seq(const DB *dbp, DBT *key, DBT *data, uint32_t flag)
7249f0aa214Scgd {
72540b37a3bSjoerg uint32_t bucket;
726cb9daf8fSchristos BUFHEAD *bufp = NULL; /* XXX: gcc */
7279f0aa214Scgd HTAB *hashp;
72840b37a3bSjoerg uint16_t *bp, ndx;
7299f0aa214Scgd
730cb9daf8fSchristos hashp = dbp->internal;
7319f0aa214Scgd if (flag && flag != R_FIRST && flag != R_NEXT) {
732135c9d24Sjtc hashp->err = errno = EINVAL;
7339f0aa214Scgd return (ERROR);
7349f0aa214Scgd }
7359f0aa214Scgd #ifdef HASH_STATISTICS
7369f0aa214Scgd hash_accesses++;
7379f0aa214Scgd #endif
7389f0aa214Scgd if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
7399f0aa214Scgd hashp->cbucket = 0;
7409f0aa214Scgd hashp->cndx = 1;
7419f0aa214Scgd hashp->cpage = NULL;
7429f0aa214Scgd }
7439f0aa214Scgd
7441be34afaSchristos next_bucket:
7459f0aa214Scgd for (bp = NULL; !bp || !bp[0]; ) {
7469f0aa214Scgd if (!(bufp = hashp->cpage)) {
7479f0aa214Scgd for (bucket = hashp->cbucket;
748c7201a0fSlukem bucket <= (uint32_t)hashp->MAX_BUCKET;
74925bda9e7Schristos bucket++) {
7509f0aa214Scgd bufp = __get_buf(hashp, bucket, NULL, 0);
7519f0aa214Scgd if (!bufp)
7529f0aa214Scgd return (ERROR);
7539f0aa214Scgd hashp->cpage = bufp;
75440b37a3bSjoerg bp = (uint16_t *)(void *)bufp->page;
7559f0aa214Scgd if (bp[0])
7569f0aa214Scgd break;
7579f0aa214Scgd }
7589f0aa214Scgd hashp->cbucket = bucket;
7599f0aa214Scgd if (hashp->cbucket > hashp->MAX_BUCKET) {
7609f0aa214Scgd hashp->cbucket = -1;
7619f0aa214Scgd return (ABNORMAL);
7629f0aa214Scgd }
76325bda9e7Schristos if (hashp->cndx == -1) {
76425bda9e7Schristos /* move to the last element of the page */
76525bda9e7Schristos hashp->cndx = 1;
76625bda9e7Schristos while (bp[hashp->cndx - 1] != 0)
76725bda9e7Schristos hashp->cndx += 2;
76825bda9e7Schristos } else {
76925bda9e7Schristos /* start on the first element */
77025bda9e7Schristos hashp->cndx = 1;
77125bda9e7Schristos }
7721be34afaSchristos } else {
7738b926f83Schristos bp = (uint16_t *)(void *)bufp->page;
7741be34afaSchristos if (flag == R_NEXT || flag == 0) {
7751be34afaSchristos if (hashp->cndx > bp[0]) {
7761be34afaSchristos hashp->cpage = NULL;
7771be34afaSchristos hashp->cbucket++;
7781be34afaSchristos hashp->cndx = 1;
7791be34afaSchristos goto next_bucket;
7801be34afaSchristos }
7811be34afaSchristos }
7821be34afaSchristos }
7831be34afaSchristos
7849f0aa214Scgd
785cb9daf8fSchristos _DIAGASSERT(bp != NULL);
786cb9daf8fSchristos _DIAGASSERT(bufp != NULL);
7879f0aa214Scgd while (bp[hashp->cndx + 1] == OVFLPAGE) {
7889f0aa214Scgd bufp = hashp->cpage =
78940b37a3bSjoerg __get_buf(hashp, (uint32_t)bp[hashp->cndx], bufp,
79061238e71Schristos 0);
7919f0aa214Scgd if (!bufp)
7929f0aa214Scgd return (ERROR);
79340b37a3bSjoerg bp = (uint16_t *)(void *)(bufp->page);
7949f0aa214Scgd hashp->cndx = 1;
7959f0aa214Scgd }
7969f0aa214Scgd if (!bp[0]) {
7979f0aa214Scgd hashp->cpage = NULL;
7989f0aa214Scgd ++hashp->cbucket;
7999f0aa214Scgd }
8009f0aa214Scgd }
8019f0aa214Scgd ndx = hashp->cndx;
8029f0aa214Scgd if (bp[ndx + 1] < REAL_KEY) {
8039f0aa214Scgd if (__big_keydata(hashp, bufp, key, data, 1))
8049f0aa214Scgd return (ERROR);
8058b926f83Schristos hashp->cndx = 1;
8069f0aa214Scgd } else {
807854b420cSchristos if (hashp->cpage == NULL)
80803c78303Srtr return (ERROR);
80940b37a3bSjoerg key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
810*14fcefd5Schristos key->size = (ndx > 1 ? bp[ndx - 1] : HASH_BSIZE(hashp)) - bp[ndx];
81140b37a3bSjoerg data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
8129f0aa214Scgd data->size = bp[ndx] - bp[ndx + 1];
81325bda9e7Schristos hashp->cndx += 2;
8148b926f83Schristos }
8159f0aa214Scgd return (SUCCESS);
8169f0aa214Scgd }
8179f0aa214Scgd
8189f0aa214Scgd /********************************* UTILITIES ************************/
8199f0aa214Scgd
8209f0aa214Scgd /*
8219f0aa214Scgd * Returns:
8229f0aa214Scgd * 0 ==> OK
8239f0aa214Scgd * -1 ==> Error
8249f0aa214Scgd */
825cb9daf8fSchristos int
__expand_table(HTAB * hashp)826cb9daf8fSchristos __expand_table(HTAB *hashp)
8279f0aa214Scgd {
82840b37a3bSjoerg uint32_t old_bucket, new_bucket;
829cb9daf8fSchristos int new_segnum, spare_ndx;
830cb9daf8fSchristos size_t dirsize;
8319f0aa214Scgd
8329f0aa214Scgd #ifdef HASH_STATISTICS
8339f0aa214Scgd hash_expansions++;
8349f0aa214Scgd #endif
8359f0aa214Scgd new_bucket = ++hashp->MAX_BUCKET;
8369f0aa214Scgd old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
8379f0aa214Scgd
8389f0aa214Scgd new_segnum = new_bucket >> hashp->SSHIFT;
8399f0aa214Scgd
8409f0aa214Scgd /* Check if we need a new segment */
8419f0aa214Scgd if (new_segnum >= hashp->nsegs) {
8429f0aa214Scgd /* Check if we need to expand directory */
8439f0aa214Scgd if (new_segnum >= hashp->DSIZE) {
8449f0aa214Scgd /* Reallocate directory */
8459f0aa214Scgd dirsize = hashp->DSIZE * sizeof(SEGMENT *);
8469f0aa214Scgd if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
8479f0aa214Scgd return (-1);
848c5e820caSchristos dirsize <<= 1;
849c5e820caSchristos _DBFIT(dirsize, uint32_t);
850c5e820caSchristos hashp->DSIZE = (uint32_t)dirsize;
8519f0aa214Scgd }
852a6d14e36Scgd if ((hashp->dir[new_segnum] =
853cb9daf8fSchristos calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
8549f0aa214Scgd return (-1);
8559f0aa214Scgd hashp->exsegs++;
8569f0aa214Scgd hashp->nsegs++;
8579f0aa214Scgd }
8589f0aa214Scgd /*
8599f0aa214Scgd * If the split point is increasing (MAX_BUCKET's log base 2
8609f0aa214Scgd * * increases), we need to copy the current contents of the spare
8619f0aa214Scgd * split bucket to the next bucket.
8629f0aa214Scgd */
86340b37a3bSjoerg spare_ndx = __log2((uint32_t)(hashp->MAX_BUCKET + 1));
8649f0aa214Scgd if (spare_ndx > hashp->OVFL_POINT) {
8659f0aa214Scgd hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
8669f0aa214Scgd hashp->OVFL_POINT = spare_ndx;
8679f0aa214Scgd }
8689f0aa214Scgd
869c7201a0fSlukem if (new_bucket > (uint32_t)hashp->HIGH_MASK) {
8709f0aa214Scgd /* Starting a new doubling */
8719f0aa214Scgd hashp->LOW_MASK = hashp->HIGH_MASK;
8729f0aa214Scgd hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
8739f0aa214Scgd }
8749f0aa214Scgd /* Relocate records to the new bucket */
8759f0aa214Scgd return (__split_page(hashp, old_bucket, new_bucket));
8769f0aa214Scgd }
8779f0aa214Scgd
8789f0aa214Scgd /*
8799f0aa214Scgd * If realloc guarantees that the pointer is not destroyed if the realloc
8809f0aa214Scgd * fails, then this routine can go away.
8819f0aa214Scgd */
8829f0aa214Scgd static void *
hash_realloc(SEGMENT ** p_ptr,size_t oldsize,size_t newsize)883cb9daf8fSchristos hash_realloc(SEGMENT **p_ptr, size_t oldsize, size_t newsize)
8849f0aa214Scgd {
885cb9daf8fSchristos void *p;
8869f0aa214Scgd
887cb9daf8fSchristos if ((p = malloc(newsize)) != NULL) {
888cb9daf8fSchristos memmove(p, *p_ptr, oldsize);
889cb9daf8fSchristos memset((char *)p + oldsize, 0, newsize - oldsize);
8909f0aa214Scgd free(*p_ptr);
8919f0aa214Scgd *p_ptr = p;
8929f0aa214Scgd }
8939f0aa214Scgd return (p);
8949f0aa214Scgd }
8959f0aa214Scgd
89640b37a3bSjoerg uint32_t
__call_hash(HTAB * hashp,char * k,int len)897cb9daf8fSchristos __call_hash(HTAB *hashp, char *k, int len)
8989f0aa214Scgd {
8999f0aa214Scgd int n, bucket;
9009f0aa214Scgd
90161238e71Schristos n = hashp->hash(k, (size_t)len);
9029f0aa214Scgd bucket = n & hashp->HIGH_MASK;
9039f0aa214Scgd if (bucket > hashp->MAX_BUCKET)
9049f0aa214Scgd bucket = bucket & hashp->LOW_MASK;
9059f0aa214Scgd return (bucket);
9069f0aa214Scgd }
9079f0aa214Scgd
9089f0aa214Scgd /*
9099f0aa214Scgd * Allocate segment table. On error, destroy the table and set errno.
9109f0aa214Scgd *
9119f0aa214Scgd * Returns 0 on success
9129f0aa214Scgd */
9139f0aa214Scgd static int
alloc_segs(HTAB * hashp,int nsegs)914cb9daf8fSchristos alloc_segs(HTAB *hashp, int nsegs)
9159f0aa214Scgd {
916cb9daf8fSchristos int i;
917cb9daf8fSchristos SEGMENT store;
9189f0aa214Scgd
9199f0aa214Scgd int save_errno;
9209f0aa214Scgd
921cb9daf8fSchristos hashp->dir = calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *));
922cb9daf8fSchristos if (hashp->dir == NULL) {
9239f0aa214Scgd save_errno = errno;
9249f0aa214Scgd (void)hdestroy(hashp);
9259f0aa214Scgd errno = save_errno;
9269f0aa214Scgd return (-1);
9279f0aa214Scgd }
92853cd70b6Schristos hashp->nsegs = nsegs;
92953cd70b6Schristos if (nsegs == 0)
93053cd70b6Schristos return 0;
9319f0aa214Scgd /* Allocate segments */
932cb9daf8fSchristos store = calloc((size_t)(nsegs << hashp->SSHIFT), sizeof(SEGMENT));
933cb9daf8fSchristos if (store == NULL) {
9349f0aa214Scgd save_errno = errno;
9359f0aa214Scgd (void)hdestroy(hashp);
9369f0aa214Scgd errno = save_errno;
9379f0aa214Scgd return (-1);
9389f0aa214Scgd }
93953cd70b6Schristos for (i = 0; i < nsegs; i++)
9409f0aa214Scgd hashp->dir[i] = &store[i << hashp->SSHIFT];
9419f0aa214Scgd return (0);
9429f0aa214Scgd }
9439f0aa214Scgd
9449f0aa214Scgd #if BYTE_ORDER == LITTLE_ENDIAN
9459f0aa214Scgd /*
9469f0aa214Scgd * Hashp->hdr needs to be byteswapped.
9479f0aa214Scgd */
9489f0aa214Scgd static void
swap_header_copy(HASHHDR * srcp,HASHHDR * destp)949cb9daf8fSchristos swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
9509f0aa214Scgd {
951cb9daf8fSchristos size_t i;
9529f0aa214Scgd
953a6d14e36Scgd P_32_COPY(srcp->magic, destp->magic);
954a6d14e36Scgd P_32_COPY(srcp->version, destp->version);
955a6d14e36Scgd P_32_COPY(srcp->lorder, destp->lorder);
956a6d14e36Scgd P_32_COPY(srcp->bsize, destp->bsize);
957a6d14e36Scgd P_32_COPY(srcp->bshift, destp->bshift);
958a6d14e36Scgd P_32_COPY(srcp->dsize, destp->dsize);
959a6d14e36Scgd P_32_COPY(srcp->ssize, destp->ssize);
960a6d14e36Scgd P_32_COPY(srcp->sshift, destp->sshift);
961a6d14e36Scgd P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
962a6d14e36Scgd P_32_COPY(srcp->last_freed, destp->last_freed);
963a6d14e36Scgd P_32_COPY(srcp->max_bucket, destp->max_bucket);
964a6d14e36Scgd P_32_COPY(srcp->high_mask, destp->high_mask);
965a6d14e36Scgd P_32_COPY(srcp->low_mask, destp->low_mask);
966a6d14e36Scgd P_32_COPY(srcp->ffactor, destp->ffactor);
967a6d14e36Scgd P_32_COPY(srcp->nkeys, destp->nkeys);
968a6d14e36Scgd P_32_COPY(srcp->hdrpages, destp->hdrpages);
969a6d14e36Scgd P_32_COPY(srcp->h_charkey, destp->h_charkey);
9709f0aa214Scgd for (i = 0; i < NCACHED; i++) {
971a6d14e36Scgd P_32_COPY(srcp->spares[i], destp->spares[i]);
972a6d14e36Scgd P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
9739f0aa214Scgd }
9749f0aa214Scgd }
9759f0aa214Scgd
9769f0aa214Scgd static void
swap_header(HTAB * hashp)977cb9daf8fSchristos swap_header(HTAB *hashp)
9789f0aa214Scgd {
9799f0aa214Scgd HASHHDR *hdrp;
980cb9daf8fSchristos size_t i;
9819f0aa214Scgd
9829f0aa214Scgd hdrp = &hashp->hdr;
9839f0aa214Scgd
984a6d14e36Scgd M_32_SWAP(hdrp->magic);
985a6d14e36Scgd M_32_SWAP(hdrp->version);
986a6d14e36Scgd M_32_SWAP(hdrp->lorder);
987a6d14e36Scgd M_32_SWAP(hdrp->bsize);
988a6d14e36Scgd M_32_SWAP(hdrp->bshift);
989a6d14e36Scgd M_32_SWAP(hdrp->dsize);
990a6d14e36Scgd M_32_SWAP(hdrp->ssize);
991a6d14e36Scgd M_32_SWAP(hdrp->sshift);
992a6d14e36Scgd M_32_SWAP(hdrp->ovfl_point);
993a6d14e36Scgd M_32_SWAP(hdrp->last_freed);
994a6d14e36Scgd M_32_SWAP(hdrp->max_bucket);
995a6d14e36Scgd M_32_SWAP(hdrp->high_mask);
996a6d14e36Scgd M_32_SWAP(hdrp->low_mask);
997a6d14e36Scgd M_32_SWAP(hdrp->ffactor);
998a6d14e36Scgd M_32_SWAP(hdrp->nkeys);
999a6d14e36Scgd M_32_SWAP(hdrp->hdrpages);
1000a6d14e36Scgd M_32_SWAP(hdrp->h_charkey);
10019f0aa214Scgd for (i = 0; i < NCACHED; i++) {
1002a6d14e36Scgd M_32_SWAP(hdrp->spares[i]);
1003a6d14e36Scgd M_16_SWAP(hdrp->bitmaps[i]);
10049f0aa214Scgd }
10059f0aa214Scgd }
10069f0aa214Scgd #endif
1007