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.32 by gwlarson, Thu Jan 7 22:04:49 1999 UTC vs.
Revision 3.40 by gwlarson, Fri Mar 12 09:37:48 1999 UTC

# 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
# Line 26 | Line 30 | static char SCCSid[] = "$SunId$ SGI";
30   #endif
31   #ifndef FF_DEFAULT
32                                  /* when to free a beam fragment */
33 < #define FF_DEFAULT      (FF_ALLOC|FF_WRITE|FF_KILL)
33 > #define FF_DEFAULT      (FF_WRITE|FF_KILL)
34   #endif
35   #ifndef MINDIRSEL
36                                  /* minimum directory seek length */
# Line 57 | Line 61 | static struct fraglist {
61   static int      nhdfragls;      /* size of hdfragl array */
62  
63  
64 + HOLO *
65 + hdalloc(hproto)         /* allocate and set holodeck section based on grid */
66 + HDGRID  *hproto;
67 + {
68 +        HOLO    hdhead;
69 +        register HOLO   *hp;
70 +        int     n;
71 +                                /* copy grid to temporary header */
72 +        bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID));
73 +                                /* compute grid vectors and sizes */
74 +        hdcompgrid(&hdhead);
75 +                                /* allocate header with directory */
76 +        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
77 +        if ((hp = (HOLO *)malloc(n)) == NULL)
78 +                return(NULL);
79 +                                /* copy header information */
80 +        copystruct(hp, &hdhead);
81 +                                /* allocate and clear beam list */
82 +        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
83 +        if (hp->bl == NULL) {
84 +                free((char *)hp);
85 +                return(NULL);
86 +        }
87 +        bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
88 +        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
89 +        hp->fd = -1;
90 +        hp->dirty = 0;
91 +        hp->priv = NULL;
92 +                                /* clear beam directory */
93 +        bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI));
94 +        return(hp);             /* all is well */
95 + }
96 +
97 +
98   char *
99   hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
100   char    *ptr;
# Line 187 | Line 225 | memerr:
225   }
226  
227  
228 < markdirty(hp, i)                /* mark holodeck directory position dirty */
228 > hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
229   register HOLO   *hp;
230   int     i;
231   {
# Line 199 | Line 237 | int    i;
237                  if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
238                                  || write(hp->fd, (char *)&smudge,
239                                          sizeof(BEAMI)) != sizeof(BEAMI))
240 <                        error(SYSTEM, "seek/write error in markdirty");
240 >                        error(SYSTEM, "seek/write error in hdmarkdirty");
241                  hp->dirseg[0].s = i;
242                  hp->dirseg[0].n = 1;
243                  return;
# Line 483 | Line 521 | int    (*bf)();                /* callback function (optional) */
521   }
522  
523  
524 + int
525   hdfreefrag(hp, i)                       /* free a file fragment */
526   HOLO    *hp;
527   int     i;
# Line 492 | Line 531 | int    i;
531          register int    j, k;
532  
533          if (bi->nrd <= 0)
534 <                return;
534 >                return(0);
535          DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
536                          CONSISTENCY, "bad file descriptor in hdfreefrag");
537          f = &hdfragl[hp->fd];
# Line 512 | Line 551 | int    i;
551          if (j >= MAXFRAGB*FRAGBLK) {
552                  f->nfrags = j--;        /* stop list growth */
553                  if (bi->nrd <= f->fi[j].nrd)
554 <                        return;         /* new one no better than discard */
554 >                        return(0);      /* new one no better than discard */
555          }
556   #endif
557          if (j % FRAGBLK == 0) {         /* more (or less) free list space */
# Line 524 | Line 563 | int    i;
563                                          (j+FRAGBLK)*sizeof(BEAMI));
564                  if (newp == NULL) {
565                          f->nfrags--;    /* graceful failure */
566 <                        return;
566 >                        return(0);
567                  }
568                  f->fi = newp;
569          }
# Line 553 | Line 592 | int    i;
592                  }
593          biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
594          bi->nrd = 0;
595 <        bi->fo = 0;
595 >        bi->fo = 0L;
596 >        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
597 >        return(1);
598   }
599  
600  
# Line 587 | Line 628 | int    fd;
628   unsigned int4   nrays;
629   {
630          register struct fraglist        *f;
631 <        register int    j, k;
631 >        register int    j;
632          long    nfo;
633  
634          if (nrays == 0)
# Line 595 | Line 636 | unsigned int4  nrays;
636          DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
637                          CONSISTENCY, "bad file descriptor in hdallocfrag");
638          f = &hdfragl[fd];
639 <        k = -1;                         /* find closest-sized fragment */
640 <        for (j = f->nfrags; j-- > 0; )
641 <                if (f->fi[j].nrd >= nrays &&
642 <                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
602 <                        if (f->fi[k=j].nrd == nrays)
603 <                                break;
604 <        if (k < 0) {                    /* no fragment -- extend file */
639 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
640 >                if (f->fi[j].nrd >= nrays)
641 >                        break;
642 >        if (j < 0) {                    /* no fragment -- extend file */
643                  nfo = f->flen;
644                  f->flen += nrays*sizeof(RAYVAL);
645          } else {                        /* else use fragment */
646 <                nfo = f->fi[k].fo;
647 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
648 <                f->fi[k].nrd -= nrays;
646 >                nfo = f->fi[j].fo;
647 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
648 >                f->fi[j].nrd -= nrays;
649          }
650          return(nfo);
651   }
# Line 618 | Line 656 | hdsyncbeam(hp, i)              /* sync beam in memory with beam on
656   register HOLO   *hp;
657   register int    i;
658   {
659 +        int     fragfreed;
660          unsigned int4   nrays;
661          unsigned int    n;
662          long    nfo;
# Line 629 | Line 668 | register int   i;
668                                          /* is current fragment OK? */
669          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
670                  return(0);
671 <        if (hdfragflags&FF_WRITE && hp->bi[i].nrd)
672 <                hdfreefrag(hp, i);      /* relinquish old fragment */
671 >                                        /* relinquish old fragment? */
672 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
673          if (nrays) {                    /* get and write new fragment */
674                  nfo = hdallocfrag(hp->fd, nrays);
675                  errno = 0;
# Line 645 | Line 684 | register int   i;
684                  hp->bi[i].fo = nfo;
685          } else
686                  hp->bi[i].fo = 0L;
687 <        biglob(hp)->nrd += hp->bi[i].nrd = nrays;
688 <        markdirty(hp, i);               /* section directory now out of date */
687 >        biglob(hp)->nrd += nrays - hp->bi[i].nrd;
688 >        hp->bi[i].nrd = nrays;
689 >        if (!fragfreed)
690 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
691          return(1);
692   }
693  
# Line 667 | Line 708 | register int   i;
708          if (hdfragl[hp->fd].writerr)    /* check for file error */
709                  return(0);
710          if (i == 0) {                   /* clear entire holodeck */
711 +                if (blglob(hp)->nrm == 0)
712 +                        return(0);              /* already clear */
713                  nchanged = 0;
714                  for (i = nbeams(hp); i > 0; i--)
715                          if (hp->bl[i] != NULL)
716                                  nchanged += hdfreebeam(hp, i);
717 +                DCHECK(blglob(hp)->nrm != 0,
718 +                                CONSISTENCY, "bad beam count in hdfreebeam");
719                  return(nchanged);
720          }
721          DCHECK(i < 1 | i > nbeams(hp),
# Line 702 | Line 747 | register int   i;
747                  return(nchanged);
748          }
749          if (i == 0) {                   /* clobber entire holodeck */
750 +                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
751 +                        return(0);              /* already empty */
752                  nchanged = 0;
753 +                nchanged = 0;
754                  for (i = nbeams(hp); i > 0; i--)
755                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
756                                  nchanged += hdkillbeam(hp, i);
# Line 715 | Line 763 | register int   i;
763          if (hp->bl[i] != NULL) {        /* free memory */
764                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
765                  free((char *)hp->bl[i]);
766 +                hp->bl[i] = NULL;
767          } else
768                  nchanged = hp->bi[i].nrd;
769 <        if (hp->bi[i].nrd) {
770 <                if (hdfragflags&FF_KILL)
771 <                        hdfreefrag(hp, i);
723 <                hp->bi[i].nrd = 0;      /* make sure it's gone */
769 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
770 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
771 >                hp->bi[i].nrd = 0;
772                  hp->bi[i].fo = 0L;
773 +                hdmarkdirty(hp, i);
774          }
726        hp->bl[i] = NULL;
775          return(nchanged);
776   }
777  
# Line 817 | Line 865 | register HOLO  *hp;            /* NULL means clean up all */
865                  return;
866          }
867                                          /* flush all data and free memory */
868 <        hdfreebeam(hp, 0);
821 <        hdsync(hp, 0);
868 >        hdflush(hp);
869                                          /* release fragment resources */
870          hdrelease(hp->fd);
871                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines