Lines Matching defs:hashp

95 	HTAB *hashp;
105 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
107 hashp->fp = -1;
112 * field in the hashp structure needs to be accurate so that
115 hashp->flags = flags;
118 if ((hashp->fp = open(file, flags | O_CLOEXEC, mode)) == -1)
120 new_table = fstat(hashp->fp, &statbuf) == 0 &&
126 if (!(hashp = init_hash(hashp, file, info)))
131 hashp->hash = info->hash;
133 hashp->hash = __default_hash;
135 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
137 swap_header(hashp);
144 if (hashp->MAGIC != HASHMAGIC)
147 if (hashp->VERSION != HASHVERSION &&
148 hashp->VERSION != OLDHASHVERSION)
150 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
157 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
158 hashp->SGSIZE;
159 if (alloc_segs(hashp, nsegs))
166 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
167 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
168 (hashp->BSHIFT + BYTE_SHIFT);
170 hashp->nmaps = bpages;
171 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
176 __buf_init(hashp, info->cachesize);
178 __buf_init(hashp, DEF_BUFSIZE);
180 hashp->new_file = new_table;
181 hashp->save_file = file && (hashp->flags & O_RDWR);
182 hashp->cbucket = -1;
185 hdestroy(hashp);
189 dbp->internal = hashp;
203 "TABLE POINTER ", hashp,
204 "BUCKET SIZE ", hashp->BSIZE,
205 "BUCKET SHIFT ", hashp->BSHIFT,
206 "DIRECTORY SIZE ", hashp->DSIZE,
207 "SEGMENT SIZE ", hashp->SGSIZE,
208 "SEGMENT SHIFT ", hashp->SSHIFT,
209 "FILL FACTOR ", hashp->FFACTOR,
210 "MAX BUCKET ", hashp->MAX_BUCKET,
211 "OVFL POINT ", hashp->OVFL_POINT,
212 "LAST FREED ", hashp->LAST_FREED,
213 "HIGH MASK ", hashp->HIGH_MASK,
214 "LOW MASK ", hashp->LOW_MASK,
215 "NSEGS ", hashp->nsegs,
216 "NKEYS ", hashp->NKEYS);
224 if (hashp != NULL)
225 (void)close(hashp->fp);
228 free(hashp);
236 HTAB *hashp;
242 hashp = (HTAB *)dbp->internal;
243 retval = hdestroy(hashp);
251 HTAB *hashp;
256 hashp = (HTAB *)dbp->internal;
257 if (hashp->fp == -1) {
261 return (hashp->fp);
266 init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
272 hashp->NKEYS = 0;
273 hashp->LORDER = BYTE_ORDER;
274 hashp->BSIZE = DEF_BUCKET_SIZE;
275 hashp->BSHIFT = DEF_BUCKET_SHIFT;
276 hashp->SGSIZE = DEF_SEGSIZE;
277 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
278 hashp->DSIZE = DEF_DIRSIZE;
279 hashp->FFACTOR = DEF_FFACTOR;
280 hashp->hash = __default_hash;
281 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
282 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
288 hashp->BSIZE = statbuf.st_blksize;
289 hashp->BSHIFT = __log2(hashp->BSIZE);
295 hashp->BSHIFT = __log2(info->bsize);
296 hashp->BSIZE = 1 << hashp->BSHIFT;
297 if (hashp->BSIZE > MAX_BSIZE) {
303 hashp->FFACTOR = info->ffactor;
305 hashp->hash = info->hash;
314 hashp->LORDER = info->lorder;
318 if (init_htab(hashp, nelem))
321 return (hashp);
330 init_htab(HTAB *hashp, int nelem)
339 nelem = (nelem - 1) / hashp->FFACTOR + 1;
344 hashp->SPARES[l2] = l2 + 1;
345 hashp->SPARES[l2 + 1] = l2 + 1;
346 hashp->OVFL_POINT = l2;
347 hashp->LAST_FREED = 2;
350 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
353 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
354 hashp->HIGH_MASK = (nbuckets << 1) - 1;
355 hashp->HDRPAGES = ((MAXIMUM(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
356 hashp->BSHIFT) + 1;
358 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
361 if (nsegs > hashp->DSIZE)
362 hashp->DSIZE = nsegs;
363 return (alloc_segs(hashp, nsegs));
369 * Flushes any changes to the file if necessary and destroys the hashp
373 hdestroy(HTAB *hashp)
387 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
391 "spares[%d] = %d\n", i, hashp->SPARES[i]);
397 if (__buf_free(hashp, 1, hashp->save_file))
399 if (hashp->dir) {
400 free(*hashp->dir); /* Free initial segments */
402 while (hashp->exsegs--)
403 free(hashp->dir[--hashp->nsegs]);
404 free(hashp->dir);
406 if (flush_meta(hashp) && !save_errno)
409 for (i = 0; i < hashp->nmaps; i++)
410 free(hashp->mapp[i]);
411 free(hashp->tmp_key);
412 free(hashp->tmp_buf);
414 if (hashp->fp != -1)
415 (void)close(hashp->fp);
417 free(hashp);
435 HTAB *hashp;
445 hashp = (HTAB *)dbp->internal;
446 if (!hashp->save_file)
448 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
450 hashp->new_file = 0;
460 flush_meta(HTAB *hashp)
468 if (!hashp->save_file)
470 hashp->MAGIC = HASHMAGIC;
471 hashp->VERSION = HASHVERSION;
472 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
474 fp = hashp->fp;
475 whdrp = &hashp->hdr;
478 swap_header_copy(&hashp->hdr, whdrp);
485 hashp->err = errno;
489 if (hashp->mapp[i])
490 if (__put_page(hashp, (char *)hashp->mapp[i],
491 hashp->BITMAPS[i], 0, 1))
508 HTAB *hashp;
510 hashp = (HTAB *)dbp->internal;
512 hashp->err = errno = EINVAL;
515 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
521 HTAB *hashp;
523 hashp = (HTAB *)dbp->internal;
525 hashp->err = errno = EINVAL;
528 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
529 hashp->err = errno = EPERM;
532 return (hash_access(hashp, flag == R_NOOVERWRITE ?
540 HTAB *hashp;
542 hashp = (HTAB *)dbp->internal;
544 hashp->err = errno = EINVAL;
547 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
548 hashp->err = errno = EPERM;
551 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
555 * Assume that hashp has been set in wrapper routine.
558 hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
571 off = hashp->BSIZE;
574 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
594 rbufp = __get_buf(hashp, *bp, rbufp, 0);
603 off = hashp->BSIZE;
606 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
611 __find_last_page(hashp, &bufp))) {
616 rbufp = __get_buf(hashp, pageno, bufp, 0);
625 off = hashp->BSIZE;
636 if (__addel(hashp, rbufp, key, val)) {
658 if (__big_return(hashp, rbufp, ndx, val, 0))
666 if ((__delpair(hashp, rbufp, ndx)) ||
667 (__addel(hashp, rbufp, key, val))) {
673 if (__delpair(hashp, rbufp, ndx))
688 HTAB *hashp;
691 hashp = (HTAB *)dbp->internal;
693 hashp->err = errno = EINVAL;
699 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
700 hashp->cbucket = 0;
701 hashp->cndx = 1;
702 hashp->cpage = NULL;
706 if (!(bufp = hashp->cpage)) {
707 for (bucket = hashp->cbucket;
708 bucket <= hashp->MAX_BUCKET;
709 bucket++, hashp->cndx = 1) {
710 bufp = __get_buf(hashp, bucket, NULL, 0);
713 hashp->cpage = bufp;
718 hashp->cbucket = bucket;
719 if (hashp->cbucket > hashp->MAX_BUCKET) {
720 hashp->cbucket = -1;
724 bp = (u_int16_t *)hashp->cpage->page;
726 hashp->cndx += 2;
727 if (hashp->cndx > bp[0]) {
728 hashp->cpage = NULL;
729 hashp->cbucket++;
730 hashp->cndx = 1;
740 while (bp[hashp->cndx + 1] == OVFLPAGE) {
741 bufp = hashp->cpage =
742 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
746 hashp->cndx = 1;
749 hashp->cpage = NULL;
750 ++hashp->cbucket;
753 ndx = hashp->cndx;
755 if (__big_keydata(hashp, bufp, key, data, 1))
758 if (hashp->cpage == 0)
760 key->data = (u_char *)hashp->cpage->page + bp[ndx];
761 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
762 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
776 __expand_table(HTAB *hashp)
784 new_bucket = ++hashp->MAX_BUCKET;
785 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
787 new_segnum = new_bucket >> hashp->SSHIFT;
790 if (new_segnum >= hashp->nsegs) {
792 if (new_segnum >= hashp->DSIZE) {
794 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
795 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
797 hashp->DSIZE = dirsize << 1;
799 if ((hashp->dir[new_segnum] =
800 (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
802 hashp->exsegs++;
803 hashp->nsegs++;
810 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
811 if (spare_ndx > hashp->OVFL_POINT) {
812 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
813 hashp->OVFL_POINT = spare_ndx;
816 if (new_bucket > hashp->HIGH_MASK) {
818 hashp->LOW_MASK = hashp->HIGH_MASK;
819 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
822 return (__split_page(hashp, old_bucket, new_bucket));
844 __call_hash(HTAB *hashp, char *k, int len)
848 n = hashp->hash(k, len);
849 bucket = n & hashp->HIGH_MASK;
850 if (bucket > hashp->MAX_BUCKET)
851 bucket = bucket & hashp->LOW_MASK;
861 alloc_segs(HTAB *hashp, int nsegs)
868 if ((hashp->dir =
869 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
871 (void)hdestroy(hashp);
875 hashp->nsegs = nsegs;
879 if ((store = (SEGMENT)calloc(nsegs << hashp->SSHIFT,
882 (void)hdestroy(hashp);
887 hashp->dir[i] = &store[i << hashp->SSHIFT];
924 swap_header(HTAB *hashp)
929 hdrp = &hashp->hdr;