--- ray/src/hd/holofile.c 1998/09/18 13:51:01 3.25 +++ ray/src/hd/holofile.c 1998/11/09 17:10:53 3.28 @@ -48,18 +48,36 @@ static struct fraglist { static int nhdfragls; /* size of hdfragl array */ +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) /* start tracking file fragments for some section */ register int fd; { 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; @@ -85,21 +103,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 */ @@ -126,8 +129,12 @@ 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"); + for (n = nbeams(hp); n > 0; n--) + if (hp->bi[n].fo < 0) { + hp->bi[n].fo = 0; + error(WARNING, "dirty holodeck section"); + break; + } } else { /* assume we're creating it */ if ((hp = hdalloc(hproto)) == NULL) goto memerr; @@ -148,7 +155,7 @@ HDGRID *hproto; /* holodeck section grid */ 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 +178,54 @@ memerr: } +markdirty(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 markdirty"); + 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)); + } + mindist = nbeams(hp); /* find closest neighbors */ + for (j = hp->dirty; --j; ) + if (hp->dirseg[j].s - (hp->dirseg[j-1].s + hp->dirseg[j-1].n) + < mindist) { + mindist = hp->dirseg[j].s - + (hp->dirseg[j-1].s + hp->dirseg[j-1].n); + minpos = j; + } + if (hp->dirty > MAXDIRSE || mindist <= BUFSIZ/sizeof(BEAMI)) { + 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--; /* close the gap */ + for (j = minpos; j < hp->dirty; j++) + copystruct(hp->dirseg+j, hp->dirseg+(j+1)); + } +} + + int hdsync(hp, all) /* update beams and directory on disk */ register HOLO *hp; @@ -190,13 +245,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); } @@ -292,13 +350,12 @@ int nr; /* number of new rays desired */ 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) { + errno = 0; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); @@ -307,10 +364,8 @@ 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; } p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; @@ -318,8 +373,6 @@ int nr; /* number of new rays desired */ bzero((char *)p, nr*sizeof(RAYVAL)); 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"); } @@ -337,10 +390,9 @@ register int i; 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); @@ -447,13 +499,17 @@ register BEAMI *bi; f->nfrags--; #endif if (j % FRAGBLK == 0) { /* more 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; + } + f->fi = newp; } for ( ; ; j--) { /* insert in descending list */ if (!j || bi->fo < f->fi[j-1].fo) { @@ -551,7 +607,7 @@ 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 */ + markdirty(hp, i); /* section directory now out of date */ return(1); } @@ -727,7 +783,8 @@ register HOLO *hp; /* NULL means clean up all */ return; } /* flush all data and free memory */ - hdflush(hp); + hdfreebeam(hp, 0); + hdsync(hp, 0); /* release fragment resources */ hdrelease(hp->fd); /* remove hp from active list */