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.24 by gwlarson, Tue Sep 15 14:26:23 1998 UTC vs.
Revision 3.34 by gwlarson, Tue Jan 19 10:08:34 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 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
17   #endif
18   #ifndef FREEBEAMS
19 < #define FREEBEAMS       1024    /* maximum beams to free at a time */
19 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22   #define PCTFREE         15      /* maximum fraction to free (%) */
23   #endif
24 < #ifndef MAXFRAG
25 < #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
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))
34 + #endif
35  
36   #ifndef BSD
37   #define write   writebuf        /* safe i/o routines */
38   #define read    readbuf
39   #endif
40  
41 < #define FRAGBLK         256     /* number of fragments to allocate at a time */
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 48 | Line 57 | static struct fraglist {
57   static int      nhdfragls;      /* size of hdfragl array */
58  
59  
60 + char *
61 + hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
62 + char    *ptr;
63 + unsigned        siz;
64 + char    *rout;
65 + {
66 +        register char   *newp;
67 +                                        /* call malloc/realloc */
68 +        if (ptr == NULL) newp = (char *)malloc(siz);
69 +        else newp = (char *)realloc(ptr, siz);
70 +                                        /* check success */
71 +        if (newp == NULL && rout != NULL) {
72 +                hdfreecache(25, NULL);  /* free some memory */
73 +                errno = 0;              /* retry */
74 +                newp = hdrealloc(ptr, siz, NULL);
75 +                if (newp == NULL) {     /* give up and report error */
76 +                        sprintf(errmsg, "out of memory in %s", rout);
77 +                        error(SYSTEM, errmsg);
78 +                }
79 +        }
80 +        return(newp);
81 + }
82 +
83 +
84   hdattach(fd)            /* start tracking file fragments for some section */
85   register int    fd;
86   {
87          if (fd >= nhdfragls) {
88 <                if (nhdfragls)
89 <                        hdfragl = (struct fraglist *)realloc((char *)hdfragl,
57 <                                        (fd+1)*sizeof(struct fraglist));
58 <                else
59 <                        hdfragl = (struct fraglist *)malloc(
60 <                                        (fd+1)*sizeof(struct fraglist));
61 <                if (hdfragl == NULL)
62 <                        error(SYSTEM, "out of memory in hdattach");
88 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
89 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
90                  bzero((char *)(hdfragl+nhdfragls),
91                                  (fd+1-nhdfragls)*sizeof(struct fraglist));
92                  nhdfragls = fd+1;
# Line 85 | Line 112 | register int   fd;
112   }
113  
114  
88 markdirty(hp)                   /* mark holodeck section directory dirty */
89 register HOLO   *hp;
90 {
91        static BEAMI    smudge = {0, -1};
92
93        if (hp->dirty)          /* already marked? */
94                return;
95        hp->dirty = 1;
96        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
97                        || write(hp->fd, (char *)&smudge,
98                                        sizeof(BEAMI)) != sizeof(BEAMI))
99                error(SYSTEM, "seek/write error in markdirty");
100 }
101
102
115   HOLO *
116   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
117   int     fd;                     /* corresponding file descriptor */
# Line 126 | Line 138 | HDGRID *hproto;                /* holodeck section grid */
138                  if (read(fd, (char *)(hp->bi+1), n) != n)
139                          error(SYSTEM, "failure loading holodeck directory");
140                                                  /* check that it's clean */
141 <                if (hp->bi[nbeams(hp)].fo < 0)
142 <                        error(WARNING, "dirty holodeck section");
141 >                for (n = nbeams(hp); n > 0; n--)
142 >                        if (hp->bi[n].fo < 0) {
143 >                                hp->bi[n].fo = 0;
144 >                                error(WARNING, "dirty holodeck section");
145 >                                break;
146 >                        }
147          } else {                        /* assume we're creating it */
148                  if ((hp = hdalloc(hproto)) == NULL)
149                          goto memerr;
# Line 148 | Line 164 | HDGRID *hproto;                /* holodeck section grid */
164          biglob(hp)->nrd = rtrunc = 0;
165          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
166                  if (hp->bi[n].nrd)
167 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos) {
167 >                        if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
168                                  rtrunc += hp->bi[n].nrd;
169                                  hp->bi[n].nrd = 0;
170                          } else
# Line 171 | Line 187 | memerr:
187   }
188  
189  
190 + markdirty(hp, i)                /* mark holodeck directory position dirty */
191 + register HOLO   *hp;
192 + int     i;
193 + {
194 +        static BEAMI    smudge = {0, -1};
195 +        int     mindist, minpos;
196 +        register int    j;
197 +
198 +        if (!hp->dirty++) {                     /* write smudge first time */
199 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
200 +                                || write(hp->fd, (char *)&smudge,
201 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
202 +                        error(SYSTEM, "seek/write error in markdirty");
203 +                hp->dirseg[0].s = i;
204 +                hp->dirseg[0].n = 1;
205 +                return;
206 +        }
207 +                                                /* insert into segment list */
208 +        for (j = hp->dirty; j--; ) {
209 +                if (!j || hp->dirseg[j-1].s < i) {
210 +                        hp->dirseg[j].s = i;
211 +                        hp->dirseg[j].n = 1;
212 +                        break;
213 +                }
214 +                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
215 +        }
216 +        do {                            /* check neighbors */
217 +                mindist = nbeams(hp);           /* find closest */
218 +                for (j = hp->dirty; --j; )
219 +                        if (hp->dirseg[j].s -
220 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
221 +                                        < mindist) {
222 +                                minpos = j;
223 +                                mindist = hp->dirseg[j].s -
224 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
225 +                        }
226 +                                                /* good enough? */
227 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
228 +                        break;
229 +                j = minpos - 1;                 /* coalesce neighbors */
230 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
231 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
232 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
233 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
234 +                hp->dirty--;
235 +                while (++j < hp->dirty)         /* close the gap */
236 +                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
237 +        } while (mindist <= MINDIRSEL);
238 + }
239 +
240 +
241   int
242   hdsync(hp, all)                 /* update beams and directory on disk */
243   register HOLO   *hp;
# Line 190 | Line 257 | int    all;
257                          hdsyncbeam(hp, j);
258          if (!hp->dirty)                 /* directory clean? */
259                  return(0);
260 <        errno = 0;
261 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
262 <                error(SYSTEM, "cannot seek on holodeck file");
263 <        n = nbeams(hp)*sizeof(BEAMI);
264 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
265 <                error(SYSTEM, "cannot update holodeck section directory");
266 <        hp->dirty = 0;
260 >        errno = 0;                      /* write dirty segments */
261 >        for (j = 0; j < hp->dirty; j++) {
262 >                if (lseek(hp->fd, biglob(hp)->fo +
263 >                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
264 >                        error(SYSTEM, "cannot seek on holodeck file");
265 >                n = hp->dirseg[j].n * sizeof(BEAMI);
266 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
267 >                        error(SYSTEM, "cannot update section directory");
268 >        }
269 >        hp->dirty = 0;                  /* all clean */
270          return(1);
271   }
272  
# Line 284 | 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))
290 <                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)
363                  hdfreecache(PCTFREE, NULL);     /* free some space */
295        errno = 0;
364          if (hp->bl[i] == NULL) {                /* allocate (and load) */
365                  n = hp->bi[i].nrd + nr;
366 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
299 <                        goto memerr;
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");
372                          n *= sizeof(RAYVAL);
# Line 307 | Line 375 | int    nr;                     /* number of new rays desired */
375                                  "error reading beam from holodeck file");
376                  }
377          } else {                                /* just grow in memory */
378 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
379 <                                hdbsiz(hp->bl[i]->nrm + nr) );
312 <                if (hp->bl[i] == NULL)
313 <                        goto memerr;
378 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
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));
387 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
320 <        blglob(hp)->tick = hdclock++;
387 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
388          return(p);                              /* point to new rays */
322 memerr:
323        error(SYSTEM, "out of memory in hdnewrays");
389   }
390  
391  
# Line 331 | 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);
404                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
405                          hdfreecache(PCTFREE, NULL);     /* get free space */
406 <                errno = 0;
342 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
343 <                        error(SYSTEM, "cannot allocate memory for beam");
406 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
407                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
408 +                errno = 0;
409                  if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
410                          error(SYSTEM, "seek error on holodeck file");
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 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
352 <        blglob(hp)->tick = hdclock++;
417 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
418          return(hp->bl[i]);
419   }
420  
# Line 383 | Line 448 | int    (*bf)();                /* callback function (optional) */
448          register BEAM   *bp;
449          register int    i;
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 417 | 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 > int
487 > hdfreefrag(hp, i)                       /* free a file fragment */
488 > HOLO    *hp;
489 > int     i;
490   {
491 +        register BEAMI  *bi = &hp->bi[i];
492          register struct fraglist        *f;
493          register int    j, k;
494  
495 <        if (bi->nrd == 0)
496 <                return;
497 < #ifdef DEBUG
498 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
499 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
432 < #endif
433 <        f = &hdfragl[fd];
495 >        if (bi->nrd <= 0)
496 >                return(0);
497 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
498 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
499 >        f = &hdfragl[hp->fd];
500          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
501                  for (j = k = 0; k < f->nfrags; j++, k++) {
502                          while (f->fi[k].nrd == 0)
# Line 443 | Line 509 | register BEAMI *bi;
509                  f->nfrags = j;
510          }
511          j = f->nfrags++;                /* allocate a slot in free list */
512 < #if MAXFRAG
513 <        if (j >= MAXFRAG-1)
514 <                f->nfrags--;
512 > #if MAXFRAGB
513 >        if (j >= MAXFRAGB*FRAGBLK) {
514 >                f->nfrags = j--;        /* stop list growth */
515 >                if (bi->nrd <= f->fi[j].nrd)
516 >                        return(0);      /* new one no better than discard */
517 >        }
518   #endif
519 <        if (j % FRAGBLK == 0) {         /* more free list space */
519 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
520 >                register BEAMI  *newp;
521                  if (f->fi == NULL)
522 <                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
522 >                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
523                  else
524 <                        f->fi = (BEAMI *)realloc((char *)f->fi,
524 >                        newp = (BEAMI *)realloc((char *)f->fi,
525                                          (j+FRAGBLK)*sizeof(BEAMI));
526 <                if (f->fi == NULL)
527 <                        error(SYSTEM, "out of memory in hdfreefrag");
526 >                if (newp == NULL) {
527 >                        f->nfrags--;    /* graceful failure */
528 >                        return(0);
529 >                }
530 >                f->fi = newp;
531          }
532          for ( ; ; j--) {                /* insert in descending list */
533                  if (!j || bi->fo < f->fi[j-1].fo) {
# Line 479 | Line 552 | register BEAMI *bi;
552                          }
553                          break;
554                  }
555 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
556 +        bi->nrd = 0;
557 +        bi->fo = 0L;
558 +        return(1);
559   }
560  
561  
562 + int
563 + hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
564 + int     fd;
565 + int     *listlen;
566 + register int4   *listsiz;
567 + {
568 +        register struct fraglist        *f;
569 +        register int    i;
570 +
571 +        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
572 +                return(0);              /* listless */
573 +        if (listlen != NULL)
574 +                *listlen = f->nfrags;
575 +        if (listsiz != NULL)
576 +                for (i = f->nfrags, *listsiz = 0; i--; )
577 +                        *listsiz += f->fi[i].nrd;
578 + #if MAXFRAGB
579 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
580 + #else
581 +        return(1);                      /* list never fills up */
582 + #endif
583 + }
584 +
585 +
586   long
587   hdallocfrag(fd, nrays)          /* allocate a file fragment */
588   int     fd;
589   unsigned int4   nrays;
590   {
591          register struct fraglist        *f;
592 <        register int    j, k;
592 >        register int    j;
593          long    nfo;
594  
595          if (nrays == 0)
596                  return(-1L);
597 < #ifdef DEBUG
598 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
498 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
499 < #endif
597 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
598 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
599          f = &hdfragl[fd];
600 <        k = -1;                         /* find closest-sized fragment */
601 <        for (j = f->nfrags; j-- > 0; )
602 <                if (f->fi[j].nrd >= nrays &&
603 <                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
505 <                        if (f->fi[k=j].nrd == nrays)
506 <                                break;
507 <        if (k < 0) {                    /* no fragment -- extend file */
600 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
601 >                if (f->fi[j].nrd >= nrays)
602 >                        break;
603 >        if (j < 0) {                    /* no fragment -- extend file */
604                  nfo = f->flen;
605                  f->flen += nrays*sizeof(RAYVAL);
606          } else {                        /* else use fragment */
607 <                nfo = f->fi[k].fo;
608 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
609 <                f->fi[k].nrd -= nrays;
607 >                nfo = f->fi[j].fo;
608 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
609 >                f->fi[j].nrd -= nrays;
610          }
611          return(nfo);
612   }
# Line 527 | Line 623 | register int   i;
623                                          /* check file status */
624          if (hdfragl[hp->fd].writerr)
625                  return(-1);
626 < #ifdef DEBUG
627 <        if (i < 1 | i > nbeams(hp))
532 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
533 < #endif
626 >        DCHECK(i < 1 | i > nbeams(hp),
627 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
628                                          /* is current fragment OK? */
629          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
630                  return(0);
631 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
632 <                hdfreefrag(hp->fd, &hp->bi[i]);
631 >        if (hdfragflags&FF_WRITE && hp->bi[i].nrd)
632 >                hdfreefrag(hp, i);      /* relinquish old fragment */
633          if (nrays) {                    /* get and write new fragment */
634                  nfo = hdallocfrag(hp->fd, nrays);
635                  errno = 0;
# Line 552 | Line 646 | register int   i;
646                  hp->bi[i].fo = 0L;
647          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
648          hp->bi[i].nrd = nrays;
649 <        markdirty(hp);                  /* section directory now out of date */
649 >        markdirty(hp, i);               /* section directory now out of date */
650          return(1);
651   }
652  
# Line 579 | Line 673 | register int   i;
673                                  nchanged += hdfreebeam(hp, i);
674                  return(nchanged);
675          }
676 < #ifdef DEBUG
677 <        if (i < 1 | i > nbeams(hp))
584 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
585 < #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 601 | Line 693 | hdkillbeam(hp, i)              /* delete beam from holodeck */
693   register HOLO   *hp;
694   register int    i;
695   {
604        static BEAM     emptybeam;
696          int     nchanged;
697  
698          if (hp == NULL) {               /* clobber all holodecks */
# Line 615 | 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)
620 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
621 < #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))
626 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
627 < #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 >                biglob(hp)->nrd -= hp->bi[i].nrd;
724 >                hp->bi[i].nrd = 0;      /* make sure it's gone */
725 >                hp->bi[i].fo = 0L;
726          }
727          hp->bl[i] = NULL;
728          return(nchanged);
# Line 680 | Line 770 | register HOLO  *honly;                 /* NULL means check all */
770          int     freetarget;
771          int     n;
772          register int    i;
773 + #ifdef DEBUG
774 +        unsigned        membefore;
775 +
776 +        membefore = hdmemuse(0);
777 + #endif
778                                                  /* compute free target */
779          freetarget = (honly != NULL) ? blglob(honly)->nrm :
780                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 700 | Line 795 | register HOLO  *honly;                 /* NULL means check all */
795                          break;
796          }
797          hdsync(honly, 0);       /* synchronize directories as necessary */
798 + #ifdef DEBUG
799 +        sprintf(errmsg,
800 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
801 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
802 +        wputs(errmsg);
803 + #endif
804          return(-freetarget);    /* return how far past goal we went */
805   }
806  
# Line 717 | Line 818 | register HOLO  *hp;            /* NULL means clean up all */
818                  return;
819          }
820                                          /* flush all data and free memory */
821 <        hdflush(hp);
821 >        hdfreebeam(hp, 0);
822 >        hdsync(hp, 0);
823                                          /* release fragment resources */
824          hdrelease(hp->fd);
825                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines