Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
Macros | Functions
hash_page.c File Reference
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../include/db.h"
#include "hash.h"
#include "page.h"
#include "extern.h"
Include dependency graph for hash_page.c:

Go to the source code of this file.

Macros

#define BYTE_MASK   ((1 << INT_BYTE_SHIFT) -1)
 
#define OVMSG   "HASH: Out of overflow pages. Increase page size\n"
 
#define PAGE_INIT(P)
 

Functions

BUFHEAD__add_ovflpage (HTAB *hashp, BUFHEAD *bufp)
 
int __addel (HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
 
int __delpair (HTAB *hashp, BUFHEAD *bufp, int ndx)
 
void __free_ovflpage (HTAB *hashp, BUFHEAD *obufp)
 
int __get_page (HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk, int is_bitmap)
 
int __ibitmap (HTAB *hashp, int pnum, int nbits, int ndx)
 
static void putpair __P ((char *, const DBT *, const DBT *))
 
static int open_temp __P ((HTAB *))
 
static u_int32_t *fetch_bitmap __P ((HTAB *, int))
 
static int ugly_split __P ((HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int))
 
static void squeeze_key __P ((u_int16_t *, const DBT *, const DBT *))
 
static u_int32_t first_free __P ((u_int32_t))
 
int __put_page (HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
 
int __split_page (HTAB *hashp, u_int32_t obucket, u_int32_t nbucket)
 
static u_int32_tfetch_bitmap (HTAB *hashp, int ndx)
 
static u_int32_t first_free (u_int32_t map)
 
static int open_temp (HTAB *hashp)
 
static u_int16_t overflow_page (HTAB *hashp)
 
static void putpair (char *p, const DBT *key, const DBT *val)
 
static void squeeze_key (u_int16_t *sp, const DBT *key, const DBT *val)
 
static int ugly_split (HTAB *hashp, u_int32_t obucket, BUFHEAD *old_bufp, BUFHEAD *new_bufp, int copyto, int moved)
 

Macro Definition Documentation

◆ BYTE_MASK

#define BYTE_MASK   ((1 << INT_BYTE_SHIFT) -1)

Definition at line 621 of file hash_page.c.

◆ OVMSG

#define OVMSG   "HASH: Out of overflow pages. Increase page size\n"

◆ PAGE_INIT

#define PAGE_INIT (   P)
Value:
{ \
((u_int16_t *)(P))[0] = 0; \
((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
((u_int16_t *)(P))[2] = hashp->BSIZE; \
}
unsigned short u_int16_t
#define P(protos)
Definition: proto.h:51

Definition at line 84 of file hash_page.c.

Function Documentation

◆ __add_ovflpage()

BUFHEAD * __add_ovflpage ( HTAB hashp,
BUFHEAD bufp 
)

Definition at line 465 of file hash_page.c.

468{
469 register u_int16_t *sp;
470 u_int16_t ndx, ovfl_num;
471#ifdef DEBUG1
472 int tmp1, tmp2;
473#endif
474 sp = (u_int16_t *)bufp->page;
475
476 /* Check if we are dynamically determining the fill factor */
477 if (hashp->FFACTOR == DEF_FFACTOR) {
478 hashp->FFACTOR = sp[0] >> 1;
479 if (hashp->FFACTOR < MIN_FFACTOR)
480 hashp->FFACTOR = MIN_FFACTOR;
481 }
482 bufp->flags |= BUF_MOD;
483 ovfl_num = overflow_page(hashp);
484#ifdef DEBUG1
485 tmp1 = bufp->addr;
486 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
487#endif
488 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
489 return (NULL);
490 bufp->ovfl->flags |= BUF_MOD;
491#ifdef DEBUG1
492 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
493 tmp1, tmp2, bufp->ovfl->addr);
494#endif
495 ndx = sp[0];
496 /*
497 * Since a pair is allocated on a page only if there's room to add
498 * an overflow page, we know that the OVFL information will fit on
499 * the page.
500 */
501 sp[ndx + 4] = OFFSET(sp);
502 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
503 sp[ndx + 1] = ovfl_num;
504 sp[ndx + 2] = OVFLPAGE;
505 sp[0] = ndx + 2;
506#ifdef HASH_STATISTICS
507 hash_overflows++;
508#endif
509 return (bufp->ovfl);
510}
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
#define OVFLPAGE
Definition: hash.h:266
#define MIN_FFACTOR
Definition: hash.h:133
#define DEF_FFACTOR
Definition: hash.h:132
#define BUF_MOD
Definition: hash.h:54
BUFHEAD * __get_buf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp, int newpage)
Definition: hash_buf.c:105
static u_int16_t overflow_page(HTAB *hashp)
Definition: hash_page.c:665
#define OFFSET(P)
Definition: page.h:81
#define OVFLSIZE
Definition: page.h:79
#define FREESPACE(P)
Definition: page.h:80
#define NULL
Definition: resample.c:96
char * page
Definition: hash.h:52
BUFHEAD * ovfl
Definition: hash.h:50
char flags
Definition: hash.h:53
u_int32_t addr
Definition: hash.h:51

References __get_buf(), _bufhead::addr, BUF_MOD, DEF_FFACTOR, _bufhead::flags, FREESPACE, if(), MIN_FFACTOR, NULL, OFFSET, overflow_page(), _bufhead::ovfl, OVFLPAGE, OVFLSIZE, and _bufhead::page.

Referenced by __addel(), __big_insert(), __big_split(), and ugly_split().

◆ __addel()

int __addel ( HTAB hashp,
BUFHEAD bufp,
const DBT key,
const DBT val 
)

Definition at line 398 of file hash_page.c.

402{
403 register u_int16_t *bp, *sop;
404 int do_expand;
405
406 bp = (u_int16_t *)bufp->page;
407 do_expand = 0;
408 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
409 /* Exception case */
410 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
411 /* This is the last page of a big key/data pair
412 and we need to add another page */
413 break;
414 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
415 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
416 if (!bufp)
417 return (-1);
418 bp = (u_int16_t *)bufp->page;
419 } else
420 /* Try to squeeze key on this page */
421 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
422 squeeze_key(bp, key, val);
423 return (0);
424 } else {
425 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
426 if (!bufp)
427 return (-1);
428 bp = (u_int16_t *)bufp->page;
429 }
430
431 if (PAIRFITS(bp, key, val))
432 putpair(bufp->page, key, val);
433 else {
434 do_expand = 1;
435 bufp = __add_ovflpage(hashp, bufp);
436 if (!bufp)
437 return (-1);
438 sop = (u_int16_t *)bufp->page;
439
440 if (PAIRFITS(sop, key, val))
441 putpair((char *)sop, key, val);
442 else
443 if (__big_insert(hashp, bufp, key, val))
444 return (-1);
445 }
446 bufp->flags |= BUF_MOD;
447 /*
448 * If the average number of keys per bucket exceeds the fill factor,
449 * expand the table.
450 */
451 hashp->NKEYS++;
452 if (do_expand ||
453 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
454 return (__expand_table(hashp));
455 return (0);
456}
while(1)
Definition: ast_expr2f.c:880
int __expand_table(HTAB *hashp)
Definition: hash.c:815
#define FULL_KEY_DATA
Definition: hash.h:269
#define REAL_KEY
Definition: hash.h:270
int __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
Definition: hash_bigkey.c:88
static void squeeze_key(u_int16_t *sp, const DBT *key, const DBT *val)
Definition: hash_page.c:883
BUFHEAD * __add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
Definition: hash_page.c:465
static void putpair(char *p, const DBT *key, const DBT *val)
Definition: hash_page.c:96
#define PAIRFITS(P, K, D)
Definition: page.h:82
#define PAIRSIZE(K, D)
Definition: page.h:76
Definition: ast_expr2.c:325

References __add_ovflpage(), __big_insert(), __expand_table(), __get_buf(), BUF_MOD, _bufhead::flags, FREESPACE, FULL_KEY_DATA, if(), OVFLPAGE, _bufhead::page, PAIRFITS, PAIRSIZE, putpair(), REAL_KEY, squeeze_key(), and while().

Referenced by hash_access().

◆ __delpair()

int __delpair ( HTAB hashp,
BUFHEAD bufp,
int  ndx 
)

Definition at line 128 of file hash_page.c.

132{
133 register u_int16_t *bp, newoff;
134 register int n;
135 u_int16_t pairlen;
136
137 bp = (u_int16_t *)bufp->page;
138 n = bp[0];
139
140 if (bp[ndx + 1] < REAL_KEY)
141 return (__big_delete(hashp, bufp));
142 if (ndx != 1)
143 newoff = bp[ndx - 1];
144 else
145 newoff = hashp->BSIZE;
146 pairlen = newoff - bp[ndx + 1];
147
148 if (ndx != (n - 1)) {
149 /* Hard Case -- need to shuffle keys */
150 register int i;
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));
154
155 /* Now adjust the pointers */
156 for (i = ndx + 2; i <= n; i += 2) {
157 if (bp[i + 1] == OVFLPAGE) {
158 bp[i - 2] = bp[i];
159 bp[i - 1] = bp[i + 1];
160 } else {
161 bp[i - 2] = bp[i] + pairlen;
162 bp[i - 1] = bp[i + 1] + pairlen;
163 }
164 }
165 }
166 /* Finally adjust the page data */
167 bp[n] = OFFSET(bp) + pairlen;
168 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
169 bp[0] = n - 2;
170 hashp->NKEYS--;
171
172 bufp->flags |= BUF_MOD;
173 return (0);
174}
int __big_delete(HTAB *hashp, BUFHEAD *bufp)
Definition: hash_bigkey.c:188

