OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [search/] [hash.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*-
/*-
 * Copyright (c) 1990, 1993, 1994
 * Copyright (c) 1990, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * This code is derived from software contributed to Berkeley by
 * This code is derived from software contributed to Berkeley by
 * Margo Seltzer.
 * Margo Seltzer.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      This product includes software developed by the University of
 *      California, Berkeley and its contributors.
 *      California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 */
 */
 
 
#include <sys/param.h>
#include <sys/param.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)hash.c      8.9 (Berkeley) 6/16/94";
static char sccsid[] = "@(#)hash.c      8.9 (Berkeley) 6/16/94";
#endif /* LIBC_SCCS and not lint */
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/types.h>
 
 
#include <sys/stat.h>
#include <sys/stat.h>
 
 
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#ifdef DEBUG
#ifdef DEBUG
#include <assert.h>
#include <assert.h>
#endif
#endif
 
 
#include "db_local.h"
#include "db_local.h"
#include "hash.h"
#include "hash.h"
#include "page.h"
#include "page.h"
#include "extern.h"
#include "extern.h"
 
 
static int   alloc_segs(HTAB *, int);
static int   alloc_segs(HTAB *, int);
static int   flush_meta(HTAB *);
static int   flush_meta(HTAB *);
static int   hash_access(HTAB *, ACTION, DBT *, DBT *);
static int   hash_access(HTAB *, ACTION, DBT *, DBT *);
static int   hash_close(DB *);
static int   hash_close(DB *);
static int   hash_delete(const DB *, const DBT *, __uint32_t);
static int   hash_delete(const DB *, const DBT *, __uint32_t);
static int   hash_fd(const DB *);
static int   hash_fd(const DB *);
static int   hash_get(const DB *, const DBT *, DBT *, __uint32_t);
static int   hash_get(const DB *, const DBT *, DBT *, __uint32_t);
static int   hash_put(const DB *, DBT *, const DBT *, __uint32_t);
static int   hash_put(const DB *, DBT *, const DBT *, __uint32_t);
static void *hash_realloc(SEGMENT **, int, int);
static void *hash_realloc(SEGMENT **, int, int);
static int   hash_seq(const DB *, DBT *, DBT *, __uint32_t);
static int   hash_seq(const DB *, DBT *, DBT *, __uint32_t);
static int   hash_sync(const DB *, __uint32_t);
static int   hash_sync(const DB *, __uint32_t);
static int   hdestroy(HTAB *);
static int   hdestroy(HTAB *);
static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
static int   init_htab(HTAB *, int);
static int   init_htab(HTAB *, int);
#if (BYTE_ORDER == LITTLE_ENDIAN)
#if (BYTE_ORDER == LITTLE_ENDIAN)
static void  swap_header(HTAB *);
static void  swap_header(HTAB *);
static void  swap_header_copy(HASHHDR *, HASHHDR *);
static void  swap_header_copy(HASHHDR *, HASHHDR *);
#endif
#endif
 
 
/* Macros for min/max.  */
/* Macros for min/max.  */
#ifndef MIN
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#endif
#ifndef MAX
#ifndef MAX
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif
#endif
 
 
/* Fast arithmetic, relying on powers of 2, */
/* Fast arithmetic, relying on powers of 2, */
#define MOD(x, y)               ((x) & ((y) - 1))
#define MOD(x, y)               ((x) & ((y) - 1))
 
 
#define RETURN_ERROR(ERR, LOC)  { save_errno = ERR; goto LOC; }
#define RETURN_ERROR(ERR, LOC)  { save_errno = ERR; goto LOC; }
 
 
/* Return values */
/* Return values */
#define SUCCESS  (0)
#define SUCCESS  (0)
#define ERROR   (-1)
#define ERROR   (-1)
#define ABNORMAL (1)
#define ABNORMAL (1)
 
 
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
#endif
#endif
 
 
/************************** INTERFACE ROUTINES ***************************/
/************************** INTERFACE ROUTINES ***************************/
/* OPEN/CLOSE */
/* OPEN/CLOSE */
 
 
extern DB *
extern DB *
__hash_open(file, flags, mode, info, dflags)
__hash_open(file, flags, mode, info, dflags)
        const char *file;
        const char *file;
        int flags, mode, dflags;
        int flags, mode, dflags;
        const HASHINFO *info;   /* Special directives for create */
        const HASHINFO *info;   /* Special directives for create */
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
        struct stat64 statbuf;
        struct stat64 statbuf;
#else
#else
        struct stat statbuf;
        struct stat statbuf;
#endif
#endif
        DB *dbp;
        DB *dbp;
        int bpages, hdrsize, new_table, nsegs, save_errno;
        int bpages, hdrsize, new_table, nsegs, save_errno;
 
 
        if ((flags & O_ACCMODE) == O_WRONLY) {
        if ((flags & O_ACCMODE) == O_WRONLY) {
                errno = EINVAL;
                errno = EINVAL;
                return (NULL);
                return (NULL);
        }
        }
 
 
        if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
        if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
                return (NULL);
                return (NULL);
        hashp->fp = -1;
        hashp->fp = -1;
 
 
        /*
        /*
         * Even if user wants write only, we need to be able to read
         * Even if user wants write only, we need to be able to read
         * the actual file, so we need to open it read/write. But, the
         * the actual file, so we need to open it read/write. But, the
         * field in the hashp structure needs to be accurate so that
         * field in the hashp structure needs to be accurate so that
         * we can check accesses.
         * we can check accesses.
         */
         */
        hashp->flags = flags;
        hashp->flags = flags;
 
 
        new_table = 0;
        new_table = 0;
        if (!file || (flags & O_TRUNC) ||
        if (!file || (flags & O_TRUNC) ||
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
            (stat64(file, &statbuf) && (errno == ENOENT))) {
            (stat64(file, &statbuf) && (errno == ENOENT))) {
#else
#else
            (stat(file, &statbuf) && (errno == ENOENT))) {
            (stat(file, &statbuf) && (errno == ENOENT))) {
#endif
#endif
                if (errno == ENOENT)
                if (errno == ENOENT)
                        errno = 0; /* Just in case someone looks at errno */
                        errno = 0; /* Just in case someone looks at errno */
                new_table = 1;
                new_table = 1;
        }
        }
        if (file) {
        if (file) {
                if ((hashp->fp = open(file, flags, mode)) == -1)
                if ((hashp->fp = open(file, flags, mode)) == -1)
                        RETURN_ERROR(errno, error0);
                        RETURN_ERROR(errno, error0);
 
 
                /* if the .db file is empty, and we had permission to create
                /* if the .db file is empty, and we had permission to create
                   a new .db file, then reinitialize the database */
                   a new .db file, then reinitialize the database */
                if ((flags & O_CREAT) &&
                if ((flags & O_CREAT) &&
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
                     fstat64(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
                     fstat64(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
#else
#else
                     fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
                     fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
#endif
#endif
                        new_table = 1;
                        new_table = 1;
 
 
#ifdef HAVE_FCNTL
#ifdef HAVE_FCNTL
                (void)fcntl(hashp->fp, F_SETFD, 1);
                (void)fcntl(hashp->fp, F_SETFD, 1);
#endif
#endif
        }
        }
        if (new_table) {
        if (new_table) {
                if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
                if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
                        RETURN_ERROR(errno, error1);
                        RETURN_ERROR(errno, error1);
        } else {
        } else {
                /* Table already exists */
                /* Table already exists */
                if (info && info->hash)
                if (info && info->hash)
                        hashp->hash = info->hash;
                        hashp->hash = info->hash;
                else
                else
                        hashp->hash = __default_hash;
                        hashp->hash = __default_hash;
 
 
                hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
                hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
#if (BYTE_ORDER == LITTLE_ENDIAN)
#if (BYTE_ORDER == LITTLE_ENDIAN)
                swap_header(hashp);
                swap_header(hashp);
#endif
#endif
                if (hdrsize == -1)
                if (hdrsize == -1)
                        RETURN_ERROR(errno, error1);
                        RETURN_ERROR(errno, error1);
                if (hdrsize != sizeof(HASHHDR))
                if (hdrsize != sizeof(HASHHDR))
                        RETURN_ERROR(EFTYPE, error1);
                        RETURN_ERROR(EFTYPE, error1);
                /* Verify file type, versions and hash function */
                /* Verify file type, versions and hash function */
                if (hashp->MAGIC != HASHMAGIC)
                if (hashp->MAGIC != HASHMAGIC)
                        RETURN_ERROR(EFTYPE, error1);
                        RETURN_ERROR(EFTYPE, error1);
#define OLDHASHVERSION  1
#define OLDHASHVERSION  1
                if (hashp->HASH_VERSION != HASHVERSION &&
                if (hashp->HASH_VERSION != HASHVERSION &&
                    hashp->HASH_VERSION != OLDHASHVERSION)
                    hashp->HASH_VERSION != OLDHASHVERSION)
                        RETURN_ERROR(EFTYPE, error1);
                        RETURN_ERROR(EFTYPE, error1);
                if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
                if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
                        RETURN_ERROR(EFTYPE, error1);
                        RETURN_ERROR(EFTYPE, error1);
                /*
                /*
                 * Figure out how many segments we need.  Max_Bucket is the
                 * Figure out how many segments we need.  Max_Bucket is the
                 * maximum bucket number, so the number of buckets is
                 * maximum bucket number, so the number of buckets is
                 * max_bucket + 1.
                 * max_bucket + 1.
                 */
                 */
                nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
                nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
                         hashp->SGSIZE;
                         hashp->SGSIZE;
                hashp->nsegs = 0;
                hashp->nsegs = 0;
                if (alloc_segs(hashp, nsegs))
                if (alloc_segs(hashp, nsegs))
                        /*
                        /*
                         * If alloc_segs fails, table will have been destroyed
                         * If alloc_segs fails, table will have been destroyed
                         * and errno will have been set.
                         * and errno will have been set.
                         */
                         */
                        return (NULL);
                        return (NULL);
                /* Read in bitmaps */
                /* Read in bitmaps */
                bpages = (hashp->SPARES[hashp->OVFL_POINT] +
                bpages = (hashp->SPARES[hashp->OVFL_POINT] +
                    (hashp->BSIZE << BYTE_SHIFT) - 1) >>
                    (hashp->BSIZE << BYTE_SHIFT) - 1) >>
                    (hashp->BSHIFT + BYTE_SHIFT);
                    (hashp->BSHIFT + BYTE_SHIFT);
 
 
                hashp->nmaps = bpages;
                hashp->nmaps = bpages;
                (void)memset(&hashp->mapp[0], 0, bpages * sizeof(__uint32_t *));
                (void)memset(&hashp->mapp[0], 0, bpages * sizeof(__uint32_t *));
        }
        }
 
 
        /* Initialize Buffer Manager */
        /* Initialize Buffer Manager */
        if (info && info->cachesize)
        if (info && info->cachesize)
                __buf_init(hashp, info->cachesize);
                __buf_init(hashp, info->cachesize);
        else
        else
                __buf_init(hashp, DEF_BUFSIZE);
                __buf_init(hashp, DEF_BUFSIZE);
 
 
        hashp->new_file = new_table;
        hashp->new_file = new_table;
        hashp->save_file = file && (hashp->flags & O_RDWR);
        hashp->save_file = file && (hashp->flags & O_RDWR);
        hashp->cbucket = -1;
        hashp->cbucket = -1;
        if (!(dbp = (DB *)malloc(sizeof(DB)))) {
        if (!(dbp = (DB *)malloc(sizeof(DB)))) {
                save_errno = errno;
                save_errno = errno;
                hdestroy(hashp);
                hdestroy(hashp);
                errno = save_errno;
                errno = save_errno;
                return (NULL);
                return (NULL);
        }
        }
        dbp->internal = hashp;
        dbp->internal = hashp;
        dbp->close = hash_close;
        dbp->close = hash_close;
        dbp->del = hash_delete;
        dbp->del = hash_delete;
        dbp->fd = hash_fd;
        dbp->fd = hash_fd;
        dbp->get = hash_get;
        dbp->get = hash_get;
        dbp->put = hash_put;
        dbp->put = hash_put;
        dbp->seq = hash_seq;
        dbp->seq = hash_seq;
        dbp->sync = hash_sync;
        dbp->sync = hash_sync;
        dbp->type = DB_HASH;
        dbp->type = DB_HASH;
 
 
#ifdef DEBUG
#ifdef DEBUG
        (void)fprintf(stderr,
        (void)fprintf(stderr,
"%s\n%s%x\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",
"%s\n%s%x\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",
            "init_htab:",
            "init_htab:",
            "TABLE POINTER   ", hashp,
            "TABLE POINTER   ", hashp,
            "BUCKET SIZE     ", hashp->BSIZE,
            "BUCKET SIZE     ", hashp->BSIZE,
            "BUCKET SHIFT    ", hashp->BSHIFT,
            "BUCKET SHIFT    ", hashp->BSHIFT,
            "DIRECTORY SIZE  ", hashp->DSIZE,
            "DIRECTORY SIZE  ", hashp->DSIZE,
            "SEGMENT SIZE    ", hashp->SGSIZE,
            "SEGMENT SIZE    ", hashp->SGSIZE,
            "SEGMENT SHIFT   ", hashp->SSHIFT,
            "SEGMENT SHIFT   ", hashp->SSHIFT,
            "FILL FACTOR     ", hashp->FFACTOR,
            "FILL FACTOR     ", hashp->FFACTOR,
            "MAX BUCKET      ", hashp->MAX_BUCKET,
            "MAX BUCKET      ", hashp->MAX_BUCKET,
            "OVFL POINT      ", hashp->OVFL_POINT,
            "OVFL POINT      ", hashp->OVFL_POINT,
            "LAST FREED      ", hashp->LAST_FREED,
            "LAST FREED      ", hashp->LAST_FREED,
            "HIGH MASK       ", hashp->HIGH_MASK,
            "HIGH MASK       ", hashp->HIGH_MASK,
            "LOW  MASK       ", hashp->LOW_MASK,
            "LOW  MASK       ", hashp->LOW_MASK,
            "NSEGS           ", hashp->nsegs,
            "NSEGS           ", hashp->nsegs,
            "NKEYS           ", hashp->NKEYS);
            "NKEYS           ", hashp->NKEYS);
#endif
#endif
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
        hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
        hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
#endif
#endif
        return (dbp);
        return (dbp);
 
 
error1:
error1:
        if (hashp != NULL)
        if (hashp != NULL)
                (void)close(hashp->fp);
                (void)close(hashp->fp);
 
 
error0:
error0:
        free(hashp);
        free(hashp);
        errno = save_errno;
        errno = save_errno;
        return (NULL);
        return (NULL);
}
}
 
 
static int
static int
hash_close(dbp)
hash_close(dbp)
        DB *dbp;
        DB *dbp;
{
{
        HTAB *hashp;
        HTAB *hashp;
        int retval;
        int retval;
 
 
        if (!dbp)
        if (!dbp)
                return (ERROR);
                return (ERROR);
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        retval = hdestroy(hashp);
        retval = hdestroy(hashp);
        free(dbp);
        free(dbp);
        return (retval);
        return (retval);
}
}
 
 
static int
static int
hash_fd(dbp)
hash_fd(dbp)
        const DB *dbp;
        const DB *dbp;
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
        if (!dbp)
        if (!dbp)
                return (ERROR);
                return (ERROR);
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (hashp->fp == -1) {
        if (hashp->fp == -1) {
                errno = ENOENT;
                errno = ENOENT;
                return (-1);
                return (-1);
        }
        }
        return (hashp->fp);
        return (hashp->fp);
}
}
 
 
/************************** LOCAL CREATION ROUTINES **********************/
/************************** LOCAL CREATION ROUTINES **********************/
static HTAB *
static HTAB *
init_hash(hashp, file, info)
init_hash(hashp, file, info)
        HTAB *hashp;
        HTAB *hashp;
        const char *file;
        const char *file;
        const HASHINFO *info;
        const HASHINFO *info;
{
{
        struct stat statbuf;
        struct stat statbuf;
        int nelem;
        int nelem;
 
 
        nelem = 1;
        nelem = 1;
        hashp->NKEYS = 0;
        hashp->NKEYS = 0;
       hashp->LORDER = DB_BYTE_ORDER;
       hashp->LORDER = DB_BYTE_ORDER;
        hashp->BSIZE = DEF_BUCKET_SIZE;
        hashp->BSIZE = DEF_BUCKET_SIZE;
        hashp->BSHIFT = DEF_BUCKET_SHIFT;
        hashp->BSHIFT = DEF_BUCKET_SHIFT;
        hashp->SGSIZE = DEF_SEGSIZE;
        hashp->SGSIZE = DEF_SEGSIZE;
        hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
        hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
        hashp->DSIZE = DEF_DIRSIZE;
        hashp->DSIZE = DEF_DIRSIZE;
        hashp->FFACTOR = DEF_FFACTOR;
        hashp->FFACTOR = DEF_FFACTOR;
        hashp->hash = __default_hash;
        hashp->hash = __default_hash;
        memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
        memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
        memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
        memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
 
 
        /* Fix bucket size to be optimal for file system */
        /* Fix bucket size to be optimal for file system */
        if (file != NULL) {
        if (file != NULL) {
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
                if (stat64(file, &statbuf))
                if (stat64(file, &statbuf))
#else
#else
                if (stat(file, &statbuf))
                if (stat(file, &statbuf))
#endif
#endif
                        return (NULL);
                        return (NULL);
                hashp->BSIZE = statbuf.st_blksize;
                hashp->BSIZE = statbuf.st_blksize;
                hashp->BSHIFT = __log2(hashp->BSIZE);
                hashp->BSHIFT = __log2(hashp->BSIZE);
        }
        }
 
 
        if (info) {
        if (info) {
                if (info->bsize) {
                if (info->bsize) {
                        /* Round pagesize up to power of 2 */
                        /* Round pagesize up to power of 2 */
                        hashp->BSHIFT = __log2(info->bsize);
                        hashp->BSHIFT = __log2(info->bsize);
                        hashp->BSIZE = 1 << hashp->BSHIFT;
                        hashp->BSIZE = 1 << hashp->BSHIFT;
                        if (hashp->BSIZE > MAX_BSIZE) {
                        if (hashp->BSIZE > MAX_BSIZE) {
                                errno = EINVAL;
                                errno = EINVAL;
                                return (NULL);
                                return (NULL);
                        }
                        }
                }
                }
                if (info->ffactor)
                if (info->ffactor)
                        hashp->FFACTOR = info->ffactor;
                        hashp->FFACTOR = info->ffactor;
                if (info->hash)
                if (info->hash)
                        hashp->hash = info->hash;
                        hashp->hash = info->hash;
                if (info->nelem)
                if (info->nelem)
                        nelem = info->nelem;
                        nelem = info->nelem;
                if (info->lorder) {
                if (info->lorder) {
                       if (info->lorder != DB_BIG_ENDIAN &&
                       if (info->lorder != DB_BIG_ENDIAN &&
                           info->lorder != DB_LITTLE_ENDIAN) {
                           info->lorder != DB_LITTLE_ENDIAN) {
                                errno = EINVAL;
                                errno = EINVAL;
                                return (NULL);
                                return (NULL);
                        }
                        }
                        hashp->LORDER = info->lorder;
                        hashp->LORDER = info->lorder;
                }
                }
        }
        }
        /* init_htab should destroy the table and set errno if it fails */
        /* init_htab should destroy the table and set errno if it fails */
        if (init_htab(hashp, nelem))
        if (init_htab(hashp, nelem))
                return (NULL);
                return (NULL);
        else
        else
                return (hashp);
                return (hashp);
}
}
/*
/*
 * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
 * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
 * the table and set errno, so we just pass the error information along.
 * the table and set errno, so we just pass the error information along.
 *
 *
 * Returns 0 on No Error
 * Returns 0 on No Error
 */
 */
static int
static int
init_htab(hashp, nelem)
init_htab(hashp, nelem)
        HTAB *hashp;
        HTAB *hashp;
        int nelem;
        int nelem;
{
{
        int nbuckets, nsegs;
        int nbuckets, nsegs;
        int l2;
        int l2;
 
 
        /*
        /*
         * Divide number of elements by the fill factor and determine a
         * Divide number of elements by the fill factor and determine a
         * desired number of buckets.  Allocate space for the next greater
         * desired number of buckets.  Allocate space for the next greater
         * power of two number of buckets.
         * power of two number of buckets.
         */
         */
        nelem = (nelem - 1) / hashp->FFACTOR + 1;
        nelem = (nelem - 1) / hashp->FFACTOR + 1;
 
 
        l2 = __log2(MAX(nelem, 2));
        l2 = __log2(MAX(nelem, 2));
        nbuckets = 1 << l2;
        nbuckets = 1 << l2;
 
 
        hashp->SPARES[l2] = l2 + 1;
        hashp->SPARES[l2] = l2 + 1;
        hashp->SPARES[l2 + 1] = l2 + 1;
        hashp->SPARES[l2 + 1] = l2 + 1;
        hashp->OVFL_POINT = l2;
        hashp->OVFL_POINT = l2;
        hashp->LAST_FREED = 2;
        hashp->LAST_FREED = 2;
 
 
        /* First bitmap page is at: splitpoint l2 page offset 1 */
        /* First bitmap page is at: splitpoint l2 page offset 1 */
        if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
        if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
                return (-1);
                return (-1);
 
 
        hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
        hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
        hashp->HIGH_MASK = (nbuckets << 1) - 1;
        hashp->HIGH_MASK = (nbuckets << 1) - 1;
        hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
        hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
            hashp->BSHIFT) + 1;
            hashp->BSHIFT) + 1;
 
 
        nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
        nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
        nsegs = 1 << __log2(nsegs);
        nsegs = 1 << __log2(nsegs);
 
 
        if (nsegs > hashp->DSIZE)
        if (nsegs > hashp->DSIZE)
                hashp->DSIZE = nsegs;
                hashp->DSIZE = nsegs;
        return (alloc_segs(hashp, nsegs));
        return (alloc_segs(hashp, nsegs));
}
}
 
 
/********************** DESTROY/CLOSE ROUTINES ************************/
/********************** DESTROY/CLOSE ROUTINES ************************/
 
 
/*
/*
 * Flushes any changes to the file if necessary and destroys the hashp
 * Flushes any changes to the file if necessary and destroys the hashp
 * structure, freeing all allocated space.
 * structure, freeing all allocated space.
 */
 */
static int
static int
hdestroy(hashp)
hdestroy(hashp)
        HTAB *hashp;
        HTAB *hashp;
{
{
        int i, save_errno;
        int i, save_errno;
 
 
        save_errno = 0;
        save_errno = 0;
 
 
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
        (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
        (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
            hash_accesses, hash_collisions);
            hash_accesses, hash_collisions);
        (void)fprintf(stderr, "hdestroy: expansions %ld\n",
        (void)fprintf(stderr, "hdestroy: expansions %ld\n",
            hash_expansions);
            hash_expansions);
        (void)fprintf(stderr, "hdestroy: overflows %ld\n",
        (void)fprintf(stderr, "hdestroy: overflows %ld\n",
            hash_overflows);
            hash_overflows);
        (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
        (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
            hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
            hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
 
 
        for (i = 0; i < NCACHED; i++)
        for (i = 0; i < NCACHED; i++)
                (void)fprintf(stderr,
                (void)fprintf(stderr,
                    "spares[%d] = %d\n", i, hashp->SPARES[i]);
                    "spares[%d] = %d\n", i, hashp->SPARES[i]);
#endif
#endif
        /*
        /*
         * Call on buffer manager to free buffers, and if required,
         * Call on buffer manager to free buffers, and if required,
         * write them to disk.
         * write them to disk.
         */
         */
        if (__buf_free(hashp, 1, hashp->save_file))
        if (__buf_free(hashp, 1, hashp->save_file))
                save_errno = errno;
                save_errno = errno;
        if (hashp->dir) {
        if (hashp->dir) {
                free(*hashp->dir);      /* Free initial segments */
                free(*hashp->dir);      /* Free initial segments */
                /* Free extra segments */
                /* Free extra segments */
                while (hashp->exsegs--)
                while (hashp->exsegs--)
                        free(hashp->dir[--hashp->nsegs]);
                        free(hashp->dir[--hashp->nsegs]);
                free(hashp->dir);
                free(hashp->dir);
        }
        }
        if (flush_meta(hashp) && !save_errno)
        if (flush_meta(hashp) && !save_errno)
                save_errno = errno;
                save_errno = errno;
        /* Free Bigmaps */
        /* Free Bigmaps */
        for (i = 0; i < hashp->nmaps; i++)
        for (i = 0; i < hashp->nmaps; i++)
                if (hashp->mapp[i])
                if (hashp->mapp[i])
                        free(hashp->mapp[i]);
                        free(hashp->mapp[i]);
 
 
        if (hashp->fp != -1)
        if (hashp->fp != -1)
                (void)close(hashp->fp);
                (void)close(hashp->fp);
 
 
        free(hashp);
        free(hashp);
 
 
        if (save_errno) {
        if (save_errno) {
                errno = save_errno;
                errno = save_errno;
                return (ERROR);
                return (ERROR);
        }
        }
        return (SUCCESS);
        return (SUCCESS);
}
}
/*
/*
 * Write modified pages to disk
 * Write modified pages to disk
 *
 *
 * Returns:
 * Returns:
 *       0 == OK
 *       0 == OK
 *      -1 ERROR
 *      -1 ERROR
 */
 */
static int
static int
hash_sync(dbp, flags)
hash_sync(dbp, flags)
        const DB *dbp;
        const DB *dbp;
        __uint32_t flags;
        __uint32_t flags;
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
        if (flags != 0) {
        if (flags != 0) {
                errno = EINVAL;
                errno = EINVAL;
                return (ERROR);
                return (ERROR);
        }
        }
 
 
        if (!dbp)
        if (!dbp)
                return (ERROR);
                return (ERROR);
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (!hashp->save_file)
        if (!hashp->save_file)
                return (0);
                return (0);
        if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
        if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
                return (ERROR);
                return (ERROR);
        hashp->new_file = 0;
        hashp->new_file = 0;
        return (0);
        return (0);
}
}
 
 
/*
/*
 * Returns:
 * Returns:
 *       0 == OK
 *       0 == OK
 *      -1 indicates that errno should be set
 *      -1 indicates that errno should be set
 */
 */
