2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
16 * 3. 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.
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
33 #include <sys/param.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 #include <sys/types.h>
53 #define __DBINTERFACE_PRIVATE /* activate prototypes from db_local.h */
59 static int alloc_segs(HTAB
*, int);
60 static int flush_meta(HTAB
*);
61 static int hash_access(HTAB
*, ACTION
, DBT
*, DBT
*);
62 static int hash_close(DB
*);
63 static int hash_delete(const DB
*, const DBT
*, u_int
);
64 static int hash_fd(const DB
*);
65 static int hash_get(const DB
*, const DBT
*, DBT
*, u_int
);
66 static int hash_put(const DB
*, DBT
*, const DBT
*, u_int
);
67 static void *hash_realloc(SEGMENT
**, int, int);
68 static int hash_seq(const DB
*, DBT
*, DBT
*, u_int
);
69 static int hash_sync(const DB
*, u_int
);
70 static int hdestroy(HTAB
*);
71 static HTAB
*init_hash(HTAB
*, const char *, const HASHINFO
*);
72 static int init_htab(HTAB
*, int);
73 #if (BYTE_ORDER == LITTLE_ENDIAN)
74 static void swap_header(HTAB
*);
75 static void swap_header_copy(HASHHDR
*, HASHHDR
*);
78 /* Macros for min/max. */
80 #define MIN(a,b) (((a)<(b))?(a):(b))
83 #define MAX(a,b) (((a)>(b))?(a):(b))
86 /* Fast arithmetic, relying on powers of 2, */
87 #define MOD(x, y) ((x) & ((y) - 1))
89 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
96 #ifdef HASH_STATISTICS
97 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
100 /************************** INTERFACE ROUTINES ***************************/
104 __hash_open (const char *file
,
108 const HASHINFO
*info
) /* Special directives for create */
112 #ifdef __USE_INTERNAL_STAT64
113 struct stat64 statbuf
;
118 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
120 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
125 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
130 * Even if user wants write only, we need to be able to read
131 * the actual file, so we need to open it read/write. But, the
132 * field in the hashp structure needs to be accurate so that
133 * we can check accesses.
135 hashp
->flags
= flags
;
138 if (!file
|| (flags
& O_TRUNC
) ||
139 #ifdef __USE_INTERNAL_STAT64
140 (_stat64_r(_REENT
, file
, &statbuf
) && (errno
== ENOENT
))) {
142 (_stat_r(_REENT
, file
, &statbuf
) && (errno
== ENOENT
))) {
145 errno
= 0; /* Just in case someone looks at errno */
149 if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
150 RETURN_ERROR(errno
, error0
);
152 /* if the .db file is empty, and we had permission to create
153 a new .db file, then reinitialize the database */
154 if ((flags
& O_CREAT
) &&
155 #ifdef __USE_INTERNAL_STAT64
156 _fstat64_r(_REENT
, hashp
->fp
, &statbuf
) == 0 && statbuf
.st_size
== 0)
158 _fstat_r(_REENT
, hashp
->fp
, &statbuf
) == 0 && statbuf
.st_size
== 0)
163 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
167 if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
168 RETURN_ERROR(errno
, error1
);
170 /* Table already exists */
171 if (info
&& info
->hash
)
172 hashp
->hash
= info
->hash
;
174 hashp
->hash
= __default_hash
;
176 hdrsize
= read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
177 #if (BYTE_ORDER == LITTLE_ENDIAN)
181 RETURN_ERROR(errno
, error1
);
182 if (hdrsize
!= sizeof(HASHHDR
))
183 RETURN_ERROR(EFTYPE
, error1
);
184 /* Verify file type, versions and hash function */
185 if (hashp
->MAGIC
!= HASHMAGIC
)
186 RETURN_ERROR(EFTYPE
, error1
);
187 #define OLDHASHVERSION 1
188 if (hashp
->HASH_VERSION
!= HASHVERSION
&&
189 hashp
->HASH_VERSION
!= OLDHASHVERSION
)
190 RETURN_ERROR(EFTYPE
, error1
);
191 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
192 RETURN_ERROR(EFTYPE
, error1
);
193 /* Check bucket size isn't too big for target int. */
194 if (hashp
->BSIZE
> INT_MAX
)
195 RETURN_ERROR(EFTYPE
, error1
);
197 * Figure out how many segments we need. Max_Bucket is the
198 * maximum bucket number, so the number of buckets is
201 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
204 if (alloc_segs(hashp
, nsegs
))
206 * If alloc_segs fails, table will have been destroyed
207 * and errno will have been set.
210 /* Read in bitmaps */
211 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
212 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
213 (hashp
->BSHIFT
+ BYTE_SHIFT
);
215 hashp
->nmaps
= bpages
;
216 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(__uint32_t
*));
219 /* Initialize Buffer Manager */
220 if (info
&& info
->cachesize
)
221 __buf_init(hashp
, info
->cachesize
);
223 __buf_init(hashp
, DEF_BUFSIZE
);
225 hashp
->new_file
= new_table
;
226 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
228 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
234 dbp
->internal
= hashp
;
235 dbp
->close
= hash_close
;
236 dbp
->del
= hash_delete
;
241 dbp
->sync
= hash_sync
;
245 (void)fprintf(stderr
,
246 "%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",
248 "TABLE POINTER ", hashp
,
249 "BUCKET SIZE ", hashp
->BSIZE
,
250 "BUCKET SHIFT ", hashp
->BSHIFT
,
251 "DIRECTORY SIZE ", hashp
->DSIZE
,
252 "SEGMENT SIZE ", hashp
->SGSIZE
,
253 "SEGMENT SHIFT ", hashp
->SSHIFT
,
254 "FILL FACTOR ", hashp
->FFACTOR
,
255 "MAX BUCKET ", hashp
->MAX_BUCKET
,
256 "OVFL POINT ", hashp
->OVFL_POINT
,
257 "LAST FREED ", hashp
->LAST_FREED
,
258 "HIGH MASK ", hashp
->HIGH_MASK
,
259 "LOW MASK ", hashp
->LOW_MASK
,
260 "NSEGS ", hashp
->nsegs
,
261 "NKEYS ", hashp
->NKEYS
);
263 #ifdef HASH_STATISTICS
264 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
270 (void)close(hashp
->fp
);
289 hashp
= (HTAB
*)dbp
->internal
;
290 retval
= hdestroy(hashp
);
305 hashp
= (HTAB
*)dbp
->internal
;
306 if (hashp
->fp
== -1) {
313 /************************** LOCAL CREATION ROUTINES **********************/
321 #ifdef __USE_INTERNAL_STAT64
322 struct stat64 statbuf
;
330 hashp
->LORDER
= DB_BYTE_ORDER
;
331 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
332 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
333 hashp
->SGSIZE
= DEF_SEGSIZE
;
334 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
335 hashp
->DSIZE
= DEF_DIRSIZE
;
336 hashp
->FFACTOR
= DEF_FFACTOR
;
337 hashp
->hash
= __default_hash
;
338 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
339 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
341 /* Fix bucket size to be optimal for file system */
343 #ifdef __USE_INTERNAL_STAT64
344 if (_stat64_r(_REENT
, file
, &statbuf
))
346 if (_stat_r(_REENT
, file
, &statbuf
))
349 hashp
->BSIZE
= MIN(statbuf
.st_blksize
, MAX_BSIZE
);
350 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
355 /* Round pagesize up to power of 2 */
356 hashp
->BSHIFT
= __log2(info
->bsize
);
357 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
358 if (hashp
->BSIZE
> MAX_BSIZE
) {
364 hashp
->FFACTOR
= info
->ffactor
;
366 hashp
->hash
= info
->hash
;
370 if (info
->lorder
!= DB_BIG_ENDIAN
&&
371 info
->lorder
!= DB_LITTLE_ENDIAN
) {
375 hashp
->LORDER
= info
->lorder
;
378 /* init_htab should destroy the table and set errno if it fails */
379 if (init_htab(hashp
, nelem
))
385 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
386 * the table and set errno, so we just pass the error information along.
388 * Returns 0 on No Error
400 * Divide number of elements by the fill factor and determine a
401 * desired number of buckets. Allocate space for the next greater
402 * power of two number of buckets.
404 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
406 l2
= __log2(MAX(nelem
, 2));
409 hashp
->SPARES
[l2
] = l2
+ 1;
410 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
411 hashp
->OVFL_POINT
= l2
;
412 hashp
->LAST_FREED
= 2;
414 /* First bitmap page is at: splitpoint l2 page offset 1 */
415 if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
418 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
419 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
420 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
423 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
424 nsegs
= 1 << __log2(nsegs
);
426 if (nsegs
> hashp
->DSIZE
)
427 hashp
->DSIZE
= nsegs
;
428 return (alloc_segs(hashp
, nsegs
));
431 /********************** DESTROY/CLOSE ROUTINES ************************/
434 * Flushes any changes to the file if necessary and destroys the hashp
435 * structure, freeing all allocated space.
446 #ifdef HASH_STATISTICS
447 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
448 hash_accesses
, hash_collisions
);
449 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
451 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
453 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
454 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
456 for (i
= 0; i
< NCACHED
; i
++)
457 (void)fprintf(stderr
,
458 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
461 * Call on buffer manager to free buffers, and if required,
462 * write them to disk.
464 if (__buf_free(hashp
, 1, hashp
->save_file
))
467 free(*hashp
->dir
); /* Free initial segments */
468 /* Free extra segments */
469 while (hashp
->exsegs
--)
470 free(hashp
->dir
[--hashp
->nsegs
]);
473 if (flush_meta(hashp
) && !save_errno
)
476 for (i
= 0; i
< hashp
->nmaps
; i
++)
478 free(hashp
->mapp
[i
]);
481 (void)close(hashp
->fp
);
492 * Write modified pages to disk
514 hashp
= (HTAB
*)dbp
->internal
;
515 if (!hashp
->save_file
)
517 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
526 * -1 indicates that errno should be set
534 #if (BYTE_ORDER == LITTLE_ENDIAN)
539 if (!hashp
->save_file
)
541 hashp
->MAGIC
= HASHMAGIC
;
542 hashp
->HASH_VERSION
= HASHVERSION
;
543 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
547 #if (BYTE_ORDER == LITTLE_ENDIAN)
549 swap_header_copy(&hashp
->hdr
, whdrp
);
551 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
552 ((wsize
= write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
555 if (wsize
!= sizeof(HASHHDR
)) {
557 hashp
->error
= errno
;
560 for (i
= 0; i
< NCACHED
; i
++)
562 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
563 hashp
->BITMAPS
[i
], 0, 1))
568 /*******************************SEARCH ROUTINES *****************************/
570 * All the access routines return
574 * 1 to indicate an external ERROR (i.e. key not found, etc)
575 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
587 hashp
= (HTAB
*)dbp
->internal
;
589 hashp
->error
= errno
= EINVAL
;
592 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
605 hashp
= (HTAB
*)dbp
->internal
;
606 if (flag
&& flag
!= R_NOOVERWRITE
) {
607 hashp
->error
= EINVAL
;
611 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
612 hashp
->error
= errno
= EPERM
;
615 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
616 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
623 u_int flag
/* Ignored */
628 hashp
= (HTAB
*)dbp
->internal
;
629 if (flag
&& flag
!= R_CURSOR
) {
630 hashp
->error
= errno
= EINVAL
;
633 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
634 hashp
->error
= errno
= EPERM
;
637 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
641 * Assume that hashp has been set in wrapper routine.
652 BUFHEAD
*bufp
, *save_bufp
;
654 int n
, ndx
, off
, size
;
658 #ifdef HASH_STATISTICS
664 kp
= (char *)key
->data
;
665 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
670 /* Pin the bucket chain */
671 rbufp
->flags
|= BUF_PIN
;
672 for (bp
= (__uint16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
673 if (bp
[1] >= REAL_KEY
) {
674 /* Real key/data pair */
675 if (size
== off
- *bp
&&
676 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
679 #ifdef HASH_STATISTICS
684 } else if (bp
[1] == OVFLPAGE
) {
685 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
687 save_bufp
->flags
&= ~BUF_PIN
;
691 bp
= (__uint16_t
*)rbufp
->page
;
695 } else if (bp
[1] < REAL_KEY
) {
697 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
702 __find_last_page(hashp
, &bufp
))) {
707 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
709 save_bufp
->flags
&= ~BUF_PIN
;
713 bp
= (__uint16_t
*)rbufp
->page
;
718 save_bufp
->flags
&= ~BUF_PIN
;
727 if (__addel(hashp
, rbufp
, key
, val
)) {
728 save_bufp
->flags
&= ~BUF_PIN
;
731 save_bufp
->flags
&= ~BUF_PIN
;
737 save_bufp
->flags
&= ~BUF_PIN
;
744 save_bufp
->flags
&= ~BUF_PIN
;
747 bp
= (__uint16_t
*)rbufp
->page
;
748 if (bp
[ndx
+ 1] < REAL_KEY
) {
749 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
752 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
753 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
757 if ((__delpair(hashp
, rbufp
, ndx
)) ||
758 (__addel(hashp
, rbufp
, key
, val
))) {
759 save_bufp
->flags
&= ~BUF_PIN
;
764 if (__delpair(hashp
, rbufp
, ndx
))
770 save_bufp
->flags
&= ~BUF_PIN
;
787 hashp
= (HTAB
*)dbp
->internal
;
788 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
789 hashp
->error
= errno
= EINVAL
;
792 #ifdef HASH_STATISTICS
795 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
801 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
802 if (!(bufp
= hashp
->cpage
)) {
803 for (bucket
= hashp
->cbucket
;
804 bucket
<= hashp
->MAX_BUCKET
;
805 bucket
++, hashp
->cndx
= 1) {
806 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
810 bp
= (__uint16_t
*)bufp
->page
;
814 hashp
->cbucket
= bucket
;
815 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
820 bp
= (__uint16_t
*)hashp
->cpage
->page
;
826 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
827 bufp
= hashp
->cpage
=
828 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
831 bp
= (__uint16_t
*)(bufp
->page
);
840 if (bp
[ndx
+ 1] < REAL_KEY
) {
841 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
844 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
845 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
846 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
847 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
859 /********************************* UTILITIES ************************/
871 __uint32_t old_bucket
, new_bucket
;
872 int dirsize
, new_segnum
, spare_ndx
;
874 #ifdef HASH_STATISTICS
877 new_bucket
= ++hashp
->MAX_BUCKET
;
878 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
880 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
882 /* Check if we need a new segment */
883 if (new_segnum
>= hashp
->nsegs
) {
884 /* Check if we need to expand directory */
885 if (new_segnum
>= hashp
->DSIZE
) {
886 /* Reallocate directory */
887 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
888 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
890 hashp
->DSIZE
= dirsize
<< 1;
892 if ((hashp
->dir
[new_segnum
] =
893 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
899 * If the split point is increasing (MAX_BUCKET's log base 2
900 * * increases), we need to copy the current contents of the spare
901 * split bucket to the next bucket.
903 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
904 if (spare_ndx
> hashp
->OVFL_POINT
) {
905 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
906 hashp
->OVFL_POINT
= spare_ndx
;
909 if (new_bucket
> hashp
->HIGH_MASK
) {
910 /* Starting a new doubling */
911 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
912 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
914 /* Relocate records to the new bucket */
915 return (__split_page(hashp
, old_bucket
, new_bucket
));
919 * If realloc guarantees that the pointer is not destroyed if the realloc
920 * fails, then this routine can go away.
931 if ( (p
= malloc(newsize
)) ) {
932 memmove(p
, *p_ptr
, oldsize
);
933 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
949 n
= hashp
->hash(k
, len
);
950 bucket
= n
& hashp
->HIGH_MASK
;
951 if (bucket
> hashp
->MAX_BUCKET
)
952 bucket
= bucket
& hashp
->LOW_MASK
;
957 * Allocate segment table. On error, destroy the table and set errno.
959 * Returns 0 on success
973 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
975 (void)hdestroy(hashp
);
979 /* Allocate segments */
981 (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
983 (void)hdestroy(hashp
);
987 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
988 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
992 #if (BYTE_ORDER == LITTLE_ENDIAN)
994 * Hashp->hdr needs to be byteswapped.
1004 P_32_COPY(srcp
->magic
, destp
->magic
);
1005 P_32_COPY(srcp
->version
, destp
->version
);
1006 P_32_COPY(srcp
->lorder
, destp
->lorder
);
1007 P_32_COPY(srcp
->bsize
, destp
->bsize
);
1008 P_32_COPY(srcp
->bshift
, destp
->bshift
);
1009 P_32_COPY(srcp
->dsize
, destp
->dsize
);
1010 P_32_COPY(srcp
->ssize
, destp
->ssize
);
1011 P_32_COPY(srcp
->sshift
, destp
->sshift
);
1012 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
1013 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
1014 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
1015 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
1016 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
1017 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
1018 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
1019 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
1020 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
1021 for (i
= 0; i
< NCACHED
; i
++) {
1022 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
1023 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
1037 M_32_SWAP(hdrp
->magic
);
1038 M_32_SWAP(hdrp
->version
);
1039 M_32_SWAP(hdrp
->lorder
);
1040 M_32_SWAP(hdrp
->bsize
);
1041 M_32_SWAP(hdrp
->bshift
);
1042 M_32_SWAP(hdrp
->dsize
);
1043 M_32_SWAP(hdrp
->ssize
);
1044 M_32_SWAP(hdrp
->sshift
);
1045 M_32_SWAP(hdrp
->ovfl_point
);
1046 M_32_SWAP(hdrp
->last_freed
);
1047 M_32_SWAP(hdrp
->max_bucket
);
1048 M_32_SWAP(hdrp
->high_mask
);
1049 M_32_SWAP(hdrp
->low_mask
);
1050 M_32_SWAP(hdrp
->ffactor
);
1051 M_32_SWAP(hdrp
->nkeys
);
1052 M_32_SWAP(hdrp
->hdrpages
);
1053 M_32_SWAP(hdrp
->h_charkey
);
1054 for (i
= 0; i
< NCACHED
; i
++) {
1055 M_32_SWAP(hdrp
->spares
[i
]);
1056 M_16_SWAP(hdrp
->bitmaps
[i
]);