]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash_page.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Page manipulation for hashing package.
78 #include <sys/types.h>
96 static u_long
*fetch_bitmap
__P((HTAB
*, int));
97 static u_long first_free
__P((u_long
));
98 static int open_temp
__P((HTAB
*));
99 static u_short overflow_page
__P((HTAB
*));
100 static void putpair
__P((char *, const DBT
*, const DBT
*));
101 static void squeeze_key
__P((u_short
*, const DBT
*, const DBT
*));
102 static int ugly_split
103 __P((HTAB
*, u_int
, BUFHEAD
*, BUFHEAD
*, int, int));
105 #define PAGE_INIT(P) { \
106 ((u_short *)(P))[0] = 0; \
107 ((u_short *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_short); \
108 ((u_short *)(P))[2] = hashp->BSIZE; \
112 * This is called AFTER we have verified that there is room on the page for
113 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
119 const DBT
*key
, *val
;
121 register u_short
*bp
, n
, off
;
125 /* Enter the key first. */
128 off
= OFFSET(bp
) - key
->size
;
129 memmove(p
+ off
, key
->data
, key
->size
);
134 memmove(p
+ off
, val
->data
, val
->size
);
137 /* Adjust page info. */
139 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_short
));
149 __delpair(hashp
, bufp
, ndx
)
154 register u_short
*bp
, newoff
;
158 bp
= (u_short
*)bufp
->page
;
161 if (bp
[ndx
+ 1] < REAL_KEY
)
162 return (__big_delete(hashp
, bufp
));
164 newoff
= bp
[ndx
- 1];
166 newoff
= hashp
->BSIZE
;
167 pairlen
= newoff
- bp
[ndx
+ 1];
169 if (ndx
!= (n
- 1)) {
170 /* Hard Case -- need to shuffle keys */
172 register char *src
= bufp
->page
+ (int)OFFSET(bp
);
173 register char *dst
= src
+ (int)pairlen
;
174 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
176 /* Now adjust the pointers */
177 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
178 if (bp
[i
+ 1] == OVFLPAGE
) {
180 bp
[i
- 1] = bp
[i
+ 1];
182 bp
[i
- 2] = bp
[i
] + pairlen
;
183 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
187 /* Finally adjust the page data */
188 bp
[n
] = OFFSET(bp
) + pairlen
;
189 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_short
);
193 bufp
->flags
|= BUF_MOD
;
202 __split_page(hashp
, obucket
, nbucket
)
204 u_int obucket
, nbucket
;
206 register BUFHEAD
*new_bufp
, *old_bufp
;
207 register u_short
*ino
;
211 u_short copyto
, diff
, off
, moved
;
214 copyto
= (u_short
)hashp
->BSIZE
;
215 off
= (u_short
)hashp
->BSIZE
;
216 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
217 if (old_bufp
== NULL
)
219 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
220 if (new_bufp
== NULL
)
223 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
224 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
226 ino
= (u_short
*)(op
= old_bufp
->page
);
231 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
232 if (ino
[n
+ 1] < REAL_KEY
) {
233 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
234 (int)copyto
, (int)moved
);
235 old_bufp
->flags
&= ~BUF_PIN
;
236 new_bufp
->flags
&= ~BUF_PIN
;
240 key
.data
= (u_char
*)op
+ ino
[n
];
241 key
.size
= off
- ino
[n
];
243 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
244 /* Don't switch page */
247 copyto
= ino
[n
+ 1] + diff
;
248 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
250 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
251 ino
[ndx
+ 1] = copyto
;
257 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
258 val
.size
= ino
[n
] - ino
[n
+ 1];
259 putpair(np
, &key
, &val
);
266 /* Now clean up the page */
268 FREESPACE(ino
) = copyto
- sizeof(u_short
) * (ino
[0] + 3);
269 OFFSET(ino
) = copyto
;
272 (void)fprintf(stderr
, "split %d/%d\n",
273 ((u_short
*)np
)[0] / 2,
274 ((u_short
*)op
)[0] / 2);
276 /* unpin both pages */
277 old_bufp
->flags
&= ~BUF_PIN
;
278 new_bufp
->flags
&= ~BUF_PIN
;
283 * Called when we encounter an overflow or big key/data page during split
284 * handling. This is special cased since we have to begin checking whether
285 * the key/data pairs fit on their respective pages and because we may need
286 * overflow pages for both the old and new pages.
288 * The first page might be a page with regular key/data pairs in which case
289 * we have a regular overflow condition and just need to go on to the next
290 * page or it might be a big key/data pair in which case we need to fix the
298 ugly_split(hashp
, obucket
, old_bufp
, new_bufp
, copyto
, moved
)
300 u_int obucket
; /* Same as __split_page. */
301 BUFHEAD
*old_bufp
, *new_bufp
;
302 int copyto
; /* First byte on page which contains key/data values. */
303 int moved
; /* Number of pairs moved to new page. */
305 register BUFHEAD
*bufp
; /* Buffer header for ino */
306 register u_short
*ino
; /* Page keys come off of */
307 register u_short
*np
; /* New page */
308 register u_short
*op
; /* Page keys go on to if they aren't moving */
310 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
313 u_short n
, off
, ov_addr
, scopyto
;
314 char *cino
; /* Character value of ino */
317 ino
= (u_short
*)old_bufp
->page
;
318 np
= (u_short
*)new_bufp
->page
;
319 op
= (u_short
*)old_bufp
->page
;
321 scopyto
= (u_short
)copyto
; /* ANSI */
325 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
326 if (__big_split(hashp
, old_bufp
,
327 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
332 op
= (u_short
*)old_bufp
->page
;
336 np
= (u_short
*)new_bufp
->page
;
340 cino
= (char *)bufp
->page
;
341 ino
= (u_short
*)cino
;
342 last_bfp
= ret
.nextp
;
343 } else if (ino
[n
+ 1] == OVFLPAGE
) {
346 * Fix up the old page -- the extra 2 are the fields
347 * which contained the overflow information.
349 ino
[0] -= (moved
+ 2);
351 scopyto
- sizeof(u_short
) * (ino
[0] + 3);
352 OFFSET(ino
) = scopyto
;
354 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
358 ino
= (u_short
*)bufp
->page
;
360 scopyto
= hashp
->BSIZE
;
364 __free_ovflpage(hashp
, last_bfp
);
367 /* Move regular sized pairs of there are any */
369 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
371 key
.data
= (u_char
*)cino
+ ino
[n
];
372 key
.size
= off
- ino
[n
];
373 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
374 val
.size
= ino
[n
] - ino
[n
+ 1];
377 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
378 /* Keep on old page */
379 if (PAIRFITS(op
, (&key
), (&val
)))
380 putpair((char *)op
, &key
, &val
);
383 __add_ovflpage(hashp
, old_bufp
);
386 op
= (u_short
*)old_bufp
->page
;
387 putpair((char *)op
, &key
, &val
);
389 old_bufp
->flags
|= BUF_MOD
;
391 /* Move to new page */
392 if (PAIRFITS(np
, (&key
), (&val
)))
393 putpair((char *)np
, &key
, &val
);
396 __add_ovflpage(hashp
, new_bufp
);
399 np
= (u_short
*)new_bufp
->page
;
400 putpair((char *)np
, &key
, &val
);
402 new_bufp
->flags
|= BUF_MOD
;
407 __free_ovflpage(hashp
, last_bfp
);
412 * Add the given pair to the page
419 __addel(hashp
, bufp
, key
, val
)
422 const DBT
*key
, *val
;
424 register u_short
*bp
, *sop
;
427 bp
= (u_short
*)bufp
->page
;
429 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
431 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
432 /* This is the last page of a big key/data pair
433 and we need to add another page */
435 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
436 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
439 bp
= (u_short
*)bufp
->page
;
441 /* Try to squeeze key on this page */
442 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
443 squeeze_key(bp
, key
, val
);
446 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
449 bp
= (u_short
*)bufp
->page
;
452 if (PAIRFITS(bp
, key
, val
))
453 putpair(bufp
->page
, key
, val
);
456 bufp
= __add_ovflpage(hashp
, bufp
);
459 sop
= (u_short
*)bufp
->page
;
461 if (PAIRFITS(sop
, key
, val
))
462 putpair((char *)sop
, key
, val
);
464 if (__big_insert(hashp
, bufp
, key
, val
))
467 bufp
->flags
|= BUF_MOD
;
469 * If the average number of keys per bucket exceeds the fill factor,
474 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
475 return (__expand_table(hashp
));
486 __add_ovflpage(hashp
, bufp
)
490 register u_short
*sp
;
491 u_short ndx
, ovfl_num
;
495 sp
= (u_short
*)bufp
->page
;
497 /* Check if we are dynamically determining the fill factor */
498 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
499 hashp
->FFACTOR
= sp
[0] >> 1;
500 if (hashp
->FFACTOR
< MIN_FFACTOR
)
501 hashp
->FFACTOR
= MIN_FFACTOR
;
503 bufp
->flags
|= BUF_MOD
;
504 ovfl_num
= overflow_page(hashp
);
507 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
509 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
511 bufp
->ovfl
->flags
|= BUF_MOD
;
513 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
514 tmp1
, tmp2
, bufp
->ovfl
->addr
);
518 * Since a pair is allocated on a page only if there's room to add
519 * an overflow page, we know that the OVFL information will fit on
522 sp
[ndx
+ 4] = OFFSET(sp
);
523 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
524 sp
[ndx
+ 1] = ovfl_num
;
525 sp
[ndx
+ 2] = OVFLPAGE
;
527 #ifdef HASH_STATISTICS
535 * 0 indicates SUCCESS
536 * -1 indicates FAILURE
539 __get_page(hashp
, p
, bucket
, is_bucket
, is_disk
, is_bitmap
)
543 int is_bucket
, is_disk
, is_bitmap
;
545 register int fd
, page
, size
;
552 if ((fd
== -1) || !is_disk
) {
557 page
= BUCKET_TO_PAGE(bucket
);
559 page
= OADDR_TO_PAGE(bucket
);
560 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
561 ((rsize
= read(fd
, p
, size
)) == -1))
565 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
571 if (!is_bitmap
&& !bp
[0]) {
574 if (hashp
->LORDER
!= BYTE_ORDER
) {
578 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
579 for (i
= 0; i
< max
; i
++)
580 M_32_SWAP(((long *)p
)[i
]);
584 for (i
= 1; i
<= max
; i
++)
592 * Write page p to disk
599 __put_page(hashp
, p
, bucket
, is_bucket
, is_bitmap
)
603 int is_bucket
, is_bitmap
;
605 register int fd
, page
, size
;
609 if ((hashp
->fp
== -1) && open_temp(hashp
))
613 if (hashp
->LORDER
!= BYTE_ORDER
) {
618 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
619 for (i
= 0; i
< max
; i
++)
620 M_32_SWAP(((long *)p
)[i
]);
622 max
= ((u_short
*)p
)[0] + 2;
623 for (i
= 0; i
<= max
; i
++)
624 M_16_SWAP(((u_short
*)p
)[i
]);
628 page
= BUCKET_TO_PAGE(bucket
);
630 page
= OADDR_TO_PAGE(bucket
);
631 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
632 ((wsize
= write(fd
, p
, size
)) == -1))
642 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
644 * Initialize a new bitmap page. Bitmap pages are left in memory
645 * once they are read in.
648 __init_bitmap(hashp
, pnum
, nbits
, ndx
)
650 int pnum
, nbits
, ndx
;
653 int clearbytes
, clearints
;
655 if ((ip
= (u_long
*)malloc(hashp
->BSIZE
)) == NULL
)
658 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
659 clearbytes
= clearints
<< INT_TO_BYTE
;
660 (void)memset((char *)ip
, 0, clearbytes
);
661 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
662 hashp
->BSIZE
- clearbytes
);
663 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
665 hashp
->BITMAPS
[ndx
] = (u_short
)pnum
;
666 hashp
->mapp
[ndx
] = ip
;
674 register u_long i
, mask
;
677 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
689 register u_long
*freep
;
690 register int max_free
, offset
, splitnum
;
692 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
696 splitnum
= hashp
->OVFL_POINT
;
697 max_free
= hashp
->SPARES
[splitnum
];
699 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
700 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
702 /* Look through all the free maps to find the first free block */
703 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
704 for ( i
= first_page
; i
<= free_page
; i
++ ) {
705 if (!(freep
= (u_long
*)hashp
->mapp
[i
]) &&
706 !(freep
= fetch_bitmap(hashp
, i
)))
709 in_use_bits
= free_bit
;
711 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
713 if (i
== first_page
) {
714 bit
= hashp
->LAST_FREED
&
715 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
716 j
= bit
/ BITS_PER_MAP
;
717 bit
= bit
& ~(BITS_PER_MAP
- 1);
722 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
723 if (freep
[j
] != ALL_SET
)
727 /* No Free Page Found */
728 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
729 hashp
->SPARES
[splitnum
]++;
730 offset
= hashp
->SPARES
[splitnum
] -
731 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
733 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
734 if (offset
> SPLITMASK
) {
735 if (++splitnum
>= NCACHED
) {
736 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
739 hashp
->OVFL_POINT
= splitnum
;
740 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
741 hashp
->SPARES
[splitnum
-1]--;
745 /* Check if we need to allocate a new bitmap page */
746 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
748 if (free_page
>= NCACHED
) {
749 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
753 * This is tricky. The 1 indicates that you want the new page
754 * allocated with 1 clear bit. Actually, you are going to
755 * allocate 2 pages from this map. The first is going to be
756 * the map page, the second is the overflow page we were
757 * looking for. The init_bitmap routine automatically, sets
758 * the first bit of itself to indicate that the bitmap itself
759 * is in use. We would explicitly set the second bit, but
760 * don't have to if we tell init_bitmap not to leave it clear
761 * in the first place.
763 if (__init_bitmap(hashp
, (int)OADDR_OF(splitnum
, offset
),
766 hashp
->SPARES
[splitnum
]++;
771 if (offset
> SPLITMASK
) {
772 if (++splitnum
>= NCACHED
) {
773 (void)write(STDERR_FILENO
, OVMSG
,
777 hashp
->OVFL_POINT
= splitnum
;
778 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
779 hashp
->SPARES
[splitnum
-1]--;
784 * Free_bit addresses the last used bit. Bump it to address
785 * the first available bit.
788 SETBIT(freep
, free_bit
);
791 /* Calculate address of the new overflow page */
792 addr
= OADDR_OF(splitnum
, offset
);
794 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
795 addr
, free_bit
, free_page
);
800 bit
= bit
+ first_free(freep
[j
]);
807 * Bits are addressed starting with 0, but overflow pages are addressed
808 * beginning at 1. Bit is a bit addressnumber, so we need to increment
809 * it to convert it to a page number.
811 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
812 if (bit
>= hashp
->LAST_FREED
)
813 hashp
->LAST_FREED
= bit
- 1;
815 /* Calculate the split number for this page */
816 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
817 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
818 if (offset
>= SPLITMASK
)
819 return (NULL
); /* Out of overflow pages */
820 addr
= OADDR_OF(i
, offset
);
822 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
826 /* Allocate and return the overflow page */
831 * Mark this overflow page as free.
834 __free_ovflpage(hashp
, obufp
)
838 register u_short addr
;
840 int bit_address
, free_page
, free_bit
;
845 (void)fprintf(stderr
, "Freeing %d\n", addr
);
847 ndx
= (((u_short
)addr
) >> SPLITSHIFT
);
849 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
850 if (bit_address
< hashp
->LAST_FREED
)
851 hashp
->LAST_FREED
= bit_address
;
852 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
853 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
855 if (!(freep
= hashp
->mapp
[free_page
]))
856 freep
= fetch_bitmap(hashp
, free_page
);
859 * This had better never happen. It means we tried to read a bitmap
860 * that has already had overflow pages allocated off it, and we
861 * failed to read it from the file.
866 CLRBIT(freep
, free_bit
);
868 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
869 obufp
->addr
, free_bit
, free_page
);
871 __reclaim_buf(hashp
, obufp
);
884 static char namestr
[] = "_hashXXXXXX";
886 /* Block signals; make sure file goes away at process exit. */
887 (void)sigfillset(&set
);
888 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
889 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
890 (void)unlink(namestr
);
891 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
893 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
894 return (hashp
->fp
!= -1 ? 0 : -1);
898 * We have to know that the key will fit, but the last entry on the page is
899 * an overflow pair, so we need to shift things.
902 squeeze_key(sp
, key
, val
)
904 const DBT
*key
, *val
;
907 u_short free_space
, n
, off
, pageno
;
911 free_space
= FREESPACE(sp
);
917 memmove(p
+ off
, key
->data
, key
->size
);
920 memmove(p
+ off
, val
->data
, val
->size
);
923 sp
[n
+ 2] = OVFLPAGE
;
924 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
929 fetch_bitmap(hashp
, ndx
)
933 if (ndx
>= hashp
->nmaps
)
935 if ((hashp
->mapp
[ndx
] = (u_long
*)malloc(hashp
->BSIZE
)) == NULL
)
937 if (__get_page(hashp
,
938 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
939 free(hashp
->mapp
[ndx
]);
942 return (hashp
->mapp
[ndx
]);
953 (void)fprintf(stderr
, "%d ", addr
);
954 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
955 bp
= (short *)bufp
->page
;
956 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
957 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
958 oaddr
= bp
[bp
[0] - 1];
959 (void)fprintf(stderr
, "%d ", (int)oaddr
);
960 bufp
= __get_buf(hashp
, (int)oaddr
, bufp
, 0);
961 bp
= (short *)bufp
->page
;
963 (void)fprintf(stderr
, "\n");