References __big_delete(), BUF_MOD, _bufhead::flags, if(), OFFSET, OVFLPAGE, _bufhead::page, and REAL_KEY.

Referenced by hash_access().

◆ __free_ovflpage()

void __free_ovflpage ( HTAB hashp,
BUFHEAD obufp 
)

Definition at line 815 of file hash_page.c.

818{
819 register u_int16_t addr;
820 u_int32_t *freep;
821 int bit_address, free_page, free_bit;
822 u_int16_t ndx;
823
824 addr = obufp->addr;
825#ifdef DEBUG1
826 (void)fprintf(stderr, "Freeing %d\n", addr);
827#endif
828 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
829 bit_address =
830 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
831 if (bit_address < hashp->LAST_FREED)
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);
835
836 if (!(freep = hashp->mapp[free_page]))
837 freep = fetch_bitmap(hashp, free_page);
838#ifdef DEBUG
839 /*
840 * This had better never happen. It means we tried to read a bitmap
841 * that has already had overflow pages allocated off it, and we
842 * failed to read it from the file.
843 */
844 if (!freep)
845 assert(0);
846#endif
847 CLRBIT(freep, free_bit);
848#ifdef DEBUG2
849 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
850 obufp->addr, free_bit, free_page);
851#endif
852 __reclaim_buf(hashp, obufp);
853}
#define BYTE_SHIFT
Definition: hash.h:137
#define SPLITSHIFT
Definition: hash.h:165
#define LAST_FREED
Definition: hash.h:280
#define SPLITMASK
Definition: hash.h:166
#define CLRBIT(A, N)
Definition: hash.h:152
void __reclaim_buf(HTAB *hashp, BUFHEAD *bp)
Definition: hash_buf.c:346
static u_int32_t * fetch_bitmap(HTAB *hashp, int ndx)
Definition: hash_page.c:910
unsigned int u_int32_t
u_int32_t * mapp[NCACHED]
Definition: hash.h:112

