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