1 /* $NetBSD: hash_page.c,v 1.22 2008/09/10 17:52:35 joerg Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. 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 HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: hash_page.c,v 1.22 2008/09/10 17:52:35 joerg Exp $");
46 * Page manipulation for hashing package.
58 #include "namespace.h"
60 #include <sys/types.h>
77 static uint32_t *fetch_bitmap(HTAB
*, int);
78 static uint32_t first_free(uint32_t);
79 static int open_temp(HTAB
*);
80 static uint16_t overflow_page(HTAB
*);
81 static void putpair(char *, const DBT
*, const DBT
*);
82 static void squeeze_key(uint16_t *, const DBT
*, const DBT
*);
83 static int ugly_split(HTAB
*, uint32_t, BUFHEAD
*, BUFHEAD
*, int, int);
85 #define PAGE_INIT(P) { \
86 ((uint16_t *)(void *)(P))[0] = 0; \
87 temp = 3 * sizeof(uint16_t); \
88 _DIAGASSERT(hashp->BSIZE >= temp); \
89 ((uint16_t *)(void *)(P))[1] = (uint16_t)(hashp->BSIZE - temp); \
90 ((uint16_t *)(void *)(P))[2] = hashp->BSIZE; \
94 * This is called AFTER we have verified that there is room on the page for
95 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
99 putpair(char *p
, const DBT
*key
, const DBT
*val
)
101 uint16_t *bp
, n
, off
;
104 bp
= (uint16_t *)(void *)p
;
106 /* Enter the key first. */
110 _DIAGASSERT(temp
>= key
->size
);
111 off
= (uint16_t)(temp
- key
->size
);
112 memmove(p
+ off
, key
->data
, key
->size
);
116 _DIAGASSERT(off
>= val
->size
);
117 off
-= (uint16_t)val
->size
;
118 memmove(p
+ off
, val
->data
, val
->size
);
121 /* Adjust page info. */
123 temp
= (n
+ 3) * sizeof(uint16_t);
124 _DIAGASSERT(off
>= temp
);
125 bp
[n
+ 1] = (uint16_t)(off
- temp
);
135 __delpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
)
137 uint16_t *bp
, newoff
;
142 bp
= (uint16_t *)(void *)bufp
->page
;
145 if (bp
[ndx
+ 1] < REAL_KEY
)
146 return (__big_delete(hashp
, bufp
));
148 newoff
= bp
[ndx
- 1];
150 newoff
= hashp
->BSIZE
;
151 pairlen
= newoff
- bp
[ndx
+ 1];
153 if (ndx
!= (n
- 1)) {
154 /* Hard Case -- need to shuffle keys */
156 char *src
= bufp
->page
+ (int)OFFSET(bp
);
157 char *dst
= src
+ (int)pairlen
;
158 memmove(dst
, src
, (size_t)(bp
[ndx
+ 1] - OFFSET(bp
)));
160 /* Now adjust the pointers */
161 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
162 if (bp
[i
+ 1] == OVFLPAGE
) {
164 bp
[i
- 1] = bp
[i
+ 1];
166 bp
[i
- 2] = bp
[i
] + pairlen
;
167 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
171 /* Finally adjust the page data */
172 bp
[n
] = OFFSET(bp
) + pairlen
;
173 temp
= bp
[n
+ 1] + pairlen
+ 2 * sizeof(uint16_t);
174 _DIAGASSERT(temp
<= 0xffff);
175 bp
[n
- 1] = (uint16_t)temp
;
179 bufp
->flags
|= BUF_MOD
;
188 __split_page(HTAB
*hashp
, uint32_t obucket
, uint32_t nbucket
)
190 BUFHEAD
*new_bufp
, *old_bufp
;
195 uint16_t copyto
, diff
, off
, moved
;
199 copyto
= (uint16_t)hashp
->BSIZE
;
200 off
= (uint16_t)hashp
->BSIZE
;
201 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
202 if (old_bufp
== NULL
)
204 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
205 if (new_bufp
== NULL
)
208 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
209 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
211 ino
= (uint16_t *)(void *)(op
= old_bufp
->page
);
216 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
217 if (ino
[n
+ 1] < REAL_KEY
) {
218 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
219 (int)copyto
, (int)moved
);
220 old_bufp
->flags
&= ~BUF_PIN
;
221 new_bufp
->flags
&= ~BUF_PIN
;
225 key
.data
= (uint8_t *)op
+ ino
[n
];
226 key
.size
= off
- ino
[n
];
228 if (__call_hash(hashp
, key
.data
, (int)key
.size
) == obucket
) {
229 /* Don't switch page */
232 copyto
= ino
[n
+ 1] + diff
;
233 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
234 (size_t)(off
- ino
[n
+ 1]));
235 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
236 ino
[ndx
+ 1] = copyto
;
242 val
.data
= (uint8_t *)op
+ ino
[n
+ 1];
243 val
.size
= ino
[n
] - ino
[n
+ 1];
244 putpair(np
, &key
, &val
);
251 /* Now clean up the page */
253 temp
= sizeof(uint16_t) * (ino
[0] + 3);
254 _DIAGASSERT(copyto
>= temp
);
255 FREESPACE(ino
) = (uint16_t)(copyto
- temp
);
256 OFFSET(ino
) = copyto
;
259 (void)fprintf(stderr
, "split %d/%d\n",
260 ((uint16_t *)np
)[0] / 2,
261 ((uint16_t *)op
)[0] / 2);
263 /* unpin both pages */
264 old_bufp
->flags
&= ~BUF_PIN
;
265 new_bufp
->flags
&= ~BUF_PIN
;
270 * Called when we encounter an overflow or big key/data page during split
271 * handling. This is special cased since we have to begin checking whether
272 * the key/data pairs fit on their respective pages and because we may need
273 * overflow pages for both the old and new pages.
275 * The first page might be a page with regular key/data pairs in which case
276 * we have a regular overflow condition and just need to go on to the next
277 * page or it might be a big key/data pair in which case we need to fix the
287 uint32_t obucket
, /* Same as __split_page. */
290 int copyto
, /* First byte on page which contains key/data values. */
291 int moved
/* Number of pairs moved to new page. */
294 BUFHEAD
*bufp
; /* Buffer header for ino */
295 uint16_t *ino
; /* Page keys come off of */
296 uint16_t *np
; /* New page */
297 uint16_t *op
; /* Page keys go on to if they aren't moving */
300 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
303 uint16_t n
, off
, ov_addr
, scopyto
;
304 char *cino
; /* Character value of ino */
307 ino
= (uint16_t *)(void *)old_bufp
->page
;
308 np
= (uint16_t *)(void *)new_bufp
->page
;
309 op
= (uint16_t *)(void *)old_bufp
->page
;
311 scopyto
= (uint16_t)copyto
; /* ANSI */
315 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
316 if (__big_split(hashp
, old_bufp
,
317 new_bufp
, bufp
, (int)bufp
->addr
, obucket
, &ret
))
322 op
= (uint16_t *)(void *)old_bufp
->page
;
326 np
= (uint16_t *)(void *)new_bufp
->page
;
330 cino
= (char *)bufp
->page
;
331 ino
= (uint16_t *)(void *)cino
;
332 last_bfp
= ret
.nextp
;
333 } else if (ino
[n
+ 1] == OVFLPAGE
) {
336 * Fix up the old page -- the extra 2 are the fields
337 * which contained the overflow information.
339 ino
[0] -= (moved
+ 2);
340 temp
= sizeof(uint16_t) * (ino
[0] + 3);
341 _DIAGASSERT(scopyto
>= temp
);
342 FREESPACE(ino
) = (uint16_t)(scopyto
- temp
);
343 OFFSET(ino
) = scopyto
;
345 bufp
= __get_buf(hashp
, (uint32_t)ov_addr
, bufp
, 0);
349 ino
= (uint16_t *)(void *)bufp
->page
;
351 scopyto
= hashp
->BSIZE
;
355 __free_ovflpage(hashp
, last_bfp
);
358 /* Move regular sized pairs of there are any */
360 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
361 cino
= (char *)(void *)ino
;
362 key
.data
= (uint8_t *)cino
+ ino
[n
];
363 key
.size
= off
- ino
[n
];
364 val
.data
= (uint8_t *)cino
+ ino
[n
+ 1];
365 val
.size
= ino
[n
] - ino
[n
+ 1];
368 if (__call_hash(hashp
, key
.data
, (int)key
.size
) == obucket
) {
369 /* Keep on old page */
370 if (PAIRFITS(op
, (&key
), (&val
)))
371 putpair((char *)(void *)op
, &key
, &val
);
374 __add_ovflpage(hashp
, old_bufp
);
377 op
= (uint16_t *)(void *)old_bufp
->page
;
378 putpair((char *)(void *)op
, &key
, &val
);
380 old_bufp
->flags
|= BUF_MOD
;
382 /* Move to new page */
383 if (PAIRFITS(np
, (&key
), (&val
)))
384 putpair((char *)(void *)np
, &key
, &val
);
387 __add_ovflpage(hashp
, new_bufp
);
390 np
= (uint16_t *)(void *)new_bufp
->page
;
391 putpair((char *)(void *)np
, &key
, &val
);
393 new_bufp
->flags
|= BUF_MOD
;
398 __free_ovflpage(hashp
, last_bfp
);
403 * Add the given pair to the page
410 __addel(HTAB
*hashp
, BUFHEAD
*bufp
, const DBT
*key
, const DBT
*val
)
415 bp
= (uint16_t *)(void *)bufp
->page
;
417 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
419 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
420 /* This is the last page of a big key/data pair
421 and we need to add another page */
423 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
424 bufp
= __get_buf(hashp
, (uint32_t)bp
[bp
[0] - 1], bufp
,
428 bp
= (uint16_t *)(void *)bufp
->page
;
429 } else if (bp
[bp
[0]] != OVFLPAGE
) {
430 /* Short key/data pairs, no more pages */
433 /* Try to squeeze key on this page */
434 if (bp
[2] >= REAL_KEY
&&
435 FREESPACE(bp
) >= PAIRSIZE(key
, val
)) {
436 squeeze_key(bp
, key
, val
);
439 bufp
= __get_buf(hashp
,
440 (uint32_t)bp
[bp
[0] - 1], bufp
, 0);
443 bp
= (uint16_t *)(void *)bufp
->page
;
447 if (PAIRFITS(bp
, key
, val
))
448 putpair(bufp
->page
, key
, val
);
451 bufp
= __add_ovflpage(hashp
, bufp
);
454 sop
= (uint16_t *)(void *)bufp
->page
;
456 if (PAIRFITS(sop
, key
, val
))
457 putpair((char *)(void *)sop
, key
, val
);
459 if (__big_insert(hashp
, bufp
, key
, val
))
463 bufp
->flags
|= BUF_MOD
;
465 * If the average number of keys per bucket exceeds the fill factor,
470 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
471 return (__expand_table(hashp
));
482 __add_ovflpage(HTAB
*hashp
, BUFHEAD
*bufp
)
485 uint16_t ndx
, ovfl_num
;
490 sp
= (uint16_t *)(void *)bufp
->page
;
492 /* Check if we are dynamically determining the fill factor */
493 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
494 hashp
->FFACTOR
= (uint32_t)sp
[0] >> 1;
495 if (hashp
->FFACTOR
< MIN_FFACTOR
)
496 hashp
->FFACTOR
= MIN_FFACTOR
;
498 bufp
->flags
|= BUF_MOD
;
499 ovfl_num
= overflow_page(hashp
);
502 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
504 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, (uint32_t)ovfl_num
,
507 bufp
->ovfl
->flags
|= BUF_MOD
;
509 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
510 tmp1
, tmp2
, bufp
->ovfl
->addr
);
514 * Since a pair is allocated on a page only if there's room to add
515 * an overflow page, we know that the OVFL information will fit on
518 sp
[ndx
+ 4] = OFFSET(sp
);
519 temp
= FREESPACE(sp
);
520 _DIAGASSERT(temp
>= OVFLSIZE
);
521 sp
[ndx
+ 3] = (uint16_t)(temp
- OVFLSIZE
);
522 sp
[ndx
+ 1] = ovfl_num
;
523 sp
[ndx
+ 2] = OVFLPAGE
;
525 #ifdef HASH_STATISTICS
533 * 0 indicates SUCCESS
534 * -1 indicates FAILURE
537 __get_page(HTAB
*hashp
, char *p
, uint32_t bucket
, int is_bucket
, int is_disk
,
548 if ((fd
== -1) || !is_disk
) {
553 page
= BUCKET_TO_PAGE(bucket
);
555 page
= OADDR_TO_PAGE(bucket
);
556 if ((rsize
= pread(fd
, p
, (size_t)size
, (off_t
)page
<< hashp
->BSHIFT
)) == -1)
558 bp
= (uint16_t *)(void *)p
;
560 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
566 if (!is_bitmap
&& !bp
[0]) {
569 if (hashp
->LORDER
!= BYTE_ORDER
) {
573 max
= (uint32_t)hashp
->BSIZE
>> 2; /* divide by 4 */
574 for (i
= 0; i
< max
; i
++)
575 M_32_SWAP(((int *)(void *)p
)[i
]);
579 for (i
= 1; i
<= max
; i
++)
587 * Write page p to disk
594 __put_page(HTAB
*hashp
, char *p
, uint32_t bucket
, int is_bucket
, int is_bitmap
)
600 if ((hashp
->fp
== -1) && open_temp(hashp
))
604 if (hashp
->LORDER
!= BYTE_ORDER
) {
609 max
= (uint32_t)hashp
->BSIZE
>> 2; /* divide by 4 */
610 for (i
= 0; i
< max
; i
++)
611 M_32_SWAP(((int *)(void *)p
)[i
]);
613 max
= ((uint16_t *)(void *)p
)[0] + 2;
614 for (i
= 0; i
<= max
; i
++)
615 M_16_SWAP(((uint16_t *)(void *)p
)[i
]);
619 page
= BUCKET_TO_PAGE(bucket
);
621 page
= OADDR_TO_PAGE(bucket
);
622 if ((wsize
= pwrite(fd
, p
, (size_t)size
, (off_t
)page
<< hashp
->BSHIFT
)) == -1)
632 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
634 * Initialize a new bitmap page. Bitmap pages are left in memory
635 * once they are read in.
638 __ibitmap(HTAB
*hashp
, int pnum
, int nbits
, int ndx
)
641 int clearbytes
, clearints
;
643 if ((ip
= malloc((size_t)hashp
->BSIZE
)) == NULL
)
646 clearints
= ((uint32_t)(nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
647 clearbytes
= clearints
<< INT_TO_BYTE
;
648 (void)memset(ip
, 0, (size_t)clearbytes
);
649 (void)memset(((char *)(void *)ip
) + clearbytes
, 0xFF,
650 (size_t)(hashp
->BSIZE
- clearbytes
));
651 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
653 hashp
->BITMAPS
[ndx
] = (uint16_t)pnum
;
654 hashp
->mapp
[ndx
] = ip
;
659 first_free(uint32_t map
)
664 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
673 overflow_page(HTAB
*hashp
)
675 uint32_t *freep
= NULL
;
676 int max_free
, offset
, splitnum
;
678 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
682 splitnum
= hashp
->OVFL_POINT
;
683 max_free
= hashp
->SPARES
[splitnum
];
685 free_page
= (uint32_t)(max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
686 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
688 /* Look through all the free maps to find the first free block */
689 first_page
= (uint32_t)hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
690 for ( i
= first_page
; i
<= free_page
; i
++ ) {
691 if (!(freep
= (uint32_t *)hashp
->mapp
[i
]) &&
692 !(freep
= fetch_bitmap(hashp
, i
)))
695 in_use_bits
= free_bit
;
697 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
699 if (i
== first_page
) {
700 bit
= hashp
->LAST_FREED
&
701 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
702 j
= bit
/ BITS_PER_MAP
;
703 bit
= bit
& ~(BITS_PER_MAP
- 1);
708 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
709 if (freep
[j
] != ALL_SET
)
713 /* No Free Page Found */
714 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
715 hashp
->SPARES
[splitnum
]++;
716 offset
= hashp
->SPARES
[splitnum
] -
717 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
719 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
720 if (offset
> SPLITMASK
) {
721 if (++splitnum
>= NCACHED
) {
722 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
726 hashp
->OVFL_POINT
= splitnum
;
727 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
728 hashp
->SPARES
[splitnum
-1]--;
732 /* Check if we need to allocate a new bitmap page */
733 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
735 if (free_page
>= NCACHED
) {
736 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
741 * This is tricky. The 1 indicates that you want the new page
742 * allocated with 1 clear bit. Actually, you are going to
743 * allocate 2 pages from this map. The first is going to be
744 * the map page, the second is the overflow page we were
745 * looking for. The init_bitmap routine automatically, sets
746 * the first bit of itself to indicate that the bitmap itself
747 * is in use. We would explicitly set the second bit, but
748 * don't have to if we tell init_bitmap not to leave it clear
749 * in the first place.
752 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
754 hashp
->SPARES
[splitnum
]++;
759 if (offset
> SPLITMASK
) {
760 if (++splitnum
>= NCACHED
) {
761 (void)write(STDERR_FILENO
, OVMSG
,
766 hashp
->OVFL_POINT
= splitnum
;
767 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
768 hashp
->SPARES
[splitnum
-1]--;
773 * Free_bit addresses the last used bit. Bump it to address
774 * the first available bit.
777 SETBIT(freep
, free_bit
);
780 /* Calculate address of the new overflow page */
781 addr
= OADDR_OF(splitnum
, offset
);
783 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
784 addr
, free_bit
, free_page
);
789 bit
= bit
+ first_free(freep
[j
]);
796 * Bits are addressed starting with 0, but overflow pages are addressed
797 * beginning at 1. Bit is a bit addressnumber, so we need to increment
798 * it to convert it to a page number.
800 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
801 if (bit
>= hashp
->LAST_FREED
)
802 hashp
->LAST_FREED
= bit
- 1;
804 /* Calculate the split number for this page */
805 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
806 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
807 if (offset
>= SPLITMASK
) {
808 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
810 return (0); /* Out of overflow pages */
812 addr
= OADDR_OF(i
, offset
);
814 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
818 /* Allocate and return the overflow page */
823 * Mark this overflow page as free.
826 __free_ovflpage(HTAB
*hashp
, BUFHEAD
*obufp
)
830 int bit_address
, free_page
, free_bit
;
835 (void)fprintf(stderr
, "Freeing %d\n", addr
);
837 ndx
= (((uint32_t)addr
) >> SPLITSHIFT
);
839 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
840 if (bit_address
< hashp
->LAST_FREED
)
841 hashp
->LAST_FREED
= bit_address
;
842 free_page
= ((uint32_t)bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
843 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
845 if (!(freep
= hashp
->mapp
[free_page
]))
846 freep
= fetch_bitmap(hashp
, free_page
);
848 * This had better never happen. It means we tried to read a bitmap
849 * that has already had overflow pages allocated off it, and we
850 * failed to read it from the file.
852 _DIAGASSERT(freep
!= NULL
);
853 CLRBIT(freep
, free_bit
);
855 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
856 obufp
->addr
, free_bit
, free_page
);
858 __reclaim_buf(hashp
, obufp
);
867 open_temp(HTAB
*hashp
)
871 char namestr
[PATH_MAX
];
876 envtmp
= getenv("TMPDIR");
878 if (-1 == snprintf(namestr
, sizeof(namestr
), "%s/_hashXXXXXX",
879 envtmp
? envtmp
: _PATH_TMP
))
882 /* Block signals; make sure file goes away at process exit. */
883 (void)sigfillset(&set
);
884 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
885 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
886 (void)unlink(namestr
);
887 (void)fcntl(hashp
->fp
, F_SETFD
, FD_CLOEXEC
);
889 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
890 return (hashp
->fp
!= -1 ? 0 : -1);
894 * We have to know that the key will fit, but the last entry on the page is
895 * an overflow pair, so we need to shift things.
898 squeeze_key(uint16_t *sp
, const DBT
*key
, const DBT
*val
)
901 uint16_t free_space
, n
, off
, pageno
;
904 p
= (char *)(void *)sp
;
906 free_space
= FREESPACE(sp
);
910 _DIAGASSERT(off
>= key
->size
);
911 off
-= (uint16_t)key
->size
;
913 memmove(p
+ off
, key
->data
, key
->size
);
914 _DIAGASSERT(off
>= val
->size
);
915 off
-= (uint16_t)val
->size
;
917 memmove(p
+ off
, val
->data
, val
->size
);
920 sp
[n
+ 2] = OVFLPAGE
;
921 temp
= PAIRSIZE(key
, val
);
922 _DIAGASSERT(free_space
>= temp
);
923 FREESPACE(sp
) = (uint16_t)(free_space
- temp
);
928 fetch_bitmap(HTAB
*hashp
, int ndx
)
930 if (ndx
>= hashp
->nmaps
)
932 if ((hashp
->mapp
[ndx
] = malloc((size_t)hashp
->BSIZE
)) == NULL
)
934 if (__get_page(hashp
,
935 (char *)(void *)hashp
->mapp
[ndx
], (uint32_t)hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
936 free(hashp
->mapp
[ndx
]);
939 return (hashp
->mapp
[ndx
]);
943 void print_chain(HTAB
*, uint32_t);
945 print_chain(HTAB
*hashp
, uint32_t addr
)
950 (void)fprintf(stderr
, "%d ", addr
);
951 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
952 bp
= (uint16_t *)bufp
->page
;
953 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
954 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
955 oaddr
= bp
[bp
[0] - 1];
956 (void)fprintf(stderr
, "%d ", (int)oaddr
);
957 bufp
= __get_buf(hashp
, (uint32_t)oaddr
, bufp
, 0);
958 bp
= (uint16_t *)bufp
->page
;
960 (void)fprintf(stderr
, "\n");