static int
static int
flush_meta(hashp)
flush_meta(hashp)
        HTAB *hashp;
        HTAB *hashp;
{
{
        HASHHDR *whdrp;
        HASHHDR *whdrp;
#if (BYTE_ORDER == LITTLE_ENDIAN)
#if (BYTE_ORDER == LITTLE_ENDIAN)
        HASHHDR whdr;
        HASHHDR whdr;
#endif
#endif
        int fp, i, wsize;
        int fp, i, wsize;
 
 
        if (!hashp->save_file)
        if (!hashp->save_file)
                return (0);
                return (0);
        hashp->MAGIC = HASHMAGIC;
        hashp->MAGIC = HASHMAGIC;
        hashp->HASH_VERSION = HASHVERSION;
        hashp->HASH_VERSION = HASHVERSION;
        hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
        hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
 
 
        fp = hashp->fp;
        fp = hashp->fp;
        whdrp = &hashp->hdr;
        whdrp = &hashp->hdr;
#if (BYTE_ORDER == LITTLE_ENDIAN)
#if (BYTE_ORDER == LITTLE_ENDIAN)
        whdrp = &whdr;
        whdrp = &whdr;
        swap_header_copy(&hashp->hdr, whdrp);
        swap_header_copy(&hashp->hdr, whdrp);
#endif
#endif
        if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
        if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
            ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
            ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
                return (-1);
                return (-1);
        else
        else
                if (wsize != sizeof(HASHHDR)) {
                if (wsize != sizeof(HASHHDR)) {
                        errno = EFTYPE;
                        errno = EFTYPE;
                        hashp->error = errno;
                        hashp->error = errno;
                        return (-1);
                        return (-1);
                }
                }
        for (i = 0; i < NCACHED; i++)
        for (i = 0; i < NCACHED; i++)
                if (hashp->mapp[i])
                if (hashp->mapp[i])
                        if (__put_page(hashp, (char *)hashp->mapp[i],
                        if (__put_page(hashp, (char *)hashp->mapp[i],
                                hashp->BITMAPS[i], 0, 1))
                                hashp->BITMAPS[i], 0, 1))
                                return (-1);
                                return (-1);
        return (0);
        return (0);
}
}
 
 
/*******************************SEARCH ROUTINES *****************************/
/*******************************SEARCH ROUTINES *****************************/
/*
/*
 * All the access routines return
 * All the access routines return
 *
 *
 * Returns:
 * Returns:
 *       0 on SUCCESS
 *       0 on SUCCESS
 *       1 to indicate an external ERROR (i.e. key not found, etc)
 *       1 to indicate an external ERROR (i.e. key not found, etc)
 *      -1 to indicate an internal ERROR (i.e. out of memory, etc)
 *      -1 to indicate an internal ERROR (i.e. out of memory, etc)
 */
 */
