]>
git.saurik.com Git - apple/libc.git/blob - db/hash/FreeBSD/hash_page.c
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/db/hash/hash_page.c,v 1.8 2002/03/21 22:46:26 obrien Exp $");
47 * Page manipulation for hashing package.
59 #include "namespace.h"
60 #include <sys/types.h>
72 #include "un-namespace.h"
79 static u_int32_t
*fetch_bitmap(HTAB
*, int);
80 static u_int32_t
first_free(u_int32_t
);
81 static int open_temp(HTAB
*);
82 static u_int16_t
overflow_page(HTAB
*);
83 static void putpair(char *, const DBT
*, const DBT
*);
84 static void squeeze_key(u_int16_t
*, const DBT
*, const DBT
*);
86 (HTAB
*, u_int32_t
, BUFHEAD
*, BUFHEAD
*, int, int);
88 #define PAGE_INIT(P) { \
89 ((u_int16_t *)(P))[0] = 0; \
90 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
91 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
95 * This is called AFTER we have verified that there is room on the page for
96 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
102 const DBT
*key
, *val
;
104 u_int16_t
*bp
, n
, off
;
108 /* Enter the key first. */
111 off
= OFFSET(bp
) - key
->size
;
112 memmove(p
+ off
, key
->data
, key
->size
);
117 memmove(p
+ off
, val
->data
, val
->size
);
120 /* Adjust page info. */
122 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_int16_t
));
132 __delpair(hashp
, bufp
, ndx
)
137 u_int16_t
*bp
, newoff
;
141 bp
= (u_int16_t
*)bufp
->page
;
144 if (bp
[ndx
+ 1] < REAL_KEY
)
145 return (__big_delete(hashp
, bufp
));
147 newoff
= bp
[ndx
- 1];
149 newoff
= hashp
->BSIZE
;
150 pairlen
= newoff
- bp
[ndx
+ 1];
152 if (ndx
!= (n
- 1)) {
153 /* Hard Case -- need to shuffle keys */
155 char *src
= bufp
->page
+ (int)OFFSET(bp
);
156 char *dst
= src
+ (int)pairlen
;
157 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
159 /* Now adjust the pointers */
160 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
161 if (bp
[i
+ 1] == OVFLPAGE
) {
163 bp
[i
- 1] = bp
[i
+ 1];
165 bp
[i
- 2] = bp
[i
] + pairlen
;
166 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
170 /* Finally adjust the page data */
171 bp
[n
] = OFFSET(bp
) + pairlen
;
172 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_int16_t
);
176 bufp
->flags
|= BUF_MOD
;
185 __split_page(hashp
, obucket
, nbucket
)
187 u_int32_t obucket
, nbucket
;
189 BUFHEAD
*new_bufp
, *old_bufp
;
194 u_int16_t copyto
, diff
, off
, moved
;
197 copyto
= (u_int16_t
)hashp
->BSIZE
;
198 off
= (u_int16_t
)hashp
->BSIZE
;
199 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
200 if (old_bufp
== NULL
)
202 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
203 if (new_bufp
== NULL
)
206 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
207 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
209 ino
= (u_int16_t
*)(op
= old_bufp
->page
);
214 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
215 if (ino
[n
+ 1] < REAL_KEY
) {
216 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
217 (int)copyto
, (int)moved
);
218 old_bufp
->flags
&= ~BUF_PIN
;
219 new_bufp
->flags
&= ~BUF_PIN
;
223 key
.data
= (u_char
*)op
+ ino
[n
];
224 key
.size
= off
- ino
[n
];
226 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
227 /* Don't switch page */
230 copyto
= ino
[n
+ 1] + diff
;
231 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
233 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
234 ino
[ndx
+ 1] = copyto
;
240 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
241 val
.size
= ino
[n
] - ino
[n
+ 1];
242 putpair(np
, &key
, &val
);
249 /* Now clean up the page */
251 FREESPACE(ino
) = copyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
252 OFFSET(ino
) = copyto
;
255 (void)fprintf(stderr
, "split %d/%d\n",
256 ((u_int16_t
*)np
)[0] / 2,
257 ((u_int16_t
*)op
)[0] / 2);
259 /* unpin both pages */
260 old_bufp
->flags
&= ~BUF_PIN
;
261 new_bufp
->flags
&= ~BUF_PIN
;
266 * Called when we encounter an overflow or big key/data page during split
267 * handling. This is special cased since we have to begin checking whether
268 * the key/data pairs fit on their respective pages and because we may need
269 * overflow pages for both the old and new pages.
271 * The first page might be a page with regular key/data pairs in which case
272 * we have a regular overflow condition and just need to go on to the next
273 * page or it might be a big key/data pair in which case we need to fix the
281 ugly_split(hashp
, obucket
, old_bufp
, new_bufp
, copyto
, moved
)
283 u_int32_t obucket
; /* Same as __split_page. */
284 BUFHEAD
*old_bufp
, *new_bufp
;
285 int copyto
; /* First byte on page which contains key/data values. */
286 int moved
; /* Number of pairs moved to new page. */
288 BUFHEAD
*bufp
; /* Buffer header for ino */
289 u_int16_t
*ino
; /* Page keys come off of */
290 u_int16_t
*np
; /* New page */
291 u_int16_t
*op
; /* Page keys go on to if they aren't moving */
293 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
296 u_int16_t n
, off
, ov_addr
, scopyto
;
297 char *cino
; /* Character value of ino */
300 ino
= (u_int16_t
*)old_bufp
->page
;
301 np
= (u_int16_t
*)new_bufp
->page
;
302 op
= (u_int16_t
*)old_bufp
->page
;
304 scopyto
= (u_int16_t
)copyto
; /* ANSI */
308 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
309 if (__big_split(hashp
, old_bufp
,
310 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
315 op
= (u_int16_t
*)old_bufp
->page
;
319 np
= (u_int16_t
*)new_bufp
->page
;
323 cino
= (char *)bufp
->page
;
324 ino
= (u_int16_t
*)cino
;
325 last_bfp
= ret
.nextp
;
326 } else if (ino
[n
+ 1] == OVFLPAGE
) {
329 * Fix up the old page -- the extra 2 are the fields
330 * which contained the overflow information.
332 ino
[0] -= (moved
+ 2);
334 scopyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
335 OFFSET(ino
) = scopyto
;
337 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
341 ino
= (u_int16_t
*)bufp
->page
;
343 scopyto
= hashp
->BSIZE
;
347 __free_ovflpage(hashp
, last_bfp
);
350 /* Move regular sized pairs of there are any */
352 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
354 key
.data
= (u_char
*)cino
+ ino
[n
];
355 key
.size
= off
- ino
[n
];
356 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
357 val
.size
= ino
[n
] - ino
[n
+ 1];
360 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
361 /* Keep on old page */
362 if (PAIRFITS(op
, (&key
), (&val
)))
363 putpair((char *)op
, &key
, &val
);
366 __add_ovflpage(hashp
, old_bufp
);
369 op
= (u_int16_t
*)old_bufp
->page
;
370 putpair((char *)op
, &key
, &val
);
372 old_bufp
->flags
|= BUF_MOD
;
374 /* Move to new page */
375 if (PAIRFITS(np
, (&key
), (&val
)))
376 putpair((char *)np
, &key
, &val
);
379 __add_ovflpage(hashp
, new_bufp
);
382 np
= (u_int16_t
*)new_bufp
->page
;
383 putpair((char *)np
, &key
, &val
);
385 new_bufp
->flags
|= BUF_MOD
;
390 __free_ovflpage(hashp
, last_bfp
);
395 * Add the given pair to the page
402 __addel(hashp
, bufp
, key
, val
)
405 const DBT
*key
, *val
;
410 bp
= (u_int16_t
*)bufp
->page
;
412 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
414 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
415 /* This is the last page of a big key/data pair
416 and we need to add another page */
418 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
419 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
422 bp
= (u_int16_t
*)bufp
->page
;
424 /* Try to squeeze key on this page */
425 if (FREESPACE(bp
) > PAIRSIZE(key
, val
)) {
426 squeeze_key(bp
, key
, val
);
429 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
432 bp
= (u_int16_t
*)bufp
->page
;
435 if (PAIRFITS(bp
, key
, val
))
436 putpair(bufp
->page
, key
, val
);
439 bufp
= __add_ovflpage(hashp
, bufp
);
442 sop
= (u_int16_t
*)bufp
->page
;
444 if (PAIRFITS(sop
, key
, val
))
445 putpair((char *)sop
, key
, val
);
447 if (__big_insert(hashp
, bufp
, key
, val
))
450 bufp
->flags
|= BUF_MOD
;
452 * If the average number of keys per bucket exceeds the fill factor,
457 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
458 return (__expand_table(hashp
));
469 __add_ovflpage(hashp
, bufp
)
474 u_int16_t ndx
, ovfl_num
;
478 sp
= (u_int16_t
*)bufp
->page
;
480 /* Check if we are dynamically determining the fill factor */
481 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
482 hashp
->FFACTOR
= sp
[0] >> 1;
483 if (hashp
->FFACTOR
< MIN_FFACTOR
)
484 hashp
->FFACTOR
= MIN_FFACTOR
;
486 bufp
->flags
|= BUF_MOD
;
487 ovfl_num
= overflow_page(hashp
);
490 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
492 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
494 bufp
->ovfl
->flags
|= BUF_MOD
;
496 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
497 tmp1
, tmp2
, bufp
->ovfl
->addr
);
501 * Since a pair is allocated on a page only if there's room to add
502 * an overflow page, we know that the OVFL information will fit on
505 sp
[ndx
+ 4] = OFFSET(sp
);
506 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
507 sp
[ndx
+ 1] = ovfl_num
;
508 sp
[ndx
+ 2] = OVFLPAGE
;
510 #ifdef HASH_STATISTICS
518 * 0 indicates SUCCESS
519 * -1 indicates FAILURE
522 __get_page(hashp
, p
, bucket
, is_bucket
, is_disk
, is_bitmap
)
526 int is_bucket
, is_disk
, is_bitmap
;
535 if ((fd
== -1) || !is_disk
) {
540 page
= BUCKET_TO_PAGE(bucket
);
542 page
= OADDR_TO_PAGE(bucket
);
543 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
544 ((rsize
= _read(fd
, p
, size
)) == -1))
548 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
554 if (!is_bitmap
&& !bp
[0]) {
557 if (hashp
->LORDER
!= BYTE_ORDER
) {
561 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
562 for (i
= 0; i
< max
; i
++)
563 M_32_SWAP(((int *)p
)[i
]);
567 for (i
= 1; i
<= max
; i
++)
575 * Write page p to disk
582 __put_page(hashp
, p
, bucket
, is_bucket
, is_bitmap
)
586 int is_bucket
, is_bitmap
;
592 if ((hashp
->fp
== -1) && open_temp(hashp
))
596 if (hashp
->LORDER
!= BYTE_ORDER
) {
601 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
602 for (i
= 0; i
< max
; i
++)
603 M_32_SWAP(((int *)p
)[i
]);
605 max
= ((u_int16_t
*)p
)[0] + 2;
606 for (i
= 0; i
<= max
; i
++)
607 M_16_SWAP(((u_int16_t
*)p
)[i
]);
611 page
= BUCKET_TO_PAGE(bucket
);
613 page
= OADDR_TO_PAGE(bucket
);
614 if ((lseek(fd
, (off_t
)page
<< hashp
->BSHIFT
, SEEK_SET
) == -1) ||
615 ((wsize
= _write(fd
, p
, size
)) == -1))
625 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
627 * Initialize a new bitmap page. Bitmap pages are left in memory
628 * once they are read in.
631 __ibitmap(hashp
, pnum
, nbits
, ndx
)
633 int pnum
, nbits
, ndx
;
636 int clearbytes
, clearints
;
638 if ((ip
= (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
641 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
642 clearbytes
= clearints
<< INT_TO_BYTE
;
643 (void)memset((char *)ip
, 0, clearbytes
);
644 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
645 hashp
->BSIZE
- clearbytes
);
646 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
648 hashp
->BITMAPS
[ndx
] = (u_int16_t
)pnum
;
649 hashp
->mapp
[ndx
] = ip
;
660 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
673 int max_free
, offset
, splitnum
;
675 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
679 splitnum
= hashp
->OVFL_POINT
;
680 max_free
= hashp
->SPARES
[splitnum
];
682 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
683 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
685 /* Look through all the free maps to find the first free block */
686 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
687 for ( i
= first_page
; i
<= free_page
; i
++ ) {
688 if (!(freep
= (u_int32_t
*)hashp
->mapp
[i
]) &&
689 !(freep
= fetch_bitmap(hashp
, i
)))
692 in_use_bits
= free_bit
;
694 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
696 if (i
== first_page
) {
697 bit
= hashp
->LAST_FREED
&
698 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
699 j
= bit
/ BITS_PER_MAP
;
700 bit
= bit
& ~(BITS_PER_MAP
- 1);
705 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
706 if (freep
[j
] != ALL_SET
)
710 /* No Free Page Found */
711 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
712 hashp
->SPARES
[splitnum
]++;
713 offset
= hashp
->SPARES
[splitnum
] -
714 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
716 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
717 if (offset
> SPLITMASK
) {
718 if (++splitnum
>= NCACHED
) {
719 (void)_write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
722 hashp
->OVFL_POINT
= splitnum
;
723 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
724 hashp
->SPARES
[splitnum
-1]--;
728 /* Check if we need to allocate a new bitmap page */
729 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
731 if (free_page
>= NCACHED
) {
732 (void)_write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
736 * This is tricky. The 1 indicates that you want the new page
737 * allocated with 1 clear bit. Actually, you are going to
738 * allocate 2 pages from this map. The first is going to be
739 * the map page, the second is the overflow page we were
740 * looking for. The init_bitmap routine automatically, sets
741 * the first bit of itself to indicate that the bitmap itself
742 * is in use. We would explicitly set the second bit, but
743 * don't have to if we tell init_bitmap not to leave it clear
744 * in the first place.
747 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
749 hashp
->SPARES
[splitnum
]++;
754 if (offset
> SPLITMASK
) {
755 if (++splitnum
>= NCACHED
) {
756 (void)_write(STDERR_FILENO
, OVMSG
,
760 hashp
->OVFL_POINT
= splitnum
;
761 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
762 hashp
->SPARES
[splitnum
-1]--;
767 * Free_bit addresses the last used bit. Bump it to address
768 * the first available bit.
771 SETBIT(freep
, free_bit
);
774 /* Calculate address of the new overflow page */
775 addr
= OADDR_OF(splitnum
, offset
);
777 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
778 addr
, free_bit
, free_page
);
783 bit
= bit
+ first_free(freep
[j
]);
790 * Bits are addressed starting with 0, but overflow pages are addressed
791 * beginning at 1. Bit is a bit addressnumber, so we need to increment
792 * it to convert it to a page number.
794 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
795 if (bit
>= hashp
->LAST_FREED
)
796 hashp
->LAST_FREED
= bit
- 1;
798 /* Calculate the split number for this page */
799 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
800 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
801 if (offset
>= SPLITMASK
)
802 return (0); /* Out of overflow pages */
803 addr
= OADDR_OF(i
, offset
);
805 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
809 /* Allocate and return the overflow page */
814 * Mark this overflow page as free.
817 __free_ovflpage(hashp
, obufp
)
823 int bit_address
, free_page
, free_bit
;
828 (void)fprintf(stderr
, "Freeing %d\n", addr
);
830 ndx
= (((u_int16_t
)addr
) >> SPLITSHIFT
);
832 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
833 if (bit_address
< hashp
->LAST_FREED
)
834 hashp
->LAST_FREED
= bit_address
;
835 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
836 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
838 if (!(freep
= hashp
->mapp
[free_page
]))
839 freep
= fetch_bitmap(hashp
, free_page
);
842 * This had better never happen. It means we tried to read a bitmap
843 * that has already had overflow pages allocated off it, and we
844 * failed to read it from the file.
849 CLRBIT(freep
, free_bit
);
851 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
852 obufp
->addr
, free_bit
, free_page
);
854 __reclaim_buf(hashp
, obufp
);
867 static char namestr
[] = "_hashXXXXXX";
869 /* Block signals; make sure file goes away at process exit. */
870 (void)sigfillset(&set
);
871 (void)_sigprocmask(SIG_BLOCK
, &set
, &oset
);
872 if ((hashp
->fp
= mkstemp(namestr
)) != -1) {
873 (void)unlink(namestr
);
874 (void)_fcntl(hashp
->fp
, F_SETFD
, 1);
876 (void)_sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
877 return (hashp
->fp
!= -1 ? 0 : -1);
881 * We have to know that the key will fit, but the last entry on the page is
882 * an overflow pair, so we need to shift things.
885 squeeze_key(sp
, key
, val
)
887 const DBT
*key
, *val
;
890 u_int16_t free_space
, n
, off
, pageno
;
894 free_space
= FREESPACE(sp
);
900 memmove(p
+ off
, key
->data
, key
->size
);
903 memmove(p
+ off
, val
->data
, val
->size
);
906 sp
[n
+ 2] = OVFLPAGE
;
907 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
912 fetch_bitmap(hashp
, ndx
)
916 if (ndx
>= hashp
->nmaps
)
918 if ((hashp
->mapp
[ndx
] = (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
920 if (__get_page(hashp
,
921 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
922 free(hashp
->mapp
[ndx
]);
925 return (hashp
->mapp
[ndx
]);
936 (void)fprintf(stderr
, "%d ", addr
);
937 bufp
= __get_buf(hashp
, addr
, NULL
, 0);
938 bp
= (short *)bufp
->page
;
939 while (bp
[0] && ((bp
[bp
[0]] == OVFLPAGE
) ||
940 ((bp
[0] > 2) && bp
[2] < REAL_KEY
))) {
941 oaddr
= bp
[bp
[0] - 1];
942 (void)fprintf(stderr
, "%d ", (int)oaddr
);
943 bufp
= __get_buf(hashp
, (int)oaddr
, bufp
, 0);
944 bp
= (short *)bufp
->page
;
946 (void)fprintf(stderr
, "\n");