References __reclaim_buf(), _bufhead::addr, BYTE_SHIFT, CLRBIT, fetch_bitmap(), LAST_FREED, htab::mapp, SPLITMASK, and SPLITSHIFT.

Referenced by __big_delete(), and ugly_split().

◆ __get_page()

int __get_page ( HTAB hashp,
char *  p,
u_int32_t  bucket,
int  is_bucket,
int  is_disk,
int  is_bitmap 
)

Definition at line 518 of file hash_page.c.

523{
524 register int fd, page, size;
525 int rsize;
526 u_int16_t *bp;
527
528 fd = hashp->fp;
529 size = hashp->BSIZE;
530
531 if ((fd == -1) || !is_disk) {
532 PAGE_INIT(p);
533 return (0);
534 }
535 if (is_bucket)
536 page = BUCKET_TO_PAGE(bucket);
537 else
538 page = OADDR_TO_PAGE(bucket);
539 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
540 ((rsize = read(fd, p, size)) == -1))
541 return (-1);
542 bp = (u_int16_t *)p;
543 if (!rsize)
544 bp[0] = 0; /* We hit the EOF, so initialize a new page */
545 else
546 if (rsize != size) {
547 errno = EFTYPE;
548 return (-1);
549 }
550 if (!is_bitmap && !bp[0]) {
551 PAGE_INIT(p);
552 } else
553 if (hashp->LORDER != BYTE_ORDER) {
554 register int i, max;
555
556 if (is_bitmap) {
557 max = hashp->BSIZE >> 2; /* divide by 4 */
558 for (i = 0; i < max; i++)
559 M_32_SWAP(((int *)p)[i]);
560 } else {
561 M_16_SWAP(bp[0]);
562 max = bp[0] + 2;
563 for (i = 1; i <= max; i++)
564 M_16_SWAP(bp[i]);
565 }
566 }
567 return (0);
568}
#define max(a, b)
Definition: f2c.h:198
#define OADDR_TO_PAGE(B)
Definition: hash.h:173
#define BUCKET_TO_PAGE(B)
Definition: hash.h:171
#define PAGE_INIT(P)
Definition: hash_page.c:84
#define BYTE_ORDER
int errno
int fp
Definition: hash.h:99
#define EFTYPE

References BUCKET_TO_PAGE, BYTE_ORDER, EFTYPE, errno, htab::fp, max, OADDR_TO_PAGE, and PAGE_INIT.

Referenced by __get_buf(), and fetch_bitmap().

◆ __ibitmap()

int __ibitmap ( HTAB hashp,
int  pnum,
int  nbits,
int  ndx 
)

Definition at line 627 of file hash_page.c.

630{
631 u_int32_t *ip;
632 int clearbytes, clearints;
633
634 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
635 return (1);
636 hashp->nmaps++;
637 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
638 clearbytes = clearints << INT_TO_BYTE;
639 (void)memset((char *)ip, 0, clearbytes);
640 (void)memset(((char *)ip) + clearbytes, 0xFF,
641 hashp->BSIZE - clearbytes);
642 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
643 SETBIT(ip, 0);
644 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
645 hashp->mapp[ndx] = ip;
646 return (0);
647}
char * malloc()
#define INT_TO_BYTE
Definition: hash.h:138
#define ALL_SET
Definition: hash.h:140
#define INT_BYTE_SHIFT
Definition: hash.h:139
#define SETBIT(A, N)
Definition: hash.h:153
#define BYTE_MASK
Definition: hash_page.c:621
int nmaps
Definition: hash.h:113

References ALL_SET, BYTE_MASK, INT_BYTE_SHIFT, INT_TO_BYTE, malloc(), htab::mapp, htab::nmaps, NULL, and SETBIT.

Referenced by init_htab(), and overflow_page().

◆ __P() [1/6]

static void putpair __P ( (char *, const DBT *, const DBT *)  )
static

◆ __P() [2/6]

static int open_temp __P ( (HTAB *)  )
static

◆ __P() [3/6]

static u_int32_t *fetch_bitmap __P ( (HTAB *, int)  )
static

◆ __P() [4/6]

static int ugly_split __P ( (HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int)  )
static

◆ __P() [5/6]

static void squeeze_key __P ( (u_int16_t *, const DBT *, const DBT *)  )
static

◆ __P() [6/6]

static u_int32_t first_free __P ( (u_int32_t )
static

◆ __put_page()

int __put_page ( HTAB hashp,
char *  p,
u_int32_t  bucket,
int  is_bucket,
int  is_bitmap 
)

Definition at line 578 of file hash_page.c.

583{
584 register int fd, page, size;
585 int wsize;
586
587 size = hashp->BSIZE;
588 if ((hashp->fp == -1) && open_temp(hashp))
589 return (-1);
590 fd = hashp->fp;
591
592 if (hashp->LORDER != BYTE_ORDER) {
593 register int i;
594 register int max;
595
596 if (is_bitmap) {
597 max = hashp->BSIZE >> 2; /* divide by 4 */
598 for (i = 0; i < max; i++)
599 M_32_SWAP(((int *)p)[i]);
600 } else {
601 max = ((u_int16_t *)p)[0] + 2;
602 for (i = 0; i <= max; i++)
603 M_16_SWAP(((u_int16_t *)p)[i]);
604 }
605 }
606 if (is_bucket)
607 page = BUCKET_TO_PAGE(bucket);
608 else
609 page = OADDR_TO_PAGE(bucket);
610 if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
611 ((wsize = write(fd, p, size)) == -1))
612 /* Errno is set */
613 return (-1);
614 if (wsize != size) {
615 errno = EFTYPE;
616 return (-1);
617 }
618 return (0);
619}
static int open_temp(HTAB *hashp)
Definition: hash_page.c:861

