Lines Matching +full:max +full:- +full:size
1 /*-
72 /* Macros for min/max. */
76 #ifndef MAX
77 #define MAX(a,b) (((a)>(b))?(a):(b)) macro
81 #define MOD(x, y) ((x) & ((y) - 1))
87 #define ERROR (-1)
122 hashp->fp = -1;
130 hashp->flags = flags;
144 if ((hashp->fp = open(file, flags, mode)) == -1)
151 fstat64(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
153 fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
158 (void)fcntl(hashp->fp, F_SETFD, 1);
166 if (info && info->hash)
167 hashp->hash = info->hash;
169 hashp->hash = __default_hash;
171 hdrsize = read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
175 if (hdrsize == -1)
180 if (hashp->MAGIC != HASHMAGIC)
183 if (hashp->HASH_VERSION != HASHVERSION &&
184 hashp->HASH_VERSION != OLDHASHVERSION)
186 if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
188 /* Check bucket size isn't too big for target int. */
189 if (hashp->BSIZE > INT_MAX)
196 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
197 hashp->SGSIZE;
198 hashp->nsegs = 0;
206 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
207 (hashp->BSIZE << BYTE_SHIFT) - 1) >>
208 (hashp->BSHIFT + BYTE_SHIFT);
210 hashp->nmaps = bpages;
211 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(__uint32_t *));
215 if (info && info->cachesize)
216 __buf_init(hashp, info->cachesize);
220 hashp->new_file = new_table;
221 hashp->save_file = file && (hashp->flags & O_RDWR);
222 hashp->cbucket = -1;
229 dbp->internal = hashp;
230 dbp->close = hash_close;
231 dbp->del = hash_delete;
232 dbp->fd = hash_fd;
233 dbp->get = hash_get;
234 dbp->put = hash_put;
235 dbp->seq = hash_seq;
236 dbp->sync = hash_sync;
237 dbp->type = DB_HASH;
244 "BUCKET SIZE ", hashp->BSIZE,
245 "BUCKET SHIFT ", hashp->BSHIFT,
246 "DIRECTORY SIZE ", hashp->DSIZE,
247 "SEGMENT SIZE ", hashp->SGSIZE,
248 "SEGMENT SHIFT ", hashp->SSHIFT,
249 "FILL FACTOR ", hashp->FFACTOR,
250 "MAX BUCKET ", hashp->MAX_BUCKET,
251 "OVFL POINT ", hashp->OVFL_POINT,
252 "LAST FREED ", hashp->LAST_FREED,
253 "HIGH MASK ", hashp->HIGH_MASK,
254 "LOW MASK ", hashp->LOW_MASK,
255 "NSEGS ", hashp->nsegs,
256 "NKEYS ", hashp->NKEYS);
265 (void)close(hashp->fp);
282 hashp = (HTAB *)dbp->internal;
296 hashp = (HTAB *)dbp->internal;
297 if (hashp->fp == -1) {
299 return (-1);
301 return (hashp->fp);
318 hashp->NKEYS = 0;
319 hashp->LORDER = DB_BYTE_ORDER;
320 hashp->BSIZE = DEF_BUCKET_SIZE;
321 hashp->BSHIFT = DEF_BUCKET_SHIFT;
322 hashp->SGSIZE = DEF_SEGSIZE;
323 hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
324 hashp->DSIZE = DEF_DIRSIZE;
325 hashp->FFACTOR = DEF_FFACTOR;
326 hashp->hash = __default_hash;
327 memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
328 memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
330 /* Fix bucket size to be optimal for file system */
338 hashp->BSIZE = MIN(statbuf.st_blksize, MAX_BSIZE);
339 hashp->BSHIFT = __log2(hashp->BSIZE);
343 if (info->bsize) {
345 hashp->BSHIFT = __log2(info->bsize);
346 hashp->BSIZE = 1 << hashp->BSHIFT;
347 if (hashp->BSIZE > MAX_BSIZE) {
352 if (info->ffactor)
353 hashp->FFACTOR = info->ffactor;
354 if (info->hash)
355 hashp->hash = info->hash;
356 if (info->nelem)
357 nelem = info->nelem;
358 if (info->lorder) {
359 if (info->lorder != DB_BIG_ENDIAN &&
360 info->lorder != DB_LITTLE_ENDIAN) {
364 hashp->LORDER = info->lorder;
390 nelem = (nelem - 1) / hashp->FFACTOR + 1;
392 l2 = __log2(MAX(nelem, 2));
395 hashp->SPARES[l2] = l2 + 1;
396 hashp->SPARES[l2 + 1] = l2 + 1;
397 hashp->OVFL_POINT = l2;
398 hashp->LAST_FREED = 2;
402 return (-1);
404 hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
405 hashp->HIGH_MASK = (nbuckets << 1) - 1;
406 hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
407 hashp->BSHIFT) + 1;
409 nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
412 if (nsegs > hashp->DSIZE)
413 hashp->DSIZE = nsegs;
438 hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
442 "spares[%d] = %d\n", i, hashp->SPARES[i]);
448 if (__buf_free(hashp, 1, hashp->save_file))
450 if (hashp->dir) {
451 free(*hashp->dir); /* Free initial segments */
453 while (hashp->exsegs--)
454 free(hashp->dir[--hashp->nsegs]);
455 free(hashp->dir);
460 for (i = 0; i < hashp->nmaps; i++)
461 if (hashp->mapp[i])
462 free(hashp->mapp[i]);
464 if (hashp->fp != -1)
465 (void)close(hashp->fp);
480 * -1 ERROR
495 hashp = (HTAB *)dbp->internal;
496 if (!hashp->save_file)
500 hashp->new_file = 0;
507 * -1 indicates that errno should be set
518 if (!hashp->save_file)
520 hashp->MAGIC = HASHMAGIC;
521 hashp->HASH_VERSION = HASHVERSION;
522 hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
524 fp = hashp->fp;
525 whdrp = &hashp->hdr;
528 swap_header_copy(&hashp->hdr, whdrp);
530 if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
531 ((wsize = write(fp, whdrp, sizeof(HASHHDR))) == -1))
532 return (-1);
536 hashp->error = errno;
537 return (-1);
540 if (hashp->mapp[i])
541 if (__put_page(hashp, (char *)hashp->mapp[i],
542 hashp->BITMAPS[i], 0, 1))
543 return (-1);
554 * -1 to indicate an internal ERROR (i.e. out of memory, etc)
564 hashp = (HTAB *)dbp->internal;
566 hashp->error = errno = EINVAL;
580 hashp = (HTAB *)dbp->internal;
582 hashp->error = EINVAL;
586 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
587 hashp->error = errno = EPERM;
601 hashp = (HTAB *)dbp->internal;
603 hashp->error = errno = EINVAL;
606 if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
607 hashp->error = errno = EPERM;
625 int n, ndx, off, size; local
633 off = hashp->BSIZE;
634 size = key->size;
635 kp = (char *)key->data;
636 rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
642 rbufp->flags |= BUF_PIN;
643 for (bp = (__uint16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
646 if (size == off - (int) *bp &&
647 memcmp(kp, rbufp->page + *bp, size) == 0)
658 save_bufp->flags &= ~BUF_PIN;
662 bp = (__uint16_t *)rbufp->page;
665 off = hashp->BSIZE;
668 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
670 if (ndx == -2) {
680 save_bufp->flags &= ~BUF_PIN;
684 bp = (__uint16_t *)rbufp->page;
687 off = hashp->BSIZE;
689 save_bufp->flags &= ~BUF_PIN;
699 save_bufp->flags &= ~BUF_PIN;
702 save_bufp->flags &= ~BUF_PIN;
708 save_bufp->flags &= ~BUF_PIN;
715 save_bufp->flags &= ~BUF_PIN;
718 bp = (__uint16_t *)rbufp->page;
723 val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
724 val->size = bp[ndx] - bp[ndx + 1];
730 save_bufp->flags &= ~BUF_PIN;
741 save_bufp->flags &= ~BUF_PIN;
756 hashp = (HTAB *)dbp->internal;
758 hashp->error = errno = EINVAL;
764 if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
765 hashp->cbucket = 0;
766 hashp->cndx = 1;
767 hashp->cpage = NULL;
771 if (!(bufp = hashp->cpage)) {
772 for (bucket = hashp->cbucket;
773 bucket <= hashp->MAX_BUCKET;
774 bucket++, hashp->cndx = 1) {
778 hashp->cpage = bufp;
779 bp = (__uint16_t *)bufp->page;
783 hashp->cbucket = bucket;
784 if (hashp->cbucket > hashp->MAX_BUCKET) {
785 hashp->cbucket = -1;
789 bp = (__uint16_t *)hashp->cpage->page;
794 while (bp[hashp->cndx + 1] == OVFLPAGE) {
795 bufp = hashp->cpage =
796 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
799 bp = (__uint16_t *)(bufp->page);
800 hashp->cndx = 1;
803 hashp->cpage = NULL;
804 ++hashp->cbucket;
807 ndx = hashp->cndx;
812 key->data = (u_char *)hashp->cpage->page + bp[ndx];
813 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
814 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
815 data->size = bp[ndx] - bp[ndx + 1];
818 hashp->cpage = NULL;
819 hashp->cbucket++;
820 hashp->cndx = 1;
822 hashp->cndx = ndx;
832 * -1 ==> Error
843 new_bucket = ++hashp->MAX_BUCKET;
844 old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
846 new_segnum = new_bucket >> hashp->SSHIFT;
849 if (new_segnum >= hashp->nsegs) {
851 if (new_segnum >= hashp->DSIZE) {
853 dirsize = hashp->DSIZE * sizeof(SEGMENT *);
854 if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
855 return (-1);
856 hashp->DSIZE = dirsize << 1;
858 SEGMENT seg = calloc(hashp->SGSIZE, sizeof(SEGMENT));
860 return (-1);
861 hashp->dir[new_segnum] = seg;
862 hashp->exsegs++;
863 hashp->nsegs++;
870 spare_ndx = __log2(hashp->MAX_BUCKET + 1);
871 if (spare_ndx > hashp->OVFL_POINT) {
872 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
873 hashp->OVFL_POINT = spare_ndx;
876 if (new_bucket > hashp->HIGH_MASK) {
878 hashp->LOW_MASK = hashp->HIGH_MASK;
879 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
896 memset((char *)p + oldsize, 0, newsize - oldsize);
910 n = hashp->hash(k, len);
911 bucket = n & hashp->HIGH_MASK;
912 if (bucket > hashp->MAX_BUCKET)
913 bucket = bucket & hashp->LOW_MASK;
930 if ((hashp->dir =
931 (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
935 return (-1);
939 (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
943 return (-1);
945 hashp->dir[0] = store;
946 for (i = 1; i < nsegs; i++, hashp->nsegs++)
947 hashp->dir[i] = &store[i << hashp->SSHIFT];
953 * Hashp->hdr needs to be byteswapped.
960 P_32_COPY(srcp->magic, destp->magic);
961 P_32_COPY(srcp->version, destp->version);
962 P_32_COPY(srcp->lorder, destp->lorder);
963 P_32_COPY(srcp->bsize, destp->bsize);
964 P_32_COPY(srcp->bshift, destp->bshift);
965 P_32_COPY(srcp->dsize, destp->dsize);
966 P_32_COPY(srcp->ssize, destp->ssize);
967 P_32_COPY(srcp->sshift, destp->sshift);
968 P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
969 P_32_COPY(srcp->last_freed, destp->last_freed);
970 P_32_COPY(srcp->max_bucket, destp->max_bucket);
971 P_32_COPY(srcp->high_mask, destp->high_mask);
972 P_32_COPY(srcp->low_mask, destp->low_mask);
973 P_32_COPY(srcp->ffactor, destp->ffactor);
974 P_32_COPY(srcp->nkeys, destp->nkeys);
975 P_32_COPY(srcp->hdrpages, destp->hdrpages);
976 P_32_COPY(srcp->h_charkey, destp->h_charkey);
978 P_32_COPY(srcp->spares[i], destp->spares[i]);
979 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
989 hdrp = &hashp->hdr;
991 M_32_SWAP(hdrp->magic);
992 M_32_SWAP(hdrp->version);
993 M_32_SWAP(hdrp->lorder);
994 M_32_SWAP(hdrp->bsize);
995 M_32_SWAP(hdrp->bshift);
996 M_32_SWAP(hdrp->dsize);
997 M_32_SWAP(hdrp->ssize);
998 M_32_SWAP(hdrp->sshift);
999 M_32_SWAP(hdrp->ovfl_point);
1000 M_32_SWAP(hdrp->last_freed);
1001 M_32_SWAP(hdrp->max_bucket);
1002 M_32_SWAP(hdrp->high_mask);
1003 M_32_SWAP(hdrp->low_mask);
1004 M_32_SWAP(hdrp->ffactor);
1005 M_32_SWAP(hdrp->nkeys);
1006 M_32_SWAP(hdrp->hdrpages);
1007 M_32_SWAP(hdrp->h_charkey);
1009 M_32_SWAP(hdrp->spares[i]);
1010 M_16_SWAP(hdrp->bitmaps[i]);