]>
git.saurik.com Git - apple/libc.git/blob - db.subproj/hash.subproj/hash.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
77 static int alloc_segs
__P((HTAB
*, int));
78 static int flush_meta
__P((HTAB
*));
79 static int hash_access
__P((HTAB
*, ACTION
, DBT
*, DBT
*));
80 static int hash_close
__P((DB
*));
81 static int hash_delete
__P((const DB
*, const DBT
*, u_int
));
82 static int hash_fd
__P((const DB
*));
83 static int hash_get
__P((const DB
*, const DBT
*, DBT
*, u_int
));
84 static int hash_put
__P((const DB
*, DBT
*, const DBT
*, u_int
));
85 static void *hash_realloc
__P((SEGMENT
**, int, int));
86 static int hash_seq
__P((const DB
*, DBT
*, DBT
*, u_int
));
87 static int hash_sync
__P((const DB
*, u_int
));
88 static int hdestroy
__P((HTAB
*));
89 static HTAB
*init_hash
__P((HTAB
*, const char *, HASHINFO
*));
90 static int init_htab
__P((HTAB
*, int));
91 #if BYTE_ORDER == LITTLE_ENDIAN
92 static void swap_header
__P((HTAB
*));
93 static void swap_header_copy
__P((HASHHDR
*, HASHHDR
*));
96 /* Fast arithmetic, relying on powers of 2, */
97 #define MOD(x, y) ((x) & ((y) - 1))
99 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
106 #ifdef HASH_STATISTICS
107 long hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
110 /************************** INTERFACE ROUTINES ***************************/
114 __hash_open(file
, flags
, mode
, info
, dflags
)
116 int flags
, mode
, dflags
;
117 const HASHINFO
*info
; /* Special directives for create */
122 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
124 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
129 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
134 * Even if user wants write only, we need to be able to read
135 * the actual file, so we need to open it read/write. But, the
136 * field in the hashp structure needs to be accurate so that
137 * we can check accesses.
139 hashp
->flags
= flags
;
142 if (!file
|| (flags
& O_TRUNC
) ||
143 (stat(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
);
151 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
154 if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
155 RETURN_ERROR(errno
, error1
);
157 /* Table already exists */
158 if (info
&& info
->hash
)
159 hashp
->hash
= info
->hash
;
161 hashp
->hash
= __default_hash
;
163 hdrsize
= read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
164 #if BYTE_ORDER == LITTLE_ENDIAN
168 RETURN_ERROR(errno
, error1
);
169 if (hdrsize
!= sizeof(HASHHDR
))
170 RETURN_ERROR(EFTYPE
, error1
);
171 /* Verify file type, versions and hash function */
172 if (hashp
->MAGIC
!= HASHMAGIC
)
173 RETURN_ERROR(EFTYPE
, error1
);
174 #define OLDHASHVERSION 1
175 if (hashp
->VERSION
!= HASHVERSION
&&
176 hashp
->VERSION
!= OLDHASHVERSION
)
177 RETURN_ERROR(EFTYPE
, error1
);
178 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
179 RETURN_ERROR(EFTYPE
, error1
);
181 * Figure out how many segments we need. Max_Bucket is the
182 * maximum bucket number, so the number of buckets is
185 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
188 if (alloc_segs(hashp
, nsegs
))
190 * If alloc_segs fails, table will have been destroyed
191 * and errno will have been set.
194 /* Read in bitmaps */
195 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
196 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
197 (hashp
->BSHIFT
+ BYTE_SHIFT
);
199 hashp
->nmaps
= bpages
;
200 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_long
*));
203 /* Initialize Buffer Manager */
204 if (info
&& info
->cachesize
)
205 __buf_init(hashp
, info
->cachesize
);
207 __buf_init(hashp
, DEF_BUFSIZE
);
209 hashp
->new_file
= new_table
;
210 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
212 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
218 dbp
->internal
= hashp
;
219 dbp
->close
= hash_close
;
220 dbp
->del
= hash_delete
;
225 dbp
->sync
= hash_sync
;
229 (void)fprintf(stderr
,
230 "%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",
232 "TABLE POINTER ", hashp
,
233 "BUCKET SIZE ", hashp
->BSIZE
,
234 "BUCKET SHIFT ", hashp
->BSHIFT
,
235 "DIRECTORY SIZE ", hashp
->DSIZE
,
236 "SEGMENT SIZE ", hashp
->SGSIZE
,
237 "SEGMENT SHIFT ", hashp
->SSHIFT
,
238 "FILL FACTOR ", hashp
->FFACTOR
,
239 "MAX BUCKET ", hashp
->MAX_BUCKET
,
240 "OVFL POINT ", hashp
->OVFL_POINT
,
241 "LAST FREED ", hashp
->LAST_FREED
,
242 "HIGH MASK ", hashp
->HIGH_MASK
,
243 "LOW MASK ", hashp
->LOW_MASK
,
244 "NSEGS ", hashp
->nsegs
,
245 "NKEYS ", hashp
->NKEYS
);
247 #ifdef HASH_STATISTICS
248 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
254 (void)close(hashp
->fp
);
272 hashp
= (HTAB
*)dbp
->internal
;
273 retval
= hdestroy(hashp
);
287 hashp
= (HTAB
*)dbp
->internal
;
288 if (hashp
->fp
== -1) {
295 /************************** LOCAL CREATION ROUTINES **********************/
297 init_hash(hashp
, file
, info
)
307 hashp
->LORDER
= BYTE_ORDER
;
308 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
309 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
310 hashp
->SGSIZE
= DEF_SEGSIZE
;
311 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
312 hashp
->DSIZE
= DEF_DIRSIZE
;
313 hashp
->FFACTOR
= DEF_FFACTOR
;
314 hashp
->hash
= __default_hash
;
315 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
316 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
318 /* Fix bucket size to be optimal for file system */
320 if (stat(file
, &statbuf
))
322 hashp
->BSIZE
= statbuf
.st_blksize
;
323 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
328 /* Round pagesize up to power of 2 */
329 hashp
->BSHIFT
= __log2(info
->bsize
);
330 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
331 if (hashp
->BSIZE
> MAX_BSIZE
) {
337 hashp
->FFACTOR
= info
->ffactor
;
339 hashp
->hash
= info
->hash
;
343 if (info
->lorder
!= BIG_ENDIAN
&&
344 info
->lorder
!= LITTLE_ENDIAN
) {
348 hashp
->LORDER
= info
->lorder
;
351 /* init_htab should destroy the table and set errno if it fails */
352 if (init_htab(hashp
, nelem
))
358 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
359 * the table and set errno, so we just pass the error information along.
361 * Returns 0 on No Error
364 init_htab(hashp
, nelem
)
368 register int nbuckets
, nsegs
;
372 * Divide number of elements by the fill factor and determine a
373 * desired number of buckets. Allocate space for the next greater
374 * power of two number of buckets.
376 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
378 l2
= __log2(MAX(nelem
, 2));
381 hashp
->SPARES
[l2
] = l2
+ 1;
382 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
383 hashp
->OVFL_POINT
= l2
;
384 hashp
->LAST_FREED
= 2;
386 /* First bitmap page is at: splitpoint l2 page offset 1 */
387 if (__init_bitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
390 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
391 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
392 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
395 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
396 nsegs
= 1 << __log2(nsegs
);
398 if (nsegs
> hashp
->DSIZE
)
399 hashp
->DSIZE
= nsegs
;
400 return (alloc_segs(hashp
, nsegs
));
403 /********************** DESTROY/CLOSE ROUTINES ************************/
406 * Flushes any changes to the file if necessary and destroys the hashp
407 * structure, freeing all allocated space.
417 #ifdef HASH_STATISTICS
418 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
419 hash_accesses
, hash_collisions
);
420 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
422 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
424 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
425 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
427 for (i
= 0; i
< NCACHED
; i
++)
428 (void)fprintf(stderr
,
429 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
432 * Call on buffer manager to free buffers, and if required,
433 * write them to disk.
435 if (__buf_free(hashp
, 1, hashp
->save_file
))
438 free(*hashp
->dir
); /* Free initial segments */
439 /* Free extra segments */
440 while (hashp
->exsegs
--)
441 free(hashp
->dir
[--hashp
->nsegs
]);
444 if (flush_meta(hashp
) && !save_errno
)
447 for (i
= 0; i
< hashp
->nmaps
; i
++)
449 free(hashp
->mapp
[i
]);
452 (void)close(hashp
->fp
);
463 * Write modified pages to disk
470 hash_sync(dbp
, flags
)
484 hashp
= (HTAB
*)dbp
->internal
;
485 if (!hashp
->save_file
)
487 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
496 * -1 indicates that errno should be set
503 #if BYTE_ORDER == LITTLE_ENDIAN
508 if (!hashp
->save_file
)
510 hashp
->MAGIC
= HASHMAGIC
;
511 hashp
->VERSION
= HASHVERSION
;
512 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
516 #if BYTE_ORDER == LITTLE_ENDIAN
518 swap_header_copy(&hashp
->hdr
, whdrp
);
520 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
521 ((wsize
= write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
524 if (wsize
!= sizeof(HASHHDR
)) {
526 hashp
->error
= errno
;
529 for (i
= 0; i
< NCACHED
; i
++)
531 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
532 hashp
->BITMAPS
[i
], 0, 1))
537 /*******************************SEARCH ROUTINES *****************************/
539 * All the access routines return
543 * 1 to indicate an external ERROR (i.e. key not found, etc)
544 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
547 hash_get(dbp
, key
, data
, flag
)
555 hashp
= (HTAB
*)dbp
->internal
;
557 hashp
->error
= errno
= EINVAL
;
560 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
564 hash_put(dbp
, key
, data
, flag
)
572 hashp
= (HTAB
*)dbp
->internal
;
573 if (flag
&& flag
!= R_NOOVERWRITE
) {
574 hashp
->error
= errno
= EINVAL
;
577 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
578 hashp
->error
= errno
= EPERM
;
581 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
582 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
586 hash_delete(dbp
, key
, flag
)
589 u_int flag
; /* Ignored */
593 hashp
= (HTAB
*)dbp
->internal
;
594 if (flag
&& flag
!= R_CURSOR
) {
595 hashp
->error
= errno
= EINVAL
;
598 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
599 hashp
->error
= errno
= EPERM
;
602 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
606 * Assume that hashp has been set in wrapper routine.
609 hash_access(hashp
, action
, key
, val
)
614 register BUFHEAD
*rbufp
;
615 BUFHEAD
*bufp
, *save_bufp
;
616 register u_short
*bp
;
617 register int n
, ndx
, off
, size
;
621 #ifdef HASH_STATISTICS
627 kp
= (char *)key
->data
;
628 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
633 /* Pin the bucket chain */
634 rbufp
->flags
|= BUF_PIN
;
635 for (bp
= (u_short
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
636 if (bp
[1] >= REAL_KEY
) {
637 /* Real key/data pair */
638 if (size
== off
- *bp
&&
639 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
642 #ifdef HASH_STATISTICS
647 } else if (bp
[1] == OVFLPAGE
) {
648 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
650 save_bufp
->flags
&= ~BUF_PIN
;
654 bp
= (u_short
*)rbufp
->page
;
658 } else if (bp
[1] < REAL_KEY
) {
660 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
665 __find_last_page(hashp
, &bufp
))) {
670 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
672 save_bufp
->flags
&= ~BUF_PIN
;
676 bp
= (u_short
*)rbufp
->page
;
681 save_bufp
->flags
&= ~BUF_PIN
;
690 if (__addel(hashp
, rbufp
, key
, val
)) {
691 save_bufp
->flags
&= ~BUF_PIN
;
694 save_bufp
->flags
&= ~BUF_PIN
;
700 save_bufp
->flags
&= ~BUF_PIN
;
707 save_bufp
->flags
&= ~BUF_PIN
;
710 bp
= (u_short
*)rbufp
->page
;
711 if (bp
[ndx
+ 1] < REAL_KEY
) {
712 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
715 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
716 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
720 if ((__delpair(hashp
, rbufp
, ndx
)) ||
721 (__addel(hashp
, rbufp
, key
, val
))) {
722 save_bufp
->flags
&= ~BUF_PIN
;
727 if (__delpair(hashp
, rbufp
, ndx
))
733 save_bufp
->flags
&= ~BUF_PIN
;
738 hash_seq(dbp
, key
, data
, flag
)
743 register u_int bucket
;
744 register BUFHEAD
*bufp
;
748 hashp
= (HTAB
*)dbp
->internal
;
749 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
750 hashp
->error
= errno
= EINVAL
;
753 #ifdef HASH_STATISTICS
756 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
762 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
763 if (!(bufp
= hashp
->cpage
)) {
764 for (bucket
= hashp
->cbucket
;
765 bucket
<= hashp
->MAX_BUCKET
;
766 bucket
++, hashp
->cndx
= 1) {
767 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
771 bp
= (u_short
*)bufp
->page
;
775 hashp
->cbucket
= bucket
;
776 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
781 bp
= (u_short
*)hashp
->cpage
->page
;
787 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
788 bufp
= hashp
->cpage
=
789 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
792 bp
= (u_short
*)(bufp
->page
);
801 if (bp
[ndx
+ 1] < REAL_KEY
) {
802 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
805 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
806 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
807 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
808 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
820 /********************************* UTILITIES ************************/
828 __expand_table(hashp
)
831 u_int old_bucket
, new_bucket
;
832 int dirsize
, new_segnum
, spare_ndx
;
834 #ifdef HASH_STATISTICS
837 new_bucket
= ++hashp
->MAX_BUCKET
;
838 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
840 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
842 /* Check if we need a new segment */
843 if (new_segnum
>= hashp
->nsegs
) {
844 /* Check if we need to expand directory */
845 if (new_segnum
>= hashp
->DSIZE
) {
846 /* Reallocate directory */
847 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
848 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
850 hashp
->DSIZE
= dirsize
<< 1;
852 if ((hashp
->dir
[new_segnum
] =
853 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
859 * If the split point is increasing (MAX_BUCKET's log base 2
860 * * increases), we need to copy the current contents of the spare
861 * split bucket to the next bucket.
863 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
864 if (spare_ndx
> hashp
->OVFL_POINT
) {
865 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
866 hashp
->OVFL_POINT
= spare_ndx
;
869 if (new_bucket
> hashp
->HIGH_MASK
) {
870 /* Starting a new doubling */
871 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
872 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
874 /* Relocate records to the new bucket */
875 return (__split_page(hashp
, old_bucket
, new_bucket
));
879 * If realloc guarantees that the pointer is not destroyed if the realloc
880 * fails, then this routine can go away.
883 hash_realloc(p_ptr
, oldsize
, newsize
)
885 int oldsize
, newsize
;
889 if (p
= malloc(newsize
)) {
890 memmove(p
, *p_ptr
, oldsize
);
891 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
899 __call_hash(hashp
, k
, len
)
906 n
= hashp
->hash(k
, len
);
907 bucket
= n
& hashp
->HIGH_MASK
;
908 if (bucket
> hashp
->MAX_BUCKET
)
909 bucket
= bucket
& hashp
->LOW_MASK
;
914 * Allocate segment table. On error, destroy the table and set errno.
916 * Returns 0 on success
919 alloc_segs(hashp
, nsegs
)
924 register SEGMENT store
;
929 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
931 (void)hdestroy(hashp
);
935 /* Allocate segments */
937 (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
939 (void)hdestroy(hashp
);
943 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
944 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
948 #if BYTE_ORDER == LITTLE_ENDIAN
950 * Hashp->hdr needs to be byteswapped.
953 swap_header_copy(srcp
, destp
)
954 HASHHDR
*srcp
, *destp
;
958 P_32_COPY(srcp
->magic
, destp
->magic
);
959 P_32_COPY(srcp
->version
, destp
->version
);
960 P_32_COPY(srcp
->lorder
, destp
->lorder
);
961 P_32_COPY(srcp
->bsize
, destp
->bsize
);
962 P_32_COPY(srcp
->bshift
, destp
->bshift
);
963 P_32_COPY(srcp
->dsize
, destp
->dsize
);
964 P_32_COPY(srcp
->ssize
, destp
->ssize
);
965 P_32_COPY(srcp
->sshift
, destp
->sshift
);
966 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
967 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
968 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
969 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
970 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
971 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
972 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
973 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
974 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
975 for (i
= 0; i
< NCACHED
; i
++) {
976 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
977 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
990 M_32_SWAP(hdrp
->magic
);
991 M_32_SWAP(hdrp
->version
);
992 M_32_SWAP(hdrp
->lorder
);
993 M_32_SWAP(hdrp
->bsize
);
994 M_32_SWAP(hdrp
->bshift
);
995 M_32_SWAP(hdrp
->dsize
);
996 M_32_SWAP(hdrp
->ssize
);
997 M_32_SWAP(hdrp
->sshift
);
998 M_32_SWAP(hdrp
->ovfl_point
);
999 M_32_SWAP(hdrp
->last_freed
);
1000 M_32_SWAP(hdrp
->max_bucket
);
1001 M_32_SWAP(hdrp
->high_mask
);
1002 M_32_SWAP(hdrp
->low_mask
);
1003 M_32_SWAP(hdrp
->ffactor
);
1004 M_32_SWAP(hdrp
->nkeys
);
1005 M_32_SWAP(hdrp
->hdrpages
);
1006 M_32_SWAP(hdrp
->h_charkey
);
1007 for (i
= 0; i
< NCACHED
; i
++) {
1008 M_32_SWAP(hdrp
->spares
[i
]);
1009 M_16_SWAP(hdrp
->bitmaps
[i
]);