Lines Matching refs:hashp

101 	HTAB *hashp;
111 if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
113 hashp->fp = -1;
118 * field in the hashp structure needs to be accurate so that
121 hashp->flags = flags;
124 if ((hashp->fp = _open(file, flags | O_CLOEXEC, mode)) == -1)
126 new_table = _fstat(hashp->fp, &statbuf) == 0 &&
132 if (!(hashp = init_hash(hashp, file, info)))
137 hashp->hash = info->hash;
139 hashp->hash = __default_hash;
141 hdrsize = _read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
143 swap_header(hashp);
150 if (hashp->MAGIC != HASHMAGIC)
153 if (hashp->VERSION != HASHVERSION &&
154 hashp->VERSION != OLDHASHVERSION)
156 if ((int32_t)hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
163 nsegs = howmany(hashp->MAX_BUCKET + 1, hashp->SGSIZE);
164 if (alloc_segs(hashp, nsegs))
171 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
172 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
173 (hashp->BSHIFT + BYTE_SHIFT);
175 hashp->nmaps = bpages;
176 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
181 __buf_init(hashp, info->cachesize);
183 __buf_init(hashp, DEF_BUFSIZE);
185 hashp->new_file = new_table;
186 hashp->save_file = file && (hashp->flags & O_RDWR);
187 hashp->cbucket = -1;
190 hdestroy(hashp);
194 dbp->internal = hashp;
208 "TABLE POINTER ", hashp,
209 "BUCKET SIZE ", hashp->BSIZE,
210 "BUCKET SHIFT ", hashp->BSHIFT,
211 "DIRECTORY SIZE ", hashp->DSIZE,
212 "SEGMENT SIZE ", hashp->SGSIZE,
213 "SEGMENT SHIFT ", hashp->SSHIFT,
214 "FILL FACTOR ", hashp->FFACTOR,
215 "MAX BUCKET ", hashp->MAX_BUCKET,
216 "OVFL POINT ", hashp->OVFL_POINT,
217 "LAST FREED ", hashp->LAST_FREED,
218 "HIGH MASK ", hashp->HIGH_MASK,
219 "LOW MASK ", hashp->LOW_MASK,
220 "NSEGS ", hashp->nsegs,
221 "NKEYS ", hashp->NKEYS);
229 if (hashp != NULL)
230 (void)_close(hashp->fp);
233 free(hashp);
241 HTAB *hashp;
247 hashp = (HTAB *)dbp->internal;
248 retval = hdestroy(hashp);
256 HTAB *hashp;
261 hashp = (HTAB *)dbp->internal;
262 if (hashp->fp == -1) {
266 return (hashp->fp);
271 init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
277 hashp->NKEYS = 0;
278 hashp->LORDER = BYTE_ORDER;
279 hashp->BSIZE = DEF_BUCKET_SIZE;
280 hashp->BSHIFT = DEF_BUCKET_SHIFT;
281 hashp->SGSIZE = DEF_SEGSIZE;
282 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
283 hashp->DSIZE = DEF_DIRSIZE;
284 hashp->FFACTOR = DEF_FFACTOR;
285 hashp->hash = __default_hash;
286 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
287 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
293 hashp->BSIZE = statbuf.st_blksize;
294 if (hashp->BSIZE > MAX_BSIZE)
295 hashp->BSIZE = MAX_BSIZE;
296 hashp->BSHIFT = __log2(hashp->BSIZE);
302 hashp->BSHIFT = __log2(info->bsize);
303 hashp->BSIZE = 1 << hashp->BSHIFT;
304 if (hashp->BSIZE > MAX_BSIZE) {
310 hashp->FFACTOR = info->ffactor;
312 hashp->hash = info->hash;
321 hashp->LORDER = info->lorder;
325 if (init_htab(hashp, nelem))
328 return (hashp);
337 init_htab(HTAB *hashp, int nelem)
346 nelem = (nelem - 1) / hashp->FFACTOR + 1;
351 hashp->SPARES[l2] = l2 + 1;
352 hashp->SPARES[l2 + 1] = l2 + 1;
353 hashp->OVFL_POINT = l2;
354 hashp->LAST_FREED = 2;
357 if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
360 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
361 hashp->HIGH_MASK = (nbuckets << 1) - 1;
362 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
363 hashp->BSHIFT) + 1;
365 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
368 if (nsegs > hashp->DSIZE)
369 hashp->DSIZE = nsegs;
370 return (alloc_segs(hashp, nsegs));
376 * Flushes any changes to the file if necessary and destroys the hashp
380 hdestroy(HTAB *hashp)
394 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
398 "spares[%d] = %d\n", i, hashp->SPARES[i]);
404 if (__buf_free(hashp, 1, hashp->save_file))
406 if (hashp->dir) {
407 free(*hashp->dir); /* Free initial segments */
409 while (hashp->exsegs--)
410 free(hashp->dir[--hashp->nsegs]);
411 free(hashp->dir);
413 if (flush_meta(hashp) && !save_errno)
416 for (i = 0; i < hashp->nmaps; i++)
417 if (hashp->mapp[i])
418 free(hashp->mapp[i]);
419 if (hashp->tmp_key)
420 free(hashp->tmp_key);
421 if (hashp->tmp_buf)
422 free(hashp->tmp_buf);
424 if (hashp->fp != -1) {
425 if (hashp->save_file)
426 (void)_fsync(hashp->fp);
427 (void)_close(hashp->fp);
430 free(hashp);
448 HTAB *hashp;
458 hashp = (HTAB *)dbp->internal;
459 if (!hashp->save_file)
461 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
463 if (hashp->fp != -1 && _fsync(hashp->fp) != 0)
465 hashp->new_file = 0;
475 flush_meta(HTAB *hashp)
483 if (!hashp->save_file)
485 hashp->MAGIC = HASHMAGIC;
486 hashp->VERSION = HASHVERSION;
487 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
489 fp = hashp->fp;
490 whdrp = &hashp->hdr;
493 swap_header_copy(&hashp->hdr, whdrp);
500 hashp->error = errno;
504 if (hashp->mapp[i])
505 if (__put_page(hashp, (char *)hashp->mapp[i],
506 hashp->BITMAPS[i], 0, 1))
523 HTAB *hashp;
525 hashp = (HTAB *)dbp->internal;
527 hashp->error = errno = EINVAL;
530 return (hash_access(hashp, HASH_GET, (DBT *)key, data));
536 HTAB *hashp;
538 hashp = (HTAB *)dbp->internal;
540 hashp->error = errno = EINVAL;
543 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
544 hashp->error = errno = EPERM;
547 return (hash_access(hashp, flag == R_NOOVERWRITE ?
555 HTAB *hashp;
557 hashp = (HTAB *)dbp->internal;
559 hashp->error = errno = EINVAL;
562 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
563 hashp->error = errno = EPERM;
566 return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
570 * Assume that hashp has been set in wrapper routine.
573 hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
586 off = hashp->BSIZE;
589 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
609 rbufp = __get_buf(hashp, *bp, rbufp, 0);
618 off = hashp->BSIZE;
621 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
626 __find_last_page(hashp, &bufp))) {
631 rbufp = __get_buf(hashp, pageno, bufp, 0);
640 off = hashp->BSIZE;
651 if (__addel(hashp, rbufp, key, val)) {
673 if (__big_return(hashp, rbufp, ndx, val, 0))
681 if ((__delpair(hashp, rbufp, ndx)) ||
682 (__addel(hashp, rbufp, key, val))) {
688 if (__delpair(hashp, rbufp, ndx))
703 HTAB *hashp;
706 hashp = (HTAB *)dbp->internal;
708 hashp->error = errno = EINVAL;
714 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
715 hashp->cbucket = 0;
716 hashp->cndx = 1;
717 hashp->cpage = NULL;
721 if (!(bufp = hashp->cpage)) {
722 for (bucket = hashp->cbucket;
723 bucket <= hashp->MAX_BUCKET;
724 bucket++, hashp->cndx = 1) {
725 bufp = __get_buf(hashp, bucket, NULL, 0);
728 hashp->cpage = bufp;
733 hashp->cbucket = bucket;
734 if ((u_int32_t)hashp->cbucket > hashp->MAX_BUCKET) {
735 hashp->cbucket = -1;
739 bp = (u_int16_t *)hashp->cpage->page;
741 hashp->cndx += 2;
742 if (hashp->cndx > bp[0]) {
743 hashp->cpage = NULL;
744 hashp->cbucket++;
745 hashp->cndx = 1;
755 while (bp[hashp->cndx + 1] == OVFLPAGE) {
756 bufp = hashp->cpage =
757 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
761 hashp->cndx = 1;
764 hashp->cpage = NULL;
765 ++hashp->cbucket;
768 ndx = hashp->cndx;
770 if (__big_keydata(hashp, bufp, key, data, 1))
773 if (hashp->cpage == NULL)
775 key->data = (u_char *)hashp->cpage->page + bp[ndx];
776 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
777 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
791 __expand_table(HTAB *hashp)
799 new_bucket = ++hashp->MAX_BUCKET;
800 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
802 new_segnum = new_bucket >> hashp->SSHIFT;
805 if (new_segnum >= hashp->nsegs) {
807 if (new_segnum >= hashp->DSIZE) {
809 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
810 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
812 hashp->DSIZE = dirsize << 1;
814 if ((hashp->dir[new_segnum] =
815 calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
817 hashp->exsegs++;
818 hashp->nsegs++;
825 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
826 if (spare_ndx > hashp->OVFL_POINT) {
827 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
828 hashp->OVFL_POINT = spare_ndx;
831 if (new_bucket > hashp->HIGH_MASK) {
833 hashp->LOW_MASK = hashp->HIGH_MASK;
834 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
837 return (__split_page(hashp, old_bucket, new_bucket));
859 __call_hash(HTAB *hashp, char *k, int len)
863 n = hashp->hash(k, len);
864 bucket = n & hashp->HIGH_MASK;
865 if (bucket > hashp->MAX_BUCKET)
866 bucket = bucket & hashp->LOW_MASK;
876 alloc_segs(HTAB *hashp, int nsegs)
883 if ((hashp->dir =
884 calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
886 (void)hdestroy(hashp);
890 hashp->nsegs = nsegs;
894 if ((store = calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
896 (void)hdestroy(hashp);
901 hashp->dir[i] = &store[i << hashp->SSHIFT];
938 swap_header(HTAB *hashp)
943 hdrp = &hashp->hdr;