Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / dbm / src / h_page.c
blob1f06a0a6ad1afe421cd121f41ae3221cb419e47c
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. ***REMOVED*** - see
17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #if defined(unix)
36 #define MY_LSEEK lseek
37 #else
38 #define MY_LSEEK new_lseek
39 extern long new_lseek(int fd, long pos, int start);
40 #endif
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
44 #endif /* LIBC_SCCS and not lint */
46 #include "watcomfx.h"
49 * PACKAGE: hashing
51 * DESCRIPTION:
52 * Page manipulation for hashing package.
54 * ROUTINES:
56 * External
57 * __get_page
58 * __add_ovflpage
59 * Internal
60 * overflow_page
61 * open_temp
63 #ifndef macintosh
64 #include <sys/types.h>
65 #endif
67 #if defined(macintosh)
68 #include <unistd.h>
69 #endif
71 #include <errno.h>
72 #include <fcntl.h>
73 #if defined(_WIN32) || defined(_WINDOWS)
74 #include <io.h>
75 #endif
76 #include <signal.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
81 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) && !defined(XP_OS2_VACPP)
82 #include <unistd.h>
83 #endif
85 #include <assert.h>
87 #include "mcom_db.h"
88 #include "hash.h"
89 #include "page.h"
90 /* #include "extern.h" */
92 extern int mkstempflags(char *path, int extraFlags);
94 static uint32 *fetch_bitmap __P((HTAB *, uint32));
95 static uint32 first_free __P((uint32));
96 static int open_temp __P((HTAB *));
97 static uint16 overflow_page __P((HTAB *));
98 static void squeeze_key __P((uint16 *, const DBT *, const DBT *));
99 static int ugly_split
100 __P((HTAB *, uint32, BUFHEAD *, BUFHEAD *, int, int));
102 #define PAGE_INIT(P) { \
103 ((uint16 *)(P))[0] = 0; \
104 ((uint16 *)(P))[1] = hashp->BSIZE - 3 * sizeof(uint16); \
105 ((uint16 *)(P))[2] = hashp->BSIZE; \
108 /* implement a new lseek using lseek that
109 * writes zero's when extending a file
110 * beyond the end.
112 long new_lseek(int fd, long offset, int origin)
114 long cur_pos=0;
115 long end_pos=0;
116 long seek_pos=0;
118 if(origin == SEEK_CUR)
120 if(offset < 1)
121 return(lseek(fd, offset, SEEK_CUR));
123 cur_pos = lseek(fd, 0, SEEK_CUR);
125 if(cur_pos < 0)
126 return(cur_pos);
129 end_pos = lseek(fd, 0, SEEK_END);
130 if(end_pos < 0)
131 return(end_pos);
133 if(origin == SEEK_SET)
134 seek_pos = offset;
135 else if(origin == SEEK_CUR)
136 seek_pos = cur_pos + offset;
137 else if(origin == SEEK_END)
138 seek_pos = end_pos + offset;
139 else
141 assert(0);
142 return(-1);
145 /* the seek position desired is before the
146 * end of the file. We don't need
147 * to do anything special except the seek.
149 if(seek_pos <= end_pos)
150 return(lseek(fd, seek_pos, SEEK_SET));
152 /* the seek position is beyond the end of the
153 * file. Write zero's to the end.
155 * we are already at the end of the file so
156 * we just need to "write()" zeros for the
157 * difference between seek_pos-end_pos and
158 * then seek to the position to finish
159 * the call
162 char buffer[1024];
163 long len = seek_pos-end_pos;
164 memset(&buffer, 0, 1024);
165 while(len > 0)
167 write(fd, (char*)&buffer, (size_t)(1024 > len ? len : 1024));
168 len -= 1024;
170 return(lseek(fd, seek_pos, SEEK_SET));
176 * This is called AFTER we have verified that there is room on the page for
177 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
178 * stuff on.
180 static void
181 putpair(char *p, const DBT *key, DBT * val)
183 register uint16 *bp, n, off;
185 bp = (uint16 *)p;
187 /* Enter the key first. */
188 n = bp[0];
190 off = OFFSET(bp) - key->size;
191 memmove(p + off, key->data, key->size);
192 bp[++n] = off;
194 /* Now the data. */
195 off -= val->size;
196 memmove(p + off, val->data, val->size);
197 bp[++n] = off;
199 /* Adjust page info. */
200 bp[0] = n;
201 bp[n + 1] = off - ((n + 3) * sizeof(uint16));
202 bp[n + 2] = off;
206 * Returns:
207 * 0 OK
208 * -1 error
210 extern int
211 __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
213 register uint16 *bp, newoff;
214 register int n;
215 uint16 pairlen;
217 bp = (uint16 *)bufp->page;
218 n = bp[0];
220 if (bp[ndx + 1] < REAL_KEY)
221 return (__big_delete(hashp, bufp));
222 if (ndx != 1)
223 newoff = bp[ndx - 1];
224 else
225 newoff = hashp->BSIZE;
226 pairlen = newoff - bp[ndx + 1];
228 if (ndx != (n - 1)) {
229 /* Hard Case -- need to shuffle keys */
230 register int i;
231 register char *src = bufp->page + (int)OFFSET(bp);
232 uint32 dst_offset = (uint32)OFFSET(bp) + (uint32)pairlen;
233 register char *dst = bufp->page + dst_offset;
234 uint32 length = bp[ndx + 1] - OFFSET(bp);
237 * +-----------+XXX+---------+XXX+---------+---------> +infinity
238 * | | | |
239 * 0 src_offset dst_offset BSIZE
241 * Dst_offset is > src_offset, so if src_offset were bad, dst_offset
242 * would be too, therefore we check only dst_offset.
244 * If dst_offset is >= BSIZE, either OFFSET(bp), or pairlen, or both
245 * is corrupted.
247 * Once we know dst_offset is < BSIZE, we can subtract it from BSIZE
248 * to get an upper bound on length.
250 if(dst_offset > (uint32)hashp->BSIZE)
251 return(DATABASE_CORRUPTED_ERROR);
253 if(length > (uint32)(hashp->BSIZE - dst_offset))
254 return(DATABASE_CORRUPTED_ERROR);
256 memmove(dst, src, length);
258 /* Now adjust the pointers */
259 for (i = ndx + 2; i <= n; i += 2) {
260 if (bp[i + 1] == OVFLPAGE) {
261 bp[i - 2] = bp[i];
262 bp[i - 1] = bp[i + 1];
263 } else {
264 bp[i - 2] = bp[i] + pairlen;
265 bp[i - 1] = bp[i + 1] + pairlen;
269 /* Finally adjust the page data */
270 bp[n] = OFFSET(bp) + pairlen;
271 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(uint16);
272 bp[0] = n - 2;
273 hashp->NKEYS--;
275 bufp->flags |= BUF_MOD;
276 return (0);
279 * Returns:
280 * 0 ==> OK
281 * -1 ==> Error
283 extern int
284 __split_page(HTAB *hashp, uint32 obucket, uint32 nbucket)
286 register BUFHEAD *new_bufp, *old_bufp;
287 register uint16 *ino;
288 register uint16 *tmp_uint16_array;
289 register char *np;
290 DBT key, val;
291 uint16 n, ndx;
292 int retval;
293 uint16 copyto, diff, moved;
294 size_t off;
295 char *op;
297 copyto = (uint16)hashp->BSIZE;
298 off = (uint16)hashp->BSIZE;
299 old_bufp = __get_buf(hashp, obucket, NULL, 0);
300 if (old_bufp == NULL)
301 return (-1);
302 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
303 if (new_bufp == NULL)
304 return (-1);
306 old_bufp->flags |= (BUF_MOD | BUF_PIN);
307 new_bufp->flags |= (BUF_MOD | BUF_PIN);
309 ino = (uint16 *)(op = old_bufp->page);
310 np = new_bufp->page;
312 moved = 0;
314 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
315 if (ino[n + 1] < REAL_KEY) {
316 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
317 (int)copyto, (int)moved);
318 old_bufp->flags &= ~BUF_PIN;
319 new_bufp->flags &= ~BUF_PIN;
320 return (retval);
323 key.data = (uint8 *)op + ino[n];
325 /* check here for ino[n] being greater than
326 * off. If it is then the database has
327 * been corrupted.
329 if(ino[n] > off)
330 return(DATABASE_CORRUPTED_ERROR);
332 key.size = off - ino[n];
334 #ifdef DEBUG
335 /* make sure the size is positive */
336 assert(((int)key.size) > -1);
337 #endif
339 if (__call_hash(hashp, (char *)key.data, key.size) == obucket) {
340 /* Don't switch page */
341 diff = copyto - off;
342 if (diff) {
343 copyto = ino[n + 1] + diff;
344 memmove(op + copyto, op + ino[n + 1],
345 off - ino[n + 1]);
346 ino[ndx] = copyto + ino[n] - ino[n + 1];
347 ino[ndx + 1] = copyto;
348 } else
349 copyto = ino[n + 1];
350 ndx += 2;
351 } else {
352 /* Switch page */
353 val.data = (uint8 *)op + ino[n + 1];
354 val.size = ino[n] - ino[n + 1];
356 /* if the pair doesn't fit something is horribly
357 * wrong. LJM
359 tmp_uint16_array = (uint16*)np;
360 if(!PAIRFITS(tmp_uint16_array, &key, &val))
361 return(DATABASE_CORRUPTED_ERROR);
363 putpair(np, &key, &val);
364 moved += 2;
367 off = ino[n + 1];
370 /* Now clean up the page */
371 ino[0] -= moved;
372 FREESPACE(ino) = copyto - sizeof(uint16) * (ino[0] + 3);
373 OFFSET(ino) = copyto;
375 #ifdef DEBUG3
376 (void)fprintf(stderr, "split %d/%d\n",
377 ((uint16 *)np)[0] / 2,
378 ((uint16 *)op)[0] / 2);
379 #endif
380 /* unpin both pages */
381 old_bufp->flags &= ~BUF_PIN;
382 new_bufp->flags &= ~BUF_PIN;
383 return (0);
387 * Called when we encounter an overflow or big key/data page during split
388 * handling. This is special cased since we have to begin checking whether
389 * the key/data pairs fit on their respective pages and because we may need
390 * overflow pages for both the old and new pages.
392 * The first page might be a page with regular key/data pairs in which case
393 * we have a regular overflow condition and just need to go on to the next
394 * page or it might be a big key/data pair in which case we need to fix the
395 * big key/data pair.
397 * Returns:
398 * 0 ==> success
399 * -1 ==> failure
402 /* the maximum number of loops we will allow UGLY split to chew
403 * on before we assume the database is corrupted and throw it
404 * away.
406 #define MAX_UGLY_SPLIT_LOOPS 10000
408 static int
409 ugly_split(HTAB *hashp, uint32 obucket, BUFHEAD *old_bufp,
410 BUFHEAD *new_bufp,/* Same as __split_page. */ int copyto, int moved)
411 /* int copyto; First byte on page which contains key/data values. */
412 /* int moved; Number of pairs moved to new page. */
414 register BUFHEAD *bufp; /* Buffer header for ino */
415 register uint16 *ino; /* Page keys come off of */
416 register uint16 *np; /* New page */
417 register uint16 *op; /* Page keys go on to if they aren't moving */
418 uint32 loop_detection=0;
420 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
421 DBT key, val;
422 SPLIT_RETURN ret;
423 uint16 n, off, ov_addr, scopyto;
424 char *cino; /* Character value of ino */
425 int status;
427 bufp = old_bufp;
428 ino = (uint16 *)old_bufp->page;
429 np = (uint16 *)new_bufp->page;
430 op = (uint16 *)old_bufp->page;
431 last_bfp = NULL;
432 scopyto = (uint16)copyto; /* ANSI */
434 n = ino[0] - 1;
435 while (n < ino[0]) {
438 /* this function goes nuts sometimes and never returns.
439 * I havent found the problem yet but I need a solution
440 * so if we loop too often we assume a database curruption error
441 * :LJM
443 loop_detection++;
445 if(loop_detection > MAX_UGLY_SPLIT_LOOPS)
446 return DATABASE_CORRUPTED_ERROR;
448 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
449 if ((status = __big_split(hashp, old_bufp,
450 new_bufp, bufp, bufp->addr, obucket, &ret)))
451 return (status);
452 old_bufp = ret.oldp;
453 if (!old_bufp)
454 return (-1);
455 op = (uint16 *)old_bufp->page;
456 new_bufp = ret.newp;
457 if (!new_bufp)
458 return (-1);
459 np = (uint16 *)new_bufp->page;
460 bufp = ret.nextp;
461 if (!bufp)
462 return (0);
463 cino = (char *)bufp->page;
464 ino = (uint16 *)cino;
465 last_bfp = ret.nextp;
466 } else if (ino[n + 1] == OVFLPAGE) {
467 ov_addr = ino[n];
469 * Fix up the old page -- the extra 2 are the fields
470 * which contained the overflow information.
472 ino[0] -= (moved + 2);
473 FREESPACE(ino) =
474 scopyto - sizeof(uint16) * (ino[0] + 3);
475 OFFSET(ino) = scopyto;
477 bufp = __get_buf(hashp, ov_addr, bufp, 0);
478 if (!bufp)
479 return (-1);
481 ino = (uint16 *)bufp->page;
482 n = 1;
483 scopyto = hashp->BSIZE;
484 moved = 0;
486 if (last_bfp)
487 __free_ovflpage(hashp, last_bfp);
488 last_bfp = bufp;
490 /* Move regular sized pairs of there are any */
491 off = hashp->BSIZE;
492 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
493 cino = (char *)ino;
494 key.data = (uint8 *)cino + ino[n];
495 key.size = off - ino[n];
496 val.data = (uint8 *)cino + ino[n + 1];
497 val.size = ino[n] - ino[n + 1];
498 off = ino[n + 1];
500 if (__call_hash(hashp, (char*)key.data, key.size) == obucket) {
501 /* Keep on old page */
502 if (PAIRFITS(op, (&key), (&val)))
503 putpair((char *)op, &key, &val);
504 else {
505 old_bufp =
506 __add_ovflpage(hashp, old_bufp);
507 if (!old_bufp)
508 return (-1);
509 op = (uint16 *)old_bufp->page;
510 putpair((char *)op, &key, &val);
512 old_bufp->flags |= BUF_MOD;
513 } else {
514 /* Move to new page */
515 if (PAIRFITS(np, (&key), (&val)))
516 putpair((char *)np, &key, &val);
517 else {
518 new_bufp =
519 __add_ovflpage(hashp, new_bufp);
520 if (!new_bufp)
521 return (-1);
522 np = (uint16 *)new_bufp->page;
523 putpair((char *)np, &key, &val);
525 new_bufp->flags |= BUF_MOD;
529 if (last_bfp)
530 __free_ovflpage(hashp, last_bfp);
531 return (0);
535 * Add the given pair to the page
537 * Returns:
538 * 0 ==> OK
539 * 1 ==> failure
541 extern int
542 __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT * val)
544 register uint16 *bp, *sop;
545 int do_expand;
547 bp = (uint16 *)bufp->page;
548 do_expand = 0;
549 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
550 /* Exception case */
551 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
552 /* This is the last page of a big key/data pair
553 and we need to add another page */
554 break;
555 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
556 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
557 if (!bufp)
559 #ifdef DEBUG
560 assert(0);
561 #endif
562 return (-1);
564 bp = (uint16 *)bufp->page;
565 } else
566 /* Try to squeeze key on this page */
567 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
569 squeeze_key(bp, key, val);
571 /* LJM: I added this because I think it was
572 * left out on accident.
573 * if this isn't incremented nkeys will not
574 * be the actual number of keys in the db.
576 hashp->NKEYS++;
577 return (0);
579 } else {
580 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
581 if (!bufp)
583 #ifdef DEBUG
584 assert(0);
585 #endif
586 return (-1);
588 bp = (uint16 *)bufp->page;
591 if (PAIRFITS(bp, key, val))
592 putpair(bufp->page, key, (DBT *)val);
593 else {
594 do_expand = 1;
595 bufp = __add_ovflpage(hashp, bufp);
596 if (!bufp)
598 #ifdef DEBUG
599 assert(0);
600 #endif
601 return (-1);
603 sop = (uint16 *)bufp->page;
605 if (PAIRFITS(sop, key, val))
606 putpair((char *)sop, key, (DBT *)val);
607 else
608 if (__big_insert(hashp, bufp, key, val))
610 #ifdef DEBUG
611 assert(0);
612 #endif
613 return (-1);
616 bufp->flags |= BUF_MOD;
618 * If the average number of keys per bucket exceeds the fill factor,
619 * expand the table.
621 hashp->NKEYS++;
622 if (do_expand ||
623 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
624 return (__expand_table(hashp));
625 return (0);
630 * Returns:
631 * pointer on success
632 * NULL on error
634 extern BUFHEAD *
635 __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
637 register uint16 *sp;
638 uint16 ndx, ovfl_num;
639 #ifdef DEBUG1
640 int tmp1, tmp2;
641 #endif
642 sp = (uint16 *)bufp->page;
644 /* Check if we are dynamically determining the fill factor */
645 if (hashp->FFACTOR == DEF_FFACTOR) {
646 hashp->FFACTOR = sp[0] >> 1;
647 if (hashp->FFACTOR < MIN_FFACTOR)
648 hashp->FFACTOR = MIN_FFACTOR;
650 bufp->flags |= BUF_MOD;
651 ovfl_num = overflow_page(hashp);
652 #ifdef DEBUG1
653 tmp1 = bufp->addr;
654 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
655 #endif
656 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
657 return (NULL);
658 bufp->ovfl->flags |= BUF_MOD;
659 #ifdef DEBUG1
660 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
661 tmp1, tmp2, bufp->ovfl->addr);
662 #endif
663 ndx = sp[0];
665 * Since a pair is allocated on a page only if there's room to add
666 * an overflow page, we know that the OVFL information will fit on
667 * the page.
669 sp[ndx + 4] = OFFSET(sp);
670 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
671 sp[ndx + 1] = ovfl_num;
672 sp[ndx + 2] = OVFLPAGE;
673 sp[0] = ndx + 2;
674 #ifdef HASH_STATISTICS
675 hash_overflows++;
676 #endif
677 return (bufp->ovfl);
681 * Returns:
682 * 0 indicates SUCCESS
683 * -1 indicates FAILURE
685 extern int
686 __get_page(HTAB *hashp,
687 char * p,
688 uint32 bucket,
689 int is_bucket,
690 int is_disk,
691 int is_bitmap)
693 register int fd, page;
694 size_t size;
695 int rsize;
696 uint16 *bp;
698 fd = hashp->fp;
699 size = hashp->BSIZE;
701 if ((fd == -1) || !is_disk) {
702 PAGE_INIT(p);
703 return (0);
705 if (is_bucket)
706 page = BUCKET_TO_PAGE(bucket);
707 else
708 page = OADDR_TO_PAGE(bucket);
709 if ((MY_LSEEK(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
710 ((rsize = read(fd, p, size)) == -1))
711 return (-1);
713 bp = (uint16 *)p;
714 if (!rsize)
715 bp[0] = 0; /* We hit the EOF, so initialize a new page */
716 else
717 if ((unsigned)rsize != size) {
718 errno = EFTYPE;
719 return (-1);
722 if (!is_bitmap && !bp[0]) {
723 PAGE_INIT(p);
724 } else {
726 #ifdef DEBUG
727 if(BYTE_ORDER == LITTLE_ENDIAN)
729 int is_little_endian;
730 is_little_endian = BYTE_ORDER;
732 else if(BYTE_ORDER == BIG_ENDIAN)
734 int is_big_endian;
735 is_big_endian = BYTE_ORDER;
737 else
739 assert(0);
741 #endif
743 if (hashp->LORDER != BYTE_ORDER) {
744 register int i, max;
746 if (is_bitmap) {
747 max = hashp->BSIZE >> 2; /* divide by 4 */
748 for (i = 0; i < max; i++)
749 M_32_SWAP(((int *)p)[i]);
750 } else {
751 M_16_SWAP(bp[0]);
752 max = bp[0] + 2;
754 /* bound the size of max by
755 * the maximum number of entries
756 * in the array
758 if((unsigned)max > (size / sizeof(uint16)))
759 return(DATABASE_CORRUPTED_ERROR);
761 /* do the byte order swap
763 for (i = 1; i <= max; i++)
764 M_16_SWAP(bp[i]);
768 /* check the validity of the page here
769 * (after doing byte order swaping if necessary)
771 if(!is_bitmap && bp[0] != 0)
773 uint16 num_keys = bp[0];
774 uint16 offset;
775 uint16 i;
777 /* bp[0] is supposed to be the number of
778 * entries currently in the page. If
779 * bp[0] is too large (larger than the whole
780 * page) then the page is corrupted
782 if(bp[0] > (size / sizeof(uint16)))
783 return(DATABASE_CORRUPTED_ERROR);
785 /* bound free space */
786 if(FREESPACE(bp) > size)
787 return(DATABASE_CORRUPTED_ERROR);
789 /* check each key and data offset to make
790 * sure they are all within bounds they
791 * should all be less than the previous
792 * offset as well.
794 offset = size;
795 for(i=1 ; i <= num_keys; i+=2)
797 /* ignore overflow pages etc. */
798 if(bp[i+1] >= REAL_KEY)
801 if(bp[i] > offset || bp[i+1] > bp[i])
802 return(DATABASE_CORRUPTED_ERROR);
804 offset = bp[i+1];
806 else
808 /* there are no other valid keys after
809 * seeing a non REAL_KEY
811 break;
816 return (0);
820 * Write page p to disk
822 * Returns:
823 * 0 ==> OK
824 * -1 ==>failure
826 extern int
827 __put_page(HTAB *hashp, char *p, uint32 bucket, int is_bucket, int is_bitmap)
829 register int fd, page;
830 size_t size;
831 int wsize;
832 off_t offset;
834 size = hashp->BSIZE;
835 if ((hashp->fp == -1) && open_temp(hashp))
836 return (-1);
837 fd = hashp->fp;
839 if (hashp->LORDER != BYTE_ORDER) {
840 register int i;
841 register int max;
843 if (is_bitmap) {
844 max = hashp->BSIZE >> 2; /* divide by 4 */
845 for (i = 0; i < max; i++)
846 M_32_SWAP(((int *)p)[i]);
847 } else {
848 max = ((uint16 *)p)[0] + 2;
850 /* bound the size of max by
851 * the maximum number of entries
852 * in the array
854 if((unsigned)max > (size / sizeof(uint16)))
855 return(DATABASE_CORRUPTED_ERROR);
857 for (i = 0; i <= max; i++)
858 M_16_SWAP(((uint16 *)p)[i]);
863 if (is_bucket)
864 page = BUCKET_TO_PAGE(bucket);
865 else
866 page = OADDR_TO_PAGE(bucket);
867 offset = (off_t)page << hashp->BSHIFT;
868 if ((MY_LSEEK(fd, offset, SEEK_SET) == -1) ||
869 ((wsize = write(fd, p, size)) == -1))
870 /* Errno is set */
871 return (-1);
872 if ((unsigned)wsize != size) {
873 errno = EFTYPE;
874 return (-1);
876 #if defined(_WIN32) || defined(_WINDOWS)
877 if (offset + size > hashp->file_size) {
878 hashp->updateEOF = 1;
880 #endif
881 /* put the page back the way it was so that it isn't byteswapped
882 * if it remains in memory - LJM
884 if (hashp->LORDER != BYTE_ORDER) {
885 register int i;
886 register int max;
888 if (is_bitmap) {
889 max = hashp->BSIZE >> 2; /* divide by 4 */
890 for (i = 0; i < max; i++)
891 M_32_SWAP(((int *)p)[i]);
892 } else {
893 uint16 *bp = (uint16 *)p;
895 M_16_SWAP(bp[0]);
896 max = bp[0] + 2;
898 /* no need to bound the size if max again
899 * since it was done already above
902 /* do the byte order re-swap
904 for (i = 1; i <= max; i++)
905 M_16_SWAP(bp[i]);
909 return (0);
912 #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
914 * Initialize a new bitmap page. Bitmap pages are left in memory
915 * once they are read in.
917 extern int
918 __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
920 uint32 *ip;
921 size_t clearbytes, clearints;
923 if ((ip = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
924 return (1);
925 hashp->nmaps++;
926 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
927 clearbytes = clearints << INT_TO_BYTE;
928 (void)memset((char *)ip, 0, clearbytes);
929 (void)memset(((char *)ip) + clearbytes, 0xFF,
930 hashp->BSIZE - clearbytes);
931 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
932 SETBIT(ip, 0);
933 hashp->BITMAPS[ndx] = (uint16)pnum;
934 hashp->mapp[ndx] = ip;
935 return (0);
938 static uint32
939 first_free(uint32 map)
941 register uint32 i, mask;
943 mask = 0x1;
944 for (i = 0; i < BITS_PER_MAP; i++) {
945 if (!(mask & map))
946 return (i);
947 mask = mask << 1;
949 return (i);
952 static uint16
953 overflow_page(HTAB *hashp)
955 register uint32 *freep=NULL;
956 register int max_free, offset, splitnum;
957 uint16 addr;
958 uint32 i;
959 int bit, first_page, free_bit, free_page, in_use_bits, j;
960 #ifdef DEBUG2
961 int tmp1, tmp2;
962 #endif
963 splitnum = hashp->OVFL_POINT;
964 max_free = hashp->SPARES[splitnum];
966 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
967 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
969 /* Look through all the free maps to find the first free block */
970 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
971 for ( i = first_page; i <= (unsigned)free_page; i++ ) {
972 if (!(freep = (uint32 *)hashp->mapp[i]) &&
973 !(freep = fetch_bitmap(hashp, i)))
974 return (0);
975 if (i == (unsigned)free_page)
976 in_use_bits = free_bit;
977 else
978 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
980 if (i == (unsigned)first_page) {
981 bit = hashp->LAST_FREED &
982 ((hashp->BSIZE << BYTE_SHIFT) - 1);
983 j = bit / BITS_PER_MAP;
984 bit = bit & ~(BITS_PER_MAP - 1);
985 } else {
986 bit = 0;
987 j = 0;
989 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
990 if (freep[j] != ALL_SET)
991 goto found;
994 /* No Free Page Found */
995 hashp->LAST_FREED = hashp->SPARES[splitnum];
996 hashp->SPARES[splitnum]++;
997 offset = hashp->SPARES[splitnum] -
998 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
1000 #define OVMSG "HASH: Out of overflow pages. Increase page size\n"
1001 if (offset > SPLITMASK) {
1002 if (++splitnum >= NCACHED) {
1003 #ifndef macintosh
1004 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
1005 #endif
1006 return (0);
1008 hashp->OVFL_POINT = splitnum;
1009 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
1010 hashp->SPARES[splitnum-1]--;
1011 offset = 1;
1014 /* Check if we need to allocate a new bitmap page */
1015 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
1016 free_page++;
1017 if (free_page >= NCACHED) {
1018 #ifndef macintosh
1019 (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
1020 #endif
1021 return (0);
1024 * This is tricky. The 1 indicates that you want the new page
1025 * allocated with 1 clear bit. Actually, you are going to
1026 * allocate 2 pages from this map. The first is going to be
1027 * the map page, the second is the overflow page we were
1028 * looking for. The init_bitmap routine automatically, sets
1029 * the first bit of itself to indicate that the bitmap itself
1030 * is in use. We would explicitly set the second bit, but
1031 * don't have to if we tell init_bitmap not to leave it clear
1032 * in the first place.
1034 if (__ibitmap(hashp,
1035 (int)OADDR_OF(splitnum, offset), 1, free_page))
1036 return (0);
1037 hashp->SPARES[splitnum]++;
1038 #ifdef DEBUG2
1039 free_bit = 2;
1040 #endif
1041 offset++;
1042 if (offset > SPLITMASK) {
1043 if (++splitnum >= NCACHED) {
1044 #ifndef macintosh
1045 (void)write(STDERR_FILENO, OVMSG,
1046 sizeof(OVMSG) - 1);
1047 #endif
1048 return (0);
1050 hashp->OVFL_POINT = splitnum;
1051 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
1052 hashp->SPARES[splitnum-1]--;
1053 offset = 0;
1055 } else {
1057 * Free_bit addresses the last used bit. Bump it to address
1058 * the first available bit.
1060 free_bit++;
1061 SETBIT(freep, free_bit);
1064 /* Calculate address of the new overflow page */
1065 addr = OADDR_OF(splitnum, offset);
1066 #ifdef DEBUG2
1067 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1068 addr, free_bit, free_page);
1069 #endif
1070 return (addr);
1072 found:
1073 bit = bit + first_free(freep[j]);
1074 SETBIT(freep, bit);
1075 #ifdef DEBUG2
1076 tmp1 = bit;
1077 tmp2 = i;
1078 #endif
1080 * Bits are addressed starting with 0, but overflow pages are addressed
1081 * beginning at 1. Bit is a bit addressnumber, so we need to increment
1082 * it to convert it to a page number.
1084 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
1085 if (bit >= hashp->LAST_FREED)
1086 hashp->LAST_FREED = bit - 1;
1088 /* Calculate the split number for this page */
1089 for (i = 0; (i < (unsigned)splitnum) && (bit > hashp->SPARES[i]); i++) {}
1090 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
1091 if (offset >= SPLITMASK)
1092 return (0); /* Out of overflow pages */
1093 addr = OADDR_OF(i, offset);
1094 #ifdef DEBUG2
1095 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
1096 addr, tmp1, tmp2);
1097 #endif
1099 /* Allocate and return the overflow page */
1100 return (addr);
1104 * Mark this overflow page as free.
1106 extern void
1107 __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
1109 uint16 addr;
1110 uint32 *freep;
1111 uint32 bit_address, free_page, free_bit;
1112 uint16 ndx;
1114 if(!obufp || !obufp->addr)
1115 return;
1117 addr = obufp->addr;
1118 #ifdef DEBUG1
1119 (void)fprintf(stderr, "Freeing %d\n", addr);
1120 #endif
1121 ndx = (((uint16)addr) >> SPLITSHIFT);
1122 bit_address =
1123 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
1124 if (bit_address < (uint32)hashp->LAST_FREED)
1125 hashp->LAST_FREED = bit_address;
1126 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
1127 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
1129 if (!(freep = hashp->mapp[free_page]))
1130 freep = fetch_bitmap(hashp, free_page);
1132 #ifdef DEBUG
1134 * This had better never happen. It means we tried to read a bitmap
1135 * that has already had overflow pages allocated off it, and we
1136 * failed to read it from the file.
1138 if (!freep)
1140 assert(0);
1141 return;
1143 #endif
1144 CLRBIT(freep, free_bit);
1145 #ifdef DEBUG2
1146 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
1147 obufp->addr, free_bit, free_page);
1148 #endif
1149 __reclaim_buf(hashp, obufp);
1153 * Returns:
1154 * 0 success
1155 * -1 failure
1157 static int
1158 open_temp(HTAB *hashp)
1160 #ifdef XP_OS2
1161 hashp->fp = mkstemp(NULL);
1162 #else
1163 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1164 sigset_t set, oset;
1165 #endif
1166 #if !defined(macintosh)
1167 char * tmpdir;
1168 size_t len;
1169 char last;
1170 #endif
1171 static const char namestr[] = "/_hashXXXXXX";
1172 char filename[1024];
1174 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1175 /* Block signals; make sure file goes away at process exit. */
1176 (void)sigfillset(&set);
1177 (void)sigprocmask(SIG_BLOCK, &set, &oset);
1178 #endif
1180 filename[0] = 0;
1181 #if defined(macintosh)
1182 strcat(filename, namestr + 1);
1183 #else
1184 tmpdir = getenv("TMP");
1185 if (!tmpdir)
1186 tmpdir = getenv("TMPDIR");
1187 if (!tmpdir)
1188 tmpdir = getenv("TEMP");
1189 if (!tmpdir)
1190 tmpdir = ".";
1191 len = strlen(tmpdir);
1192 if (len && len < (sizeof filename - sizeof namestr)) {
1193 strcpy(filename, tmpdir);
1195 len = strlen(filename);
1196 last = tmpdir[len - 1];
1197 strcat(filename, (last == '/' || last == '\\') ? namestr + 1 : namestr);
1198 #endif
1200 #if defined(_WIN32) || defined(_WINDOWS)
1201 if ((hashp->fp = mkstempflags(filename, _O_BINARY|_O_TEMPORARY)) != -1) {
1202 if (hashp->filename) {
1203 free(hashp->filename);
1205 hashp->filename = strdup(filename);
1206 hashp->is_temp = 1;
1208 #else
1209 if ((hashp->fp = mkstemp(filename)) != -1) {
1210 (void)unlink(filename);
1211 #if !defined(macintosh)
1212 (void)fcntl(hashp->fp, F_SETFD, 1);
1213 #endif
1215 #endif
1217 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
1218 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
1219 #endif
1220 #endif /* !OS2 */
1221 return (hashp->fp != -1 ? 0 : -1);
1225 * We have to know that the key will fit, but the last entry on the page is
1226 * an overflow pair, so we need to shift things.
1228 static void
1229 squeeze_key(uint16 *sp, const DBT * key, const DBT * val)
1231 register char *p;
1232 uint16 free_space, n, off, pageno;
1234 p = (char *)sp;
1235 n = sp[0];
1236 free_space = FREESPACE(sp);
1237 off = OFFSET(sp);
1239 pageno = sp[n - 1];
1240 off -= key->size;
1241 sp[n - 1] = off;
1242 memmove(p + off, key->data, key->size);
1243 off -= val->size;
1244 sp[n] = off;
1245 memmove(p + off, val->data, val->size);
1246 sp[0] = n + 2;
1247 sp[n + 1] = pageno;
1248 sp[n + 2] = OVFLPAGE;
1249 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
1250 OFFSET(sp) = off;
1253 static uint32 *
1254 fetch_bitmap(HTAB *hashp, uint32 ndx)
1256 if (ndx >= (unsigned)hashp->nmaps)
1257 return (NULL);
1258 if ((hashp->mapp[ndx] = (uint32 *)malloc((size_t)hashp->BSIZE)) == NULL)
1259 return (NULL);
1260 if (__get_page(hashp,
1261 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
1262 free(hashp->mapp[ndx]);
1263 hashp->mapp[ndx] = NULL; /* NEW: 9-11-95 */
1264 return (NULL);
1266 return (hashp->mapp[ndx]);
1269 #ifdef DEBUG4
1271 print_chain(int addr)
1273 BUFHEAD *bufp;
1274 short *bp, oaddr;
1276 (void)fprintf(stderr, "%d ", addr);
1277 bufp = __get_buf(hashp, addr, NULL, 0);
1278 bp = (short *)bufp->page;
1279 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
1280 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
1281 oaddr = bp[bp[0] - 1];
1282 (void)fprintf(stderr, "%d ", (int)oaddr);
1283 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
1284 bp = (short *)bufp->page;
1286 (void)fprintf(stderr, "\n");
1288 #endif