]>
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 * 4. 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 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/db/hash/hash.c,v 1.21 2009/03/28 07:20:39 delphij Exp $");
39 #include "namespace.h"
40 #include <sys/param.h>
52 #include "un-namespace.h"
57 #include "hash_extern.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_int32_t
);
64 static int hash_fd(const DB
*);
65 static int hash_get(const DB
*, const DBT
*, DBT
*, u_int32_t
);
66 static int hash_put(const DB
*, DBT
*, const DBT
*, u_int32_t
);
67 static void *hash_realloc(SEGMENT
**, int, int);
68 static int hash_seq(const DB
*, DBT
*, DBT
*, u_int32_t
);
69 static int hash_sync(const DB
*, u_int32_t
);
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 /* Fast arithmetic, relying on powers of 2, */
79 #define MOD(x, y) ((x) & ((y) - 1))
81 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
88 #ifdef HASH_STATISTICS
89 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
92 /************************** INTERFACE ROUTINES ***************************/
97 __hash_open(const char *file
, int flags
, int mode
,
98 const HASHINFO
*info
, /* Special directives for create */
104 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
106 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
107 flags
+= O_RDWR
- O_WRONLY
; /* POSIX */
110 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
115 * Even if user wants write only, we need to be able to read
116 * the actual file, so we need to open it read/write. But, the
117 * field in the hashp structure needs to be accurate so that
118 * we can check accesses.
120 hashp
->flags
= flags
;
123 if ((hashp
->fp
= _open(file
, flags
, mode
)) == -1)
124 RETURN_ERROR(errno
, error0
);
125 (void)_fcntl(hashp
->fp
, F_SETFD
, 1);
126 new_table
= _fstat(hashp
->fp
, &statbuf
) == 0 &&
127 statbuf
.st_size
== 0 &&
128 ((flags
& O_ACCMODE
) != O_RDONLY
|| (flags
& O_CREAT
) != 0);
133 if (!(hashp
= init_hash(hashp
, file
, info
)))
134 RETURN_ERROR(errno
, error1
);
136 /* Table already exists */
137 if (info
&& info
->hash
)
138 hashp
->hash
= info
->hash
;
140 hashp
->hash
= __default_hash
;
142 hdrsize
= _read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
143 #if BYTE_ORDER == LITTLE_ENDIAN
147 RETURN_ERROR(errno
, error1
);
148 if (hdrsize
!= sizeof(HASHHDR
))
149 RETURN_ERROR(EFTYPE
, error1
);
150 /* Verify file type, versions and hash function */
151 if (hashp
->MAGIC
!= HASHMAGIC
)
152 RETURN_ERROR(EFTYPE
, error1
);
153 #define OLDHASHVERSION 1
154 if (hashp
->VERSION
!= HASHVERSION
&&
155 hashp
->VERSION
!= OLDHASHVERSION
)
156 RETURN_ERROR(EFTYPE
, error1
);
157 if ((int32_t)hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
158 RETURN_ERROR(EFTYPE
, error1
);
160 * Figure out how many segments we need. Max_Bucket is the
161 * maximum bucket number, so the number of buckets is
164 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
166 if (alloc_segs(hashp
, nsegs
))
168 * If alloc_segs fails, table will have been destroyed
169 * and errno will have been set.
172 /* Read in bitmaps */
173 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
174 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
175 (hashp
->BSHIFT
+ BYTE_SHIFT
);
177 hashp
->nmaps
= bpages
;
178 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_int32_t
*));
181 /* Initialize Buffer Manager */
182 if (info
&& info
->cachesize
)
183 __buf_init(hashp
, info
->cachesize
);
185 __buf_init(hashp
, DEF_BUFSIZE
);
187 hashp
->new_file
= new_table
;
188 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
190 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
196 dbp
->internal
= hashp
;
197 dbp
->close
= hash_close
;
198 dbp
->del
= hash_delete
;
203 dbp
->sync
= hash_sync
;
207 (void)fprintf(stderr
,
208 "%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",
210 "TABLE POINTER ", hashp
,
211 "BUCKET SIZE ", hashp
->BSIZE
,
212 "BUCKET SHIFT ", hashp
->BSHIFT
,
213 "DIRECTORY SIZE ", hashp
->DSIZE
,
214 "SEGMENT SIZE ", hashp
->SGSIZE
,
215 "SEGMENT SHIFT ", hashp
->SSHIFT
,
216 "FILL FACTOR ", hashp
->FFACTOR
,
217 "MAX BUCKET ", hashp
->MAX_BUCKET
,
218 "OVFL POINT ", hashp
->OVFL_POINT
,
219 "LAST FREED ", hashp
->LAST_FREED
,
220 "HIGH MASK ", hashp
->HIGH_MASK
,
221 "LOW MASK ", hashp
->LOW_MASK
,
222 "NSEGS ", hashp
->nsegs
,
223 "NKEYS ", hashp
->NKEYS
);
225 #ifdef HASH_STATISTICS
226 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
232 (void)_close(hashp
->fp
);
249 hashp
= (HTAB
*)dbp
->internal
;
250 retval
= hdestroy(hashp
);
256 hash_fd(const DB
*dbp
)
263 hashp
= (HTAB
*)dbp
->internal
;
264 if (hashp
->fp
== -1) {
271 /************************** LOCAL CREATION ROUTINES **********************/
273 init_hash(HTAB
*hashp
, const char *file
, const HASHINFO
*info
)
280 hashp
->LORDER
= BYTE_ORDER
;
281 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
282 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
283 hashp
->SGSIZE
= DEF_SEGSIZE
;
284 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
285 hashp
->DSIZE
= DEF_DIRSIZE
;
286 hashp
->FFACTOR
= DEF_FFACTOR
;
287 hashp
->hash
= __default_hash
;
288 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
289 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
291 /* Fix bucket size to be optimal for file system */
293 if (stat(file
, &statbuf
))
295 hashp
->BSIZE
= statbuf
.st_blksize
;
296 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
301 /* Round pagesize up to power of 2 */
302 hashp
->BSHIFT
= __log2(info
->bsize
);
303 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
304 if (hashp
->BSIZE
> MAX_BSIZE
) {
310 hashp
->FFACTOR
= info
->ffactor
;
312 hashp
->hash
= info
->hash
;
316 if (info
->lorder
!= BIG_ENDIAN
&&
317 info
->lorder
!= LITTLE_ENDIAN
) {
321 hashp
->LORDER
= info
->lorder
;
324 /* init_htab should destroy the table and set errno if it fails */
325 if (init_htab(hashp
, nelem
))
331 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
332 * the table and set errno, so we just pass the error information along.
334 * Returns 0 on No Error
337 init_htab(HTAB
*hashp
, int nelem
)
339 int nbuckets
, nsegs
, l2
;
342 * Divide number of elements by the fill factor and determine a
343 * desired number of buckets. Allocate space for the next greater
344 * power of two number of buckets.
346 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
348 l2
= __log2(MAX(nelem
, 2));
351 hashp
->SPARES
[l2
] = l2
+ 1;
352 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
353 hashp
->OVFL_POINT
= l2
;
354 hashp
->LAST_FREED
= 2;
356 /* First bitmap page is at: splitpoint l2 page offset 1 */
357 if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
360 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
361 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
362 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
365 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
366 nsegs
= 1 << __log2(nsegs
);
368 if (nsegs
> hashp
->DSIZE
)
369 hashp
->DSIZE
= nsegs
;
370 return (alloc_segs(hashp
, nsegs
));
373 /********************** DESTROY/CLOSE ROUTINES ************************/
376 * Flushes any changes to the file if necessary and destroys the hashp
377 * structure, freeing all allocated space.
380 hdestroy(HTAB
*hashp
)
386 #ifdef HASH_STATISTICS
387 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
388 hash_accesses
, hash_collisions
);
389 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
391 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
393 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
394 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
396 for (i
= 0; i
< NCACHED
; i
++)
397 (void)fprintf(stderr
,
398 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
401 * Call on buffer manager to free buffers, and if required,
402 * write them to disk.
404 if (__buf_free(hashp
, 1, hashp
->save_file
))
407 free(*hashp
->dir
); /* Free initial segments */
408 /* Free extra segments */
409 while (hashp
->exsegs
--)
410 free(hashp
->dir
[--hashp
->nsegs
]);
413 if (flush_meta(hashp
) && !save_errno
)
416 for (i
= 0; i
< hashp
->nmaps
; i
++)
418 free(hashp
->mapp
[i
]);
420 free(hashp
->tmp_key
);
422 free(hashp
->tmp_buf
);
425 (void)_close(hashp
->fp
);
436 * Write modified pages to disk
443 hash_sync(const DB
*dbp
, u_int32_t flags
)
455 hashp
= (HTAB
*)dbp
->internal
;
456 if (!hashp
->save_file
)
458 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
467 * -1 indicates that errno should be set
470 flush_meta(HTAB
*hashp
)
473 #if BYTE_ORDER == LITTLE_ENDIAN
478 if (!hashp
->save_file
)
480 hashp
->MAGIC
= HASHMAGIC
;
481 hashp
->VERSION
= HASHVERSION
;
482 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
486 #if BYTE_ORDER == LITTLE_ENDIAN
488 swap_header_copy(&hashp
->hdr
, whdrp
);
490 if ((wsize
= pwrite(fp
, whdrp
, sizeof(HASHHDR
), (off_t
)0)) == -1)
493 if (wsize
!= sizeof(HASHHDR
)) {
495 hashp
->error
= errno
;
498 for (i
= 0; i
< NCACHED
; i
++)
500 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
501 hashp
->BITMAPS
[i
], 0, 1))
506 /*******************************SEARCH ROUTINES *****************************/
508 * All the access routines return
512 * 1 to indicate an external ERROR (i.e. key not found, etc)
513 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
516 hash_get(const DB
*dbp
, const DBT
*key
, DBT
*data
, u_int32_t flag
)
520 hashp
= (HTAB
*)dbp
->internal
;
522 hashp
->error
= errno
= EINVAL
;
525 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
529 hash_put(const DB
*dbp
, DBT
*key
, const DBT
*data
, u_int32_t flag
)
533 hashp
= (HTAB
*)dbp
->internal
;
534 if (flag
&& flag
!= R_NOOVERWRITE
) {
535 hashp
->error
= errno
= EINVAL
;
538 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
539 hashp
->error
= errno
= EPERM
;
542 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
543 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
547 hash_delete(const DB
*dbp
, const DBT
*key
,
548 u_int32_t flag
) /* Ignored */
552 hashp
= (HTAB
*)dbp
->internal
;
553 if (flag
&& flag
!= R_CURSOR
) {
554 hashp
->error
= errno
= EINVAL
;
557 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
558 hashp
->error
= errno
= EPERM
;
561 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
565 * Assume that hashp has been set in wrapper routine.
568 hash_access(HTAB
*hashp
, ACTION action
, DBT
*key
, DBT
*val
)
571 BUFHEAD
*bufp
, *save_bufp
;
573 int n
, ndx
, off
, size
;
577 #ifdef HASH_STATISTICS
583 kp
= (char *)key
->data
;
584 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
589 /* Pin the bucket chain */
590 rbufp
->flags
|= BUF_PIN
;
591 for (bp
= (u_int16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
592 if (bp
[1] >= REAL_KEY
) {
593 /* Real key/data pair */
594 if (size
== off
- *bp
&&
595 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
598 #ifdef HASH_STATISTICS
603 } else if (bp
[1] == OVFLPAGE
) {
604 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
606 save_bufp
->flags
&= ~BUF_PIN
;
610 bp
= (u_int16_t
*)rbufp
->page
;
614 } else if (bp
[1] < REAL_KEY
) {
616 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
621 __find_last_page(hashp
, &bufp
))) {
626 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
628 save_bufp
->flags
&= ~BUF_PIN
;
632 bp
= (u_int16_t
*)rbufp
->page
;
637 save_bufp
->flags
&= ~BUF_PIN
;
646 if (__addel(hashp
, rbufp
, key
, val
)) {
647 save_bufp
->flags
&= ~BUF_PIN
;
650 save_bufp
->flags
&= ~BUF_PIN
;
656 save_bufp
->flags
&= ~BUF_PIN
;
663 save_bufp
->flags
&= ~BUF_PIN
;
666 bp
= (u_int16_t
*)rbufp
->page
;
667 if (bp
[ndx
+ 1] < REAL_KEY
) {
668 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
671 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
672 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
676 if ((__delpair(hashp
, rbufp
, ndx
)) ||
677 (__addel(hashp
, rbufp
, key
, val
))) {
678 save_bufp
->flags
&= ~BUF_PIN
;
683 if (__delpair(hashp
, rbufp
, ndx
))
687 LIBC_ABORT("illegal action (%d)", action
);
689 save_bufp
->flags
&= ~BUF_PIN
;
694 hash_seq(const DB
*dbp
, DBT
*key
, DBT
*data
, u_int32_t flag
)
701 hashp
= (HTAB
*)dbp
->internal
;
702 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
703 hashp
->error
= errno
= EINVAL
;
706 #ifdef HASH_STATISTICS
709 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
715 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
716 if (!(bufp
= hashp
->cpage
)) {
717 for (bucket
= hashp
->cbucket
;
718 bucket
<= hashp
->MAX_BUCKET
;
719 bucket
++, hashp
->cndx
= 1) {
720 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
724 bp
= (u_int16_t
*)bufp
->page
;
728 hashp
->cbucket
= bucket
;
729 if ((u_int32_t
)hashp
->cbucket
> hashp
->MAX_BUCKET
) {
734 bp
= (u_int16_t
*)hashp
->cpage
->page
;
735 if (flag
== R_NEXT
) {
737 if (hashp
->cndx
> bp
[0]) {
750 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
751 bufp
= hashp
->cpage
=
752 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
755 bp
= (u_int16_t
*)(bufp
->page
);
764 if (bp
[ndx
+ 1] < REAL_KEY
) {
765 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
768 if (hashp
->cpage
== 0)
770 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
771 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
772 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
773 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
778 /********************************* UTILITIES ************************/
786 __expand_table(HTAB
*hashp
)
788 u_int32_t old_bucket
, new_bucket
;
789 int dirsize
, new_segnum
, spare_ndx
;
791 #ifdef HASH_STATISTICS
794 new_bucket
= ++hashp
->MAX_BUCKET
;
795 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
797 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
799 /* Check if we need a new segment */
800 if (new_segnum
>= hashp
->nsegs
) {
801 /* Check if we need to expand directory */
802 if (new_segnum
>= hashp
->DSIZE
) {
803 /* Reallocate directory */
804 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
805 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
807 hashp
->DSIZE
= dirsize
<< 1;
809 if ((hashp
->dir
[new_segnum
] =
810 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
816 * If the split point is increasing (MAX_BUCKET's log base 2
817 * * increases), we need to copy the current contents of the spare
818 * split bucket to the next bucket.
820 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
821 if (spare_ndx
> hashp
->OVFL_POINT
) {
822 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
823 hashp
->OVFL_POINT
= spare_ndx
;
826 if (new_bucket
> hashp
->HIGH_MASK
) {
827 /* Starting a new doubling */
828 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
829 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
831 /* Relocate records to the new bucket */
832 return (__split_page(hashp
, old_bucket
, new_bucket
));
836 * If realloc guarantees that the pointer is not destroyed if the realloc
837 * fails, then this routine can go away.
840 hash_realloc(SEGMENT
**p_ptr
, int oldsize
, int newsize
)
844 if ( (p
= malloc(newsize
)) ) {
845 memmove(p
, *p_ptr
, oldsize
);
846 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
854 __call_hash(HTAB
*hashp
, char *k
, int len
)
856 unsigned int n
, bucket
;
858 n
= hashp
->hash(k
, len
);
859 bucket
= n
& hashp
->HIGH_MASK
;
860 if (bucket
> hashp
->MAX_BUCKET
)
861 bucket
= bucket
& hashp
->LOW_MASK
;
866 * Allocate segment table. On error, destroy the table and set errno.
868 * Returns 0 on success
871 alloc_segs(HTAB
*hashp
, int nsegs
)
879 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
881 (void)hdestroy(hashp
);
885 hashp
->nsegs
= nsegs
;
888 /* Allocate segments */
889 if ((store
= (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
,
890 sizeof(SEGMENT
))) == NULL
) {
892 (void)hdestroy(hashp
);
896 for (i
= 0; i
< nsegs
; i
++)
897 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
901 #if BYTE_ORDER == LITTLE_ENDIAN
903 * Hashp->hdr needs to be byteswapped.
906 swap_header_copy(HASHHDR
*srcp
, HASHHDR
*destp
)
910 P_32_COPY(srcp
->magic
, destp
->magic
);
911 P_32_COPY(srcp
->version
, destp
->version
);
912 P_32_COPY(srcp
->lorder
, destp
->lorder
);
913 P_32_COPY(srcp
->bsize
, destp
->bsize
);
914 P_32_COPY(srcp
->bshift
, destp
->bshift
);
915 P_32_COPY(srcp
->dsize
, destp
->dsize
);
916 P_32_COPY(srcp
->ssize
, destp
->ssize
);
917 P_32_COPY(srcp
->sshift
, destp
->sshift
);
918 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
919 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
920 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
921 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
922 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
923 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
924 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
925 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
926 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
927 for (i
= 0; i
< NCACHED
; i
++) {
928 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
929 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
934 swap_header(HTAB
*hashp
)
941 M_32_SWAP(hdrp
->magic
);
942 M_32_SWAP(hdrp
->version
);
943 M_32_SWAP(hdrp
->lorder
);
944 M_32_SWAP(hdrp
->bsize
);
945 M_32_SWAP(hdrp
->bshift
);
946 M_32_SWAP(hdrp
->dsize
);
947 M_32_SWAP(hdrp
->ssize
);
948 M_32_SWAP(hdrp
->sshift
);
949 M_32_SWAP(hdrp
->ovfl_point
);
950 M_32_SWAP(hdrp
->last_freed
);
951 M_32_SWAP(hdrp
->max_bucket
);
952 M_32_SWAP(hdrp
->high_mask
);
953 M_32_SWAP(hdrp
->low_mask
);
954 M_32_SWAP(hdrp
->ffactor
);
955 M_32_SWAP(hdrp
->nkeys
);
956 M_32_SWAP(hdrp
->hdrpages
);
957 M_32_SWAP(hdrp
->h_charkey
);
958 for (i
= 0; i
< NCACHED
; i
++) {
959 M_32_SWAP(hdrp
->spares
[i
]);
960 M_16_SWAP(hdrp
->bitmaps
[i
]);