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.31 by gwlarson, Wed Dec 30 11:30:04 1998 UTC vs.
Revision 3.41 by gwlarson, Thu Aug 5 15:53:09 1999 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1998 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1999 Regents of the University of California */
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
# Line 24 | Line 28 | static char SCCSid[] = "$SunId$ SGI";
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 36 | Line 44 | static char SCCSid[] = "$SunId$ SGI";
44  
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  
53   static struct fraglist {
54          short   nlinks;         /* number of holodeck sections using us */
55 <        short   writerr;        /* write error encountered */
55 >        short   writable;       /* 0 read-only, <0 write error encountered */
56          int     nfrags;         /* number of known fragments */
57          BEAMI   *fi;            /* fragments, descending file position */
58          long    flen;           /* last known file length */
# Line 52 | 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 76 | Line 119 | char   *rout;
119   }
120  
121  
122 < hdattach(fd)            /* start tracking file fragments for some section */
122 > hdattach(fd, wr)        /* start tracking file fragments for some section */
123   register int    fd;
124 + int     wr;
125   {
126          if (fd >= nhdfragls) {
127                  hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
# Line 87 | Line 131 | register int   fd;
131                  nhdfragls = fd+1;
132          }
133          hdfragl[fd].nlinks++;
134 +        hdfragl[fd].writable = wr;              /* set writable flag */
135          hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
136   }
137  
# Line 114 | Line 159 | HDGRID *hproto;                /* holodeck section grid */
159   {
160          long    rtrunc;
161          long    fpos;
162 +        int     writable;
163          register HOLO   *hp;
164          register int    n;
165                                          /* prepare for system errors */
# Line 139 | Line 185 | HDGRID *hproto;                /* holodeck section grid */
185                                  error(WARNING, "dirty holodeck section");
186                                  break;
187                          }
188 <        } else {                        /* assume we're creating it */
188 >                                                /* check writability */
189 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
190 >                        writable = hdfragl[fd].writable;
191 >                else
192 >                        writable = lseek(fd, fpos, 0) == fpos &&
193 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
194 >                                                        sizeof(HDGRID);
195 >        } else {                        /* else assume we're creating it */
196                  if ((hp = hdalloc(hproto)) == NULL)
197                          goto memerr;
198                                                  /* write header and skeleton */
# Line 148 | Line 201 | HDGRID *hproto;                /* holodeck section grid */
201                                          sizeof(HDGRID) ||
202                                  write(fd, (char *)(hp->bi+1), n) != n)
203                          error(SYSTEM, "cannot write header to holodeck file");
204 +                writable = 1;
205          }
206          hp->fd = fd;    
207          hp->dirty = 0;
208          biglob(hp)->fo = fpos + sizeof(HDGRID);
209                                          /* start tracking fragments */
210 <        hdattach(fd);
210 >        hdattach(fd, writable);
211                                          /* check rays on disk */
212          fpos = hdfilen(fd);
213          biglob(hp)->nrd = rtrunc = 0;
# Line 182 | Line 236 | memerr:
236   }
237  
238  
239 < markdirty(hp, i)                /* mark holodeck directory position dirty */
239 > hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
240   register HOLO   *hp;
241   int     i;
242   {
# Line 194 | Line 248 | int    i;
248                  if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
249                                  || write(hp->fd, (char *)&smudge,
250                                          sizeof(BEAMI)) != sizeof(BEAMI))
251 <                        error(SYSTEM, "seek/write error in markdirty");
251 >                        error(SYSTEM, "seek/write error in hdmarkdirty");
252                  hp->dirseg[0].s = i;
253                  hp->dirseg[0].n = 1;
254                  return;
# Line 349 | Line 403 | int    nr;                     /* number of new rays desired */
403          RAYVAL  *p;
404          int     n;
405  
406 <        if (nr <= 0)
407 <                return(NULL);
408 <        if (i < 1 | i > nbeams(hp))
355 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
406 >        if (nr <= 0) return(NULL);
407 >        CHECK(i < 1 | i > nbeams(hp),
408 >                        CONSISTENCY, "bad beam index given to hdnewrays");
409          if (hp->bl[i] != NULL)
410                  hp->bl[i]->tick = hdclock;      /* preempt swap */
411          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
# Line 361 | Line 414 | int    nr;                     /* number of new rays desired */
414                  n = hp->bi[i].nrd + nr;
415                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
416                  blglob(hp)->nrm += n;
417 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
417 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
418                          errno = 0;
419                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
420                                  error(SYSTEM, "seek error on holodeck file");
# Line 375 | Line 428 | int    nr;                     /* number of new rays desired */
428                                  hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
429                  blglob(hp)->nrm += nr;
430          }
431 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
432 +                hdfreefrag(hp, i);              /* relinquish old fragment */
433          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
434          hp->bl[i]->nrm += nr;                   /* update in-core structure */
435          bzero((char *)p, nr*sizeof(RAYVAL));
# Line 390 | Line 445 | register int   i;
445   {
446          register int    n;
447  
448 <        if (i < 1 | i > nbeams(hp))
449 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
448 >        CHECK(i < 1 | i > nbeams(hp),
449 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
450          if (hp->bl[i] == NULL) {                /* load from disk */
451                  if (!(n = hp->bi[i].nrd))
452                          return(NULL);
# Line 405 | Line 460 | register int   i;
460                  n *= sizeof(RAYVAL);
461                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
462                          error(SYSTEM, "error reading beam from holodeck file");
463 +                if (hdfragflags&FF_READ)
464 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
465          }
466          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
467          return(hp->bl[i]);
# Line 442 | Line 499 | int    (*bf)();                /* callback function (optional) */
499                                          /* precheck consistency */
500          if (n <= 0) return;
501          for (i = n; i--; )
502 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
502 >                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
503                          error(CONSISTENCY, "bad beam in hdloadbeams");
504                                          /* sort list for optimal access */
505          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
# Line 475 | Line 532 | int    (*bf)();                /* callback function (optional) */
532   }
533  
534  
535 < hdfreefrag(fd, bi)                      /* free a file fragment */
536 < int     fd;
537 < register BEAMI  *bi;
535 > int
536 > hdfreefrag(hp, i)                       /* free a file fragment */
537 > HOLO    *hp;
538 > int     i;
539   {
540 +        register BEAMI  *bi = &hp->bi[i];
541          register struct fraglist        *f;
542          register int    j, k;
543  
544 <        if (bi->nrd == 0)
545 <                return;
546 < #ifdef DEBUG
547 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
548 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
549 < #endif
550 <        f = &hdfragl[fd];
544 >        if (bi->nrd <= 0)
545 >                return(0);
546 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
547 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
548 >        f = &hdfragl[hp->fd];
549 >        if (!f->writable)
550 >                return(0);
551          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
552                  for (j = k = 0; k < f->nfrags; j++, k++) {
553                          while (f->fi[k].nrd == 0)
# Line 505 | Line 564 | register BEAMI *bi;
564          if (j >= MAXFRAGB*FRAGBLK) {
565                  f->nfrags = j--;        /* stop list growth */
566                  if (bi->nrd <= f->fi[j].nrd)
567 <                        return;         /* new one no better than discard */
567 >                        return(0);      /* new one no better than discard */
568          }
569   #endif
570 <        if (j % FRAGBLK == 0) {         /* more free list space */
570 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
571                  register BEAMI  *newp;
572                  if (f->fi == NULL)
573                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
# Line 517 | Line 576 | register BEAMI *bi;
576                                          (j+FRAGBLK)*sizeof(BEAMI));
577                  if (newp == NULL) {
578                          f->nfrags--;    /* graceful failure */
579 <                        return;
579 >                        return(0);
580                  }
581                  f->fi = newp;
582          }
# Line 544 | Line 603 | register BEAMI *bi;
603                          }
604                          break;
605                  }
606 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
607 +        bi->nrd = 0;
608 +        bi->fo = 0L;
609 +        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
610 +        return(1);
611   }
612  
613  
614 + int
615 + hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
616 + int     fd;
617 + int     *listlen;
618 + register int4   *listsiz;
619 + {
620 +        register struct fraglist        *f;
621 +        register int    i;
622 +
623 +        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
624 +                return(0);              /* listless */
625 +        if (listlen != NULL)
626 +                *listlen = f->nfrags;
627 +        if (listsiz != NULL)
628 +                for (i = f->nfrags, *listsiz = 0; i--; )
629 +                        *listsiz += f->fi[i].nrd;
630 + #if MAXFRAGB
631 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
632 + #else
633 +        return(1);                      /* list never fills up */
634 + #endif
635 + }
636 +
637 +
638   long
639   hdallocfrag(fd, nrays)          /* allocate a file fragment */
640   int     fd;
641   unsigned int4   nrays;
642   {
643          register struct fraglist        *f;
644 <        register int    j, k;
644 >        register int    j;
645          long    nfo;
646  
647          if (nrays == 0)
648                  return(-1L);
649 < #ifdef DEBUG
650 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
563 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
564 < #endif
649 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
650 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
651          f = &hdfragl[fd];
652 <        k = -1;                         /* find closest-sized fragment */
653 <        for (j = f->nfrags; j-- > 0; )
654 <                if (f->fi[j].nrd >= nrays &&
655 <                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
570 <                        if (f->fi[k=j].nrd == nrays)
571 <                                break;
572 <        if (k < 0) {                    /* no fragment -- extend file */
652 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
653 >                if (f->fi[j].nrd >= nrays)
654 >                        break;
655 >        if (j < 0) {                    /* no fragment -- extend file */
656                  nfo = f->flen;
657                  f->flen += nrays*sizeof(RAYVAL);
658          } else {                        /* else use fragment */
659 <                nfo = f->fi[k].fo;
660 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
661 <                f->fi[k].nrd -= nrays;
659 >                nfo = f->fi[j].fo;
660 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
661 >                f->fi[j].nrd -= nrays;
662          }
663          return(nfo);
664   }
# Line 586 | Line 669 | hdsyncbeam(hp, i)              /* sync beam in memory with beam on
669   register HOLO   *hp;
670   register int    i;
671   {
672 +        int     fragfreed;
673          unsigned int4   nrays;
674          unsigned int    n;
675          long    nfo;
676                                          /* check file status */
677 <        if (hdfragl[hp->fd].writerr)
678 <                return(-1);
679 < #ifdef DEBUG
680 <        if (i < 1 | i > nbeams(hp))
597 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
598 < #endif
677 >        if (hdfragl[hp->fd].writable <= 0)
678 >                return(hdfragl[hp->fd].writable);
679 >        DCHECK(i < 1 | i > nbeams(hp),
680 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
681                                          /* is current fragment OK? */
682          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
683                  return(0);
684 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
685 <                hdfreefrag(hp->fd, &hp->bi[i]);
684 >                                        /* relinquish old fragment? */
685 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
686          if (nrays) {                    /* get and write new fragment */
687                  nfo = hdallocfrag(hp->fd, nrays);
688                  errno = 0;
# Line 608 | Line 690 | register int   i;
690                          error(SYSTEM, "cannot seek on holodeck file");
691                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
692                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
693 <                        hdfragl[hp->fd].writerr++;
693 >                        hdfragl[hp->fd].writable = -1;
694                          hdsync(NULL, 0);        /* sync directories */
695                          error(SYSTEM, "write error in hdsyncbeam");
696                  }
# Line 617 | Line 699 | register int   i;
699                  hp->bi[i].fo = 0L;
700          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
701          hp->bi[i].nrd = nrays;
702 <        markdirty(hp, i);               /* section directory now out of date */
702 >        if (!fragfreed)
703 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
704          return(1);
705   }
706  
# Line 635 | Line 718 | register int   i;
718                          nchanged += hdfreebeam(hdlist[i], 0);
719                  return(nchanged);
720          }
721 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
721 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
722                  return(0);
723          if (i == 0) {                   /* clear entire holodeck */
724 +                if (blglob(hp)->nrm == 0)
725 +                        return(0);              /* already clear */
726                  nchanged = 0;
727                  for (i = nbeams(hp); i > 0; i--)
728                          if (hp->bl[i] != NULL)
729                                  nchanged += hdfreebeam(hp, i);
730 +                DCHECK(blglob(hp)->nrm != 0,
731 +                                CONSISTENCY, "bad beam count in hdfreebeam");
732                  return(nchanged);
733          }
734 < #ifdef DEBUG
735 <        if (i < 1 | i > nbeams(hp))
649 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
650 < #endif
734 >        DCHECK(i < 1 | i > nbeams(hp),
735 >                        CONSISTENCY, "bad beam index to hdfreebeam");
736          if (hp->bl[i] == NULL)
737                  return(0);
738                                          /* check for additions */
# Line 666 | Line 751 | hdkillbeam(hp, i)              /* delete beam from holodeck */
751   register HOLO   *hp;
752   register int    i;
753   {
669        static BEAM     emptybeam;
754          int     nchanged;
755  
756          if (hp == NULL) {               /* clobber all holodecks */
# Line 676 | Line 760 | register int   i;
760                  return(nchanged);
761          }
762          if (i == 0) {                   /* clobber entire holodeck */
763 +                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
764 +                        return(0);              /* already empty */
765                  nchanged = 0;
766 +                nchanged = 0;
767                  for (i = nbeams(hp); i > 0; i--)
768                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
769                                  nchanged += hdkillbeam(hp, i);
770 < #ifdef DEBUG
771 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
685 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
686 < #endif
770 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
771 >                                CONSISTENCY, "bad beam count in hdkillbeam");
772                  return(nchanged);
773          }
774 < #ifdef DEBUG
775 <        if (i < 1 | i > nbeams(hp))
776 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
777 < #endif
774 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
775 >                        "bad beam index to hdkillbeam");
776 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
777 >                        "hdkillbeam called on read-only holodeck");
778          if (hp->bl[i] != NULL) {        /* free memory */
779                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
780                  free((char *)hp->bl[i]);
781 +                hp->bl[i] = NULL;
782          } else
783                  nchanged = hp->bi[i].nrd;
784 <        if (hp->bi[i].nrd) {            /* free file fragment */
785 <                hp->bl[i] = &emptybeam;
786 <                hdsyncbeam(hp, i);
784 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
785 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
786 >                hp->bi[i].nrd = 0;
787 >                hp->bi[i].fo = 0L;
788 >                hdmarkdirty(hp, i);
789          }
702        hp->bl[i] = NULL;
790          return(nchanged);
791   }
792  
# Line 793 | Line 880 | register HOLO  *hp;            /* NULL means clean up all */
880                  return;
881          }
882                                          /* flush all data and free memory */
883 <        hdfreebeam(hp, 0);
797 <        hdsync(hp, 0);
883 >        hdflush(hp);
884                                          /* release fragment resources */
885          hdrelease(hp->fd);
886                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines