Asterisk - The Open Source Telephony Project GIT-master-f36a736
Macros | Functions
hash.c File Reference
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.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.c:

Go to the source code of this file.

Macros

#define ABNORMAL   (1)
 
#define ERROR   (-1)
 
#define MOD(x, y)   ((x) & ((y) - 1))
 
#define OLDHASHVERSION   1
 
#define RETURN_ERROR(ERR, LOC)   { save_errno = ERR; goto LOC; }
 
#define SUCCESS   (0)
 

Functions

u_int32_t __call_hash (HTAB *hashp, char *k, int len)
 
int __expand_table (HTAB *hashp)
 
DB__hash_open (char *file, int flags, int mode, const HASHINFO *info, int dflags) const
 
static int hash_fd __P ((const DB *))
 
static int hash_get __P ((const DB *, const DBT *, DBT *, u_int32_t))
 
static int hash_delete __P ((const DB *, const DBT *, u_int32_t))
 
static int hash_seq __P ((const DB *, DBT *, DBT *, u_int32_t))
 
static int hash_sync __P ((const DB *, u_int32_t))
 
static int hash_close __P ((DB *))
 
static void swap_header_copy __P ((HASHHDR *, HASHHDR *))
 
static int flush_meta __P ((HTAB *))
 
static int hash_access __P ((HTAB *, ACTION, DBT *, DBT *))
 
static HTAB *init_hash __P ((HTAB *, const char *, const HASHINFO *))
 
static int alloc_segs __P ((HTAB *, int))
 
static void *hash_realloc __P ((SEGMENT **, int, int))
 
static int alloc_segs (HTAB *hashp, int nsegs)
 
static int flush_meta (HTAB *hashp)
 
static int hash_access (HTAB *hashp, ACTION action, DBT *key, DBT *val)
 
static int hash_close (DB *dbp)
 
static int hash_delete (DB *dbp, const DBT *key, u_int32_t flag) const
 
static int hash_fd (DB *dbp) const
 
static int hash_get (DB *dbp, const DBT *key, DBT *data, u_int32_t flag) const
 
static int hash_put (DB *dbp, DBT *key, const DBT *data, u_int32_t flag) const
 
static void * hash_realloc (SEGMENT **p_ptr, int oldsize, int newsize)
 
static int hash_seq (DB *dbp, DBT *key, DBT *data, u_int32_t flag) const
 
static int hash_sync (DB *dbp, u_int32_t flags) const
 
static int hdestroy (HTAB *hashp)
 
static HTABinit_hash (HTAB *hashp, const char *file, const HASHINFO *info)
 
static int init_htab (HTAB *hashp, int nelem)
 
static void swap_header (HTAB *hashp)
 
static void swap_header_copy (HASHHDR *srcp, HASHHDR *destp)
 

Macro Definition Documentation

◆ ABNORMAL

#define ABNORMAL   (1)

Definition at line 86 of file hash.c.

◆ ERROR

#define ERROR   (-1)

Definition at line 85 of file hash.c.

◆ MOD

#define MOD (   x,
 
)    ((x) & ((y) - 1))

Definition at line 79 of file hash.c.

◆ OLDHASHVERSION

#define OLDHASHVERSION   1

◆ RETURN_ERROR

#define RETURN_ERROR (   ERR,
  LOC 
)    { save_errno = ERR; goto LOC; }

Definition at line 81 of file hash.c.

◆ SUCCESS

#define SUCCESS   (0)

Definition at line 84 of file hash.c.

Function Documentation

◆ __call_hash()

u_int32_t __call_hash ( HTAB hashp,
char *  k,
int  len 
)

Definition at line 886 of file hash.c.

890{
891 int n, bucket;
892
893 n = hashp->hash(k, len);
894 bucket = n & hashp->HIGH_MASK;
895 if (bucket > hashp->MAX_BUCKET)
896 bucket = bucket & hashp->LOW_MASK;
897 return (bucket);
898}
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References len().

Referenced by __big_split(), __split_page(), hash_access(), and ugly_split().

◆ __expand_table()

int __expand_table ( HTAB hashp)

Definition at line 815 of file hash.c.

