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.8 by gregl, Tue Nov 11 11:31:54 1997 UTC vs.
Revision 3.11 by gregl, Thu Dec 11 19:57:58 1997 UTC

# Line 335 | Line 335 | register int   i;
335  
336  
337   int
338 < hdgetbi(hp, i)                  /* allocate a file fragment */
338 > hdsyncbi(hp, i)                 /* sync beam in memory with beam on disk */
339   register HOLO   *hp;
340   register int    i;
341   {
342 <        int     nrays = hp->bl[i]->nrm;
343 <
344 <        if (hp->bi[i].nrd == nrays)     /* current one will do? */
342 >        unsigned int    nrays = hp->bl[i]->nrm;
343 >        long    nfo;
344 >        unsigned int    n;
345 >                                        /* is current fragment OK? */
346 >        if (nrays == hp->bi[i].nrd)
347                  return(0);
348 +                                        /* check file status */
349 +        if (hp->dirty < 0)
350 +                return(-1);
351  
352          if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
353                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
354  
355          else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
356                          hdfrag[hp->fd].flen)            /* EOF special case */
357 <                hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
357 >                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
358  
359          else {                                          /* general case */
360                  register struct fragment        *f = &hdfrag[hp->fd];
# Line 370 | Line 375 | register int   i;
375                                                  (j+FRAGBLK)*sizeof(BEAMI));
376                                  if (f->fi == NULL)
377                                          error(SYSTEM,
378 <                                                "out of memory in hdgetbi");
378 >                                                "out of memory in hdsyncbi");
379                          }
380                          for ( ; ; j--) {        /* insert in descending list */
381                                  if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
# Line 395 | Line 400 | register int   i;
400                          f->nfrags = j;
401                  }
402                  k = -1;                 /* find closest-sized fragment */
403 <                for (j = f->nfrags; j-- > 0; )
403 >                for (j = nrays ? f->nfrags : 0; j-- > 0; )
404                          if (f->fi[j].nrd >= nrays &&
405                                          (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
406                                  if (f->fi[k=j].nrd == nrays)
407                                          break;
408                  if (k < 0) {            /* no fragment -- extend file */
409 <                        hp->bi[i].fo = f->flen;
409 >                        nfo = f->flen;
410                          f->flen += nrays*sizeof(RAYVAL);
411                  } else {                /* else use fragment */
412 <                        hp->bi[i].fo = f->fi[k].fo;
412 >                        nfo = f->fi[k].fo;
413                          if (f->fi[k].nrd == nrays) {    /* delete fragment */
414                                  f->nfrags--;
415                                  for (j = k; j < f->nfrags; j++)
# Line 415 | Line 420 | register int   i;
420                          }
421                  }
422          }
423 +        if (nrays) {            /* write the new fragment */
424 +                errno = 0;
425 +                if (lseek(hp->fd, nfo, 0) < 0)
426 +                        error(SYSTEM, "cannot seek on holodeck file");
427 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
428 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
429 +                        hp->dirty = -1;         /* avoid recursive error */
430 +                        error(SYSTEM, "write error in hdsyncbi");
431 +                }
432 +        }
433          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
434          hp->bi[i].nrd = nrays;
435 +        hp->bi[i].fo = nfo;
436          markdirty(hp);          /* section directory now out of date */
437          return(1);
438   }
# Line 427 | Line 443 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
443   register HOLO   *hp;
444   register int    i;
445   {
446 <        int     nchanged, n;
446 >        int     nchanged;
447  
448          if (hp == NULL) {               /* clear all holodecks */
449                  nchanged = 0;
# Line 437 | Line 453 | register int   i;
453          }
454          if (i == 0) {                   /* clear entire holodeck */
455                  nchanged = 0;
456 <                for (i = 1; i <= nbeams(hp); i++)
457 <                        nchanged += hdfreebeam(hp, i);
456 >                for (i = nbeams(hp); i > 0; i--)
457 >                        if (hp->bl[i] != NULL)
458 >                                nchanged += hdfreebeam(hp, i);
459                  return(nchanged);
460          }
461          if (i < 1 | i > nbeams(hp))
# Line 447 | Line 464 | register int   i;
464                  return(0);
465                                          /* check for additions */
466          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
467 <        if (nchanged) {
468 <                hdgetbi(hp, i);                 /* allocate a file position */
452 <                errno = 0;
453 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
454 <                        error(SYSTEM, "cannot seek on holodeck file");
455 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
456 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
457 <                        error(SYSTEM, "write error in hdfreebeam");
458 <        }
467 >        if (nchanged)
468 >                hdsyncbi(hp, i);                        /* write new fragment */
469          blglob(hp)->nrm -= hp->bl[i]->nrm;
470          free((char *)hp->bl[i]);                /* free memory */
471          hp->bl[i] = NULL;
# Line 463 | Line 473 | register int   i;
473   }
474  
475  
476 + int
477 + hdkillbeam(hp, i)               /* delete beam from holodeck */
478 + register HOLO   *hp;
479 + register int    i;
480 + {
481 +        static BEAM     emptybeam;
482 +        int     nchanged;
483 +
484 +        if (hp == NULL) {               /* clobber all holodecks */
485 +                nchanged = 0;
486 +                for (i = 0; hdlist[i] != NULL; i++)
487 +                        nchanged += hdkillbeam(hdlist[i], 0);
488 +                return(nchanged);
489 +        }
490 +        if (i == 0) {                   /* clobber entire holodeck */
491 +                nchanged = 0;
492 +                for (i = nbeams(hp); i > 0; i--)
493 +                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
494 +                                nchanged += hdkillbeam(hp, i);
495 + #ifdef DEBUG
496 +                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
497 +                        error(CONSISTENCY, "bad beam count in hdkillbeam");
498 + #endif
499 +                return(nchanged);
500 +        }
501 +        if (i < 1 | i > nbeams(hp))
502 +                error(CONSISTENCY, "bad beam index to hdkillbeam");
503 +        if (hp->bl[i] != NULL) {        /* free memory */
504 +                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
505 +                free((char *)hp->bl[i]);
506 +        } else
507 +                nchanged = hp->bi[i].nrd;
508 +        if (hp->bi[i].nrd) {            /* free file fragment */
509 +                hp->bl[i] = &emptybeam;
510 +                hdsyncbi(hp, i);
511 +        }
512 +        hp->bl[i] = NULL;
513 +        return(nchanged);
514 + }
515 +
516 +
517   hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
518   register HOLO   *ha[];                  /* section list (NULL terminated) */
519   register int    ba[];                   /* beam index to go with section */
# Line 476 | Line 527 | register HOLO  *hp;                    /* section we're adding from */
527                  ;
528          nents = j;
529                                          /* insert each beam from hp */
530 <        for (i = nbeams(hp); i > 0; i-- ) {
530 >        for (i = nbeams(hp); i > 0; i--) {
531                  if (hp->bl[i] == NULL)          /* check if loaded */
532                          continue;
533                  if ((j = ++nents) > n)          /* grow list if we can */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines