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.32 by gwlarson, Thu Jan 7 22:04:49 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 24 | Line 24 | static char SCCSid[] = "$SunId$ SGI";
24   #ifndef MAXFRAGB
25   #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
26   #endif
27 + #ifndef FF_DEFAULT
28 +                                /* when to free a beam fragment */
29 + #define FF_DEFAULT      (FF_ALLOC|FF_WRITE|FF_KILL)
30 + #endif
31   #ifndef MINDIRSEL
32                                  /* minimum directory seek length */
33   #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
# Line 36 | Line 40 | static char SCCSid[] = "$SunId$ SGI";
40  
41   #define FRAGBLK         512     /* number of fragments to allocate at a time */
42  
43 + int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */
44   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
45 < unsigned long   hdclock;        /* clock value */
45 > unsigned long   hdclock;                /* clock value */
46  
47   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
48  
# Line 349 | Line 354 | int    nr;                     /* number of new rays desired */
354          RAYVAL  *p;
355          int     n;
356  
357 <        if (nr <= 0)
358 <                return(NULL);
359 <        if (i < 1 | i > nbeams(hp))
355 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
357 >        if (nr <= 0) return(NULL);
358 >        CHECK(i < 1 | i > nbeams(hp),
359 >                        CONSISTENCY, "bad beam index given to hdnewrays");
360          if (hp->bl[i] != NULL)
361                  hp->bl[i]->tick = hdclock;      /* preempt swap */
362          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
# Line 361 | Line 365 | int    nr;                     /* number of new rays desired */
365                  n = hp->bi[i].nrd + nr;
366                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
367                  blglob(hp)->nrm += n;
368 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
368 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
369                          errno = 0;
370                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
371                                  error(SYSTEM, "seek error on holodeck file");
# Line 375 | Line 379 | int    nr;                     /* number of new rays desired */
379                                  hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
380                  blglob(hp)->nrm += nr;
381          }
382 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
383 +                hdfreefrag(hp, i);              /* relinquish old fragment */
384          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
385          hp->bl[i]->nrm += nr;                   /* update in-core structure */
386          bzero((char *)p, nr*sizeof(RAYVAL));
# Line 390 | Line 396 | register int   i;
396   {
397          register int    n;
398  
399 <        if (i < 1 | i > nbeams(hp))
400 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
399 >        CHECK(i < 1 | i > nbeams(hp),
400 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
401          if (hp->bl[i] == NULL) {                /* load from disk */
402                  if (!(n = hp->bi[i].nrd))
403                          return(NULL);
# Line 405 | Line 411 | register int   i;
411                  n *= sizeof(RAYVAL);
412                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
413                          error(SYSTEM, "error reading beam from holodeck file");
414 +                if (hdfragflags&FF_READ)
415 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
416          }
417          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
418          return(hp->bl[i]);
# Line 442 | Line 450 | int    (*bf)();                /* callback function (optional) */
450                                          /* precheck consistency */
451          if (n <= 0) return;
452          for (i = n; i--; )
453 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
453 >                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
454                          error(CONSISTENCY, "bad beam in hdloadbeams");
455                                          /* sort list for optimal access */
456          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
# Line 475 | Line 483 | int    (*bf)();                /* callback function (optional) */
483   }
484  
485  
486 < hdfreefrag(fd, bi)                      /* free a file fragment */
487 < int     fd;
488 < register BEAMI  *bi;
486 > hdfreefrag(hp, i)                       /* free a file fragment */
487 > HOLO    *hp;
488 > int     i;
489   {
490 +        register BEAMI  *bi = &hp->bi[i];
491          register struct fraglist        *f;
492          register int    j, k;
493  
494 <        if (bi->nrd == 0)
494 >        if (bi->nrd <= 0)
495                  return;
496 < #ifdef DEBUG
497 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
498 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
490 < #endif
491 <        f = &hdfragl[fd];
496 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
497 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
498 >        f = &hdfragl[hp->fd];
499          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
500                  for (j = k = 0; k < f->nfrags; j++, k++) {
501                          while (f->fi[k].nrd == 0)
# Line 508 | Line 515 | register BEAMI *bi;
515                          return;         /* new one no better than discard */
516          }
517   #endif
518 <        if (j % FRAGBLK == 0) {         /* more free list space */
518 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
519                  register BEAMI  *newp;
520                  if (f->fi == NULL)
521                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
# Line 544 | Line 551 | register BEAMI *bi;
551                          }
552                          break;
553                  }
554 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
555 +        bi->nrd = 0;
556 +        bi->fo = 0;
557   }
558  
559  
560 + int
561 + hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
562 + int     fd;
563 + int     *listlen;
564 + register int4   *listsiz;
565 + {
566 +        register struct fraglist        *f;
567 +        register int    i;
568 +
569 +        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
570 +                return(0);              /* listless */
571 +        if (listlen != NULL)
572 +                *listlen = f->nfrags;
573 +        if (listsiz != NULL)
574 +                for (i = f->nfrags, *listsiz = 0; i--; )
575 +                        *listsiz += f->fi[i].nrd;
576 + #if MAXFRAGB
577 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
578 + #else
579 +        return(1);                      /* list never fills up */
580 + #endif
581 + }
582 +
583 +
584   long
585   hdallocfrag(fd, nrays)          /* allocate a file fragment */
586   int     fd;
# Line 558 | Line 592 | unsigned int4  nrays;
592  
593          if (nrays == 0)
594                  return(-1L);
595 < #ifdef DEBUG
596 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
563 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
564 < #endif
595 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
596 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
597          f = &hdfragl[fd];
598          k = -1;                         /* find closest-sized fragment */
599          for (j = f->nfrags; j-- > 0; )
# Line 592 | Line 624 | register int   i;
624                                          /* check file status */
625          if (hdfragl[hp->fd].writerr)
626                  return(-1);
627 < #ifdef DEBUG
628 <        if (i < 1 | i > nbeams(hp))
597 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
598 < #endif
627 >        DCHECK(i < 1 | i > nbeams(hp),
628 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
629                                          /* is current fragment OK? */
630          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
631                  return(0);
632 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
633 <                hdfreefrag(hp->fd, &hp->bi[i]);
632 >        if (hdfragflags&FF_WRITE && hp->bi[i].nrd)
633 >                hdfreefrag(hp, i);      /* relinquish old fragment */
634          if (nrays) {                    /* get and write new fragment */
635                  nfo = hdallocfrag(hp->fd, nrays);
636                  errno = 0;
# Line 615 | Line 645 | register int   i;
645                  hp->bi[i].fo = nfo;
646          } else
647                  hp->bi[i].fo = 0L;
648 <        biglob(hp)->nrd += nrays - hp->bi[i].nrd;
619 <        hp->bi[i].nrd = nrays;
648 >        biglob(hp)->nrd += hp->bi[i].nrd = nrays;
649          markdirty(hp, i);               /* section directory now out of date */
650          return(1);
651   }
# Line 644 | Line 673 | register int   i;
673                                  nchanged += hdfreebeam(hp, i);
674                  return(nchanged);
675          }
676 < #ifdef DEBUG
677 <        if (i < 1 | i > nbeams(hp))
649 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
650 < #endif
676 >        DCHECK(i < 1 | i > nbeams(hp),
677 >                        CONSISTENCY, "bad beam index to hdfreebeam");
678          if (hp->bl[i] == NULL)
679                  return(0);
680                                          /* check for additions */
# Line 666 | Line 693 | hdkillbeam(hp, i)              /* delete beam from holodeck */
693   register HOLO   *hp;
694   register int    i;
695   {
669        static BEAM     emptybeam;
696          int     nchanged;
697  
698          if (hp == NULL) {               /* clobber all holodecks */
# Line 680 | Line 706 | register int   i;
706                  for (i = nbeams(hp); i > 0; i--)
707                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
708                                  nchanged += hdkillbeam(hp, i);
709 < #ifdef DEBUG
710 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
685 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
686 < #endif
709 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
710 >                                CONSISTENCY, "bad beam count in hdkillbeam");
711                  return(nchanged);
712          }
713 < #ifdef DEBUG
714 <        if (i < 1 | i > nbeams(hp))
691 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
692 < #endif
713 >        DCHECK(i < 1 | i > nbeams(hp),
714 >                        CONSISTENCY, "bad beam index to hdkillbeam");
715          if (hp->bl[i] != NULL) {        /* free memory */
716                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
717                  free((char *)hp->bl[i]);
718          } else
719                  nchanged = hp->bi[i].nrd;
720 <        if (hp->bi[i].nrd) {            /* free file fragment */
721 <                hp->bl[i] = &emptybeam;
722 <                hdsyncbeam(hp, i);
720 >        if (hp->bi[i].nrd) {
721 >                if (hdfragflags&FF_KILL)
722 >                        hdfreefrag(hp, i);
723 >                hp->bi[i].nrd = 0;      /* make sure it's gone */
724 >                hp->bi[i].fo = 0L;
725          }
726          hp->bl[i] = NULL;
727          return(nchanged);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines