]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash.c
b22f1c6fd79202db34b307a01fcb99870aa147ac
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993, 1994
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
64 #include <sys/param.h>
82 static int alloc_segs(HTAB
*, int);
83 static int flush_meta(HTAB
*);
84 static int hash_access(HTAB
*, ACTION
, DBT
*, DBT
*);
85 static int hash_close(DB
*);
86 static int hash_delete(const DB
*, const DBT
*, u_int32_t
);
87 static int hash_fd(const DB
*);
88 static int hash_get(const DB
*, const DBT
*, DBT
*, u_int32_t
);
89 static int hash_put(const DB
*, DBT
*, const DBT
*, u_int32_t
);
90 static void *hash_realloc(SEGMENT
**, int, int);
91 static int hash_seq(const DB
*, DBT
*, DBT
*, u_int32_t
);
92 static int hash_sync(const DB
*, u_int32_t
);
93 static int hdestroy(HTAB
*);
94 static HTAB
*init_hash(HTAB
*, const char *, HASHINFO
*);
95 static int init_htab(HTAB
*, int);
96 #if BYTE_ORDER == LITTLE_ENDIAN
97 static void swap_header(HTAB
*);
98 static void swap_header_copy(HASHHDR
*, HASHHDR
*);
101 /* Fast arithmetic, relying on powers of 2, */
102 #define MOD(x, y) ((x) & ((y) - 1))
104 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
111 #ifdef HASH_STATISTICS
112 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
115 /************************** INTERFACE ROUTINES ***************************/
119 __hash_open(file
, flags
, mode
, info
, dflags
)
121 int flags
, mode
, dflags
;
122 const HASHINFO
*info
; /* Special directives for create */
127 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
129 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
134 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
))))
139 * Even if user wants write only, we need to be able to read
140 * the actual file, so we need to open it read/write. But, the
141 * field in the hashp structure needs to be accurate so that
142 * we can check accesses.
144 hashp
->flags
= flags
;
147 if (!file
|| (flags
& O_TRUNC
) ||
148 (stat(file
, &statbuf
) && (errno
== ENOENT
))) {
150 errno
= 0; /* Just in case someone looks at errno */
154 if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
155 RETURN_ERROR(errno
, error0
);
157 /* if the .db file is empty, and we had permission to create
158 a new .db file, then reinitialize the database */
159 if ((flags
& O_CREAT
) &&
160 fstat(hashp
->fp
, &statbuf
) == 0 && statbuf
.st_size
== 0)
163 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
166 if (!(hashp
= init_hash(hashp
, file
, (HASHINFO
*)info
)))
167 RETURN_ERROR(errno
, error1
);
169 /* Table already exists */
170 if (info
&& info
->hash
)
171 hashp
->hash
= info
->hash
;
173 hashp
->hash
= __default_hash
;
175 hdrsize
= read(hashp
->fp
, &hashp
->hdr
, sizeof(HASHHDR
));
176 #if BYTE_ORDER == LITTLE_ENDIAN
180 RETURN_ERROR(errno
, error1
);
181 if (hdrsize
!= sizeof(HASHHDR
))
182 RETURN_ERROR(EFTYPE
, error1
);
183 /* Verify file type, versions and hash function */
184 if (hashp
->MAGIC
!= HASHMAGIC
)
185 RETURN_ERROR(EFTYPE
, error1
);
186 #define OLDHASHVERSION 1
187 if (hashp
->VERSION
!= HASHVERSION
&&
188 hashp
->VERSION
!= OLDHASHVERSION
)
189 RETURN_ERROR(EFTYPE
, error1
);
190 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
191 RETURN_ERROR(EFTYPE
, error1
);
193 * Figure out how many segments we need. Max_Bucket is the
194 * maximum bucket number, so the number of buckets is
197 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
200 if (alloc_segs(hashp
, nsegs
))
202 * If alloc_segs fails, table will have been destroyed
203 * and errno will have been set.
206 /* Read in bitmaps */
207 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
208 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
209 (hashp
->BSHIFT
+ BYTE_SHIFT
);
211 hashp
->nmaps
= bpages
;
212 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(u_int32_t
*));
215 /* Initialize Buffer Manager */
216 if (info
&& info
->cachesize
)
217 __buf_init(hashp
, info
->cachesize
);
219 __buf_init(hashp
, DEF_BUFSIZE
);
221 hashp
->new_file
= new_table
;
222 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
224 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
230 dbp
->internal
= hashp
;
231 dbp
->close
= hash_close
;
232 dbp
->del
= hash_delete
;
237 dbp
->sync
= hash_sync
;
241 (void)fprintf(stderr
,
242 "%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",
244 "TABLE POINTER ", hashp
,
245 "BUCKET SIZE ", hashp
->BSIZE
,
246 "BUCKET SHIFT ", hashp
->BSHIFT
,
247 "DIRECTORY SIZE ", hashp
->DSIZE
,
248 "SEGMENT SIZE ", hashp
->SGSIZE
,
249 "SEGMENT SHIFT ", hashp
->SSHIFT
,
250 "FILL FACTOR ", hashp
->FFACTOR
,
251 "MAX BUCKET ", hashp
->MAX_BUCKET
,
252 "OVFL POINT ", hashp
->OVFL_POINT
,
253 "LAST FREED ", hashp
->LAST_FREED
,
254 "HIGH MASK ", hashp
->HIGH_MASK
,
255 "LOW MASK ", hashp
->LOW_MASK
,
256 "NSEGS ", hashp
->nsegs
,
257 "NKEYS ", hashp
->NKEYS
);
259 #ifdef HASH_STATISTICS
260 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
266 (void)close(hashp
->fp
);
284 hashp
= (HTAB
*)dbp
->internal
;
285 retval
= hdestroy(hashp
);
299 hashp
= (HTAB
*)dbp
->internal
;
300 if (hashp
->fp
== -1) {
307 /************************** LOCAL CREATION ROUTINES **********************/
309 init_hash(hashp
, file
, info
)
319 hashp
->LORDER
= BYTE_ORDER
;
320 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
321 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
322 hashp
->SGSIZE
= DEF_SEGSIZE
;
323 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
324 hashp
->DSIZE
= DEF_DIRSIZE
;
325 hashp
->FFACTOR
= DEF_FFACTOR
;
326 hashp
->hash
= __default_hash
;
327 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
328 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
330 /* Fix bucket size to be optimal for file system */
332 if (stat(file
, &statbuf
))
334 hashp
->BSIZE
= statbuf
.st_blksize
;
335 hashp
->BSHIFT
= __log2(hashp
->BSIZE
);
340 /* Round pagesize up to power of 2 */
341 hashp
->BSHIFT
= __log2(info
->bsize
);
342 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
343 if (hashp
->BSIZE
> MAX_BSIZE
) {
349 hashp
->FFACTOR
= info
->ffactor
;
351 hashp
->hash
= info
->hash
;
355 if (info
->lorder
!= BIG_ENDIAN
&&
356 info
->lorder
!= LITTLE_ENDIAN
) {
360 hashp
->LORDER
= info
->lorder
;
363 /* init_htab should destroy the table and set errno if it fails */
364 if (init_htab(hashp
, nelem
))
370 * This calls alloc_segs which may run out of memory. Alloc_segs will destroy
371 * the table and set errno, so we just pass the error information along.
373 * Returns 0 on No Error
376 init_htab(hashp
, nelem
)
384 * Divide number of elements by the fill factor and determine a
385 * desired number of buckets. Allocate space for the next greater
386 * power of two number of buckets.
388 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
390 l2
= __log2(MAX(nelem
, 2));
393 hashp
->SPARES
[l2
] = l2
+ 1;
394 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
395 hashp
->OVFL_POINT
= l2
;
396 hashp
->LAST_FREED
= 2;
398 /* First bitmap page is at: splitpoint l2 page offset 1 */
399 if (__ibitmap(hashp
, OADDR_OF(l2
, 1), l2
+ 1, 0))
402 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
403 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
404 hashp
->HDRPAGES
= ((MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
407 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
408 nsegs
= 1 << __log2(nsegs
);
410 if (nsegs
> hashp
->DSIZE
)
411 hashp
->DSIZE
= nsegs
;
412 return (alloc_segs(hashp
, nsegs
));
415 /********************** DESTROY/CLOSE ROUTINES ************************/
418 * Flushes any changes to the file if necessary and destroys the hashp
419 * structure, freeing all allocated space.
429 #ifdef HASH_STATISTICS
430 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
431 hash_accesses
, hash_collisions
);
432 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
434 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
436 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
437 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
439 for (i
= 0; i
< NCACHED
; i
++)
440 (void)fprintf(stderr
,
441 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
444 * Call on buffer manager to free buffers, and if required,
445 * write them to disk.
447 if (__buf_free(hashp
, 1, hashp
->save_file
))
450 free(*hashp
->dir
); /* Free initial segments */
451 /* Free extra segments */
452 while (hashp
->exsegs
--)
453 free(hashp
->dir
[--hashp
->nsegs
]);
456 if (flush_meta(hashp
) && !save_errno
)
459 for (i
= 0; i
< hashp
->nmaps
; i
++)
461 free(hashp
->mapp
[i
]);
464 (void)close(hashp
->fp
);
475 * Write modified pages to disk
482 hash_sync(dbp
, flags
)
496 hashp
= (HTAB
*)dbp
->internal
;
497 if (!hashp
->save_file
)
499 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
508 * -1 indicates that errno should be set
515 #if BYTE_ORDER == LITTLE_ENDIAN
520 if (!hashp
->save_file
)
522 hashp
->MAGIC
= HASHMAGIC
;
523 hashp
->VERSION
= HASHVERSION
;
524 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
528 #if BYTE_ORDER == LITTLE_ENDIAN
530 swap_header_copy(&hashp
->hdr
, whdrp
);
532 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
533 ((wsize
= write(fp
, whdrp
, sizeof(HASHHDR
))) == -1))
536 if (wsize
!= sizeof(HASHHDR
)) {
538 hashp
->error
= errno
;
541 for (i
= 0; i
< NCACHED
; i
++)
543 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
544 hashp
->BITMAPS
[i
], 0, 1))
549 /*******************************SEARCH ROUTINES *****************************/
551 * All the access routines return
555 * 1 to indicate an external ERROR (i.e. key not found, etc)
556 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
559 hash_get(dbp
, key
, data
, flag
)
567 hashp
= (HTAB
*)dbp
->internal
;
569 hashp
->error
= errno
= EINVAL
;
572 return (hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
));
576 hash_put(dbp
, key
, data
, flag
)
584 hashp
= (HTAB
*)dbp
->internal
;
585 if (flag
&& flag
!= R_NOOVERWRITE
) {
586 hashp
->error
= EINVAL
;
590 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
591 hashp
->error
= errno
= EPERM
;
594 return (hash_access(hashp
, flag
== R_NOOVERWRITE
?
595 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
));
599 hash_delete(dbp
, key
, flag
)
602 u_int32_t flag
; /* Ignored */
606 hashp
= (HTAB
*)dbp
->internal
;
607 if (flag
&& flag
!= R_CURSOR
) {
608 hashp
->error
= errno
= EINVAL
;
611 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
612 hashp
->error
= errno
= EPERM
;
615 return (hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
));
619 * Assume that hashp has been set in wrapper routine.
622 hash_access(hashp
, action
, key
, val
)
628 BUFHEAD
*bufp
, *save_bufp
;
630 int n
, ndx
, off
, size
;
634 #ifdef HASH_STATISTICS
640 kp
= (char *)key
->data
;
641 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
646 /* Pin the bucket chain */
647 rbufp
->flags
|= BUF_PIN
;
648 for (bp
= (u_int16_t
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
649 if (bp
[1] >= REAL_KEY
) {
650 /* Real key/data pair */
651 if (size
== off
- *bp
&&
652 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
655 #ifdef HASH_STATISTICS
660 } else if (bp
[1] == OVFLPAGE
) {
661 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
663 save_bufp
->flags
&= ~BUF_PIN
;
667 bp
= (u_int16_t
*)rbufp
->page
;
671 } else if (bp
[1] < REAL_KEY
) {
673 __find_bigpair(hashp
, rbufp
, ndx
, kp
, size
)) > 0)
678 __find_last_page(hashp
, &bufp
))) {
683 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
685 save_bufp
->flags
&= ~BUF_PIN
;
689 bp
= (u_int16_t
*)rbufp
->page
;
694 save_bufp
->flags
&= ~BUF_PIN
;
703 if (__addel(hashp
, rbufp
, key
, val
)) {
704 save_bufp
->flags
&= ~BUF_PIN
;
707 save_bufp
->flags
&= ~BUF_PIN
;
713 save_bufp
->flags
&= ~BUF_PIN
;
720 save_bufp
->flags
&= ~BUF_PIN
;
723 bp
= (u_int16_t
*)rbufp
->page
;
724 if (bp
[ndx
+ 1] < REAL_KEY
) {
725 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
728 val
->data
= (u_char
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
729 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
733 if ((__delpair(hashp
, rbufp
, ndx
)) ||
734 (__addel(hashp
, rbufp
, key
, val
))) {
735 save_bufp
->flags
&= ~BUF_PIN
;
740 if (__delpair(hashp
, rbufp
, ndx
))
746 save_bufp
->flags
&= ~BUF_PIN
;
751 hash_seq(dbp
, key
, data
, flag
)
761 hashp
= (HTAB
*)dbp
->internal
;
762 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
763 hashp
->error
= errno
= EINVAL
;
766 #ifdef HASH_STATISTICS
769 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
775 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
776 if (!(bufp
= hashp
->cpage
)) {
777 for (bucket
= hashp
->cbucket
;
778 bucket
<= hashp
->MAX_BUCKET
;
779 bucket
++, hashp
->cndx
= 1) {
780 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
784 bp
= (u_int16_t
*)bufp
->page
;
788 hashp
->cbucket
= bucket
;
789 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
794 bp
= (u_int16_t
*)hashp
->cpage
->page
;
800 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
801 bufp
= hashp
->cpage
=
802 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
805 bp
= (u_int16_t
*)(bufp
->page
);
814 if (bp
[ndx
+ 1] < REAL_KEY
) {
815 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
818 key
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
];
819 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
820 data
->data
= (u_char
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
821 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
833 /********************************* UTILITIES ************************/
841 __expand_table(hashp
)
844 u_int32_t old_bucket
, new_bucket
;
845 int dirsize
, new_segnum
, spare_ndx
;
847 #ifdef HASH_STATISTICS
850 new_bucket
= ++hashp
->MAX_BUCKET
;
851 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
853 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
855 /* Check if we need a new segment */
856 if (new_segnum
>= hashp
->nsegs
) {
857 /* Check if we need to expand directory */
858 if (new_segnum
>= hashp
->DSIZE
) {
859 /* Reallocate directory */
860 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
861 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
863 hashp
->DSIZE
= dirsize
<< 1;
865 if ((hashp
->dir
[new_segnum
] =
866 (SEGMENT
)calloc(hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
872 * If the split point is increasing (MAX_BUCKET's log base 2
873 * * increases), we need to copy the current contents of the spare
874 * split bucket to the next bucket.
876 spare_ndx
= __log2(hashp
->MAX_BUCKET
+ 1);
877 if (spare_ndx
> hashp
->OVFL_POINT
) {
878 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
879 hashp
->OVFL_POINT
= spare_ndx
;
882 if (new_bucket
> hashp
->HIGH_MASK
) {
883 /* Starting a new doubling */
884 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
885 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
887 /* Relocate records to the new bucket */
888 return (__split_page(hashp
, old_bucket
, new_bucket
));
892 * If realloc guarantees that the pointer is not destroyed if the realloc
893 * fails, then this routine can go away.
896 hash_realloc(p_ptr
, oldsize
, newsize
)
898 int oldsize
, newsize
;
902 if ( (p
= malloc(newsize
)) ) {
903 memmove(p
, *p_ptr
, oldsize
);
904 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
912 __call_hash(hashp
, k
, len
)
919 n
= hashp
->hash(k
, len
);
920 bucket
= n
& hashp
->HIGH_MASK
;
921 if (bucket
> hashp
->MAX_BUCKET
)
922 bucket
= bucket
& hashp
->LOW_MASK
;
927 * Allocate segment table. On error, destroy the table and set errno.
929 * Returns 0 on success
932 alloc_segs(hashp
, nsegs
)
942 (SEGMENT
*)calloc(hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
944 (void)hdestroy(hashp
);
948 /* Allocate segments */
950 (SEGMENT
)calloc(nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
952 (void)hdestroy(hashp
);
956 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
957 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
961 #if BYTE_ORDER == LITTLE_ENDIAN
963 * Hashp->hdr needs to be byteswapped.
966 swap_header_copy(srcp
, destp
)
967 HASHHDR
*srcp
, *destp
;
971 P_32_COPY(srcp
->magic
, destp
->magic
);
972 P_32_COPY(srcp
->version
, destp
->version
);
973 P_32_COPY(srcp
->lorder
, destp
->lorder
);
974 P_32_COPY(srcp
->bsize
, destp
->bsize
);
975 P_32_COPY(srcp
->bshift
, destp
->bshift
);
976 P_32_COPY(srcp
->dsize
, destp
->dsize
);
977 P_32_COPY(srcp
->ssize
, destp
->ssize
);
978 P_32_COPY(srcp
->sshift
, destp
->sshift
);
979 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
980 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
981 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
982 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
983 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
984 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
985 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
986 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
987 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
988 for (i
= 0; i
< NCACHED
; i
++) {
989 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
990 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
1003 M_32_SWAP(hdrp
->magic
);
1004 M_32_SWAP(hdrp
->version
);
1005 M_32_SWAP(hdrp
->lorder
);
1006 M_32_SWAP(hdrp
->bsize
);
1007 M_32_SWAP(hdrp
->bshift
);
1008 M_32_SWAP(hdrp
->dsize
);
1009 M_32_SWAP(hdrp
->ssize
);
1010 M_32_SWAP(hdrp
->sshift
);
1011 M_32_SWAP(hdrp
->ovfl_point
);
1012 M_32_SWAP(hdrp
->last_freed
);
1013 M_32_SWAP(hdrp
->max_bucket
);
1014 M_32_SWAP(hdrp
->high_mask
);
1015 M_32_SWAP(hdrp
->low_mask
);
1016 M_32_SWAP(hdrp
->ffactor
);
1017 M_32_SWAP(hdrp
->nkeys
);
1018 M_32_SWAP(hdrp
->hdrpages
);
1019 M_32_SWAP(hdrp
->h_charkey
);
1020 for (i
= 0; i
< NCACHED
; i
++) {
1021 M_32_SWAP(hdrp
->spares
[i
]);
1022 M_16_SWAP(hdrp
->bitmaps
[i
]);