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