]>
git.saurik.com Git - apple/libc.git/blob - db/hash/hash_page.c
f045255455e2df6a8dfa4616644ae2159b3051c7
2 * Copyright (c) 2003 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, 1994
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
61 #if defined(LIBC_SCCS) && !defined(lint)
62 static char sccsid
[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
63 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
70 * Page manipulation for hashing package.
82 #include <sys/types.h>
100 static u_int32_t
*fetch_bitmap(HTAB
*, int);
101 static u_int32_t
first_free(u_int32_t
);
102 static int open_temp(HTAB
*);
103 static u_int16_t
overflow_page(HTAB
*);
104 static void putpair(char *, const DBT
*, const DBT
*);
105 static void squeeze_key(u_int16_t
*, const DBT
*, const DBT
*);
106 static int ugly_split
107 (HTAB
*, u_int32_t
, BUFHEAD
*, BUFHEAD
*, int, int);
109 #define PAGE_INIT(P) { \
110 ((u_int16_t *)(P))[0] = 0; \
111 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
112 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
116 * This is called AFTER we have verified that there is room on the page for
117 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
123 const DBT
*key
, *val
;
125 u_int16_t
*bp
, n
, off
;
129 /* Enter the key first. */
132 off
= OFFSET(bp
) - key
->size
;
133 memmove(p
+ off
, key
->data
, key
->size
);
138 memmove(p
+ off
, val
->data
, val
->size
);
141 /* Adjust page info. */
143 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_int16_t
));
153 __delpair(hashp
, bufp
, ndx
)
158 u_int16_t
*bp
, newoff
;
162 bp
= (u_int16_t
*)bufp
->page
;
165 if (bp
[ndx
+ 1] < REAL_KEY
)
166 return (__big_delete(hashp
, bufp
));
168 newoff
= bp
[ndx
- 1];
170 newoff
= hashp
->BSIZE
;
171 pairlen
= newoff
- bp
[ndx
+ 1];
173 if (ndx
!= (n
- 1)) {
174 /* Hard Case -- need to shuffle keys */
176 char *src
= bufp
->page
+ (int)OFFSET(bp
);
177 char *dst
= src
+ (int)pairlen
;
178 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
180 /* Now adjust the pointers */
181 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
182 if (bp
[i
+ 1] == OVFLPAGE
) {
184 bp
[i
- 1] = bp
[i
+ 1];
186 bp
[i
- 2] = bp
[i
] + pairlen
;
187 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
191 /* Finally adjust the page data */
192 bp
[n
] = OFFSET(bp
) + pairlen
;
193 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_int16_t
);
197 bufp
->flags
|= BUF_MOD
;
206 __split_page(hashp
, obucket
, nbucket
)
208 u_int32_t obucket
, nbucket
;
210 BUFHEAD
*new_bufp
, *old_bufp
;
215 u_int16_t copyto
, diff
, off
, moved
;
218 copyto
= (u_int16_t
)hashp
->BSIZE
;
219 off
= (u_int16_t
)hashp
->BSIZE
;
220 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
221 if (old_bufp
== NULL
)
223 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
224 if (new_bufp
== NULL
)
227 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
228 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
230 ino
= (u_int16_t
*)(op
= old_bufp
->page
);
235 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
236 if (ino
[n
+ 1] < REAL_KEY
) {
237 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
238 (int)copyto
, (int)moved
);
239 old_bufp
->flags
&= ~BUF_PIN
;
240 new_bufp
->flags
&= ~BUF_PIN
;
244 key
.data
= (u_char
*)op
+ ino
[n
];
245 key
.size
= off
- ino
[n
];
247 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
248 /* Don't switch page */
251 copyto
= ino
[n
+ 1] + diff
;
252 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
254 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
255 ino
[ndx
+ 1] = copyto
;
261 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
262 val
.size
= ino
[n
] - ino
[n
+ 1];
263 putpair(np
, &key
, &val
);
270 /* Now clean up the page */
272 FREESPACE(ino
) = copyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
273 OFFSET(ino
) = copyto
;
276 (void)fprintf(stderr
, "split %d/%d\n",
277 ((u_int16_t
*)np
)[0] / 2,
278 ((u_int16_t
*)op
)[0] / 2);
280 /* unpin both pages */
281 old_bufp
->flags
&= ~BUF_PIN
;
282 new_bufp
->flags
&= ~BUF_PIN
;
287 * Called when we encounter an overflow or big key/data page during split
288 * handling. This is special cased since we have to begin checking whether
289 * the key/data pairs fit on their respective pages and because we may need
290 * overflow pages for both the old and new pages.
292 * The first page might be a page with regular key/data pairs in which case
293 * we have a regular overflow condition and just need to go on to the next
294 * page or it might be a big key/data pair in which case we need to fix the
302 ugly_split(hashp
, obucket
, old_bufp
, new_bufp
, copyto
, moved
)
304 u_int32_t obucket
; /* Same as __split_page. */
305 BUFHEAD
*old_bufp
, *new_bufp
;
306 int copyto
; /* First byte on page which contains key/data values. */
307 int moved
; /* Number of pairs moved to new page. */
309 BUFHEAD
*bufp
; /* Buffer header for ino */
310 u_int16_t
*ino
; /* Page keys come off of */
311 u_int16_t
*np
; /* New page */
312 u_int16_t
*op
; /* Page keys go on to if they aren't moving */
314 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
317 u_int16_t n
, off
, ov_addr
, scopyto
;
318 char *cino
; /* Character value of ino */
321 ino
= (u_int16_t
*)old_bufp
->page
;
322 np
= (u_int16_t
*)new_bufp
->page
;
323 op
= (u_int16_t
*)old_bufp
->page
;
325 scopyto
= (u_int16_t
)copyto
; /* ANSI */
329 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
330 if (__big_split(hashp
, old_bufp
,
331 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
336 op
= (u_int16_t
*)old_bufp
->page
;
340 np
= (u_int16_t
*)new_bufp
->page
;
344 cino
= (char *)bufp
->page
;
345 ino
= (u_int16_t
*)cino
;
346 last_bfp
= ret
.nextp
;
347 } else if (ino
[n
+ 1] == OVFLPAGE
) {
350 * Fix up the old page -- the extra 2 are the fields
351 * which contained the overflow information.
353 ino
[0] -= (moved
+ 2);
355 scopyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
356 OFFSET(ino
) = scopyto
;
358 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
362 ino
= (u_int16_t
*)bufp
->page
;
364 scopyto
= hashp
->BSIZE
;
368 __free_ovflpage(hashp
, last_bfp
);
371 /* Move regular sized pairs of there are any */
373 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
375 key
.data
= (u_char
*)cino
+ ino
[n
];
376 key
.size
= off
- ino
[n
];
377 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
378 val
.size
= ino
[n
] - ino
[n
+ 1];
381 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
382 /* Keep on old page */
383 if (PAIRFITS(op
, (&key
), (&val
)))
384 putpair((char *)op
, &key
, &val
);
387 __add_ovflpage(hashp
, old_bufp
);
390 op
= (u_int16_t
*)old_bufp
->page
;
391 putpair((char *)op
, &key
, &val
);
393 old_bufp
->flags
|= BUF_MOD
;
395 /* Move to new page */
396 if (PAIRFITS(np
, (&key
), (&val
)))
397 putpair((char *)np
, &key
, &val
);
400 __add_ovflpage(hashp
, new_bufp
);
403 np
= (u_int16_t
*)new_bufp
->page
;
404 putpair((char *)np
, &key
, &val
);
406 new_bufp
->flags
|= BUF_MOD
;
411 __free_ovflpage(hashp
, last_bfp
);
416 * Add the given pair to the page
423 __addel(hashp
, bufp
, key
, val
)
426 const DBT
*key
, *val
;
431 bp
= (u_int16_t
*)bufp
->page
;
433 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
435 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
436 /* This is the last page of a big key/data pair
437 and we need to add another page */
439 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
440 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
443 bp
= (u_int16_t
*)bufp
->page
;
445 /* Try to squeeze key on this page */
446 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
447 squeeze_key(bp
, key
, val
);
450 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
453 bp
= (u_int16_t
*)bufp
->page
;
456 if (PAIRFITS(bp
, key
, val
))
457 putpair(bufp
->page
, key
, val
);
460 bufp
= __add_ovflpage(hashp
, bufp
);
463 sop
= (u_int16_t
*)bufp
->page
;
465 if (PAIRFITS(sop
, key
, val
))
466 putpair((char *)sop
, key
, val
);
468 if (__big_insert(hashp
, bufp
, key
, val
))
471 bufp
->flags
|= BUF_MOD
;
473 * If the average number of keys per bucket exceeds the fill factor,
478 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
479 return (__expand_table(hashp
));
490 __add_ovflpage(hashp
, bufp
)
495 u_int16_t ndx
, ovfl_num
;
499 sp
= (u_int16_t
*)bufp
->page
;
501 /* Check if we are dynamically determining the fill factor */
502 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
503 hashp
->FFACTOR
= sp
[0] >> 1;
504 if (hashp
->FFACTOR
< MIN_FFACTOR
)
505 hashp
->FFACTOR
= MIN_FFACTOR
;
507 bufp
->flags
|= BUF_MOD
;
508 ovfl_num
= overflow_page(hashp
);
511 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
513 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
515 bufp
->ovfl
->flags
|= BUF_MOD
;
517 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
518 tmp1
, tmp2
, bufp
->ovfl
->addr
);
522 * Since a pair is allocated on a page only if there's room to add
523 * an overflow page, we know that the OVFL information will fit on
526 sp
[ndx
+ 4] = OFFSET(sp
);
527 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
528 sp
[ndx
+ 1] = ovfl_num
;
529 sp
[ndx
+ 2] = OVFLPAGE
;
531 #ifdef HASH_STATISTICS
539 * 0 indicates SUCCESS
540 * -1 indicates FAILURE
543 __get_page(hashp
, p
, bucket
, is_bucket
, is_disk
, is_bitmap
)
547 int is_bucket
, is_disk
, is_bitmap
;
556 if ((fd
== -1) || !is_disk
) {
561 page
= BUCKET_TO_PAGE(bucket
);
563 page
= OADDR_TO_PAGE(bucket
);
564 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
565 ((rsize
= read(fd
, p
, size
)) == -1))
569 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
575 if (!is_bitmap
&& !bp
[0]) {
578 if (hashp
->LORDER
!= BYTE_ORDER
) {
582 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
583 for (i
= 0; i
< max
; i
++)
584 M_32_SWAP(((int *)p
)[i
]);
588 for (i
= 1; i
<= max
; i
++)
596 * Write page p to disk
603 __put_page(hashp
, p
, bucket
, is_bucket
, is_bitmap
)
607 int is_bucket
, is_bitmap
;
613 if ((hashp
->fp
== -1) && open_temp(hashp
))
617 if (hashp
->LORDER
!= BYTE_ORDER
) {
622 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
623 for (i
= 0; i
< max
; i
++)
624 M_32_SWAP(((int *)p
)[i
]);
626 max
= ((u_int16_t
*)p
)[0] + 2;
627 for (i
= 0; i
<= max
; i
++)
628 M_16_SWAP(((u_int16_t
*)p
)[i
]);
632 page
= BUCKET_TO_PAGE(bucket
);
634 page
= OADDR_TO_PAGE(bucket
);
635 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
636 ((wsize
= write(fd
, p
, size
)) == -1))
646 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
648 * Initialize a new bitmap page. Bitmap pages are left in memory
649 * once they are read in.
652 __ibitmap(hashp
, pnum
, nbits
, ndx
)
654 int pnum
, nbits
, ndx
;
657 int clearbytes
, clearints
;
659 if ((ip
= (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
662 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
663 clearbytes
= clearints
<< INT_TO_BYTE
;
664 (void)memset((char *)ip
, 0, clearbytes
);
665 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
666 hashp
->BSIZE
- clearbytes
);
667 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
669 hashp
->BITMAPS
[ndx
] = (u_int16_t
)pnum
;
670 hashp
->mapp
[ndx
] = ip
;
681 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
694 int max_free
, offset
, splitnum
;
696 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
700 splitnum
= hashp
->OVFL_POINT
;
701 max_free
= hashp
->SPARES
[splitnum
];
703 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
704 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
706 /* Look through all the free maps to find the first free block */
707 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
708 for ( i
= first_page
; i
<= free_page
; i
++ ) {
709 if (!(freep
= (u_int32_t
*)hashp
->mapp
[i
]) &&
710 !(freep
= fetch_bitmap(hashp
, i
)))
713 in_use_bits
= free_bit
;
715 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
717 if (i
== first_page
) {
718 bit
= hashp
->LAST_FREED
&
719 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
720 j
= bit
/ BITS_PER_MAP
;
721 bit
= bit
& ~(BITS_PER_MAP
- 1);
726 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
727 if (freep
[j
] != ALL_SET
)
731 /* No Free Page Found */
732 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
733 hashp
->SPARES
[splitnum
]++;
734 offset
= hashp
->SPARES
[splitnum
] -
735 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
737 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
738 if (offset
> SPLITMASK
) {
739 if (++splitnum
>= NCACHED
) {
740 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
743 hashp
->OVFL_POINT
= splitnum
;
744 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
745 hashp
->SPARES
[splitnum
-1]--;
749 /* Check if we need to allocate a new bitmap page */
750 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
752 if (free_page
>= NCACHED
) {
753 (void)write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
757 * This is tricky. The 1 indicates that you want the new page
758 * allocated with 1 clear bit. Actually, you are going to
759 * allocate 2 pages from this map. The first is going to be
760 * the map page, the second is the overflow page we were
761 * looking for. The init_bitmap routine automatically, sets
762 * the first bit of itself to indicate that the bitmap itself
763 * is in use. We would explicitly set the second bit, but
764 * don't have to if we tell init_bitmap not to leave it clear
765 * in the first place.
768 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
770 hashp
->SPARES
[splitnum
]++;
775 if (offset
> SPLITMASK
) {
776 if (++splitnum
>= NCACHED
) {
777 (void)write(STDERR_FILENO
, OVMSG
,
781 hashp
->OVFL_POINT
= splitnum
;
782 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
783 hashp
->SPARES
[splitnum
-1]--;
788 * Free_bit addresses the last used bit. Bump it to address
789 * the first available bit.
792 SETBIT(freep
, free_bit
);
795 /* Calculate address of the new overflow page */
796 addr
= OADDR_OF(splitnum
, offset
);
798 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
799 addr
, free_bit
, free_page
);
804 bit
= bit
+ first_free(freep
[j
]);
811 * Bits are addressed starting with 0, but overflow pages are addressed
812 * beginning at 1. Bit is a bit addressnumber, so we need to increment
813 * it to convert it to a page number.
815 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
816 if (bit
>= hashp
->LAST_FREED
)
817 hashp
->LAST_FREED
= bit
- 1;
819 /* Calculate the split number for this page */
820 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
821 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
822 if (offset
>= SPLITMASK
)
823 return (0); /* Out of overflow pages */
824 addr
= OADDR_OF(i
, offset
);
826 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
830 /* Allocate and return the overflow page */
835 * Mark this overflow page as free.
838 __free_ovflpage(hashp
, obufp
)
844 int bit_address
, free_page
, free_bit
;
849 (void)fprintf(stderr
, "Freeing %d\n", addr
);
851 ndx
= (((u_int16_t
)addr
) >> SPLITSHIFT
);
853 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
854 if (bit_address
< hashp
->LAST_FREED
)
855 hashp
->LAST_FREED
= bit_address
;
856 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
857 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
859 if (!(freep
= hashp
->mapp
[free_page
]))
860 freep
= fetch_bitmap(hashp
, free_page
);
863 * This had better never happen. It means we tried to read a bitmap
864 * that has already had overflow pages allocated off it, and we
865 * failed to read it from the file.
870 CLRBIT(freep
, free_bit
);
872 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
873 obufp
->addr
, free_bit
, free_page
);
875 __reclaim_buf(hashp
, obufp
);
888 static char namestr
[] = "_hashXXXXXX";
890 /* Block signals; make sure file goes away at process exit. */
891 (void)sigfillset(&set
);
892 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
893 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
894 (void)unlink(namestr
);
895 (void)fcntl(hashp
->fp
, F_SETFD
, 1);
897 (void)sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
898 return (hashp
->fp
!= -1 ? 0 : -1);
902 * We have to know that the key will fit, but the last entry on the page is
903 * an overflow pair, so we need to shift things.
906 squeeze_key(sp
, key
, val
)
908 const DBT
*key
, *val
;
911 u_int16_t free_space
, n
, off
, pageno
;
915 free_space
= FREESPACE(sp
);
921 memmove(p
+ off
, key
->data
, key
->size
);
924 memmove(p
+ off
, val
->data
, val
->size
);
927 sp
[n
+ 2] = OVFLPAGE
;
928 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
933 fetch_bitmap(hashp
, ndx
)
937 if (ndx
>= hashp
->nmaps
)
939 if ((hashp
->mapp
[ndx
] = (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
941 if (__get_page(hashp
,
942 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
943 free(hashp
->mapp
[ndx
]);
946 return (hashp
->mapp
[ndx
]);
957 (void)fprintf(stderr
, "%d ", addr
);
958 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
959 bp
= (short *)bufp
->page
;
960 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
961 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
962 oaddr
= bp
[bp
[0] - 1];
963 (void)fprintf(stderr
, "%d ", (int)oaddr
);
964 bufp
= __get_buf(hashp
, (int)oaddr
, bufp
, 0);
965 bp
= (short *)bufp
->page
;
967 (void)fprintf(stderr
, "\n");