]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash.c
84da4d9304a093cf5f3b667b8181a9f8eb23e41c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
66 #include <sys/param.h>
84 static int alloc_segs(HTAB
*, int);
85 static int flush_meta(HTAB
*);
86 static int hash_access(HTAB
*, ACTION
, DBT
*, DBT
*);
87 static int hash_close(DB
*);
88 static int hash_delete(const DB
*, const DBT
*, u_int32_t
);
89 static int hash_fd(const DB
*);
90 static int hash_get(const DB
*, const DBT
*, DBT
*, u_int32_t
);
91 static int hash_put(const DB
*, DBT
*, const DBT
*, u_int32_t
);
92 static void *hash_realloc(SEGMENT
**, int, int);
93 static int hash_seq(const DB
*, DBT
*, DBT
*, u_int32_t
);
94 static int hash_sync(const DB
*, u_int32_t
);
95 static int hdestroy(HTAB
*);
96 static HTAB
*init_hash(HTAB
*, const char *, HASHINFO
*);
97 static int init_htab(HTAB
*, int);
98 #if BYTE_ORDER == LITTLE_ENDIAN
99 static void swap_header(HTAB
*);
100 static void swap_header_copy(HASHHDR
*, HASHHDR
*);
103 /* Fast arithmetic, relying on powers of 2, */
104 #define MOD(x, y) ((x) & ((y) - 1))
106 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
113 #ifdef HASH_STATISTICS
114 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
117 /************************** INTERFACE ROUTINES ***************************/
121 __hash_open(file
, flags
, mode
, info
, dflags
)
123 int flags
, mode
, dflags
;
124 const HASHINFO
*info
; /* Special directives for create */
129 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
131 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
136 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
141 * Even if user wants write only, we need to be able to read
142 * the actual file, so we need to open it read/write. But, the
143 * field in the hashp structure needs to be accurate so that
144 * we can check accesses.
146 hashp
->flags
= flags
;
149 if (!file
|| (flags
& O_TRUNC
) ||
150 (stat(file
, &statbuf
) && (errno
== ENOENT
))) {
152 errno
= 0; /* Just in case someone looks at errno */
156 if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
157 RETURN_ERROR(errno
, error0
);
159 /* if the .db file is empty, and we had permission to create
160 a new .db file, then reinitialize the database */
161 if ((flags
& O_CREAT
) &&
162 fstat(hashp
->fp
, &statbuf
) == 0 && statbuf
.st_size
== 0)
165 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
168 if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
169 RETURN_ERROR(errno
, error1
);
171 /* Table already exists */
172 if (info
&& info
->hash
)
173 hashp
->hash
= info
->hash
;
175 hashp
->hash
= __default_hash
;
177 hdrsize
= read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
178 #if BYTE_ORDER == LITTLE_ENDIAN
182 RETURN_ERROR(errno
, error1
);
183 if (hdrsize
!= sizeof(HASHHDR
))
184 RETURN_ERROR(EFTYPE
, error1
);
185 /* Verify file type, versions and hash function */
186 if (hashp
->MAGIC
!= HASHMAGIC
)
187 RETURN_ERROR(EFTYPE
, error1
);
188 #define OLDHASHVERSION 1
189 if (hashp
->VERSION
!= HASHVERSION
&&
190 hashp
->VERSION
!= OLDHASHVERSION
)
191 RETURN_ERROR(EFTYPE
, error1
);
192 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
193 RETURN_ERROR(EFTYPE
, error1
);
195 * Figure out how many segments we need. Max_Bucket is the
196 * maximum bucket number, so the number of buckets is
199 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
202 if (alloc_segs(hashp
, nsegs
))
204 * If alloc_segs fails, table will have been destroyed
205 * and errno will have been set.
208 /* Read in bitmaps */
209 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
210 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
211 (hashp
->BSHIFT
+ BYTE_SHIFT
);
213 hashp
->nmaps
= bpages
;
214 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_int32_t
*));
217 /* Initialize Buffer Manager */
218 if (info
&& info
->cachesize
)
219 __buf_init(hashp
, info
->cachesize
);
221 __buf_init(hashp
, DEF_BUFSIZE
);
223 hashp
->new_file
= new_table
;
224 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
226 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
232 dbp
->internal
= hashp
;
233 dbp
->close
= hash_close
;
234 dbp
->del
= hash_delete
;
239 dbp
->sync
= hash_sync
;
243 (void)fprintf(stderr
,
244 "%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",
246 "TABLE POINTER ", hashp
,
247 "BUCKET SIZE ", hashp
->BSIZE
,
248 "BUCKET SHIFT ", hashp
->BSHIFT
,
249 "DIRECTORY SIZE ", hashp
->DSIZE
,
250 "SEGMENT SIZE ", hashp
->SGSIZE
,
251 "SEGMENT SHIFT ", hashp
->SSHIFT
,
252 "FILL FACTOR ", hashp
->FFACTOR
,
253 "MAX BUCKET ", hashp
->MAX_BUCKET
,
254 "OVFL POINT ", hashp
->OVFL_POINT
,
255 "LAST FREED ", hashp
->LAST_FREED
,
256 "HIGH MASK ", hashp
->HIGH_MASK
,
257 "LOW MASK ", hashp
->LOW_MASK
,
258 "NSEGS ", hashp
->nsegs
,
259 "NKEYS ", hashp
->NKEYS
);
261 #ifdef HASH_STATISTICS
262 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
268 (void)close(hashp
->fp
);
286 hashp
= (HTAB
*)dbp
->internal
;
287 retval
= hdestroy(hashp
);
301 hashp
= (HTAB
*)dbp
->internal
;
302 if (hashp
->fp
== -1) {
309 /************************** LOCAL CREATION ROUTINES **********************/
311 init_hash(hashp
, file
, info
)
321 hashp
->LORDER
= BYTE_ORDER
;
322 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
323 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
324 hashp
->SGSIZE
= DEF_SEGSIZE
;
325 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
326 hashp
->DSIZE
= DEF_DIRSIZE
;
327 hashp
->FFACTOR
= DEF_FFACTOR
;
328 hashp
->hash
= __default_hash
;
329 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
330 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
332 /* Fix bucket size to be optimal for file system */
334 if (stat(file
, &statbuf
))
336 hashp
->BSIZE
= statbuf
.st_blksize
;
337 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
342 /* Round pagesize up to power of 2 */
343 hashp
->BSHIFT
= __log2(info
->bsize
);
344 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
345 if (hashp
->BSIZE
> MAX_BSIZE
) {
351 hashp
->FFACTOR
= info
->ffactor
;
353 hashp
->hash
= info
->hash
;
357 if (info
->lorder
!= BIG_ENDIAN
&&
358 info
->lorder
!= LITTLE_ENDIAN
) {
362 hashp
->LORDER
= info
->lorder
;
365 /* init_htab should destroy the table and set errno if it fails */
366 if (init_htab(hashp
, nelem
))
372 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
373 * the table and set errno, so we just pass the error information along.
375 * Returns 0 on No Error
378 init_htab(hashp
, nelem
)
386 * Divide number of elements by the fill factor and determine a
387 * desired number of buckets. Allocate space for the next greater
388 * power of two number of buckets.
390 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
392 l2
= __log2(MAX(nelem
, 2));
395 hashp
->SPARES
[l2
] = l2
+ 1;
396 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
397 hashp
->OVFL_POINT
= l2
;
398 hashp
->LAST_FREED
= 2;
400 /* First bitmap page is at: splitpoint l2 page offset 1 */
401 if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
404 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
405 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
406 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
409 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
410 nsegs
= 1 << __log2(nsegs
);
412 if (nsegs
> hashp
->DSIZE
)
413 hashp
->DSIZE
= nsegs
;
414 return (alloc_segs(hashp
, nsegs
));
417 /********************** DESTROY/CLOSE ROUTINES ************************/
420 * Flushes any changes to the file if necessary and destroys the hashp
421 * structure, freeing all allocated space.
431 #ifdef HASH_STATISTICS
432 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
433 hash_accesses
, hash_collisions
);
434 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
436 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
438 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
439 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
441 for (i
= 0; i
< NCACHED
; i
++)
442 (void)fprintf(stderr
,
443 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
446 * Call on buffer manager to free buffers, and if required,
447 * write them to disk.
449 if (__buf_free(hashp
, 1, hashp
->save_file
))
452 free(*hashp
->dir
); /* Free initial segments */
453 /* Free extra segments */
454 while (hashp
->exsegs
--)
455 free(hashp
->dir
[--hashp
->nsegs
]);
458 if (flush_meta(hashp
) && !save_errno
)
461 for (i
= 0; i
< hashp
->nmaps
; i
++)
463 free(hashp
->mapp
[i
]);
466 (void)close(hashp
->fp
);
477 * Write modified pages to disk
484 hash_sync(dbp
, flags
)
498 hashp
= (HTAB
*)dbp
->internal
;
499 if (!hashp
->save_file
)
501 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
510 * -1 indicates that errno should be set
517 #if BYTE_ORDER == LITTLE_ENDIAN
522 if (!hashp
->save_file
)
524 hashp
->MAGIC
= HASHMAGIC
;
525 hashp
->VERSION
= HASHVERSION
;
526 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
530 #if BYTE_ORDER == LITTLE_ENDIAN
532 swap_header_copy(&hashp
->hdr
, whdrp
);
534 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
535 ((wsize
= write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
538 if (wsize
!= sizeof(HASHHDR
)) {
540 hashp
->error
= errno
;
543 for (i
= 0; i
< NCACHED
; i
++)
545 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
546 hashp
->BITMAPS
[i
], 0, 1))
551 /*******************************SEARCH ROUTINES *****************************/
553 * All the access routines return
557 * 1 to indicate an external ERROR (i.e. key not found, etc)
558 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
561 hash_get(dbp
, key
, data
, flag
)
569 hashp
= (HTAB
*)dbp
->internal
;
571 hashp
->error
= errno
= EINVAL
;
574 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
578 hash_put(dbp
, key
, data
, flag
)
586 hashp
= (HTAB
*)dbp
->internal
;
587 if (flag
&& flag
!= R_NOOVERWRITE
) {
588 hashp
->error
= EINVAL
;
592 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
593 hashp
->error
= errno
= EPERM
;
596 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
597 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
601 hash_delete(dbp
, key
, flag
)
604 u_int32_t flag
; /* Ignored */
608 hashp
= (HTAB
*)dbp
->internal
;
609 if (flag
&& flag
!= R_CURSOR
) {
610 hashp
->error
= errno
= EINVAL
;
613 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
614 hashp
->error
= errno
= EPERM
;
617 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
621 * Assume that hashp has been set in wrapper routine.
624 hash_access(hashp
, action
, key
, val
)
630 BUFHEAD
*bufp
, *save_bufp
;
632 int n
, ndx
, off
, size
;
636 #ifdef HASH_STATISTICS
642 kp
= (char *)key
->data
;
643 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
648 /* Pin the bucket chain */
649 rbufp
->flags
|= BUF_PIN
;
650 for (bp
= (u_int16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
651 if (bp
[1] >= REAL_KEY
) {
652 /* Real key/data pair */
653 if (size
== off
- *bp
&&
654 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
657 #ifdef HASH_STATISTICS
662 } else if (bp
[1] == OVFLPAGE
) {
663 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
665 save_bufp
->flags
&= ~BUF_PIN
;
669 bp
= (u_int16_t
*)rbufp
->page
;
673 } else if (bp
[1] < REAL_KEY
) {
675 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
680 __find_last_page(hashp
, &bufp
))) {
685 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
687 save_bufp
->flags
&= ~BUF_PIN
;
691 bp
= (u_int16_t
*)rbufp
->page
;
696 save_bufp
->flags
&= ~BUF_PIN
;
705 if (__addel(hashp
, rbufp
, key
, val
)) {
706 save_bufp
->flags
&= ~BUF_PIN
;
709 save_bufp
->flags
&= ~BUF_PIN
;
715 save_bufp
->flags
&= ~BUF_PIN
;
722 save_bufp
->flags
&= ~BUF_PIN
;
725 bp
= (u_int16_t
*)rbufp
->page
;
726 if (bp
[ndx
+ 1] < REAL_KEY
) {
727 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
730 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
731 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
735 if ((__delpair(hashp
, rbufp
, ndx
)) ||
736 (__addel(hashp
, rbufp
, key
, val
))) {
737 save_bufp
->flags
&= ~BUF_PIN
;
742 if (__delpair(hashp
, rbufp
, ndx
))
748 save_bufp
->flags
&= ~BUF_PIN
;
753 hash_seq(dbp
, key
, data
, flag
)
763 hashp
= (HTAB
*)dbp
->internal
;
764 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
765 hashp
->error
= errno
= EINVAL
;
768 #ifdef HASH_STATISTICS
771 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
777 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
778 if (!(bufp
= hashp
->cpage
)) {
779 for (bucket
= hashp
->cbucket
;
780 bucket
<= hashp
->MAX_BUCKET
;
781 bucket
++, hashp
->cndx
= 1) {
782 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
786 bp
= (u_int16_t
*)bufp
->page
;
790 hashp
->cbucket
= bucket
;
791 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
796 bp
= (u_int16_t
*)hashp
->cpage
->page
;
802 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
803 bufp
= hashp
->cpage
=
804 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
807 bp
= (u_int16_t
*)(bufp
->page
);
816 if (bp
[ndx
+ 1] < REAL_KEY
) {
817 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
820 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
821 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
822 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
823 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
835 /********************************* UTILITIES ************************/
843 __expand_table(hashp
)
846 u_int32_t old_bucket
, new_bucket
;
847 int dirsize
, new_segnum
, spare_ndx
;
849 #ifdef HASH_STATISTICS
852 new_bucket
= ++hashp
->MAX_BUCKET
;
853 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
855 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
857 /* Check if we need a new segment */
858 if (new_segnum
>= hashp
->nsegs
) {
859 /* Check if we need to expand directory */
860 if (new_segnum
>= hashp
->DSIZE
) {
861 /* Reallocate directory */
862 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
863 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
865 hashp
->DSIZE
= dirsize
<< 1;
867 if ((hashp
->dir
[new_segnum
] =
868 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
874 * If the split point is increasing (MAX_BUCKET's log base 2
875 * * increases), we need to copy the current contents of the spare
876 * split bucket to the next bucket.
878 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
879 if (spare_ndx
> hashp
->OVFL_POINT
) {
880 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
881 hashp
->OVFL_POINT
= spare_ndx
;
884 if (new_bucket
> hashp
->HIGH_MASK
) {
885 /* Starting a new doubling */
886 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
887 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
889 /* Relocate records to the new bucket */
890 return (__split_page(hashp
, old_bucket
, new_bucket
));
894 * If realloc guarantees that the pointer is not destroyed if the realloc
895 * fails, then this routine can go away.
898 hash_realloc(p_ptr
, oldsize
, newsize
)
900 int oldsize
, newsize
;
904 if ( (p
= malloc(newsize
)) ) {
905 memmove(p
, *p_ptr
, oldsize
);
906 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
914 __call_hash(hashp
, k
, len
)
921 n
= hashp
->hash(k
, len
);
922 bucket
= n
& hashp
->HIGH_MASK
;
923 if (bucket
> hashp
->MAX_BUCKET
)
924 bucket
= bucket
& hashp
->LOW_MASK
;
929 * Allocate segment table. On error, destroy the table and set errno.
931 * Returns 0 on success
934 alloc_segs(hashp
, nsegs
)
944 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
946 (void)hdestroy(hashp
);
950 /* Allocate segments */
952 (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
954 (void)hdestroy(hashp
);
958 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
959 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
963 #if BYTE_ORDER == LITTLE_ENDIAN
965 * Hashp->hdr needs to be byteswapped.
968 swap_header_copy(srcp
, destp
)
969 HASHHDR
*srcp
, *destp
;
973 P_32_COPY(srcp
->magic
, destp
->magic
);
974 P_32_COPY(srcp
->version
, destp
->version
);
975 P_32_COPY(srcp
->lorder
, destp
->lorder
);
976 P_32_COPY(srcp
->bsize
, destp
->bsize
);
977 P_32_COPY(srcp
->bshift
, destp
->bshift
);
978 P_32_COPY(srcp
->dsize
, destp
->dsize
);
979 P_32_COPY(srcp
->ssize
, destp
->ssize
);
980 P_32_COPY(srcp
->sshift
, destp
->sshift
);
981 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
982 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
983 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
984 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
985 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
986 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
987 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
988 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
989 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
990 for (i
= 0; i
< NCACHED
; i
++) {
991 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
992 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
1005 M_32_SWAP(hdrp
->magic
);
1006 M_32_SWAP(hdrp
->version
);
1007 M_32_SWAP(hdrp
->lorder
);
1008 M_32_SWAP(hdrp
->bsize
);
1009 M_32_SWAP(hdrp
->bshift
);
1010 M_32_SWAP(hdrp
->dsize
);
1011 M_32_SWAP(hdrp
->ssize
);
1012 M_32_SWAP(hdrp
->sshift
);
1013 M_32_SWAP(hdrp
->ovfl_point
);
1014 M_32_SWAP(hdrp
->last_freed
);
1015 M_32_SWAP(hdrp
->max_bucket
);
1016 M_32_SWAP(hdrp
->high_mask
);
1017 M_32_SWAP(hdrp
->low_mask
);
1018 M_32_SWAP(hdrp
->ffactor
);
1019 M_32_SWAP(hdrp
->nkeys
);
1020 M_32_SWAP(hdrp
->hdrpages
);
1021 M_32_SWAP(hdrp
->h_charkey
);
1022 for (i
= 0; i
< NCACHED
; i
++) {
1023 M_32_SWAP(hdrp
->spares
[i
]);
1024 M_16_SWAP(hdrp
->bitmaps
[i
]);