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. ***REMOVED*** - see
17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid
[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
37 #endif /* LIBC_SCCS and not lint */
39 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
40 #include <sys/param.h>
43 #if !defined(macintosh)
45 #include <sys/types.h>
50 #if defined(macintosh)
61 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
64 #if defined(_WIN32) || defined(_WINDOWS)
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
*, uint
));
82 static int hash_fd
__P((const DB
*));
83 static int hash_get
__P((const DB
*, const DBT
*, DBT
*, uint
));
84 static int hash_put
__P((const DB
*, DBT
*, const DBT
*, uint
));
85 static void *hash_realloc
__P((SEGMENT
**, size_t, size_t));
86 static int hash_seq
__P((const DB
*, DBT
*, DBT
*, uint
));
87 static int hash_sync
__P((const DB
*, uint
));
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; }
103 #define DBM_ERROR (-1)
106 #ifdef HASH_STATISTICS
107 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
110 /* A new Lou (montulli@mozilla.com) routine.
112 * The database is screwed.
114 * This closes the file, flushing buffers as appropriate.
117 __remove_database(DB
*dbp
)
119 HTAB
*hashp
= (HTAB
*)dbp
->internal
;
126 dbp
->internal
= NULL
;
129 /************************** INTERFACE ROUTINES ***************************/
134 __hash_open(const char *file
, int flags
, int mode
, const HASHINFO
*info
, int dflags
)
139 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
141 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
146 /* zero the statbuffer so that
147 * we can check it for a non-zero
148 * date to see if stat succeeded
150 memset(&statbuf
, 0, sizeof(struct stat
));
152 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
)))) {
158 hashp
->filename
= strdup(file
);
161 * Even if user wants write only, we need to be able to read
162 * the actual file, so we need to open it read/write. But, the
163 * field in the hashp structure needs to be accurate so that
164 * we can check accesses.
166 hashp
->flags
= flags
;
169 if (!file
|| (flags
& O_TRUNC
) || (stat(file
, &statbuf
) && (errno
== ENOENT
)))
172 errno
= 0; /* Just in case someone looks at errno */
175 else if(statbuf
.st_mtime
&& statbuf
.st_size
== 0)
177 /* check for a zero length file and delete it
182 hashp
->file_size
= statbuf
.st_size
;
185 #if defined(_WIN32) || defined(_WINDOWS) || defined (macintosh) || defined(XP_OS2)
186 if ((hashp
->fp
= DBFILE_OPEN(file
, flags
| O_BINARY
, mode
)) == -1)
187 RETURN_ERROR(errno
, error1
);
189 if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
190 RETURN_ERROR(errno
, error1
);
191 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
195 if (!init_hash(hashp
, file
, (HASHINFO
*)info
))
196 RETURN_ERROR(errno
, error1
);
198 /* Table already exists */
199 if (info
&& info
->hash
)
200 hashp
->hash
= info
->hash
;
202 hashp
->hash
= __default_hash
;
204 hdrsize
= read(hashp
->fp
, (char *)&hashp
->hdr
, sizeof(HASHHDR
));
206 RETURN_ERROR(errno
, error1
);
207 if (hdrsize
!= sizeof(HASHHDR
))
208 RETURN_ERROR(EFTYPE
, error1
);
209 #if BYTE_ORDER == LITTLE_ENDIAN
212 /* Verify file type, versions and hash function */
213 if (hashp
->MAGIC
!= HASHMAGIC
)
214 RETURN_ERROR(EFTYPE
, error1
);
215 #define OLDHASHVERSION 1
216 if (hashp
->VERSION
!= HASHVERSION
&&
217 hashp
->VERSION
!= OLDHASHVERSION
)
218 RETURN_ERROR(EFTYPE
, error1
);
219 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
220 RETURN_ERROR(EFTYPE
, error1
);
221 if (hashp
->NKEYS
< 0) /* Old bad database. */
222 RETURN_ERROR(EFTYPE
, error1
);
225 * Figure out how many segments we need. Max_Bucket is the
226 * maximum bucket number, so the number of buckets is
229 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
232 if (alloc_segs(hashp
, nsegs
))
233 /* If alloc_segs fails, errno will have been set. */
234 RETURN_ERROR(errno
, error1
);
235 /* Read in bitmaps */
236 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
237 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
238 (hashp
->BSHIFT
+ BYTE_SHIFT
);
240 hashp
->nmaps
= bpages
;
241 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(uint32
*));
244 /* Initialize Buffer Manager */
245 if (info
&& info
->cachesize
)
246 __buf_init(hashp
, (int32
) info
->cachesize
);
248 __buf_init(hashp
, DEF_BUFSIZE
);
250 hashp
->new_file
= new_table
;
252 hashp
->save_file
= file
&& !(hashp
->flags
& O_RDONLY
);
254 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
257 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
258 RETURN_ERROR(ENOMEM
, error1
);
260 dbp
->internal
= hashp
;
261 dbp
->close
= hash_close
;
262 dbp
->del
= hash_delete
;
267 dbp
->sync
= hash_sync
;
270 #ifdef HASH_STATISTICS
271 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
290 hashp
= (HTAB
*)dbp
->internal
;
294 retval
= hdestroy(hashp
);
299 static int hash_fd(const DB
*dbp
)
306 hashp
= (HTAB
*)dbp
->internal
;
310 if (hashp
->fp
== -1) {
317 /************************** LOCAL CREATION ROUTINES **********************/
319 init_hash(HTAB
*hashp
, const char *file
, HASHINFO
*info
)
326 hashp
->LORDER
= BYTE_ORDER
;
327 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
328 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
329 hashp
->SGSIZE
= DEF_SEGSIZE
;
330 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
331 hashp
->DSIZE
= DEF_DIRSIZE
;
332 hashp
->FFACTOR
= DEF_FFACTOR
;
333 hashp
->hash
= __default_hash
;
334 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
335 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
337 /* Fix bucket size to be optimal for file system */
339 if (stat(file
, &statbuf
))
342 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2)
343 #if defined(__QNX__) && !defined(__QNXNTO__)
344 hashp
->BSIZE
= 512; /* preferred blk size on qnx4 */
346 hashp
->BSIZE
= statbuf
.st_blksize
;
349 /* new code added by Lou to reduce block
350 * size down below MAX_BSIZE
352 if (hashp
->BSIZE
> MAX_BSIZE
)
353 hashp
->BSIZE
= MAX_BSIZE
;
355 hashp
->BSHIFT
= __log2((uint32
)hashp
->BSIZE
);
360 /* Round pagesize up to power of 2 */
361 hashp
->BSHIFT
= __log2(info
->bsize
);
362 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
363 if (hashp
->BSIZE
> MAX_BSIZE
) {
369 hashp
->FFACTOR
= info
->ffactor
;
371 hashp
->hash
= info
->hash
;
375 if (info
->lorder
!= BIG_ENDIAN
&&
376 info
->lorder
!= LITTLE_ENDIAN
) {
380 hashp
->LORDER
= info
->lorder
;
383 /* init_htab sets errno if it fails */
384 if (init_htab(hashp
, nelem
))
390 * This calls alloc_segs which may run out of memory. Alloc_segs will
391 * set errno, so we just pass the error information along.
393 * Returns 0 on No Error
396 init_htab(HTAB
*hashp
, int nelem
)
398 register int nbuckets
, nsegs
;
402 * Divide number of elements by the fill factor and determine a
403 * desired number of buckets. Allocate space for the next greater
404 * power of two number of buckets.
406 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
408 l2
= __log2((uint32
)PR_MAX(nelem
, 2));
411 hashp
->SPARES
[l2
] = l2
+ 1;
412 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
413 hashp
->OVFL_POINT
= l2
;
414 hashp
->LAST_FREED
= 2;
416 /* First bitmap page is at: splitpoint l2 page offset 1 */
417 if (__ibitmap(hashp
, (int)OADDR_OF(l2
, 1), l2
+ 1, 0))
420 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
421 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
422 hashp
->HDRPAGES
= ((PR_MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
425 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
426 nsegs
= 1 << __log2((uint32
)nsegs
);
428 if (nsegs
> hashp
->DSIZE
)
429 hashp
->DSIZE
= nsegs
;
430 return (alloc_segs(hashp
, nsegs
));
433 /********************** DESTROY/CLOSE ROUTINES ************************/
436 * Flushes any changes to the file if necessary and destroys the hashp
437 * structure, freeing all allocated space.
440 hdestroy(HTAB
*hashp
)
446 #ifdef HASH_STATISTICS
447 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
448 hash_accesses
, hash_collisions
);
449 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
451 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
453 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
454 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
456 for (i
= 0; i
< NCACHED
; i
++)
457 (void)fprintf(stderr
,
458 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
461 * Call on buffer manager to free buffers, and if required,
462 * write them to disk.
464 if (__buf_free(hashp
, 1, hashp
->save_file
))
467 free(*hashp
->dir
); /* Free initial segments */
468 /* Free extra segments */
469 while (hashp
->exsegs
--)
470 free(hashp
->dir
[--hashp
->nsegs
]);
473 if (flush_meta(hashp
) && !save_errno
)
476 for (i
= 0; i
< hashp
->nmaps
; i
++)
478 free(hashp
->mapp
[i
]);
481 (void)close(hashp
->fp
);
483 if(hashp
->filename
) {
484 #if defined(_WIN32) || defined(_WINDOWS) || defined(XP_OS2)
486 (void)unlink(hashp
->filename
);
488 free(hashp
->filename
);
491 free(hashp
->tmp_buf
);
493 free(hashp
->tmp_key
);
502 #if defined(_WIN32) || defined(_WINDOWS)
504 * Close and reopen file to force file length update on windows.
511 update_EOF(HTAB
*hashp
)
513 #if defined(DBM_REOPEN_ON_FLUSH)
514 char * file
= hashp
->filename
;
520 memset(&statbuf
, 0, sizeof statbuf
);
522 /* make sure we won't lose the file by closing it. */
523 if (!file
|| (stat(file
, &statbuf
) && (errno
== ENOENT
))) {
524 /* pretend we did it. */
528 (void)close(hashp
->fp
);
530 flags
= hashp
->flags
& ~(O_TRUNC
| O_CREAT
| O_EXCL
);
532 if ((hashp
->fp
= DBFILE_OPEN(file
, flags
| O_BINARY
, mode
)) == -1)
534 file_size
= lseek(hashp
->fp
, (off_t
)0, SEEK_END
);
537 hashp
->file_size
= file_size
;
541 off_t file_size
= lseek(fd
, (off_t
)0, SEEK_END
);
542 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
543 BOOL cool
= FlushFileBuffers(handle
);
546 DWORD err
= GetLastError();
547 (void)fprintf(stderr
,
548 "FlushFileBuffers failed, last error = %d, 0x%08x\n",
554 hashp
->file_size
= file_size
;
555 return cool
? 0 : -1;
561 * Write modified pages to disk
568 hash_sync(const DB
*dbp
, uint flags
)
580 hashp
= (HTAB
*)dbp
->internal
;
584 if (!hashp
->save_file
)
586 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
588 #if defined(_WIN32) || defined(_WINDOWS)
589 if (hashp
->updateEOF
&& hashp
->filename
&& !hashp
->is_temp
) {
590 int status
= update_EOF(hashp
);
591 hashp
->updateEOF
= 0;
603 * -1 indicates that errno should be set
606 flush_meta(HTAB
*hashp
)
609 #if BYTE_ORDER == LITTLE_ENDIAN
614 if (!hashp
->save_file
)
616 hashp
->MAGIC
= HASHMAGIC
;
617 hashp
->VERSION
= HASHVERSION
;
618 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
622 #if BYTE_ORDER == LITTLE_ENDIAN
624 swap_header_copy(&hashp
->hdr
, whdrp
);
626 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
627 ((wsize
= write(fp
, (char*)whdrp
, sizeof(HASHHDR
))) == -1))
630 if (wsize
!= sizeof(HASHHDR
)) {
632 hashp
->dbmerrno
= errno
;
635 for (i
= 0; i
< NCACHED
; i
++)
637 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
638 hashp
->BITMAPS
[i
], 0, 1))
643 /*******************************SEARCH ROUTINES *****************************/
645 * All the access routines return
649 * 1 to indicate an external DBM_ERROR (i.e. key not found, etc)
650 * -1 to indicate an internal DBM_ERROR (i.e. out of memory, etc)
662 hashp
= (HTAB
*)dbp
->internal
;
667 hashp
->dbmerrno
= errno
= EINVAL
;
671 rv
= hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
);
673 if(rv
== DATABASE_CORRUPTED_ERROR
)
675 #if defined(unix) && defined(DEBUG)
676 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
678 __remove_database((DB
*)dbp
);
694 hashp
= (HTAB
*)dbp
->internal
;
698 if (flag
&& flag
!= R_NOOVERWRITE
) {
699 hashp
->dbmerrno
= errno
= EINVAL
;
702 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
703 hashp
->dbmerrno
= errno
= EPERM
;
707 rv
= hash_access(hashp
, flag
== R_NOOVERWRITE
?
708 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
);
710 if(rv
== DATABASE_CORRUPTED_ERROR
)
712 #if defined(unix) && defined(DEBUG)
713 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
715 __remove_database((DB
*)dbp
);
725 uint flag
) /* Ignored */
730 hashp
= (HTAB
*)dbp
->internal
;
734 if (flag
&& flag
!= R_CURSOR
) {
735 hashp
->dbmerrno
= errno
= EINVAL
;
738 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
739 hashp
->dbmerrno
= errno
= EPERM
;
742 rv
= hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
);
744 if(rv
== DATABASE_CORRUPTED_ERROR
)
746 #if defined(unix) && defined(DEBUG)
747 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
749 __remove_database((DB
*)dbp
);
755 #define MAX_OVERFLOW_HASH_ACCESS_LOOPS 2000
757 * Assume that hashp has been set in wrapper routine.
765 register BUFHEAD
*rbufp
;
766 BUFHEAD
*bufp
, *save_bufp
;
768 register long n
, ndx
, off
;
769 register size_t size
;
772 uint32 ovfl_loop_count
=0;
773 int32 last_overflow_page_no
= -1;
775 #ifdef HASH_STATISTICS
781 kp
= (char *)key
->data
;
782 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
784 return (DATABASE_CORRUPTED_ERROR
);
787 /* Pin the bucket chain */
788 rbufp
->flags
|= BUF_PIN
;
789 for (bp
= (uint16
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
792 if (bp
[1] >= REAL_KEY
) {
793 /* Real key/data pair */
794 if (size
== (unsigned long)(off
- *bp
) &&
795 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
798 #ifdef HASH_STATISTICS
803 } else if (bp
[1] == OVFLPAGE
) {
805 /* database corruption: overflow loop detection */
806 if(last_overflow_page_no
== (int32
)*bp
)
807 return (DATABASE_CORRUPTED_ERROR
);
809 last_overflow_page_no
= *bp
;
811 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
813 save_bufp
->flags
&= ~BUF_PIN
;
818 if(ovfl_loop_count
> MAX_OVERFLOW_HASH_ACCESS_LOOPS
)
819 return (DATABASE_CORRUPTED_ERROR
);
822 bp
= (uint16
*)rbufp
->page
;
826 } else if (bp
[1] < REAL_KEY
) {
828 __find_bigpair(hashp
, rbufp
, ndx
, kp
, (int)size
)) > 0)
833 __find_last_page(hashp
, &bufp
))) {
838 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
840 save_bufp
->flags
&= ~BUF_PIN
;
844 bp
= (uint16
*)rbufp
->page
;
849 save_bufp
->flags
&= ~BUF_PIN
;
860 if (__addel(hashp
, rbufp
, key
, val
)) {
861 save_bufp
->flags
&= ~BUF_PIN
;
864 save_bufp
->flags
&= ~BUF_PIN
;
870 save_bufp
->flags
&= ~BUF_PIN
;
877 save_bufp
->flags
&= ~BUF_PIN
;
880 bp
= (uint16
*)rbufp
->page
;
881 if (bp
[ndx
+ 1] < REAL_KEY
) {
882 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
885 val
->data
= (uint8
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
886 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
890 if ((__delpair(hashp
, rbufp
, ndx
)) ||
891 (__addel(hashp
, rbufp
, key
, val
))) {
892 save_bufp
->flags
&= ~BUF_PIN
;
897 if (__delpair(hashp
, rbufp
, ndx
))
903 save_bufp
->flags
&= ~BUF_PIN
;
913 register uint32 bucket
;
914 register BUFHEAD
*bufp
;
918 hashp
= (HTAB
*)dbp
->internal
;
922 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
923 hashp
->dbmerrno
= errno
= EINVAL
;
926 #ifdef HASH_STATISTICS
929 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
935 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
936 if (!(bufp
= hashp
->cpage
)) {
937 for (bucket
= hashp
->cbucket
;
938 bucket
<= (uint32
)hashp
->MAX_BUCKET
;
939 bucket
++, hashp
->cndx
= 1) {
940 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
944 bp
= (uint16
*)bufp
->page
;
948 hashp
->cbucket
= bucket
;
949 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
954 bp
= (uint16
*)hashp
->cpage
->page
;
960 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
961 bufp
= hashp
->cpage
=
962 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
965 bp
= (uint16
*)(bufp
->page
);
974 if (bp
[ndx
+ 1] < REAL_KEY
) {
975 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
978 key
->data
= (uint8
*)hashp
->cpage
->page
+ bp
[ndx
];
979 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
980 data
->data
= (uint8
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
981 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
993 /********************************* UTILITIES ************************/
1001 __expand_table(HTAB
*hashp
)
1003 uint32 old_bucket
, new_bucket
;
1004 int new_segnum
, spare_ndx
;
1007 #ifdef HASH_STATISTICS
1010 new_bucket
= ++hashp
->MAX_BUCKET
;
1011 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
1013 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
1015 /* Check if we need a new segment */
1016 if (new_segnum
>= hashp
->nsegs
) {
1017 /* Check if we need to expand directory */
1018 if (new_segnum
>= hashp
->DSIZE
) {
1019 /* Reallocate directory */
1020 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
1021 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
1023 hashp
->DSIZE
= dirsize
<< 1;
1025 if ((hashp
->dir
[new_segnum
] =
1026 (SEGMENT
)calloc((size_t)hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
1032 * If the split point is increasing (MAX_BUCKET's log base 2
1033 * * increases), we need to copy the current contents of the spare
1034 * split bucket to the next bucket.
1036 spare_ndx
= __log2((uint32
)(hashp
->MAX_BUCKET
+ 1));
1037 if (spare_ndx
> hashp
->OVFL_POINT
) {
1038 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
1039 hashp
->OVFL_POINT
= spare_ndx
;
1042 if (new_bucket
> (uint32
)hashp
->HIGH_MASK
) {
1043 /* Starting a new doubling */
1044 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
1045 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
1047 /* Relocate records to the new bucket */
1048 return (__split_page(hashp
, old_bucket
, new_bucket
));
1052 * If realloc guarantees that the pointer is not destroyed if the realloc
1053 * fails, then this routine can go away.
1058 size_t oldsize
, size_t newsize
)
1062 if ((p
= malloc(newsize
))) {
1063 memmove(p
, *p_ptr
, oldsize
);
1064 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
1066 *p_ptr
= (SEGMENT
*)p
;
1072 __call_hash(HTAB
*hashp
, char *k
, size_t len
)
1076 n
= hashp
->hash(k
, len
);
1077 bucket
= n
& hashp
->HIGH_MASK
;
1078 if (bucket
> (uint32
)hashp
->MAX_BUCKET
)
1079 bucket
= bucket
& hashp
->LOW_MASK
;
1084 * Allocate segment table. On error, set errno.
1086 * Returns 0 on success
1094 register SEGMENT store
;
1097 (SEGMENT
*)calloc((size_t)hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
1101 /* Allocate segments */
1103 (SEGMENT
)calloc((size_t)nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
1107 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
1108 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
1112 #if BYTE_ORDER == LITTLE_ENDIAN
1114 * Hashp->hdr needs to be byteswapped.
1118 HASHHDR
*srcp
, HASHHDR
*destp
)
1122 P_32_COPY(srcp
->magic
, destp
->magic
);
1123 P_32_COPY(srcp
->version
, destp
->version
);
1124 P_32_COPY(srcp
->lorder
, destp
->lorder
);
1125 P_32_COPY(srcp
->bsize
, destp
->bsize
);
1126 P_32_COPY(srcp
->bshift
, destp
->bshift
);
1127 P_32_COPY(srcp
->dsize
, destp
->dsize
);
1128 P_32_COPY(srcp
->ssize
, destp
->ssize
);
1129 P_32_COPY(srcp
->sshift
, destp
->sshift
);
1130 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
1131 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
1132 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
1133 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
1134 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
1135 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
1136 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
1137 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
1138 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
1139 for (i
= 0; i
< NCACHED
; i
++) {
1140 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
1141 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
1146 swap_header(HTAB
*hashp
)
1153 M_32_SWAP(hdrp
->magic
);
1154 M_32_SWAP(hdrp
->version
);
1155 M_32_SWAP(hdrp
->lorder
);
1156 M_32_SWAP(hdrp
->bsize
);
1157 M_32_SWAP(hdrp
->bshift
);
1158 M_32_SWAP(hdrp
->dsize
);
1159 M_32_SWAP(hdrp
->ssize
);
1160 M_32_SWAP(hdrp
->sshift
);
1161 M_32_SWAP(hdrp
->ovfl_point
);
1162 M_32_SWAP(hdrp
->last_freed
);
1163 M_32_SWAP(hdrp
->max_bucket
);
1164 M_32_SWAP(hdrp
->high_mask
);
1165 M_32_SWAP(hdrp
->low_mask
);
1166 M_32_SWAP(hdrp
->ffactor
);
1167 M_32_SWAP(hdrp
->nkeys
);
1168 M_32_SWAP(hdrp
->hdrpages
);
1169 M_32_SWAP(hdrp
->h_charkey
);
1170 for (i
= 0; i
< NCACHED
; i
++) {
1171 M_32_SWAP(hdrp
->spares
[i
]);
1172 M_16_SWAP(hdrp
->bitmaps
[i
]);