]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash-fbsd.c
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/db/hash/hash.c,v 1.12 2004/09/10 05:41:41 kuriyama Exp $");
43 #include "namespace.h"
44 #include <sys/param.h>
56 #include "un-namespace.h"
61 #include "hash_extern.h"
63 static int alloc_segs(HTAB
*, int);
64 static int flush_meta(HTAB
*);
65 static int hash_access(HTAB
*, ACTION
, DBT
*, DBT
*);
66 static int hash_close(DB
*);
67 static int hash_delete(const DB
*, const DBT
*, u_int32_t
);
68 static int hash_fd(const DB
*);
69 static int hash_get(const DB
*, const DBT
*, DBT
*, u_int32_t
);
70 static int hash_put(const DB
*, DBT
*, const DBT
*, u_int32_t
);
71 static void *hash_realloc(SEGMENT
**, int, int);
72 static int hash_seq(const DB
*, DBT
*, DBT
*, u_int32_t
);
73 static int hash_sync(const DB
*, u_int32_t
);
74 static int hdestroy(HTAB
*);
75 static HTAB
*init_hash(HTAB
*, const char *, HASHINFO
*);
76 static int init_htab(HTAB
*, int);
77 #if BYTE_ORDER == LITTLE_ENDIAN
78 static void swap_header(HTAB
*);
79 static void swap_header_copy(HASHHDR
*, HASHHDR
*);
82 /* Fast arithmetic, relying on powers of 2, */
83 #define MOD(x, y) ((x) & ((y) - 1))
85 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
92 #ifdef HASH_STATISTICS
93 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
96 /************************** INTERFACE ROUTINES ***************************/
100 __hash_open(file
, flags
, mode
, info
, dflags
)
102 int flags
, mode
, dflags
;
103 const HASHINFO
*info
; /* Special directives for create */
108 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
110 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
111 flags
+= O_RDWR
- O_WRONLY
; /* POSIX */
114 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
119 * Even if user wants write only, we need to be able to read
120 * the actual file, so we need to open it read/write. But, the
121 * field in the hashp structure needs to be accurate so that
122 * we can check accesses.
124 hashp
->flags
= flags
;
127 if (!file
|| (flags
& O_TRUNC
) ||
128 (stat(file
, &statbuf
) && (errno
== ENOENT
))) {
130 errno
= 0; /* Just in case someone looks at errno */
134 if ((hashp
->fp
= _open(file
, flags
, mode
)) == -1)
135 RETURN_ERROR(errno
, error0
);
137 /* if the .db file is empty, and we had permission to create
138 a new .db file, then reinitialize the database */
139 if ((flags
& O_CREAT
) &&
140 _fstat(hashp
->fp
, &statbuf
) == 0 && statbuf
.st_size
== 0)
143 (void)_fcntl(hashp
->fp
, F_SETFD
, 1);
146 if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
147 RETURN_ERROR(errno
, error1
);
149 /* Table already exists */
150 if (info
&& info
->hash
)
151 hashp
->hash
= info
->hash
;
153 hashp
->hash
= __default_hash
;
155 hdrsize
= _read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
156 #if BYTE_ORDER == LITTLE_ENDIAN
160 RETURN_ERROR(errno
, error1
);
161 if (hdrsize
!= sizeof(HASHHDR
))
162 RETURN_ERROR(EFTYPE
, error1
);
163 /* Verify file type, versions and hash function */
164 if (hashp
->MAGIC
!= HASHMAGIC
)
165 RETURN_ERROR(EFTYPE
, error1
);
166 #define OLDHASHVERSION 1
167 if (hashp
->VERSION
!= HASHVERSION
&&
168 hashp
->VERSION
!= OLDHASHVERSION
)
169 RETURN_ERROR(EFTYPE
, error1
);
170 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
171 RETURN_ERROR(EFTYPE
, error1
);
173 * Figure out how many segments we need. Max_Bucket is the
174 * maximum bucket number, so the number of buckets is
177 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
180 if (alloc_segs(hashp
, nsegs
))
182 * If alloc_segs fails, table will have been destroyed
183 * and errno will have been set.
186 /* Read in bitmaps */
187 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
188 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
189 (hashp
->BSHIFT
+ BYTE_SHIFT
);
191 hashp
->nmaps
= bpages
;
192 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_int32_t
*));
195 /* Initialize Buffer Manager */
196 if (info
&& info
->cachesize
)
197 __buf_init(hashp
, info
->cachesize
);
199 __buf_init(hashp
, DEF_BUFSIZE
);
201 hashp
->new_file
= new_table
;
202 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
204 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
210 dbp
->internal
= hashp
;
211 dbp
->close
= hash_close
;
212 dbp
->del
= hash_delete
;
217 dbp
->sync
= hash_sync
;
221 (void)fprintf(stderr
,
222 "%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 "TABLE POINTER ", hashp
,
225 "BUCKET SIZE ", hashp
->BSIZE
,
226 "BUCKET SHIFT ", hashp
->BSHIFT
,
227 "DIRECTORY SIZE ", hashp
->DSIZE
,
228 "SEGMENT SIZE ", hashp
->SGSIZE
,
229 "SEGMENT SHIFT ", hashp
->SSHIFT
,
230 "FILL FACTOR ", hashp
->FFACTOR
,
231 "MAX BUCKET ", hashp
->MAX_BUCKET
,
232 "OVFL POINT ", hashp
->OVFL_POINT
,
233 "LAST FREED ", hashp
->LAST_FREED
,
234 "HIGH MASK ", hashp
->HIGH_MASK
,
235 "LOW MASK ", hashp
->LOW_MASK
,
236 "NSEGS ", hashp
->nsegs
,
237 "NKEYS ", hashp
->NKEYS
);
239 #ifdef HASH_STATISTICS
240 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
246 (void)_close(hashp
->fp
);
264 hashp
= (HTAB
*)dbp
->internal
;
265 retval
= hdestroy(hashp
);
279 hashp
= (HTAB
*)dbp
->internal
;
280 if (hashp
->fp
== -1) {
287 /************************** LOCAL CREATION ROUTINES **********************/
289 init_hash(hashp
, file
, info
)
299 hashp
->LORDER
= BYTE_ORDER
;
300 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
301 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
302 hashp
->SGSIZE
= DEF_SEGSIZE
;
303 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
304 hashp
->DSIZE
= DEF_DIRSIZE
;
305 hashp
->FFACTOR
= DEF_FFACTOR
;
306 hashp
->hash
= __default_hash
;
307 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
308 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
310 /* Fix bucket size to be optimal for file system */
312 if (stat(file
, &statbuf
))
314 hashp
->BSIZE
= statbuf
.st_blksize
;
315 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
320 /* Round pagesize up to power of 2 */
321 hashp
->BSHIFT
= __log2(info
->bsize
);
322 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
323 if (hashp
->BSIZE
> MAX_BSIZE
) {
329 hashp
->FFACTOR
= info
->ffactor
;
331 hashp
->hash
= info
->hash
;
335 if (info
->lorder
!= BIG_ENDIAN
&&
336 info
->lorder
!= LITTLE_ENDIAN
) {
340 hashp
->LORDER
= info
->lorder
;
343 /* init_htab should destroy the table and set errno if it fails */
344 if (init_htab(hashp
, nelem
))
350 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
351 * the table and set errno, so we just pass the error information along.
353 * Returns 0 on No Error
356 init_htab(hashp
, nelem
)
364 * Divide number of elements by the fill factor and determine a
365 * desired number of buckets. Allocate space for the next greater
366 * power of two number of buckets.
368 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
370 l2
= __log2(MAX(nelem
, 2));
373 hashp
->SPARES
[l2
] = l2
+ 1;
374 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
375 hashp
->OVFL_POINT
= l2
;
376 hashp
->LAST_FREED
= 2;
378 /* First bitmap page is at: splitpoint l2 page offset 1 */
379 if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
382 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
383 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
384 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
387 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
388 nsegs
= 1 << __log2(nsegs
);
390 if (nsegs
> hashp
->DSIZE
)
391 hashp
->DSIZE
= nsegs
;
392 return (alloc_segs(hashp
, nsegs
));
395 /********************** DESTROY/CLOSE ROUTINES ************************/
398 * Flushes any changes to the file if necessary and destroys the hashp
399 * structure, freeing all allocated space.
409 #ifdef HASH_STATISTICS
410 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
411 hash_accesses
, hash_collisions
);
412 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
414 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
416 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
417 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
419 for (i
= 0; i
< NCACHED
; i
++)
420 (void)fprintf(stderr
,
421 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
424 * Call on buffer manager to free buffers, and if required,
425 * write them to disk.
427 if (__buf_free(hashp
, 1, hashp
->save_file
))
430 free(*hashp
->dir
); /* Free initial segments */
431 /* Free extra segments */
432 while (hashp
->exsegs
--)
433 free(hashp
->dir
[--hashp
->nsegs
]);
436 if (flush_meta(hashp
) && !save_errno
)
439 for (i
= 0; i
< hashp
->nmaps
; i
++)
441 free(hashp
->mapp
[i
]);
444 (void)_close(hashp
->fp
);
455 * Write modified pages to disk
462 hash_sync(dbp
, flags
)
476 hashp
= (HTAB
*)dbp
->internal
;
477 if (!hashp
->save_file
)
479 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
488 * -1 indicates that errno should be set
495 #if BYTE_ORDER == LITTLE_ENDIAN
500 if (!hashp
->save_file
)
502 hashp
->MAGIC
= HASHMAGIC
;
503 hashp
->VERSION
= HASHVERSION
;
504 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
508 #if BYTE_ORDER == LITTLE_ENDIAN
510 swap_header_copy(&hashp
->hdr
, whdrp
);
512 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
513 ((wsize
= _write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
516 if (wsize
!= sizeof(HASHHDR
)) {
518 hashp
->error
= errno
;
521 for (i
= 0; i
< NCACHED
; i
++)
523 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
524 hashp
->BITMAPS
[i
], 0, 1))
529 /*******************************SEARCH ROUTINES *****************************/
531 * All the access routines return
535 * 1 to indicate an external ERROR (i.e. key not found, etc)
536 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
539 hash_get(dbp
, key
, data
, flag
)
547 hashp
= (HTAB
*)dbp
->internal
;
549 hashp
->error
= errno
= EINVAL
;
552 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
556 hash_put(dbp
, key
, data
, flag
)
564 hashp
= (HTAB
*)dbp
->internal
;
565 if (flag
&& flag
!= R_NOOVERWRITE
) {
566 hashp
->error
= EINVAL
;
570 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
571 hashp
->error
= errno
= EPERM
;
574 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
575 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
579 hash_delete(dbp
, key
, flag
)
582 u_int32_t flag
; /* Ignored */
586 hashp
= (HTAB
*)dbp
->internal
;
587 if (flag
&& flag
!= R_CURSOR
) {
588 hashp
->error
= errno
= EINVAL
;
591 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
592 hashp
->error
= errno
= EPERM
;
595 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
599 * Assume that hashp has been set in wrapper routine.
602 hash_access(hashp
, action
, key
, val
)
608 BUFHEAD
*bufp
, *save_bufp
;
610 int n
, ndx
, off
, size
;
614 #ifdef HASH_STATISTICS
620 kp
= (char *)key
->data
;
621 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
626 /* Pin the bucket chain */
627 rbufp
->flags
|= BUF_PIN
;
628 for (bp
= (u_int16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
629 if (bp
[1] >= REAL_KEY
) {
630 /* Real key/data pair */
631 if (size
== off
- *bp
&&
632 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
635 #ifdef HASH_STATISTICS
640 } else if (bp
[1] == OVFLPAGE
) {
641 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
643 save_bufp
->flags
&= ~BUF_PIN
;
647 bp
= (u_int16_t
*)rbufp
->page
;
651 } else if (bp
[1] < REAL_KEY
) {
653 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
658 __find_last_page(hashp
, &bufp
))) {
663 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
665 save_bufp
->flags
&= ~BUF_PIN
;
669 bp
= (u_int16_t
*)rbufp
->page
;
674 save_bufp
->flags
&= ~BUF_PIN
;
683 if (__addel(hashp
, rbufp
, key
, val
)) {
684 save_bufp
->flags
&= ~BUF_PIN
;
687 save_bufp
->flags
&= ~BUF_PIN
;
693 save_bufp
->flags
&= ~BUF_PIN
;
700 save_bufp
->flags
&= ~BUF_PIN
;
703 bp
= (u_int16_t
*)rbufp
->page
;
704 if (bp
[ndx
+ 1] < REAL_KEY
) {
705 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
708 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
709 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
713 if ((__delpair(hashp
, rbufp
, ndx
)) ||
714 (__addel(hashp
, rbufp
, key
, val
))) {
715 save_bufp
->flags
&= ~BUF_PIN
;
720 if (__delpair(hashp
, rbufp
, ndx
))
724 LIBC_ABORT("illegal action (%d)", action
);
726 save_bufp
->flags
&= ~BUF_PIN
;
731 hash_seq(dbp
, key
, data
, flag
)
741 hashp
= (HTAB
*)dbp
->internal
;
742 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
743 hashp
->error
= errno
= EINVAL
;
746 #ifdef HASH_STATISTICS
749 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
755 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
756 if (!(bufp
= hashp
->cpage
)) {
757 for (bucket
= hashp
->cbucket
;
758 bucket
<= hashp
->MAX_BUCKET
;
759 bucket
++, hashp
->cndx
= 1) {
760 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
764 bp
= (u_int16_t
*)bufp
->page
;
768 hashp
->cbucket
= bucket
;
769 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
774 bp
= (u_int16_t
*)hashp
->cpage
->page
;
780 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
781 bufp
= hashp
->cpage
=
782 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
785 bp
= (u_int16_t
*)(bufp
->page
);
794 if (bp
[ndx
+ 1] < REAL_KEY
) {
795 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
798 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
799 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
800 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
801 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
813 /********************************* UTILITIES ************************/
821 __expand_table(hashp
)
824 u_int32_t old_bucket
, new_bucket
;
825 int dirsize
, new_segnum
, spare_ndx
;
827 #ifdef HASH_STATISTICS
830 new_bucket
= ++hashp
->MAX_BUCKET
;
831 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
833 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
835 /* Check if we need a new segment */
836 if (new_segnum
>= hashp
->nsegs
) {
837 /* Check if we need to expand directory */
838 if (new_segnum
>= hashp
->DSIZE
) {
839 /* Reallocate directory */
840 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
841 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
843 hashp
->DSIZE
= dirsize
<< 1;
845 if ((hashp
->dir
[new_segnum
] =
846 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
852 * If the split point is increasing (MAX_BUCKET's log base 2
853 * * increases), we need to copy the current contents of the spare
854 * split bucket to the next bucket.
856 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
857 if (spare_ndx
> hashp
->OVFL_POINT
) {
858 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
859 hashp
->OVFL_POINT
= spare_ndx
;
862 if (new_bucket
> hashp
->HIGH_MASK
) {
863 /* Starting a new doubling */
864 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
865 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
867 /* Relocate records to the new bucket */
868 return (__split_page(hashp
, old_bucket
, new_bucket
));
872 * If realloc guarantees that the pointer is not destroyed if the realloc
873 * fails, then this routine can go away.
876 hash_realloc(p_ptr
, oldsize
, newsize
)
878 int oldsize
, newsize
;
882 if ( (p
= malloc(newsize
)) ) {
883 memmove(p
, *p_ptr
, oldsize
);
884 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
892 __call_hash(hashp
, k
, len
)
899 n
= hashp
->hash(k
, len
);
900 bucket
= n
& hashp
->HIGH_MASK
;
901 if (bucket
> hashp
->MAX_BUCKET
)
902 bucket
= bucket
& hashp
->LOW_MASK
;
907 * Allocate segment table. On error, destroy the table and set errno.
909 * Returns 0 on success
912 alloc_segs(hashp
, nsegs
)
922 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
924 (void)hdestroy(hashp
);
928 /* Allocate segments */
930 (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
932 (void)hdestroy(hashp
);
936 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
937 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
941 #if BYTE_ORDER == LITTLE_ENDIAN
943 * Hashp->hdr needs to be byteswapped.
946 swap_header_copy(srcp
, destp
)
947 HASHHDR
*srcp
, *destp
;
951 P_32_COPY(srcp
->magic
, destp
->magic
);
952 P_32_COPY(srcp
->version
, destp
->version
);
953 P_32_COPY(srcp
->lorder
, destp
->lorder
);
954 P_32_COPY(srcp
->bsize
, destp
->bsize
);
955 P_32_COPY(srcp
->bshift
, destp
->bshift
);
956 P_32_COPY(srcp
->dsize
, destp
->dsize
);
957 P_32_COPY(srcp
->ssize
, destp
->ssize
);
958 P_32_COPY(srcp
->sshift
, destp
->sshift
);
959 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
960 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
961 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
962 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
963 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
964 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
965 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
966 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
967 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
968 for (i
= 0; i
< NCACHED
; i
++) {
969 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
970 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
983 M_32_SWAP(hdrp
->magic
);
984 M_32_SWAP(hdrp
->version
);
985 M_32_SWAP(hdrp
->lorder
);
986 M_32_SWAP(hdrp
->bsize
);
987 M_32_SWAP(hdrp
->bshift
);
988 M_32_SWAP(hdrp
->dsize
);
989 M_32_SWAP(hdrp
->ssize
);
990 M_32_SWAP(hdrp
->sshift
);
991 M_32_SWAP(hdrp
->ovfl_point
);
992 M_32_SWAP(hdrp
->last_freed
);
993 M_32_SWAP(hdrp
->max_bucket
);
994 M_32_SWAP(hdrp
->high_mask
);
995 M_32_SWAP(hdrp
->low_mask
);
996 M_32_SWAP(hdrp
->ffactor
);
997 M_32_SWAP(hdrp
->nkeys
);
998 M_32_SWAP(hdrp
->hdrpages
);
999 M_32_SWAP(hdrp
->h_charkey
);
1000 for (i
= 0; i
< NCACHED
; i
++) {
1001 M_32_SWAP(hdrp
->spares
[i
]);
1002 M_16_SWAP(hdrp
->bitmaps
[i
]);