817{
818 u_int32_t old_bucket, new_bucket;
819 int dirsize, new_segnum, spare_ndx;
820
821#ifdef HASH_STATISTICS
822 hash_expansions++;
823#endif
824 new_bucket = ++hashp->MAX_BUCKET;
825 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
826
827 new_segnum = new_bucket >> hashp->SSHIFT;
828
829 /* Check if we need a new segment */
830 if (new_segnum >= hashp->nsegs) {
831 /* Check if we need to expand directory */
832 if (new_segnum >= hashp->DSIZE) {
833 /* Reallocate directory */
834 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
835 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
836 return (-1);
837 hashp->DSIZE = dirsize << 1;
838 }
839 if ((hashp->dir[new_segnum] =
840 (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
841 return (-1);
842 hashp->exsegs++;
843 hashp->nsegs++;
844 }
845 /*
846 * If the split point is increasing (MAX_BUCKET's log base 2
847 * * increases), we need to copy the current contents of the spare
848 * split bucket to the next bucket.
849 */
850 spare_ndx = __hash_log2(hashp->MAX_BUCKET + 1);
851 if (spare_ndx > hashp->OVFL_POINT) {
852 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
853 hashp->OVFL_POINT = spare_ndx;
854 }
855
856 if (new_bucket > (u_int32_t) hashp->HIGH_MASK) {
857 /* Starting a new doubling */
858 hashp->LOW_MASK = hashp->HIGH_MASK;
859 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
860 }
861 /* Relocate records to the new bucket */
862 return (__split_page(hashp, old_bucket, new_bucket));
863}
#define calloc(a, b)
Definition: astmm.h:155
static void * hash_realloc(SEGMENT **p_ptr, int oldsize, int newsize)
Definition: hash.c:870
u_int32_t __hash_log2(u_int32_t num)
Definition: hash_log2.c:48
int __split_page(HTAB *hashp, u_int32_t obucket, u_int32_t nbucket)
Definition: hash_page.c:181
unsigned int u_int32_t
#define NULL
Definition: resample.c:96
Definition: hash.h:47
int exsegs
Definition: hash.h:94
SEGMENT * dir
Definition: hash.h:117
int nsegs
Definition: hash.h:93

References __hash_log2(), __split_page(), calloc, htab::dir, htab::exsegs, hash_realloc(), htab::nsegs, and NULL.

Referenced by __addel().

◆ __hash_open()

DB * __hash_open ( char *  file,
int  flags,
int  mode,
const HASHINFO info,
int  dflags 
) const

Definition at line 96 of file hash.c.

100{
101 HTAB *hashp;
102 struct stat statbuf;
103 DB *dbp;
104 int bpages, hdrsize, new_table, nsegs, save_errno;
105
106 if ((flags & O_ACCMODE) == O_WRONLY) {
107 errno = EINVAL;
108 return (NULL);
109 }
110
111 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
112 return (NULL);
113 hashp->fp = -1;
114
115 /*
116 * Even if user wants write only, we need to be able to read
117 * the actual file, so we need to open it read/write. But, the
118 * field in the hashp structure needs to be accurate so that
119 * we can check accesses.
120 */
121 hashp->flags = flags;
122
123 new_table = 0;
124 if (!file || (flags & O_TRUNC) ||
125 (stat(file, &statbuf) && (errno == ENOENT))) {
126 if (errno == ENOENT)
127 errno = 0; /* Just in case someone looks at errno */
128 new_table = 1;
129 }
130 if (file) {
131 if ((hashp->fp = open(file, flags, mode)) == -1)
132 RETURN_ERROR(errno, error0);
133 (void)fcntl(hashp->fp, F_SETFD, 1);
134 }
135 if (new_table) {
136 if (!(hashp = init_hash(hashp, file, info)))
137 RETURN_ERROR(errno, error1);
138 } else {
139 /* Table already exists */
140 if (info && info->hash)
141 hashp->hash = info->hash;
142 else
143 hashp->hash = __default_hash;
144
145 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
146#if BYTE_ORDER == LITTLE_ENDIAN
147 swap_header(hashp);
148#endif
149 if (hdrsize == -1)
150 RETURN_ERROR(errno, error1);
151 if (hdrsize != sizeof(HASHHDR))
152 RETURN_ERROR(EFTYPE, error1);
153 /* Verify file type, versions and hash function */
154 if (hashp->MAGIC != HASHMAGIC)
155 RETURN_ERROR(EFTYPE, error1);
156#define OLDHASHVERSION 1
157 if (hashp->VERSION != HASHVERSION &&
158 hashp->VERSION != OLDHASHVERSION)
159 RETURN_ERROR(EFTYPE, error1);
160 if (hashp->hash(CHARKEY, sizeof(CHARKEY))
161 != (u_int32_t) hashp->H_CHARKEY)
162 RETURN_ERROR(EFTYPE, error1);
163 /*
164 * Figure out how many segments we need. Max_Bucket is the
165 * maximum bucket number, so the number of buckets is
166 * max_bucket + 1.
167 */
168 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
169 hashp->SGSIZE;
170 hashp->nsegs = 0;
171 if (alloc_segs(hashp, nsegs))
172 /*
173 * If alloc_segs fails, table will have been destroyed
174 * and errno will have been set.
175 */
176 return (NULL);
177 /* Read in bitmaps */
178 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
179 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
180 (hashp->BSHIFT + BYTE_SHIFT);
181
182 hashp->nmaps = bpages;
183 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
184 }
185
186 /* Initialize Buffer Manager */
187 if (info && info->cachesize)
188 __buf_init(hashp, info->cachesize);
189 else
190 __buf_init(hashp, DEF_BUFSIZE);
191
192 hashp->new_file = new_table;
193 hashp->save_file = file && (hashp->flags & O_ACCMODE) != O_RDONLY;
194 hashp->cbucket = -1;
195 if (!(dbp = (DB *)malloc(sizeof(DB)))) {
196 save_errno = errno;
197 hdestroy(hashp);
198 errno = save_errno;
199 return (NULL);
200 }
201 dbp->internal = hashp;
202 dbp->close = hash_close;
203 dbp->del = hash_delete;
204 dbp->fd = hash_fd;
205 dbp->get = hash_get;
206 dbp->put = hash_put;
207 dbp->seq = hash_seq;
208 dbp->sync = hash_sync;
209 dbp->type = DB_HASH;
210
211#ifdef DEBUG
212 (void)fprintf(stderr,
213"%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
214 "init_htab:",
215 "TABLE POINTER ", hashp,
216 "BUCKET SIZE ", hashp->BSIZE,
217 "BUCKET SHIFT ", hashp->BSHIFT,
218 "DIRECTORY SIZE ", hashp->DSIZE,
219 "SEGMENT SIZE ", hashp->SGSIZE,
220 "SEGMENT SHIFT ", hashp->SSHIFT,
221 "FILL FACTOR ", hashp->FFACTOR,
222 "MAX BUCKET ", hashp->MAX_BUCKET,
223 "OVFL POINT ", hashp->OVFL_POINT,
224 "LAST FREED ", hashp->LAST_FREED,
225 "HIGH MASK ", hashp->HIGH_MASK,
226 "LOW MASK ", hashp->LOW_MASK,
227 "NSEGS ", hashp->nsegs,
228 "NKEYS ", hashp->NKEYS);
229#endif
230#ifdef HASH_STATISTICS
231 hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
232#endif
233 return (dbp);
234
235error1:
236 if (hashp != NULL)
237 (void)close(hashp->fp);
238
239error0:
240 free(hashp);
241 errno = save_errno;
242 return (NULL);
243}
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
#define HASHVERSION
Definition: db.h:160
@ DB_HASH
Definition: db.h:103
#define HASHMAGIC
Definition: db.h:159
char * malloc()
void free()
static HTAB * init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
Definition: hash.c:280
static int hdestroy(HTAB *hashp)
Definition: hash.c:397
static int hash_sync(DB *dbp, u_int32_t flags) const
Definition: hash.c:457
static int alloc_segs(HTAB *hashp, int nsegs)
Definition: hash.c:906
static int hash_seq(DB *dbp, DBT *key, DBT *data, u_int32_t flag) const
Definition: hash.c:725
static int hash_put(DB *dbp, DBT *key, const DBT *data, u_int32_t flag) const
Definition: hash.c:551
static int hash_close(DB *dbp)
Definition: hash.c:246
static int hash_get(DB *dbp, const DBT *key, DBT *data, u_int32_t flag) const
Definition: hash.c:534
static int hash_delete(DB *dbp, const DBT *key, u_int32_t flag) const
Definition: hash.c:573
static int hash_fd(DB *dbp) const
Definition: hash.c:262
#define RETURN_ERROR(ERR, LOC)
Definition: hash.c:81
#define OLDHASHVERSION
static void swap_header(HTAB *hashp)
Definition: hash.c:969
#define BYTE_SHIFT
Definition: hash.h:137
#define CHARKEY
Definition: hash.h:135
#define DEF_BUFSIZE
Definition: hash.h:126
void __buf_init(HTAB *hashp, int nbytes)
Definition: hash_buf.c:290
static DB * dbp
Definition: hsearch.c:49
int errno
def info(msg)
Definition: db.h:129
DBTYPE type
Definition: db.h:130
void * internal
Definition: db.h:137
Definition: hash.h:65
Definition: hash.h:91
int cbucket
Definition: hash.h:103
int save_file
Definition: hash.h:109
int new_file
Definition: hash.h:107
int fp
Definition: hash.h:99
u_int32_t * mapp[NCACHED]
Definition: hash.h:112
HASHHDR hdr
Definition: hash.h:92
int nmaps
Definition: hash.h:113
int flags
Definition: hash.h:98
#define EFTYPE

References __buf_init(), alloc_segs(), BYTE_SHIFT, calloc, htab::cbucket, CHARKEY, DB_HASH, dbp, DEF_BUFSIZE, EFTYPE, errno, make_ari_stubs::file, htab::flags, htab::fp, free(), hash_close(), hash_delete(), hash_fd(), hash_get(), hash_put(), hash_seq(), hash_sync(), HASHMAGIC, HASHVERSION, hdestroy(), htab::hdr, if(), sip_to_pjsip::info(), init_hash(), __db::internal, malloc(), htab::mapp, htab::new_file, htab::nmaps, htab::nsegs, NULL, OLDHASHVERSION, RETURN_ERROR, htab::save_file, swap_header(), and __db::type.

Referenced by dbm_open(), dbopen(), and hcreate().

◆ __P() [1/12]

static int hash_fd __P ( (const DB *)  )
static

◆ __P() [2/12]

static int hash_put __P ( (const DB *, const DBT *, DBT *, u_int32_t )
static

◆ __P() [3/12]

static int hash_delete __P ( (const DB *, const DBT *, u_int32_t )
static

◆ __P() [4/12]

static int hash_seq __P ( (const DB *, DBT *, DBT *, u_int32_t )
static

◆ __P() [5/12]

static int hash_sync __P ( (const DB *, u_int32_t )
static

◆ __P() [6/12]

static int hash_close __P ( (DB *)  )
static

◆ __P() [7/12]

static void swap_header_copy __P ( (HASHHDR *, HASHHDR *)  )
static

◆ __P() [8/12]

static int flush_meta __P ( (HTAB *)  )
static

◆ __P() [9/12]

static int hash_access __P ( (HTAB *, ACTION, DBT *, DBT *)  )
static

◆ __P() [10/12]

static HTAB *init_hash __P ( (HTAB *, const char *, const HASHINFO *)  )
static

◆ __P() [11/12]

static int alloc_segs __P ( (HTAB *, int)  )
static

◆ __P() [12/12]

static void *hash_realloc __P ( (SEGMENT **, int, int)  )
static

◆ alloc_segs()

static int alloc_segs ( HTAB hashp,
int  nsegs 
)
static

Definition at line 906 of file hash.c.

909{
910 register int i;
911 register SEGMENT store;
912
913 int save_errno;
914
915 if ((hashp->dir =
916 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
917 save_errno = errno;
918 (void)hdestroy(hashp);
919 errno = save_errno;
920 return (-1);
921 }
922 /* Allocate segments */
923 if ((store =
924 (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
925 save_errno = errno;
926 (void)hdestroy(hashp);
927 errno = save_errno;
928 return (-1);
929 }
930 for (i = 0; i < nsegs; i++, hashp->nsegs++)
931 hashp->dir[i] = &store[i << hashp->SSHIFT];
932 return (0);
933}

References calloc, htab::dir, errno, hdestroy(), htab::nsegs, and NULL.

Referenced by __hash_open(), and init_htab().

◆ flush_meta()

static int flush_meta ( HTAB hashp)
static

Definition at line 486 of file hash.c.

488{
489 HASHHDR *whdrp;
490#if BYTE_ORDER == LITTLE_ENDIAN
491 HASHHDR whdr;
492#endif
493 int fp, i, wsize;
494
495 if (!hashp->save_file)
496 return (0);
497 hashp->MAGIC = HASHMAGIC;
498 hashp->VERSION = HASHVERSION;
499 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
500
501 fp = hashp->fp;
502 whdrp = &hashp->hdr;
503#if BYTE_ORDER == LITTLE_ENDIAN
504 whdrp = &whdr;
505 swap_header_copy(&hashp->hdr, whdrp);
506#endif
507 if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
508 ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
509 return (-1);
510 else
511 if (wsize != sizeof(HASHHDR)) {
512 errno = EFTYPE;
513 hashp->errnum = errno;
514 return (-1);
515 }
516 for (i = 0; i < NCACHED; i++)
517 if (hashp->mapp[i])
518 if (__put_page(hashp, (char *)hashp->mapp[i],
519 hashp->BITMAPS[i], 0, 1))
520 return (-1);
521 return (0);
522}
static void swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
Definition: hash.c:940
#define NCACHED
Definition: hash.h:85
int __put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
Definition: hash_page.c:578
int errnum
Definition: hash.h:105

References __put_page(), CHARKEY, EFTYPE, errno, htab::errnum, htab::fp, HASHMAGIC, HASHVERSION, htab::hdr, htab::mapp, NCACHED, htab::save_file, and swap_header_copy().

Referenced by hash_sync(), and hdestroy().

◆ hash_access()

static int hash_access ( HTAB hashp,
ACTION  action,
DBT key,
DBT val 
)
static

Definition at line 596 of file hash.c.

600{
601 register BUFHEAD *rbufp;
602 BUFHEAD *bufp, *save_bufp;
603 register u_int16_t *bp;
604 register int n, ndx, off, size;
605 register char *kp;
606 u_int16_t pageno;
607
608#ifdef HASH_STATISTICS
609 hash_accesses++;
610#endif
611
612 off = hashp->BSIZE;
613 size = key->size;
614 kp = (char *)key->data;
615 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
616 if (!rbufp)
617 return (ERROR);
618 save_bufp = rbufp;
619
620 /* Pin the bucket chain */
621 rbufp->flags |= BUF_PIN;
622 for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
623 if (bp[1] >= REAL_KEY) {
624 /* Real key/data pair */
625 if (size == off - *bp &&
626 memcmp(kp, rbufp->page + *bp, size) == 0)
627 goto found;
628 off = bp[1];
629#ifdef HASH_STATISTICS
630 hash_collisions++;
631#endif
632 bp += 2;
633 ndx += 2;
634 } else if (bp[1] == OVFLPAGE) {
635 rbufp = __get_buf(hashp, *bp, rbufp, 0);
636 if (!rbufp) {
637 save_bufp->flags &= ~BUF_PIN;
638 return (ERROR);
639 }
640 /* FOR LOOP INIT */
641 bp = (u_int16_t *)rbufp->page;
642 n = *bp++;
643 ndx = 1;
644 off = hashp->BSIZE;
645 } else if (bp[1] < REAL_KEY) {
646 if ((ndx =
647 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
648 goto found;
649 if (ndx == -2) {
650 bufp = rbufp;
651 if (!(pageno =
652 __find_last_page(hashp, &bufp))) {
653 ndx = 0;
654 rbufp = bufp;
655 break; /* FOR */
656 }
657 rbufp = __get_buf(hashp, pageno, bufp, 0);
658 if (!rbufp) {
659 save_bufp->flags &= ~BUF_PIN;
660 return (ERROR);
661 }
662 /* FOR LOOP INIT */
663 bp = (u_int16_t *)rbufp->page;
664 n = *bp++;
665 ndx = 1;
666 off = hashp->BSIZE;
667 } else {
668 save_bufp->flags &= ~BUF_PIN;
669 return (ERROR);
670 }
671 }
672
673 /* Not found */
674 switch (action) {
675 case HASH_PUT:
676 case HASH_PUTNEW:
677 if (__addel(hashp, rbufp, key, val)) {
678 save_bufp->flags &= ~BUF_PIN;
679 return (ERROR);
680 } else {
681 save_bufp->flags &= ~BUF_PIN;
682 return (SUCCESS);
683 }
684 case HASH_GET:
685 case HASH_DELETE:
686 default:
687 save_bufp->flags &= ~BUF_PIN;
688 return (ABNORMAL);
689 }
690
691found:
692 switch (action) {
693 case HASH_PUTNEW:
694 save_bufp->flags &= ~BUF_PIN;
695 return (ABNORMAL);
696 case HASH_GET:
697 bp = (u_int16_t *)rbufp->page;
698 if (bp[ndx + 1] < REAL_KEY) {
699 if (__big_return(hashp, rbufp, ndx, val, 0))
700 return (ERROR);
701 } else {
702 val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
703 val->size = bp[ndx] - bp[ndx + 1];
704 }
705 break;
706 case HASH_PUT:
707 if ((__delpair(hashp, rbufp, ndx)) ||
708 (__addel(hashp, rbufp, key, val))) {
709 save_bufp->flags &= ~BUF_PIN;
710 return (ERROR);
711 }
712 break;
713 case HASH_DELETE:
714 if (__delpair(hashp, rbufp, ndx))
715 return (ERROR);
716 break;
717 default:
718 abort();
719 }
720 save_bufp->flags &= ~BUF_PIN;
721 return (SUCCESS);
722}
#define ABNORMAL
Definition: hash.c:86
#define ERROR
Definition: hash.c:85
#define SUCCESS
Definition: hash.c:84
u_int32_t __call_hash(HTAB *hashp, char *k, int len)
Definition: hash.c:886
#define OVFLPAGE
Definition: hash.h:266
#define REAL_KEY
Definition: hash.h:270
#define BUF_PIN
Definition: hash.h:57
@ HASH_DELETE
Definition: hash.h:41
@ HASH_PUT
Definition: hash.h:41
@ HASH_PUTNEW
Definition: hash.h:41
@ HASH_GET
Definition: hash.h:41
int __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
Definition: hash_bigkey.c:267
int __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current)
Definition: hash_bigkey.c:360
u_int16_t __find_last_page(HTAB *hashp, BUFHEAD **bpp)
Definition: hash_bigkey.c:319
BUFHEAD * __get_buf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp, int newpage)
Definition: hash_buf.c:105
int __delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
Definition: hash_page.c:128
int __addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
Definition: hash_page.c:398
unsigned short u_int16_t
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
char * page
Definition: hash.h:52
char flags
Definition: hash.h:53
Definition: ast_expr2.c:325

References __addel(), __big_return(), __call_hash(), __delpair(), __find_bigpair(), __find_last_page(), __get_buf(), ABNORMAL, BUF_PIN, DBT::data, ERROR, _bufhead::flags, HASH_DELETE, HASH_GET, HASH_PUT, HASH_PUTNEW, if(), NULL, OVFLPAGE, _bufhead::page, REAL_KEY, DBT::size, and SUCCESS.

Referenced by hash_delete(), hash_get(), and hash_put().

◆ hash_close()

static int hash_close ( DB dbp)
static

Definition at line 246 of file hash.c.

248{
249 HTAB *hashp;
250 int retval;
251
252 if (!dbp)
253 return (ERROR);
254
255 hashp = (HTAB *)dbp->internal;
256 retval = hdestroy(hashp);
257 free(dbp);
258 return (retval);
259}
static ENTRY retval
Definition: hsearch.c:50

References dbp, ERROR, free(), hdestroy(), __db::internal, and retval.

Referenced by __hash_open().

◆ hash_delete()

static int hash_delete ( DB dbp,
const DBT key,
u_int32_t  flag 
) const
static

Definition at line 573 of file hash.c.

577{
578 HTAB *hashp;
579
580 hashp = (HTAB *)dbp->internal;
581 if (flag && flag != R_CURSOR) {
582 hashp->errnum = errno = EINVAL;
583 return (ERROR);
584 }
585 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
586 hashp->errnum = errno = EPERM;
587 return (ERROR);
588 }
589 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
590}
#define R_CURSOR
Definition: db.h:91
long int flag
Definition: f2c.h:83
static int hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
Definition: hash.c:596
Definition: db.h:85

References dbp, errno, htab::errnum, ERROR, htab::flags, hash_access(), HASH_DELETE, if(), __db::internal, NULL, and R_CURSOR.

Referenced by __hash_open().

◆ hash_fd()

static int hash_fd ( DB dbp) const
static

Definition at line 262 of file hash.c.

264{
265 HTAB *hashp;
266
267 if (!dbp)
268 return (ERROR);
269
270 hashp = (HTAB *)dbp->internal;
271 if (hashp->fp == -1) {
272 errno = ENOENT;
273 return (-1);
274 }
275 return (hashp->fp);
276}

References dbp, errno, ERROR, htab::fp, if(), and __db::internal.

Referenced by __hash_open().

◆ hash_get()

static int hash_get ( DB dbp,
const DBT key,
DBT data,
u_int32_t  flag 
) const
static

Definition at line 534 of file hash.c.

539{
540 HTAB *hashp;
541
542 hashp = (HTAB *)dbp->internal;
543 if (flag) {
544 hashp->errnum = errno = EINVAL;
545 return (ERROR);
546 }
547 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
548}

References dbp, errno, htab::errnum, ERROR, hash_access(), HASH_GET, if(), and __db::internal.

Referenced by __hash_open().

◆ hash_put()

static int hash_put ( DB dbp,
DBT key,
const DBT data,
u_int32_t  flag 
) const
static

Definition at line 551 of file hash.c.

556{
557 HTAB *hashp;
558
559 hashp = (HTAB *)dbp->internal;
560 if (flag && flag != R_NOOVERWRITE) {
561 hashp->errnum = errno = EINVAL;
562 return (ERROR);
563 }
564 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
565 hashp->errnum = errno = EPERM;
566 return (ERROR);
567 }
568 return (hash_access(hashp, flag == R_NOOVERWRITE ?
569 HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
570}
#define R_NOOVERWRITE
Definition: db.h:98

References dbp, errno, htab::errnum, ERROR, htab::flags, hash_access(), HASH_PUT, HASH_PUTNEW, if(), __db::internal, and R_NOOVERWRITE.

Referenced by __hash_open().

◆ hash_realloc()

static void * hash_realloc ( SEGMENT **  p_ptr,
int  oldsize,
int  newsize 
)
static

Definition at line 870 of file hash.c.

873{
874 register void *p;
875
876 if ((p = malloc(newsize))) {
877 memmove(p, *p_ptr, oldsize);
878 memset((char *)p + oldsize, 0, newsize - oldsize);
879 free(*p_ptr);
880 *p_ptr = p;
881 }
882 return (p);
883}

References free(), and malloc().

Referenced by __expand_table().

◆ hash_seq()

static int hash_seq ( DB dbp,
DBT key,
DBT data,
u_int32_t  flag 
) const
static

Definition at line 725 of file hash.c.

729{
730 register u_int32_t bucket;
731 register BUFHEAD *bufp = NULL;
732 HTAB *hashp;
733 u_int16_t *bp, ndx;
734
735 hashp = (HTAB *)dbp->internal;
736 if (flag && flag != R_FIRST && flag != R_NEXT) {
737 hashp->errnum = errno = EINVAL;
738 return (ERROR);
739 }
740#ifdef HASH_STATISTICS
741 hash_accesses++;
742#endif
743 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
744 hashp->cbucket = 0;
745 hashp->cndx = 1;
746 hashp->cpage = NULL;
747 }
748
749 for (bp = NULL; !bp || !bp[0]; ) {
750 if (!(bufp = hashp->cpage)) {
751 for (bucket = hashp->cbucket;
752 bucket <= (u_int32_t) hashp->MAX_BUCKET;
753 bucket++, hashp->cndx = 1) {
754 bufp = __get_buf(hashp, bucket, NULL, 0);
755 if (!bufp)
756 return (ERROR);
757 hashp->cpage = bufp;
758 bp = (u_int16_t *)bufp->page;
759 if (bp[0])
760 break;
761 }
762 hashp->cbucket = bucket;
763 if (hashp->cbucket > hashp->MAX_BUCKET) {
764 hashp->cbucket = -1;
765 return (ABNORMAL);
766 }
767 } else
768 bp = (u_int16_t *)hashp->cpage->page;
769
770#ifdef DEBUG
771 assert(bp);
772 assert(bufp);
773#endif
774 while (bp[hashp->cndx + 1] == OVFLPAGE) {
775 bufp = hashp->cpage =
776 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
777 if (!bufp)
778 return (ERROR);
779 bp = (u_int16_t *)(bufp->page);
780 hashp->cndx = 1;
781 }
782 if (!bp[0]) {
783 hashp->cpage = NULL;
784 ++hashp->cbucket;
785 }
786 }
787 ndx = hashp->cndx;
788 if (bp[ndx + 1] < REAL_KEY) {
789 if (__big_keydata(hashp, bufp, key, data, 1))
790 return (ERROR);
791 } else {
792 key->data = (u_char *)hashp->cpage->page + bp[ndx];
793 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
794 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
795 data->size = bp[ndx] - bp[ndx + 1];
796 ndx += 2;
797 if (ndx > bp[0]) {
798 hashp->cpage = NULL;
799 hashp->cbucket++;
800 hashp->cndx = 1;
801 } else
802 hashp->cndx = ndx;
803 }
804 return (SUCCESS);
805}
#define R_NEXT
Definition: db.h:97
#define R_FIRST
Definition: db.h:93
int __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set)
Definition: hash_bigkey.c:507
#define DEBUG(a)
Definition: io.c:48
BUFHEAD * cpage
Definition: hash.h:102
int cndx
Definition: hash.h:104

References __big_keydata(), __get_buf(), ABNORMAL, htab::cbucket, htab::cndx, htab::cpage, DBT::data, dbp, DEBUG, errno, htab::errnum, ERROR, if(), __db::internal, NULL, OVFLPAGE, _bufhead::page, R_FIRST, R_NEXT, REAL_KEY, DBT::size, and SUCCESS.

Referenced by __hash_open().

◆ hash_sync()

static int hash_sync ( DB dbp,
u_int32_t  flags 
) const
static

Definition at line 457 of file hash.c.

460{
461 HTAB *hashp;
462
463 if (flags != 0) {
464 errno = EINVAL;
465 return (ERROR);
466 }
467
468 if (!dbp)
469 return (ERROR);
470
471 hashp = (HTAB *)dbp->internal;
472 if (!hashp->save_file)
473 return (0);
474 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
475 return (ERROR);
476 hashp->new_file = 0;
477 return (0);
478}
static int flush_meta(HTAB *hashp)
Definition: hash.c:486
int __buf_free(HTAB *hashp, int do_free, int to_disk)
Definition: hash_buf.c:315

References __buf_free(), dbp, errno, ERROR, flush_meta(), if(), __db::internal, htab::new_file, and htab::save_file.

Referenced by __hash_open().

◆ hdestroy()

static int hdestroy ( HTAB hashp)
static

Definition at line 397 of file hash.c.

399{
400 int i, save_errno;
401
402 save_errno = 0;
403
404#ifdef HASH_STATISTICS
405 (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
406 hash_accesses, hash_collisions);
407 (void)fprintf(stderr, "hdestroy: expansions %ld\n",
408 hash_expansions);
409 (void)fprintf(stderr, "hdestroy: overflows %ld\n",
410 hash_overflows);
411 (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
412 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
413
414 for (i = 0; i < NCACHED; i++)
415 (void)fprintf(stderr,
416 "spares[%d] = %d\n", i, hashp->SPARES[i]);
417#endif
418 /*
419 * Call on buffer manager to free buffers, and if required,
420 * write them to disk.
421 */
422 if (__buf_free(hashp, 1, hashp->save_file))
423 save_errno = errno;
424 if (hashp->dir) {
425 free(*hashp->dir); /* Free initial segments */
426 /* Free extra segments */
427 while (hashp->exsegs--)
428 free(hashp->dir[--hashp->nsegs]);
429 free(hashp->dir);
430 }
431 if (flush_meta(hashp) && !save_errno)
432 save_errno = errno;
433 /* Free Bigmaps */
434 for (i = 0; i < hashp->nmaps; i++)
435 if (hashp->mapp[i])
436 free(hashp->mapp[i]);
437
438 if (hashp->fp != -1)
439 (void)close(hashp->fp);
440
441 free(hashp);
442
443 if (save_errno) {
444 errno = save_errno;
445 return (ERROR);
446 }
447 return (SUCCESS);
448}

References __buf_free(), htab::dir, errno, ERROR, htab::exsegs, flush_meta(), htab::fp, free(), htab::mapp, NCACHED, htab::nmaps, htab::nsegs, htab::save_file, and SUCCESS.

Referenced by __hash_open(), alloc_segs(), and hash_close().

◆ init_hash()

static HTAB * init_hash ( HTAB hashp,
const char *  file,
const HASHINFO info 
)
static

Definition at line 280 of file hash.c.

284{
285#ifdef _STATBUF_ST_BLKSIZE
286 struct stat statbuf;
287#endif
288 int nelem;
289
290 nelem = 1;
291 hashp->NKEYS = 0;
292 hashp->LORDER = BYTE_ORDER;
293 hashp->BSIZE = DEF_BUCKET_SIZE;
294 hashp->BSHIFT = DEF_BUCKET_SHIFT;
295 hashp->SGSIZE = DEF_SEGSIZE;
296 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
297 hashp->DSIZE = DEF_DIRSIZE;
298 hashp->FFACTOR = DEF_FFACTOR;
299 hashp->hash = __default_hash;
300 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
301 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
302
303 /* Fix bucket size to be optimal for file system */
304#ifdef _STATBUF_ST_BLKSIZE
305 if (file != NULL) {
306 if (stat(file, &statbuf))
307 return (NULL);
308 hashp->BSIZE = statbuf.st_blksize;
309 hashp->BSHIFT = __hash_log2(hashp->BSIZE);
310 }
311#endif
312
313 if (info) {
314 if (info->bsize) {
315 /* Round pagesize up to power of 2 */
316 hashp->BSHIFT = __hash_log2(info->bsize);
317 hashp->BSIZE = 1 << hashp->BSHIFT;
318 if (hashp->BSIZE > MAX_BSIZE) {
319 errno = EINVAL;
320 return (NULL);
321 }
322 }
323 if (info->ffactor)
324 hashp->FFACTOR = info->ffactor;
325 if (info->hash)
326 hashp->hash = info->hash;
327 if (info->nelem)
328 nelem = info->nelem;
329 if (info->lorder) {
330 if (info->lorder != BIG_ENDIAN &&
331 info->lorder != LITTLE_ENDIAN) {
332 errno = EINVAL;
333 return (NULL);
334 }
335 hashp->LORDER = info->lorder;
336 }
337 }
338 /* init_htab should destroy the table and set errno if it fails */
339 if (init_htab(hashp, nelem))
340 return (NULL);
341 else
342 return (hashp);
343}
static int init_htab(HTAB *hashp, int nelem)
Definition: hash.c:351
#define DEF_DIRSIZE
Definition: hash.h:131
#define DEF_BUCKET_SIZE
Definition: hash.h:127
#define DEF_SEGSIZE_SHIFT
Definition: hash.h:130
#define DEF_SEGSIZE
Definition: hash.h:129
#define DEF_BUCKET_SHIFT
Definition: hash.h:128
#define DEF_FFACTOR
Definition: hash.h:132
#define MAX_BSIZE
Definition: hash.h:123
#define BYTE_ORDER
#define BIG_ENDIAN
#define LITTLE_ENDIAN

References __hash_log2(), BIG_ENDIAN, BYTE_ORDER, DEF_BUCKET_SHIFT, DEF_BUCKET_SIZE, DEF_DIRSIZE, DEF_FFACTOR, DEF_SEGSIZE, DEF_SEGSIZE_SHIFT, errno, make_ari_stubs::file, sip_to_pjsip::info(), init_htab(), LITTLE_ENDIAN, MAX_BSIZE, and NULL.

Referenced by __hash_open().

◆ init_htab()

static int init_htab ( HTAB hashp,
int  nelem 
)
static

Definition at line 351 of file hash.c.

354{
355 register int nbuckets, nsegs;
356 int l2;
357
358 /*
359 * Divide number of elements by the fill factor and determine a
360 * desired number of buckets. Allocate space for the next greater
361 * power of two number of buckets.
362 */
363 nelem = (nelem - 1) / hashp->FFACTOR + 1;
364
365 l2 = __hash_log2(MAX(nelem, 2));
366 nbuckets = 1 << l2;
367
368 hashp->SPARES[l2] = l2 + 1;
369 hashp->SPARES[l2 + 1] = l2 + 1;
370 hashp->OVFL_POINT = l2;
371 hashp->LAST_FREED = 2;
372
373 /* First bitmap page is at: splitpoint l2 page offset 1 */
374 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
375 return (-1);
376
377 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
378 hashp->HIGH_MASK = (nbuckets << 1) - 1;
379 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
380 hashp->BSHIFT) + 1;
381
382 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
383 nsegs = 1 << __hash_log2(nsegs);
384
385 if (nsegs > hashp->DSIZE)
386 hashp->DSIZE = nsegs;
387 return (alloc_segs(hashp, nsegs));
388}
#define OADDR_OF(S, O)
Definition: hash.h:169
#define MINHDRSIZE
Definition: hash.h:125
int __ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
Definition: hash_page.c:627
#define MAX(a, b)
Definition: utils.h:233

References __hash_log2(), __ibitmap(), alloc_segs(), MAX, MINHDRSIZE, and OADDR_OF.

Referenced by init_hash().

◆ swap_header()

static void swap_header ( HTAB hashp)
static

Definition at line 969 of file hash.c.

971{
972 HASHHDR *hdrp;
973 int i;
974
975 hdrp = &hashp->hdr;
976
977 M_32_SWAP(hdrp->magic);
978 M_32_SWAP(hdrp->version);
979 M_32_SWAP(hdrp->lorder);
980 M_32_SWAP(hdrp->bsize);
981 M_32_SWAP(hdrp->bshift);
982 M_32_SWAP(hdrp->dsize);
983 M_32_SWAP(hdrp->ssize);
984 M_32_SWAP(hdrp->sshift);
985 M_32_SWAP(hdrp->ovfl_point);
986 M_32_SWAP(hdrp->last_freed);
987 M_32_SWAP(hdrp->max_bucket);
988 M_32_SWAP(hdrp->high_mask);
989 M_32_SWAP(hdrp->low_mask);
990 M_32_SWAP(hdrp->ffactor);
991 M_32_SWAP(hdrp->nkeys);
992 M_32_SWAP(hdrp->hdrpages);
993 M_32_SWAP(hdrp->h_charkey);
994 for (i = 0; i < NCACHED; i++) {
995 M_32_SWAP(hdrp->spares[i]);
996 M_16_SWAP(hdrp->bitmaps[i]);
997 }
998}
int spares[NCACHED]
Definition: hash.h:86
int bsize
Definition: hash.h:69
int high_mask
Definition: hash.h:78
int hdrpages
Definition: hash.h:83
int h_charkey
Definition: hash.h:84
int bshift
Definition: hash.h:70
int ovfl_point
Definition: hash.h:74
int magic
Definition: hash.h:66
int ffactor
Definition: hash.h:81
int nkeys
Definition: hash.h:82
u_int32_t lorder
Definition: hash.h:68
int low_mask
Definition: hash.h:79
int max_bucket
Definition: hash.h:77
int version
Definition: hash.h:67
int dsize
Definition: hash.h:71
int sshift
Definition: hash.h:73
int ssize
Definition: hash.h:72
int last_freed
Definition: hash.h:76
u_int16_t bitmaps[NCACHED]
Definition: hash.h:87

References hashhdr::bitmaps, hashhdr::bshift, hashhdr::bsize, hashhdr::dsize, hashhdr::ffactor, hashhdr::h_charkey, htab::hdr, hashhdr::hdrpages, hashhdr::high_mask, hashhdr::last_freed, hashhdr::lorder, hashhdr::low_mask, hashhdr::magic, hashhdr::max_bucket, NCACHED, hashhdr::nkeys, hashhdr::ovfl_point, hashhdr::spares, hashhdr::sshift, hashhdr::ssize, and hashhdr::version.

Referenced by __hash_open().

◆ swap_header_copy()

static void swap_header_copy ( HASHHDR srcp,
HASHHDR destp 
)
static

Definition at line 940 of file hash.c.

942{
943 int i;
944
945 P_32_COPY(srcp->magic, destp->magic);
946 P_32_COPY(srcp->version, destp->version);
947 P_32_COPY(srcp->lorder, destp->lorder);
948 P_32_COPY(srcp->bsize, destp->bsize);
949 P_32_COPY(srcp->bshift, destp->bshift);
950 P_32_COPY(srcp->dsize, destp->dsize);
951 P_32_COPY(srcp->ssize, destp->ssize);
952 P_32_COPY(srcp->sshift, destp->sshift);
953 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
954 P_32_COPY(srcp->last_freed, destp->last_freed);
955 P_32_COPY(srcp->max_bucket, destp->max_bucket);
956 P_32_COPY(srcp->high_mask, destp->high_mask);
957 P_32_COPY(srcp->low_mask, destp->low_mask);
958 P_32_COPY(srcp->ffactor, destp->ffactor);
959 P_32_COPY(srcp->nkeys, destp->nkeys);
960 P_32_COPY(srcp->hdrpages, destp->hdrpages);
961 P_32_COPY(srcp->h_charkey, destp->h_charkey);
962 for (i = 0; i < NCACHED; i++) {
963 P_32_COPY(srcp->spares[i], destp->spares[i]);
964 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
965 }
966}

References hashhdr::bitmaps, hashhdr::bshift, hashhdr::bsize, hashhdr::dsize, hashhdr::ffactor, hashhdr::h_charkey, hashhdr::hdrpages, hashhdr::high_mask, hashhdr::last_freed, hashhdr::lorder, hashhdr::low_mask, hashhdr::magic, hashhdr::max_bucket, NCACHED, hashhdr::nkeys, hashhdr::ovfl_point, hashhdr::spares, hashhdr::sshift, hashhdr::ssize, and hashhdr::version.

Referenced by flush_meta().