]>
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/db/hash/hash_page.c,v 1.16 2009/03/28 06:30:43 delphij Exp $");
43 * Page manipulation for hashing package.
55 #include "namespace.h"
56 #include <sys/param.h>
68 #include "un-namespace.h"
73 #include "hash_extern.h"
75 static u_int32_t
*fetch_bitmap(HTAB
*, int);
76 static u_int32_t
first_free(u_int32_t
);
77 static int open_temp(HTAB
*);
78 static u_int16_t
overflow_page(HTAB
*);
79 static void putpair(char *, const DBT
*, const DBT
*);
80 static void squeeze_key(u_int16_t
*, const DBT
*, const DBT
*);
81 static int ugly_split(HTAB
*, u_int32_t
, BUFHEAD
*, BUFHEAD
*, int, int);
83 #define PAGE_INIT(P) { \
84 ((u_int16_t *)(P))[0] = 0; \
85 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
86 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
90 * This is called AFTER we have verified that there is room on the page for
91 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
95 putpair(char *p
, const DBT
*key
, const DBT
*val
)
97 u_int16_t
*bp
, n
, off
;
101 /* Enter the key first. */
104 off
= OFFSET(bp
) - key
->size
;
105 memmove(p
+ off
, key
->data
, key
->size
);
110 memmove(p
+ off
, val
->data
, val
->size
);
113 /* Adjust page info. */
115 bp
[n
+ 1] = off
- ((n
+ 3) * sizeof(u_int16_t
));
125 __delpair(HTAB
*hashp
, BUFHEAD
*bufp
, int ndx
)
127 u_int16_t
*bp
, newoff
, pairlen
;
130 bp
= (u_int16_t
*)bufp
->page
;
133 if (bp
[ndx
+ 1] < REAL_KEY
)
134 return (__big_delete(hashp
, bufp
));
136 newoff
= bp
[ndx
- 1];
138 newoff
= hashp
->BSIZE
;
139 pairlen
= newoff
- bp
[ndx
+ 1];
141 if (ndx
!= (n
- 1)) {
142 /* Hard Case -- need to shuffle keys */
144 char *src
= bufp
->page
+ (int)OFFSET(bp
);
145 char *dst
= src
+ (int)pairlen
;
146 memmove(dst
, src
, bp
[ndx
+ 1] - OFFSET(bp
));
148 /* Now adjust the pointers */
149 for (i
= ndx
+ 2; i
<= n
; i
+= 2) {
150 if (bp
[i
+ 1] == OVFLPAGE
) {
152 bp
[i
- 1] = bp
[i
+ 1];
154 bp
[i
- 2] = bp
[i
] + pairlen
;
155 bp
[i
- 1] = bp
[i
+ 1] + pairlen
;
158 if (ndx
== hashp
->cndx
) {
160 * We just removed pair we were "pointing" to.
161 * By moving back the cndx we ensure subsequent
162 * hash_seq() calls won't skip over any entries.
167 /* Finally adjust the page data */
168 bp
[n
] = OFFSET(bp
) + pairlen
;
169 bp
[n
- 1] = bp
[n
+ 1] + pairlen
+ 2 * sizeof(u_int16_t
);
173 bufp
->flags
|= BUF_MOD
;
182 __split_page(HTAB
*hashp
, u_int32_t obucket
, u_int32_t nbucket
)
184 BUFHEAD
*new_bufp
, *old_bufp
;
189 u_int16_t copyto
, diff
, off
, moved
;
192 copyto
= (u_int16_t
)hashp
->BSIZE
;
193 off
= (u_int16_t
)hashp
->BSIZE
;
194 old_bufp
= __get_buf(hashp
, obucket
, NULL
, 0);
195 if (old_bufp
== NULL
)
197 new_bufp
= __get_buf(hashp
, nbucket
, NULL
, 0);
198 if (new_bufp
== NULL
)
201 old_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
202 new_bufp
->flags
|= (BUF_MOD
| BUF_PIN
);
204 ino
= (u_int16_t
*)(op
= old_bufp
->page
);
209 for (n
= 1, ndx
= 1; n
< ino
[0]; n
+= 2) {
210 if (ino
[n
+ 1] < REAL_KEY
) {
211 retval
= ugly_split(hashp
, obucket
, old_bufp
, new_bufp
,
212 (int)copyto
, (int)moved
);
213 old_bufp
->flags
&= ~BUF_PIN
;
214 new_bufp
->flags
&= ~BUF_PIN
;
218 key
.data
= (u_char
*)op
+ ino
[n
];
219 key
.size
= off
- ino
[n
];
221 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
222 /* Don't switch page */
225 copyto
= ino
[n
+ 1] + diff
;
226 memmove(op
+ copyto
, op
+ ino
[n
+ 1],
228 ino
[ndx
] = copyto
+ ino
[n
] - ino
[n
+ 1];
229 ino
[ndx
+ 1] = copyto
;
235 val
.data
= (u_char
*)op
+ ino
[n
+ 1];
236 val
.size
= ino
[n
] - ino
[n
+ 1];
237 putpair(np
, &key
, &val
);
244 /* Now clean up the page */
246 FREESPACE(ino
) = copyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
247 OFFSET(ino
) = copyto
;
250 (void)fprintf(stderr
, "split %d/%d\n",
251 ((u_int16_t
*)np
)[0] / 2,
252 ((u_int16_t
*)op
)[0] / 2);
254 /* unpin both pages */
255 old_bufp
->flags
&= ~BUF_PIN
;
256 new_bufp
->flags
&= ~BUF_PIN
;
261 * Called when we encounter an overflow or big key/data page during split
262 * handling. This is special cased since we have to begin checking whether
263 * the key/data pairs fit on their respective pages and because we may need
264 * overflow pages for both the old and new pages.
266 * The first page might be a page with regular key/data pairs in which case
267 * we have a regular overflow condition and just need to go on to the next
268 * page or it might be a big key/data pair in which case we need to fix the
276 ugly_split(HTAB
*hashp
,
277 u_int32_t obucket
, /* Same as __split_page. */
280 int copyto
, /* First byte on page which contains key/data values. */
281 int moved
) /* Number of pairs moved to new page. */
283 BUFHEAD
*bufp
; /* Buffer header for ino */
284 u_int16_t
*ino
; /* Page keys come off of */
285 u_int16_t
*np
; /* New page */
286 u_int16_t
*op
; /* Page keys go on to if they aren't moving */
288 BUFHEAD
*last_bfp
; /* Last buf header OVFL needing to be freed */
291 u_int16_t n
, off
, ov_addr
, scopyto
;
292 char *cino
; /* Character value of ino */
295 ino
= (u_int16_t
*)old_bufp
->page
;
296 np
= (u_int16_t
*)new_bufp
->page
;
297 op
= (u_int16_t
*)old_bufp
->page
;
299 scopyto
= (u_int16_t
)copyto
; /* ANSI */
303 if (ino
[2] < REAL_KEY
&& ino
[2] != OVFLPAGE
) {
304 if (__big_split(hashp
, old_bufp
,
305 new_bufp
, bufp
, bufp
->addr
, obucket
, &ret
))
310 op
= (u_int16_t
*)old_bufp
->page
;
314 np
= (u_int16_t
*)new_bufp
->page
;
318 cino
= (char *)bufp
->page
;
319 ino
= (u_int16_t
*)cino
;
320 last_bfp
= ret
.nextp
;
321 } else if (ino
[n
+ 1] == OVFLPAGE
) {
324 * Fix up the old page -- the extra 2 are the fields
325 * which contained the overflow information.
327 ino
[0] -= (moved
+ 2);
329 scopyto
- sizeof(u_int16_t
) * (ino
[0] + 3);
330 OFFSET(ino
) = scopyto
;
332 bufp
= __get_buf(hashp
, ov_addr
, bufp
, 0);
336 ino
= (u_int16_t
*)bufp
->page
;
338 scopyto
= hashp
->BSIZE
;
342 __free_ovflpage(hashp
, last_bfp
);
345 /* Move regular sized pairs of there are any */
347 for (n
= 1; (n
< ino
[0]) && (ino
[n
+ 1] >= REAL_KEY
); n
+= 2) {
349 key
.data
= (u_char
*)cino
+ ino
[n
];
350 key
.size
= off
- ino
[n
];
351 val
.data
= (u_char
*)cino
+ ino
[n
+ 1];
352 val
.size
= ino
[n
] - ino
[n
+ 1];
355 if (__call_hash(hashp
, key
.data
, key
.size
) == obucket
) {
356 /* Keep on old page */
357 if (PAIRFITS(op
, (&key
), (&val
)))
358 putpair((char *)op
, &key
, &val
);
361 __add_ovflpage(hashp
, old_bufp
);
364 op
= (u_int16_t
*)old_bufp
->page
;
365 putpair((char *)op
, &key
, &val
);
367 old_bufp
->flags
|= BUF_MOD
;
369 /* Move to new page */
370 if (PAIRFITS(np
, (&key
), (&val
)))
371 putpair((char *)np
, &key
, &val
);
374 __add_ovflpage(hashp
, new_bufp
);
377 np
= (u_int16_t
*)new_bufp
->page
;
378 putpair((char *)np
, &key
, &val
);
380 new_bufp
->flags
|= BUF_MOD
;
385 __free_ovflpage(hashp
, last_bfp
);
390 * Add the given pair to the page
397 __addel(HTAB
*hashp
, BUFHEAD
*bufp
, const DBT
*key
, const DBT
*val
)
402 bp
= (u_int16_t
*)bufp
->page
;
404 while (bp
[0] && (bp
[2] < REAL_KEY
|| bp
[bp
[0]] < REAL_KEY
))
406 if (bp
[2] == FULL_KEY_DATA
&& bp
[0] == 2)
407 /* This is the last page of a big key/data pair
408 and we need to add another page */
410 else if (bp
[2] < REAL_KEY
&& bp
[bp
[0]] != OVFLPAGE
) {
411 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
414 bp
= (u_int16_t
*)bufp
->page
;
415 } else if (bp
[bp
[0]] != OVFLPAGE
) {
416 /* Short key/data pairs, no more pages */
419 /* Try to squeeze key on this page */
420 if (bp
[2] >= REAL_KEY
&&
421 FREESPACE(bp
) >= PAIRSIZE(key
, val
)) {
422 squeeze_key(bp
, key
, val
);
425 bufp
= __get_buf(hashp
, bp
[bp
[0] - 1], bufp
, 0);
428 bp
= (u_int16_t
*)bufp
->page
;
432 if (PAIRFITS(bp
, key
, val
))
433 putpair(bufp
->page
, key
, val
);
436 bufp
= __add_ovflpage(hashp
, bufp
);
439 sop
= (u_int16_t
*)bufp
->page
;
441 if (PAIRFITS(sop
, key
, val
))
442 putpair((char *)sop
, key
, val
);
444 if (__big_insert(hashp
, bufp
, key
, val
))
448 bufp
->flags
|= BUF_MOD
;
450 * If the average number of keys per bucket exceeds the fill factor,
455 (hashp
->NKEYS
/ (hashp
->MAX_BUCKET
+ 1) > hashp
->FFACTOR
))
456 return (__expand_table(hashp
));
467 __add_ovflpage(HTAB
*hashp
, BUFHEAD
*bufp
)
469 u_int16_t
*sp
, ndx
, ovfl_num
;
473 sp
= (u_int16_t
*)bufp
->page
;
475 /* Check if we are dynamically determining the fill factor */
476 if (hashp
->FFACTOR
== DEF_FFACTOR
) {
477 hashp
->FFACTOR
= sp
[0] >> 1;
478 if (hashp
->FFACTOR
< MIN_FFACTOR
)
479 hashp
->FFACTOR
= MIN_FFACTOR
;
481 bufp
->flags
|= BUF_MOD
;
482 ovfl_num
= overflow_page(hashp
);
485 tmp2
= bufp
->ovfl
? bufp
->ovfl
->addr
: 0;
487 if (!ovfl_num
|| !(bufp
->ovfl
= __get_buf(hashp
, ovfl_num
, bufp
, 1)))
489 bufp
->ovfl
->flags
|= BUF_MOD
;
491 (void)fprintf(stderr
, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
492 tmp1
, tmp2
, bufp
->ovfl
->addr
);
496 * Since a pair is allocated on a page only if there's room to add
497 * an overflow page, we know that the OVFL information will fit on
500 sp
[ndx
+ 4] = OFFSET(sp
);
501 sp
[ndx
+ 3] = FREESPACE(sp
) - OVFLSIZE
;
502 sp
[ndx
+ 1] = ovfl_num
;
503 sp
[ndx
+ 2] = OVFLPAGE
;
505 #ifdef HASH_STATISTICS
513 * 0 indicates SUCCESS
514 * -1 indicates FAILURE
517 __get_page(HTAB
*hashp
, char *p
, u_int32_t bucket
, int is_bucket
, int is_disk
,
520 int fd
, page
, size
, rsize
;
526 if ((fd
== -1) || !is_disk
) {
531 page
= BUCKET_TO_PAGE(bucket
);
533 page
= OADDR_TO_PAGE(bucket
);
534 if ((rsize
= pread(fd
, p
, size
, (off_t
)page
<< hashp
->BSHIFT
)) == -1)
538 bp
[0] = 0; /* We hit the EOF, so initialize a new page */
544 if (!is_bitmap
&& !bp
[0]) {
547 if (hashp
->LORDER
!= BYTE_ORDER
) {
551 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
552 for (i
= 0; i
< max
; i
++)
553 M_32_SWAP(((int *)p
)[i
]);
557 for (i
= 1; i
<= max
; i
++)
565 * Write page p to disk
572 __put_page(HTAB
*hashp
, char *p
, u_int32_t bucket
, int is_bucket
, int is_bitmap
)
574 int fd
, page
, size
, wsize
, max
;
577 if ((hashp
->fp
== -1) && open_temp(hashp
))
581 if (hashp
->LORDER
!= BYTE_ORDER
) {
585 max
= hashp
->BSIZE
>> 2; /* divide by 4 */
586 for (i
= 0; i
< max
; i
++)
587 M_32_SWAP(((int *)p
)[i
]);
589 max
= ((u_int16_t
*)p
)[0] + 2;
590 for (i
= 0; i
<= max
; i
++)
591 M_16_SWAP(((u_int16_t
*)p
)[i
]);
595 page
= BUCKET_TO_PAGE(bucket
);
597 page
= OADDR_TO_PAGE(bucket
);
598 if ((wsize
= pwrite(fd
, p
, size
, (off_t
)page
<< hashp
->BSHIFT
)) == -1)
605 /* 4485533 - reswap the in-memory copy */
606 if (hashp
->LORDER
!= BYTE_ORDER
) {
610 for (i
= 0; i
< max
; i
++)
611 M_32_SWAP(((int *)p
)[i
]);
613 for (i
= 0; i
<= max
; i
++)
614 M_16_SWAP(((u_int16_t
*)p
)[i
]);
620 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
622 * Initialize a new bitmap page. Bitmap pages are left in memory
623 * once they are read in.
626 __ibitmap(HTAB
*hashp
, int pnum
, int nbits
, int ndx
)
629 int clearbytes
, clearints
;
631 if ((ip
= (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
634 clearints
= ((nbits
- 1) >> INT_BYTE_SHIFT
) + 1;
635 clearbytes
= clearints
<< INT_TO_BYTE
;
636 (void)memset((char *)ip
, 0, clearbytes
);
637 (void)memset(((char *)ip
) + clearbytes
, 0xFF,
638 hashp
->BSIZE
- clearbytes
);
639 ip
[clearints
- 1] = ALL_SET
<< (nbits
& BYTE_MASK
);
641 hashp
->BITMAPS
[ndx
] = (u_int16_t
)pnum
;
642 hashp
->mapp
[ndx
] = ip
;
647 first_free(u_int32_t map
)
652 for (i
= 0; i
< BITS_PER_MAP
; i
++) {
661 overflow_page(HTAB
*hashp
)
664 int max_free
, offset
, splitnum
;
666 int bit
, first_page
, free_bit
, free_page
, i
, in_use_bits
, j
;
670 splitnum
= hashp
->OVFL_POINT
;
671 max_free
= hashp
->SPARES
[splitnum
];
673 free_page
= (max_free
- 1) >> (hashp
->BSHIFT
+ BYTE_SHIFT
);
674 free_bit
= (max_free
- 1) & ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
676 /* Look through all the free maps to find the first free block */
677 first_page
= hashp
->LAST_FREED
>>(hashp
->BSHIFT
+ BYTE_SHIFT
);
678 for ( i
= first_page
; i
<= free_page
; i
++ ) {
679 if (!(freep
= (u_int32_t
*)hashp
->mapp
[i
]) &&
680 !(freep
= fetch_bitmap(hashp
, i
)))
683 in_use_bits
= free_bit
;
685 in_use_bits
= (hashp
->BSIZE
<< BYTE_SHIFT
) - 1;
687 if (i
== first_page
) {
688 bit
= hashp
->LAST_FREED
&
689 ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
690 j
= bit
/ BITS_PER_MAP
;
691 bit
= bit
& ~(BITS_PER_MAP
- 1);
696 for (; bit
<= in_use_bits
; j
++, bit
+= BITS_PER_MAP
)
697 if (freep
[j
] != ALL_SET
)
701 /* No Free Page Found */
702 hashp
->LAST_FREED
= hashp
->SPARES
[splitnum
];
703 hashp
->SPARES
[splitnum
]++;
704 offset
= hashp
->SPARES
[splitnum
] -
705 (splitnum
? hashp
->SPARES
[splitnum
- 1] : 0);
707 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
708 if (offset
> SPLITMASK
) {
709 if (++splitnum
>= NCACHED
) {
710 (void)_write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
714 hashp
->OVFL_POINT
= splitnum
;
715 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
716 hashp
->SPARES
[splitnum
-1]--;
720 /* Check if we need to allocate a new bitmap page */
721 if (free_bit
== (hashp
->BSIZE
<< BYTE_SHIFT
) - 1) {
723 if (free_page
>= NCACHED
) {
724 (void)_write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
729 * This is tricky. The 1 indicates that you want the new page
730 * allocated with 1 clear bit. Actually, you are going to
731 * allocate 2 pages from this map. The first is going to be
732 * the map page, the second is the overflow page we were
733 * looking for. The init_bitmap routine automatically, sets
734 * the first bit of itself to indicate that the bitmap itself
735 * is in use. We would explicitly set the second bit, but
736 * don't have to if we tell init_bitmap not to leave it clear
737 * in the first place.
740 (int)OADDR_OF(splitnum
, offset
), 1, free_page
))
742 hashp
->SPARES
[splitnum
]++;
747 if (offset
> SPLITMASK
) {
748 if (++splitnum
>= NCACHED
) {
749 (void)_write(STDERR_FILENO
, OVMSG
,
754 hashp
->OVFL_POINT
= splitnum
;
755 hashp
->SPARES
[splitnum
] = hashp
->SPARES
[splitnum
-1];
756 hashp
->SPARES
[splitnum
-1]--;
761 * Free_bit addresses the last used bit. Bump it to address
762 * the first available bit.
765 SETBIT(freep
, free_bit
);
768 /* Calculate address of the new overflow page */
769 addr
= OADDR_OF(splitnum
, offset
);
771 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
772 addr
, free_bit
, free_page
);
777 bit
= bit
+ first_free(freep
[j
]);
784 * Bits are addressed starting with 0, but overflow pages are addressed
785 * beginning at 1. Bit is a bit addressnumber, so we need to increment
786 * it to convert it to a page number.
788 bit
= 1 + bit
+ (i
* (hashp
->BSIZE
<< BYTE_SHIFT
));
789 if (bit
>= hashp
->LAST_FREED
)
790 hashp
->LAST_FREED
= bit
- 1;
792 /* Calculate the split number for this page */
793 for (i
= 0; (i
< splitnum
) && (bit
> hashp
->SPARES
[i
]); i
++);
794 offset
= (i
? bit
- hashp
->SPARES
[i
- 1] : bit
);
795 if (offset
>= SPLITMASK
) {
796 (void)_write(STDERR_FILENO
, OVMSG
, sizeof(OVMSG
) - 1);
798 return (0); /* Out of overflow pages */
800 addr
= OADDR_OF(i
, offset
);
802 (void)fprintf(stderr
, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
806 /* Allocate and return the overflow page */
811 * Mark this overflow page as free.
814 __free_ovflpage(HTAB
*hashp
, BUFHEAD
*obufp
)
818 int bit_address
, free_page
, free_bit
;
823 (void)fprintf(stderr
, "Freeing %d\n", addr
);
825 ndx
= (((u_int16_t
)addr
) >> SPLITSHIFT
);
827 (ndx
? hashp
->SPARES
[ndx
- 1] : 0) + (addr
& SPLITMASK
) - 1;
828 if (bit_address
< hashp
->LAST_FREED
)
829 hashp
->LAST_FREED
= bit_address
;
830 free_page
= (bit_address
>> (hashp
->BSHIFT
+ BYTE_SHIFT
));
831 free_bit
= bit_address
& ((hashp
->BSIZE
<< BYTE_SHIFT
) - 1);
833 if (!(freep
= hashp
->mapp
[free_page
]))
834 freep
= fetch_bitmap(hashp
, free_page
);
837 * This had better never happen. It means we tried to read a bitmap
838 * that has already had overflow pages allocated off it, and we
839 * failed to read it from the file.
844 CLRBIT(freep
, free_bit
);
846 (void)fprintf(stderr
, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
847 obufp
->addr
, free_bit
, free_page
);
849 __reclaim_buf(hashp
, obufp
);
858 open_temp(HTAB
*hashp
)
863 char path
[MAXPATHLEN
];
865 if (issetugid() == 0)
866 envtmp
= getenv("TMPDIR");
868 sizeof(path
), "%s/_hash.XXXXXX", envtmp
? envtmp
: "/tmp");
869 if (len
< 0 || len
>= sizeof(path
)) {
870 errno
= ENAMETOOLONG
;
874 /* Block signals; make sure file goes away at process exit. */
875 (void)sigfillset(&set
);
876 (void)_sigprocmask(SIG_BLOCK
, &set
, &oset
);
877 if ((hashp
->fp
= mkstemp(path
)) != -1) {
879 (void)_fcntl(hashp
->fp
, F_SETFD
, 1);
881 (void)_sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
882 return (hashp
->fp
!= -1 ? 0 : -1);
886 * We have to know that the key will fit, but the last entry on the page is
887 * an overflow pair, so we need to shift things.
890 squeeze_key(u_int16_t
*sp
, const DBT
*key
, const DBT
*val
)
893 u_int16_t free_space
, n
, off
, pageno
;
897 free_space
= FREESPACE(sp
);
903 memmove(p
+ off
, key
->data
, key
->size
);
906 memmove(p
+ off
, val
->data
, val
->size
);
909 sp
[n
+ 2] = OVFLPAGE
;
910 FREESPACE(sp
) = free_space
- PAIRSIZE(key
, val
);
915 fetch_bitmap(HTAB
*hashp
, int ndx
)
917 if (ndx
>= hashp
->nmaps
)
919 if ((hashp
->mapp
[ndx
] = (u_int32_t
*)malloc(hashp
->BSIZE
)) == NULL
)
921 if (__get_page(hashp
,
922 (char *)hashp
->mapp
[ndx
], hashp
->BITMAPS
[ndx
], 0, 1, 1)) {
923 free(hashp
->mapp
[ndx
]);
926 return (hashp
->mapp
[ndx
]);
931 print_chain(int addr
)
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");