37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] =
"@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
70#include "../include/db.h"
84#define PAGE_INIT(P) { \
85 ((u_int16_t *)(P))[0] = 0; \
86 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
87 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
108 memmove(p + off, key->
data, key->
size);
113 memmove(p + off,
val->data,
val->size);
118 bp[n + 1] = off - ((n + 3) *
sizeof(
u_int16_t));
143 newoff = bp[ndx - 1];
145 newoff = hashp->BSIZE;
146 pairlen = newoff - bp[ndx + 1];
148 if (ndx != (n - 1)) {
151 register char *src = bufp->
page + (int)
OFFSET(bp);
152 register char *dst = src + (int)pairlen;
153 memmove(dst, src, bp[ndx + 1] -
OFFSET(bp));
156 for (i = ndx + 2; i <= n; i += 2) {
159 bp[i - 1] = bp[i + 1];
161 bp[i - 2] = bp[i] + pairlen;
162 bp[i - 1] = bp[i + 1] + pairlen;
167 bp[n] =
OFFSET(bp) + pairlen;
168 bp[n - 1] = bp[n + 1] + pairlen + 2 *
sizeof(
u_int16_t);
185 register BUFHEAD *new_bufp, *old_bufp;
196 if (old_bufp ==
NULL)
199 if (new_bufp ==
NULL)
210 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
213 (
int)copyto, (
int)moved);
214 old_bufp->
flags &= ~BUF_PIN;
215 new_bufp->
flags &= ~BUF_PIN;
219 key.
data = (u_char *)op + ino[n];
220 key.
size = off - ino[n];
226 copyto = ino[n + 1] + diff;
227 memmove(op + copyto, op + ino[n + 1],
229 ino[ndx] = copyto + ino[n] - ino[n + 1];
230 ino[ndx + 1] = copyto;
236 val.data = (u_char *)op + ino[n + 1];
237 val.size = ino[n] - ino[n + 1];
251 (void)fprintf(stderr,
"split %d/%d\n",
256 old_bufp->
flags &= ~BUF_PIN;
257 new_bufp->
flags &= ~BUF_PIN;
306 new_bufp, bufp, bufp->
addr, obucket, &ret))
319 cino = (
char *)bufp->
page;
321 last_bfp = ret.
nextp;
322 }
else if (ino[n + 1] ==
OVFLPAGE) {
328 ino[0] -= (moved + 2);
330 scopyto -
sizeof(
u_int16_t) * (ino[0] + 3);
333 bufp =
__get_buf(hashp, ov_addr, bufp, 0);
339 scopyto = hashp->BSIZE;
348 for (n = 1; (n < ino[0]) && (ino[n + 1] >=
REAL_KEY); n += 2) {
350 key.
data = (u_char *)cino + ino[n];
351 key.
size = off - ino[n];
352 val.data = (u_char *)cino + ino[n + 1];
353 val.size = ino[n] - ino[n + 1];
415 bufp =
__get_buf(hashp, bp[bp[0] - 1], bufp, 0);
425 bufp =
__get_buf(hashp, bp[bp[0] - 1], bufp, 0);
453 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
478 hashp->FFACTOR = sp[0] >> 1;
488 if (!ovfl_num || !(bufp->
ovfl =
__get_buf(hashp, ovfl_num, bufp, 1)))
492 (void)fprintf(stderr,
"ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
503 sp[ndx + 1] = ovfl_num;
506#ifdef HASH_STATISTICS
522 int is_bucket, is_disk, is_bitmap;
524 register int fd, page, size;
531 if ((fd == -1) || !is_disk) {
539 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
540 ((rsize = read(fd, p, size)) == -1))
550 if (!is_bitmap && !bp[0]) {
557 max = hashp->BSIZE >> 2;
558 for (i = 0; i <
max; i++)
559 M_32_SWAP(((
int *)p)[i]);
563 for (i = 1; i <=
max; i++)
582 int is_bucket, is_bitmap;
584 register int fd, page, size;
597 max = hashp->BSIZE >> 2;
598 for (i = 0; i <
max; i++)
599 M_32_SWAP(((
int *)p)[i]);
602 for (i = 0; i <=
max; i++)
610 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
611 ((wsize = write(fd, p, size)) == -1))
621#define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
629 int pnum, nbits, ndx;
632 int clearbytes, clearints;
639 (void)memset((
char *)ip, 0, clearbytes);
640 (void)memset(((
char *)ip) + clearbytes, 0xFF,
641 hashp->BSIZE - clearbytes);
645 hashp->
mapp[ndx] = ip;
669 register int max_free, offset, splitnum;
671 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
675 splitnum = hashp->OVFL_POINT;
676 max_free = hashp->SPARES[splitnum];
678 free_page = (max_free - 1) >> (hashp->BSHIFT +
BYTE_SHIFT);
679 free_bit = (max_free - 1) & ((hashp->BSIZE <<
BYTE_SHIFT) - 1);
682 first_page = hashp->LAST_FREED >>(hashp->BSHIFT +
BYTE_SHIFT);
683 for ( i = first_page; i <= free_page; i++ ) {
688 in_use_bits = free_bit;
690 in_use_bits = (hashp->BSIZE <<
BYTE_SHIFT) - 1;
692 if (i == first_page) {
693 bit = hashp->LAST_FREED &
707 hashp->LAST_FREED = hashp->SPARES[splitnum];
708 hashp->SPARES[splitnum]++;
709 offset = hashp->SPARES[splitnum] -
710 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
712#define OVMSG "HASH: Out of overflow pages. Increase page size\n"
715 if (write(STDERR_FILENO,
OVMSG,
sizeof(
OVMSG) - 1) < 0) {
719 hashp->OVFL_POINT = splitnum;
720 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
721 hashp->SPARES[splitnum-1]--;
726 if (free_bit == (hashp->BSIZE <<
BYTE_SHIFT) - 1) {
729 if (write(STDERR_FILENO,
OVMSG,
sizeof(
OVMSG) - 1) < 0) {
745 (
int)
OADDR_OF(splitnum, offset), 1, free_page))
747 hashp->SPARES[splitnum]++;
754 if (write(STDERR_FILENO,
OVMSG,
sizeof(
OVMSG) - 1) < 0) {
758 hashp->OVFL_POINT = splitnum;
759 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
760 hashp->SPARES[splitnum-1]--;
775 (void)fprintf(stderr,
"OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
776 addr, free_bit, free_page);
792 bit = 1 + bit + (i * (hashp->BSIZE <<
BYTE_SHIFT));
793 if (bit >= hashp->LAST_FREED)
794 hashp->LAST_FREED = bit - 1;
797 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
798 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
803 (void)fprintf(stderr,
"OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
821 int bit_address, free_page, free_bit;
826 (void)fprintf(stderr,
"Freeing %d\n", addr);
830 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr &
SPLITMASK) - 1;
832 hashp->LAST_FREED = bit_address;
833 free_page = (bit_address >> (hashp->BSHIFT +
BYTE_SHIFT));
834 free_bit = bit_address & ((hashp->BSIZE <<
BYTE_SHIFT) - 1);
836 if (!(freep = hashp->
mapp[free_page]))
849 (void)fprintf(stderr,
"FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
850 obufp->
addr, free_bit, free_page);
865 static char namestr[] =
"_hashXXXXXX";
868 (void)sigfillset(&
set);
869 (void)sigprocmask(SIG_BLOCK, &
set, &oset);
870 if ((hashp->
fp = mkstemp(namestr)) != -1) {
871 (void)unlink(namestr);
872 (void)fcntl(hashp->
fp, F_SETFD, 1);
874 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)
NULL);
875 return (hashp->
fp != -1 ? 0 : -1);
898 memmove(p + off, key->
data, key->
size);
901 memmove(p + off,
val->data,
val->size);
914 if (ndx >= hashp->
nmaps)
919 (
char *)hashp->
mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
923 return (hashp->
mapp[ndx]);
934 (void)fprintf(stderr,
"%d ", addr);
936 bp = (
short *)bufp->
page;
938 ((bp[0] > 2) && bp[2] <
REAL_KEY))) {
939 oaddr = bp[bp[0] - 1];
940 (void)fprintf(stderr,
"%d ", (
int)oaddr);
941 bufp =
__get_buf(hashp, (
int)oaddr, bufp, 0);
942 bp = (
short *)bufp->
page;
944 (void)fprintf(stderr,
"\n");
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
int __expand_table(HTAB *hashp)
u_int32_t __call_hash(HTAB *hashp, char *k, int len)
#define BUCKET_TO_PAGE(B)
int __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
int __big_delete(HTAB *hashp, BUFHEAD *bufp)
int __big_split(HTAB *hashp, BUFHEAD *op, BUFHEAD *np, BUFHEAD *big_keyp, int addr, u_int32_t obucket, SPLIT_RETURN *ret)
BUFHEAD * __get_buf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp, int newpage)
void __reclaim_buf(HTAB *hashp, BUFHEAD *bp)
int __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
int __put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
static int open_temp(HTAB *hashp)
int __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
static u_int32_t first_free(u_int32_t map)
static u_int32_t *fetch_bitmap __P((HTAB *, int))
static u_int32_t * fetch_bitmap(HTAB *hashp, int ndx)
int __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
static u_int16_t overflow_page(HTAB *hashp)
static int ugly_split(HTAB *hashp, u_int32_t obucket, BUFHEAD *old_bufp, BUFHEAD *new_bufp, int copyto, int moved)
static void squeeze_key(u_int16_t *sp, const DBT *key, const DBT *val)
int __split_page(HTAB *hashp, u_int32_t obucket, u_int32_t nbucket)
BUFHEAD * __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
static void putpair(char *p, const DBT *key, const DBT *val)
int __get_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk, int is_bitmap)
void __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
#define PAIRFITS(P, K, D)
u_int32_t * mapp[NCACHED]