Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / search / hash_page.c
blobc073f49dfc36d950d0be3c31bbf6a6066c592da5
1 /*-
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
6 * Margo Seltzer.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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. 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
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
40 * PACKAGE: hashing
42 * DESCRIPTION:
43 * Page manipulation for hashing package.
45 * ROUTINES:
47 * External
48 * __get_page
49 * __add_ovflpage
50 * Internal
51 * overflow_page
52 * open_temp
55 #include <sys/types.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #ifdef DEBUG
65 #include <assert.h>
66 #endif
68 #include "db_local.h"
69 #include "hash.h"
70 #include "page.h"
71 #include "extern.h"
73 static __uint32_t *fetch_bitmap(HTAB *, int);
74 static __uint32_t first_free(__uint32_t);
75 static int open_temp(HTAB *);
76 static __uint16_t overflow_page(HTAB *);
77 static void putpair(char *, const DBT *, const DBT *);
78 static void squeeze_key(__uint16_t *, const DBT *, const DBT *);
79 static int ugly_split
80 (HTAB *, __uint32_t, BUFHEAD *, BUFHEAD *, int, int);
82 #define PAGE_INIT(P) { \
83 ((__uint16_t *)(P))[0] = 0; \
84 ((__uint16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(__uint16_t); \
85 ((__uint16_t *)(P))[2] = hashp->BSIZE; \
89 * This is called AFTER we have verified that there is room on the page for
90 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
91 * stuff on.
93 static void
94 putpair(
95 char *p,
96 const DBT *key,
97 const DBT *val
100 __uint16_t *bp, n, off;
102 bp = (__uint16_t *)p;
104 /* Enter the key first. */
105 n = bp[0];
107 off = OFFSET(bp) - key->size;
108 memmove(p + off, key->data, key->size);
109 bp[++n] = off;
111 /* Now the data. */
112 off -= val->size;
113 memmove(p + off, val->data, val->size);
114 bp[++n] = off;
116 /* Adjust page info. */
117 bp[0] = n;
118 bp[n + 1] = off - ((n + 3) * sizeof(__uint16_t));
119 bp[n + 2] = off;
123 * Returns:
124 * 0 OK
125 * -1 error
127 extern int
128 __delpair(
129 HTAB *hashp,
130 BUFHEAD *bufp,
131 int ndx
134 __uint16_t *bp, newoff;
135 int n;
136 __uint16_t pairlen;
138 bp = (__uint16_t *)bufp->page;
139 n = bp[0];
141 if (bp[ndx + 1] < REAL_KEY)
142 return (__big_delete(hashp, bufp));
143 if (ndx != 1)
144 newoff = bp[ndx - 1];
145 else
146 newoff = hashp->BSIZE;
147 pairlen = newoff - bp[ndx + 1];
149 if (ndx != (n - 1)) {
150 /* Hard Case -- need to shuffle keys */
151 int i;
152 char *src = bufp->page + (int)OFFSET(bp);
153 char *dst = src + (int)pairlen;
154 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
156 /* Now adjust the pointers */
157 for (i = ndx + 2; i <= n; i += 2) {
158 if (bp[i + 1] == OVFLPAGE) {
159 bp[i - 2] = bp[i];
160 bp[i - 1] = bp[i + 1];
161 } else {
162 bp[i - 2] = bp[i] + pairlen;
163 bp[i - 1] = bp[i + 1] + pairlen;
167 /* Finally adjust the page data */
168 bp[n] = OFFSET(bp) + pairlen;
169 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(__uint16_t);
170 bp[0] = n - 2;
171 hashp->NKEYS--;
173 bufp->flags |= BUF_MOD;
174 return (0);
177 * Returns:
178 * 0 ==> OK
179 * -1 ==> Error
181 extern int
182 __split_page(
183 HTAB *hashp,
184 __uint32_t obucket,
185 __uint32_t nbucket
188 BUFHEAD *new_bufp, *old_bufp;
189 __uint16_t *ino;
190 char *np;
191 DBT key, val;
192 int n, ndx, retval;
193 __uint16_t copyto, diff, off, moved;
194 char *op;
196 copyto = (__uint16_t)hashp->BSIZE;
197 off = (__uint16_t)hashp->BSIZE;
198 old_bufp = __get_buf(hashp, obucket, NULL, 0);
199 if (old_bufp == NULL)
200 return (-1);
201 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
202 if (new_bufp == NULL)
203 return (-1);
205 old_bufp->flags |= (BUF_MOD | BUF_PIN);
206 new_bufp->flags |= (BUF_MOD | BUF_PIN);
208 ino = (__uint16_t *)(op = old_bufp->page);
209 np = new_bufp->page;
211 moved = 0;
213 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
214 if (ino[n + 1] < REAL_KEY) {
215 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
216 (int)copyto, (int)moved);
217 old_bufp->flags &= ~BUF_PIN;
218 new_bufp->flags &= ~BUF_PIN;
219 return (retval);
222 key.data = (u_char *)op + ino[n];
223 key.size = off - ino[n];
225 if (__call_hash(hashp, key.data, key.size) == obucket) {
226 /* Don't switch page */
227 diff = copyto - off;
228 if (diff) {
229 copyto = ino[n + 1] + diff;
230 memmove(op + copyto, op + ino[n + 1],
231 off - ino[n + 1]);
232 ino[ndx] = copyto + ino[n] - ino[n + 1];
233 ino[ndx + 1] = copyto;
234 } else
235 copyto = ino[n + 1];
236 ndx += 2;
237 } else {
238 /* Switch page */
239 val.data = (u_char *)op + ino[n + 1];
240 val.size = ino[n] - ino[n + 1];
241 putpair(np, &key, &val);
242 moved += 2;
245 off = ino[n + 1];
248 /* Now clean up the page */
249 ino[0] -= moved;
250 FREESPACE(ino) = copyto - sizeof(__uint16_t) * (ino[0] + 3);
251 OFFSET(ino) = copyto;
253 #ifdef DEBUG3
254 (void)fprintf(stderr, "split %d/%d\n",
255 ((__uint16_t *)np)[0] / 2,
256 ((__uint16_t *)op)[0] / 2);
257 #endif
258 /* unpin both pages */
259 old_bufp->flags &= ~BUF_PIN;
260 new_bufp->flags &= ~BUF_PIN;
261 return (0);
265 * Called when we encounter an overflow or big key/data page during split
266 * handling. This is special cased since we have to begin checking whether
267 * the key/data pairs fit on their respective pages and because we may need
268 * overflow pages for both the old and new pages.
270 * The first page might be a page with regular key/data pairs in which case
271 * we have a regular overflow condition and just need to go on to the next
272 * page or it might be a big key/data pair in which case we need to fix the
273 * big key/data pair.
275 * Returns:
276 * 0 ==> success
277 * -1 ==> failure
279 static int
280 ugly_split(
281 HTAB *hashp,
282 __uint32_t obucket, /* Same as __split_page. */
283 BUFHEAD *old_bufp,
284 BUFHEAD *new_bufp,
285 int copyto, /* First byte on page which contains key/data values. */
286 int moved /* Number of pairs moved to new page. */
289 BUFHEAD *bufp; /* Buffer header for ino */
290 __uint16_t *ino; /* Page keys come off of */
291 __uint16_t *np; /* New page */
292 __uint16_t *op; /* Page keys go on to if they aren't moving */
294 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
295 DBT key, val;
296 SPLIT_RETURN ret;
297 __uint16_t n, off, ov_addr, scopyto;
298 char *cino; /* Character value of ino */
300 bufp = old_bufp;
301 ino = (__uint16_t *)old_bufp->page;
302 np = (__uint16_t *)new_bufp->page;
303 op = (__uint16_t *)old_bufp->page;
304 last_bfp = NULL;
305 scopyto = (__uint16_t)copyto; /* ANSI */
307 n = ino[0] - 1;
308 while (n < ino[0]) {
309 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
310 if (__big_split(hashp, old_bufp,
311 new_bufp, bufp, bufp->addr, obucket, &ret))
312 return (-1);
313 old_bufp = ret.oldp;
314 if (!old_bufp)
315 return (-1);
316 op = (__uint16_t *)old_bufp->page;
317 new_bufp = ret.newp;
318 if (!new_bufp)
319 return (-1);
320 np = (__uint16_t *)new_bufp->page;
321 bufp = ret.nextp;
322 if (!bufp)
323 return (0);
324 cino = (char *)bufp->page;
325 ino = (__uint16_t *)cino;
326 last_bfp = ret.nextp;
327 } else if (ino[n + 1] == OVFLPAGE) {
328 ov_addr = ino[n];
330 * Fix up the old page -- the extra 2 are the fields
331 * which contained the overflow information.
333 ino[0] -= (moved + 2);
334 FREESPACE(ino) =
335 scopyto - sizeof(__uint16_t) * (ino[0] + 3);
336 OFFSET(ino) = scopyto;
338 bufp = __get_buf(hashp, ov_addr, bufp, 0);
339 if (!bufp)
340 return (-1);
342 ino = (__uint16_t *)bufp->page;
343 n = 1;
344 scopyto = hashp->BSIZE;
345 moved = 0;
347 if (last_bfp)
348 __free_ovflpage(hashp, last_bfp);
349 last_bfp = bufp;
351 /* Move regular sized pairs of there are any */
352 off = hashp->BSIZE;
353 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
354 cino = (char *)ino;
355 key.data = (u_char *)cino + ino[n];
356 key.size = off - ino[n];
357 val.data = (u_char *)cino + ino[n + 1];
358 val.size = ino[n] - ino[n + 1];
359 off = ino[n + 1];
361 if (__call_hash(hashp, key.data, key.size) == obucket) {
362 /* Keep on old page */
363 if (PAIRFITS(op, (&key), (&val)))
364 putpair((char *)op, &key, &val);
365 else {
366 old_bufp =
367 __add_ovflpage(hashp, old_bufp);
368 if (!old_bufp)
369 return (-1);
370 op = (__uint16_t *)old_bufp->page;
371 putpair((char *)op, &key, &val);
373 old_bufp->flags |= BUF_MOD;
374 } else {
375 /* Move to new page */
376 if (PAIRFITS(np, (&key), (&val)))
377 putpair((char *)np, &key, &val);
378 else {
379 new_bufp =
380 __add_ovflpage(hashp, new_bufp);
381 if (!new_bufp)
382 return (-1);
383 np = (__uint16_t *)new_bufp->page;
384 putpair((char *)np, &key, &val);
386 new_bufp->flags |= BUF_MOD;
390 if (last_bfp)
391 __free_ovflpage(hashp, last_bfp);
392 return (0);
396 * Add the given pair to the page
398 * Returns:
399 * 0 ==> OK
400 * 1 ==> failure
402 extern int
403 __addel(
404 HTAB *hashp,
405 BUFHEAD *bufp,
406 const DBT *key,
407 const DBT *val
410 __uint16_t *bp, *sop;
411 int do_expand;
413 bp = (__uint16_t *)bufp->page;
414 do_expand = 0;
415 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
416 /* Exception case */
417 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
418 /* This is the last page of a big key/data pair
419 and we need to add another page */
420 break;
421 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
422 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
423 if (!bufp)
424 return (-1);
425 bp = (__uint16_t *)bufp->page;
426 } else
427 /* Try to squeeze key on this page */
428 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
429 squeeze_key(bp, key, val);
430 return (0);
431 } else {
432 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
433 if (!bufp)
434 return (-1);
435 bp = (__uint16_t *)bufp->page;
438 if (PAIRFITS(bp, key, val))
439 putpair(bufp->page, key, val);
440 else {
441 do_expand = 1;
442 bufp = __add_ovflpage(hashp, bufp);
443 if (!bufp)
444 return (-1);
445 sop = (__uint16_t *)bufp->page;
447 if (PAIRFITS(sop, key, val))
448 putpair((char *)sop, key, val);
449 else
450 if (__big_insert(hashp, bufp, key, val))
451 return (-1);
453 bufp->flags |= BUF_MOD;
455 * If the average number of keys per bucket exceeds the fill factor,
456 * expand the table.
458 hashp->NKEYS++;
459 if (do_expand ||
460 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
461 return (__expand_table(hashp));
462 return (0);
467 * Returns:
468 * pointer on success
469 * NULL on error
471 extern BUFHEAD *
472 __add_ovflpage(
473 HTAB *hashp,
474 BUFHEAD *bufp
477 __uint16_t *sp;
478 __uint16_t ndx, ovfl_num;
479 #ifdef DEBUG1
480 int tmp1, tmp2;
481 #endif
482 sp = (__uint16_t *)bufp->page;
484 /* Check if we are dynamically determining the fill factor */
485 if (hashp->FFACTOR == DEF_FFACTOR) {
486 hashp->FFACTOR = sp[0] >> 1;
487 if (hashp->FFACTOR < MIN_FFACTOR)
488 hashp->FFACTOR = MIN_FFACTOR;
490 bufp->flags |= BUF_MOD;
491 ovfl_num = overflow_page(hashp);
492 #ifdef DEBUG1
493 tmp1 = bufp->addr;
494 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
495 #endif
496 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
497 return (NULL);
498 bufp->ovfl->flags |= BUF_MOD;
499 #ifdef DEBUG1
500 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
501 tmp1, tmp2, bufp->ovfl->addr);
502 #endif
503 ndx = sp[0];
505 * Since a pair is allocated on a page only if there's room to add
506 * an overflow page, we know that the OVFL information will fit on
507 * the page.
509 sp[ndx + 4] = OFFSET(sp);
510 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
511 sp[ndx + 1] = ovfl_num;
512 sp[ndx + 2] = OVFLPAGE;
513 sp[0] = ndx + 2;
514 #ifdef HASH_STATISTICS
515 hash_overflows++;
516 #endif
517 return (bufp->ovfl);
521 * Returns:
522 * 0 indicates SUCCESS
523 * -1 indicates FAILURE
525 extern int
526 __get_page(
527 HTAB *hashp,
528 char *p,
529 __uint32_t bucket,
530 int is_bucket,
531 int is_disk,
532 int is_bitmap
535 int fd, page, size;
536 int rsize;
537 __uint16_t *bp;
539 fd = hashp->fp;
540 size = hashp->BSIZE;
542 if ((fd == -1) || !is_disk) {
543 PAGE_INIT(p);
544 return (0);
546 if (is_bucket)
547 page = BUCKET_TO_PAGE(bucket);
548 else
549 page = OADDR_TO_PAGE(bucket);
550 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
551 ((rsize = read(fd, p, size)) == -1))
552 return (-1);
553 bp = (__uint16_t *)p;
554 if (!rsize)
555 bp[0] = 0; /* We hit the EOF, so initialize a new page */
556 else
557 if (rsize != size) {
558 errno = EFTYPE;
559 return (-1);
561 if (!is_bitmap && !bp[0]) {
562 PAGE_INIT(p);
563 } else
564 if (hashp->LORDER != DB_BYTE_ORDER) {
565 int i, max;
567 if (is_bitmap) {
568 max = hashp->BSIZE >> 2; /* divide by 4 */
569 for (i = 0; i < max; i++)
570 M_32_SWAP(((int *)p)[i]);
571 } else {
572 M_16_SWAP(bp[0]);
573 max = bp[0] + 2;
574 for (i = 1; i <= max; i++)
575 M_16_SWAP(bp[i]);
578 return (0);
582 * Write page p to disk
584 * Returns:
585 * 0 ==> OK
586 * -1 ==>failure
588 extern int
589 __put_page(
590 HTAB *hashp,
591 char *p,
592 __uint32_t bucket,
593 int is_bucket,
594 int is_bitmap
597 int fd, page, size;
598 int wsize;
600 size = hashp->BSIZE;
601 if ((hashp->fp == -1) && open_temp(hashp))
602 return (-1);
603 fd = hashp->fp;
605 if (hashp->LORDER != DB_BYTE_ORDER) {
606 int i;
607 int max;
609 if (is_bitmap) {
610 max = hashp->BSIZE >> 2; /* divide by 4 */
611 for (i = 0; i < max; i++)
612 M_32_SWAP(((int *)p)[i]);
613 } else {
614 max = ((__uint16_t *)p)[0] + 2;
615 for (i = 0; i <= max; i++)
616 M_16_SWAP(((__uint16_t *)p)[i]);
619 if (is_bucket)
620 page = BUCKET_TO_PAGE(bucket);
621 else
622 page = OADDR_TO_PAGE(bucket);
623 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
624 ((wsize = write(fd, p, size)) == -1))
625 /* Errno is set */
626 return (-1);
627 if (wsize != size) {
628 errno = EFTYPE;
629 return (-1);
631 return (0);
634 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
636 * Initialize a new bitmap page. Bitmap pages are left in memory
637 * once they are read in.
639 extern int
640 __ibitmap(
641 HTAB *hashp,
642 int pnum,
643 int nbits,
644 int ndx
647 __uint32_t *ip;
648 int clearbytes, clearints;
650 if ((ip = (__uint32_t *)malloc(hashp->BSIZE)) == NULL)
651 return (1);
652 hashp->nmaps++;
653 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
654 clearbytes = clearints << INT_TO_BYTE;
655 (void)memset((char *)ip, 0, clearbytes);
656 (void)memset(((char *)ip) + clearbytes, 0xFF,
657 hashp->BSIZE - clearbytes);
658 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
659 SETBIT(ip, 0);
660 hashp->BITMAPS[ndx] = (__uint16_t)pnum;
661 hashp->mapp[ndx] = ip;
662 return (0);
665 static __uint32_t
666 first_free(
667 __uint32_t map
670 __uint32_t i, mask;
672 mask = 0x1;
673 for (i = 0; i < BITS_PER_MAP; i++) {
674 if (!(mask & map))
675 return (i);
676 mask = mask << 1;
678 return (i);
681 static __uint16_t
682 overflow_page(
683 HTAB *hashp
686 __uint32_t *freep = NULL;
687 int max_free, offset, splitnum;
688 __uint16_t addr;
689 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
690 #ifdef DEBUG2
691 int tmp1, tmp2;
692 #endif
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 = (__uint32_t *)hashp->mapp[i]) &&
703 !(freep = fetch_bitmap(hashp, i)))
704 return (0);
705 if (i == free_page)
706 in_use_bits = free_bit;
707 else
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);
715 } else {
716 bit = 0;
717 j = 0;
719 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
720 if (freep[j] != ALL_SET)
721 goto found;
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);
734 return (0);
736 hashp->OVFL_POINT = splitnum;
737 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
738 hashp->SPARES[splitnum-1]--;
739 offset = 1;
742 /* Check if we need to allocate a new bitmap page */
743 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
744 free_page++;
745 if (free_page >= NCACHED) {
746 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
747 return (0);
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 (__ibitmap(hashp,
761 (int)OADDR_OF(splitnum, offset), 1, free_page))
762 return (0);
763 hashp->SPARES[splitnum]++;
764 #ifdef DEBUG2
765 free_bit = 2;
766 #endif
767 offset++;
768 if (offset > SPLITMASK) {
769 if (++splitnum >= NCACHED) {
770 (void)write(STDERR_FILENO, OVMSG,
771 sizeof(OVMSG) - 1);
772 return (0);
774 hashp->OVFL_POINT = splitnum;
775 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
776 hashp->SPARES[splitnum-1]--;
777 offset = 0;
779 } else {
781 * Free_bit addresses the last used bit. Bump it to address
782 * the first available bit.
784 free_bit++;
785 SETBIT(freep, free_bit);
788 /* Calculate address of the new overflow page */
789 addr = OADDR_OF(splitnum, offset);
790 #ifdef DEBUG2
791 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
792 addr, free_bit, free_page);
793 #endif
794 return (addr);
796 found:
797 bit = bit + first_free(freep[j]);
798 SETBIT(freep, bit);
799 #ifdef DEBUG2
800 tmp1 = bit;
801 tmp2 = i;
802 #endif
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 (0); /* Out of overflow pages */
817 addr = OADDR_OF(i, offset);
818 #ifdef DEBUG2
819 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
820 addr, tmp1, tmp2);
821 #endif
823 /* Allocate and return the overflow page */
824 return (addr);
828 * Mark this overflow page as free.
830 extern void
831 __free_ovflpage(
832 HTAB *hashp,
833 BUFHEAD *obufp
836 __uint16_t addr;
837 __uint32_t *freep;
838 int bit_address, free_page, free_bit;
839 __uint16_t ndx;
841 addr = obufp->addr;
842 #ifdef DEBUG1
843 (void)fprintf(stderr, "Freeing %d\n", addr);
844 #endif
845 ndx = (((__uint16_t)addr) >> SPLITSHIFT);
846 bit_address =
847 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
848 if (bit_address < hashp->LAST_FREED)
849 hashp->LAST_FREED = bit_address;
850 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
851 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
853 if (!(freep = hashp->mapp[free_page]))
854 freep = fetch_bitmap(hashp, free_page);
855 #ifdef DEBUG
857 * This had better never happen. It means we tried to read a bitmap
858 * that has already had overflow pages allocated off it, and we
859 * failed to read it from the file.
861 if (!freep)
862 assert(0);
863 #endif
864 CLRBIT(freep, free_bit);
865 #ifdef DEBUG2
866 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
867 obufp->addr, free_bit, free_page);
868 #endif
869 __reclaim_buf(hashp, obufp);
873 * Returns:
874 * 0 success
875 * -1 failure
877 static int
878 open_temp(
879 HTAB *hashp
882 sigset_t set, oset;
883 static char namestr[] = "_hashXXXXXX";
885 /* Block signals; make sure file goes away at process exit. */
886 (void)sigfillset(&set);
887 (void)sigprocmask(SIG_BLOCK, &set, &oset);
888 if ((hashp->fp = mkstemp(namestr)) != -1) {
889 (void)unlink(namestr);
890 #ifdef HAVE_FCNTL
891 (void)fcntl(hashp->fp, F_SETFD, 1);
892 #endif
894 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
895 return (hashp->fp != -1 ? 0 : -1);
899 * We have to know that the key will fit, but the last entry on the page is
900 * an overflow pair, so we need to shift things.
902 static void
903 squeeze_key(
904 __uint16_t *sp,
905 const DBT *key,
906 const DBT *val
909 char *p;
910 __uint16_t free_space, n, off, pageno;
912 p = (char *)sp;
913 n = sp[0];
914 free_space = FREESPACE(sp);
915 off = OFFSET(sp);
917 pageno = sp[n - 1];
918 off -= key->size;
919 sp[n - 1] = off;
920 memmove(p + off, key->data, key->size);
921 off -= val->size;
922 sp[n] = off;
923 memmove(p + off, val->data, val->size);
924 sp[0] = n + 2;
925 sp[n + 1] = pageno;
926 sp[n + 2] = OVFLPAGE;
927 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
928 OFFSET(sp) = off;
931 static __uint32_t *
932 fetch_bitmap(
933 HTAB *hashp,
934 int ndx
937 if (ndx >= hashp->nmaps)
938 return (NULL);
939 if ((hashp->mapp[ndx] = (__uint32_t *)malloc(hashp->BSIZE)) == NULL)
940 return (NULL);
941 if (__get_page(hashp,
942 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
943 free(hashp->mapp[ndx]);
944 return (NULL);
946 return (hashp->mapp[ndx]);
949 #ifdef DEBUG4
951 print_chain(addr)
952 int addr;
954 BUFHEAD *bufp;
955 short *bp, oaddr;
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");
969 #endif