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
36 #define MY_LSEEK lseek
38 #define MY_LSEEK new_lseek
39 extern long new_lseek(int fd
, long pos
, int start
);
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid
[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
44 #endif /* LIBC_SCCS and not lint */
52 * Page manipulation for hashing package.
64 #include <sys/types.h>
67 #if defined(macintosh)
73 #if defined(_WIN32) || defined(_WINDOWS)
81 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
90 /* #include "extern.h" */
92 extern int mkstempflags(char *path
, int extraFlags
);
94 static uint32
*fetch_bitmap
__P((HTAB
*, uint32
));
95 static uint32 first_free
__P((uint32
));
96 static int open_temp
__P((HTAB
*));
97 static uint16 overflow_page
__P((HTAB
*));
98 static void squeeze_key
__P((uint16
*, const DBT
*, const DBT
*));
100 __P((HTAB
*, uint32
, BUFHEAD
*, BUFHEAD
*, int, int));
102 #define PAGE_INIT(P) { \
103 ((uint16 *)(P))[0] = 0; \
104 ((uint16 *)(P))[1] = hashp->BSIZE - 3 * sizeof(uint16); \
105 ((uint16 *)(P))[2] = hashp->BSIZE; \
108 /* implement a new lseek using lseek that
109 * writes zero's when extending a file
112 long new_lseek(int fd
, long offset
, int origin
)
118 if(origin
== SEEK_CUR
)
121 return(lseek(fd
, offset
, SEEK_CUR
));
123 cur_pos
= lseek(fd
, 0, SEEK_CUR
);
129 end_pos
= lseek(fd
, 0, SEEK_END
);
133 if(origin
== SEEK_SET
)
135 else if(origin
== SEEK_CUR
)
136 seek_pos
= cur_pos
+ offset
;
137 else if(origin
== SEEK_END
)
138 seek_pos
= end_pos
+ offset
;
145 /* the seek position desired is before the
146 * end of the file. We don't need
147 * to do anything special except the seek.
149 if(seek_pos
<= end_pos
)
150 return(lseek(fd
, seek_pos
, SEEK_SET
));
152 /* the seek position is beyond the end of the
153 * file. Write zero's to the end.
155 * we are already at the end of the file so
156 * we just need to "write()" zeros for the
157 * difference between seek_pos-end_pos and
158 * then seek to the position to finish
163 long len
= seek_pos
-end_pos
;
164 memset(&buffer
, 0, 1024);
167 write(fd
, (char*)&buffer
, (size_t)(1024 > len
? len
: 1024));
170 return(lseek(fd
, seek_pos
, SEEK_SET
));
176 * This is called AFTER we have verified that there is room on the page for
177 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
181 putpair(char *p
, const DBT
*key
, DBT
* val
)
183 register uint16
*bp
, n
, off
;
187 /* Enter the key first. */
190 off
= OFFSET(bp
) - key
->size
;
191 memmove(p
+ off
, key
->data
, key
->size
);
196 memmove(p
+ off
, val
->data
, val
->size
);
199 /* Adjust page info. */
201 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(uint16
));
211 __delpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
)
213 register uint16
*bp
, newoff
;
217 bp
= (uint16
*)bufp
->page
;
220 if (bp
[ndx
+ 1] < REAL_KEY
)
221 return (__big_delete(hashp
, bufp
));
223 newoff
= bp
[ndx
- 1];
225 newoff
= hashp
->BSIZE
;
226 pairlen
= newoff
- bp
[ndx
+ 1];
228 if (ndx
!= (n
- 1)) {
229 /* Hard Case -- need to shuffle keys */
231 register char *src
= bufp
->page
+ (int)OFFSET(bp
);
232 uint32 dst_offset
= (uint32
)OFFSET(bp
) + (uint32
)pairlen
;
233 register char *dst
= bufp
->page
+ dst_offset
;
234 uint32 length
= bp
[ndx
+ 1] - OFFSET(bp
);
237 * +-----------+XXX+---------+XXX+---------+---------> +infinity
239 * 0 src_offset dst_offset BSIZE
241 * Dst_offset is > src_offset, so if src_offset were bad, dst_offset
242 * would be too, therefore we check only dst_offset.
244 * If dst_offset is >= BSIZE, either OFFSET(bp), or pairlen, or both
247 * Once we know dst_offset is < BSIZE, we can subtract it from BSIZE
248 * to get an upper bound on length.
250 if(dst_offset
> (uint32
)hashp
->BSIZE
)
251 return(DATABASE_CORRUPTED_ERROR
);
253 if(length
> (uint32
)(hashp
->BSIZE
- dst_offset
))
254 return(DATABASE_CORRUPTED_ERROR
);
256 memmove(dst
, src
, length
);
258 /* Now adjust the pointers */
259 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
260 if (bp
[i
+ 1] == OVFLPAGE
) {
262 bp
[i
- 1] = bp
[i
+ 1];
264 bp
[i
- 2] = bp
[i
] + pairlen
;
265 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
269 /* Finally adjust the page data */
270 bp
[n
] = OFFSET(bp
) + pairlen
;
271 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(uint16
);
275 bufp
->flags
|= BUF_MOD
;
284 __split_page(HTAB
*hashp
, uint32 obucket
, uint32 nbucket
)
286 register BUFHEAD
*new_bufp
, *old_bufp
;
287 register uint16
*ino
;
288 register uint16
*tmp_uint16_array
;
293 uint16 copyto
, diff
, moved
;
297 copyto
= (uint16
)hashp
->BSIZE
;
298 off
= (uint16
)hashp
->BSIZE
;
299 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
300 if (old_bufp
== NULL
)
302 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
303 if (new_bufp
== NULL
)
306 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
307 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
309 ino
= (uint16
*)(op
= old_bufp
->page
);
314 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
315 if (ino
[n
+ 1] < REAL_KEY
) {
316 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
317 (int)copyto
, (int)moved
);
318 old_bufp
->flags
&= ~BUF_PIN
;
319 new_bufp
->flags
&= ~BUF_PIN
;
323 key
.data
= (uint8
*)op
+ ino
[n
];
325 /* check here for ino[n] being greater than
326 * off. If it is then the database has
330 return(DATABASE_CORRUPTED_ERROR
);
332 key
.size
= off
- ino
[n
];
335 /* make sure the size is positive */
336 assert(((int)key
.size
) > -1);
339 if (__call_hash(hashp
, (char *)key
.data
, key
.size
) == obucket
) {
340 /* Don't switch page */
343 copyto
= ino
[n
+ 1] + diff
;
344 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
346 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
347 ino
[ndx
+ 1] = copyto
;
353 val
.data
= (uint8
*)op
+ ino
[n
+ 1];
354 val
.size
= ino
[n
] - ino
[n
+ 1];
356 /* if the pair doesn't fit something is horribly
359 tmp_uint16_array
= (uint16
*)np
;
360 if(!PAIRFITS(tmp_uint16_array
, &key
, &val
))
361 return(DATABASE_CORRUPTED_ERROR
);
363 putpair(np
, &key
, &val
);
370 /* Now clean up the page */
372 FREESPACE(ino
) = copyto
- sizeof(uint16
) * (ino
[0] + 3);
373 OFFSET(ino
) = copyto
;
376 (void)fprintf(stderr
, "split %d/%d\n",
377 ((uint16
*)np
)[0] / 2,
378 ((uint16
*)op
)[0] / 2);
380 /* unpin both pages */
381 old_bufp
->flags
&= ~BUF_PIN
;
382 new_bufp
->flags
&= ~BUF_PIN
;
387 * Called when we encounter an overflow or big key/data page during split
388 * handling. This is special cased since we have to begin checking whether
389 * the key/data pairs fit on their respective pages and because we may need
390 * overflow pages for both the old and new pages.
392 * The first page might be a page with regular key/data pairs in which case
393 * we have a regular overflow condition and just need to go on to the next
394 * page or it might be a big key/data pair in which case we need to fix the
402 /* the maximum number of loops we will allow UGLY split to chew
403 * on before we assume the database is corrupted and throw it
406 #define MAX_UGLY_SPLIT_LOOPS 10000
409 ugly_split(HTAB
*hashp
, uint32 obucket
, BUFHEAD
*old_bufp
,
410 BUFHEAD
*new_bufp
,/* Same as __split_page. */ int copyto
, int moved
)
411 /* int copyto; First byte on page which contains key/data values. */
412 /* int moved; Number of pairs moved to new page. */
414 register BUFHEAD
*bufp
; /* Buffer header for ino */
415 register uint16
*ino
; /* Page keys come off of */
416 register uint16
*np
; /* New page */
417 register uint16
*op
; /* Page keys go on to if they aren't moving */
418 uint32 loop_detection
=0;
420 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
423 uint16 n
, off
, ov_addr
, scopyto
;
424 char *cino
; /* Character value of ino */
428 ino
= (uint16
*)old_bufp
->page
;
429 np
= (uint16
*)new_bufp
->page
;
430 op
= (uint16
*)old_bufp
->page
;
432 scopyto
= (uint16
)copyto
; /* ANSI */
438 /* this function goes nuts sometimes and never returns.
439 * I havent found the problem yet but I need a solution
440 * so if we loop too often we assume a database curruption error
445 if(loop_detection
> MAX_UGLY_SPLIT_LOOPS
)
446 return DATABASE_CORRUPTED_ERROR
;
448 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
449 if ((status
= __big_split(hashp
, old_bufp
,
450 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
)))
455 op
= (uint16
*)old_bufp
->page
;
459 np
= (uint16
*)new_bufp
->page
;
463 cino
= (char *)bufp
->page
;
464 ino
= (uint16
*)cino
;
465 last_bfp
= ret
.nextp
;
466 } else if (ino
[n
+ 1] == OVFLPAGE
) {
469 * Fix up the old page -- the extra 2 are the fields
470 * which contained the overflow information.
472 ino
[0] -= (moved
+ 2);
474 scopyto
- sizeof(uint16
) * (ino
[0] + 3);
475 OFFSET(ino
) = scopyto
;
477 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
481 ino
= (uint16
*)bufp
->page
;
483 scopyto
= hashp
->BSIZE
;
487 __free_ovflpage(hashp
, last_bfp
);
490 /* Move regular sized pairs of there are any */
492 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
494 key
.data
= (uint8
*)cino
+ ino
[n
];
495 key
.size
= off
- ino
[n
];
496 val
.data
= (uint8
*)cino
+ ino
[n
+ 1];
497 val
.size
= ino
[n
] - ino
[n
+ 1];
500 if (__call_hash(hashp
, (char*)key
.data
, key
.size
) == obucket
) {
501 /* Keep on old page */
502 if (PAIRFITS(op
, (&key
), (&val
)))
503 putpair((char *)op
, &key
, &val
);
506 __add_ovflpage(hashp
, old_bufp
);
509 op
= (uint16
*)old_bufp
->page
;
510 putpair((char *)op
, &key
, &val
);
512 old_bufp
->flags
|= BUF_MOD
;
514 /* Move to new page */
515 if (PAIRFITS(np
, (&key
), (&val
)))
516 putpair((char *)np
, &key
, &val
);
519 __add_ovflpage(hashp
, new_bufp
);
522 np
= (uint16
*)new_bufp
->page
;
523 putpair((char *)np
, &key
, &val
);
525 new_bufp
->flags
|= BUF_MOD
;
530 __free_ovflpage(hashp
, last_bfp
);
535 * Add the given pair to the page
542 __addel(HTAB
*hashp
, BUFHEAD
*bufp
, const DBT
*key
, const DBT
* val
)
544 register uint16
*bp
, *sop
;
547 bp
= (uint16
*)bufp
->page
;
549 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
551 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
552 /* This is the last page of a big key/data pair
553 and we need to add another page */
555 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
556 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
564 bp
= (uint16
*)bufp
->page
;
566 /* Try to squeeze key on this page */
567 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
569 squeeze_key(bp
, key
, val
);
571 /* LJM: I added this because I think it was
572 * left out on accident.
573 * if this isn't incremented nkeys will not
574 * be the actual number of keys in the db.
580 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
588 bp
= (uint16
*)bufp
->page
;
591 if (PAIRFITS(bp
, key
, val
))
592 putpair(bufp
->page
, key
, (DBT
*)val
);
595 bufp
= __add_ovflpage(hashp
, bufp
);
603 sop
= (uint16
*)bufp
->page
;
605 if (PAIRFITS(sop
, key
, val
))
606 putpair((char *)sop
, key
, (DBT
*)val
);
608 if (__big_insert(hashp
, bufp
, key
, val
))
616 bufp
->flags
|= BUF_MOD
;
618 * If the average number of keys per bucket exceeds the fill factor,
623 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
624 return (__expand_table(hashp
));
635 __add_ovflpage(HTAB
*hashp
, BUFHEAD
*bufp
)
638 uint16 ndx
, ovfl_num
;
642 sp
= (uint16
*)bufp
->page
;
644 /* Check if we are dynamically determining the fill factor */
645 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
646 hashp
->FFACTOR
= sp
[0] >> 1;
647 if (hashp
->FFACTOR
< MIN_FFACTOR
)
648 hashp
->FFACTOR
= MIN_FFACTOR
;
650 bufp
->flags
|= BUF_MOD
;
651 ovfl_num
= overflow_page(hashp
);
654 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
656 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
658 bufp
->ovfl
->flags
|= BUF_MOD
;
660 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
661 tmp1
, tmp2
, bufp
->ovfl
->addr
);
665 * Since a pair is allocated on a page only if there's room to add
666 * an overflow page, we know that the OVFL information will fit on
669 sp
[ndx
+ 4] = OFFSET(sp
);
670 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
671 sp
[ndx
+ 1] = ovfl_num
;
672 sp
[ndx
+ 2] = OVFLPAGE
;
674 #ifdef HASH_STATISTICS
682 * 0 indicates SUCCESS
683 * -1 indicates FAILURE
686 __get_page(HTAB
*hashp
,
693 register int fd
, page
;
701 if ((fd
== -1) || !is_disk
) {
706 page
= BUCKET_TO_PAGE(bucket
);
708 page
= OADDR_TO_PAGE(bucket
);
709 if ((MY_LSEEK(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
710 ((rsize
= read(fd
, p
, size
)) == -1))
715 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
717 if ((unsigned)rsize
!= size
) {
722 if (!is_bitmap
&& !bp
[0]) {
727 if(BYTE_ORDER
== LITTLE_ENDIAN
)
729 int is_little_endian
;
730 is_little_endian
= BYTE_ORDER
;
732 else if(BYTE_ORDER
== BIG_ENDIAN
)
735 is_big_endian
= BYTE_ORDER
;
743 if (hashp
->LORDER
!= BYTE_ORDER
) {
747 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
748 for (i
= 0; i
< max
; i
++)
749 M_32_SWAP(((int *)p
)[i
]);
754 /* bound the size of max by
755 * the maximum number of entries
758 if((unsigned)max
> (size
/ sizeof(uint16
)))
759 return(DATABASE_CORRUPTED_ERROR
);
761 /* do the byte order swap
763 for (i
= 1; i
<= max
; i
++)
768 /* check the validity of the page here
769 * (after doing byte order swaping if necessary)
771 if(!is_bitmap
&& bp
[0] != 0)
773 uint16 num_keys
= bp
[0];
777 /* bp[0] is supposed to be the number of
778 * entries currently in the page. If
779 * bp[0] is too large (larger than the whole
780 * page) then the page is corrupted
782 if(bp
[0] > (size
/ sizeof(uint16
)))
783 return(DATABASE_CORRUPTED_ERROR
);
785 /* bound free space */
786 if(FREESPACE(bp
) > size
)
787 return(DATABASE_CORRUPTED_ERROR
);
789 /* check each key and data offset to make
790 * sure they are all within bounds they
791 * should all be less than the previous
795 for(i
=1 ; i
<= num_keys
; i
+=2)
797 /* ignore overflow pages etc. */
798 if(bp
[i
+1] >= REAL_KEY
)
801 if(bp
[i
] > offset
|| bp
[i
+1] > bp
[i
])
802 return(DATABASE_CORRUPTED_ERROR
);
808 /* there are no other valid keys after
809 * seeing a non REAL_KEY
820 * Write page p to disk
827 __put_page(HTAB
*hashp
, char *p
, uint32 bucket
, int is_bucket
, int is_bitmap
)
829 register int fd
, page
;
835 if ((hashp
->fp
== -1) && open_temp(hashp
))
839 if (hashp
->LORDER
!= BYTE_ORDER
) {
844 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
845 for (i
= 0; i
< max
; i
++)
846 M_32_SWAP(((int *)p
)[i
]);
848 max
= ((uint16
*)p
)[0] + 2;
850 /* bound the size of max by
851 * the maximum number of entries
854 if((unsigned)max
> (size
/ sizeof(uint16
)))
855 return(DATABASE_CORRUPTED_ERROR
);
857 for (i
= 0; i
<= max
; i
++)
858 M_16_SWAP(((uint16
*)p
)[i
]);
864 page
= BUCKET_TO_PAGE(bucket
);
866 page
= OADDR_TO_PAGE(bucket
);
867 offset
= (off_t
)page
<< hashp
->BSHIFT
;
868 if ((MY_LSEEK(fd
, offset
, SEEK_SET
) == -1) ||
869 ((wsize
= write(fd
, p
, size
)) == -1))
872 if ((unsigned)wsize
!= size
) {
876 #if defined(_WIN32) || defined(_WINDOWS)
877 if (offset
+ size
> hashp
->file_size
) {
878 hashp
->updateEOF
= 1;
881 /* put the page back the way it was so that it isn't byteswapped
882 * if it remains in memory - LJM
884 if (hashp
->LORDER
!= BYTE_ORDER
) {
889 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
890 for (i
= 0; i
< max
; i
++)
891 M_32_SWAP(((int *)p
)[i
]);
893 uint16
*bp
= (uint16
*)p
;
898 /* no need to bound the size if max again
899 * since it was done already above
902 /* do the byte order re-swap
904 for (i
= 1; i
<= max
; i
++)
912 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
914 * Initialize a new bitmap page. Bitmap pages are left in memory
915 * once they are read in.
918 __ibitmap(HTAB
*hashp
, int pnum
, int nbits
, int ndx
)
921 size_t clearbytes
, clearints
;
923 if ((ip
= (uint32
*)malloc((size_t)hashp
->BSIZE
)) == NULL
)
926 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
927 clearbytes
= clearints
<< INT_TO_BYTE
;
928 (void)memset((char *)ip
, 0, clearbytes
);
929 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
930 hashp
->BSIZE
- clearbytes
);
931 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
933 hashp
->BITMAPS
[ndx
] = (uint16
)pnum
;
934 hashp
->mapp
[ndx
] = ip
;
939 first_free(uint32 map
)
941 register uint32 i
, mask
;
944 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
953 overflow_page(HTAB
*hashp
)
955 register uint32
*freep
=NULL
;
956 register int max_free
, offset
, splitnum
;
959 int bit
, first_page
, free_bit
, free_page
, in_use_bits
, j
;
963 splitnum
= hashp
->OVFL_POINT
;
964 max_free
= hashp
->SPARES
[splitnum
];
966 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
967 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
969 /* Look through all the free maps to find the first free block */
970 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
971 for ( i
= first_page
; i
<= (unsigned)free_page
; i
++ ) {
972 if (!(freep
= (uint32
*)hashp
->mapp
[i
]) &&
973 !(freep
= fetch_bitmap(hashp
, i
)))
975 if (i
== (unsigned)free_page
)
976 in_use_bits
= free_bit
;
978 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
980 if (i
== (unsigned)first_page
) {
981 bit
= hashp
->LAST_FREED
&
982 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
983 j
= bit
/ BITS_PER_MAP
;
984 bit
= bit
& ~(BITS_PER_MAP
- 1);
989 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
990 if (freep
[j
] != ALL_SET
)
994 /* No Free Page Found */
995 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
996 hashp
->SPARES
[splitnum
]++;
997 offset
= hashp
->SPARES
[splitnum
] -
998 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
1000 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
1001 if (offset
> SPLITMASK
) {
1002 if (++splitnum
>= NCACHED
) {
1004 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
1008 hashp
->OVFL_POINT
= splitnum
;
1009 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
1010 hashp
->SPARES
[splitnum
-1]--;
1014 /* Check if we need to allocate a new bitmap page */
1015 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
1017 if (free_page
>= NCACHED
) {
1019 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
1024 * This is tricky. The 1 indicates that you want the new page
1025 * allocated with 1 clear bit. Actually, you are going to
1026 * allocate 2 pages from this map. The first is going to be
1027 * the map page, the second is the overflow page we were
1028 * looking for. The init_bitmap routine automatically, sets
1029 * the first bit of itself to indicate that the bitmap itself
1030 * is in use. We would explicitly set the second bit, but
1031 * don't have to if we tell init_bitmap not to leave it clear
1032 * in the first place.
1034 if (__ibitmap(hashp
,
1035 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
1037 hashp
->SPARES
[splitnum
]++;
1042 if (offset
> SPLITMASK
) {
1043 if (++splitnum
>= NCACHED
) {
1045 (void)write(STDERR_FILENO
, OVMSG
,
1050 hashp
->OVFL_POINT
= splitnum
;
1051 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
1052 hashp
->SPARES
[splitnum
-1]--;
1057 * Free_bit addresses the last used bit. Bump it to address
1058 * the first available bit.
1061 SETBIT(freep
, free_bit
);
1064 /* Calculate address of the new overflow page */
1065 addr
= OADDR_OF(splitnum
, offset
);
1067 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1068 addr
, free_bit
, free_page
);
1073 bit
= bit
+ first_free(freep
[j
]);
1080 * Bits are addressed starting with 0, but overflow pages are addressed
1081 * beginning at 1. Bit is a bit addressnumber, so we need to increment
1082 * it to convert it to a page number.
1084 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
1085 if (bit
>= hashp
->LAST_FREED
)
1086 hashp
->LAST_FREED
= bit
- 1;
1088 /* Calculate the split number for this page */
1089 for (i
= 0; (i
< (unsigned)splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++) {}
1090 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
1091 if (offset
>= SPLITMASK
)
1092 return (0); /* Out of overflow pages */
1093 addr
= OADDR_OF(i
, offset
);
1095 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1099 /* Allocate and return the overflow page */
1104 * Mark this overflow page as free.
1107 __free_ovflpage(HTAB
*hashp
, BUFHEAD
*obufp
)
1111 uint32 bit_address
, free_page
, free_bit
;
1114 if(!obufp
|| !obufp
->addr
)
1119 (void)fprintf(stderr
, "Freeing %d\n", addr
);
1121 ndx
= (((uint16
)addr
) >> SPLITSHIFT
);
1123 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
1124 if (bit_address
< (uint32
)hashp
->LAST_FREED
)
1125 hashp
->LAST_FREED
= bit_address
;
1126 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
1127 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
1129 if (!(freep
= hashp
->mapp
[free_page
]))
1130 freep
= fetch_bitmap(hashp
, free_page
);
1134 * This had better never happen. It means we tried to read a bitmap
1135 * that has already had overflow pages allocated off it, and we
1136 * failed to read it from the file.
1144 CLRBIT(freep
, free_bit
);
1146 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
1147 obufp
->addr
, free_bit
, free_page
);
1149 __reclaim_buf(hashp
, obufp
);
1158 open_temp(HTAB
*hashp
)
1161 hashp
->fp
= mkstemp(NULL
);
1163 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1166 #if !defined(macintosh)
1171 static const char namestr
[] = "/_hashXXXXXX";
1172 char filename
[1024];
1174 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1175 /* Block signals; make sure file goes away at process exit. */
1176 (void)sigfillset(&set
);
1177 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
1181 #if defined(macintosh)
1182 strcat(filename
, namestr
+ 1);
1184 tmpdir
= getenv("TMP");
1186 tmpdir
= getenv("TMPDIR");
1188 tmpdir
= getenv("TEMP");
1191 len
= strlen(tmpdir
);
1192 if (len
&& len
< (sizeof filename
- sizeof namestr
)) {
1193 strcpy(filename
, tmpdir
);
1195 len
= strlen(filename
);
1196 last
= tmpdir
[len
- 1];
1197 strcat(filename
, (last
== '/' || last
== '\\') ? namestr
+ 1 : namestr
);
1200 #if defined(_WIN32) || defined(_WINDOWS)
1201 if ((hashp
->fp
= mkstempflags(filename
, _O_BINARY
|_O_TEMPORARY
)) != -1) {
1202 if (hashp
->filename
) {
1203 free(hashp
->filename
);
1205 hashp
->filename
= strdup(filename
);
1209 if ((hashp
->fp
= mkstemp(filename
)) != -1) {
1210 (void)unlink(filename
);
1211 #if !defined(macintosh)
1212 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
1217 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1218 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
1221 return (hashp
->fp
!= -1 ? 0 : -1);
1225 * We have to know that the key will fit, but the last entry on the page is
1226 * an overflow pair, so we need to shift things.
1229 squeeze_key(uint16
*sp
, const DBT
* key
, const DBT
* val
)
1232 uint16 free_space
, n
, off
, pageno
;
1236 free_space
= FREESPACE(sp
);
1242 memmove(p
+ off
, key
->data
, key
->size
);
1245 memmove(p
+ off
, val
->data
, val
->size
);
1248 sp
[n
+ 2] = OVFLPAGE
;
1249 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
1254 fetch_bitmap(HTAB
*hashp
, uint32 ndx
)
1256 if (ndx
>= (unsigned)hashp
->nmaps
)
1258 if ((hashp
->mapp
[ndx
] = (uint32
*)malloc((size_t)hashp
->BSIZE
)) == NULL
)
1260 if (__get_page(hashp
,
1261 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
1262 free(hashp
->mapp
[ndx
]);
1263 hashp
->mapp
[ndx
] = NULL
; /* NEW: 9-11-95 */
1266 return (hashp
->mapp
[ndx
]);
1271 print_chain(int addr
)
1276 (void)fprintf(stderr
, "%d ", addr
);
1277 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
1278 bp
= (short *)bufp
->page
;
1279 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
1280 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
1281 oaddr
= bp
[bp
[0] - 1];
1282 (void)fprintf(stderr
, "%d ", (int)oaddr
);
1283 bufp
= __get_buf(hashp
, (int)oaddr
, bufp
, 0);
1284 bp
= (short *)bufp
->page
;
1286 (void)fprintf(stderr
, "\n");