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 */
41 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
42 #include <sys/param.h>
45 #if !defined(macintosh)
47 #include <sys/types.h>
52 #if defined(macintosh)
63 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
66 #if defined(_WIN32) || defined(_WINDOWS)
79 static int alloc_segs
__P((HTAB
*, int));
80 static int flush_meta
__P((HTAB
*));
81 static int hash_access
__P((HTAB
*, ACTION
, DBT
*, DBT
*));
82 static int hash_close
__P((DB
*));
83 static int hash_delete
__P((const DB
*, const DBT
*, uint
));
84 static int hash_fd
__P((const DB
*));
85 static int hash_get
__P((const DB
*, const DBT
*, DBT
*, uint
));
86 static int hash_put
__P((const DB
*, DBT
*, const DBT
*, uint
));
87 static void *hash_realloc
__P((SEGMENT
**, size_t, size_t));
88 static int hash_seq
__P((const DB
*, DBT
*, DBT
*, uint
));
89 static int hash_sync
__P((const DB
*, uint
));
90 static int hdestroy
__P((HTAB
*));
91 static HTAB
*init_hash
__P((HTAB
*, const char *, HASHINFO
*));
92 static int init_htab
__P((HTAB
*, int));
93 #if BYTE_ORDER == LITTLE_ENDIAN
94 static void swap_header
__P((HTAB
*));
95 static void swap_header_copy
__P((HASHHDR
*, HASHHDR
*));
98 /* Fast arithmetic, relying on powers of 2, */
99 #define MOD(x, y) ((x) & ((y) - 1))
101 #define RETURN_ERROR(ERR, LOC) { save_errno = ERR; goto LOC; }
105 #define DBM_ERROR (-1)
108 #ifdef HASH_STATISTICS
109 int hash_accesses
, hash_collisions
, hash_expansions
, hash_overflows
;
112 /* A new Lou (montulli@mozilla.com) routine.
114 * The database is screwed.
116 * This closes the file, flushing buffers as appropriate.
119 __remove_database(DB
*dbp
)
121 HTAB
*hashp
= (HTAB
*)dbp
->internal
;
128 dbp
->internal
= NULL
;
131 /************************** INTERFACE ROUTINES ***************************/
136 __hash_open(const char *file
, int flags
, int mode
, const HASHINFO
*info
, int dflags
)
141 int bpages
, hdrsize
, new_table
, nsegs
, save_errno
;
143 if ((flags
& O_ACCMODE
) == O_WRONLY
) {
148 /* zero the statbuffer so that
149 * we can check it for a non-zero
150 * date to see if stat succeeded
152 memset(&statbuf
, 0, sizeof(struct stat
));
154 if (!(hashp
= (HTAB
*)calloc(1, sizeof(HTAB
)))) {
160 hashp
->filename
= strdup(file
);
163 * Even if user wants write only, we need to be able to read
164 * the actual file, so we need to open it read/write. But, the
165 * field in the hashp structure needs to be accurate so that
166 * we can check accesses.
168 hashp
->flags
= flags
;
171 if (!file
|| (flags
& O_TRUNC
) || (stat(file
, &statbuf
) && (errno
== ENOENT
)))
174 errno
= 0; /* Just in case someone looks at errno */
177 else if(statbuf
.st_mtime
&& statbuf
.st_size
== 0)
179 /* check for a zero length file and delete it
184 hashp
->file_size
= statbuf
.st_size
;
187 #if defined(_WIN32) || defined(_WINDOWS) || defined (macintosh) || defined(XP_OS2)
188 if ((hashp
->fp
= DBFILE_OPEN(file
, flags
| O_BINARY
, mode
)) == -1)
189 RETURN_ERROR(errno
, error1
);
191 if ((hashp
->fp
= open(file
, flags
, mode
)) == -1)
192 RETURN_ERROR(errno
, error1
);
193 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
197 if (!init_hash(hashp
, file
, (HASHINFO
*)info
))
198 RETURN_ERROR(errno
, error1
);
200 /* Table already exists */
201 if (info
&& info
->hash
)
202 hashp
->hash
= info
->hash
;
204 hashp
->hash
= __default_hash
;
206 hdrsize
= read(hashp
->fp
, (char *)&hashp
->hdr
, sizeof(HASHHDR
));
208 RETURN_ERROR(errno
, error1
);
209 if (hdrsize
!= sizeof(HASHHDR
))
210 RETURN_ERROR(EFTYPE
, error1
);
211 #if BYTE_ORDER == LITTLE_ENDIAN
214 /* Verify file type, versions and hash function */
215 if (hashp
->MAGIC
!= HASHMAGIC
)
216 RETURN_ERROR(EFTYPE
, error1
);
217 #define OLDHASHVERSION 1
218 if (hashp
->VERSION
!= HASHVERSION
&&
219 hashp
->VERSION
!= OLDHASHVERSION
)
220 RETURN_ERROR(EFTYPE
, error1
);
221 if (hashp
->hash(CHARKEY
, sizeof(CHARKEY
)) != hashp
->H_CHARKEY
)
222 RETURN_ERROR(EFTYPE
, error1
);
223 if (hashp
->NKEYS
< 0) /* Old bad database. */
224 RETURN_ERROR(EFTYPE
, error1
);
227 * Figure out how many segments we need. Max_Bucket is the
228 * maximum bucket number, so the number of buckets is
231 nsegs
= (hashp
->MAX_BUCKET
+ 1 + hashp
->SGSIZE
- 1) /
234 if (alloc_segs(hashp
, nsegs
))
235 /* If alloc_segs fails, errno will have been set. */
236 RETURN_ERROR(errno
, error1
);
237 /* Read in bitmaps */
238 bpages
= (hashp
->SPARES
[hashp
->OVFL_POINT
] +
239 (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) >>
240 (hashp
->BSHIFT
+ BYTE_SHIFT
);
242 hashp
->nmaps
= bpages
;
243 (void)memset(&hashp
->mapp
[0], 0, bpages
* sizeof(uint32
*));
246 /* Initialize Buffer Manager */
247 if (info
&& info
->cachesize
)
248 __buf_init(hashp
, (int32
) info
->cachesize
);
250 __buf_init(hashp
, DEF_BUFSIZE
);
252 hashp
->new_file
= new_table
;
254 hashp
->save_file
= file
&& !(hashp
->flags
& O_RDONLY
);
256 hashp
->save_file
= file
&& (hashp
->flags
& O_RDWR
);
259 if (!(dbp
= (DB
*)malloc(sizeof(DB
)))) {
260 RETURN_ERROR(ENOMEM
, error1
);
262 dbp
->internal
= hashp
;
263 dbp
->close
= hash_close
;
264 dbp
->del
= hash_delete
;
269 dbp
->sync
= hash_sync
;
272 #ifdef HASH_STATISTICS
273 hash_overflows
= hash_accesses
= hash_collisions
= hash_expansions
= 0;
292 hashp
= (HTAB
*)dbp
->internal
;
296 retval
= hdestroy(hashp
);
301 static int hash_fd(const DB
*dbp
)
308 hashp
= (HTAB
*)dbp
->internal
;
312 if (hashp
->fp
== -1) {
319 /************************** LOCAL CREATION ROUTINES **********************/
321 init_hash(HTAB
*hashp
, const char *file
, HASHINFO
*info
)
328 hashp
->LORDER
= BYTE_ORDER
;
329 hashp
->BSIZE
= DEF_BUCKET_SIZE
;
330 hashp
->BSHIFT
= DEF_BUCKET_SHIFT
;
331 hashp
->SGSIZE
= DEF_SEGSIZE
;
332 hashp
->SSHIFT
= DEF_SEGSIZE_SHIFT
;
333 hashp
->DSIZE
= DEF_DIRSIZE
;
334 hashp
->FFACTOR
= DEF_FFACTOR
;
335 hashp
->hash
= __default_hash
;
336 memset(hashp
->SPARES
, 0, sizeof(hashp
->SPARES
));
337 memset(hashp
->BITMAPS
, 0, sizeof (hashp
->BITMAPS
));
339 /* Fix bucket size to be optimal for file system */
341 if (stat(file
, &statbuf
))
344 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(VMS) && !defined(XP_OS2)
345 #if defined(__QNX__) && !defined(__QNXNTO__)
346 hashp
->BSIZE
= 512; /* preferred blk size on qnx4 */
348 hashp
->BSIZE
= statbuf
.st_blksize
;
351 /* new code added by Lou to reduce block
352 * size down below MAX_BSIZE
354 if (hashp
->BSIZE
> MAX_BSIZE
)
355 hashp
->BSIZE
= MAX_BSIZE
;
357 hashp
->BSHIFT
= __log2((uint32
)hashp
->BSIZE
);
362 /* Round pagesize up to power of 2 */
363 hashp
->BSHIFT
= __log2(info
->bsize
);
364 hashp
->BSIZE
= 1 << hashp
->BSHIFT
;
365 if (hashp
->BSIZE
> MAX_BSIZE
) {
371 hashp
->FFACTOR
= info
->ffactor
;
373 hashp
->hash
= info
->hash
;
377 if (info
->lorder
!= BIG_ENDIAN
&&
378 info
->lorder
!= LITTLE_ENDIAN
) {
382 hashp
->LORDER
= info
->lorder
;
385 /* init_htab sets errno if it fails */
386 if (init_htab(hashp
, nelem
))
392 * This calls alloc_segs which may run out of memory. Alloc_segs will
393 * set errno, so we just pass the error information along.
395 * Returns 0 on No Error
398 init_htab(HTAB
*hashp
, int nelem
)
400 register int nbuckets
, nsegs
;
404 * Divide number of elements by the fill factor and determine a
405 * desired number of buckets. Allocate space for the next greater
406 * power of two number of buckets.
408 nelem
= (nelem
- 1) / hashp
->FFACTOR
+ 1;
410 l2
= __log2((uint32
)PR_MAX(nelem
, 2));
413 hashp
->SPARES
[l2
] = l2
+ 1;
414 hashp
->SPARES
[l2
+ 1] = l2
+ 1;
415 hashp
->OVFL_POINT
= l2
;
416 hashp
->LAST_FREED
= 2;
418 /* First bitmap page is at: splitpoint l2 page offset 1 */
419 if (__ibitmap(hashp
, (int)OADDR_OF(l2
, 1), l2
+ 1, 0))
422 hashp
->MAX_BUCKET
= hashp
->LOW_MASK
= nbuckets
- 1;
423 hashp
->HIGH_MASK
= (nbuckets
<< 1) - 1;
424 hashp
->HDRPAGES
= ((PR_MAX(sizeof(HASHHDR
), MINHDRSIZE
) - 1) >>
427 nsegs
= (nbuckets
- 1) / hashp
->SGSIZE
+ 1;
428 nsegs
= 1 << __log2((uint32
)nsegs
);
430 if (nsegs
> hashp
->DSIZE
)
431 hashp
->DSIZE
= nsegs
;
432 return (alloc_segs(hashp
, nsegs
));
435 /********************** DESTROY/CLOSE ROUTINES ************************/
438 * Flushes any changes to the file if necessary and destroys the hashp
439 * structure, freeing all allocated space.
442 hdestroy(HTAB
*hashp
)
448 #ifdef HASH_STATISTICS
449 (void)fprintf(stderr
, "hdestroy: accesses %ld collisions %ld\n",
450 hash_accesses
, hash_collisions
);
451 (void)fprintf(stderr
, "hdestroy: expansions %ld\n",
453 (void)fprintf(stderr
, "hdestroy: overflows %ld\n",
455 (void)fprintf(stderr
, "keys %ld maxp %d segmentcount %d\n",
456 hashp
->NKEYS
, hashp
->MAX_BUCKET
, hashp
->nsegs
);
458 for (i
= 0; i
< NCACHED
; i
++)
459 (void)fprintf(stderr
,
460 "spares[%d] = %d\n", i
, hashp
->SPARES
[i
]);
463 * Call on buffer manager to free buffers, and if required,
464 * write them to disk.
466 if (__buf_free(hashp
, 1, hashp
->save_file
))
469 free(*hashp
->dir
); /* Free initial segments */
470 /* Free extra segments */
471 while (hashp
->exsegs
--)
472 free(hashp
->dir
[--hashp
->nsegs
]);
475 if (flush_meta(hashp
) && !save_errno
)
478 for (i
= 0; i
< hashp
->nmaps
; i
++)
480 free(hashp
->mapp
[i
]);
483 (void)close(hashp
->fp
);
485 if(hashp
->filename
) {
486 #if defined(_WIN32) || defined(_WINDOWS) || defined(XP_OS2)
488 (void)unlink(hashp
->filename
);
490 free(hashp
->filename
);
493 free(hashp
->tmp_buf
);
495 free(hashp
->tmp_key
);
504 #if defined(_WIN32) || defined(_WINDOWS)
506 * Close and reopen file to force file length update on windows.
513 update_EOF(HTAB
*hashp
)
515 #if defined(DBM_REOPEN_ON_FLUSH)
516 char * file
= hashp
->filename
;
522 memset(&statbuf
, 0, sizeof statbuf
);
524 /* make sure we won't lose the file by closing it. */
525 if (!file
|| (stat(file
, &statbuf
) && (errno
== ENOENT
))) {
526 /* pretend we did it. */
530 (void)close(hashp
->fp
);
532 flags
= hashp
->flags
& ~(O_TRUNC
| O_CREAT
| O_EXCL
);
534 if ((hashp
->fp
= DBFILE_OPEN(file
, flags
| O_BINARY
, mode
)) == -1)
536 file_size
= lseek(hashp
->fp
, (off_t
)0, SEEK_END
);
539 hashp
->file_size
= file_size
;
543 off_t file_size
= lseek(fd
, (off_t
)0, SEEK_END
);
544 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
545 BOOL cool
= FlushFileBuffers(handle
);
548 DWORD err
= GetLastError();
549 (void)fprintf(stderr
,
550 "FlushFileBuffers failed, last error = %d, 0x%08x\n",
556 hashp
->file_size
= file_size
;
557 return cool
? 0 : -1;
563 * Write modified pages to disk
570 hash_sync(const DB
*dbp
, uint flags
)
582 hashp
= (HTAB
*)dbp
->internal
;
586 if (!hashp
->save_file
)
588 if (__buf_free(hashp
, 0, 1) || flush_meta(hashp
))
590 #if defined(_WIN32) || defined(_WINDOWS)
591 if (hashp
->updateEOF
&& hashp
->filename
&& !hashp
->is_temp
) {
592 int status
= update_EOF(hashp
);
593 hashp
->updateEOF
= 0;
605 * -1 indicates that errno should be set
608 flush_meta(HTAB
*hashp
)
611 #if BYTE_ORDER == LITTLE_ENDIAN
616 if (!hashp
->save_file
)
618 hashp
->MAGIC
= HASHMAGIC
;
619 hashp
->VERSION
= HASHVERSION
;
620 hashp
->H_CHARKEY
= hashp
->hash(CHARKEY
, sizeof(CHARKEY
));
624 #if BYTE_ORDER == LITTLE_ENDIAN
626 swap_header_copy(&hashp
->hdr
, whdrp
);
628 if ((lseek(fp
, (off_t
)0, SEEK_SET
) == -1) ||
629 ((wsize
= write(fp
, (char*)whdrp
, sizeof(HASHHDR
))) == -1))
632 if (wsize
!= sizeof(HASHHDR
)) {
634 hashp
->dbmerrno
= errno
;
637 for (i
= 0; i
< NCACHED
; i
++)
639 if (__put_page(hashp
, (char *)hashp
->mapp
[i
],
640 hashp
->BITMAPS
[i
], 0, 1))
645 /*******************************SEARCH ROUTINES *****************************/
647 * All the access routines return
651 * 1 to indicate an external DBM_ERROR (i.e. key not found, etc)
652 * -1 to indicate an internal DBM_ERROR (i.e. out of memory, etc)
664 hashp
= (HTAB
*)dbp
->internal
;
669 hashp
->dbmerrno
= errno
= EINVAL
;
673 rv
= hash_access(hashp
, HASH_GET
, (DBT
*)key
, data
);
675 if(rv
== DATABASE_CORRUPTED_ERROR
)
677 #if defined(unix) && defined(DEBUG)
678 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
680 __remove_database((DB
*)dbp
);
696 hashp
= (HTAB
*)dbp
->internal
;
700 if (flag
&& flag
!= R_NOOVERWRITE
) {
701 hashp
->dbmerrno
= errno
= EINVAL
;
704 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
705 hashp
->dbmerrno
= errno
= EPERM
;
709 rv
= hash_access(hashp
, flag
== R_NOOVERWRITE
?
710 HASH_PUTNEW
: HASH_PUT
, (DBT
*)key
, (DBT
*)data
);
712 if(rv
== DATABASE_CORRUPTED_ERROR
)
714 #if defined(unix) && defined(DEBUG)
715 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
717 __remove_database((DB
*)dbp
);
727 uint flag
) /* Ignored */
732 hashp
= (HTAB
*)dbp
->internal
;
736 if (flag
&& flag
!= R_CURSOR
) {
737 hashp
->dbmerrno
= errno
= EINVAL
;
740 if ((hashp
->flags
& O_ACCMODE
) == O_RDONLY
) {
741 hashp
->dbmerrno
= errno
= EPERM
;
744 rv
= hash_access(hashp
, HASH_DELETE
, (DBT
*)key
, NULL
);
746 if(rv
== DATABASE_CORRUPTED_ERROR
)
748 #if defined(unix) && defined(DEBUG)
749 printf("\n\nDBM Database has been corrupted, tell Lou...\n\n");
751 __remove_database((DB
*)dbp
);
757 #define MAX_OVERFLOW_HASH_ACCESS_LOOPS 2000
759 * Assume that hashp has been set in wrapper routine.
767 register BUFHEAD
*rbufp
;
768 BUFHEAD
*bufp
, *save_bufp
;
770 register long n
, ndx
, off
;
771 register size_t size
;
774 uint32 ovfl_loop_count
=0;
775 int32 last_overflow_page_no
= -1;
777 #ifdef HASH_STATISTICS
783 kp
= (char *)key
->data
;
784 rbufp
= __get_buf(hashp
, __call_hash(hashp
, kp
, size
), NULL
, 0);
786 return (DATABASE_CORRUPTED_ERROR
);
789 /* Pin the bucket chain */
790 rbufp
->flags
|= BUF_PIN
;
791 for (bp
= (uint16
*)rbufp
->page
, n
= *bp
++, ndx
= 1; ndx
< n
;)
794 if (bp
[1] >= REAL_KEY
) {
795 /* Real key/data pair */
796 if (size
== (unsigned long)(off
- *bp
) &&
797 memcmp(kp
, rbufp
->page
+ *bp
, size
) == 0)
800 #ifdef HASH_STATISTICS
805 } else if (bp
[1] == OVFLPAGE
) {
807 /* database corruption: overflow loop detection */
808 if(last_overflow_page_no
== (int32
)*bp
)
809 return (DATABASE_CORRUPTED_ERROR
);
811 last_overflow_page_no
= *bp
;
813 rbufp
= __get_buf(hashp
, *bp
, rbufp
, 0);
815 save_bufp
->flags
&= ~BUF_PIN
;
820 if(ovfl_loop_count
> MAX_OVERFLOW_HASH_ACCESS_LOOPS
)
821 return (DATABASE_CORRUPTED_ERROR
);
824 bp
= (uint16
*)rbufp
->page
;
828 } else if (bp
[1] < REAL_KEY
) {
830 __find_bigpair(hashp
, rbufp
, ndx
, kp
, (int)size
)) > 0)
835 __find_last_page(hashp
, &bufp
))) {
840 rbufp
= __get_buf(hashp
, pageno
, bufp
, 0);
842 save_bufp
->flags
&= ~BUF_PIN
;
846 bp
= (uint16
*)rbufp
->page
;
851 save_bufp
->flags
&= ~BUF_PIN
;
862 if (__addel(hashp
, rbufp
, key
, val
)) {
863 save_bufp
->flags
&= ~BUF_PIN
;
866 save_bufp
->flags
&= ~BUF_PIN
;
872 save_bufp
->flags
&= ~BUF_PIN
;
879 save_bufp
->flags
&= ~BUF_PIN
;
882 bp
= (uint16
*)rbufp
->page
;
883 if (bp
[ndx
+ 1] < REAL_KEY
) {
884 if (__big_return(hashp
, rbufp
, ndx
, val
, 0))
887 val
->data
= (uint8
*)rbufp
->page
+ (int)bp
[ndx
+ 1];
888 val
->size
= bp
[ndx
] - bp
[ndx
+ 1];
892 if ((__delpair(hashp
, rbufp
, ndx
)) ||
893 (__addel(hashp
, rbufp
, key
, val
))) {
894 save_bufp
->flags
&= ~BUF_PIN
;
899 if (__delpair(hashp
, rbufp
, ndx
))
905 save_bufp
->flags
&= ~BUF_PIN
;
915 register uint32 bucket
;
916 register BUFHEAD
*bufp
;
920 hashp
= (HTAB
*)dbp
->internal
;
924 if (flag
&& flag
!= R_FIRST
&& flag
!= R_NEXT
) {
925 hashp
->dbmerrno
= errno
= EINVAL
;
928 #ifdef HASH_STATISTICS
931 if ((hashp
->cbucket
< 0) || (flag
== R_FIRST
)) {
937 for (bp
= NULL
; !bp
|| !bp
[0]; ) {
938 if (!(bufp
= hashp
->cpage
)) {
939 for (bucket
= hashp
->cbucket
;
940 bucket
<= (uint32
)hashp
->MAX_BUCKET
;
941 bucket
++, hashp
->cndx
= 1) {
942 bufp
= __get_buf(hashp
, bucket
, NULL
, 0);
946 bp
= (uint16
*)bufp
->page
;
950 hashp
->cbucket
= bucket
;
951 if (hashp
->cbucket
> hashp
->MAX_BUCKET
) {
956 bp
= (uint16
*)hashp
->cpage
->page
;
962 while (bp
[hashp
->cndx
+ 1] == OVFLPAGE
) {
963 bufp
= hashp
->cpage
=
964 __get_buf(hashp
, bp
[hashp
->cndx
], bufp
, 0);
967 bp
= (uint16
*)(bufp
->page
);
976 if (bp
[ndx
+ 1] < REAL_KEY
) {
977 if (__big_keydata(hashp
, bufp
, key
, data
, 1))
980 key
->data
= (uint8
*)hashp
->cpage
->page
+ bp
[ndx
];
981 key
->size
= (ndx
> 1 ? bp
[ndx
- 1] : hashp
->BSIZE
) - bp
[ndx
];
982 data
->data
= (uint8
*)hashp
->cpage
->page
+ bp
[ndx
+ 1];
983 data
->size
= bp
[ndx
] - bp
[ndx
+ 1];
995 /********************************* UTILITIES ************************/
1003 __expand_table(HTAB
*hashp
)
1005 uint32 old_bucket
, new_bucket
;
1006 int new_segnum
, spare_ndx
;
1009 #ifdef HASH_STATISTICS
1012 new_bucket
= ++hashp
->MAX_BUCKET
;
1013 old_bucket
= (hashp
->MAX_BUCKET
& hashp
->LOW_MASK
);
1015 new_segnum
= new_bucket
>> hashp
->SSHIFT
;
1017 /* Check if we need a new segment */
1018 if (new_segnum
>= hashp
->nsegs
) {
1019 /* Check if we need to expand directory */
1020 if (new_segnum
>= hashp
->DSIZE
) {
1021 /* Reallocate directory */
1022 dirsize
= hashp
->DSIZE
* sizeof(SEGMENT
*);
1023 if (!hash_realloc(&hashp
->dir
, dirsize
, dirsize
<< 1))
1025 hashp
->DSIZE
= dirsize
<< 1;
1027 if ((hashp
->dir
[new_segnum
] =
1028 (SEGMENT
)calloc((size_t)hashp
->SGSIZE
, sizeof(SEGMENT
))) == NULL
)
1034 * If the split point is increasing (MAX_BUCKET's log base 2
1035 * * increases), we need to copy the current contents of the spare
1036 * split bucket to the next bucket.
1038 spare_ndx
= __log2((uint32
)(hashp
->MAX_BUCKET
+ 1));
1039 if (spare_ndx
> hashp
->OVFL_POINT
) {
1040 hashp
->SPARES
[spare_ndx
] = hashp
->SPARES
[hashp
->OVFL_POINT
];
1041 hashp
->OVFL_POINT
= spare_ndx
;
1044 if (new_bucket
> (uint32
)hashp
->HIGH_MASK
) {
1045 /* Starting a new doubling */
1046 hashp
->LOW_MASK
= hashp
->HIGH_MASK
;
1047 hashp
->HIGH_MASK
= new_bucket
| hashp
->LOW_MASK
;
1049 /* Relocate records to the new bucket */
1050 return (__split_page(hashp
, old_bucket
, new_bucket
));
1054 * If realloc guarantees that the pointer is not destroyed if the realloc
1055 * fails, then this routine can go away.
1060 size_t oldsize
, size_t newsize
)
1064 if ((p
= malloc(newsize
))) {
1065 memmove(p
, *p_ptr
, oldsize
);
1066 memset((char *)p
+ oldsize
, 0, newsize
- oldsize
);
1068 *p_ptr
= (SEGMENT
*)p
;
1074 __call_hash(HTAB
*hashp
, char *k
, size_t len
)
1078 n
= hashp
->hash(k
, len
);
1079 bucket
= n
& hashp
->HIGH_MASK
;
1080 if (bucket
> (uint32
)hashp
->MAX_BUCKET
)
1081 bucket
= bucket
& hashp
->LOW_MASK
;
1086 * Allocate segment table. On error, set errno.
1088 * Returns 0 on success
1096 register SEGMENT store
;
1099 (SEGMENT
*)calloc((size_t)hashp
->DSIZE
, sizeof(SEGMENT
*))) == NULL
) {
1103 /* Allocate segments */
1105 (SEGMENT
)calloc((size_t)nsegs
<< hashp
->SSHIFT
, sizeof(SEGMENT
))) == NULL
) {
1109 for (i
= 0; i
< nsegs
; i
++, hashp
->nsegs
++)
1110 hashp
->dir
[i
] = &store
[i
<< hashp
->SSHIFT
];
1114 #if BYTE_ORDER == LITTLE_ENDIAN
1116 * Hashp->hdr needs to be byteswapped.
1120 HASHHDR
*srcp
, HASHHDR
*destp
)
1124 P_32_COPY(srcp
->magic
, destp
->magic
);
1125 P_32_COPY(srcp
->version
, destp
->version
);
1126 P_32_COPY(srcp
->lorder
, destp
->lorder
);
1127 P_32_COPY(srcp
->bsize
, destp
->bsize
);
1128 P_32_COPY(srcp
->bshift
, destp
->bshift
);
1129 P_32_COPY(srcp
->dsize
, destp
->dsize
);
1130 P_32_COPY(srcp
->ssize
, destp
->ssize
);
1131 P_32_COPY(srcp
->sshift
, destp
->sshift
);
1132 P_32_COPY(srcp
->ovfl_point
, destp
->ovfl_point
);
1133 P_32_COPY(srcp
->last_freed
, destp
->last_freed
);
1134 P_32_COPY(srcp
->max_bucket
, destp
->max_bucket
);
1135 P_32_COPY(srcp
->high_mask
, destp
->high_mask
);
1136 P_32_COPY(srcp
->low_mask
, destp
->low_mask
);
1137 P_32_COPY(srcp
->ffactor
, destp
->ffactor
);
1138 P_32_COPY(srcp
->nkeys
, destp
->nkeys
);
1139 P_32_COPY(srcp
->hdrpages
, destp
->hdrpages
);
1140 P_32_COPY(srcp
->h_charkey
, destp
->h_charkey
);
1141 for (i
= 0; i
< NCACHED
; i
++) {
1142 P_32_COPY(srcp
->spares
[i
], destp
->spares
[i
]);
1143 P_16_COPY(srcp
->bitmaps
[i
], destp
->bitmaps
[i
]);
1148 swap_header(HTAB
*hashp
)
1155 M_32_SWAP(hdrp
->magic
);
1156 M_32_SWAP(hdrp
->version
);
1157 M_32_SWAP(hdrp
->lorder
);
1158 M_32_SWAP(hdrp
->bsize
);
1159 M_32_SWAP(hdrp
->bshift
);
1160 M_32_SWAP(hdrp
->dsize
);
1161 M_32_SWAP(hdrp
->ssize
);
1162 M_32_SWAP(hdrp
->sshift
);
1163 M_32_SWAP(hdrp
->ovfl_point
);
1164 M_32_SWAP(hdrp
->last_freed
);
1165 M_32_SWAP(hdrp
->max_bucket
);
1166 M_32_SWAP(hdrp
->high_mask
);
1167 M_32_SWAP(hdrp
->low_mask
);
1168 M_32_SWAP(hdrp
->ffactor
);
1169 M_32_SWAP(hdrp
->nkeys
);
1170 M_32_SWAP(hdrp
->hdrpages
);
1171 M_32_SWAP(hdrp
->h_charkey
);
1172 for (i
= 0; i
< NCACHED
; i
++) {
1173 M_32_SWAP(hdrp
->spares
[i
]);
1174 M_16_SWAP(hdrp
->bitmaps
[i
]);