]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash_page.c
a48fe48228f761a567d9389788d00b70b3f4b5b1
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_page.c 8.7 (Berkeley) 8/16/94";
61 #endif /* LIBC_SCCS and not lint */
62 #include <sys/cdefs.h>
68 * Page manipulation for hashing package.
80 #include <sys/types.h>
98 static u_int32_t
*fetch_bitmap(HTAB
*, int);
99 static u_int32_t
first_free(u_int32_t
);
100 static int open_temp(HTAB
*);
101 static u_int16_t
overflow_page(HTAB
*);
102 static void putpair(char *, const DBT
*, const DBT
*);
103 static void squeeze_key(u_int16_t
*, const DBT
*, const DBT
*);
104 static int ugly_split
105 (HTAB
*, u_int32_t
, BUFHEAD
*, BUFHEAD
*, int, int);
107 #define PAGE_INIT(P) { \
108 ((u_int16_t *)(P))[0] = 0; \
109 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
110 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
114 * This is called AFTER we have verified that there is room on the page for
115 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
121 const DBT
*key
, *val
;
123 u_int16_t
*bp
, n
, off
;
127 /* Enter the key first. */
130 off
= OFFSET(bp
) - key
->size
;
131 memmove(p
+ off
, key
->data
, key
->size
);
136 memmove(p
+ off
, val
->data
, val
->size
);
139 /* Adjust page info. */
141 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_int16_t
));
151 __delpair(hashp
, bufp
, ndx
)
156 u_int16_t
*bp
, newoff
;
160 bp
= (u_int16_t
*)bufp
->page
;
163 if (bp
[ndx
+ 1] < REAL_KEY
)
164 return (__big_delete(hashp
, bufp
));
166 newoff
= bp
[ndx
- 1];
168 newoff
= hashp
->BSIZE
;
169 pairlen
= newoff
- bp
[ndx
+ 1];
171 if (ndx
!= (n
- 1)) {
172 /* Hard Case -- need to shuffle keys */
174 char *src
= bufp
->page
+ (int)OFFSET(bp
);
175 char *dst
= src
+ (int)pairlen
;
176 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
178 /* Now adjust the pointers */
179 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
180 if (bp
[i
+ 1] == OVFLPAGE
) {
182 bp
[i
- 1] = bp
[i
+ 1];
184 bp
[i
- 2] = bp
[i
] + pairlen
;
185 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
189 /* Finally adjust the page data */
190 bp
[n
] = OFFSET(bp
) + pairlen
;
191 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_int16_t
);
195 bufp
->flags
|= BUF_MOD
;
204 __split_page(hashp
, obucket
, nbucket
)
206 u_int32_t obucket
, nbucket
;
208 BUFHEAD
*new_bufp
, *old_bufp
;
213 u_int16_t copyto
, diff
, off
, moved
;
216 copyto
= (u_int16_t
)hashp
->BSIZE
;
217 off
= (u_int16_t
)hashp
->BSIZE
;
218 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
219 if (old_bufp
== NULL
)
221 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
222 if (new_bufp
== NULL
)
225 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
226 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
228 ino
= (u_int16_t
*)(op
= old_bufp
->page
);
233 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
234 if (ino
[n
+ 1] < REAL_KEY
) {
235 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
236 (int)copyto
, (int)moved
);
237 old_bufp
->flags
&= ~BUF_PIN
;
238 new_bufp
->flags
&= ~BUF_PIN
;
242 key
.data
= (u_char
*)op
+ ino
[n
];
243 key
.size
= off
- ino
[n
];
245 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
246 /* Don't switch page */
249 copyto
= ino
[n
+ 1] + diff
;
250 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
252 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
253 ino
[ndx
+ 1] = copyto
;
259 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
260 val
.size
= ino
[n
] - ino
[n
+ 1];
261 putpair(np
, &key
, &val
);
268 /* Now clean up the page */
270 FREESPACE(ino
) = copyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
271 OFFSET(ino
) = copyto
;
274 (void)fprintf(stderr
, "split %d/%d\n",
275 ((u_int16_t
*)np
)[0] / 2,
276 ((u_int16_t
*)op
)[0] / 2);
278 /* unpin both pages */
279 old_bufp
->flags
&= ~BUF_PIN
;
280 new_bufp
->flags
&= ~BUF_PIN
;
285 * Called when we encounter an overflow or big key/data page during split
286 * handling. This is special cased since we have to begin checking whether
287 * the key/data pairs fit on their respective pages and because we may need
288 * overflow pages for both the old and new pages.
290 * The first page might be a page with regular key/data pairs in which case
291 * we have a regular overflow condition and just need to go on to the next
292 * page or it might be a big key/data pair in which case we need to fix the
300 ugly_split(hashp
, obucket
, old_bufp
, new_bufp
, copyto
, moved
)
302 u_int32_t obucket
; /* Same as __split_page. */
303 BUFHEAD
*old_bufp
, *new_bufp
;
304 int copyto
; /* First byte on page which contains key/data values. */
305 int moved
; /* Number of pairs moved to new page. */
307 BUFHEAD
*bufp
; /* Buffer header for ino */
308 u_int16_t
*ino
; /* Page keys come off of */
309 u_int16_t
*np
; /* New page */
310 u_int16_t
*op
; /* Page keys go on to if they aren't moving */
312 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
315 u_int16_t n
, off
, ov_addr
, scopyto
;
316 char *cino
; /* Character value of ino */
319 ino
= (u_int16_t
*)old_bufp
->page
;
320 np
= (u_int16_t
*)new_bufp
->page
;
321 op
= (u_int16_t
*)old_bufp
->page
;
323 scopyto
= (u_int16_t
)copyto
; /* ANSI */
327 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
328 if (__big_split(hashp
, old_bufp
,
329 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
334 op
= (u_int16_t
*)old_bufp
->page
;
338 np
= (u_int16_t
*)new_bufp
->page
;
342 cino
= (char *)bufp
->page
;
343 ino
= (u_int16_t
*)cino
;
344 last_bfp
= ret
.nextp
;
345 } else if (ino
[n
+ 1] == OVFLPAGE
) {
348 * Fix up the old page -- the extra 2 are the fields
349 * which contained the overflow information.
351 ino
[0] -= (moved
+ 2);
353 scopyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
354 OFFSET(ino
) = scopyto
;
356 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
360 ino
= (u_int16_t
*)bufp
->page
;
362 scopyto
= hashp
->BSIZE
;
366 __free_ovflpage(hashp
, last_bfp
);
369 /* Move regular sized pairs of there are any */
371 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
373 key
.data
= (u_char
*)cino
+ ino
[n
];
374 key
.size
= off
- ino
[n
];
375 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
376 val
.size
= ino
[n
] - ino
[n
+ 1];
379 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
380 /* Keep on old page */
381 if (PAIRFITS(op
, (&key
), (&val
)))
382 putpair((char *)op
, &key
, &val
);
385 __add_ovflpage(hashp
, old_bufp
);
388 op
= (u_int16_t
*)old_bufp
->page
;
389 putpair((char *)op
, &key
, &val
);
391 old_bufp
->flags
|= BUF_MOD
;
393 /* Move to new page */
394 if (PAIRFITS(np
, (&key
), (&val
)))
395 putpair((char *)np
, &key
, &val
);
398 __add_ovflpage(hashp
, new_bufp
);
401 np
= (u_int16_t
*)new_bufp
->page
;
402 putpair((char *)np
, &key
, &val
);
404 new_bufp
->flags
|= BUF_MOD
;
409 __free_ovflpage(hashp
, last_bfp
);
414 * Add the given pair to the page
421 __addel(hashp
, bufp
, key
, val
)
424 const DBT
*key
, *val
;
429 bp
= (u_int16_t
*)bufp
->page
;
431 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
433 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
434 /* This is the last page of a big key/data pair
435 and we need to add another page */
437 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
438 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
441 bp
= (u_int16_t
*)bufp
->page
;
443 /* Try to squeeze key on this page */
444 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
445 squeeze_key(bp
, key
, val
);
448 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
451 bp
= (u_int16_t
*)bufp
->page
;
454 if (PAIRFITS(bp
, key
, val
))
455 putpair(bufp
->page
, key
, val
);
458 bufp
= __add_ovflpage(hashp
, bufp
);
461 sop
= (u_int16_t
*)bufp
->page
;
463 if (PAIRFITS(sop
, key
, val
))
464 putpair((char *)sop
, key
, val
);
466 if (__big_insert(hashp
, bufp
, key
, val
))
469 bufp
->flags
|= BUF_MOD
;
471 * If the average number of keys per bucket exceeds the fill factor,
476 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
477 return (__expand_table(hashp
));
488 __add_ovflpage(hashp
, bufp
)
493 u_int16_t ndx
, ovfl_num
;
497 sp
= (u_int16_t
*)bufp
->page
;
499 /* Check if we are dynamically determining the fill factor */
500 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
501 hashp
->FFACTOR
= sp
[0] >> 1;
502 if (hashp
->FFACTOR
< MIN_FFACTOR
)
503 hashp
->FFACTOR
= MIN_FFACTOR
;
505 bufp
->flags
|= BUF_MOD
;
506 ovfl_num
= overflow_page(hashp
);
509 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
511 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
513 bufp
->ovfl
->flags
|= BUF_MOD
;
515 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
516 tmp1
, tmp2
, bufp
->ovfl
->addr
);
520 * Since a pair is allocated on a page only if there's room to add
521 * an overflow page, we know that the OVFL information will fit on
524 sp
[ndx
+ 4] = OFFSET(sp
);
525 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
526 sp
[ndx
+ 1] = ovfl_num
;
527 sp
[ndx
+ 2] = OVFLPAGE
;
529 #ifdef HASH_STATISTICS
537 * 0 indicates SUCCESS
538 * -1 indicates FAILURE
541 __get_page(hashp
, p
, bucket
, is_bucket
, is_disk
, is_bitmap
)
545 int is_bucket
, is_disk
, is_bitmap
;
554 if ((fd
== -1) || !is_disk
) {
559 page
= BUCKET_TO_PAGE(bucket
);
561 page
= OADDR_TO_PAGE(bucket
);
562 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
563 ((rsize
= read(fd
, p
, size
)) == -1))
567 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
573 if (!is_bitmap
&& !bp
[0]) {
576 if (hashp
->LORDER
!= BYTE_ORDER
) {
580 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
581 for (i
= 0; i
< max
; i
++)
582 M_32_SWAP(((int *)p
)[i
]);
586 for (i
= 1; i
<= max
; i
++)
594 * Write page p to disk
601 __put_page(hashp
, p
, bucket
, is_bucket
, is_bitmap
)
605 int is_bucket
, is_bitmap
;
611 if ((hashp
->fp
== -1) && open_temp(hashp
))
615 if (hashp
->LORDER
!= BYTE_ORDER
) {
620 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
621 for (i
= 0; i
< max
; i
++)
622 M_32_SWAP(((int *)p
)[i
]);
624 max
= ((u_int16_t
*)p
)[0] + 2;
625 for (i
= 0; i
<= max
; i
++)
626 M_16_SWAP(((u_int16_t
*)p
)[i
]);
630 page
= BUCKET_TO_PAGE(bucket
);
632 page
= OADDR_TO_PAGE(bucket
);
633 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
634 ((wsize
= write(fd
, p
, size
)) == -1))
644 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
646 * Initialize a new bitmap page. Bitmap pages are left in memory
647 * once they are read in.
650 __ibitmap(hashp
, pnum
, nbits
, ndx
)
652 int pnum
, nbits
, ndx
;
655 int clearbytes
, clearints
;
657 if ((ip
= (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
660 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
661 clearbytes
= clearints
<< INT_TO_BYTE
;
662 (void)memset((char *)ip
, 0, clearbytes
);
663 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
664 hashp
->BSIZE
- clearbytes
);
665 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
667 hashp
->BITMAPS
[ndx
] = (u_int16_t
)pnum
;
668 hashp
->mapp
[ndx
] = ip
;
679 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
692 int max_free
, offset
, splitnum
;
694 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
698 splitnum
= hashp
->OVFL_POINT
;
699 max_free
= hashp
->SPARES
[splitnum
];
701 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
702 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
704 /* Look through all the free maps to find the first free block */
705 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
706 for ( i
= first_page
; i
<= free_page
; i
++ ) {
707 if (!(freep
= (u_int32_t
*)hashp
->mapp
[i
]) &&
708 !(freep
= fetch_bitmap(hashp
, i
)))
711 in_use_bits
= free_bit
;
713 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
715 if (i
== first_page
) {
716 bit
= hashp
->LAST_FREED
&
717 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
718 j
= bit
/ BITS_PER_MAP
;
719 bit
= bit
& ~(BITS_PER_MAP
- 1);
724 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
725 if (freep
[j
] != ALL_SET
)
729 /* No Free Page Found */
730 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
731 hashp
->SPARES
[splitnum
]++;
732 offset
= hashp
->SPARES
[splitnum
] -
733 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
735 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
736 if (offset
> SPLITMASK
) {
737 if (++splitnum
>= NCACHED
) {
738 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
741 hashp
->OVFL_POINT
= splitnum
;
742 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
743 hashp
->SPARES
[splitnum
-1]--;
747 /* Check if we need to allocate a new bitmap page */
748 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
750 if (free_page
>= NCACHED
) {
751 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
755 * This is tricky. The 1 indicates that you want the new page
756 * allocated with 1 clear bit. Actually, you are going to
757 * allocate 2 pages from this map. The first is going to be
758 * the map page, the second is the overflow page we were
759 * looking for. The init_bitmap routine automatically, sets
760 * the first bit of itself to indicate that the bitmap itself
761 * is in use. We would explicitly set the second bit, but
762 * don't have to if we tell init_bitmap not to leave it clear
763 * in the first place.
766 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
768 hashp
->SPARES
[splitnum
]++;
773 if (offset
> SPLITMASK
) {
774 if (++splitnum
>= NCACHED
) {
775 (void)write(STDERR_FILENO
, OVMSG
,
779 hashp
->OVFL_POINT
= splitnum
;
780 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
781 hashp
->SPARES
[splitnum
-1]--;
786 * Free_bit addresses the last used bit. Bump it to address
787 * the first available bit.
790 SETBIT(freep
, free_bit
);
793 /* Calculate address of the new overflow page */
794 addr
= OADDR_OF(splitnum
, offset
);
796 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
797 addr
, free_bit
, free_page
);
802 bit
= bit
+ first_free(freep
[j
]);
809 * Bits are addressed starting with 0, but overflow pages are addressed
810 * beginning at 1. Bit is a bit addressnumber, so we need to increment
811 * it to convert it to a page number.
813 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
814 if (bit
>= hashp
->LAST_FREED
)
815 hashp
->LAST_FREED
= bit
- 1;
817 /* Calculate the split number for this page */
818 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
819 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
820 if (offset
>= SPLITMASK
)
821 return (0); /* Out of overflow pages */
822 addr
= OADDR_OF(i
, offset
);
824 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
828 /* Allocate and return the overflow page */
833 * Mark this overflow page as free.
836 __free_ovflpage(hashp
, obufp
)
842 int bit_address
, free_page
, free_bit
;
847 (void)fprintf(stderr
, "Freeing %d\n", addr
);
849 ndx
= (((u_int16_t
)addr
) >> SPLITSHIFT
);
851 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
852 if (bit_address
< hashp
->LAST_FREED
)
853 hashp
->LAST_FREED
= bit_address
;
854 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
855 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
857 if (!(freep
= hashp
->mapp
[free_page
]))
858 freep
= fetch_bitmap(hashp
, free_page
);
861 * This had better never happen. It means we tried to read a bitmap
862 * that has already had overflow pages allocated off it, and we
863 * failed to read it from the file.
868 CLRBIT(freep
, free_bit
);
870 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
871 obufp
->addr
, free_bit
, free_page
);
873 __reclaim_buf(hashp
, obufp
);
886 static char namestr
[] = "_hashXXXXXX";
888 /* Block signals; make sure file goes away at process exit. */
889 (void)sigfillset(&set
);
890 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
891 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
892 (void)unlink(namestr
);
893 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
895 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
896 return (hashp
->fp
!= -1 ? 0 : -1);
900 * We have to know that the key will fit, but the last entry on the page is
901 * an overflow pair, so we need to shift things.
904 squeeze_key(sp
, key
, val
)
906 const DBT
*key
, *val
;
909 u_int16_t free_space
, n
, off
, pageno
;
913 free_space
= FREESPACE(sp
);
919 memmove(p
+ off
, key
->data
, key
->size
);
922 memmove(p
+ off
, val
->data
, val
->size
);
925 sp
[n
+ 2] = OVFLPAGE
;
926 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
931 fetch_bitmap(hashp
, ndx
)
935 if (ndx
>= hashp
->nmaps
)
937 if ((hashp
->mapp
[ndx
] = (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
939 if (__get_page(hashp
,
940 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
941 free(hashp
->mapp
[ndx
]);
944 return (hashp
->mapp
[ndx
]);
955 (void)fprintf(stderr
, "%d ", addr
);
956 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
957 bp
= (short *)bufp
->page
;
958 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
959 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
960 oaddr
= bp
[bp
[0] - 1];
961 (void)fprintf(stderr
, "%d ", (int)oaddr
);
962 bufp
= __get_buf(hashp
, (int)oaddr
, bufp
, 0);
963 bp
= (short *)bufp
->page
;
965 (void)fprintf(stderr
, "\n");