References BUCKET_TO_PAGE, BYTE_ORDER, EFTYPE, errno, htab::fp, max, OADDR_TO_PAGE, and open_temp().

Referenced by __buf_free(), flush_meta(), and newbuf().

◆ __split_page()

int __split_page ( HTAB hashp,
u_int32_t  obucket,
u_int32_t  nbucket 
)

Definition at line 181 of file hash_page.c.

184{
185 register BUFHEAD *new_bufp, *old_bufp;
186 register u_int16_t *ino;
187 register char *np;
188 DBT key, val;
189 int n, ndx, retval;
190 u_int16_t copyto, diff, off, moved;
191 char *op;
192
193 copyto = (u_int16_t)hashp->BSIZE;
194 off = (u_int16_t)hashp->BSIZE;
195 old_bufp = __get_buf(hashp, obucket, NULL, 0);
196 if (old_bufp == NULL)
197 return (-1);
198 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
199 if (new_bufp == NULL)
200 return (-1);
201
202 old_bufp->flags |= (BUF_MOD | BUF_PIN);
203 new_bufp->flags |= (BUF_MOD | BUF_PIN);
204
205 ino = (u_int16_t *)(op = old_bufp->page);
206 np = new_bufp->page;
207
208 moved = 0;
209
210 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
211 if (ino[n + 1] < REAL_KEY) {
212 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
213 (int)copyto, (int)moved);
214 old_bufp->flags &= ~BUF_PIN;
215 new_bufp->flags &= ~BUF_PIN;
216 return (retval);
217
218 }
219 key.data = (u_char *)op + ino[n];
220 key.size = off - ino[n];
221
222 if (__call_hash(hashp, key.data, key.size) == obucket) {
223 /* Don't switch page */
224 diff = copyto - off;
225 if (diff) {
226 copyto = ino[n + 1] + diff;
227 memmove(op + copyto, op + ino[n + 1],
228 off - ino[n + 1]);
229 ino[ndx] = copyto + ino[n] - ino[n + 1];
230 ino[ndx + 1] = copyto;
231 } else
232 copyto = ino[n + 1];
233 ndx += 2;
234 } else {
235 /* Switch page */
236 val.data = (u_char *)op + ino[n + 1];
237 val.size = ino[n] - ino[n + 1];
238 putpair(np, &key, &val);
239 moved += 2;
240 }
241
242 off = ino[n + 1];
243 }
244
245 /* Now clean up the page */
246 ino[0] -= moved;
247 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
248 OFFSET(ino) = copyto;
249
250#ifdef DEBUG3
251 (void)fprintf(stderr, "split %d/%d\n",
252 ((u_int16_t *)np)[0] / 2,
253 ((u_int16_t *)op)[0] / 2);
254#endif
255 /* unpin both pages */
256 old_bufp->flags &= ~BUF_PIN;
257 new_bufp->flags &= ~BUF_PIN;
258 return (0);
259}
u_int32_t __call_hash(HTAB *hashp, char *k, int len)
Definition: hash.c:886
#define BUF_PIN
Definition: hash.h:57
static int ugly_split(HTAB *hashp, u_int32_t obucket, BUFHEAD *old_bufp, BUFHEAD *new_bufp, int copyto, int moved)
Definition: hash_page.c:277
static ENTRY retval
Definition: hsearch.c:50
Definition: db.h:85
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
Definition: hash.h:47

References __call_hash(), __get_buf(), BUF_MOD, BUF_PIN, DBT::data, _bufhead::flags, FREESPACE, NULL, OFFSET, _bufhead::page, putpair(), REAL_KEY, retval, DBT::size, and ugly_split().

Referenced by __expand_table().

◆ fetch_bitmap()

static u_int32_t * fetch_bitmap ( HTAB hashp,
int  ndx 
)
static

Definition at line 910 of file hash_page.c.

