--- ray/src/hd/holofile.c 1997/11/06 09:37:04 3.6 +++ ray/src/hd/holofile.c 1997/12/11 11:29:35 3.10 @@ -22,13 +22,13 @@ static char SCCSid[] = "$SunId$ SGI"; #define PCTFREE 20 /* maximum fraction to free (%) */ #endif +/* define MAXFRAG if you want to limit fragment tracking memory */ + #ifndef BSD #define write writebuf /* safe i/o routines */ #define read readbuf #endif -#define MAXFRAG (128*FRAGBLK) /* maximum fragments per file */ - #define FRAGBLK 64 /* number of fragments to allocate at a time */ int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ @@ -73,7 +73,7 @@ register int fd; hdrelease(fd) /* stop tracking file fragments for some section */ register int fd; { - if (fd >= nhdfrags || !hdfrag[fd].nlinks) + if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) return; if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { free((char *)hdfrag[fd].fi); @@ -211,6 +211,25 @@ int all; /* include overhead (painful) */ long +hdfilen(fd) /* return file length for fd */ +int fd; +{ + long fpos, flen; + + if (fd < 0) + return(-1); + if (fd >= nhdfrags || !hdfrag[fd].nlinks) { + if ((fpos = lseek(fd, 0L, 1)) < 0) + return(-1); + flen = lseek(fd, 0L, 2); + lseek(fd, fpos, 0); + return(flen); + } + return(hdfrag[fd].flen); +} + + +long hdfiluse(fd, all) /* compute file usage (in bytes) */ int fd; /* open file descriptor to check */ int all; /* include overhead and unflushed data */ @@ -416,10 +435,13 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } + if (hp->fd < 0) /* check for recursive error */ + return(-1); if (i == 0) { /* clear entire holodeck */ nchanged = 0; - for (i = 1; i <= nbeams(hp); i++) - nchanged += hdfreebeam(hp, i); + for (i = nbeams(hp); i > 0; i--) + if (hp->bl[i] != NULL) + nchanged += hdfreebeam(hp, i); return(nchanged); } if (i < 1 | i > nbeams(hp)) @@ -434,8 +456,10 @@ register int i; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "cannot seek on holodeck file"); n = hp->bl[i]->nrm * sizeof(RAYVAL); - if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) + if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { + hp->fd = -1; /* avoid recursive error */ error(SYSTEM, "write error in hdfreebeam"); + } } blglob(hp)->nrm -= hp->bl[i]->nrm; free((char *)hp->bl[i]); /* free memory */ @@ -444,6 +468,47 @@ register int i; } +int +hdkillbeam(hp, i) /* delete beam from holodeck */ +register HOLO *hp; +register int i; +{ + static BEAM emptybeam; + int nchanged, n; + + if (hp == NULL) { /* clobber all holodecks */ + nchanged = 0; + for (i = 0; hdlist[i] != NULL; i++) + nchanged += hdkillbeam(hdlist[i], 0); + return(nchanged); + } + if (i == 0) { /* clobber entire holodeck */ + nchanged = 0; + for (i = nbeams(hp); i > 0; i--) + if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) + nchanged += hdkillbeam(hp, i); +#ifdef DEBUG + if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) + error(CONSISTENCY, "bad beam count in hdkillbeam"); +#endif + return(nchanged); + } + if (i < 1 | i > nbeams(hp)) + error(CONSISTENCY, "bad beam index to hdkillbeam"); + if (hp->bl[i] != NULL) { /* free memory */ + blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; + free((char *)hp->bl[i]); + } else + nchanged = hp->bi[i].nrd; + if (hp->bi[i].nrd) { /* free file fragment */ + hp->bl[i] = &emptybeam; + hdgetbi(hp, i); + } + hp->bl[i] = NULL; + return(nchanged); +} + + hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ register HOLO *ha[]; /* section list (NULL terminated) */ register int ba[]; /* beam index to go with section */ @@ -457,7 +522,7 @@ register HOLO *hp; /* section we're adding from */ ; nents = j; /* insert each beam from hp */ - for (i = nbeams(hp); i > 0; i-- ) { + for (i = nbeams(hp); i > 0; i--) { if (hp->bl[i] == NULL) /* check if loaded */ continue; if ((j = ++nents) > n) /* grow list if we can */