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