Home | History | Annotate | Download | only in hash

Lines Matching defs:hashp

100 	HTAB *hashp;
111 if (!(hashp = calloc(1, sizeof(HTAB))))
113 hashp->fp = -1;
118 * field in the hashp structure needs to be accurate so that
121 hashp->flags = flags;
131 if ((hashp->fp = __dbopen(file, flags, mode, &statbuf)) == -1)
136 if (!(hashp = init_hash(hashp, file, info)))
141 hashp->hash = info->hash;
143 hashp->hash = __default_hash;
145 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
147 swap_header(hashp);
154 if (hashp->MAGIC != HASHMAGIC)
157 if (hashp->VERSION != HASHVERSION &&
158 hashp->VERSION != OLDHASHVERSION)
160 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) !=
161 (uint32_t)hashp->H_CHARKEY)
168 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
169 hashp->SGSIZE;
170 hashp->nsegs = 0;
171 if (alloc_segs(hashp, nsegs))
178 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
179 (unsigned int)(hashp->BSIZE << BYTE_SHIFT) - 1) >>
180 (hashp->BSHIFT + BYTE_SHIFT);
182 hashp->nmaps = bpages;
183 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(uint32_t *));
188 __buf_init(hashp, info->cachesize);
190 __buf_init(hashp, DEF_BUFSIZE);
192 hashp->new_file = new_table;
193 hashp->save_file = file && (hashp->flags & O_RDWR);
194 hashp->cbucket = -1;
197 hdestroy(hashp);
201 dbp->internal = hashp;
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);
236 if (hashp != NULL)
237 (void)close(hashp->fp);
240 free(hashp);
248 HTAB *hashp;
254 hashp = dbp->internal;
255 retval = hdestroy(hashp);
263 HTAB *hashp;
268 hashp = dbp->internal;
269 if (hashp->fp == -1) {
273 return (hashp->fp);
278 init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
284 hashp->NKEYS = 0;
285 hashp->LORDER = BYTE_ORDER;
286 hashp->BSIZE = DEF_BUCKET_SIZE;
287 hashp->BSHIFT = DEF_BUCKET_SHIFT;
288 hashp->SGSIZE = DEF_SEGSIZE;
289 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
290 hashp->DSIZE = DEF_DIRSIZE;
291 hashp->FFACTOR = DEF_FFACTOR;
292 hashp->hash = __default_hash;
293 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
294 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
300 hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE);
301 hashp->BSHIFT = __log2((uint32_t)hashp->BSIZE);
307 hashp->BSHIFT = __log2(info->bsize);
308 hashp->BSIZE = 1 << hashp->BSHIFT;
309 if (hashp->BSIZE > MAX_BSIZE) {
315 hashp->FFACTOR = info->ffactor;
317 hashp->hash = info->hash;
326 hashp->LORDER = info->lorder;
330 if (init_htab(hashp, (size_t)nelem))
333 return (hashp);
342 init_htab(HTAB *hashp, size_t nelem)
353 nelem = (nelem - 1) / hashp->FFACTOR + 1;
359 hashp->SPARES[l2] = l2 + 1;
360 hashp->SPARES[l2 + 1] = l2 + 1;
361 hashp->OVFL_POINT = l2;
362 hashp->LAST_FREED = 2;
365 if (__ibitmap(hashp, (int)OADDR_OF(l2, 1), l2 + 1, 0))
368 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
369 hashp->HIGH_MASK = (nbuckets << 1) - 1;
371 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
372 hashp->BSHIFT) + 1;
374 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
377 if (nsegs > (uint32_t)hashp->DSIZE)
378 hashp->DSIZE = nsegs;
379 return (alloc_segs(hashp, (int)nsegs));
385 * Flushes any changes to the file if necessary and destroys the hashp
389 hdestroy(HTAB *hashp)
403 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
407 "spares[%d] = %d\n", i, hashp->SPARES[i]);
413 if (__buf_free(hashp, 1, hashp->save_file))
415 if (hashp->dir) {
416 free(*hashp->dir); /* Free initial segments */
418 while (hashp->exsegs--)
419 free(hashp->dir[--hashp->nsegs]);
420 free(hashp->dir);
422 if (flush_meta(hashp) && !save_errno)
425 for (i = 0; i < hashp->nmaps; i++)
426 if (hashp->mapp[i])
427 free(hashp->mapp[i]);
429 if (hashp->fp != -1)
430 (void)close(hashp->fp);
432 free(hashp);
450 HTAB *hashp;
460 hashp = dbp->internal;
461 if (!hashp->save_file)
463 if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
465 hashp->new_file = 0;
475 flush_meta(HTAB *hashp)
484 if (!hashp->save_file)
486 hashp->MAGIC = HASHMAGIC;
487 hashp->VERSION = HASHVERSION;
488 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
490 fp = hashp->fp;
491 whdrp = &hashp->hdr;
494 swap_header_copy(&hashp->hdr, whdrp);
501 hashp->err = errno;
505 if (hashp->mapp[i])
506 if (__put_page(hashp, (char *)(void *)hashp->mapp[i],
507 (u_int)hashp->BITMAPS[i], 0, 1))
524 HTAB *hashp;
526 hashp = dbp->internal;
528 hashp->err = errno = EINVAL;
531 return (hash_access(hashp, HASH_GET, __UNCONST(key), data));
537 HTAB *hashp;
539 hashp = dbp->internal;
541 hashp->err = errno = EINVAL;
544 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
545 hashp->err = errno = EPERM;
549 return (hash_access(hashp, flag == R_NOOVERWRITE ?
556 HTAB *hashp;
558 hashp = dbp->internal;
560 hashp->err = errno = EINVAL;
563 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
564 hashp->err = errno = EPERM;
567 return hash_access(hashp, HASH_DELETE, __UNCONST(key), NULL);
571 * Assume that hashp has been set in wrapper routine.
574 hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
588 off = HASH_BSIZE(hashp);
591 rbufp = __get_buf(hashp, __call_hash(hashp, kp, (int)size), NULL, 0);
611 rbufp = __get_buf(hashp, (uint32_t)*bp, rbufp, 0);
620 off = HASH_BSIZE(hashp);
623 __find_bigpair(hashp, rbufp, ndx, kp, (int)size)) > 0)
628 __find_last_page(hashp, &bufp))) {
633 rbufp = __get_buf(hashp, (uint32_t)pageno,
643 off = HASH_BSIZE(hashp);
654 if (__addel(hashp, rbufp, key, val)) {
676 if (__big_return(hashp, rbufp, ndx, val, 0))
684 if ((__delpair(hashp, rbufp, ndx)) ||
685 (__addel(hashp, rbufp, key, val))) {
691 if (__delpair(hashp, rbufp, ndx))
699 if (ndx != hashp->cndx - 2 || rbufp != hashp->cpage)
702 if (hashp->cndx > 1) {
704 hashp->cndx -= 2;
710 hashp->cbucket--;
711 hashp->cpage = NULL;
712 hashp->cndx = -1;
727 HTAB *hashp;
730 hashp = dbp->internal;
732 hashp->err = errno = EINVAL;
738 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
739 hashp->cbucket = 0;
740 hashp->cndx = 1;
741 hashp->cpage = NULL;
746 if (!(bufp = hashp->cpage)) {
747 for (bucket = hashp->cbucket;
748 bucket <= (uint32_t)hashp->MAX_BUCKET;
750 bufp = __get_buf(hashp, bucket, NULL, 0);
753 hashp->cpage = bufp;
758 hashp->cbucket = bucket;
759 if (hashp->cbucket > hashp->MAX_BUCKET) {
760 hashp->cbucket = -1;
763 if (hashp->cndx == -1) {
765 hashp->cndx = 1;
766 while (bp[hashp->cndx - 1] != 0)
767 hashp->cndx += 2;
770 hashp->cndx = 1;
775 if (hashp->cndx > bp[0]) {
776 hashp->cpage = NULL;
777 hashp->cbucket++;
778 hashp->cndx = 1;
787 while (bp[hashp->cndx + 1] == OVFLPAGE) {
788 bufp = hashp->cpage =
789 __get_buf(hashp, (uint32_t)bp[hashp->cndx], bufp,
794 hashp->cndx = 1;
797 hashp->cpage = NULL;
798 ++hashp->cbucket;
801 ndx = hashp->cndx;
803 if (__big_keydata(hashp, bufp, key, data, 1))
805 hashp->cndx = 1;
807 if (hashp->cpage == NULL)
809 key->data = (uint8_t *)hashp->cpage->page + bp[ndx];
810 key->size = (ndx > 1 ? bp[ndx - 1] : HASH_BSIZE(hashp)) - bp[ndx];
811 data->data = (uint8_t *)hashp->cpage->page + bp[ndx + 1];
813 hashp->cndx += 2;
826 __expand_table(HTAB *hashp)
835 new_bucket = ++hashp->MAX_BUCKET;
836 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
838 new_segnum = new_bucket >> hashp->SSHIFT;
841 if (new_segnum >= hashp->nsegs) {
843 if (new_segnum >= hashp->DSIZE) {
845 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
846 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
850 hashp->DSIZE = (uint32_t)dirsize;
852 if ((hashp->dir[new_segnum] =
853 calloc((size_t)hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
855 hashp->exsegs++;
856 hashp->nsegs++;
863 spare_ndx = __log2((uint32_t)(hashp->MAX_BUCKET + 1));
864 if (spare_ndx > hashp->OVFL_POINT) {
865 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
866 hashp->OVFL_POINT = spare_ndx;
869 if (new_bucket > (uint32_t)hashp->HIGH_MASK) {
871 hashp->LOW_MASK = hashp->HIGH_MASK;
872 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
875 return (__split_page(hashp, old_bucket, new_bucket));
897 __call_hash(HTAB *hashp, char *k, int len)
901 n = hashp->hash(k, (size_t)len);
902 bucket = n & hashp->HIGH_MASK;
903 if (bucket > hashp->MAX_BUCKET)
904 bucket = bucket & hashp->LOW_MASK;
914 alloc_segs(HTAB *hashp, int nsegs)
921 hashp->dir = calloc((size_t)hashp->DSIZE, sizeof(SEGMENT *));
922 if (hashp->dir == NULL) {
924 (void)hdestroy(hashp);
928 hashp->nsegs = nsegs;
932 store = calloc((size_t)(nsegs << hashp->SSHIFT), sizeof(SEGMENT));
935 (void)hdestroy(hashp);
940 hashp->dir[i] = &store[i << hashp->SSHIFT];
946 * Hashp->hdr needs to be byteswapped.
977 swap_header(HTAB *hashp)
982 hdrp = &hashp->hdr;