913{
914 if (ndx >= hashp->nmaps)
915 return (NULL);
916 if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
917 return (NULL);
918 if (__get_page(hashp,
919 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
920 free(hashp->mapp[ndx]);
921 return (NULL);
922 }
923 return (hashp->mapp[ndx]);
924}
void free()
int __get_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk, int is_bitmap)
Definition: hash_page.c:518

References __get_page(), free(), malloc(), htab::mapp, htab::nmaps, and NULL.

Referenced by __free_ovflpage(), and overflow_page().

◆ first_free()

static u_int32_t first_free ( u_int32_t  map)
static

Definition at line 650 of file hash_page.c.

652{
653 register u_int32_t i, mask;
654
655 mask = 0x1;
656 for (i = 0; i < BITS_PER_MAP; i++) {
657 if (!(mask & map))
658 return (i);
659 mask = mask << 1;
660 }
661 return (i);
662}
#define BITS_PER_MAP
Definition: hash.h:149

References BITS_PER_MAP.

Referenced by overflow_page().

◆ open_temp()

static int open_temp ( HTAB hashp)
static

Definition at line 861 of file hash_page.c.

863{
864 sigset_t set, oset;
865 static char namestr[] = "_hashXXXXXX";
866
867 /* Block signals; make sure file goes away at process exit. */
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);
873 }
874 (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
875 return (hashp->fp != -1 ? 0 : -1);
876}
static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:238

References htab::fp, NULL, and set().

Referenced by __put_page().

◆ overflow_page()

static u_int16_t overflow_page ( HTAB hashp)
static

Definition at line 665 of file hash_page.c.

667{
668 register u_int32_t *freep = 0;
669 register int max_free, offset, splitnum;
670 u_int16_t addr;
671 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
672#ifdef DEBUG2
673 int tmp1, tmp2;
674#endif
675 splitnum = hashp->OVFL_POINT;
676 max_free = hashp->SPARES[splitnum];
677
678 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
679 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
680
681 /* Look through all the free maps to find the first free block */
682 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
683 for ( i = first_page; i <= free_page; i++ ) {
684 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
685 !(freep = fetch_bitmap(hashp, i)))
686 return (0);
687 if (i == free_page)
688 in_use_bits = free_bit;
689 else
690 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
691
692 if (i == first_page) {
693 bit = hashp->LAST_FREED &
694 ((hashp->BSIZE << BYTE_SHIFT) - 1);
695 j = bit / BITS_PER_MAP;
696 bit = bit & ~(BITS_PER_MAP - 1);
697 } else {
698 bit = 0;
699 j = 0;
700 }
701 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
702 if (freep[j] != ALL_SET)
703 goto found;
704 }
705
706 /* No Free Page Found */
707 hashp->LAST_FREED = hashp->SPARES[splitnum];
708 hashp->SPARES[splitnum]++;
709 offset = hashp->SPARES[splitnum] -
710 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
711
712#define OVMSG "HASH: Out of overflow pages. Increase page size\n"
713 if (offset > SPLITMASK) {
714 if (++splitnum >= NCACHED) {
715 if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
716 }
717 return (0);
718 }
719 hashp->OVFL_POINT = splitnum;
720 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
721 hashp->SPARES[splitnum-1]--;
722 offset = 1;
723 }
724
725 /* Check if we need to allocate a new bitmap page */
726 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
727 free_page++;
728 if (free_page >= NCACHED) {
729 if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
730 }
731 return (0);
732 }
733 /*
734 * This is tricky. The 1 indicates that you want the new page
735 * allocated with 1 clear bit. Actually, you are going to
736 * allocate 2 pages from this map. The first is going to be
737 * the map page, the second is the overflow page we were
738 * looking for. The init_bitmap routine automatically, sets
739 * the first bit of itself to indicate that the bitmap itself
740 * is in use. We would explicitly set the second bit, but
741 * don't have to if we tell init_bitmap not to leave it clear
742 * in the first place.
743 */
744 if (__ibitmap(hashp,
745 (int)OADDR_OF(splitnum, offset), 1, free_page))
746 return (0);
747 hashp->SPARES[splitnum]++;
748#ifdef DEBUG2
749 free_bit = 2;
750#endif
751 offset++;
752 if (offset > SPLITMASK) {
753 if (++splitnum >= NCACHED) {
754 if (write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1) < 0) {
755 }
756 return (0);
757 }
758 hashp->OVFL_POINT = splitnum;
759 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
760 hashp->SPARES[splitnum-1]--;
761 offset = 0;
762 }
763 } else {
764 /*
765 * Free_bit addresses the last used bit. Bump it to address
766 * the first available bit.
767 */
768 free_bit++;
769 SETBIT(freep, free_bit);
770 }
771
772 /* Calculate address of the new overflow page */
773 addr = OADDR_OF(splitnum, offset);
774#ifdef DEBUG2
775 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
776 addr, free_bit, free_page);
777#endif
778 return (addr);
779
780found:
781 bit = bit + first_free(freep[j]);
782 SETBIT(freep, bit);
783#ifdef DEBUG2
784 tmp1 = bit;
785 tmp2 = i;
786#endif
787 /*
788 * Bits are addressed starting with 0, but overflow pages are addressed
789 * beginning at 1. Bit is a bit addressnumber, so we need to increment
790 * it to convert it to a page number.
791 */
792 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
793 if (bit >= hashp->LAST_FREED)
794 hashp->LAST_FREED = bit - 1;
795
796 /* Calculate the split number for this page */
797 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
798 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
799 if (offset >= SPLITMASK)
800 return (0); /* Out of overflow pages */
801 addr = OADDR_OF(i, offset);
802#ifdef DEBUG2
803 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
804 addr, tmp1, tmp2);
805#endif
806
807 /* Allocate and return the overflow page */
808 return (addr);
809}
#define NCACHED
Definition: hash.h:85
#define OADDR_OF(S, O)
Definition: hash.h:169
static u_int32_t first_free(u_int32_t map)
Definition: hash_page.c:650
#define OVMSG
int __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
Definition: hash_page.c:627

