hash_page.c revision 1.13 1 /* $NetBSD: hash_page.c,v 1.13 1998/12/09 12:42:50 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Margo Seltzer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
43 #else
44 __RCSID("$NetBSD: hash_page.c,v 1.13 1998/12/09 12:42:50 christos Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 /*
49 * PACKAGE: hashing
50 *
51 * DESCRIPTION:
52 * Page manipulation for hashing package.
53 *
54 * ROUTINES:
55 *
56 * External
57 * __get_page
58 * __add_ovflpage
59 * Internal
60 * overflow_page
61 * open_temp
62 */
63
64 #include "namespace.h"
65
66 #include <sys/types.h>
67
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #ifdef DEBUG
76 #include <assert.h>
77 #endif
78
79 #include <db.h>
80 #include "hash.h"
81 #include "page.h"
82 #include "extern.h"
83
84 static u_int32_t *fetch_bitmap __P((HTAB *, int));
85 static u_int32_t first_free __P((u_int32_t));
86 static int open_temp __P((HTAB *));
87 static u_int16_t overflow_page __P((HTAB *));
88 static void putpair __P((char *, const DBT *, const DBT *));
89 static void squeeze_key __P((u_int16_t *, const DBT *, const DBT *));
90 static int ugly_split
91 __P((HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int));
92
93 #define PAGE_INIT(P) { \
94 ((u_int16_t *)(void *)(P))[0] = 0; \
95 ((u_int16_t *)(void *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
96 ((u_int16_t *)(void *)(P))[2] = hashp->BSIZE; \
97 }
98
99 /*
100 * This is called AFTER we have verified that there is room on the page for
101 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
102 * stuff on.
103 */
104 static void
105 putpair(p, key, val)
106 char *p;
107 const DBT *key, *val;
108 {
109 register u_int16_t *bp, n, off;
110
111 bp = (u_int16_t *)(void *)p;
112
113 /* Enter the key first. */
114 n = bp[0];
115
116 off = OFFSET(bp) - key->size;
117 memmove(p + off, key->data, key->size);
118 bp[++n] = off;
119
120 /* Now the data. */
121 off -= val->size;
122 memmove(p + off, val->data, val->size);
123 bp[++n] = off;
124
125 /* Adjust page info. */
126 bp[0] = n;
127 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
128 bp[n + 2] = off;
129 }
130
131 /*
132 * Returns:
133 * 0 OK
134 * -1 error
135 */
136 extern int
137 __delpair(hashp, bufp, ndx)
138 HTAB *hashp;
139 BUFHEAD *bufp;
140 register int ndx;
141 {
142 register u_int16_t *bp, newoff;
143 register int n;
144 u_int16_t pairlen;
145
146 bp = (u_int16_t *)(void *)bufp->page;
147 n = bp[0];
148
149 if (bp[ndx + 1] < REAL_KEY)
150 return (__big_delete(hashp, bufp));
151 if (ndx != 1)
152 newoff = bp[ndx - 1];
153 else
154 newoff = hashp->BSIZE;
155 pairlen = newoff - bp[ndx + 1];
156
157 if (ndx != (n - 1)) {
158 /* Hard Case -- need to shuffle keys */
159 register int i;
160 register char *src = bufp->page + (int)OFFSET(bp);
161 register char *dst = src + (int)pairlen;
162 memmove(dst, src, (size_t)(bp[ndx + 1] - OFFSET(bp)));
163
164 /* Now adjust the pointers */
165 for (i = ndx + 2; i <= n; i += 2) {
166 if (bp[i + 1] == OVFLPAGE) {
167 bp[i - 2] = bp[i];
168 bp[i - 1] = bp[i + 1];
169 } else {
170 bp[i - 2] = bp[i] + pairlen;
171 bp[i - 1] = bp[i + 1] + pairlen;
172 }
173 }
174 }
175 /* Finally adjust the page data */
176 bp[n] = OFFSET(bp) + pairlen;
177 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
178 bp[0] = n - 2;
179 hashp->NKEYS--;
180
181 bufp->flags |= BUF_MOD;
182 return (0);
183 }
184 /*
185 * Returns:
186 * 0 ==> OK
187 * -1 ==> Error
188 */
189 extern int
190 __split_page(hashp, obucket, nbucket)
191 HTAB *hashp;
192 u_int32_t obucket, nbucket;
193 {
194 register BUFHEAD *new_bufp, *old_bufp;
195 register u_int16_t *ino;
196 register char *np;
197 DBT key, val;
198 int n, ndx, retval;
199 u_int16_t copyto, diff, off, moved;
200 char *op;
201
202 copyto = (u_int16_t)hashp->BSIZE;
203 off = (u_int16_t)hashp->BSIZE;
204 old_bufp = __get_buf(hashp, obucket, NULL, 0);
205 if (old_bufp == NULL)
206 return (-1);
207 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
208 if (new_bufp == NULL)
209 return (-1);
210
211 old_bufp->flags |= (BUF_MOD | BUF_PIN);
212 new_bufp->flags |= (BUF_MOD | BUF_PIN);
213
214 ino = (u_int16_t *)(void *)(op = old_bufp->page);
215 np = new_bufp->page;
216
217 moved = 0;
218
219 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
220 if (ino[n + 1] < REAL_KEY) {
221 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
222 (int)copyto, (int)moved);
223 old_bufp->flags &= ~BUF_PIN;
224 new_bufp->flags &= ~BUF_PIN;
225 return (retval);
226
227 }
228 key.data = (u_char *)op + ino[n];
229 key.size = off - ino[n];
230
231 if (__call_hash(hashp, key.data, (int)key.size) == obucket) {
232 /* Don't switch page */
233 diff = copyto - off;
234 if (diff) {
235 copyto = ino[n + 1] + diff;
236 memmove(op + copyto, op + ino[n + 1],
237 (size_t)(off - ino[n + 1]));
238 ino[ndx] = copyto + ino[n] - ino[n + 1];
239 ino[ndx + 1] = copyto;
240 } else
241 copyto = ino[n + 1];
242 ndx += 2;
243 } else {
244 /* Switch page */
245 val.data = (u_char *)op + ino[n + 1];
246 val.size = ino[n] - ino[n + 1];
247 putpair(np, &key, &val);
248 moved += 2;
249 }
250
251 off = ino[n + 1];
252 }
253
254 /* Now clean up the page */
255 ino[0] -= moved;
256 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
257 OFFSET(ino) = copyto;
258
259 #ifdef DEBUG3
260 (void)fprintf(stderr, "split %d/%d\n",
261 ((u_int16_t *)np)[0] / 2,
262 ((u_int16_t *)op)[0] / 2);
263 #endif
264 /* unpin both pages */
265 old_bufp->flags &= ~BUF_PIN;
266 new_bufp->flags &= ~BUF_PIN;
267 return (0);
268 }
269
270 /*
271 * Called when we encounter an overflow or big key/data page during split
272 * handling. This is special cased since we have to begin checking whether
273 * the key/data pairs fit on their respective pages and because we may need
274 * overflow pages for both the old and new pages.
275 *
276 * The first page might be a page with regular key/data pairs in which case
277 * we have a regular overflow condition and just need to go on to the next
278 * page or it might be a big key/data pair in which case we need to fix the
279 * big key/data pair.
280 *
281 * Returns:
282 * 0 ==> success
283 * -1 ==> failure
284 */
285 static int
286 ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
287 HTAB *hashp;
288 u_int32_t obucket; /* Same as __split_page. */
289 BUFHEAD *old_bufp, *new_bufp;
290 int copyto; /* First byte on page which contains key/data values. */
291 int moved; /* Number of pairs moved to new page. */
292 {
293 register BUFHEAD *bufp; /* Buffer header for ino */
294 register u_int16_t *ino; /* Page keys come off of */
295 register u_int16_t *np; /* New page */
296 register u_int16_t *op; /* Page keys go on to if they aren't moving */
297
298 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
299 DBT key, val;
300 SPLIT_RETURN ret;
301 u_int16_t n, off, ov_addr, scopyto;
302 char *cino; /* Character value of ino */
303
304 bufp = old_bufp;
305 ino = (u_int16_t *)(void *)old_bufp->page;
306 np = (u_int16_t *)(void *)new_bufp->page;
307 op = (u_int16_t *)(void *)old_bufp->page;
308 last_bfp = NULL;
309 scopyto = (u_int16_t)copyto; /* ANSI */
310
311 n = ino[0] - 1;
312 while (n < ino[0]) {
313 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
314 if (__big_split(hashp, old_bufp,
315 new_bufp, bufp, (int)bufp->addr, obucket, &ret))
316 return (-1);
317 old_bufp = ret.oldp;
318 if (!old_bufp)
319 return (-1);
320 op = (u_int16_t *)(void *)old_bufp->page;
321 new_bufp = ret.newp;
322 if (!new_bufp)
323 return (-1);
324 np = (u_int16_t *)(void *)new_bufp->page;
325 bufp = ret.nextp;
326 if (!bufp)
327 return (0);
328 cino = (char *)bufp->page;
329 ino = (u_int16_t *)(void *)cino;
330 last_bfp = ret.nextp;
331 } else if (ino[n + 1] == OVFLPAGE) {
332 ov_addr = ino[n];
333 /*
334 * Fix up the old page -- the extra 2 are the fields
335 * which contained the overflow information.
336 */
337 ino[0] -= (moved + 2);
338 FREESPACE(ino) =
339 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
340 OFFSET(ino) = scopyto;
341
342 bufp = __get_buf(hashp, (u_int32_t)ov_addr, bufp, 0);
343 if (!bufp)
344 return (-1);
345
346 ino = (u_int16_t *)(void *)bufp->page;
347 n = 1;
348 scopyto = hashp->BSIZE;
349 moved = 0;
350
351 if (last_bfp)
352 __free_ovflpage(hashp, last_bfp);
353 last_bfp = bufp;
354 }
355 /* Move regular sized pairs of there are any */
356 off = hashp->BSIZE;
357 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
358 cino = (char *)(void *)ino;
359 key.data = (u_char *)cino + ino[n];
360 key.size = off - ino[n];
361 val.data = (u_char *)cino + ino[n + 1];
362 val.size = ino[n] - ino[n + 1];
363 off = ino[n + 1];
364
365 if (__call_hash(hashp, key.data, (int)key.size) == obucket) {
366 /* Keep on old page */
367 if (PAIRFITS(op, (&key), (&val)))
368 putpair((char *)(void *)op, &key, &val);
369 else {
370 old_bufp =
371 __add_ovflpage(hashp, old_bufp);
372 if (!old_bufp)
373 return (-1);
374 op = (u_int16_t *)(void *)old_bufp->page;
375 putpair((char *)(void *)op, &key, &val);
376 }
377 old_bufp->flags |= BUF_MOD;
378 } else {
379 /* Move to new page */
380 if (PAIRFITS(np, (&key), (&val)))
381 putpair((char *)(void *)np, &key, &val);
382 else {
383 new_bufp =
384 __add_ovflpage(hashp, new_bufp);
385 if (!new_bufp)
386 return (-1);
387 np = (u_int16_t *)(void *)new_bufp->page;
388 putpair((char *)(void *)np, &key, &val);
389 }
390 new_bufp->flags |= BUF_MOD;
391 }
392 }
393 }
394 if (last_bfp)
395 __free_ovflpage(hashp, last_bfp);
396 return (0);
397 }
398
399 /*
400 * Add the given pair to the page
401 *
402 * Returns:
403 * 0 ==> OK
404 * 1 ==> failure
405 */
406 extern int
407 __addel(hashp, bufp, key, val)
408 HTAB *hashp;
409 BUFHEAD *bufp;
410 const DBT *key, *val;
411 {
412 register u_int16_t *bp, *sop;
413 int do_expand;
414
415 bp = (u_int16_t *)(void *)bufp->page;
416 do_expand = 0;
417 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
418 /* Exception case */
419 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
420 /* This is the last page of a big key/data pair
421 and we need to add another page */
422 break;
423 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
424 bufp = __get_buf(hashp, (u_int32_t)bp[bp[0] - 1], bufp,
425 0);
426 if (!bufp)
427 return (-1);
428 bp = (u_int16_t *)(void *)bufp->page;
429 } else
430 /* Try to squeeze key on this page */
431 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
432 squeeze_key(bp, key, val);
433 return (0);
434 } else {
435 bufp = __get_buf(hashp,
436 (u_int32_t)bp[bp[0] - 1], bufp, 0);
437 if (!bufp)
438 return (-1);
439 bp = (u_int16_t *)(void *)bufp->page;
440 }
441
442 if (PAIRFITS(bp, key, val))
443 putpair(bufp->page, key, val);
444 else {
445 do_expand = 1;
446 bufp = __add_ovflpage(hashp, bufp);
447 if (!bufp)
448 return (-1);
449 sop = (u_int16_t *)(void *)bufp->page;
450
451 if (PAIRFITS(sop, key, val))
452 putpair((char *)(void *)sop, key, val);
453 else
454 if (__big_insert(hashp, bufp, key, val))
455 return (-1);
456 }
457 bufp->flags |= BUF_MOD;
458 /*
459 * If the average number of keys per bucket exceeds the fill factor,
460 * expand the table.
461 */
462 hashp->NKEYS++;
463 if (do_expand ||
464 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
465 return (__expand_table(hashp));
466 return (0);
467 }
468
469 /*
470 *
471 * Returns:
472 * pointer on success
473 * NULL on error
474 */
475 extern BUFHEAD *
476 __add_ovflpage(hashp, bufp)
477 HTAB *hashp;
478 BUFHEAD *bufp;
479 {
480 register u_int16_t *sp;
481 u_int16_t ndx, ovfl_num;
482 #ifdef DEBUG1
483 int tmp1, tmp2;
484 #endif
485 sp = (u_int16_t *)(void *)bufp->page;
486
487 /* Check if we are dynamically determining the fill factor */
488 if (hashp->FFACTOR == DEF_FFACTOR) {
489 hashp->FFACTOR = (u_int32_t)sp[0] >> 1;
490 if (hashp->FFACTOR < MIN_FFACTOR)
491 hashp->FFACTOR = MIN_FFACTOR;
492 }
493 bufp->flags |= BUF_MOD;
494 ovfl_num = overflow_page(hashp);
495 #ifdef DEBUG1
496 tmp1 = bufp->addr;
497 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
498 #endif
499 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, (u_int32_t)ovfl_num,
500 bufp, 1)))
501 return (NULL);
502 bufp->ovfl->flags |= BUF_MOD;
503 #ifdef DEBUG1
504 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
505 tmp1, tmp2, bufp->ovfl->addr);
506 #endif
507 ndx = sp[0];
508 /*
509 * Since a pair is allocated on a page only if there's room to add
510 * an overflow page, we know that the OVFL information will fit on
511 * the page.
512 */
513 sp[ndx + 4] = OFFSET(sp);
514 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
515 sp[ndx + 1] = ovfl_num;
516 sp[ndx + 2] = OVFLPAGE;
517 sp[0] = ndx + 2;
518 #ifdef HASH_STATISTICS
519 hash_overflows++;
520 #endif
521 return (bufp->ovfl);
522 }
523
524 /*
525 * Returns:
526 * 0 indicates SUCCESS
527 * -1 indicates FAILURE
528 */
529 extern int
530 __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
531 HTAB *hashp;
532 char *p;
533 u_int32_t bucket;
534 int is_bucket, is_disk, is_bitmap;
535 {
536 register int fd, page, size;
537 int rsize;
538 u_int16_t *bp;
539
540 fd = hashp->fp;
541 size = hashp->BSIZE;
542
543 if ((fd == -1) || !is_disk) {
544 PAGE_INIT(p);
545 return (0);
546 }
547 if (is_bucket)
548 page = BUCKET_TO_PAGE(bucket);
549 else
550 page = OADDR_TO_PAGE(bucket);
551 if ((rsize = pread(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1)
552 return (-1);
553 bp = (u_int16_t *)(void *)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);
560 }
561 if (!is_bitmap && !bp[0]) {
562 PAGE_INIT(p);
563 } else
564 if (hashp->LORDER != BYTE_ORDER) {
565 register int i, max;
566
567 if (is_bitmap) {
568 max = (u_int32_t)hashp->BSIZE >> 2; /* divide by 4 */
569 for (i = 0; i < max; i++)
570 M_32_SWAP(((int *)(void *)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]);
576 }
577 }
578 return (0);
579 }
580
581 /*
582 * Write page p to disk
583 *
584 * Returns:
585 * 0 ==> OK
586 * -1 ==>failure
587 */
588 extern int
589 __put_page(hashp, p, bucket, is_bucket, is_bitmap)
590 HTAB *hashp;
591 char *p;
592 u_int32_t bucket;
593 int is_bucket, is_bitmap;
594 {
595 register int fd, page, size;
596 int wsize;
597
598 size = hashp->BSIZE;
599 if ((hashp->fp == -1) && open_temp(hashp))
600 return (-1);
601 fd = hashp->fp;
602
603 if (hashp->LORDER != BYTE_ORDER) {
604 register int i;
605 register int max;
606
607 if (is_bitmap) {
608 max = (u_int32_t)hashp->BSIZE >> 2; /* divide by 4 */
609 for (i = 0; i < max; i++)
610 M_32_SWAP(((int *)(void *)p)[i]);
611 } else {
612 max = ((u_int16_t *)(void *)p)[0] + 2;
613 for (i = 0; i <= max; i++)
614 M_16_SWAP(((u_int16_t *)(void *)p)[i]);
615 }
616 }
617 if (is_bucket)
618 page = BUCKET_TO_PAGE(bucket);
619 else
620 page = OADDR_TO_PAGE(bucket);
621 if ((wsize = pwrite(fd, p, (size_t)size, (off_t)page << hashp->BSHIFT)) == -1)
622 /* Errno is set */
623 return (-1);
624 if (wsize != size) {
625 errno = EFTYPE;
626 return (-1);
627 }
628 return (0);
629 }
630
631 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
632 /*
633 * Initialize a new bitmap page. Bitmap pages are left in memory
634 * once they are read in.
635 */
636 extern int
637 __ibitmap(hashp, pnum, nbits, ndx)
638 HTAB *hashp;
639 int pnum, nbits, ndx;
640 {
641 u_int32_t *ip;
642 int clearbytes, clearints;
643
644 if ((ip = (u_int32_t *)malloc((size_t)hashp->BSIZE)) == NULL)
645 return (1);
646 hashp->nmaps++;
647 clearints = ((u_int32_t)(nbits - 1) >> INT_BYTE_SHIFT) + 1;
648 clearbytes = clearints << INT_TO_BYTE;
649 (void)memset(ip, 0, (size_t)clearbytes);
650 (void)memset(((char *)(void *)ip) + clearbytes, 0xFF,
651 (size_t)(hashp->BSIZE - clearbytes));
652 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
653 SETBIT(ip, 0);
654 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
655 hashp->mapp[ndx] = ip;
656 return (0);
657 }
658
659 static u_int32_t
660 first_free(map)
661 u_int32_t map;
662 {
663 register u_int32_t i, mask;
664
665 mask = 0x1;
666 for (i = 0; i < BITS_PER_MAP; i++) {
667 if (!(mask & map))
668 return (i);
669 mask = mask << 1;
670 }
671 return (i);
672 }
673
674 static u_int16_t
675 overflow_page(hashp)
676 HTAB *hashp;
677 {
678 register u_int32_t *freep = NULL;
679 register int max_free, offset, splitnum;
680 u_int16_t addr;
681 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
682 #ifdef DEBUG2
683 int tmp1, tmp2;
684 #endif
685 splitnum = hashp->OVFL_POINT;
686 max_free = hashp->SPARES[splitnum];
687
688 free_page = (u_int32_t)(max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
689 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
690
691 /* Look through all the free maps to find the first free block */
692 first_page = (u_int32_t)hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
693 for ( i = first_page; i <= free_page; i++ ) {
694 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
695 !(freep = fetch_bitmap(hashp, i)))
696 return (0);
697 if (i == free_page)
698 in_use_bits = free_bit;
699 else
700 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
701
702 if (i == first_page) {
703 bit = hashp->LAST_FREED &
704 ((hashp->BSIZE << BYTE_SHIFT) - 1);
705 j = bit / BITS_PER_MAP;
706 bit = bit & ~(BITS_PER_MAP - 1);
707 } else {
708 bit = 0;
709 j = 0;
710 }
711 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
712 if (freep[j] != ALL_SET)
713 goto found;
714 }
715
716 /* No Free Page Found */
717 hashp->LAST_FREED = hashp->SPARES[splitnum];
718 hashp->SPARES[splitnum]++;
719 offset = hashp->SPARES[splitnum] -
720 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
721
722 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
723 if (offset > SPLITMASK) {
724 if (++splitnum >= NCACHED) {
725 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
726 return (0);
727 }
728 hashp->OVFL_POINT = splitnum;
729 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
730 hashp->SPARES[splitnum-1]--;
731 offset = 1;
732 }
733
734 /* Check if we need to allocate a new bitmap page */
735 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
736 free_page++;
737 if (free_page >= NCACHED) {
738 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
739 return (0);
740 }
741 /*
742 * This is tricky. The 1 indicates that you want the new page
743 * allocated with 1 clear bit. Actually, you are going to
744 * allocate 2 pages from this map. The first is going to be
745 * the map page, the second is the overflow page we were
746 * looking for. The init_bitmap routine automatically, sets
747 * the first bit of itself to indicate that the bitmap itself
748 * is in use. We would explicitly set the second bit, but
749 * don't have to if we tell init_bitmap not to leave it clear
750 * in the first place.
751 */
752 if (__ibitmap(hashp,
753 (int)OADDR_OF(splitnum, offset), 1, free_page))
754 return (0);
755 hashp->SPARES[splitnum]++;
756 #ifdef DEBUG2
757 free_bit = 2;
758 #endif
759 offset++;
760 if (offset > SPLITMASK) {
761 if (++splitnum >= NCACHED) {
762 (void)write(STDERR_FILENO, OVMSG,
763 sizeof(OVMSG) - 1);
764 return (0);
765 }
766 hashp->OVFL_POINT = splitnum;
767 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
768 hashp->SPARES[splitnum-1]--;
769 offset = 0;
770 }
771 } else {
772 /*
773 * Free_bit addresses the last used bit. Bump it to address
774 * the first available bit.
775 */
776 free_bit++;
777 SETBIT(freep, free_bit);
778 }
779
780 /* Calculate address of the new overflow page */
781 addr = OADDR_OF(splitnum, offset);
782 #ifdef DEBUG2
783 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
784 addr, free_bit, free_page);
785 #endif
786 return (addr);
787
788 found:
789 bit = bit + first_free(freep[j]);
790 SETBIT(freep, bit);
791 #ifdef DEBUG2
792 tmp1 = bit;
793 tmp2 = i;
794 #endif
795 /*
796 * Bits are addressed starting with 0, but overflow pages are addressed
797 * beginning at 1. Bit is a bit addressnumber, so we need to increment
798 * it to convert it to a page number.
799 */
800 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
801 if (bit >= hashp->LAST_FREED)
802 hashp->LAST_FREED = bit - 1;
803
804 /* Calculate the split number for this page */
805 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
806 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
807 if (offset >= SPLITMASK)
808 return (0); /* Out of overflow pages */
809 addr = OADDR_OF(i, offset);
810 #ifdef DEBUG2
811 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
812 addr, tmp1, tmp2);
813 #endif
814
815 /* Allocate and return the overflow page */
816 return (addr);
817 }
818
819 /*
820 * Mark this overflow page as free.
821 */
822 extern void
823 __free_ovflpage(hashp, obufp)
824 HTAB *hashp;
825 BUFHEAD *obufp;
826 {
827 register u_int16_t addr;
828 u_int32_t *freep;
829 int bit_address, free_page, free_bit;
830 u_int16_t ndx;
831
832 addr = obufp->addr;
833 #ifdef DEBUG1
834 (void)fprintf(stderr, "Freeing %d\n", addr);
835 #endif
836 ndx = (((u_int32_t)addr) >> SPLITSHIFT);
837 bit_address =
838 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
839 if (bit_address < hashp->LAST_FREED)
840 hashp->LAST_FREED = bit_address;
841 free_page = ((u_int32_t)bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
842 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
843
844 if (!(freep = hashp->mapp[free_page]))
845 freep = fetch_bitmap(hashp, free_page);
846 #ifdef DEBUG
847 /*
848 * This had better never happen. It means we tried to read a bitmap
849 * that has already had overflow pages allocated off it, and we
850 * failed to read it from the file.
851 */
852 if (!freep)
853 assert(0);
854 #endif
855 CLRBIT(freep, free_bit);
856 #ifdef DEBUG2
857 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
858 obufp->addr, free_bit, free_page);
859 #endif
860 __reclaim_buf(hashp, obufp);
861 }
862
863 /*
864 * Returns:
865 * 0 success
866 * -1 failure
867 */
868 static int
869 open_temp(hashp)
870 HTAB *hashp;
871 {
872 sigset_t set, oset;
873 char namestr[] = "_hashXXXXXX";
874
875 /* Block signals; make sure file goes away at process exit. */
876 (void)sigfillset(&set);
877 (void)sigprocmask(SIG_BLOCK, &set, &oset);
878 if ((hashp->fp = mkstemp(namestr)) != -1) {
879 (void)unlink(namestr);
880 (void)fcntl(hashp->fp, F_SETFD, 1);
881 }
882 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
883 return (hashp->fp != -1 ? 0 : -1);
884 }
885
886 /*
887 * We have to know that the key will fit, but the last entry on the page is
888 * an overflow pair, so we need to shift things.
889 */
890 static void
891 squeeze_key(sp, key, val)
892 u_int16_t *sp;
893 const DBT *key, *val;
894 {
895 register char *p;
896 u_int16_t free_space, n, off, pageno;
897
898 p = (char *)(void *)sp;
899 n = sp[0];
900 free_space = FREESPACE(sp);
901 off = OFFSET(sp);
902
903 pageno = sp[n - 1];
904 off -= key->size;
905 sp[n - 1] = off;
906 memmove(p + off, key->data, key->size);
907 off -= val->size;
908 sp[n] = off;
909 memmove(p + off, val->data, val->size);
910 sp[0] = n + 2;
911 sp[n + 1] = pageno;
912 sp[n + 2] = OVFLPAGE;
913 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
914 OFFSET(sp) = off;
915 }
916
917 static u_int32_t *
918 fetch_bitmap(hashp, ndx)
919 HTAB *hashp;
920 int ndx;
921 {
922 if (ndx >= hashp->nmaps)
923 return (NULL);
924 if ((hashp->mapp[ndx] = (u_int32_t *)malloc((size_t)hashp->BSIZE)) == NULL)
925 return (NULL);
926 if (__get_page(hashp,
927 (char *)(void *)hashp->mapp[ndx], (u_int32_t)hashp->BITMAPS[ndx], 0, 1, 1)) {
928 free(hashp->mapp[ndx]);
929 return (NULL);
930 }
931 return (hashp->mapp[ndx]);
932 }
933
934 #ifdef DEBUG4
935 int
936 print_chain(addr)
937 int addr;
938 {
939 BUFHEAD *bufp;
940 short *bp, oaddr;
941
942 (void)fprintf(stderr, "%d ", addr);
943 bufp = __get_buf(hashp, addr, NULL, 0);
944 bp = (short *)bufp->page;
945 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
946 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
947 oaddr = bp[bp[0] - 1];
948 (void)fprintf(stderr, "%d ", (int)oaddr);
949 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
950 bp = (short *)bufp->page;
951 }
952 (void)fprintf(stderr, "\n");
953 }
954 #endif
955