xref: /csrg-svn/lib/libc/db/hash/hash.c (revision 46562)
146366Sbostic /*-
246366Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346366Sbostic  * All rights reserved.
446366Sbostic  *
546366Sbostic  * This code is derived from software contributed to Berkeley by
646366Sbostic  * Margo Seltzer.
746366Sbostic  *
846366Sbostic  * %sccs.include.redist.c%
946366Sbostic  */
1046366Sbostic 
1146366Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46562Sbostic static char sccsid[] = "@(#)hash.c	5.10 (Berkeley) 02/22/91";
1346366Sbostic #endif /* LIBC_SCCS and not lint */
1446366Sbostic 
1546366Sbostic #include <sys/param.h>
1646366Sbostic #include <sys/stat.h>
17*46562Sbostic #include <fcntl.h>
1846366Sbostic #include <errno.h>
1946366Sbostic #include <assert.h>
2046366Sbostic #include <db.h>
21*46562Sbostic #include <stdio.h>
22*46562Sbostic #include <stdlib.h>
2346500Sbostic #include <unistd.h>
24*46562Sbostic #include <string.h>
2546366Sbostic #include "hash.h"
2646366Sbostic 
2746366Sbostic /* Fast arithmetic, relying on powers of 2, */
2846366Sbostic 
2946366Sbostic #define MOD(x,y)		((x) & ((y)-1))
3046366Sbostic #define RETURN_ERROR(ERR,LOC)	{ save_errno = ERR; goto LOC; }
3146366Sbostic 
3246366Sbostic /* Return values */
3346366Sbostic 
3446366Sbostic #define	SUCCESS	0
3546366Sbostic #define	ERROR	-1
3646366Sbostic #define	ABNORMAL 1
3746366Sbostic 
3846366Sbostic /* page.c */
3946366Sbostic extern int __get_page();
4046366Sbostic extern int __split_page();
4146366Sbostic extern int __addel();
4246366Sbostic extern int __delpair();
4346366Sbostic extern u_long *__init_bitmap();
4446366Sbostic 
4546366Sbostic /* big.c */
4646366Sbostic extern void __big_return();
4746366Sbostic extern int __big_keydata();
4846366Sbostic extern u_short __find_last_page();
4946366Sbostic 
5046366Sbostic /* buf.c */
5146366Sbostic extern BUFHEAD	*__get_buf();
5246366Sbostic extern void __buf_init();
5346366Sbostic extern int __buf_free();
5446366Sbostic 
5546366Sbostic /* hash function */
5646366Sbostic extern int (*default_hash)();
5746366Sbostic 
5846366Sbostic /* My externals */
5946366Sbostic extern int __expand_table();
6046366Sbostic extern int __call_hash();
6146366Sbostic 
6246366Sbostic /* Internal routines */
6346366Sbostic static HTAB *init_hash();
6446366Sbostic static int hash_access();
6546366Sbostic static int flush_meta();
6646366Sbostic static int alloc_segs();
6746366Sbostic static int init_htab();
6846366Sbostic static char *hash_realloc();
6946366Sbostic static int hdestroy();
7046366Sbostic static int hash_put();
7146366Sbostic static int hash_delete();
7246366Sbostic static int hash_get();
7346366Sbostic static int hash_sync();
7446366Sbostic static int hash_close();
7546366Sbostic static int hash_seq();
7646366Sbostic static void swap_header_copy();
7746366Sbostic static void swap_header();
7846366Sbostic 
7946366Sbostic /* Local data */
8046366Sbostic 
8146366Sbostic HTAB *hashp = NULL;
8246366Sbostic 
8346366Sbostic #ifdef HASH_STATISTICS
8446366Sbostic long hash_accesses, hash_collisions, hash_expansions, hash_overflows;
8546366Sbostic #endif
8646366Sbostic 
8746366Sbostic /************************** INTERFACE ROUTINES **********************/
8846366Sbostic /* OPEN/CLOSE */
8946366Sbostic 
9046366Sbostic extern	DB *
9146366Sbostic hash_open ( file, flags, mode, info )
9246534Sbostic const char	*file;
9346366Sbostic int	flags;
9446366Sbostic int	mode;
9546534Sbostic const HASHINFO	*info;		/* Special directives for create */
9646366Sbostic {
9746366Sbostic     int		buckets;
9846366Sbostic     int		bpages;
9946366Sbostic     int		hdrsize;
10046366Sbostic     int		i;
10146366Sbostic     int		new_table = 0;
10246366Sbostic     int		nkey;
10346366Sbostic     int		nsegs;
10446366Sbostic     int		save_errno;
10546366Sbostic     struct	stat statbuf;
10646366Sbostic     DB		*dbp;
10746366Sbostic 
10846366Sbostic 
10946366Sbostic     if ( !(hashp = (HTAB *) calloc( 1, sizeof(HTAB) ))) {
11046366Sbostic 	/* calloc should set errno */
11146366Sbostic 	return(0);
11246366Sbostic     }
11346457Sbostic     hashp->fp = -1;
11446366Sbostic     /*
11546366Sbostic 	Select flags relevant to us.
11646366Sbostic 	Even if user wants write only, we need to be able to read
11746366Sbostic 	the actual file, so we need to open it read/write.  But, the
11846366Sbostic 	field in the hashp structure needs to be accurate so that
11946366Sbostic 	we can check accesses.
12046366Sbostic     */
12146366Sbostic     flags = flags & (O_CREAT | O_EXCL | O_RDONLY | O_RDWR | O_TRUNC | O_WRONLY);
12246366Sbostic     hashp->flags = flags;
12346366Sbostic     if ( flags & O_WRONLY )  flags = (flags & ~O_WRONLY) | O_RDWR;
12446366Sbostic 
12546366Sbostic     if ( !file ||
12646366Sbostic 	 (flags & O_TRUNC) ||
12746366Sbostic 	 (stat ( file, &statbuf ) && (errno == ENOENT)) ) {
12846503Sbostic 	 if ( errno == ENOENT ) {
12946503Sbostic 	    errno = 0;		/* Just in case someone looks at errno */
13046503Sbostic 	 }
13146366Sbostic 	 new_table = 1;
13246366Sbostic     }
13346366Sbostic 
13446485Sbostic     if ( file ) {
13546485Sbostic 	if ((hashp->fp = open ( file, flags, mode )) == -1) {
13646485Sbostic 	    RETURN_ERROR (errno, error0);
13746485Sbostic 	}
13846485Sbostic 	(void)fcntl(hashp->fp, F_SETFD, 1);
13946366Sbostic     }
14046366Sbostic 
14146366Sbostic     if ( new_table ) {
14246366Sbostic 	if ( !(hashp = init_hash( info )) ) {
14346366Sbostic 	    RETURN_ERROR(errno,error1);
14446366Sbostic 	}
14546366Sbostic     } else {
14646366Sbostic 	/* Table already exists */
14746366Sbostic 	if ( info && info->hash ) hashp->hash = info->hash;
14846366Sbostic 	else hashp->hash = default_hash;
14946366Sbostic 
15046366Sbostic 	hdrsize = read ( hashp->fp, &hashp->hdr, sizeof(HASHHDR) );
15146366Sbostic #if BYTE_ORDER == LITTLE_ENDIAN
15246366Sbostic 	swap_header ( );
15346366Sbostic #endif
15446366Sbostic 	if ( hdrsize == -1 ) {
15546366Sbostic 	    RETURN_ERROR(errno, error1);
15646366Sbostic 	}
15746366Sbostic 	if ( hdrsize != sizeof(HASHHDR) ) {
15846366Sbostic 	    RETURN_ERROR(EFTYPE, error1);
15946366Sbostic 	}
16046366Sbostic 
16146366Sbostic 	/* Verify file type, versions and hash function */
16246366Sbostic 	if ( hashp->MAGIC != HASHMAGIC )
16346366Sbostic 	    RETURN_ERROR ( EFTYPE, error1 );
16446366Sbostic 	if ( hashp->VERSION != VERSION_NO )
16546366Sbostic 	    RETURN_ERROR ( EFTYPE, error1 );
16646366Sbostic 	if (hashp->hash ( CHARKEY, sizeof(CHARKEY) ) != hashp->H_CHARKEY ) {
16746366Sbostic 	    RETURN_ERROR ( EFTYPE, error1 );
16846366Sbostic 	}
16946366Sbostic 
17046366Sbostic 	/*
17146366Sbostic 	    Figure out how many segments we need.
17246366Sbostic 	*/
17346366Sbostic 	nsegs = (hashp->MAX_BUCKET + hashp->SGSIZE -1)/ hashp->SGSIZE;
17446366Sbostic 	hashp->nsegs = 0;
17546366Sbostic 	if (alloc_segs( nsegs )) {
17646366Sbostic 	    /*
17746366Sbostic 		If alloc_segs fails, table will have been destroyed
17846366Sbostic 		and errno will have been set.
17946366Sbostic 	    */
18046366Sbostic 	    return( (DB *) NULL );
18146366Sbostic 	}
18246366Sbostic 
18346366Sbostic 	/* Read in bitmaps */
18446366Sbostic 	bpages = (hashp->SPARES[__log2(hashp->MAX_BUCKET)] +
18546366Sbostic 		  (hashp->BSIZE << BYTE_SHIFT) - 1) >>
18646366Sbostic 		  (hashp->BSHIFT + BYTE_SHIFT);
18746366Sbostic 
18846503Sbostic 	hashp->nmaps = bpages;
18946366Sbostic 	hashp->mapp[0] = (u_long *)malloc(bpages<<hashp->BSHIFT);
19046366Sbostic 	if ( !hashp->mapp[0] ) {
19146366Sbostic 	    RETURN_ERROR(errno, error2);
19246366Sbostic 	}
19346366Sbostic 	for ( i = 0; i < bpages; i++ ) {
19446366Sbostic 	    hashp->mapp[i] = &hashp->mapp[0][i<<hashp->BSHIFT];
19546366Sbostic 	    if (__get_page ((char *)hashp->mapp[i],
19646366Sbostic 		hashp->BITMAPS[i], 0, 1, 1)) {
19746366Sbostic 		    RETURN_ERROR(errno, error2);
19846366Sbostic 	    }
19946366Sbostic 	}
20046366Sbostic 
20146366Sbostic     }
20246366Sbostic 
20346366Sbostic     /* Initialize Buffer Manager */
20446366Sbostic     if ( info && info->ncached ) {
20546366Sbostic 	__buf_init (info->ncached);
20646366Sbostic     } else {
20746366Sbostic 	__buf_init (DEF_BUFSIZE);
20846366Sbostic     }
20946366Sbostic 
21046366Sbostic     hashp->new_file = new_table;
21146457Sbostic     hashp->save_file = file && !(hashp->flags&O_RDONLY);
21246366Sbostic     hashp->cbucket = -1;
21346366Sbostic     if ( !(dbp = (DB *)malloc ( sizeof (DB) )) ) {
21446366Sbostic 	save_errno = errno;
21546366Sbostic 	hdestroy();
21646366Sbostic 	errno = save_errno;
21746366Sbostic 	return ( NULL );
21846366Sbostic     }
21946366Sbostic     dbp->internal = (char *)hashp;
22046366Sbostic     dbp->close = hash_close;
22146534Sbostic     dbp->del = hash_delete;
22246366Sbostic     dbp->get = hash_get;
22346366Sbostic     dbp->put = hash_put;
22446366Sbostic     dbp->seq = hash_seq;
22546366Sbostic     dbp->sync = hash_sync;
22646366Sbostic 
22746366Sbostic #ifdef DEBUG
22846366Sbostic 	(void)fprintf(stderr, "%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%x\n%s%x\n%s%d\n%s%d\n",
22946366Sbostic 		"init_htab:",
23046366Sbostic 		"TABLE POINTER   ", hashp,
23146366Sbostic 		"BUCKET SIZE     ", hashp->BSIZE,
23246366Sbostic 		"BUCKET SHIFT    ", hashp->BSHIFT,
23346366Sbostic 		"DIRECTORY SIZE  ", hashp->DSIZE,
23446366Sbostic 		"SEGMENT SIZE    ", hashp->SGSIZE,
23546366Sbostic 		"SEGMENT SHIFT   ", hashp->SSHIFT,
23646366Sbostic 		"FILL FACTOR     ", hashp->FFACTOR,
23746366Sbostic 		"MAX BUCKET      ", hashp->MAX_BUCKET,
23846366Sbostic 		"HIGH MASK       ", hashp->HIGH_MASK,
23946366Sbostic 		"LOW  MASK       ", hashp->LOW_MASK,
24046366Sbostic 		"NSEGS           ", hashp->nsegs,
24146366Sbostic 		"NKEYS           ", hashp->NKEYS );
24246366Sbostic #endif
24346366Sbostic #ifdef HASH_STATISTICS
24446366Sbostic 	hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
24546366Sbostic #endif
24646366Sbostic     return (dbp);
24746366Sbostic 
24846366Sbostic error2:
24946366Sbostic     (void)hdestroy();
25046366Sbostic     errno = save_errno;
25146366Sbostic     return ( (DB *)NULL );
25246366Sbostic 
25346366Sbostic error1:
25446366Sbostic     (void) close ( hashp->fp );
25546366Sbostic 
25646366Sbostic error0:
25746366Sbostic     free ( hashp );
25846366Sbostic     errno = save_errno;
25946366Sbostic     return ( (DB *) NULL );
26046366Sbostic }
26146366Sbostic 
26246366Sbostic static int
26346366Sbostic hash_close (dbp)
26446366Sbostic DB	*dbp;
26546366Sbostic {
26646457Sbostic 	int	retval;
26746457Sbostic 
26846366Sbostic 	if ( !dbp ) {
26946366Sbostic 	    return(ERROR);
27046366Sbostic 	}
27146366Sbostic 	hashp = (HTAB *)dbp->internal;
27246457Sbostic 	retval = hdestroy();
27346457Sbostic 	(void)free ( dbp );
27446457Sbostic 	return ( retval );
27546366Sbostic }
27646366Sbostic 
27746366Sbostic 
27846366Sbostic /************************** LOCAL CREATION ROUTINES **********************/
27946366Sbostic static HTAB *
28046366Sbostic init_hash( info )
28146366Sbostic HASHINFO *info;
28246366Sbostic {
28346366Sbostic 	int	nelem;
28446366Sbostic 
28546366Sbostic 	nelem = 1;
28646366Sbostic 
28746366Sbostic 	hashp->LORDER	= BYTE_ORDER;
28846366Sbostic 	hashp->BSIZE    = DEF_BUCKET_SIZE;
28946366Sbostic 	hashp->BSHIFT   = DEF_BUCKET_SHIFT;
29046366Sbostic 	hashp->SGSIZE   = DEF_SEGSIZE;
29146366Sbostic 	hashp->SSHIFT   = DEF_SEGSIZE_SHIFT;
29246366Sbostic 	hashp->DSIZE    = DEF_DIRSIZE;
29346366Sbostic 	hashp->FFACTOR  = DEF_FFACTOR;
29446366Sbostic 	hashp->hash     = default_hash;
29546366Sbostic 	bzero (hashp->SPARES, sizeof (hashp->SPARES));
29646366Sbostic 
29746366Sbostic 	if ( info ) {
29846366Sbostic 	    if ( info->bsize )   {
29946366Sbostic 		/* Round pagesize up to power of 2 */
30046366Sbostic 		hashp->BSHIFT  = __log2(info->bsize);
30146366Sbostic 		hashp->BSIZE   = 1 << hashp->BSHIFT;
30246366Sbostic 		if ( hashp->BSIZE > MAX_BSIZE ) {
30346366Sbostic 		    errno = EINVAL;
30446366Sbostic 		    return(0);
30546366Sbostic 		}
30646366Sbostic 	    }
30746366Sbostic 	    if ( info->ffactor )  hashp->FFACTOR = info->ffactor;
30846366Sbostic 	    if ( info->hash ) hashp->hash    = info->hash;
30946366Sbostic 	    if ( info->nelem ) nelem = info->nelem;
31046366Sbostic 	    if ( info->lorder ) {
31146366Sbostic 		if ( info->lorder != BIG_ENDIAN &&
31246366Sbostic 		     info->lorder != LITTLE_ENDIAN) {
31346366Sbostic 		    errno = EINVAL;
31446366Sbostic 		    return(0);
31546366Sbostic 		}
31646366Sbostic 		hashp->LORDER = info->lorder;
31746366Sbostic 	    }
31846366Sbostic 	}
31946366Sbostic 
32046366Sbostic 	/* init_htab should destroy the table and set errno if it fails */
32146366Sbostic 	if ( init_htab ( nelem ) ) {
32246366Sbostic 	    return(0);
32346366Sbostic 	} else {
32446366Sbostic 	    return(hashp);
32546366Sbostic 	}
32646366Sbostic }
32746366Sbostic 
32846366Sbostic /*
32946366Sbostic     This calls alloc_segs which may run out of memory.
33046366Sbostic     Alloc_segs will destroy the table and set errno,
33146366Sbostic     so we just pass the error information along.
33246366Sbostic 
33346366Sbostic     Returns 0 on No Error
33446366Sbostic 
33546366Sbostic */
33646366Sbostic static int
33746366Sbostic init_htab ( nelem )
33846366Sbostic int	nelem;
33946366Sbostic {
34046366Sbostic 	register SEGMENT	*segp;
34146366Sbostic 	register int nbuckets;
34246366Sbostic 	register int nsegs;
34346366Sbostic 	int	l2;
34446366Sbostic 
34546366Sbostic  /*
34646366Sbostic   * Divide number of elements by the fill factor and determine a desired
34746366Sbostic   * number of buckets.  Allocate space for the next greater power of
34846366Sbostic   * two number of buckets
34946366Sbostic   */
35046366Sbostic 	nelem = (nelem - 1) / hashp->FFACTOR + 1;
35146366Sbostic 
35246366Sbostic 	l2 = __log2(nelem);
35346366Sbostic 	nbuckets = 1 << l2;
35446366Sbostic 	nbuckets = MAX ( nbuckets, 2 );
35546366Sbostic 
35646366Sbostic 	hashp->SPARES[l2] = l2 + 1;
35746366Sbostic 	hashp->SPARES[l2+1] = l2 + 1;
35846366Sbostic 	/*
35946366Sbostic 	    First bitmap page is at:
36046366Sbostic 		splitpoint l2
36146366Sbostic 		page offset 1
36246366Sbostic 	*/
36346366Sbostic 	__init_bitmap ( OADDR_OF(l2,1), l2+1, 0 );
36446366Sbostic 
36546366Sbostic 	hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
36646366Sbostic 	hashp->HIGH_MASK = (nbuckets << 1) - 1;
36746366Sbostic 	hashp->HDRPAGES = ((MAX(sizeof(HASHHDR),MINHDRSIZE) - 1) >>
36846366Sbostic 			  hashp->BSHIFT) + 1;
36946366Sbostic 
37046366Sbostic 	nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
37146366Sbostic 	nsegs = 1 << __log2(nsegs);
37246366Sbostic 
37346366Sbostic 	if ( nsegs > hashp->DSIZE ) {
37446366Sbostic 	    hashp->DSIZE  = nsegs;
37546366Sbostic 	}
37646366Sbostic 
37746366Sbostic 	return (alloc_segs ( nsegs ) );
37846366Sbostic }
37946366Sbostic 
38046366Sbostic /********************** DESTROY/CLOSE ROUTINES ************************/
38146366Sbostic 
38246366Sbostic /*
38346366Sbostic     Flushes any changes to the file if necessary and
38446366Sbostic     destroys the hashp structure, freeing all allocated
38546366Sbostic     space.
38646366Sbostic */
38746366Sbostic static int
38846366Sbostic hdestroy()
38946366Sbostic {
39046366Sbostic 	int	save_errno;
39146366Sbostic 	int	i;
39246503Sbostic 	u_long	**mapp;
39346366Sbostic 
39446366Sbostic 	save_errno = 0;
39546366Sbostic 
39646366Sbostic 	if (hashp != NULL) {
39746366Sbostic #ifdef HASH_STATISTICS
39846366Sbostic 	 (void)	fprintf(stderr,	"hdestroy: accesses %ld collisions %ld\n",
39946366Sbostic 			hash_accesses, hash_collisions);
40046366Sbostic 	 (void)	fprintf(stderr, "hdestroy: expansions %ld\n",
40146366Sbostic 			hash_expansions);
40246366Sbostic 	 (void)	fprintf(stderr, "hdestroy: overflows %ld\n",
40346366Sbostic 			hash_overflows);
40446366Sbostic 	 (void)	fprintf(stderr,	"keys %ld maxp %d segmentcount %d\n",
40546366Sbostic 			hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
40646366Sbostic 
40746366Sbostic 	for ( i = 0; i < NCACHED; i++ ) {
40846366Sbostic 	    (void) fprintf ( stderr, "spares[%d] = %d\n", i, hashp->SPARES[i] );
40946366Sbostic 	}
41046366Sbostic #endif
41146366Sbostic 
41246366Sbostic 		/*
41346366Sbostic 		    Call on buffer manager to free buffers, and if
41446366Sbostic 		    required, write them to disk
41546366Sbostic 		*/
41646366Sbostic 		if (__buf_free(1, hashp->save_file)) {
41746366Sbostic 		    save_errno = errno;
41846366Sbostic 		}
41946366Sbostic 		if ( hashp->dir ) {
42046366Sbostic 		    (void)free(*hashp->dir);	/* Free initial segments */
42146366Sbostic 		    /* Free extra segments */
42246366Sbostic 		    while ( hashp->exsegs-- ) {
42346366Sbostic 			(void)free ( hashp->dir[--hashp->nsegs] );
42446366Sbostic 		    }
42546366Sbostic 		    (void)free(hashp->dir);
42646366Sbostic 		}
42746366Sbostic 		if (flush_meta() && !save_errno) {
42846366Sbostic 		    save_errno = errno;
42946366Sbostic 		}
43046503Sbostic 
43146503Sbostic 		/* Free Initial Bigmaps */
43246503Sbostic 		if ( hashp->nmaps ) {
43346503Sbostic 		    (void)free(hashp->mapp[0]);
43446503Sbostic 		}
43546503Sbostic 
43646503Sbostic 		/* Free extra bitmaps */
43746503Sbostic 		for ( mapp = &hashp->mapp[hashp->nmaps];
43846503Sbostic 		      hashp->exmaps--;
43946503Sbostic 		      mapp++ ) {
44046503Sbostic 		      (void) free ( *mapp );
44146503Sbostic 		}
44246503Sbostic 
44346457Sbostic 		if ( hashp->fp != -1 ) {
44446457Sbostic 			(void)close (hashp->fp);
44546457Sbostic 		}
44646366Sbostic 		(void)free(hashp);
44746366Sbostic 		hashp = NULL;
44846366Sbostic 	}
44946366Sbostic 	if ( save_errno ) {
45046366Sbostic 	    errno = save_errno;
45146366Sbostic 	    return(ERROR);
45246366Sbostic 	} else {
45346366Sbostic 	    return(SUCCESS);
45446366Sbostic 	}
45546366Sbostic }
45646366Sbostic 
45746366Sbostic /*
45846366Sbostic     Write modified pages to disk
45946366Sbostic     0 == OK
46046366Sbostic     -1 ERROR
46146366Sbostic */
46246366Sbostic static int
46346366Sbostic hash_sync(dbp)
46446366Sbostic DB	*dbp;
46546366Sbostic {
46646366Sbostic 	if ( !dbp ) {
46746366Sbostic 	    return (ERROR);
46846366Sbostic 	}
46946366Sbostic 
47046366Sbostic 	hashp = (HTAB *)dbp->internal;
47146366Sbostic 
47246366Sbostic 	if (!hashp->save_file) return(0);
47346366Sbostic 	if ( __buf_free ( 0, 1 )  || flush_meta()) {
47446366Sbostic 	    return(ERROR);
47546366Sbostic 	}
47646366Sbostic 	hashp->new_file = 0;
47746366Sbostic 	return(0);
47846366Sbostic }
47946366Sbostic 
48046366Sbostic /*
48146366Sbostic 0 == OK
48246366Sbostic -1 indicates that errno should be set
48346366Sbostic */
48446366Sbostic static int
48546366Sbostic flush_meta()
48646366Sbostic {
48746366Sbostic     	int	fp;
48846366Sbostic 	int	hdrsize;
48946366Sbostic 	int	i;
49046366Sbostic 	int	wsize;
49146366Sbostic 	HASHHDR	*whdrp;
49246366Sbostic 	HASHHDR	whdr;
49346366Sbostic 
49446366Sbostic 	if (!hashp->save_file) return(0);
49546366Sbostic 	hashp->MAGIC = HASHMAGIC;
49646366Sbostic 	hashp->VERSION = VERSION_NO;
49746366Sbostic 	hashp->H_CHARKEY = hashp->hash ( CHARKEY, sizeof(CHARKEY));
49846366Sbostic 
49946366Sbostic 	fp = hashp->fp;
50046366Sbostic 	whdrp = &hashp->hdr;
50146366Sbostic #if BYTE_ORDER == LITTLE_ENDIAN
50246366Sbostic 	whdrp = &whdr;
50346366Sbostic 	swap_header_copy( &hashp->hdr, whdrp );
50446366Sbostic #endif
50546500Sbostic 	if ( (lseek (fp, 0, SEEK_SET) == -1 ) ||
50646366Sbostic 	     ((wsize = write ( fp, whdrp, sizeof(HASHHDR))) == -1)) {
50746366Sbostic 	    return(-1);
50846366Sbostic 	} else if ( wsize != sizeof(HASHHDR) ) {
50946366Sbostic 	    errno = EFTYPE;
51046366Sbostic 	    hashp->errno = errno;
51146366Sbostic 	    return(-1);
51246366Sbostic 	}
51346366Sbostic 	for ( i = 0; i < NCACHED; i++ ) {
51446366Sbostic 	    if ( hashp->mapp[i] ) {
51546366Sbostic 		if (!__put_page((char *)hashp->mapp[i],
51646366Sbostic 		    hashp->BITMAPS[i], 0, 1)){
51746366Sbostic 		    return(-1);
51846366Sbostic 		}
51946366Sbostic 	    }
52046366Sbostic 	}
52146366Sbostic 	return(0);
52246366Sbostic }
52346366Sbostic /*******************************SEARCH ROUTINES *****************************/
52446366Sbostic /*
52546366Sbostic     All the access routines return
52646366Sbostic 	0 on SUCCESS
52746366Sbostic 	1 to indicate an external ERROR (i.e. key not found, etc)
52846366Sbostic 	-1 to indicate an internal ERROR (i.e. out of memory, etc)
52946366Sbostic */
53046366Sbostic static int
53146366Sbostic hash_get ( dbp, key, data )
53246366Sbostic DB	*dbp;
53346366Sbostic DBT *key, *data;
53446366Sbostic {
53546366Sbostic     if ( !dbp ) {
53646366Sbostic 	return (ERROR);
53746366Sbostic     }
53846366Sbostic     hashp = (HTAB *)dbp->internal;
53946366Sbostic     if ( hashp->flags & O_WRONLY ) {
54046366Sbostic 	errno = EBADF;
54146366Sbostic 	hashp->errno = errno;
54246366Sbostic 	return ( ERROR );
54346366Sbostic     }
54446366Sbostic     return ( hash_access ( HASH_GET, key, data ) );
54546366Sbostic }
54646366Sbostic 
54746366Sbostic static int
54846366Sbostic hash_put ( dbp, key, data, flag )
54946366Sbostic DB	*dbp;
55046366Sbostic DBT 	*key, *data;
55146366Sbostic int	flag;
55246366Sbostic {
55346366Sbostic     if ( !dbp ) {
55446366Sbostic 	return (ERROR);
55546366Sbostic     }
55646366Sbostic     hashp = (HTAB *)dbp->internal;
55746366Sbostic     if ( (hashp->flags & O_ACCMODE) == O_RDONLY ) {
55846366Sbostic 	errno = EBADF;
55946366Sbostic 	hashp->errno = errno;
56046366Sbostic 	return ( ERROR );
56146366Sbostic     }
56246366Sbostic     if ( flag == R_NOOVERWRITE ) {
56346366Sbostic 	return ( hash_access ( HASH_PUTNEW, key, data ) );
56446366Sbostic     } else {
56546366Sbostic 	return ( hash_access ( HASH_PUT, key, data ) );
56646366Sbostic     }
56746366Sbostic }
56846366Sbostic 
56946366Sbostic static int
57046366Sbostic hash_delete ( dbp, key, flags )
57146366Sbostic DB	*dbp;
57246366Sbostic DBT 	*key;
57346366Sbostic int	flags;		/* Ignored */
57446366Sbostic {
57546366Sbostic     if ( !dbp ) {
57646366Sbostic 	return (ERROR);
57746366Sbostic     }
57846366Sbostic     hashp = (HTAB *)dbp->internal;
57946366Sbostic     if ( (hashp->flags & O_ACCMODE) == O_RDONLY ) {
58046366Sbostic 	errno = EBADF;
58146366Sbostic 	hashp->errno = errno;
58246366Sbostic 	return ( ERROR );
58346366Sbostic     }
58446366Sbostic     return ( hash_access ( HASH_DELETE, key, NULL ) );
58546366Sbostic }
58646366Sbostic 
58746366Sbostic /*
58846366Sbostic     Assume that hashp has been set in wrapper routine
58946366Sbostic */
59046366Sbostic static int
59146366Sbostic hash_access(action, key, val)
59246366Sbostic ACTION action;
59346366Sbostic DBT *key, *val;
59446366Sbostic {
59546366Sbostic 	register	BUFHEAD	*rbufp;
59646366Sbostic 	register	u_short	*bp;
59746366Sbostic 	register	int	ndx;
59846366Sbostic 	register 	int n;
59946366Sbostic 	register 	int off = hashp->BSIZE;
60046366Sbostic 	register	int		size;
60146366Sbostic 	register	char	*kp;
60246366Sbostic 	BUFHEAD	*bufp;
60346457Sbostic 	BUFHEAD	*save_bufp;
60446366Sbostic 	u_short	pageno;
60546366Sbostic 
60646366Sbostic #ifdef HASH_STATISTICS
60746366Sbostic 	hash_accesses++;
60846366Sbostic #endif
60946366Sbostic 
61046366Sbostic 	size = key->size;
61146366Sbostic 	kp = key->data;
61246457Sbostic 	rbufp = __get_buf ( __call_hash(kp, size), NULL, 0 );
61346366Sbostic 	if ( !rbufp ) return(ERROR);
61446457Sbostic 	save_bufp = rbufp;
61546366Sbostic 
61646457Sbostic 	/* Pin the bucket chain */
61746457Sbostic 	rbufp->flags |= BUF_PIN;
61846366Sbostic 	for ( bp = (u_short *)rbufp->page, n = *bp++, ndx = 1; ndx < n;  ) {
61946366Sbostic 	    if ( bp[1] >= REAL_KEY ) {
62046366Sbostic 		/* Real key/data pair */
62146366Sbostic 		if (size == off - *bp &&
62246366Sbostic 		    bcmp(kp, rbufp->page + *bp, size) == 0) {
62346366Sbostic 		    goto found;
62446366Sbostic 		}
62546366Sbostic 		off = bp[1];
62646366Sbostic #ifdef HASH_STATISTICS
62746366Sbostic 		hash_collisions++;
62846366Sbostic #endif
62946366Sbostic 	        bp += 2;
63046366Sbostic 	        ndx += 2;
63146366Sbostic 	    } else if ( bp[1] == OVFLPAGE ) {
63246366Sbostic 		rbufp = __get_buf ( *bp, rbufp, 0 );
63346457Sbostic 		if ( !rbufp ) {
63446457Sbostic 		    save_bufp->flags &= ~BUF_PIN;
63546457Sbostic 		    return (ERROR);
63646457Sbostic 		}
63746366Sbostic 		/* FOR LOOP INIT */
63846366Sbostic 		bp = (u_short *)rbufp->page;
63946366Sbostic 		n = *bp++;
64046366Sbostic 		ndx = 1;
64146366Sbostic 		off = hashp->BSIZE;
64246366Sbostic 	    } else if ( bp[1] < REAL_KEY ) {
64346366Sbostic 		if ( (ndx = __find_bigpair(rbufp, ndx, kp, size )) > 0 ) {
64446366Sbostic 		    goto found;
64546366Sbostic 		} else if ( ndx == -2 ) {
64646366Sbostic 		    bufp = rbufp;
64746366Sbostic 		    if ( !(pageno = __find_last_page ( &bufp )) ) {
64846366Sbostic 			ndx = 0;
64946366Sbostic 			rbufp = bufp;
65046366Sbostic 			break;	/* FOR */
65146366Sbostic 		    }
65246366Sbostic 		    rbufp = __get_buf ( pageno, bufp, 0 );
65346457Sbostic 		    if ( !rbufp ) {
65446457Sbostic 			save_bufp->flags &= ~BUF_PIN;
65546457Sbostic 			return (ERROR);
65646457Sbostic 		    }
65746366Sbostic 		    /* FOR LOOP INIT */
65846366Sbostic 		    bp = (u_short *)rbufp->page;
65946366Sbostic 		    n = *bp++;
66046366Sbostic 		    ndx = 1;
66146366Sbostic 		    off = hashp->BSIZE;
66246366Sbostic 		} else  {
66346457Sbostic 		    save_bufp->flags &= ~BUF_PIN;
66446366Sbostic 		    return(ERROR);
66546366Sbostic 		}
66646366Sbostic 	    }
66746366Sbostic 	}
66846366Sbostic 
66946366Sbostic 	/* Not found */
67046366Sbostic 	switch ( action ) {
67146366Sbostic 	    case HASH_PUT:
67246366Sbostic 	    case HASH_PUTNEW:
67346366Sbostic 		if (__addel(rbufp, key, val)) {
67446457Sbostic 		    save_bufp->flags &= ~BUF_PIN;
67546366Sbostic 		    return(ERROR);
67646366Sbostic 		} else {
67746457Sbostic 		    save_bufp->flags &= ~BUF_PIN;
67846366Sbostic 		    return(SUCCESS);
67946366Sbostic 		}
68046366Sbostic 	    case HASH_GET:
68146366Sbostic 	    case HASH_DELETE:
68246366Sbostic 	    default:
68346457Sbostic 		save_bufp->flags &= ~BUF_PIN;
68446366Sbostic 		return(ABNORMAL);
68546366Sbostic 	}
68646366Sbostic 
68746366Sbostic found:
68846366Sbostic 	switch (action) {
68946366Sbostic 	    case HASH_PUTNEW:
69046457Sbostic 		save_bufp->flags &= ~BUF_PIN;
69146366Sbostic 		return(ABNORMAL);
69246366Sbostic 	    case HASH_GET:
69346366Sbostic 		bp = (u_short *)rbufp->page;
69446366Sbostic 		if (bp[ndx+1] < REAL_KEY) __big_return(rbufp, ndx, val, 0);
69546366Sbostic 		else {
69646366Sbostic 		    val->data = rbufp->page + (int) bp[ndx + 1];
69746366Sbostic 		    val->size = bp[ndx] - bp[ndx + 1];
69846366Sbostic 		}
69946366Sbostic 		break;
70046366Sbostic 	    case HASH_PUT:
70146457Sbostic 		if ((__delpair (rbufp, ndx)) || (__addel (rbufp, key, val)) ) {
70246457Sbostic 		    save_bufp->flags &= ~BUF_PIN;
70346457Sbostic 		    return(ERROR);
70446457Sbostic 		}
70546366Sbostic 		break;
70646366Sbostic 	    case HASH_DELETE:
70746366Sbostic 		if (__delpair (rbufp, ndx))return(ERROR);
70846366Sbostic 		break;
70946366Sbostic 	}
71046457Sbostic 	save_bufp->flags &= ~BUF_PIN;
71146366Sbostic 	return (SUCCESS);
71246366Sbostic }
71346366Sbostic 
71446366Sbostic static int
71546366Sbostic hash_seq(dbp, key, data, flag)
71646366Sbostic DB	*dbp;
71746366Sbostic DBT 	*key, *data;
71846366Sbostic int flag;
71946366Sbostic {
72046366Sbostic 	register	int bucket;
72146366Sbostic 	register	BUFHEAD	*bufp;
72246457Sbostic 	BUFHEAD	*save_bufp;
72346366Sbostic 	u_short	*bp;
72446366Sbostic 	u_short	ndx;
72546366Sbostic 	u_short	pageno;
72646366Sbostic 
72746366Sbostic 	if ( !dbp ) {
72846366Sbostic 	    return (ERROR);
72946366Sbostic 	}
73046366Sbostic 
73146366Sbostic 	hashp = (HTAB *)dbp->internal;
73246366Sbostic 	if ( hashp->flags & O_WRONLY ) {
73346366Sbostic 	    errno = EBADF;
73446366Sbostic 	    hashp->errno = errno;
73546366Sbostic 	    return ( ERROR );
73646366Sbostic 	}
73746366Sbostic #ifdef HASH_STATISTICS
73846366Sbostic 	hash_accesses++;
73946366Sbostic #endif
74046366Sbostic 
74146366Sbostic 	if ( (hashp->cbucket < 0) || (flag == R_FIRST) ) {
74246366Sbostic 	    hashp->cbucket = 0;
74346366Sbostic 	    hashp->cndx = 1;
74446366Sbostic 	    hashp->cpage = NULL;
74546366Sbostic 	}
74646366Sbostic 
74746366Sbostic 	if ( !(bufp = hashp->cpage) ) {
74846366Sbostic 	    for (bucket = hashp->cbucket;
74946366Sbostic 		 bucket <= hashp->MAX_BUCKET;
75046366Sbostic 		 bucket++, hashp->cndx = 1 ) {
75146366Sbostic 
75246366Sbostic 		bufp = __get_buf ( bucket, NULL, 0 );
75346366Sbostic 		if (!bufp) return(ERROR);
75446366Sbostic 		hashp->cpage = bufp;
75546366Sbostic 		bp = (u_short *)bufp->page;
75646366Sbostic 		if (bp[0]) break;
75746366Sbostic 	    }
75846366Sbostic 	    hashp->cbucket = bucket;
75946366Sbostic 	    if ( hashp->cbucket > hashp->MAX_BUCKET ) {
76046366Sbostic 		hashp->cbucket = -1;
76146366Sbostic 		return ( ABNORMAL );
76246366Sbostic 	    }
76346366Sbostic 	} else {
76446366Sbostic 	    bp  = (u_short *)hashp->cpage->page;
76546366Sbostic 	}
76646366Sbostic 
76746366Sbostic #ifdef DEBUG
76846366Sbostic 	assert (bp);
76946366Sbostic 	assert (bufp);
77046366Sbostic #endif
77146366Sbostic 	while ( bp[hashp->cndx+1] == OVFLPAGE ) {
77246366Sbostic 	    bufp = hashp->cpage = __get_buf ( bp[hashp->cndx], bufp, 0 );
77346366Sbostic 	    if (!bufp) return(ERROR);
77446366Sbostic 	    bp = (u_short *)(bufp->page);
77546366Sbostic 	    hashp->cndx = 1;
77646366Sbostic 	}
77746366Sbostic 	ndx = hashp->cndx;
77846366Sbostic 	if (bp[ndx+1] < REAL_KEY) {
77946366Sbostic 	    if (__big_keydata(bufp, ndx, key, data, 1)) {
78046366Sbostic 		return(ERROR);
78146366Sbostic 	    }
78246366Sbostic 	} else {
78346366Sbostic 	    key->data = hashp->cpage->page + bp[ndx];
78446366Sbostic 	    key->size = (ndx > 1 ? bp[ndx-1] : hashp->BSIZE) - bp[ndx];
78546366Sbostic 	    data->data = hashp->cpage->page + bp[ndx + 1];
78646366Sbostic 	    data->size = bp[ndx] - bp[ndx + 1];
78746366Sbostic 	    ndx += 2;
78846366Sbostic 	    if ( ndx > bp[0] ) {
78946366Sbostic 		hashp->cpage = NULL;
79046366Sbostic 		hashp->cbucket++;
79146366Sbostic 		hashp->cndx=1;
79246366Sbostic 	    } else hashp->cndx = ndx;
79346366Sbostic 	}
79446366Sbostic 	return (SUCCESS);
79546366Sbostic }
79646366Sbostic 
79746366Sbostic /********************************* UTILITIES ************************/
79846366Sbostic /*
79946366Sbostic     0 ==> OK
80046366Sbostic     -1 ==> Error
80146366Sbostic */
80246366Sbostic extern int
80346366Sbostic __expand_table()
80446366Sbostic {
80546366Sbostic 	int	old_bucket, new_bucket;
80646366Sbostic 	int	new_segnum;
80746366Sbostic 	int	dirsize;
80846366Sbostic 	int	spare_ndx;
80946366Sbostic 	register	char **old, **new;
81046366Sbostic #ifdef HASH_STATISTICS
81146366Sbostic 	hash_expansions++;
81246366Sbostic #endif
81346366Sbostic 
81446366Sbostic 	new_bucket = ++hashp->MAX_BUCKET;
81546366Sbostic 	old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
81646366Sbostic 
81746366Sbostic 	new_segnum = new_bucket >> hashp->SSHIFT;
81846366Sbostic 
81946366Sbostic 	/* Check if we need a new segment */
82046366Sbostic 	if ( new_segnum >= hashp->nsegs ) {
82146366Sbostic 
82246366Sbostic 	    /* Check if we need to expand directory */
82346366Sbostic 	    if ( new_segnum >= hashp->DSIZE ) {
82446366Sbostic 
82546366Sbostic 		/* Reallocate directory */
82646366Sbostic 		dirsize = hashp->DSIZE * sizeof ( SEGMENT * );
82746366Sbostic 		if (!hash_realloc ( &hashp->dir, dirsize, (dirsize << 1 ) )) {
82846366Sbostic 		    return(-1);
82946366Sbostic 		}
83046366Sbostic 		hashp->DSIZE = dirsize << 1;
83146366Sbostic 	    }
83246366Sbostic 	    if (!(hashp->dir[new_segnum] =
83346366Sbostic 		    (SEGMENT)calloc ( hashp->SGSIZE, sizeof(SEGMENT)))) {
83446366Sbostic 		    return(-1);
83546366Sbostic 	    }
83646366Sbostic 	    hashp->exsegs++;
83746366Sbostic 	    hashp->nsegs++;
83846366Sbostic 	}
83946366Sbostic 
84046366Sbostic 	/*
84146366Sbostic 	    If the split point is increasing (MAX_BUCKET's log
84246366Sbostic 	    base 2 increases), we need to copy the current contents
84346366Sbostic 	    of the spare split bucket to the next bucket
84446366Sbostic 	*/
84546366Sbostic 	spare_ndx = __log2(hashp->MAX_BUCKET);
84646366Sbostic 	if ( spare_ndx != (__log2(hashp->MAX_BUCKET - 1))) {
84746366Sbostic 	    hashp->SPARES[spare_ndx] = hashp->SPARES[spare_ndx-1];
84846366Sbostic 	}
84946366Sbostic 
85046366Sbostic 	if ( new_bucket > hashp->HIGH_MASK ) {
85146366Sbostic 	    /* Starting a new doubling */
85246366Sbostic 	    hashp->LOW_MASK = hashp->HIGH_MASK;
85346366Sbostic 	    hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
85446366Sbostic 	}
85546366Sbostic 
85646366Sbostic 	/*
85746366Sbostic 	 * Relocate records to the new bucket
85846366Sbostic 	 */
85946366Sbostic 	return(__split_page ( old_bucket, new_bucket ));
86046366Sbostic }
86146366Sbostic /*
86246366Sbostic     If realloc guarantees that the pointer is not destroyed
86346366Sbostic     if the realloc fails, then this routine can go away
86446366Sbostic */
86546366Sbostic static char *
86646366Sbostic hash_realloc ( p_ptr, oldsize, newsize )
86746366Sbostic char	**p_ptr;
86846366Sbostic int	oldsize;
86946366Sbostic int	newsize;
87046366Sbostic {
87146366Sbostic 	register char	*p;
87246366Sbostic 
87346366Sbostic 	if (p = (char *)malloc ( newsize ) ) {
87446366Sbostic 		bcopy ( *p_ptr, p, oldsize );
87546366Sbostic 		bzero ( *p_ptr + oldsize, newsize-oldsize );
87646366Sbostic 		free(*p_ptr);
87746366Sbostic 		*p_ptr = p;
87846366Sbostic 	}
87946366Sbostic 	return (p);
88046366Sbostic }
88146366Sbostic 
88246366Sbostic extern int
88346366Sbostic __call_hash ( k, len )
88446366Sbostic char	*k;
88546366Sbostic int	len;
88646366Sbostic {
88746366Sbostic 	int	n, bucket;
88846366Sbostic 	n = hashp->hash(k, len);
88946366Sbostic 
89046366Sbostic 	bucket = n & hashp->HIGH_MASK;
89146366Sbostic 	if ( bucket > hashp->MAX_BUCKET ) {
89246366Sbostic 	    bucket = bucket & hashp->LOW_MASK;
89346366Sbostic 	}
89446366Sbostic 
89546366Sbostic 	return(bucket);
89646366Sbostic }
89746366Sbostic 
89846366Sbostic /*
89946366Sbostic     Allocate segment table.  On error, destroy the table
90046366Sbostic     and set errno.
90146366Sbostic 
90246366Sbostic     Returns 0 on success
90346366Sbostic */
90446366Sbostic static int
90546366Sbostic alloc_segs ( nsegs )
90646366Sbostic int	nsegs;
90746366Sbostic {
90846366Sbostic     register int	i;
90946366Sbostic     register SEGMENT	store;
91046366Sbostic 
91146366Sbostic     int	save_errno;
91246366Sbostic 
91346366Sbostic     if (!(hashp->dir = (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *)))) {
91446366Sbostic 	save_errno = errno;
91546366Sbostic 	(void)hdestroy();
91646366Sbostic 	errno = save_errno;
91746366Sbostic 	return(-1);
91846366Sbostic     }
91946366Sbostic 
92046366Sbostic     /* Allocate segments */
92146366Sbostic     store = (SEGMENT)calloc ( nsegs << hashp->SSHIFT, sizeof (SEGMENT) );
92246366Sbostic     if ( !store ) {
92346366Sbostic 	save_errno = errno;
92446366Sbostic 	(void)hdestroy();
92546366Sbostic 	errno = save_errno;
92646366Sbostic 	return(-1);
92746366Sbostic     }
92846366Sbostic 
92946366Sbostic     for ( i=0; i < nsegs; i++, hashp->nsegs++ ) {
93046366Sbostic 	hashp->dir[i] = &store[i<<hashp->SSHIFT];
93146366Sbostic     }
93246366Sbostic     return(0);
93346366Sbostic }
93446366Sbostic 
93546366Sbostic /*
93646366Sbostic     Hashp->hdr needs to be byteswapped.
93746366Sbostic */
93846366Sbostic static void
93946366Sbostic swap_header_copy ( srcp, destp )
94046366Sbostic HASHHDR	*srcp;
94146366Sbostic HASHHDR	*destp;
94246366Sbostic {
94346366Sbostic     int i;
94446366Sbostic 
94546366Sbostic     BLSWAP_COPY(srcp->magic, destp->magic);
94646366Sbostic     BLSWAP_COPY(srcp->version, destp->version);
94746366Sbostic     BLSWAP_COPY(srcp->lorder, destp->lorder);
94846366Sbostic     BLSWAP_COPY(srcp->bsize, destp->bsize);
94946366Sbostic     BLSWAP_COPY(srcp->bshift, destp->bshift);
95046366Sbostic     BLSWAP_COPY(srcp->dsize, destp->dsize);
95146366Sbostic     BLSWAP_COPY(srcp->ssize, destp->ssize);
95246366Sbostic     BLSWAP_COPY(srcp->sshift, destp->sshift);
95346366Sbostic     BLSWAP_COPY(srcp->max_bucket, destp->max_bucket);
95446366Sbostic     BLSWAP_COPY(srcp->high_mask, destp->high_mask);
95546366Sbostic     BLSWAP_COPY(srcp->low_mask, destp->low_mask);
95646366Sbostic     BLSWAP_COPY(srcp->ffactor, destp->ffactor);
95746366Sbostic     BLSWAP_COPY(srcp->nkeys, destp->nkeys);
95846366Sbostic     BLSWAP_COPY(srcp->hdrpages, destp->hdrpages);
95946366Sbostic     BLSWAP_COPY(srcp->h_charkey, destp->h_charkey);
96046366Sbostic     for ( i=0; i < NCACHED; i++ ) {
96146366Sbostic 	BLSWAP_COPY ( srcp->spares[i] , destp->spares[i]);
96246366Sbostic 	BSSWAP_COPY ( srcp->bitmaps[i] , destp->bitmaps[i]);
96346366Sbostic     }
96446366Sbostic     return;
96546366Sbostic }
96646366Sbostic 
96746366Sbostic static void
96846366Sbostic swap_header ( )
96946366Sbostic {
97046366Sbostic     HASHHDR	*hdrp;
97146366Sbostic     int	i;
97246366Sbostic 
97346366Sbostic     hdrp = &hashp->hdr;
97446366Sbostic 
97546366Sbostic     BLSWAP(hdrp->magic);
97646366Sbostic     BLSWAP(hdrp->version);
97746366Sbostic     BLSWAP(hdrp->lorder);
97846366Sbostic     BLSWAP(hdrp->bsize);
97946366Sbostic     BLSWAP(hdrp->bshift);
98046366Sbostic     BLSWAP(hdrp->dsize);
98146366Sbostic     BLSWAP(hdrp->ssize);
98246366Sbostic     BLSWAP(hdrp->sshift);
98346366Sbostic     BLSWAP(hdrp->max_bucket);
98446366Sbostic     BLSWAP(hdrp->high_mask);
98546366Sbostic     BLSWAP(hdrp->low_mask);
98646366Sbostic     BLSWAP(hdrp->ffactor);
98746366Sbostic     BLSWAP(hdrp->nkeys);
98846366Sbostic     BLSWAP(hdrp->hdrpages);
98946366Sbostic     BLSWAP(hdrp->h_charkey);
99046366Sbostic     for ( i=0; i < NCACHED; i++ ) {
99146366Sbostic 	BLSWAP ( hdrp->spares[i] );
99246366Sbostic 	BSSWAP ( hdrp->bitmaps[i] );
99346366Sbostic     }
99446366Sbostic     return;
99546366Sbostic }
996