--- ray/src/hd/holofile.c 1998/09/15 14:26:23 3.24 +++ ray/src/hd/holofile.c 1999/08/05 15:53:09 3.41 @@ -1,4 +1,4 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ +/* Copyright (c) 1999 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -13,33 +13,46 @@ static char SCCSid[] = "$SunId$ SGI"; #include "holo.h" #ifndef CACHESIZE -#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ +#ifdef BIGMEM +#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ +#else +#define CACHESIZE 5 #endif +#endif #ifndef FREEBEAMS -#define FREEBEAMS 1024 /* maximum beams to free at a time */ +#define FREEBEAMS 1500 /* maximum beams to free at a time */ #endif #ifndef PCTFREE #define PCTFREE 15 /* maximum fraction to free (%) */ #endif -#ifndef MAXFRAG -#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ +#ifndef MAXFRAGB +#define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */ #endif +#ifndef FF_DEFAULT + /* when to free a beam fragment */ +#define FF_DEFAULT (FF_WRITE|FF_KILL) +#endif +#ifndef MINDIRSEL + /* minimum directory seek length */ +#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) +#endif #ifndef BSD #define write writebuf /* safe i/o routines */ #define read readbuf #endif -#define FRAGBLK 256 /* number of fragments to allocate at a time */ +#define FRAGBLK 512 /* number of fragments to allocate at a time */ +int hdfragflags = FF_DEFAULT; /* tells when to free fragments */ unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ -unsigned long hdclock; /* clock value */ +unsigned long hdclock; /* clock value */ HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ static struct fraglist { short nlinks; /* number of holodeck sections using us */ - short writerr; /* write error encountered */ + short writable; /* 0 read-only, <0 write error encountered */ int nfrags; /* number of known fragments */ BEAMI *fi; /* fragments, descending file position */ long flen; /* last known file length */ @@ -48,23 +61,77 @@ static struct fraglist { static int nhdfragls; /* size of hdfragl array */ -hdattach(fd) /* start tracking file fragments for some section */ +HOLO * +hdalloc(hproto) /* allocate and set holodeck section based on grid */ +HDGRID *hproto; +{ + HOLO hdhead; + register HOLO *hp; + int n; + /* copy grid to temporary header */ + bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); + /* compute grid vectors and sizes */ + hdcompgrid(&hdhead); + /* allocate header with directory */ + n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); + if ((hp = (HOLO *)malloc(n)) == NULL) + return(NULL); + /* copy header information */ + copystruct(hp, &hdhead); + /* allocate and clear beam list */ + hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); + if (hp->bl == NULL) { + free((char *)hp); + return(NULL); + } + bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); + hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ + hp->fd = -1; + hp->dirty = 0; + hp->priv = NULL; + /* clear beam directory */ + bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); + return(hp); /* all is well */ +} + + +char * +hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ +char *ptr; +unsigned siz; +char *rout; +{ + register char *newp; + /* call malloc/realloc */ + if (ptr == NULL) newp = (char *)malloc(siz); + else newp = (char *)realloc(ptr, siz); + /* check success */ + if (newp == NULL && rout != NULL) { + hdfreecache(25, NULL); /* free some memory */ + errno = 0; /* retry */ + newp = hdrealloc(ptr, siz, NULL); + if (newp == NULL) { /* give up and report error */ + sprintf(errmsg, "out of memory in %s", rout); + error(SYSTEM, errmsg); + } + } + return(newp); +} + + +hdattach(fd, wr) /* start tracking file fragments for some section */ register int fd; +int wr; { if (fd >= nhdfragls) { - if (nhdfragls) - hdfragl = (struct fraglist *)realloc((char *)hdfragl, - (fd+1)*sizeof(struct fraglist)); - else - hdfragl = (struct fraglist *)malloc( - (fd+1)*sizeof(struct fraglist)); - if (hdfragl == NULL) - error(SYSTEM, "out of memory in hdattach"); + hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, + (fd+1)*sizeof(struct fraglist), "hdattach"); bzero((char *)(hdfragl+nhdfragls), (fd+1-nhdfragls)*sizeof(struct fraglist)); nhdfragls = fd+1; } hdfragl[fd].nlinks++; + hdfragl[fd].writable = wr; /* set writable flag */ hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ } @@ -85,21 +152,6 @@ register int fd; } -markdirty(hp) /* mark holodeck section directory dirty */ -register HOLO *hp; -{ - static BEAMI smudge = {0, -1}; - - if (hp->dirty) /* already marked? */ - return; - hp->dirty = 1; - if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 - || write(hp->fd, (char *)&smudge, - sizeof(BEAMI)) != sizeof(BEAMI)) - error(SYSTEM, "seek/write error in markdirty"); -} - - HOLO * hdinit(fd, hproto) /* initialize a holodeck section in a file */ int fd; /* corresponding file descriptor */ @@ -107,6 +159,7 @@ HDGRID *hproto; /* holodeck section grid */ { long rtrunc; long fpos; + int writable; register HOLO *hp; register int n; /* prepare for system errors */ @@ -126,9 +179,20 @@ HDGRID *hproto; /* holodeck section grid */ if (read(fd, (char *)(hp->bi+1), n) != n) error(SYSTEM, "failure loading holodeck directory"); /* check that it's clean */ - if (hp->bi[nbeams(hp)].fo < 0) - error(WARNING, "dirty holodeck section"); - } else { /* assume we're creating it */ + for (n = nbeams(hp); n > 0; n--) + if (hp->bi[n].fo < 0) { + hp->bi[n].fo = 0; + error(WARNING, "dirty holodeck section"); + break; + } + /* check writability */ + if (fd < nhdfragls && hdfragl[fd].nlinks) + writable = hdfragl[fd].writable; + else + writable = lseek(fd, fpos, 0) == fpos && + write(fd, (char *)hp, sizeof(HDGRID)) == + sizeof(HDGRID); + } else { /* else assume we're creating it */ if ((hp = hdalloc(hproto)) == NULL) goto memerr; /* write header and skeleton */ @@ -137,18 +201,19 @@ HDGRID *hproto; /* holodeck section grid */ sizeof(HDGRID) || write(fd, (char *)(hp->bi+1), n) != n) error(SYSTEM, "cannot write header to holodeck file"); + writable = 1; } hp->fd = fd; hp->dirty = 0; biglob(hp)->fo = fpos + sizeof(HDGRID); /* start tracking fragments */ - hdattach(fd); + hdattach(fd, writable); /* check rays on disk */ fpos = hdfilen(fd); biglob(hp)->nrd = rtrunc = 0; for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) if (hp->bi[n].nrd) - if (hp->bi[n].fo + hp->bi[n].nrd > fpos) { + if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { rtrunc += hp->bi[n].nrd; hp->bi[n].nrd = 0; } else @@ -171,6 +236,57 @@ memerr: } +hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ +register HOLO *hp; +int i; +{ + static BEAMI smudge = {0, -1}; + int mindist, minpos; + register int j; + + if (!hp->dirty++) { /* write smudge first time */ + if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 + || write(hp->fd, (char *)&smudge, + sizeof(BEAMI)) != sizeof(BEAMI)) + error(SYSTEM, "seek/write error in hdmarkdirty"); + hp->dirseg[0].s = i; + hp->dirseg[0].n = 1; + return; + } + /* insert into segment list */ + for (j = hp->dirty; j--; ) { + if (!j || hp->dirseg[j-1].s < i) { + hp->dirseg[j].s = i; + hp->dirseg[j].n = 1; + break; + } + copystruct(hp->dirseg+j, hp->dirseg+(j-1)); + } + do { /* check neighbors */ + mindist = nbeams(hp); /* find closest */ + for (j = hp->dirty; --j; ) + if (hp->dirseg[j].s - + (hp->dirseg[j-1].s + hp->dirseg[j-1].n) + < mindist) { + minpos = j; + mindist = hp->dirseg[j].s - + (hp->dirseg[j-1].s + hp->dirseg[j-1].n); + } + /* good enough? */ + if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) + break; + j = minpos - 1; /* coalesce neighbors */ + if (hp->dirseg[j].s + hp->dirseg[j].n < + hp->dirseg[minpos].s + hp->dirseg[minpos].n) + hp->dirseg[j].n = hp->dirseg[minpos].s + + hp->dirseg[minpos].n - hp->dirseg[j].s; + hp->dirty--; + while (++j < hp->dirty) /* close the gap */ + copystruct(hp->dirseg+j, hp->dirseg+(j+1)); + } while (mindist <= MINDIRSEL); +} + + int hdsync(hp, all) /* update beams and directory on disk */ register HOLO *hp; @@ -190,13 +306,16 @@ int all; hdsyncbeam(hp, j); if (!hp->dirty) /* directory clean? */ return(0); - errno = 0; - if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) - error(SYSTEM, "cannot seek on holodeck file"); - n = nbeams(hp)*sizeof(BEAMI); - if (write(hp->fd, (char *)(hp->bi+1), n) != n) - error(SYSTEM, "cannot update holodeck section directory"); - hp->dirty = 0; + errno = 0; /* write dirty segments */ + for (j = 0; j < hp->dirty; j++) { + if (lseek(hp->fd, biglob(hp)->fo + + (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) + error(SYSTEM, "cannot seek on holodeck file"); + n = hp->dirseg[j].n * sizeof(BEAMI); + if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) + error(SYSTEM, "cannot update section directory"); + } + hp->dirty = 0; /* all clean */ return(1); } @@ -284,21 +403,19 @@ int nr; /* number of new rays desired */ RAYVAL *p; int n; - if (nr <= 0) - return(NULL); - if (i < 1 | i > nbeams(hp)) - error(CONSISTENCY, "bad beam index given to hdnewrays"); + if (nr <= 0) return(NULL); + CHECK(i < 1 | i > nbeams(hp), + CONSISTENCY, "bad beam index given to hdnewrays"); if (hp->bl[i] != NULL) hp->bl[i]->tick = hdclock; /* preempt swap */ if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) hdfreecache(PCTFREE, NULL); /* free some space */ - errno = 0; if (hp->bl[i] == NULL) { /* allocate (and load) */ n = hp->bi[i].nrd + nr; - if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) - goto memerr; + hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); blglob(hp)->nrm += n; - if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { + if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { + errno = 0; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); @@ -307,20 +424,17 @@ int nr; /* number of new rays desired */ "error reading beam from holodeck file"); } } else { /* just grow in memory */ - hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], - hdbsiz(hp->bl[i]->nrm + nr) ); - if (hp->bl[i] == NULL) - goto memerr; + hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], + hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); blglob(hp)->nrm += nr; } + if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) + hdfreefrag(hp, i); /* relinquish old fragment */ p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; hp->bl[i]->nrm += nr; /* update in-core structure */ bzero((char *)p, nr*sizeof(RAYVAL)); - hp->bl[i]->tick = hdclock; /* update LRU clock */ - blglob(hp)->tick = hdclock++; + blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ return(p); /* point to new rays */ -memerr: - error(SYSTEM, "out of memory in hdnewrays"); } @@ -331,25 +445,25 @@ register int i; { register int n; - if (i < 1 | i > nbeams(hp)) - error(CONSISTENCY, "bad beam index given to hdgetbeam"); + CHECK(i < 1 | i > nbeams(hp), + CONSISTENCY, "bad beam index given to hdgetbeam"); if (hp->bl[i] == NULL) { /* load from disk */ if (!(n = hp->bi[i].nrd)) return(NULL); if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) hdfreecache(PCTFREE, NULL); /* get free space */ - errno = 0; - if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) - error(SYSTEM, "cannot allocate memory for beam"); + hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); blglob(hp)->nrm += hp->bl[i]->nrm = n; + errno = 0; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) error(SYSTEM, "error reading beam from holodeck file"); + if (hdfragflags&FF_READ) + hdfreefrag(hp, i); /* relinquish old frag. */ } - hp->bl[i]->tick = hdclock; /* update LRU clock */ - blglob(hp)->tick = hdclock++; + blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ return(hp->bl[i]); } @@ -383,8 +497,9 @@ int (*bf)(); /* callback function (optional) */ register BEAM *bp; register int i; /* precheck consistency */ + if (n <= 0) return; for (i = n; i--; ) - if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) + if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) error(CONSISTENCY, "bad beam in hdloadbeams"); /* sort list for optimal access */ qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); @@ -417,20 +532,22 @@ int (*bf)(); /* callback function (optional) */ } -hdfreefrag(fd, bi) /* free a file fragment */ -int fd; -register BEAMI *bi; +int +hdfreefrag(hp, i) /* free a file fragment */ +HOLO *hp; +int i; { + register BEAMI *bi = &hp->bi[i]; register struct fraglist *f; register int j, k; - if (bi->nrd == 0) - return; -#ifdef DEBUG - if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) - error(CONSISTENCY, "bad file descriptor in hdfreefrag"); -#endif - f = &hdfragl[fd]; + if (bi->nrd <= 0) + return(0); + DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, + CONSISTENCY, "bad file descriptor in hdfreefrag"); + f = &hdfragl[hp->fd]; + if (!f->writable) + return(0); if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ for (j = k = 0; k < f->nfrags; j++, k++) { while (f->fi[k].nrd == 0) @@ -443,18 +560,25 @@ register BEAMI *bi; f->nfrags = j; } j = f->nfrags++; /* allocate a slot in free list */ -#if MAXFRAG - if (j >= MAXFRAG-1) - f->nfrags--; +#if MAXFRAGB + if (j >= MAXFRAGB*FRAGBLK) { + f->nfrags = j--; /* stop list growth */ + if (bi->nrd <= f->fi[j].nrd) + return(0); /* new one no better than discard */ + } #endif - if (j % FRAGBLK == 0) { /* more free list space */ + if (j % FRAGBLK == 0) { /* more (or less) free list space */ + register BEAMI *newp; if (f->fi == NULL) - f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); + newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); else - f->fi = (BEAMI *)realloc((char *)f->fi, + newp = (BEAMI *)realloc((char *)f->fi, (j+FRAGBLK)*sizeof(BEAMI)); - if (f->fi == NULL) - error(SYSTEM, "out of memory in hdfreefrag"); + if (newp == NULL) { + f->nfrags--; /* graceful failure */ + return(0); + } + f->fi = newp; } for ( ; ; j--) { /* insert in descending list */ if (!j || bi->fo < f->fi[j-1].fo) { @@ -479,38 +603,62 @@ register BEAMI *bi; } break; } + biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ + bi->nrd = 0; + bi->fo = 0L; + hdmarkdirty(hp, i); /* assume we'll reallocate */ + return(1); } +int +hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ +int fd; +int *listlen; +register int4 *listsiz; +{ + register struct fraglist *f; + register int i; + + if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) + return(0); /* listless */ + if (listlen != NULL) + *listlen = f->nfrags; + if (listsiz != NULL) + for (i = f->nfrags, *listsiz = 0; i--; ) + *listsiz += f->fi[i].nrd; +#if MAXFRAGB + return(f->nfrags < MAXFRAGB*FRAGBLK); +#else + return(1); /* list never fills up */ +#endif +} + + long hdallocfrag(fd, nrays) /* allocate a file fragment */ int fd; unsigned int4 nrays; { register struct fraglist *f; - register int j, k; + register int j; long nfo; if (nrays == 0) return(-1L); -#ifdef DEBUG - if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) - error(CONSISTENCY, "bad file descriptor in hdallocfrag"); -#endif + DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, + CONSISTENCY, "bad file descriptor in hdallocfrag"); f = &hdfragl[fd]; - k = -1; /* find closest-sized fragment */ - for (j = f->nfrags; j-- > 0; ) - if (f->fi[j].nrd >= nrays && - (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) - if (f->fi[k=j].nrd == nrays) - break; - if (k < 0) { /* no fragment -- extend file */ + for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ + if (f->fi[j].nrd >= nrays) + break; + if (j < 0) { /* no fragment -- extend file */ nfo = f->flen; f->flen += nrays*sizeof(RAYVAL); } else { /* else use fragment */ - nfo = f->fi[k].fo; - f->fi[k].fo += nrays*sizeof(RAYVAL); - f->fi[k].nrd -= nrays; + nfo = f->fi[j].fo; + f->fi[j].fo += nrays*sizeof(RAYVAL); + f->fi[j].nrd -= nrays; } return(nfo); } @@ -521,21 +669,20 @@ hdsyncbeam(hp, i) /* sync beam in memory with beam on register HOLO *hp; register int i; { + int fragfreed; unsigned int4 nrays; unsigned int n; long nfo; /* check file status */ - if (hdfragl[hp->fd].writerr) - return(-1); -#ifdef DEBUG - if (i < 1 | i > nbeams(hp)) - error(CONSISTENCY, "bad beam index in hdsyncbeam"); -#endif + if (hdfragl[hp->fd].writable <= 0) + return(hdfragl[hp->fd].writable); + DCHECK(i < 1 | i > nbeams(hp), + CONSISTENCY, "bad beam index in hdsyncbeam"); /* is current fragment OK? */ if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) return(0); - if (hp->bi[i].nrd) /* relinquish old fragment */ - hdfreefrag(hp->fd, &hp->bi[i]); + /* relinquish old fragment? */ + fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); if (nrays) { /* get and write new fragment */ nfo = hdallocfrag(hp->fd, nrays); errno = 0; @@ -543,7 +690,7 @@ register int i; 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) { - hdfragl[hp->fd].writerr++; + hdfragl[hp->fd].writable = -1; hdsync(NULL, 0); /* sync directories */ error(SYSTEM, "write error in hdsyncbeam"); } @@ -552,7 +699,8 @@ register int i; hp->bi[i].fo = 0L; biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; - markdirty(hp); /* section directory now out of date */ + if (!fragfreed) + hdmarkdirty(hp, i); /* need to flag dir. ent. */ return(1); } @@ -570,19 +718,21 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } - if (hdfragl[hp->fd].writerr) /* check for file error */ + if (hdfragl[hp->fd].writable < 0) /* check for file error */ return(0); if (i == 0) { /* clear entire holodeck */ + if (blglob(hp)->nrm == 0) + return(0); /* already clear */ nchanged = 0; for (i = nbeams(hp); i > 0; i--) if (hp->bl[i] != NULL) nchanged += hdfreebeam(hp, i); + DCHECK(blglob(hp)->nrm != 0, + CONSISTENCY, "bad beam count in hdfreebeam"); return(nchanged); } -#ifdef DEBUG - if (i < 1 | i > nbeams(hp)) - error(CONSISTENCY, "bad beam index to hdfreebeam"); -#endif + DCHECK(i < 1 | i > nbeams(hp), + CONSISTENCY, "bad beam index to hdfreebeam"); if (hp->bl[i] == NULL) return(0); /* check for additions */ @@ -601,7 +751,6 @@ hdkillbeam(hp, i) /* delete beam from holodeck */ register HOLO *hp; register int i; { - static BEAM emptybeam; int nchanged; if (hp == NULL) { /* clobber all holodecks */ @@ -611,30 +760,33 @@ register int i; return(nchanged); } if (i == 0) { /* clobber entire holodeck */ + if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) + return(0); /* already empty */ nchanged = 0; + 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 + DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, + CONSISTENCY, "bad beam count in hdkillbeam"); return(nchanged); } -#ifdef DEBUG - if (i < 1 | i > nbeams(hp)) - error(CONSISTENCY, "bad beam index to hdkillbeam"); -#endif + DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, + "bad beam index to hdkillbeam"); + DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, + "hdkillbeam called on read-only holodeck"); if (hp->bl[i] != NULL) { /* free memory */ blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; free((char *)hp->bl[i]); + hp->bl[i] = NULL; } else nchanged = hp->bi[i].nrd; - if (hp->bi[i].nrd) { /* free file fragment */ - hp->bl[i] = &emptybeam; - hdsyncbeam(hp, i); + if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { + biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ + hp->bi[i].nrd = 0; + hp->bi[i].fo = 0L; + hdmarkdirty(hp, i); } - hp->bl[i] = NULL; return(nchanged); } @@ -680,6 +832,11 @@ register HOLO *honly; /* NULL means check all */ int freetarget; int n; register int i; +#ifdef DEBUG + unsigned membefore; + + membefore = hdmemuse(0); +#endif /* compute free target */ freetarget = (honly != NULL) ? blglob(honly)->nrm : hdmemuse(0)/sizeof(RAYVAL) ; @@ -700,6 +857,12 @@ register HOLO *honly; /* NULL means check all */ break; } hdsync(honly, 0); /* synchronize directories as necessary */ +#ifdef DEBUG + sprintf(errmsg, + "%dK before, %dK after hdfreecache (%dK total), %d rays short\n", + membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); + wputs(errmsg); +#endif return(-freetarget); /* return how far past goal we went */ }