static int
static int
hash_get(dbp, key, data, flag)
hash_get(dbp, key, data, flag)
        const DB *dbp;
        const DB *dbp;
        const DBT *key;
        const DBT *key;
        DBT *data;
        DBT *data;
        __uint32_t flag;
        __uint32_t flag;
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (flag) {
        if (flag) {
                hashp->error = errno = EINVAL;
                hashp->error = errno = EINVAL;
                return (ERROR);
                return (ERROR);
        }
        }
        return (hash_access(hashp, HASH_GET, (DBT *)key, data));
        return (hash_access(hashp, HASH_GET, (DBT *)key, data));
}
}
 
 
static int
static int
hash_put(dbp, key, data, flag)
hash_put(dbp, key, data, flag)
        const DB *dbp;
        const DB *dbp;
        DBT *key;
        DBT *key;
        const DBT *data;
        const DBT *data;
        __uint32_t flag;
        __uint32_t flag;
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (flag && flag != R_NOOVERWRITE) {
        if (flag && flag != R_NOOVERWRITE) {
                hashp->error = EINVAL;
                hashp->error = EINVAL;
                errno = EINVAL;
                errno = EINVAL;
                return (ERROR);
                return (ERROR);
        }
        }
        if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
        if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
                hashp->error = errno = EPERM;
                hashp->error = errno = EPERM;
                return (ERROR);
                return (ERROR);
        }
        }
        return (hash_access(hashp, flag == R_NOOVERWRITE ?
        return (hash_access(hashp, flag == R_NOOVERWRITE ?
            HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
            HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
}
}
 
 
static int
static int
hash_delete(dbp, key, flag)
hash_delete(dbp, key, flag)
        const DB *dbp;
        const DB *dbp;
        const DBT *key;
        const DBT *key;
        __uint32_t flag;                /* Ignored */
        __uint32_t flag;                /* Ignored */
{
{
        HTAB *hashp;
        HTAB *hashp;
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (flag && flag != R_CURSOR) {
        if (flag && flag != R_CURSOR) {
                hashp->error = errno = EINVAL;
                hashp->error = errno = EINVAL;
                return (ERROR);
                return (ERROR);
        }
        }
        if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
        if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
                hashp->error = errno = EPERM;
                hashp->error = errno = EPERM;
                return (ERROR);
                return (ERROR);
        }
        }
        return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
        return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
}
}
 
 
/*
/*
 * Assume that hashp has been set in wrapper routine.
 * Assume that hashp has been set in wrapper routine.
 */
 */