References __ibitmap(), ALL_SET, BITS_PER_MAP, BYTE_SHIFT, fetch_bitmap(), first_free(), htab::mapp, NCACHED, OADDR_OF, OVMSG, SETBIT, and SPLITMASK.

Referenced by __add_ovflpage().

◆ putpair()

static void putpair ( char *  p,
const DBT key,
const DBT val 
)
static

Definition at line 96 of file hash_page.c.

99{
100 register u_int16_t *bp, n, off;
101
102 bp = (u_int16_t *)p;
103
104 /* Enter the key first. */
105 n = bp[0];
106
107 off = OFFSET(bp) - key->size;
108 memmove(p + off, key->data, key->size);
109 bp[++n] = off;
110
111 /* Now the data. */
112 off -= val->size;
113 memmove(p + off, val->data, val->size);
114 bp[++n] = off;
115
116 /* Adjust page info. */
117 bp[0] = n;
118 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
119 bp[n + 2] = off;
120}

References DBT::data, OFFSET, and DBT::size.

Referenced by __addel(), __split_page(), and ugly_split().

◆ squeeze_key()

static void squeeze_key ( u_int16_t sp,
const DBT key,
const DBT val 
)
static

Definition at line 883 of file hash_page.c.

886{
887 register char *p;
888 u_int16_t free_space, n, off, pageno;
889
890 p = (char *)sp;
891 n = sp[0];
892 free_space = FREESPACE(sp);
893 off = OFFSET(sp);
894
895 pageno = sp[n - 1];
896 off -= key->size;
897 sp[n - 1] = off;
898 memmove(p + off, key->data, key->size);
899 off -= val->size;
900 sp[n] = off;
901 memmove(p + off, val->data, val->size);
902 sp[0] = n + 2;
903 sp[n + 1] = pageno;
904 sp[n + 2] = OVFLPAGE;
905 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
906 OFFSET(sp) = off;
907}

References DBT::data, FREESPACE, OFFSET, OVFLPAGE, PAIRSIZE, and DBT::size.

Referenced by __addel().

◆ ugly_split()

static int ugly_split ( HTAB hashp,
u_int32_t  obucket,
BUFHEAD old_bufp,
BUFHEAD new_bufp,
int  copyto,
int  moved 
)
static

Definition at line 277 of file hash_page.c.

