]> git.saurik.com Git - apple/libc.git/blob - db/hash/hash_page.c
f045255455e2df6a8dfa4616644ae2159b3051c7
[apple/libc.git] / db / hash / hash_page.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*-
26 * Copyright (c) 1990, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Margo Seltzer.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 */
60
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>
65
66 /*
67 * PACKAGE: hashing
68 *
69 * DESCRIPTION:
70 * Page manipulation for hashing package.
71 *
72 * ROUTINES:
73 *
74 * External
75 * __get_page
76 * __add_ovflpage
77 * Internal
78 * overflow_page
79 * open_temp
80 */
81
82 #include <sys/types.h>
83
84 #include <errno.h>
85 #include <fcntl.h>
86 #include <signal.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <unistd.h>
91 #ifdef DEBUG
92 #include <assert.h>
93 #endif
94
95 #include <db.h>
96 #include "hash.h"
97 #include "page.h"
98 #include "extern.h"
99
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);
108
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; \
113 }
114
115 /*
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
118 * stuff on.
119 */
120 static void
121 putpair(p, key, val)
122 char *p;
123 const DBT *key, *val;
124 {
125 u_int16_t *bp, n, off;
126
127 bp = (u_int16_t *)p;
128
129 /* Enter the key first. */
130 n = bp[0];
131
132 off = OFFSET(bp) - key->size;
133 memmove(p + off, key->data, key->size);
134 bp[++n] = off;
135
136 /* Now the data. */
137 off -= val->size;
138 memmove(p + off, val->data, val->size);
139 bp[++n] = off;
140
141 /* Adjust page info. */
142 bp[0] = n;
143 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
144 bp[n + 2] = off;
145 }
146
147 /*
148 * Returns:
149 * 0 OK
150 * -1 error
151 */
152 extern int
153 __delpair(hashp, bufp, ndx)
154 HTAB *hashp;
155 BUFHEAD *bufp;
156 int ndx;
157 {
158 u_int16_t *bp, newoff;
159 int n;
160 u_int16_t pairlen;
161
162 bp = (u_int16_t *)bufp->page;
163 n = bp[0];
164
165 if (bp[ndx + 1] < REAL_KEY)
166 return (__big_delete(hashp, bufp));
167 if (ndx != 1)
168 newoff = bp[ndx - 1];
169 else
170 newoff = hashp->BSIZE;
171 pairlen = newoff - bp[ndx + 1];
172
173 if (ndx != (n - 1)) {
174 /* Hard Case -- need to shuffle keys */
175 int i;
176 char *src = bufp->page + (int)OFFSET(bp);
177 char *dst = src + (int)pairlen;
178 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
179
180 /* Now adjust the pointers */
181 for (i = ndx + 2; i <= n; i += 2) {
182 if (bp[i + 1] == OVFLPAGE) {
183 bp[i - 2] = bp[i];
184 bp[i - 1] = bp[i + 1];
185 } else {
186 bp[i - 2] = bp[i] + pairlen;
187 bp[i - 1] = bp[i + 1] + pairlen;
188 }
189 }
190 }
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);
194 bp[0] = n - 2;
195 hashp->NKEYS--;
196
197 bufp->flags |= BUF_MOD;
198 return (0);
199 }
200 /*
201 * Returns:
202 * 0 ==> OK
203 * -1 ==> Error
204 */
205 extern int
206 __split_page(hashp, obucket, nbucket)
207 HTAB *hashp;
208 u_int32_t obucket, nbucket;
209 {
210 BUFHEAD *new_bufp, *old_bufp;
211 u_int16_t *ino;
212 char *np;
213 DBT key, val;
214 int n, ndx, retval;
215 u_int16_t copyto, diff, off, moved;
216 char *op;
217
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)
222 return (-1);
223 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
224 if (new_bufp == NULL)
225 return (-1);
226
227 old_bufp->flags |= (BUF_MOD | BUF_PIN);
228 new_bufp->flags |= (BUF_MOD | BUF_PIN);
229
230 ino = (u_int16_t *)(op = old_bufp->page);
231 np = new_bufp->page;
232
233 moved = 0;
234
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;
241 return (retval);
242
243 }
244 key.data = (u_char *)op + ino[n];
245 key.size = off - ino[n];
246
247 if (__call_hash(hashp, key.data, key.size) == obucket) {
248 /* Don't switch page */
249 diff = copyto - off;
250 if (diff) {
251 copyto = ino[n + 1] + diff;
252 memmove(op + copyto, op + ino[n + 1],
253 off - ino[n + 1]);
254 ino[ndx] = copyto + ino[n] - ino[n + 1];
255 ino[ndx + 1] = copyto;
256 } else
257 copyto = ino[n + 1];
258 ndx += 2;
259 } else {
260 /* Switch page */
261 val.data = (u_char *)op + ino[n + 1];
262 val.size = ino[n] - ino[n + 1];
263 putpair(np, &key, &val);
264 moved += 2;
265 }
266
267 off = ino[n + 1];
268 }
269
270 /* Now clean up the page */
271 ino[0] -= moved;
272 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
273 OFFSET(ino) = copyto;
274
275 #ifdef DEBUG3
276 (void)fprintf(stderr, "split %d/%d\n",
277 ((u_int16_t *)np)[0] / 2,
278 ((u_int16_t *)op)[0] / 2);
279 #endif
280 /* unpin both pages */
281 old_bufp->flags &= ~BUF_PIN;
282 new_bufp->flags &= ~BUF_PIN;
283 return (0);
284 }
285
286 /*
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.
291 *
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
295 * big key/data pair.
296 *
297 * Returns:
298 * 0 ==> success
299 * -1 ==> failure
300 */
301 static int
302 ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
303 HTAB *hashp;
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. */
308 {
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 */
313
314 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
315 DBT key, val;
316 SPLIT_RETURN ret;
317 u_int16_t n, off, ov_addr, scopyto;
318 char *cino; /* Character value of ino */
319
320 bufp = old_bufp;
321 ino = (u_int16_t *)old_bufp->page;
322 np = (u_int16_t *)new_bufp->page;
323 op = (u_int16_t *)old_bufp->page;
324 last_bfp = NULL;
325 scopyto = (u_int16_t)copyto; /* ANSI */
326
327 n = ino[0] - 1;
328 while (n < ino[0]) {
329 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
330 if (__big_split(hashp, old_bufp,
331 new_bufp, bufp, bufp->addr, obucket, &ret))
332 return (-1);
333 old_bufp = ret.oldp;
334 if (!old_bufp)
335 return (-1);
336 op = (u_int16_t *)old_bufp->page;
337 new_bufp = ret.newp;
338 if (!new_bufp)
339 return (-1);
340 np = (u_int16_t *)new_bufp->page;
341 bufp = ret.nextp;
342 if (!bufp)
343 return (0);
344 cino = (char *)bufp->page;
345 ino = (u_int16_t *)cino;
346 last_bfp = ret.nextp;
347 } else if (ino[n + 1] == OVFLPAGE) {
348 ov_addr = ino[n];
349 /*
350 * Fix up the old page -- the extra 2 are the fields
351 * which contained the overflow information.
352 */
353 ino[0] -= (moved + 2);
354 FREESPACE(ino) =
355 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
356 OFFSET(ino) = scopyto;
357
358 bufp = __get_buf(hashp, ov_addr, bufp, 0);
359 if (!bufp)
360 return (-1);
361
362 ino = (u_int16_t *)bufp->page;
363 n = 1;
364 scopyto = hashp->BSIZE;
365 moved = 0;
366
367 if (last_bfp)
368 __free_ovflpage(hashp, last_bfp);
369 last_bfp = bufp;
370 }
371 /* Move regular sized pairs of there are any */
372 off = hashp->BSIZE;
373 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
374 cino = (char *)ino;
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];
379 off = ino[n + 1];
380
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);
385 else {
386 old_bufp =
387 __add_ovflpage(hashp, old_bufp);
388 if (!old_bufp)
389 return (-1);
390 op = (u_int16_t *)old_bufp->page;
391 putpair((char *)op, &key, &val);
392 }
393 old_bufp->flags |= BUF_MOD;
394 } else {
395 /* Move to new page */
396 if (PAIRFITS(np, (&key), (&val)))
397 putpair((char *)np, &key, &val);
398 else {
399 new_bufp =
400 __add_ovflpage(hashp, new_bufp);
401 if (!new_bufp)
402 return (-1);
403 np = (u_int16_t *)new_bufp->page;
404 putpair((char *)np, &key, &val);
405 }
406 new_bufp->flags |= BUF_MOD;
407 }
408 }
409 }
410 if (last_bfp)
411 __free_ovflpage(hashp, last_bfp);
412 return (0);
413 }
414
415 /*
416 * Add the given pair to the page
417 *
418 * Returns:
419 * 0 ==> OK
420 * 1 ==> failure
421 */
422 extern int
423 __addel(hashp, bufp, key, val)
424 HTAB *hashp;
425 BUFHEAD *bufp;
426 const DBT *key, *val;
427 {
428 u_int16_t *bp, *sop;
429 int do_expand;
430
431 bp = (u_int16_t *)bufp->page;
432 do_expand = 0;
433 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
434 /* Exception case */
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 */
438 break;
439 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
440 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
441 if (!bufp)
442 return (-1);
443 bp = (u_int16_t *)bufp->page;
444 } else
445 /* Try to squeeze key on this page */
446 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
447 squeeze_key(bp, key, val);
448 return (0);
449 } else {
450 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
451 if (!bufp)
452 return (-1);
453 bp = (u_int16_t *)bufp->page;
454 }
455
456 if (PAIRFITS(bp, key, val))
457 putpair(bufp->page, key, val);
458 else {
459 do_expand = 1;
460 bufp = __add_ovflpage(hashp, bufp);
461 if (!bufp)
462 return (-1);
463 sop = (u_int16_t *)bufp->page;
464
465 if (PAIRFITS(sop, key, val))
466 putpair((char *)sop, key, val);
467 else
468 if (__big_insert(hashp, bufp, key, val))
469 return (-1);
470 }
471 bufp->flags |= BUF_MOD;
472 /*
473 * If the average number of keys per bucket exceeds the fill factor,
474 * expand the table.
475 */
476 hashp->NKEYS++;
477 if (do_expand ||
478 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
479 return (__expand_table(hashp));
480 return (0);
481 }
482
483 /*
484 *
485 * Returns:
486 * pointer on success
487 * NULL on error
488 */
489 extern BUFHEAD *
490 __add_ovflpage(hashp, bufp)
491 HTAB *hashp;
492 BUFHEAD *bufp;
493 {
494 u_int16_t *sp;
495 u_int16_t ndx, ovfl_num;
496 #ifdef DEBUG1
497 int tmp1, tmp2;
498 #endif
499 sp = (u_int16_t *)bufp->page;
500
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;
506 }
507 bufp->flags |= BUF_MOD;
508 ovfl_num = overflow_page(hashp);
509 #ifdef DEBUG1
510 tmp1 = bufp->addr;
511 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
512 #endif
513 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
514 return (NULL);
515 bufp->ovfl->flags |= BUF_MOD;
516 #ifdef DEBUG1
517 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
518 tmp1, tmp2, bufp->ovfl->addr);
519 #endif
520 ndx = sp[0];
521 /*
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
524 * the page.
525 */
526 sp[ndx + 4] = OFFSET(sp);
527 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
528 sp[ndx + 1] = ovfl_num;
529 sp[ndx + 2] = OVFLPAGE;
530 sp[0] = ndx + 2;
531 #ifdef HASH_STATISTICS
532 hash_overflows++;
533 #endif
534 return (bufp->ovfl);
535 }
536
537 /*
538 * Returns:
539 * 0 indicates SUCCESS
540 * -1 indicates FAILURE
541 */
542 extern int
543 __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
544 HTAB *hashp;
545 char *p;
546 u_int32_t bucket;
547 int is_bucket, is_disk, is_bitmap;
548 {
549 int fd, page, size;
550 int rsize;
551 u_int16_t *bp;
552
553 fd = hashp->fp;
554 size = hashp->BSIZE;
555
556 if ((fd == -1) || !is_disk) {
557 PAGE_INIT(p);
558 return (0);
559 }
560 if (is_bucket)
561 page = BUCKET_TO_PAGE(bucket);
562 else
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))
566 return (-1);
567 bp = (u_int16_t *)p;
568 if (!rsize)
569 bp[0] = 0; /* We hit the EOF, so initialize a new page */
570 else
571 if (rsize != size) {
572 errno = EFTYPE;
573 return (-1);
574 }
575 if (!is_bitmap && !bp[0]) {
576 PAGE_INIT(p);
577 } else
578 if (hashp->LORDER != BYTE_ORDER) {
579 int i, max;
580
581 if (is_bitmap) {
582 max = hashp->BSIZE >> 2; /* divide by 4 */
583 for (i = 0; i < max; i++)
584 M_32_SWAP(((int *)p)[i]);
585 } else {
586 M_16_SWAP(bp[0]);
587 max = bp[0] + 2;
588 for (i = 1; i <= max; i++)
589 M_16_SWAP(bp[i]);
590 }
591 }
592 return (0);
593 }
594
595 /*
596 * Write page p to disk
597 *
598 * Returns:
599 * 0 ==> OK
600 * -1 ==>failure
601 */
602 extern int
603 __put_page(hashp, p, bucket, is_bucket, is_bitmap)
604 HTAB *hashp;
605 char *p;
606 u_int32_t bucket;
607 int is_bucket, is_bitmap;
608 {
609 int fd, page, size;
610 int wsize;
611
612 size = hashp->BSIZE;
613 if ((hashp->fp == -1) && open_temp(hashp))
614 return (-1);
615 fd = hashp->fp;
616
617 if (hashp->LORDER != BYTE_ORDER) {
618 int i;
619 int max;
620
621 if (is_bitmap) {
622 max = hashp->BSIZE >> 2; /* divide by 4 */
623 for (i = 0; i < max; i++)
624 M_32_SWAP(((int *)p)[i]);
625 } else {
626 max = ((u_int16_t *)p)[0] + 2;
627 for (i = 0; i <= max; i++)
628 M_16_SWAP(((u_int16_t *)p)[i]);
629 }
630 }
631 if (is_bucket)
632 page = BUCKET_TO_PAGE(bucket);
633 else
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))
637 /* Errno is set */
638 return (-1);
639 if (wsize != size) {
640 errno = EFTYPE;
641 return (-1);
642 }
643 return (0);
644 }
645
646 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
647 /*
648 * Initialize a new bitmap page. Bitmap pages are left in memory
649 * once they are read in.
650 */
651 extern int
652 __ibitmap(hashp, pnum, nbits, ndx)
653 HTAB *hashp;
654 int pnum, nbits, ndx;
655 {
656 u_int32_t *ip;
657 int clearbytes, clearints;
658
659 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
660 return (1);
661 hashp->nmaps++;
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);
668 SETBIT(ip, 0);
669 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
670 hashp->mapp[ndx] = ip;
671 return (0);
672 }
673
674 static u_int32_t
675 first_free(map)
676 u_int32_t map;
677 {
678 u_int32_t i, mask;
679
680 mask = 0x1;
681 for (i = 0; i < BITS_PER_MAP; i++) {
682 if (!(mask & map))
683 return (i);
684 mask = mask << 1;
685 }
686 return (i);
687 }
688
689 static u_int16_t
690 overflow_page(hashp)
691 HTAB *hashp;
692 {
693 u_int32_t *freep;
694 int max_free, offset, splitnum;
695 u_int16_t addr;
696 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
697 #ifdef DEBUG2
698 int tmp1, tmp2;
699 #endif
700 splitnum = hashp->OVFL_POINT;
701 max_free = hashp->SPARES[splitnum];
702
703 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
704 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
705
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)))
711 return (0);
712 if (i == free_page)
713 in_use_bits = free_bit;
714 else
715 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
716
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);
722 } else {
723 bit = 0;
724 j = 0;
725 }
726 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
727 if (freep[j] != ALL_SET)
728 goto found;
729 }
730
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);
736
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);
741 return (0);
742 }
743 hashp->OVFL_POINT = splitnum;
744 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
745 hashp->SPARES[splitnum-1]--;
746 offset = 1;
747 }
748
749 /* Check if we need to allocate a new bitmap page */
750 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
751 free_page++;
752 if (free_page >= NCACHED) {
753 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
754 return (0);
755 }
756 /*
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.
766 */
767 if (__ibitmap(hashp,
768 (int)OADDR_OF(splitnum, offset), 1, free_page))
769 return (0);
770 hashp->SPARES[splitnum]++;
771 #ifdef DEBUG2
772 free_bit = 2;
773 #endif
774 offset++;
775 if (offset > SPLITMASK) {
776 if (++splitnum >= NCACHED) {
777 (void)write(STDERR_FILENO, OVMSG,
778 sizeof(OVMSG) - 1);
779 return (0);
780 }
781 hashp->OVFL_POINT = splitnum;
782 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
783 hashp->SPARES[splitnum-1]--;
784 offset = 0;
785 }
786 } else {
787 /*
788 * Free_bit addresses the last used bit. Bump it to address
789 * the first available bit.
790 */
791 free_bit++;
792 SETBIT(freep, free_bit);
793 }
794
795 /* Calculate address of the new overflow page */
796 addr = OADDR_OF(splitnum, offset);
797 #ifdef DEBUG2
798 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
799 addr, free_bit, free_page);
800 #endif
801 return (addr);
802
803 found:
804 bit = bit + first_free(freep[j]);
805 SETBIT(freep, bit);
806 #ifdef DEBUG2
807 tmp1 = bit;
808 tmp2 = i;
809 #endif
810 /*
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.
814 */
815 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
816 if (bit >= hashp->LAST_FREED)
817 hashp->LAST_FREED = bit - 1;
818
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);
825 #ifdef DEBUG2
826 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
827 addr, tmp1, tmp2);
828 #endif
829
830 /* Allocate and return the overflow page */
831 return (addr);
832 }
833
834 /*
835 * Mark this overflow page as free.
836 */
837 extern void
838 __free_ovflpage(hashp, obufp)
839 HTAB *hashp;
840 BUFHEAD *obufp;
841 {
842 u_int16_t addr;
843 u_int32_t *freep;
844 int bit_address, free_page, free_bit;
845 u_int16_t ndx;
846
847 addr = obufp->addr;
848 #ifdef DEBUG1
849 (void)fprintf(stderr, "Freeing %d\n", addr);
850 #endif
851 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
852 bit_address =
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);
858
859 if (!(freep = hashp->mapp[free_page]))
860 freep = fetch_bitmap(hashp, free_page);
861 #ifdef DEBUG
862 /*
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.
866 */
867 if (!freep)
868 assert(0);
869 #endif
870 CLRBIT(freep, free_bit);
871 #ifdef DEBUG2
872 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
873 obufp->addr, free_bit, free_page);
874 #endif
875 __reclaim_buf(hashp, obufp);
876 }
877
878 /*
879 * Returns:
880 * 0 success
881 * -1 failure
882 */
883 static int
884 open_temp(hashp)
885 HTAB *hashp;
886 {
887 sigset_t set, oset;
888 static char namestr[] = "_hashXXXXXX";
889
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);
896 }
897 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
898 return (hashp->fp != -1 ? 0 : -1);
899 }
900
901 /*
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.
904 */
905 static void
906 squeeze_key(sp, key, val)
907 u_int16_t *sp;
908 const DBT *key, *val;
909 {
910 char *p;
911 u_int16_t free_space, n, off, pageno;
912
913 p = (char *)sp;
914 n = sp[0];
915 free_space = FREESPACE(sp);
916 off = OFFSET(sp);
917
918 pageno = sp[n - 1];
919 off -= key->size;
920 sp[n - 1] = off;
921 memmove(p + off, key->data, key->size);
922 off -= val->size;
923 sp[n] = off;
924 memmove(p + off, val->data, val->size);
925 sp[0] = n + 2;
926 sp[n + 1] = pageno;
927 sp[n + 2] = OVFLPAGE;
928 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
929 OFFSET(sp) = off;
930 }
931
932 static u_int32_t *
933 fetch_bitmap(hashp, ndx)
934 HTAB *hashp;
935 int ndx;
936 {
937 if (ndx >= hashp->nmaps)
938 return (NULL);
939 if ((hashp->mapp[ndx] = (u_int32_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);
945 }
946 return (hashp->mapp[ndx]);
947 }
948
949 #ifdef DEBUG4
950 int
951 print_chain(addr)
952 int addr;
953 {
954 BUFHEAD *bufp;
955 short *bp, oaddr;
956
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;
966 }
967 (void)fprintf(stderr, "\n");
968 }
969 #endif