]>
git.saurik.com Git - apple/libc.git/blob - db.subproj/hash.subproj/hash_page.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Page manipulation for hashing package.
75 #include <sys/types.h>
93 static u_long
*fetch_bitmap
__P((HTAB
*, int));
94 static u_long first_free
__P((u_long
));
95 static int open_temp
__P((HTAB
*));
96 static u_short overflow_page
__P((HTAB
*));
97 static void putpair
__P((char *, const DBT
*, const DBT
*));
98 static void squeeze_key
__P((u_short
*, const DBT
*, const DBT
*));
100 __P((HTAB
*, u_int
, BUFHEAD
*, BUFHEAD
*, int, int));
102 #define PAGE_INIT(P) { \
103 ((u_short *)(P))[0] = 0; \
104 ((u_short *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_short); \
105 ((u_short *)(P))[2] = hashp->BSIZE; \
109 * This is called AFTER we have verified that there is room on the page for
110 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
116 const DBT
*key
, *val
;
118 register u_short
*bp
, n
, off
;
122 /* Enter the key first. */
125 off
= OFFSET(bp
) - key
->size
;
126 memmove(p
+ off
, key
->data
, key
->size
);
131 memmove(p
+ off
, val
->data
, val
->size
);
134 /* Adjust page info. */
136 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_short
));
146 __delpair(hashp
, bufp
, ndx
)
151 register u_short
*bp
, newoff
;
155 bp
= (u_short
*)bufp
->page
;
158 if (bp
[ndx
+ 1] < REAL_KEY
)
159 return (__big_delete(hashp
, bufp
));
161 newoff
= bp
[ndx
- 1];
163 newoff
= hashp
->BSIZE
;
164 pairlen
= newoff
- bp
[ndx
+ 1];
166 if (ndx
!= (n
- 1)) {
167 /* Hard Case -- need to shuffle keys */
169 register char *src
= bufp
->page
+ (int)OFFSET(bp
);
170 register char *dst
= src
+ (int)pairlen
;
171 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
173 /* Now adjust the pointers */
174 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
175 if (bp
[i
+ 1] == OVFLPAGE
) {
177 bp
[i
- 1] = bp
[i
+ 1];
179 bp
[i
- 2] = bp
[i
] + pairlen
;
180 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
184 /* Finally adjust the page data */
185 bp
[n
] = OFFSET(bp
) + pairlen
;
186 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_short
);
190 bufp
->flags
|= BUF_MOD
;
199 __split_page(hashp
, obucket
, nbucket
)
201 u_int obucket
, nbucket
;
203 register BUFHEAD
*new_bufp
, *old_bufp
;
204 register u_short
*ino
;
208 u_short copyto
, diff
, off
, moved
;
211 copyto
= (u_short
)hashp
->BSIZE
;
212 off
= (u_short
)hashp
->BSIZE
;
213 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
214 if (old_bufp
== NULL
)
216 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
217 if (new_bufp
== NULL
)
220 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
221 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
223 ino
= (u_short
*)(op
= old_bufp
->page
);
228 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
229 if (ino
[n
+ 1] < REAL_KEY
) {
230 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
231 (int)copyto
, (int)moved
);
232 old_bufp
->flags
&= ~BUF_PIN
;
233 new_bufp
->flags
&= ~BUF_PIN
;
237 key
.data
= (u_char
*)op
+ ino
[n
];
238 key
.size
= off
- ino
[n
];
240 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
241 /* Don't switch page */
244 copyto
= ino
[n
+ 1] + diff
;
245 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
247 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
248 ino
[ndx
+ 1] = copyto
;
254 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
255 val
.size
= ino
[n
] - ino
[n
+ 1];
256 putpair(np
, &key
, &val
);
263 /* Now clean up the page */
265 FREESPACE(ino
) = copyto
- sizeof(u_short
) * (ino
[0] + 3);
266 OFFSET(ino
) = copyto
;
269 (void)fprintf(stderr
, "split %d/%d\n",
270 ((u_short
*)np
)[0] / 2,
271 ((u_short
*)op
)[0] / 2);
273 /* unpin both pages */
274 old_bufp
->flags
&= ~BUF_PIN
;
275 new_bufp
->flags
&= ~BUF_PIN
;
280 * Called when we encounter an overflow or big key/data page during split
281 * handling. This is special cased since we have to begin checking whether
282 * the key/data pairs fit on their respective pages and because we may need
283 * overflow pages for both the old and new pages.
285 * The first page might be a page with regular key/data pairs in which case
286 * we have a regular overflow condition and just need to go on to the next
287 * page or it might be a big key/data pair in which case we need to fix the
295 ugly_split(hashp
, obucket
, old_bufp
, new_bufp
, copyto
, moved
)
297 u_int obucket
; /* Same as __split_page. */
298 BUFHEAD
*old_bufp
, *new_bufp
;
299 int copyto
; /* First byte on page which contains key/data values. */
300 int moved
; /* Number of pairs moved to new page. */
302 register BUFHEAD
*bufp
; /* Buffer header for ino */
303 register u_short
*ino
; /* Page keys come off of */
304 register u_short
*np
; /* New page */
305 register u_short
*op
; /* Page keys go on to if they aren't moving */
307 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
310 u_short n
, off
, ov_addr
, scopyto
;
311 char *cino
; /* Character value of ino */
314 ino
= (u_short
*)old_bufp
->page
;
315 np
= (u_short
*)new_bufp
->page
;
316 op
= (u_short
*)old_bufp
->page
;
318 scopyto
= (u_short
)copyto
; /* ANSI */
322 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
323 if (__big_split(hashp
, old_bufp
,
324 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
329 op
= (u_short
*)old_bufp
->page
;
333 np
= (u_short
*)new_bufp
->page
;
337 cino
= (char *)bufp
->page
;
338 ino
= (u_short
*)cino
;
339 last_bfp
= ret
.nextp
;
340 } else if (ino
[n
+ 1] == OVFLPAGE
) {
343 * Fix up the old page -- the extra 2 are the fields
344 * which contained the overflow information.
346 ino
[0] -= (moved
+ 2);
348 scopyto
- sizeof(u_short
) * (ino
[0] + 3);
349 OFFSET(ino
) = scopyto
;
351 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
355 ino
= (u_short
*)bufp
->page
;
357 scopyto
= hashp
->BSIZE
;
361 __free_ovflpage(hashp
, last_bfp
);
364 /* Move regular sized pairs of there are any */
366 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
368 key
.data
= (u_char
*)cino
+ ino
[n
];
369 key
.size
= off
- ino
[n
];
370 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
371 val
.size
= ino
[n
] - ino
[n
+ 1];
374 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
375 /* Keep on old page */
376 if (PAIRFITS(op
, (&key
), (&val
)))
377 putpair((char *)op
, &key
, &val
);
380 __add_ovflpage(hashp
, old_bufp
);
383 op
= (u_short
*)old_bufp
->page
;
384 putpair((char *)op
, &key
, &val
);
386 old_bufp
->flags
|= BUF_MOD
;
388 /* Move to new page */
389 if (PAIRFITS(np
, (&key
), (&val
)))
390 putpair((char *)np
, &key
, &val
);
393 __add_ovflpage(hashp
, new_bufp
);
396 np
= (u_short
*)new_bufp
->page
;
397 putpair((char *)np
, &key
, &val
);
399 new_bufp
->flags
|= BUF_MOD
;
404 __free_ovflpage(hashp
, last_bfp
);
409 * Add the given pair to the page
416 __addel(hashp
, bufp
, key
, val
)
419 const DBT
*key
, *val
;
421 register u_short
*bp
, *sop
;
424 bp
= (u_short
*)bufp
->page
;
426 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
428 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
429 /* This is the last page of a big key/data pair
430 and we need to add another page */
432 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
433 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
436 bp
= (u_short
*)bufp
->page
;
438 /* Try to squeeze key on this page */
439 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
440 squeeze_key(bp
, key
, val
);
443 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
446 bp
= (u_short
*)bufp
->page
;
449 if (PAIRFITS(bp
, key
, val
))
450 putpair(bufp
->page
, key
, val
);
453 bufp
= __add_ovflpage(hashp
, bufp
);
456 sop
= (u_short
*)bufp
->page
;
458 if (PAIRFITS(sop
, key
, val
))
459 putpair((char *)sop
, key
, val
);
461 if (__big_insert(hashp
, bufp
, key
, val
))
464 bufp
->flags
|= BUF_MOD
;
466 * If the average number of keys per bucket exceeds the fill factor,
471 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
472 return (__expand_table(hashp
));
483 __add_ovflpage(hashp
, bufp
)
487 register u_short
*sp
;
488 u_short ndx
, ovfl_num
;
492 sp
= (u_short
*)bufp
->page
;
494 /* Check if we are dynamically determining the fill factor */
495 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
496 hashp
->FFACTOR
= sp
[0] >> 1;
497 if (hashp
->FFACTOR
< MIN_FFACTOR
)
498 hashp
->FFACTOR
= MIN_FFACTOR
;
500 bufp
->flags
|= BUF_MOD
;
501 ovfl_num
= overflow_page(hashp
);
504 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
506 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
508 bufp
->ovfl
->flags
|= BUF_MOD
;
510 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
511 tmp1
, tmp2
, bufp
->ovfl
->addr
);
515 * Since a pair is allocated on a page only if there's room to add
516 * an overflow page, we know that the OVFL information will fit on
519 sp
[ndx
+ 4] = OFFSET(sp
);
520 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
521 sp
[ndx
+ 1] = ovfl_num
;
522 sp
[ndx
+ 2] = OVFLPAGE
;
524 #ifdef HASH_STATISTICS
532 * 0 indicates SUCCESS
533 * -1 indicates FAILURE
536 __get_page(hashp
, p
, bucket
, is_bucket
, is_disk
, is_bitmap
)
540 int is_bucket
, is_disk
, is_bitmap
;
542 register int fd
, page
, size
;
549 if ((fd
== -1) || !is_disk
) {
554 page
= BUCKET_TO_PAGE(bucket
);
556 page
= OADDR_TO_PAGE(bucket
);
557 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
558 ((rsize
= read(fd
, p
, size
)) == -1))
562 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
568 if (!is_bitmap
&& !bp
[0]) {
571 if (hashp
->LORDER
!= BYTE_ORDER
) {
575 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
576 for (i
= 0; i
< max
; i
++)
577 M_32_SWAP(((long *)p
)[i
]);
581 for (i
= 1; i
<= max
; i
++)
589 * Write page p to disk
596 __put_page(hashp
, p
, bucket
, is_bucket
, is_bitmap
)
600 int is_bucket
, is_bitmap
;
602 register int fd
, page
, size
;
606 if ((hashp
->fp
== -1) && open_temp(hashp
))
610 if (hashp
->LORDER
!= BYTE_ORDER
) {
615 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
616 for (i
= 0; i
< max
; i
++)
617 M_32_SWAP(((long *)p
)[i
]);
619 max
= ((u_short
*)p
)[0] + 2;
620 for (i
= 0; i
<= max
; i
++)
621 M_16_SWAP(((u_short
*)p
)[i
]);
625 page
= BUCKET_TO_PAGE(bucket
);
627 page
= OADDR_TO_PAGE(bucket
);
628 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
629 ((wsize
= write(fd
, p
, size
)) == -1))
639 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
641 * Initialize a new bitmap page. Bitmap pages are left in memory
642 * once they are read in.
645 __init_bitmap(hashp
, pnum
, nbits
, ndx
)
647 int pnum
, nbits
, ndx
;
650 int clearbytes
, clearints
;
652 if ((ip
= (u_long
*)malloc(hashp
->BSIZE
)) == NULL
)
655 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
656 clearbytes
= clearints
<< INT_TO_BYTE
;
657 (void)memset((char *)ip
, 0, clearbytes
);
658 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
659 hashp
->BSIZE
- clearbytes
);
660 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
662 hashp
->BITMAPS
[ndx
] = (u_short
)pnum
;
663 hashp
->mapp
[ndx
] = ip
;
671 register u_long i
, mask
;
674 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
686 register u_long
*freep
;
687 register int max_free
, offset
, splitnum
;
689 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
693 splitnum
= hashp
->OVFL_POINT
;
694 max_free
= hashp
->SPARES
[splitnum
];
696 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
697 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
699 /* Look through all the free maps to find the first free block */
700 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
701 for ( i
= first_page
; i
<= free_page
; i
++ ) {
702 if (!(freep
= (u_long
*)hashp
->mapp
[i
]) &&
703 !(freep
= fetch_bitmap(hashp
, i
)))
706 in_use_bits
= free_bit
;
708 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
710 if (i
== first_page
) {
711 bit
= hashp
->LAST_FREED
&
712 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
713 j
= bit
/ BITS_PER_MAP
;
714 bit
= bit
& ~(BITS_PER_MAP
- 1);
719 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
720 if (freep
[j
] != ALL_SET
)
724 /* No Free Page Found */
725 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
726 hashp
->SPARES
[splitnum
]++;
727 offset
= hashp
->SPARES
[splitnum
] -
728 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
730 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
731 if (offset
> SPLITMASK
) {
732 if (++splitnum
>= NCACHED
) {
733 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
736 hashp
->OVFL_POINT
= splitnum
;
737 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
738 hashp
->SPARES
[splitnum
-1]--;
742 /* Check if we need to allocate a new bitmap page */
743 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
745 if (free_page
>= NCACHED
) {
746 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
750 * This is tricky. The 1 indicates that you want the new page
751 * allocated with 1 clear bit. Actually, you are going to
752 * allocate 2 pages from this map. The first is going to be
753 * the map page, the second is the overflow page we were
754 * looking for. The init_bitmap routine automatically, sets
755 * the first bit of itself to indicate that the bitmap itself
756 * is in use. We would explicitly set the second bit, but
757 * don't have to if we tell init_bitmap not to leave it clear
758 * in the first place.
760 if (__init_bitmap(hashp
, (int)OADDR_OF(splitnum
, offset
),
763 hashp
->SPARES
[splitnum
]++;
768 if (offset
> SPLITMASK
) {
769 if (++splitnum
>= NCACHED
) {
770 (void)write(STDERR_FILENO
, OVMSG
,
774 hashp
->OVFL_POINT
= splitnum
;
775 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
776 hashp
->SPARES
[splitnum
-1]--;
781 * Free_bit addresses the last used bit. Bump it to address
782 * the first available bit.
785 SETBIT(freep
, free_bit
);
788 /* Calculate address of the new overflow page */
789 addr
= OADDR_OF(splitnum
, offset
);
791 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
792 addr
, free_bit
, free_page
);
797 bit
= bit
+ first_free(freep
[j
]);
804 * Bits are addressed starting with 0, but overflow pages are addressed
805 * beginning at 1. Bit is a bit addressnumber, so we need to increment
806 * it to convert it to a page number.
808 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
809 if (bit
>= hashp
->LAST_FREED
)
810 hashp
->LAST_FREED
= bit
- 1;
812 /* Calculate the split number for this page */
813 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
814 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
815 if (offset
>= SPLITMASK
)
816 return (NULL
); /* Out of overflow pages */
817 addr
= OADDR_OF(i
, offset
);
819 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
823 /* Allocate and return the overflow page */
828 * Mark this overflow page as free.
831 __free_ovflpage(hashp
, obufp
)
835 register u_short addr
;
837 int bit_address
, free_page
, free_bit
;
842 (void)fprintf(stderr
, "Freeing %d\n", addr
);
844 ndx
= (((u_short
)addr
) >> SPLITSHIFT
);
846 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
847 if (bit_address
< hashp
->LAST_FREED
)
848 hashp
->LAST_FREED
= bit_address
;
849 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
850 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
852 if (!(freep
= hashp
->mapp
[free_page
]))
853 freep
= fetch_bitmap(hashp
, free_page
);
856 * This had better never happen. It means we tried to read a bitmap
857 * that has already had overflow pages allocated off it, and we
858 * failed to read it from the file.
863 CLRBIT(freep
, free_bit
);
865 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
866 obufp
->addr
, free_bit
, free_page
);
868 __reclaim_buf(hashp
, obufp
);
881 static char namestr
[] = "_hashXXXXXX";
883 /* Block signals; make sure file goes away at process exit. */
884 (void)sigfillset(&set
);
885 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
886 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
887 (void)unlink(namestr
);
888 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
890 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
891 return (hashp
->fp
!= -1 ? 0 : -1);
895 * We have to know that the key will fit, but the last entry on the page is
896 * an overflow pair, so we need to shift things.
899 squeeze_key(sp
, key
, val
)
901 const DBT
*key
, *val
;
904 u_short free_space
, n
, off
, pageno
;
908 free_space
= FREESPACE(sp
);
914 memmove(p
+ off
, key
->data
, key
->size
);
917 memmove(p
+ off
, val
->data
, val
->size
);
920 sp
[n
+ 2] = OVFLPAGE
;
921 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
926 fetch_bitmap(hashp
, ndx
)
930 if (ndx
>= hashp
->nmaps
)
932 if ((hashp
->mapp
[ndx
] = (u_long
*)malloc(hashp
->BSIZE
)) == NULL
)
934 if (__get_page(hashp
,
935 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
936 free(hashp
->mapp
[ndx
]);
939 return (hashp
->mapp
[ndx
]);
950 (void)fprintf(stderr
, "%d ", addr
);
951 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
952 bp
= (short *)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
, (int)oaddr
, bufp
, 0);
958 bp
= (short *)bufp
->page
;
960 (void)fprintf(stderr
, "\n");