Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / search / hash_bigkey.c
blob29a14502a7d6c229993af67a34ac6b0c40b7478e
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_bigkey.c 8.3 (Berkeley) 5/31/94";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
40 * PACKAGE: hash
41 * DESCRIPTION:
42 * Big key/data handling for the hashing package.
44 * ROUTINES:
45 * External
46 * __big_keydata
47 * __big_split
48 * __big_insert
49 * __big_return
50 * __big_delete
51 * __find_last_page
52 * Internal
53 * collect_key
54 * collect_data
57 #include <sys/param.h>
59 #include <errno.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.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 int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
74 static int collect_data(HTAB *, BUFHEAD *, int, int);
77 * Big_insert
79 * You need to do an insert and the key/data pair is too big
81 * Returns:
82 * 0 ==> OK
83 *-1 ==> ERROR
85 extern int
86 __big_insert(
87 HTAB *hashp,
88 BUFHEAD *bufp,
89 const DBT *key,
90 const DBT *val
93 __uint16_t *p;
94 int key_size, n, val_size;
95 __uint16_t space, move_bytes, off;
96 char *cp, *key_data, *val_data;
98 cp = bufp->page; /* Character pointer of p. */
99 p = (__uint16_t *)cp;
101 key_data = (char *)key->data;
102 key_size = key->size;
103 val_data = (char *)val->data;
104 val_size = val->size;
106 /* First move the Key */
107 for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
108 space = FREESPACE(p) - BIGOVERHEAD) {
109 move_bytes = MIN(space, key_size);
110 off = OFFSET(p) - move_bytes;
111 memmove(cp + off, key_data, move_bytes);
112 key_size -= move_bytes;
113 key_data += move_bytes;
114 n = p[0];
115 p[++n] = off;
116 p[0] = ++n;
117 FREESPACE(p) = off - PAGE_META(n);
118 OFFSET(p) = off;
119 p[n] = PARTIAL_KEY;
120 bufp = __add_ovflpage(hashp, bufp);
121 if (!bufp)
122 return (-1);
123 n = p[0];
124 if (!key_size) {
125 if (FREESPACE(p)) {
126 move_bytes = MIN(FREESPACE(p), val_size);
127 off = OFFSET(p) - move_bytes;
128 p[n] = off;
129 memmove(cp + off, val_data, move_bytes);
130 val_data += move_bytes;
131 val_size -= move_bytes;
132 p[n - 2] = FULL_KEY_DATA;
133 FREESPACE(p) = FREESPACE(p) - move_bytes;
134 OFFSET(p) = off;
135 } else
136 p[n - 2] = FULL_KEY;
138 p = (__uint16_t *)bufp->page;
139 cp = bufp->page;
140 bufp->flags |= BUF_MOD;
143 /* Now move the data */
144 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
145 space = FREESPACE(p) - BIGOVERHEAD) {
146 move_bytes = MIN(space, val_size);
148 * Here's the hack to make sure that if the data ends on the
149 * same page as the key ends, FREESPACE is at least one.
151 if (space == val_size && val_size == val->size)
152 move_bytes--;
153 off = OFFSET(p) - move_bytes;
154 memmove(cp + off, val_data, move_bytes);
155 val_size -= move_bytes;
156 val_data += move_bytes;
157 n = p[0];
158 p[++n] = off;
159 p[0] = ++n;
160 FREESPACE(p) = off - PAGE_META(n);
161 OFFSET(p) = off;
162 if (val_size) {
163 p[n] = FULL_KEY;
164 bufp = __add_ovflpage(hashp, bufp);
165 if (!bufp)
166 return (-1);
167 cp = bufp->page;
168 p = (__uint16_t *)cp;
169 } else
170 p[n] = FULL_KEY_DATA;
171 bufp->flags |= BUF_MOD;
173 return (0);
177 * Called when bufp's page contains a partial key (index should be 1)
179 * All pages in the big key/data pair except bufp are freed. We cannot
180 * free bufp because the page pointing to it is lost and we can't get rid
181 * of its pointer.
183 * Returns:
184 * 0 => OK
185 *-1 => ERROR
187 extern int
188 __big_delete(
189 HTAB *hashp,
190 BUFHEAD *bufp
193 BUFHEAD *last_bfp, *rbufp;
194 __uint16_t *bp, pageno;
195 int key_done, n;
197 rbufp = bufp;
198 last_bfp = NULL;
199 bp = (__uint16_t *)bufp->page;
200 pageno = 0;
201 key_done = 0;
203 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
204 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
205 key_done = 1;
208 * If there is freespace left on a FULL_KEY_DATA page, then
209 * the data is short and fits entirely on this page, and this
210 * is the last page.
212 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
213 break;
214 pageno = bp[bp[0] - 1];
215 rbufp->flags |= BUF_MOD;
216 rbufp = __get_buf(hashp, pageno, rbufp, 0);
217 if (last_bfp)
218 __free_ovflpage(hashp, last_bfp);
219 last_bfp = rbufp;
220 if (!rbufp)
221 return (-1); /* Error. */
222 bp = (__uint16_t *)rbufp->page;
226 * If we get here then rbufp points to the last page of the big
227 * key/data pair. Bufp points to the first one -- it should now be
228 * empty pointing to the next page after this pair. Can't free it
229 * because we don't have the page pointing to it.
232 /* This is information from the last page of the pair. */
233 n = bp[0];
234 pageno = bp[n - 1];
236 /* Now, bp is the first page of the pair. */
237 bp = (__uint16_t *)bufp->page;
238 if (n > 2) {
239 /* There is an overflow page. */
240 bp[1] = pageno;
241 bp[2] = OVFLPAGE;
242 bufp->ovfl = rbufp->ovfl;
243 } else
244 /* This is the last page. */
245 bufp->ovfl = NULL;
246 n -= 2;
247 bp[0] = n;
248 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
249 OFFSET(bp) = hashp->BSIZE - 1;
251 bufp->flags |= BUF_MOD;
252 if (rbufp)
253 __free_ovflpage(hashp, rbufp);
254 if (last_bfp != rbufp)
255 __free_ovflpage(hashp, last_bfp);
257 hashp->NKEYS--;
258 return (0);
261 * Returns:
262 * 0 = key not found
263 * -1 = get next overflow page
264 * -2 means key not found and this is big key/data
265 * -3 error
267 extern int
268 __find_bigpair(
269 HTAB *hashp,
270 BUFHEAD *bufp,
271 int ndx,
272 char *key,
273 int size
276 __uint16_t *bp;
277 char *p;
278 int ksize;
279 __uint16_t bytes;
280 char *kkey;
282 bp = (__uint16_t *)bufp->page;
283 p = bufp->page;
284 ksize = size;
285 kkey = key;
287 for (bytes = hashp->BSIZE - bp[ndx];
288 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
289 bytes = hashp->BSIZE - bp[ndx]) {
290 if (memcmp(p + bp[ndx], kkey, bytes))
291 return (-2);
292 kkey += bytes;
293 ksize -= bytes;
294 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
295 if (!bufp)
296 return (-3);
297 p = bufp->page;
298 bp = (__uint16_t *)p;
299 ndx = 1;
302 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
303 #ifdef HASH_STATISTICS
304 ++hash_collisions;
305 #endif
306 return (-2);
307 } else
308 return (ndx);
312 * Given the buffer pointer of the first overflow page of a big pair,
313 * find the end of the big pair
315 * This will set bpp to the buffer header of the last page of the big pair.
316 * It will return the pageno of the overflow page following the last page
317 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
318 * bucket)
320 extern __uint16_t
321 __find_last_page(
322 HTAB *hashp,
323 BUFHEAD **bpp
326 BUFHEAD *bufp;
327 __uint16_t *bp, pageno;
328 int n;
330 bufp = *bpp;
331 bp = (__uint16_t *)bufp->page;
332 for (;;) {
333 n = bp[0];
336 * This is the last page if: the tag is FULL_KEY_DATA and
337 * either only 2 entries OVFLPAGE marker is explicit there
338 * is freespace on the page.
340 if (bp[2] == FULL_KEY_DATA &&
341 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
342 break;
344 pageno = bp[n - 1];
345 bufp = __get_buf(hashp, pageno, bufp, 0);
346 if (!bufp)
347 return (0); /* Need to indicate an error! */
348 bp = (__uint16_t *)bufp->page;
351 *bpp = bufp;
352 if (bp[0] > 2)
353 return (bp[3]);
354 else
355 return (0);
359 * Return the data for the key/data pair that begins on this page at this
360 * index (index should always be 1).
362 extern int
363 __big_return(
364 HTAB *hashp,
365 BUFHEAD *bufp,
366 int ndx,
367 DBT *val,
368 int set_current
371 BUFHEAD *save_p;
372 __uint16_t *bp, len, off, save_addr;
373 char *tp;
375 bp = (__uint16_t *)bufp->page;
376 while (bp[ndx + 1] == PARTIAL_KEY) {
377 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
378 if (!bufp)
379 return (-1);
380 bp = (__uint16_t *)bufp->page;
381 ndx = 1;
384 if (bp[ndx + 1] == FULL_KEY) {
385 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
386 if (!bufp)
387 return (-1);
388 bp = (__uint16_t *)bufp->page;
389 save_p = bufp;
390 save_addr = save_p->addr;
391 off = bp[1];
392 len = 0;
393 } else
394 if (!FREESPACE(bp)) {
396 * This is a hack. We can't distinguish between
397 * FULL_KEY_DATA that contains complete data or
398 * incomplete data, so we require that if the data
399 * is complete, there is at least 1 byte of free
400 * space left.
402 off = bp[bp[0]];
403 len = bp[1] - off;
404 save_p = bufp;
405 save_addr = bufp->addr;
406 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
407 if (!bufp)
408 return (-1);
409 bp = (__uint16_t *)bufp->page;
410 } else {
411 /* The data is all on one page. */
412 tp = (char *)bp;
413 off = bp[bp[0]];
414 val->data = (u_char *)tp + off;
415 val->size = bp[1] - off;
416 if (set_current) {
417 if (bp[0] == 2) { /* No more buckets in
418 * chain */
419 hashp->cpage = NULL;
420 hashp->cbucket++;
421 hashp->cndx = 1;
422 } else {
423 hashp->cpage = __get_buf(hashp,
424 bp[bp[0] - 1], bufp, 0);
425 if (!hashp->cpage)
426 return (-1);
427 hashp->cndx = 1;
428 if (!((__uint16_t *)
429 hashp->cpage->page)[0]) {
430 hashp->cbucket++;
431 hashp->cpage = NULL;
435 return (0);
438 val->size = collect_data(hashp, bufp, (int)len, set_current);
439 if (val->size == -1)
440 return (-1);
441 if (save_p->addr != save_addr) {
442 /* We are pretty short on buffers. */
443 errno = EINVAL; /* OUT OF BUFFERS */
444 return (-1);
446 memmove(hashp->tmp_buf, (save_p->page) + off, len);
447 val->data = (u_char *)hashp->tmp_buf;
448 return (0);
451 * Count how big the total datasize is by recursing through the pages. Then
452 * allocate a buffer and copy the data as you recurse up.
454 static int
455 collect_data(
456 HTAB *hashp,
457 BUFHEAD *bufp,
458 int len,
459 int set
462 __uint16_t *bp;
463 char *p;
464 BUFHEAD *xbp;
465 __uint16_t save_addr;
466 int mylen, totlen;
468 p = bufp->page;
469 bp = (__uint16_t *)p;
470 mylen = hashp->BSIZE - bp[1];
471 save_addr = bufp->addr;
473 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
474 totlen = len + mylen;
475 if (hashp->tmp_buf)
476 free(hashp->tmp_buf);
477 if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
478 return (-1);
479 if (set) {
480 hashp->cndx = 1;
481 if (bp[0] == 2) { /* No more buckets in chain */
482 hashp->cpage = NULL;
483 hashp->cbucket++;
484 } else {
485 hashp->cpage =
486 __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
487 if (!hashp->cpage)
488 return (-1);
489 else if (!((__uint16_t *)hashp->cpage->page)[0]) {
490 hashp->cbucket++;
491 hashp->cpage = NULL;
495 } else {
496 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
497 if (!xbp || ((totlen =
498 collect_data(hashp, xbp, len + mylen, set)) < 1))
499 return (-1);
501 if (bufp->addr != save_addr) {
502 errno = EINVAL; /* Out of buffers. */
503 return (-1);
505 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
506 return (totlen);
510 * Fill in the key and data for this big pair.
512 extern int
513 __big_keydata(
514 HTAB *hashp,
515 BUFHEAD *bufp,
516 DBT *key,
517 DBT *val,
518 int set
521 key->size = collect_key(hashp, bufp, 0, val, set);
522 if (key->size == -1)
523 return (-1);
524 key->data = (u_char *)hashp->tmp_key;
525 return (0);
529 * Count how big the total key size is by recursing through the pages. Then
530 * collect the data, allocate a buffer and copy the key as you recurse up.
532 static int
533 collect_key(
534 HTAB *hashp,
535 BUFHEAD *bufp,
536 int len,
537 DBT *val,
538 int set
541 BUFHEAD *xbp;
542 char *p;
543 int mylen, totlen;
544 __uint16_t *bp, save_addr;
546 p = bufp->page;
547 bp = (__uint16_t *)p;
548 mylen = hashp->BSIZE - bp[1];
550 save_addr = bufp->addr;
551 totlen = len + mylen;
552 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
553 if (hashp->tmp_key != NULL)
554 free(hashp->tmp_key);
555 if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
556 return (-1);
557 if (__big_return(hashp, bufp, 1, val, set))
558 return (-1);
559 } else {
560 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
561 if (!xbp || ((totlen =
562 collect_key(hashp, xbp, totlen, val, set)) < 1))
563 return (-1);
565 if (bufp->addr != save_addr) {
566 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
567 return (-1);
569 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
570 return (totlen);
574 * Returns:
575 * 0 => OK
576 * -1 => error
578 extern int
579 __big_split(
580 HTAB *hashp,
581 BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */
582 BUFHEAD *np, /* Pointer to new bucket page */
583 /* Pointer to first page containing the big key/data */
584 BUFHEAD *big_keyp,
585 int addr, /* Address of big_keyp */
586 __uint32_t obucket,/* Old Bucket */
587 SPLIT_RETURN *ret
590 BUFHEAD *tmpp;
591 __uint16_t *tp;
592 BUFHEAD *bp;
593 DBT key, val;
594 __uint32_t change;
595 __uint16_t free_space, n, off;
597 bp = big_keyp;
599 /* Now figure out where the big key/data goes */
600 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
601 return (-1);
602 change = (__call_hash(hashp, key.data, key.size) != obucket);
604 if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
605 if (!(ret->nextp =
606 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
607 return (-1);;
608 } else
609 ret->nextp = NULL;
611 /* Now make one of np/op point to the big key/data pair */
612 #ifdef DEBUG
613 assert(np->ovfl == NULL);
614 #endif
615 if (change)
616 tmpp = np;
617 else
618 tmpp = op;
620 tmpp->flags |= BUF_MOD;
621 #ifdef DEBUG1
622 (void)fprintf(stderr,
623 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
624 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
625 #endif
626 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
627 tp = (__uint16_t *)tmpp->page;
628 #ifdef DEBUG
629 assert(FREESPACE(tp) >= OVFLSIZE);
630 #endif
631 n = tp[0];
632 off = OFFSET(tp);
633 free_space = FREESPACE(tp);
634 tp[++n] = (__uint16_t)addr;
635 tp[++n] = OVFLPAGE;
636 tp[0] = n;
637 OFFSET(tp) = off;
638 FREESPACE(tp) = free_space - OVFLSIZE;
641 * Finally, set the new and old return values. BIG_KEYP contains a
642 * pointer to the last page of the big key_data pair. Make sure that
643 * big_keyp has no following page (2 elements) or create an empty
644 * following page.
647 ret->newp = np;
648 ret->oldp = op;
650 tp = (__uint16_t *)big_keyp->page;
651 big_keyp->flags |= BUF_MOD;
652 if (tp[0] > 2) {
654 * There may be either one or two offsets on this page. If
655 * there is one, then the overflow page is linked on normally
656 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
657 * the second offset and needs to get stuffed in after the
658 * next overflow page is added.
660 n = tp[4];
661 free_space = FREESPACE(tp);
662 off = OFFSET(tp);
663 tp[0] -= 2;
664 FREESPACE(tp) = free_space + OVFLSIZE;
665 OFFSET(tp) = off;
666 tmpp = __add_ovflpage(hashp, big_keyp);
667 if (!tmpp)
668 return (-1);
669 tp[4] = n;
670 } else
671 tmpp = big_keyp;
673 if (change)
674 ret->newp = tmpp;
675 else
676 ret->oldp = tmpp;
677 return (0);