ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
(Generate patch)

Comparing ray/src/hd/holofile.c (file contents):
Revision 3.30 by gwlarson, Thu Dec 10 10:45:02 1998 UTC vs.
Revision 3.39 by gwlarson, Tue Mar 9 08:40:11 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1998 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1999 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 13 | Line 13 | static char SCCSid[] = "$SunId$ SGI";
13   #include "holo.h"
14  
15   #ifndef CACHESIZE
16 < #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
16 > #ifdef BIGMEM
17 > #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
18 > #else
19 > #define CACHESIZE       5
20   #endif
21 + #endif
22   #ifndef FREEBEAMS
23   #define FREEBEAMS       1500    /* maximum beams to free at a time */
24   #endif
25   #ifndef PCTFREE
26   #define PCTFREE         15      /* maximum fraction to free (%) */
27   #endif
28 < #ifndef MAXFRAG
29 < #define MAXFRAG         8191    /* maximum fragments/file to track (0==inf) */
28 > #ifndef MAXFRAGB
29 > #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
30   #endif
31 + #ifndef FF_DEFAULT
32 +                                /* when to free a beam fragment */
33 + #define FF_DEFAULT      (FF_WRITE|FF_KILL)
34 + #endif
35   #ifndef MINDIRSEL
36                                  /* minimum directory seek length */
37   #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
# Line 34 | Line 42 | static char SCCSid[] = "$SunId$ SGI";
42   #define read    readbuf
43   #endif
44  
45 < #define FRAGBLK         256     /* number of fragments to allocate at a time */
45 > #define FRAGBLK         512     /* number of fragments to allocate at a time */
46  
47 + int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */
48   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
49 < unsigned long   hdclock;        /* clock value */
49 > unsigned long   hdclock;                /* clock value */
50  
51   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
52  
# Line 182 | Line 191 | memerr:
191   }
192  
193  
194 < markdirty(hp, i)                /* mark holodeck directory position dirty */
194 > hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
195   register HOLO   *hp;
196   int     i;
197   {
# Line 194 | Line 203 | int    i;
203                  if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
204                                  || write(hp->fd, (char *)&smudge,
205                                          sizeof(BEAMI)) != sizeof(BEAMI))
206 <                        error(SYSTEM, "seek/write error in markdirty");
206 >                        error(SYSTEM, "seek/write error in hdmarkdirty");
207                  hp->dirseg[0].s = i;
208                  hp->dirseg[0].n = 1;
209                  return;
# Line 349 | Line 358 | int    nr;                     /* number of new rays desired */
358          RAYVAL  *p;
359          int     n;
360  
361 <        if (nr <= 0)
362 <                return(NULL);
363 <        if (i < 1 | i > nbeams(hp))
355 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
361 >        if (nr <= 0) return(NULL);
362 >        CHECK(i < 1 | i > nbeams(hp),
363 >                        CONSISTENCY, "bad beam index given to hdnewrays");
364          if (hp->bl[i] != NULL)
365                  hp->bl[i]->tick = hdclock;      /* preempt swap */
366          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
# Line 361 | Line 369 | int    nr;                     /* number of new rays desired */
369                  n = hp->bi[i].nrd + nr;
370                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
371                  blglob(hp)->nrm += n;
372 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
372 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
373                          errno = 0;
374                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
375                                  error(SYSTEM, "seek error on holodeck file");
# Line 375 | Line 383 | int    nr;                     /* number of new rays desired */
383                                  hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
384                  blglob(hp)->nrm += nr;
385          }
386 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
387 +                hdfreefrag(hp, i);              /* relinquish old fragment */
388          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
389          hp->bl[i]->nrm += nr;                   /* update in-core structure */
390          bzero((char *)p, nr*sizeof(RAYVAL));
# Line 390 | Line 400 | register int   i;
400   {
401          register int    n;
402  
403 <        if (i < 1 | i > nbeams(hp))
404 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
403 >        CHECK(i < 1 | i > nbeams(hp),
404 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
405          if (hp->bl[i] == NULL) {                /* load from disk */
406                  if (!(n = hp->bi[i].nrd))
407                          return(NULL);
# Line 405 | Line 415 | register int   i;
415                  n *= sizeof(RAYVAL);
416                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
417                          error(SYSTEM, "error reading beam from holodeck file");
418 +                if (hdfragflags&FF_READ)
419 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
420          }
421          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
422          return(hp->bl[i]);
# Line 442 | Line 454 | int    (*bf)();                /* callback function (optional) */
454                                          /* precheck consistency */
455          if (n <= 0) return;
456          for (i = n; i--; )
457 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
457 >                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
458                          error(CONSISTENCY, "bad beam in hdloadbeams");
459                                          /* sort list for optimal access */
460          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
# Line 475 | Line 487 | int    (*bf)();                /* callback function (optional) */
487   }
488  
489  
490 < hdfreefrag(fd, bi)                      /* free a file fragment */
491 < int     fd;
492 < register BEAMI  *bi;
490 > int
491 > hdfreefrag(hp, i)                       /* free a file fragment */
492 > HOLO    *hp;
493 > int     i;
494   {
495 +        register BEAMI  *bi = &hp->bi[i];
496          register struct fraglist        *f;
497          register int    j, k;
498  
499 <        if (bi->nrd == 0)
500 <                return;
501 < #ifdef DEBUG
502 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
503 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
490 < #endif
491 <        f = &hdfragl[fd];
499 >        if (bi->nrd <= 0)
500 >                return(0);
501 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
502 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
503 >        f = &hdfragl[hp->fd];
504          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
505                  for (j = k = 0; k < f->nfrags; j++, k++) {
506                          while (f->fi[k].nrd == 0)
# Line 501 | Line 513 | register BEAMI *bi;
513                  f->nfrags = j;
514          }
515          j = f->nfrags++;                /* allocate a slot in free list */
516 < #if MAXFRAG
517 <        if (j >= MAXFRAG-1)
518 <                f->nfrags--;
516 > #if MAXFRAGB
517 >        if (j >= MAXFRAGB*FRAGBLK) {
518 >                f->nfrags = j--;        /* stop list growth */
519 >                if (bi->nrd <= f->fi[j].nrd)
520 >                        return(0);      /* new one no better than discard */
521 >        }
522   #endif
523 <        if (j % FRAGBLK == 0) {         /* more free list space */
523 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
524                  register BEAMI  *newp;
525                  if (f->fi == NULL)
526                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
# Line 514 | Line 529 | register BEAMI *bi;
529                                          (j+FRAGBLK)*sizeof(BEAMI));
530                  if (newp == NULL) {
531                          f->nfrags--;    /* graceful failure */
532 <                        return;
532 >                        return(0);
533                  }
534                  f->fi = newp;
535          }
# Line 541 | Line 556 | register BEAMI *bi;
556                          }
557                          break;
558                  }
559 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
560 +        bi->nrd = 0;
561 +        bi->fo = 0L;
562 +        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
563 +        return(1);
564   }
565  
566  
567 + int
568 + hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
569 + int     fd;
570 + int     *listlen;
571 + register int4   *listsiz;
572 + {
573 +        register struct fraglist        *f;
574 +        register int    i;
575 +
576 +        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
577 +                return(0);              /* listless */
578 +        if (listlen != NULL)
579 +                *listlen = f->nfrags;
580 +        if (listsiz != NULL)
581 +                for (i = f->nfrags, *listsiz = 0; i--; )
582 +                        *listsiz += f->fi[i].nrd;
583 + #if MAXFRAGB
584 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
585 + #else
586 +        return(1);                      /* list never fills up */
587 + #endif
588 + }
589 +
590 +
591   long
592   hdallocfrag(fd, nrays)          /* allocate a file fragment */
593   int     fd;
594   unsigned int4   nrays;
595   {
596          register struct fraglist        *f;
597 <        register int    j, k;
597 >        register int    j;
598          long    nfo;
599  
600          if (nrays == 0)
601                  return(-1L);
602 < #ifdef DEBUG
603 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
560 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
561 < #endif
602 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
603 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
604          f = &hdfragl[fd];
605 <        k = -1;                         /* find closest-sized fragment */
606 <        for (j = f->nfrags; j-- > 0; )
607 <                if (f->fi[j].nrd >= nrays &&
608 <                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
567 <                        if (f->fi[k=j].nrd == nrays)
568 <                                break;
569 <        if (k < 0) {                    /* no fragment -- extend file */
605 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
606 >                if (f->fi[j].nrd >= nrays)
607 >                        break;
608 >        if (j < 0) {                    /* no fragment -- extend file */
609                  nfo = f->flen;
610                  f->flen += nrays*sizeof(RAYVAL);
611          } else {                        /* else use fragment */
612 <                nfo = f->fi[k].fo;
613 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
614 <                f->fi[k].nrd -= nrays;
612 >                nfo = f->fi[j].fo;
613 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
614 >                f->fi[j].nrd -= nrays;
615          }
616          return(nfo);
617   }
# Line 583 | Line 622 | hdsyncbeam(hp, i)              /* sync beam in memory with beam on
622   register HOLO   *hp;
623   register int    i;
624   {
625 +        int     fragfreed;
626          unsigned int4   nrays;
627          unsigned int    n;
628          long    nfo;
629                                          /* check file status */
630          if (hdfragl[hp->fd].writerr)
631                  return(-1);
632 < #ifdef DEBUG
633 <        if (i < 1 | i > nbeams(hp))
594 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
595 < #endif
632 >        DCHECK(i < 1 | i > nbeams(hp),
633 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
634                                          /* is current fragment OK? */
635          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
636                  return(0);
637 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
638 <                hdfreefrag(hp->fd, &hp->bi[i]);
637 >                                        /* relinquish old fragment? */
638 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
639          if (nrays) {                    /* get and write new fragment */
640                  nfo = hdallocfrag(hp->fd, nrays);
641                  errno = 0;
# Line 614 | Line 652 | register int   i;
652                  hp->bi[i].fo = 0L;
653          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
654          hp->bi[i].nrd = nrays;
655 <        markdirty(hp, i);               /* section directory now out of date */
655 >        if (!fragfreed)
656 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
657          return(1);
658   }
659  
# Line 635 | Line 674 | register int   i;
674          if (hdfragl[hp->fd].writerr)    /* check for file error */
675                  return(0);
676          if (i == 0) {                   /* clear entire holodeck */
677 +                if (blglob(hp)->nrm == 0)
678 +                        return(0);              /* already clear */
679                  nchanged = 0;
680                  for (i = nbeams(hp); i > 0; i--)
681                          if (hp->bl[i] != NULL)
682                                  nchanged += hdfreebeam(hp, i);
683 +                DCHECK(blglob(hp)->nrm != 0,
684 +                                CONSISTENCY, "bad beam count in hdfreebeam");
685                  return(nchanged);
686          }
687 < #ifdef DEBUG
688 <        if (i < 1 | i > nbeams(hp))
646 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
647 < #endif
687 >        DCHECK(i < 1 | i > nbeams(hp),
688 >                        CONSISTENCY, "bad beam index to hdfreebeam");
689          if (hp->bl[i] == NULL)
690                  return(0);
691                                          /* check for additions */
# Line 663 | Line 704 | hdkillbeam(hp, i)              /* delete beam from holodeck */
704   register HOLO   *hp;
705   register int    i;
706   {
666        static BEAM     emptybeam;
707          int     nchanged;
708  
709          if (hp == NULL) {               /* clobber all holodecks */
# Line 673 | Line 713 | register int   i;
713                  return(nchanged);
714          }
715          if (i == 0) {                   /* clobber entire holodeck */
716 +                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
717 +                        return(0);              /* already empty */
718                  nchanged = 0;
719 +                nchanged = 0;
720                  for (i = nbeams(hp); i > 0; i--)
721                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
722                                  nchanged += hdkillbeam(hp, i);
723 < #ifdef DEBUG
724 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
682 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
683 < #endif
723 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
724 >                                CONSISTENCY, "bad beam count in hdkillbeam");
725                  return(nchanged);
726          }
727 < #ifdef DEBUG
728 <        if (i < 1 | i > nbeams(hp))
688 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
689 < #endif
727 >        DCHECK(i < 1 | i > nbeams(hp),
728 >                        CONSISTENCY, "bad beam index to hdkillbeam");
729          if (hp->bl[i] != NULL) {        /* free memory */
730                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
731                  free((char *)hp->bl[i]);
732 +                hp->bl[i] = NULL;
733          } else
734                  nchanged = hp->bi[i].nrd;
735 <        if (hp->bi[i].nrd) {            /* free file fragment */
736 <                hp->bl[i] = &emptybeam;
737 <                hdsyncbeam(hp, i);
735 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
736 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
737 >                hp->bi[i].nrd = 0;
738 >                hp->bi[i].fo = 0L;
739 >                hdmarkdirty(hp, i);
740          }
699        hp->bl[i] = NULL;
741          return(nchanged);
742   }
743  
# Line 790 | Line 831 | register HOLO  *hp;            /* NULL means clean up all */
831                  return;
832          }
833                                          /* flush all data and free memory */
834 <        hdfreebeam(hp, 0);
794 <        hdsync(hp, 0);
834 >        hdflush(hp);
835                                          /* release fragment resources */
836          hdrelease(hp->fd);
837                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines