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