283{
284 register BUFHEAD *bufp; /* Buffer header for ino */
285 register u_int16_t *ino; /* Page keys come off of */
286 register u_int16_t *np; /* New page */
287 register u_int16_t *op; /* Page keys go on to if they aren't moving */
288
289 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
290 DBT key, val;
291 SPLIT_RETURN ret;
292 u_int16_t n, off, ov_addr, scopyto;
293 char *cino; /* Character value of ino */
294
295 bufp = old_bufp;
296 ino = (u_int16_t *)old_bufp->page;
297 np = (u_int16_t *)new_bufp->page;
298 op = (u_int16_t *)old_bufp->page;
299 last_bfp = NULL;
300 scopyto = (u_int16_t)copyto; /* ANSI */
301
302 n = ino[0] - 1;
303 while (n < ino[0]) {
304 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
305 if (__big_split(hashp, old_bufp,
306 new_bufp, bufp, bufp->addr, obucket, &ret))
307 return (-1);
308 old_bufp = ret.oldp;
309 if (!old_bufp)
310 return (-1);
311 op = (u_int16_t *)old_bufp->page;
312 new_bufp = ret.newp;
313 if (!new_bufp)
314 return (-1);
315 np = (u_int16_t *)new_bufp->page;
316 bufp = ret.nextp;
317 if (!bufp)
318 return (0);
319 cino = (char *)bufp->page;
320 ino = (u_int16_t *)cino;
321 last_bfp = ret.nextp;
322 } else if (ino[n + 1] == OVFLPAGE) {
323 ov_addr = ino[n];
324 /*
325 * Fix up the old page -- the extra 2 are the fields
326 * which contained the overflow information.
327 */
328 ino[0] -= (moved + 2);
329 FREESPACE(ino) =
330 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
331 OFFSET(ino) = scopyto;
332
333 bufp = __get_buf(hashp, ov_addr, bufp, 0);
334 if (!bufp)
335 return (-1);
336
337 ino = (u_int16_t *)bufp->page;
338 n = 1;
339 scopyto = hashp->BSIZE;
340 moved = 0;
341
342 if (last_bfp)
343 __free_ovflpage(hashp, last_bfp);
344 last_bfp = bufp;
345 }
346 /* Move regular sized pairs of there are any */
347 off = hashp->BSIZE;
348 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
349 cino = (char *)ino;
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];
354 off = ino[n + 1];
355
356 if (__call_hash(hashp, key.data, key.size) == obucket) {
357 /* Keep on old page */
358 if (PAIRFITS(op, (&key), (&val)))
359 putpair((char *)op, &key, &val);
360 else {
361 old_bufp =
362 __add_ovflpage(hashp, old_bufp);
363 if (!old_bufp)
364 return (-1);
365 op = (u_int16_t *)old_bufp->page;
366 putpair((char *)op, &key, &val);
367 }
368 old_bufp->flags |= BUF_MOD;
369 } else {
370 /* Move to new page */
371 if (PAIRFITS(np, (&key), (&val)))
372 putpair((char *)np, &key, &val);
373 else {
374 new_bufp =
375 __add_ovflpage(hashp, new_bufp);
376 if (!new_bufp)
377 return (-1);
378 np = (u_int16_t *)new_bufp->page;
379 putpair((char *)np, &key, &val);
380 }
381 new_bufp->flags |= BUF_MOD;
382 }
383 }
384 }
385 if (last_bfp)
386 __free_ovflpage(hashp, last_bfp);
387 return (0);
388}
int __big_split(HTAB *hashp, BUFHEAD *op, BUFHEAD *np, BUFHEAD *big_keyp, int addr, u_int32_t obucket, SPLIT_RETURN *ret)
Definition: hash_bigkey.c:570
void __free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
Definition: hash_page.c:815
BUFHEAD * nextp
Definition: page.h:90
BUFHEAD * newp
Definition: page.h:88
BUFHEAD * oldp
Definition: page.h:89

References __add_ovflpage(), __big_split(), __call_hash(), __free_ovflpage(), __get_buf(), _bufhead::addr, BUF_MOD, DBT::data, _bufhead::flags, FREESPACE, if(), SPLIT_RETURN::newp, SPLIT_RETURN::nextp, NULL, OFFSET, SPLIT_RETURN::oldp, OVFLPAGE, _bufhead::page, PAIRFITS, putpair(), REAL_KEY, and DBT::size.

Referenced by __split_page().