static int
static int
hash_access(hashp, action, key, val)
hash_access(hashp, action, key, val)
        HTAB *hashp;
        HTAB *hashp;
        ACTION action;
        ACTION action;
        DBT *key, *val;
        DBT *key, *val;
{
{
        BUFHEAD *rbufp;
        BUFHEAD *rbufp;
        BUFHEAD *bufp, *save_bufp;
        BUFHEAD *bufp, *save_bufp;
        __uint16_t *bp;
        __uint16_t *bp;
        int n, ndx, off, size;
        int n, ndx, off, size;
        char *kp;
        char *kp;
        __uint16_t pageno;
        __uint16_t pageno;
 
 
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
        hash_accesses++;
        hash_accesses++;
#endif
#endif
 
 
        off = hashp->BSIZE;
        off = hashp->BSIZE;
        size = key->size;
        size = key->size;
        kp = (char *)key->data;
        kp = (char *)key->data;
        rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
        rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
        if (!rbufp)
        if (!rbufp)
                return (ERROR);
                return (ERROR);
        save_bufp = rbufp;
        save_bufp = rbufp;
 
 
        /* Pin the bucket chain */
        /* Pin the bucket chain */
        rbufp->flags |= BUF_PIN;
        rbufp->flags |= BUF_PIN;
        for (bp = (__uint16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
        for (bp = (__uint16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
                if (bp[1] >= REAL_KEY) {
                if (bp[1] >= REAL_KEY) {
                        /* Real key/data pair */
                        /* Real key/data pair */
                        if (size == off - *bp &&
                        if (size == off - *bp &&
                            memcmp(kp, rbufp->page + *bp, size) == 0)
                            memcmp(kp, rbufp->page + *bp, size) == 0)
                                goto found;
                                goto found;
                        off = bp[1];
                        off = bp[1];
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
                        hash_collisions++;
                        hash_collisions++;
#endif
#endif
                        bp += 2;
                        bp += 2;
                        ndx += 2;
                        ndx += 2;
                } else if (bp[1] == OVFLPAGE) {
                } else if (bp[1] == OVFLPAGE) {
                        rbufp = __get_buf(hashp, *bp, rbufp, 0);
                        rbufp = __get_buf(hashp, *bp, rbufp, 0);
                        if (!rbufp) {
                        if (!rbufp) {
                                save_bufp->flags &= ~BUF_PIN;
                                save_bufp->flags &= ~BUF_PIN;
                                return (ERROR);
                                return (ERROR);
                        }
                        }
                        /* FOR LOOP INIT */
                        /* FOR LOOP INIT */
                        bp = (__uint16_t *)rbufp->page;
                        bp = (__uint16_t *)rbufp->page;
                        n = *bp++;
                        n = *bp++;
                        ndx = 1;
                        ndx = 1;
                        off = hashp->BSIZE;
                        off = hashp->BSIZE;
                } else if (bp[1] < REAL_KEY) {
                } else if (bp[1] < REAL_KEY) {
                        if ((ndx =
                        if ((ndx =
                            __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
                            __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
                                goto found;
                                goto found;
                        if (ndx == -2) {
                        if (ndx == -2) {
                                bufp = rbufp;
                                bufp = rbufp;
                                if (!(pageno =
                                if (!(pageno =
                                    __find_last_page(hashp, &bufp))) {
                                    __find_last_page(hashp, &bufp))) {
                                        ndx = 0;
                                        ndx = 0;
                                        rbufp = bufp;
                                        rbufp = bufp;
                                        break;  /* FOR */
                                        break;  /* FOR */
                                }
                                }
                                rbufp = __get_buf(hashp, pageno, bufp, 0);
                                rbufp = __get_buf(hashp, pageno, bufp, 0);
                                if (!rbufp) {
                                if (!rbufp) {
                                        save_bufp->flags &= ~BUF_PIN;
                                        save_bufp->flags &= ~BUF_PIN;
                                        return (ERROR);
                                        return (ERROR);
                                }
                                }
                                /* FOR LOOP INIT */
                                /* FOR LOOP INIT */
                                bp = (__uint16_t *)rbufp->page;
                                bp = (__uint16_t *)rbufp->page;
                                n = *bp++;
                                n = *bp++;
                                ndx = 1;
                                ndx = 1;
                                off = hashp->BSIZE;
                                off = hashp->BSIZE;
                        } else {
                        } else {
                                save_bufp->flags &= ~BUF_PIN;
                                save_bufp->flags &= ~BUF_PIN;
                                return (ERROR);
                                return (ERROR);
                        }
                        }
                }
                }
 
 
        /* Not found */
        /* Not found */
        switch (action) {
        switch (action) {
        case HASH_PUT:
        case HASH_PUT:
        case HASH_PUTNEW:
        case HASH_PUTNEW:
                if (__addel(hashp, rbufp, key, val)) {
                if (__addel(hashp, rbufp, key, val)) {
                        save_bufp->flags &= ~BUF_PIN;
                        save_bufp->flags &= ~BUF_PIN;
                        return (ERROR);
                        return (ERROR);
                } else {
                } else {
                        save_bufp->flags &= ~BUF_PIN;
                        save_bufp->flags &= ~BUF_PIN;
                        return (SUCCESS);
                        return (SUCCESS);
                }
                }
        case HASH_GET:
        case HASH_GET:
        case HASH_DELETE:
        case HASH_DELETE:
        default:
        default:
                save_bufp->flags &= ~BUF_PIN;
                save_bufp->flags &= ~BUF_PIN;
                return (ABNORMAL);
                return (ABNORMAL);
        }
        }
 
 
found:
found:
        switch (action) {
        switch (action) {
        case HASH_PUTNEW:
        case HASH_PUTNEW:
                save_bufp->flags &= ~BUF_PIN;
                save_bufp->flags &= ~BUF_PIN;
                return (ABNORMAL);
                return (ABNORMAL);
        case HASH_GET:
        case HASH_GET:
                bp = (__uint16_t *)rbufp->page;
                bp = (__uint16_t *)rbufp->page;
                if (bp[ndx + 1] < REAL_KEY) {
                if (bp[ndx + 1] < REAL_KEY) {
                        if (__big_return(hashp, rbufp, ndx, val, 0))
                        if (__big_return(hashp, rbufp, ndx, val, 0))
                                return (ERROR);
                                return (ERROR);
                } else {
                } else {
                        val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
                        val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
                        val->size = bp[ndx] - bp[ndx + 1];
                        val->size = bp[ndx] - bp[ndx + 1];
                }
                }
                break;
                break;
        case HASH_PUT:
        case HASH_PUT:
                if ((__delpair(hashp, rbufp, ndx)) ||
                if ((__delpair(hashp, rbufp, ndx)) ||
                    (__addel(hashp, rbufp, key, val))) {
                    (__addel(hashp, rbufp, key, val))) {
                        save_bufp->flags &= ~BUF_PIN;
                        save_bufp->flags &= ~BUF_PIN;
                        return (ERROR);
                        return (ERROR);
                }
                }
                break;
                break;
        case HASH_DELETE:
        case HASH_DELETE:
                if (__delpair(hashp, rbufp, ndx))
                if (__delpair(hashp, rbufp, ndx))
                        return (ERROR);
                        return (ERROR);
                break;
                break;
        default:
        default:
                abort();
                abort();
        }
        }
        save_bufp->flags &= ~BUF_PIN;
        save_bufp->flags &= ~BUF_PIN;
        return (SUCCESS);
        return (SUCCESS);
}
}
 
 
static int
static int
hash_seq(dbp, key, data, flag)
hash_seq(dbp, key, data, flag)
        const DB *dbp;
        const DB *dbp;
        DBT *key, *data;
        DBT *key, *data;
        __uint32_t flag;
        __uint32_t flag;
{
{
        __uint32_t bucket;
        __uint32_t bucket;
        BUFHEAD *bufp;
        BUFHEAD *bufp;
        HTAB *hashp;
        HTAB *hashp;
        __uint16_t *bp, ndx;
        __uint16_t *bp, ndx;
 
 
        hashp = (HTAB *)dbp->internal;
        hashp = (HTAB *)dbp->internal;
        if (flag && flag != R_FIRST && flag != R_NEXT) {
        if (flag && flag != R_FIRST && flag != R_NEXT) {
                hashp->error = errno = EINVAL;
                hashp->error = errno = EINVAL;
                return (ERROR);
                return (ERROR);
        }
        }
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
        hash_accesses++;
        hash_accesses++;
#endif
#endif
        if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
        if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
                hashp->cbucket = 0;
                hashp->cbucket = 0;
                hashp->cndx = 1;
                hashp->cndx = 1;
                hashp->cpage = NULL;
                hashp->cpage = NULL;
        }
        }
 
 
        for (bp = NULL; !bp || !bp[0]; ) {
        for (bp = NULL; !bp || !bp[0]; ) {
                if (!(bufp = hashp->cpage)) {
                if (!(bufp = hashp->cpage)) {
                        for (bucket = hashp->cbucket;
                        for (bucket = hashp->cbucket;
                            bucket <= hashp->MAX_BUCKET;
                            bucket <= hashp->MAX_BUCKET;
                            bucket++, hashp->cndx = 1) {
                            bucket++, hashp->cndx = 1) {
                                bufp = __get_buf(hashp, bucket, NULL, 0);
                                bufp = __get_buf(hashp, bucket, NULL, 0);
                                if (!bufp)
                                if (!bufp)
                                        return (ERROR);
                                        return (ERROR);
                                hashp->cpage = bufp;
                                hashp->cpage = bufp;
                                bp = (__uint16_t *)bufp->page;
                                bp = (__uint16_t *)bufp->page;
                                if (bp[0])
                                if (bp[0])
                                        break;
                                        break;
                        }
                        }
                        hashp->cbucket = bucket;
                        hashp->cbucket = bucket;
                        if (hashp->cbucket > hashp->MAX_BUCKET) {
                        if (hashp->cbucket > hashp->MAX_BUCKET) {
                                hashp->cbucket = -1;
                                hashp->cbucket = -1;
                                return (ABNORMAL);
                                return (ABNORMAL);
                        }
                        }
                } else
                } else
                        bp = (__uint16_t *)hashp->cpage->page;
                        bp = (__uint16_t *)hashp->cpage->page;
 
 
#ifdef DEBUG
#ifdef DEBUG
                assert(bp);
                assert(bp);
                assert(bufp);
                assert(bufp);
#endif
#endif
                while (bp[hashp->cndx + 1] == OVFLPAGE) {
                while (bp[hashp->cndx + 1] == OVFLPAGE) {
                        bufp = hashp->cpage =
                        bufp = hashp->cpage =
                            __get_buf(hashp, bp[hashp->cndx], bufp, 0);
                            __get_buf(hashp, bp[hashp->cndx], bufp, 0);
                        if (!bufp)
                        if (!bufp)
                                return (ERROR);
                                return (ERROR);
                        bp = (__uint16_t *)(bufp->page);
                        bp = (__uint16_t *)(bufp->page);
                        hashp->cndx = 1;
                        hashp->cndx = 1;
                }
                }
                if (!bp[0]) {
                if (!bp[0]) {
                        hashp->cpage = NULL;
                        hashp->cpage = NULL;
                        ++hashp->cbucket;
                        ++hashp->cbucket;
                }
                }
        }
        }
        ndx = hashp->cndx;
        ndx = hashp->cndx;
        if (bp[ndx + 1] < REAL_KEY) {
        if (bp[ndx + 1] < REAL_KEY) {
                if (__big_keydata(hashp, bufp, key, data, 1))
                if (__big_keydata(hashp, bufp, key, data, 1))
                        return (ERROR);
                        return (ERROR);
        } else {
        } else {
                key->data = (u_char *)hashp->cpage->page + bp[ndx];
                key->data = (u_char *)hashp->cpage->page + bp[ndx];
                key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
                key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
                data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
                data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
                data->size = bp[ndx] - bp[ndx + 1];
                data->size = bp[ndx] - bp[ndx + 1];
                ndx += 2;
                ndx += 2;
                if (ndx > bp[0]) {
                if (ndx > bp[0]) {
                        hashp->cpage = NULL;
                        hashp->cpage = NULL;
                        hashp->cbucket++;
                        hashp->cbucket++;
                        hashp->cndx = 1;
                        hashp->cndx = 1;
                } else
                } else
                        hashp->cndx = ndx;
                        hashp->cndx = ndx;
        }
        }
        return (SUCCESS);
        return (SUCCESS);
}
}
 
 
/********************************* UTILITIES ************************/
/********************************* UTILITIES ************************/
 
 
/*
/*
 * Returns:
 * Returns:
 *       0 ==> OK
 *       0 ==> OK
 *      -1 ==> Error
 *      -1 ==> Error
 */
 */
extern int
extern int
__expand_table(hashp)
__expand_table(hashp)
        HTAB *hashp;
        HTAB *hashp;
{
{
        __uint32_t old_bucket, new_bucket;
        __uint32_t old_bucket, new_bucket;
        int dirsize, new_segnum, spare_ndx;
        int dirsize, new_segnum, spare_ndx;
 
 
#ifdef HASH_STATISTICS
#ifdef HASH_STATISTICS
        hash_expansions++;
        hash_expansions++;
#endif
#endif
        new_bucket = ++hashp->MAX_BUCKET;
        new_bucket = ++hashp->MAX_BUCKET;
        old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
        old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
 
 
        new_segnum = new_bucket >> hashp->SSHIFT;
        new_segnum = new_bucket >> hashp->SSHIFT;
 
 
        /* Check if we need a new segment */
        /* Check if we need a new segment */
        if (new_segnum >= hashp->nsegs) {
        if (new_segnum >= hashp->nsegs) {
                /* Check if we need to expand directory */
                /* Check if we need to expand directory */
                if (new_segnum >= hashp->DSIZE) {
                if (new_segnum >= hashp->DSIZE) {
                        /* Reallocate directory */
                        /* Reallocate directory */
                        dirsize = hashp->DSIZE * sizeof(SEGMENT *);
                        dirsize = hashp->DSIZE * sizeof(SEGMENT *);
                        if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
                        if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
                                return (-1);
                                return (-1);
                        hashp->DSIZE = dirsize << 1;
                        hashp->DSIZE = dirsize << 1;
                }
                }
                if ((hashp->dir[new_segnum] =
                if ((hashp->dir[new_segnum] =
                    (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
                    (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
                        return (-1);
                        return (-1);
                hashp->exsegs++;
                hashp->exsegs++;
                hashp->nsegs++;
                hashp->nsegs++;
        }
        }
        /*
        /*
         * If the split point is increasing (MAX_BUCKET's log base 2
         * If the split point is increasing (MAX_BUCKET's log base 2
         * * increases), we need to copy the current contents of the spare
         * * increases), we need to copy the current contents of the spare
         * split bucket to the next bucket.
         * split bucket to the next bucket.
         */
         */
        spare_ndx = __log2(hashp->MAX_BUCKET + 1);
        spare_ndx = __log2(hashp->MAX_BUCKET + 1);
        if (spare_ndx > hashp->OVFL_POINT) {
        if (spare_ndx > hashp->OVFL_POINT) {
                hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
                hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
                hashp->OVFL_POINT = spare_ndx;
                hashp->OVFL_POINT = spare_ndx;
        }
        }
 
 
        if (new_bucket > hashp->HIGH_MASK) {
        if (new_bucket > hashp->HIGH_MASK) {
                /* Starting a new doubling */
                /* Starting a new doubling */
                hashp->LOW_MASK = hashp->HIGH_MASK;
                hashp->LOW_MASK = hashp->HIGH_MASK;
                hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
                hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
        }
        }
        /* Relocate records to the new bucket */
        /* Relocate records to the new bucket */
        return (__split_page(hashp, old_bucket, new_bucket));
        return (__split_page(hashp, old_bucket, new_bucket));
}
}
 
 
/*
/*
 * If realloc guarantees that the pointer is not destroyed if the realloc
 * If realloc guarantees that the pointer is not destroyed if the realloc
 * fails, then this routine can go away.
 * fails, then this routine can go away.
 */
 */
static void *
static void *
hash_realloc(p_ptr, oldsize, newsize)
hash_realloc(p_ptr, oldsize, newsize)
        SEGMENT **p_ptr;
        SEGMENT **p_ptr;
        int oldsize, newsize;
        int oldsize, newsize;
{
{
        void *p;
        void *p;
 
 
        if ( (p = malloc(newsize)) ) {
        if ( (p = malloc(newsize)) ) {
                memmove(p, *p_ptr, oldsize);
                memmove(p, *p_ptr, oldsize);
                memset((char *)p + oldsize, 0, newsize - oldsize);
                memset((char *)p + oldsize, 0, newsize - oldsize);
                free(*p_ptr);
                free(*p_ptr);
                *p_ptr = p;
                *p_ptr = p;
        }
        }
        return (p);
        return (p);
}
}
 
 
extern __uint32_t
extern __uint32_t
__call_hash(hashp, k, len)
__call_hash(hashp, k, len)
        HTAB *hashp;
        HTAB *hashp;
        char *k;
        char *k;
        int len;
        int len;
{
{
        int n, bucket;
        int n, bucket;
 
 
        n = hashp->hash(k, len);
        n = hashp->hash(k, len);
        bucket = n & hashp->HIGH_MASK;
        bucket = n & hashp->HIGH_MASK;
        if (bucket > hashp->MAX_BUCKET)
        if (bucket > hashp->MAX_BUCKET)
                bucket = bucket & hashp->LOW_MASK;
                bucket = bucket & hashp->LOW_MASK;
        return (bucket);
        return (bucket);
}
}
 
 
/*
/*
 * Allocate segment table.  On error, destroy the table and set errno.
 * Allocate segment table.  On error, destroy the table and set errno.
 *
 *
 * Returns 0 on success
 * Returns 0 on success
 */
 */
static int
static int
alloc_segs(hashp, nsegs)
alloc_segs(hashp, nsegs)
        HTAB *hashp;
        HTAB *hashp;
        int nsegs;
        int nsegs;
{
{
        int i;
        int i;
        SEGMENT store;
        SEGMENT store;
 
 
        int save_errno;
        int save_errno;
 
 
        if ((hashp->dir =
        if ((hashp->dir =
            (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
            (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
                save_errno = errno;
                save_errno = errno;
                (void)hdestroy(hashp);
                (void)hdestroy(hashp);
                errno = save_errno;
                errno = save_errno;
                return (-1);
                return (-1);
        }
        }
        /* Allocate segments */
        /* Allocate segments */
        if ((store =
        if ((store =
            (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
            (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
                save_errno = errno;
                save_errno = errno;
                (void)hdestroy(hashp);
                (void)hdestroy(hashp);
                errno = save_errno;
                errno = save_errno;
                return (-1);
                return (-1);
        }
        }
        for (i = 0; i < nsegs; i++, hashp->nsegs++)
        for (i = 0; i < nsegs; i++, hashp->nsegs++)
                hashp->dir[i] = &store[i << hashp->SSHIFT];
                hashp->dir[i] = &store[i << hashp->SSHIFT];
        return (0);
        return (0);
}
}
 
 
#if (BYTE_ORDER == LITTLE_ENDIAN)
#if (BYTE_ORDER == LITTLE_ENDIAN)
/*
/*
 * Hashp->hdr needs to be byteswapped.
 * Hashp->hdr needs to be byteswapped.
 */
 */
static void
static void
swap_header_copy(srcp, destp)
swap_header_copy(srcp, destp)
        HASHHDR *srcp, *destp;
        HASHHDR *srcp, *destp;
{
{
        int i;
        int i;
 
 
        P_32_COPY(srcp->magic, destp->magic);
        P_32_COPY(srcp->magic, destp->magic);
        P_32_COPY(srcp->version, destp->version);
        P_32_COPY(srcp->version, destp->version);
        P_32_COPY(srcp->lorder, destp->lorder);
        P_32_COPY(srcp->lorder, destp->lorder);
        P_32_COPY(srcp->bsize, destp->bsize);
        P_32_COPY(srcp->bsize, destp->bsize);
        P_32_COPY(srcp->bshift, destp->bshift);
        P_32_COPY(srcp->bshift, destp->bshift);
        P_32_COPY(srcp->dsize, destp->dsize);
        P_32_COPY(srcp->dsize, destp->dsize);
        P_32_COPY(srcp->ssize, destp->ssize);
        P_32_COPY(srcp->ssize, destp->ssize);
        P_32_COPY(srcp->sshift, destp->sshift);
        P_32_COPY(srcp->sshift, destp->sshift);
        P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
        P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
        P_32_COPY(srcp->last_freed, destp->last_freed);
        P_32_COPY(srcp->last_freed, destp->last_freed);
        P_32_COPY(srcp->max_bucket, destp->max_bucket);
        P_32_COPY(srcp->max_bucket, destp->max_bucket);
        P_32_COPY(srcp->high_mask, destp->high_mask);
        P_32_COPY(srcp->high_mask, destp->high_mask);
        P_32_COPY(srcp->low_mask, destp->low_mask);
        P_32_COPY(srcp->low_mask, destp->low_mask);
        P_32_COPY(srcp->ffactor, destp->ffactor);
        P_32_COPY(srcp->ffactor, destp->ffactor);
        P_32_COPY(srcp->nkeys, destp->nkeys);
        P_32_COPY(srcp->nkeys, destp->nkeys);
        P_32_COPY(srcp->hdrpages, destp->hdrpages);
        P_32_COPY(srcp->hdrpages, destp->hdrpages);
        P_32_COPY(srcp->h_charkey, destp->h_charkey);
        P_32_COPY(srcp->h_charkey, destp->h_charkey);
        for (i = 0; i < NCACHED; i++) {
        for (i = 0; i < NCACHED; i++) {
                P_32_COPY(srcp->spares[i], destp->spares[i]);
                P_32_COPY(srcp->spares[i], destp->spares[i]);
                P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
                P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
        }
        }
}
}
 
 
static void
static void
swap_header(hashp)
swap_header(hashp)
        HTAB *hashp;
        HTAB *hashp;
{
{
        HASHHDR *hdrp;
        HASHHDR *hdrp;
        int i;
        int i;
 
 
        hdrp = &hashp->hdr;
        hdrp = &hashp->hdr;
 
 
        M_32_SWAP(hdrp->magic);
        M_32_SWAP(hdrp->magic);
        M_32_SWAP(hdrp->version);
        M_32_SWAP(hdrp->version);
        M_32_SWAP(hdrp->lorder);
        M_32_SWAP(hdrp->lorder);
        M_32_SWAP(hdrp->bsize);
        M_32_SWAP(hdrp->bsize);
        M_32_SWAP(hdrp->bshift);
        M_32_SWAP(hdrp->bshift);
        M_32_SWAP(hdrp->dsize);
        M_32_SWAP(hdrp->dsize);
        M_32_SWAP(hdrp->ssize);
        M_32_SWAP(hdrp->ssize);
        M_32_SWAP(hdrp->sshift);
        M_32_SWAP(hdrp->sshift);
        M_32_SWAP(hdrp->ovfl_point);
        M_32_SWAP(hdrp->ovfl_point);
        M_32_SWAP(hdrp->last_freed);
        M_32_SWAP(hdrp->last_freed);
        M_32_SWAP(hdrp->max_bucket);
        M_32_SWAP(hdrp->max_bucket);
        M_32_SWAP(hdrp->high_mask);
        M_32_SWAP(hdrp->high_mask);
        M_32_SWAP(hdrp->low_mask);
        M_32_SWAP(hdrp->low_mask);
        M_32_SWAP(hdrp->ffactor);
        M_32_SWAP(hdrp->ffactor);
        M_32_SWAP(hdrp->nkeys);
        M_32_SWAP(hdrp->nkeys);
        M_32_SWAP(hdrp->hdrpages);
        M_32_SWAP(hdrp->hdrpages);
        M_32_SWAP(hdrp->h_charkey);
        M_32_SWAP(hdrp->h_charkey);
        for (i = 0; i < NCACHED; i++) {
        for (i = 0; i < NCACHED; i++) {
                M_32_SWAP(hdrp->spares[i]);
                M_32_SWAP(hdrp->spares[i]);
                M_16_SWAP(hdrp->bitmaps[i]);
                M_16_SWAP(hdrp->bitmaps[i]);
        }
        }
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.