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