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.39 by gwlarson, Tue Mar 9 08:40:11 1999 UTC vs.
Revision 3.52 by schorsch, Sun Jul 27 22:12:02 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for managing holodeck files
6   *
7   *      9/30/97 GWLarson
8   */
9  
10 + #include "copyright.h"
11 +
12 + #include <string.h>
13 +
14   #include "holo.h"
15  
16   #ifndef CACHESIZE
17 < #ifdef BIGMEM
17 < #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
18 < #else
17 > #ifdef SMLMEM
18   #define CACHESIZE       5
19 + #else
20 + #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
21   #endif
22   #endif
23   #ifndef FREEBEAMS
# Line 52 | Line 53 | HOLO   *hdlist[HDMAX+1];       /* holodeck pointers (NULL term
53  
54   static struct fraglist {
55          short   nlinks;         /* number of holodeck sections using us */
56 <        short   writerr;        /* write error encountered */
56 >        short   writable;       /* 0 read-only, <0 write error encountered */
57          int     nfrags;         /* number of known fragments */
58          BEAMI   *fi;            /* fragments, descending file position */
59 <        long    flen;           /* last known file length */
59 >        off_t   flen;           /* last known file length */
60   } *hdfragl;             /* fragment lists, indexed by file descriptor */
61  
62   static int      nhdfragls;      /* size of hdfragl array */
63  
64  
65 + HOLO *
66 + hdalloc(hproto)         /* allocate and set holodeck section based on grid */
67 + HDGRID  *hproto;
68 + {
69 +        HOLO    hdhead;
70 +        register HOLO   *hp;
71 +        int     n;
72 +                                /* copy grid to temporary header */
73 +        memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID));
74 +                                /* compute grid vectors and sizes */
75 +        hdcompgrid(&hdhead);
76 +                                /* allocate header with directory */
77 +        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
78 +        if ((hp = (HOLO *)malloc(n)) == NULL)
79 +                return(NULL);
80 +                                /* copy header information */
81 +        *hp = hdhead;
82 +                                /* allocate and clear beam list */
83 +        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
84 +        if (hp->bl == NULL) {
85 +                free((void *)hp);
86 +                return(NULL);
87 +        }
88 +        memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
89 +        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
90 +        hp->fd = -1;
91 +        hp->dirty = 0;
92 +        hp->priv = NULL;
93 +                                /* clear beam directory */
94 +        memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI));
95 +        return(hp);             /* all is well */
96 + }
97 +
98 +
99   char *
100   hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
101   char    *ptr;
# Line 70 | Line 105 | char   *rout;
105          register char   *newp;
106                                          /* call malloc/realloc */
107          if (ptr == NULL) newp = (char *)malloc(siz);
108 <        else newp = (char *)realloc(ptr, siz);
108 >        else newp = (char *)realloc((void *)ptr, siz);
109                                          /* check success */
110          if (newp == NULL && rout != NULL) {
111                  hdfreecache(25, NULL);  /* free some memory */
# Line 85 | Line 120 | char   *rout;
120   }
121  
122  
123 < hdattach(fd)            /* start tracking file fragments for some section */
123 > hdattach(fd, wr)        /* start tracking file fragments for some section */
124   register int    fd;
125 + int     wr;
126   {
127          if (fd >= nhdfragls) {
128                  hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
129                                  (fd+1)*sizeof(struct fraglist), "hdattach");
130 <                bzero((char *)(hdfragl+nhdfragls),
131 <                                (fd+1-nhdfragls)*sizeof(struct fraglist));
130 >                memset((void *)(hdfragl+nhdfragls),
131 >                                '\0', (fd+1-nhdfragls)*sizeof(struct fraglist));
132                  nhdfragls = fd+1;
133          }
134          hdfragl[fd].nlinks++;
135 <        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
135 >        hdfragl[fd].writable = wr;              /* set writable flag */
136 >        hdfragl[fd].flen = lseek(fd, (off_t)0, 2);      /* get file length */
137   }
138  
139  
# Line 106 | Line 143 | register int   fd;
143   hdrelease(fd)           /* stop tracking file fragments for some section */
144   register int    fd;
145   {
146 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
146 >        if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks)
147                  return;
148          if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
149 <                free((char *)hdfragl[fd].fi);
149 >                free((void *)hdfragl[fd].fi);
150                  hdfragl[fd].fi = NULL;
151                  hdfragl[fd].nfrags = 0;
152          }
# Line 121 | Line 158 | hdinit(fd, hproto)     /* initialize a holodeck section in
158   int     fd;                     /* corresponding file descriptor */
159   HDGRID  *hproto;                /* holodeck section grid */
160   {
161 <        long    rtrunc;
162 <        long    fpos;
161 >        off_t   rtrunc;
162 >        off_t   fpos;
163 >        int     writable;
164          register HOLO   *hp;
165          register int    n;
166                                          /* prepare for system errors */
167          errno = 0;
168 <        if ((fpos = lseek(fd, 0L, 1)) < 0)
168 >        if ((fpos = lseek(fd, (off_t)0, 1)) < 0)
169                  error(SYSTEM, "cannot determine holodeck file position");
170          if (hproto == NULL) {           /* assume we're loading it */
171                  HDGRID  hpr;
# Line 148 | Line 186 | HDGRID *hproto;                /* holodeck section grid */
186                                  error(WARNING, "dirty holodeck section");
187                                  break;
188                          }
189 <        } else {                        /* assume we're creating it */
189 >                                                /* check writability */
190 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
191 >                        writable = hdfragl[fd].writable;
192 >                else
193 >                        writable = lseek(fd, fpos, 0) == fpos &&
194 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
195 >                                                        sizeof(HDGRID);
196 >        } else {                        /* else assume we're creating it */
197                  if ((hp = hdalloc(hproto)) == NULL)
198                          goto memerr;
199                                                  /* write header and skeleton */
# Line 157 | Line 202 | HDGRID *hproto;                /* holodeck section grid */
202                                          sizeof(HDGRID) ||
203                                  write(fd, (char *)(hp->bi+1), n) != n)
204                          error(SYSTEM, "cannot write header to holodeck file");
205 +                writable = 1;
206          }
207          hp->fd = fd;    
208          hp->dirty = 0;
209          biglob(hp)->fo = fpos + sizeof(HDGRID);
210                                          /* start tracking fragments */
211 <        hdattach(fd);
211 >        hdattach(fd, writable);
212                                          /* check rays on disk */
213          fpos = hdfilen(fd);
214          biglob(hp)->nrd = rtrunc = 0;
215          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
216 <                if (hp->bi[n].nrd)
216 >                if (hp->bi[n].nrd) {
217                          if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
218                                  rtrunc += hp->bi[n].nrd;
219                                  hp->bi[n].nrd = 0;
220                          } else
221                                  biglob(hp)->nrd += hp->bi[n].nrd;
222 +                }
223          if (rtrunc) {
224                  sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
225                                  rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
# Line 215 | Line 262 | int    i;
262                          hp->dirseg[j].n = 1;
263                          break;
264                  }
265 <                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
265 >                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
266          }
267          do {                            /* check neighbors */
268                  mindist = nbeams(hp);           /* find closest */
# Line 237 | Line 284 | int    i;
284                                          hp->dirseg[minpos].n - hp->dirseg[j].s;
285                  hp->dirty--;
286                  while (++j < hp->dirty)         /* close the gap */
287 <                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
287 >                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
288          } while (mindist <= MINDIRSEL);
289   }
290  
# Line 304 | Line 351 | int    all;                    /* include overhead (painful) */
351   }
352  
353  
354 < long
354 > off_t
355   hdfilen(fd)             /* return file length for fd */
356   int     fd;
357   {
358 <        long    fpos, flen;
358 >        off_t   fpos, flen;
359  
360          if (fd < 0)
361                  return(-1);
362          if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
363 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
363 >                if ((fpos = lseek(fd, (off_t)0, 1)) < 0)
364                          return(-1);
365 <                flen = lseek(fd, 0L, 2);
365 >                flen = lseek(fd, (off_t)0, 2);
366                  lseek(fd, fpos, 0);
367                  return(flen);
368          }
# Line 323 | Line 370 | int    fd;
370   }
371  
372  
373 < long
373 > off_t
374   hdfiluse(fd, all)       /* compute file usage (in bytes) */
375   int     fd;                     /* open file descriptor to check */
376   int     all;                    /* include overhead and unflushed data */
377   {
378 <        long    total = 0;
378 >        off_t   total = 0;
379          register int    i, j;
380  
381          for (j = 0; hdlist[j] != NULL; j++) {
# Line 359 | Line 406 | int    nr;                     /* number of new rays desired */
406          int     n;
407  
408          if (nr <= 0) return(NULL);
409 <        CHECK(i < 1 | i > nbeams(hp),
409 >        CHECK((i < 1) | (i > nbeams(hp)),
410                          CONSISTENCY, "bad beam index given to hdnewrays");
411          if (hp->bl[i] != NULL)
412                  hp->bl[i]->tick = hdclock;      /* preempt swap */
# Line 387 | Line 434 | int    nr;                     /* number of new rays desired */
434                  hdfreefrag(hp, i);              /* relinquish old fragment */
435          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
436          hp->bl[i]->nrm += nr;                   /* update in-core structure */
437 <        bzero((char *)p, nr*sizeof(RAYVAL));
437 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
438          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
439          return(p);                              /* point to new rays */
440   }
# Line 400 | Line 447 | register int   i;
447   {
448          register int    n;
449  
450 <        CHECK(i < 1 | i > nbeams(hp),
450 >        CHECK((i < 1) | (i > nbeams(hp)),
451                          CONSISTENCY, "bad beam index given to hdgetbeam");
452          if (hp->bl[i] == NULL) {                /* load from disk */
453                  if (!(n = hp->bi[i].nrd))
# Line 427 | Line 474 | int
474   hdfilord(hb1, hb2)      /* order beams for quick loading */
475   register HDBEAMI        *hb1, *hb2;
476   {
477 <        register long   c;
477 >        register off_t  c;
478                                  /* residents go first */
479          if (hb2->h->bl[hb2->b] != NULL)
480                  return(hb1->h->bl[hb1->b] == NULL);
481          if (hb1->h->bl[hb1->b] != NULL)
482                  return(-1);
483                                  /* otherwise sort by file descriptor */
484 <        if ((c = hb1->h->fd - hb2->h->fd))
485 <                return(c);
484 >        if (hb1->h->fd != hb2->h->fd)
485 >                return(hb1->h->fd - hb2->h->fd);
486                                  /* then by position in file */
487          c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
488          return(c > 0 ? 1 : c < 0 ? -1 : 0);
# Line 445 | Line 492 | register HDBEAMI       *hb1, *hb2;
492   hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
493   register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
494   int     n;                      /* list length */
495 < int     (*bf)();                /* callback function (optional) */
495 > void    (*bf)();                /* callback function (optional) */
496   {
497          unsigned        origcachesize, memuse;
498          int     bytesloaded, needbytes, bytes2free;
# Line 454 | Line 501 | int    (*bf)();                /* callback function (optional) */
501                                          /* precheck consistency */
502          if (n <= 0) return;
503          for (i = n; i--; )
504 <                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
504 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
505                          error(CONSISTENCY, "bad beam in hdloadbeams");
506                                          /* sort list for optimal access */
507 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
507 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
508          bytesloaded = 0;                /* run through loaded beams */
509          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
510                  bp->tick = hdclock;     /* preempt swap */
# Line 501 | Line 548 | int    i;
548          DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
549                          CONSISTENCY, "bad file descriptor in hdfreefrag");
550          f = &hdfragl[hp->fd];
551 +        if (!f->writable)
552 +                return(0);
553          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
554                  for (j = k = 0; k < f->nfrags; j++, k++) {
555                          while (f->fi[k].nrd == 0)
556                                  if (++k >= f->nfrags)
557                                          goto endloop;
558                          if (k > j)
559 <                                copystruct(f->fi+j, f->fi+k);
559 >                                *(f->fi+j) = *(f->fi+k);
560                  }
561          endloop:
562                  f->nfrags = j;
# Line 525 | Line 574 | int    i;
574                  if (f->fi == NULL)
575                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
576                  else
577 <                        newp = (BEAMI *)realloc((char *)f->fi,
577 >                        newp = (BEAMI *)realloc((void *)f->fi,
578                                          (j+FRAGBLK)*sizeof(BEAMI));
579                  if (newp == NULL) {
580                          f->nfrags--;    /* graceful failure */
# Line 539 | Line 588 | int    i;
588                          f->fi[j].nrd = bi->nrd;
589                          break;
590                  }
591 <                copystruct(f->fi+j, f->fi+(j-1));
591 >                *(f->fi+j) = *(f->fi+(j-1));
592          }
593                                          /* coalesce adjacent fragments */
594                                                  /* successors never empty */
# Line 558 | Line 607 | int    i;
607                  }
608          biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
609          bi->nrd = 0;
610 <        bi->fo = 0L;
610 >        bi->fo = 0;
611          hdmarkdirty(hp, i);                     /* assume we'll reallocate */
612          return(1);
613   }
# Line 568 | Line 617 | int
617   hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
618   int     fd;
619   int     *listlen;
620 < register int4   *listsiz;
620 > register int32  *listsiz;
621   {
622          register struct fraglist        *f;
623          register int    i;
624  
625 <        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
625 >        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
626                  return(0);              /* listless */
627          if (listlen != NULL)
628                  *listlen = f->nfrags;
# Line 588 | Line 637 | register int4  *listsiz;
637   }
638  
639  
640 < long
640 > off_t
641   hdallocfrag(fd, nrays)          /* allocate a file fragment */
642   int     fd;
643 < unsigned int4   nrays;
643 > uint32  nrays;
644   {
645          register struct fraglist        *f;
646          register int    j;
647 <        long    nfo;
647 >        off_t   nfo;
648  
649          if (nrays == 0)
650                  return(-1L);
# Line 623 | Line 672 | register HOLO  *hp;
672   register int    i;
673   {
674          int     fragfreed;
675 <        unsigned int4   nrays;
675 >        uint32  nrays;
676          unsigned int    n;
677 <        long    nfo;
677 >        off_t   nfo;
678                                          /* check file status */
679 <        if (hdfragl[hp->fd].writerr)
680 <                return(-1);
679 >        if (hdfragl[hp->fd].writable <= 0)
680 >                return(hdfragl[hp->fd].writable);
681          DCHECK(i < 1 | i > nbeams(hp),
682                          CONSISTENCY, "bad beam index in hdsyncbeam");
683                                          /* is current fragment OK? */
# Line 643 | Line 692 | register int   i;
692                          error(SYSTEM, "cannot seek on holodeck file");
693                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
694                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
695 <                        hdfragl[hp->fd].writerr++;
695 >                        hdfragl[hp->fd].writable = -1;
696                          hdsync(NULL, 0);        /* sync directories */
697                          error(SYSTEM, "write error in hdsyncbeam");
698                  }
699                  hp->bi[i].fo = nfo;
700          } else
701 <                hp->bi[i].fo = 0L;
701 >                hp->bi[i].fo = 0;
702          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
703          hp->bi[i].nrd = nrays;
704          if (!fragfreed)
# Line 671 | Line 720 | register int   i;
720                          nchanged += hdfreebeam(hdlist[i], 0);
721                  return(nchanged);
722          }
723 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
723 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
724                  return(0);
725          if (i == 0) {                   /* clear entire holodeck */
726                  if (blglob(hp)->nrm == 0)
# Line 693 | Line 742 | register int   i;
742          if (nchanged)
743                  hdsyncbeam(hp, i);              /* write new fragment */
744          blglob(hp)->nrm -= hp->bl[i]->nrm;
745 <        free((char *)hp->bl[i]);                /* free memory */
745 >        free((void *)hp->bl[i]);                /* free memory */
746          hp->bl[i] = NULL;
747          return(nchanged);
748   }
# Line 713 | Line 762 | register int   i;
762                  return(nchanged);
763          }
764          if (i == 0) {                   /* clobber entire holodeck */
765 <                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
765 >                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
766                          return(0);              /* already empty */
767                  nchanged = 0;
768                  nchanged = 0;
# Line 724 | Line 773 | register int   i;
773                                  CONSISTENCY, "bad beam count in hdkillbeam");
774                  return(nchanged);
775          }
776 <        DCHECK(i < 1 | i > nbeams(hp),
777 <                        CONSISTENCY, "bad beam index to hdkillbeam");
776 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
777 >                        "bad beam index to hdkillbeam");
778 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
779 >                        "hdkillbeam called on read-only holodeck");
780          if (hp->bl[i] != NULL) {        /* free memory */
781                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
782 <                free((char *)hp->bl[i]);
782 >                free((void *)hp->bl[i]);
783                  hp->bl[i] = NULL;
784          } else
785                  nchanged = hp->bi[i].nrd;
786          if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
787                  biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
788                  hp->bi[i].nrd = 0;
789 <                hp->bi[i].fo = 0L;
789 >                hp->bi[i].fo = 0;
790                  hdmarkdirty(hp, i);
791          }
792          return(nchanged);
# Line 767 | Line 818 | register HOLO  *hp;                    /* section we're adding from */
818                                  hb[j].b = i;
819                                  break;
820                          }
821 <                        copystruct(hb+j, hb+(j-1));
821 >                        *(hb+j) = *(hb+(j-1));
822                  }
823          }
824          return(nents);                  /* return new list length */
# Line 826 | Line 877 | register HOLO  *hp;            /* NULL means clean up all */
877          if (hp == NULL) {               /* NULL means clean up everything */
878                  while (hdlist[0] != NULL)
879                          hddone(hdlist[0]);
880 <                free((char *)hdfragl);
880 >                free((void *)hdfragl);
881                  hdfragl = NULL; nhdfragls = 0;
882                  return;
883          }
# Line 841 | Line 892 | register HOLO  *hp;            /* NULL means clean up all */
892                                  i++;
893                          break;
894                  }
895 <        free((char *)hp->bl);           /* free beam list */
896 <        free((char *)hp);               /* free holodeck struct */
895 >        free((void *)hp->bl);           /* free beam list */
896 >        free((void *)hp);               /* free holodeck struct */
897   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines