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.46 by greg, Thu May 29 16:26:21 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for managing holodeck files
6   *
# Line 13 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10   #include "holo.h"
11  
12   #ifndef CACHESIZE
13 < #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
13 > #ifdef SMLMEM
14 > #define CACHESIZE       5
15 > #else
16 > #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
17   #endif
18 + #endif
19   #ifndef FREEBEAMS
20 < #define FREEBEAMS       1024    /* maximum beams to free at a time */
20 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
21   #endif
22   #ifndef PCTFREE
23   #define PCTFREE         15      /* maximum fraction to free (%) */
24   #endif
25 < #ifndef MAXFRAG
26 < #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
25 > #ifndef MAXFRAGB
26 > #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
27   #endif
28 + #ifndef FF_DEFAULT
29 +                                /* when to free a beam fragment */
30 + #define FF_DEFAULT      (FF_WRITE|FF_KILL)
31 + #endif
32 + #ifndef MINDIRSEL
33 +                                /* minimum directory seek length */
34 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
35 + #endif
36  
37   #ifndef BSD
38   #define write   writebuf        /* safe i/o routines */
39   #define read    readbuf
40   #endif
41  
42 < #define FRAGBLK         256     /* number of fragments to allocate at a time */
42 > #define FRAGBLK         512     /* number of fragments to allocate at a time */
43  
44 + int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */
45   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
46 < unsigned long   hdclock;        /* clock value */
46 > unsigned long   hdclock;                /* clock value */
47  
48   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
49  
50   static struct fraglist {
51          short   nlinks;         /* number of holodeck sections using us */
52 <        short   writerr;        /* write error encountered */
52 >        short   writable;       /* 0 read-only, <0 write error encountered */
53          int     nfrags;         /* number of known fragments */
54          BEAMI   *fi;            /* fragments, descending file position */
55 <        long    flen;           /* last known file length */
55 >        off_t   flen;           /* last known file length */
56   } *hdfragl;             /* fragment lists, indexed by file descriptor */
57  
58   static int      nhdfragls;      /* size of hdfragl array */
59  
60  
61 < hdattach(fd)            /* start tracking file fragments for some section */
61 > HOLO *
62 > hdalloc(hproto)         /* allocate and set holodeck section based on grid */
63 > HDGRID  *hproto;
64 > {
65 >        HOLO    hdhead;
66 >        register HOLO   *hp;
67 >        int     n;
68 >                                /* copy grid to temporary header */
69 >        bcopy((void *)hproto, (void *)&hdhead, sizeof(HDGRID));
70 >                                /* compute grid vectors and sizes */
71 >        hdcompgrid(&hdhead);
72 >                                /* allocate header with directory */
73 >        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
74 >        if ((hp = (HOLO *)malloc(n)) == NULL)
75 >                return(NULL);
76 >                                /* copy header information */
77 >        copystruct(hp, &hdhead);
78 >                                /* allocate and clear beam list */
79 >        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
80 >        if (hp->bl == NULL) {
81 >                free((void *)hp);
82 >                return(NULL);
83 >        }
84 >        bzero((void *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
85 >        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
86 >        hp->fd = -1;
87 >        hp->dirty = 0;
88 >        hp->priv = NULL;
89 >                                /* clear beam directory */
90 >        bzero((void *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI));
91 >        return(hp);             /* all is well */
92 > }
93 >
94 >
95 > char *
96 > hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
97 > char    *ptr;
98 > unsigned        siz;
99 > char    *rout;
100 > {
101 >        register char   *newp;
102 >                                        /* call malloc/realloc */
103 >        if (ptr == NULL) newp = (char *)malloc(siz);
104 >        else newp = (char *)realloc((void *)ptr, siz);
105 >                                        /* check success */
106 >        if (newp == NULL && rout != NULL) {
107 >                hdfreecache(25, NULL);  /* free some memory */
108 >                errno = 0;              /* retry */
109 >                newp = hdrealloc(ptr, siz, NULL);
110 >                if (newp == NULL) {     /* give up and report error */
111 >                        sprintf(errmsg, "out of memory in %s", rout);
112 >                        error(SYSTEM, errmsg);
113 >                }
114 >        }
115 >        return(newp);
116 > }
117 >
118 >
119 > hdattach(fd, wr)        /* start tracking file fragments for some section */
120   register int    fd;
121 + int     wr;
122   {
123          if (fd >= nhdfragls) {
124 <                if (nhdfragls)
125 <                        hdfragl = (struct fraglist *)realloc((char *)hdfragl,
126 <                                        (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");
63 <                bzero((char *)(hdfragl+nhdfragls),
124 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
125 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
126 >                bzero((void *)(hdfragl+nhdfragls),
127                                  (fd+1-nhdfragls)*sizeof(struct fraglist));
128                  nhdfragls = fd+1;
129          }
130          hdfragl[fd].nlinks++;
131 <        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
131 >        hdfragl[fd].writable = wr;              /* set writable flag */
132 >        hdfragl[fd].flen = lseek(fd, (off_t)0L, 2);     /* get file length */
133   }
134  
135  
# Line 78 | Line 142 | register int   fd;
142          if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
143                  return;
144          if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
145 <                free((char *)hdfragl[fd].fi);
145 >                free((void *)hdfragl[fd].fi);
146                  hdfragl[fd].fi = NULL;
147                  hdfragl[fd].nfrags = 0;
148          }
149   }
150  
151  
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
152   HOLO *
153   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
154   int     fd;                     /* corresponding file descriptor */
155   HDGRID  *hproto;                /* holodeck section grid */
156   {
157 <        long    rtrunc;
158 <        long    fpos;
157 >        off_t   rtrunc;
158 >        off_t   fpos;
159 >        int     writable;
160          register HOLO   *hp;
161          register int    n;
162                                          /* prepare for system errors */
163          errno = 0;
164 <        if ((fpos = lseek(fd, 0L, 1)) < 0)
164 >        if ((fpos = lseek(fd, (off_t)0L, 1)) < 0)
165                  error(SYSTEM, "cannot determine holodeck file position");
166          if (hproto == NULL) {           /* assume we're loading it */
167                  HDGRID  hpr;
# Line 126 | Line 176 | HDGRID *hproto;                /* holodeck section grid */
176                  if (read(fd, (char *)(hp->bi+1), n) != n)
177                          error(SYSTEM, "failure loading holodeck directory");
178                                                  /* check that it's clean */
179 <                if (hp->bi[nbeams(hp)].fo < 0)
180 <                        error(WARNING, "dirty holodeck section");
181 <        } else {                        /* assume we're creating it */
179 >                for (n = nbeams(hp); n > 0; n--)
180 >                        if (hp->bi[n].fo < 0) {
181 >                                hp->bi[n].fo = 0;
182 >                                error(WARNING, "dirty holodeck section");
183 >                                break;
184 >                        }
185 >                                                /* check writability */
186 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
187 >                        writable = hdfragl[fd].writable;
188 >                else
189 >                        writable = lseek(fd, fpos, 0) == fpos &&
190 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
191 >                                                        sizeof(HDGRID);
192 >        } else {                        /* else assume we're creating it */
193                  if ((hp = hdalloc(hproto)) == NULL)
194                          goto memerr;
195                                                  /* write header and skeleton */
# Line 137 | Line 198 | HDGRID *hproto;                /* holodeck section grid */
198                                          sizeof(HDGRID) ||
199                                  write(fd, (char *)(hp->bi+1), n) != n)
200                          error(SYSTEM, "cannot write header to holodeck file");
201 +                writable = 1;
202          }
203          hp->fd = fd;    
204          hp->dirty = 0;
205          biglob(hp)->fo = fpos + sizeof(HDGRID);
206                                          /* start tracking fragments */
207 <        hdattach(fd);
207 >        hdattach(fd, writable);
208                                          /* check rays on disk */
209          fpos = hdfilen(fd);
210          biglob(hp)->nrd = rtrunc = 0;
211          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
212                  if (hp->bi[n].nrd)
213 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos) {
213 >                        if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
214                                  rtrunc += hp->bi[n].nrd;
215                                  hp->bi[n].nrd = 0;
216                          } else
# Line 171 | Line 233 | memerr:
233   }
234  
235  
236 + hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
237 + register HOLO   *hp;
238 + int     i;
239 + {
240 +        static BEAMI    smudge = {0, -1};
241 +        int     mindist, minpos;
242 +        register int    j;
243 +
244 +        if (!hp->dirty++) {                     /* write smudge first time */
245 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
246 +                                || write(hp->fd, (char *)&smudge,
247 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
248 +                        error(SYSTEM, "seek/write error in hdmarkdirty");
249 +                hp->dirseg[0].s = i;
250 +                hp->dirseg[0].n = 1;
251 +                return;
252 +        }
253 +                                                /* insert into segment list */
254 +        for (j = hp->dirty; j--; ) {
255 +                if (!j || hp->dirseg[j-1].s < i) {
256 +                        hp->dirseg[j].s = i;
257 +                        hp->dirseg[j].n = 1;
258 +                        break;
259 +                }
260 +                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
261 +        }
262 +        do {                            /* check neighbors */
263 +                mindist = nbeams(hp);           /* find closest */
264 +                for (j = hp->dirty; --j; )
265 +                        if (hp->dirseg[j].s -
266 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
267 +                                        < mindist) {
268 +                                minpos = j;
269 +                                mindist = hp->dirseg[j].s -
270 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
271 +                        }
272 +                                                /* good enough? */
273 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
274 +                        break;
275 +                j = minpos - 1;                 /* coalesce neighbors */
276 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
277 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
278 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
279 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
280 +                hp->dirty--;
281 +                while (++j < hp->dirty)         /* close the gap */
282 +                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
283 +        } while (mindist <= MINDIRSEL);
284 + }
285 +
286 +
287   int
288   hdsync(hp, all)                 /* update beams and directory on disk */
289   register HOLO   *hp;
# Line 190 | Line 303 | int    all;
303                          hdsyncbeam(hp, j);
304          if (!hp->dirty)                 /* directory clean? */
305                  return(0);
306 <        errno = 0;
307 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
308 <                error(SYSTEM, "cannot seek on holodeck file");
309 <        n = nbeams(hp)*sizeof(BEAMI);
310 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
311 <                error(SYSTEM, "cannot update holodeck section directory");
312 <        hp->dirty = 0;
306 >        errno = 0;                      /* write dirty segments */
307 >        for (j = 0; j < hp->dirty; j++) {
308 >                if (lseek(hp->fd, biglob(hp)->fo +
309 >                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
310 >                        error(SYSTEM, "cannot seek on holodeck file");
311 >                n = hp->dirseg[j].n * sizeof(BEAMI);
312 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
313 >                        error(SYSTEM, "cannot update section directory");
314 >        }
315 >        hp->dirty = 0;                  /* all clean */
316          return(1);
317   }
318  
# Line 230 | Line 346 | int    all;                    /* include overhead (painful) */
346   }
347  
348  
349 < long
349 > off_t
350   hdfilen(fd)             /* return file length for fd */
351   int     fd;
352   {
353 <        long    fpos, flen;
353 >        off_t   fpos, flen;
354  
355          if (fd < 0)
356                  return(-1);
357          if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
358 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
358 >                if ((fpos = lseek(fd, (off_t)0L, 1)) < 0)
359                          return(-1);
360 <                flen = lseek(fd, 0L, 2);
360 >                flen = lseek(fd, (off_t)0L, 2);
361                  lseek(fd, fpos, 0);
362                  return(flen);
363          }
# Line 249 | Line 365 | int    fd;
365   }
366  
367  
368 < long
368 > off_t
369   hdfiluse(fd, all)       /* compute file usage (in bytes) */
370   int     fd;                     /* open file descriptor to check */
371   int     all;                    /* include overhead and unflushed data */
372   {
373 <        long    total = 0;
373 >        off_t   total = 0;
374          register int    i, j;
375  
376          for (j = 0; hdlist[j] != NULL; j++) {
# Line 284 | Line 400 | int    nr;                     /* number of new rays desired */
400          RAYVAL  *p;
401          int     n;
402  
403 <        if (nr <= 0)
404 <                return(NULL);
405 <        if (i < 1 | i > nbeams(hp))
290 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
403 >        if (nr <= 0) return(NULL);
404 >        CHECK(i < 1 | i > nbeams(hp),
405 >                        CONSISTENCY, "bad beam index given to hdnewrays");
406          if (hp->bl[i] != NULL)
407                  hp->bl[i]->tick = hdclock;      /* preempt swap */
408          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
409                  hdfreecache(PCTFREE, NULL);     /* free some space */
295        errno = 0;
410          if (hp->bl[i] == NULL) {                /* allocate (and load) */
411                  n = hp->bi[i].nrd + nr;
412 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
299 <                        goto memerr;
412 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
413                  blglob(hp)->nrm += n;
414 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
414 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
415 >                        errno = 0;
416                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
417                                  error(SYSTEM, "seek error on holodeck file");
418                          n *= sizeof(RAYVAL);
# Line 307 | Line 421 | int    nr;                     /* number of new rays desired */
421                                  "error reading beam from holodeck file");
422                  }
423          } else {                                /* just grow in memory */
424 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
425 <                                hdbsiz(hp->bl[i]->nrm + nr) );
312 <                if (hp->bl[i] == NULL)
313 <                        goto memerr;
424 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
425 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
426                  blglob(hp)->nrm += nr;
427          }
428 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
429 +                hdfreefrag(hp, i);              /* relinquish old fragment */
430          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
431          hp->bl[i]->nrm += nr;                   /* update in-core structure */
432 <        bzero((char *)p, nr*sizeof(RAYVAL));
433 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
320 <        blglob(hp)->tick = hdclock++;
432 >        bzero((void *)p, nr*sizeof(RAYVAL));
433 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
434          return(p);                              /* point to new rays */
322 memerr:
323        error(SYSTEM, "out of memory in hdnewrays");
435   }
436  
437  
# Line 331 | Line 442 | register int   i;
442   {
443          register int    n;
444  
445 <        if (i < 1 | i > nbeams(hp))
446 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
445 >        CHECK(i < 1 | i > nbeams(hp),
446 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
447          if (hp->bl[i] == NULL) {                /* load from disk */
448                  if (!(n = hp->bi[i].nrd))
449                          return(NULL);
450                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
451                          hdfreecache(PCTFREE, NULL);     /* get free space */
452 <                errno = 0;
342 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
343 <                        error(SYSTEM, "cannot allocate memory for beam");
452 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
453                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
454 +                errno = 0;
455                  if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
456                          error(SYSTEM, "seek error on holodeck file");
457                  n *= sizeof(RAYVAL);
458                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
459                          error(SYSTEM, "error reading beam from holodeck file");
460 +                if (hdfragflags&FF_READ)
461 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
462          }
463 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
352 <        blglob(hp)->tick = hdclock++;
463 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
464          return(hp->bl[i]);
465   }
466  
# Line 383 | Line 494 | int    (*bf)();                /* callback function (optional) */
494          register BEAM   *bp;
495          register int    i;
496                                          /* precheck consistency */
497 +        if (n <= 0) return;
498          for (i = n; i--; )
499 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
499 >                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
500                          error(CONSISTENCY, "bad beam in hdloadbeams");
501                                          /* sort list for optimal access */
502 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
502 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
503          bytesloaded = 0;                /* run through loaded beams */
504          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
505                  bp->tick = hdclock;     /* preempt swap */
# Line 417 | Line 529 | int    (*bf)();                /* callback function (optional) */
529   }
530  
531  
532 < hdfreefrag(fd, bi)                      /* free a file fragment */
533 < int     fd;
534 < register BEAMI  *bi;
532 > int
533 > hdfreefrag(hp, i)                       /* free a file fragment */
534 > HOLO    *hp;
535 > int     i;
536   {
537 +        register BEAMI  *bi = &hp->bi[i];
538          register struct fraglist        *f;
539          register int    j, k;
540  
541 <        if (bi->nrd == 0)
542 <                return;
543 < #ifdef DEBUG
544 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
545 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
546 < #endif
547 <        f = &hdfragl[fd];
541 >        if (bi->nrd <= 0)
542 >                return(0);
543 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
544 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
545 >        f = &hdfragl[hp->fd];
546 >        if (!f->writable)
547 >                return(0);
548          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
549                  for (j = k = 0; k < f->nfrags; j++, k++) {
550                          while (f->fi[k].nrd == 0)
# Line 443 | Line 557 | register BEAMI *bi;
557                  f->nfrags = j;
558          }
559          j = f->nfrags++;                /* allocate a slot in free list */
560 < #if MAXFRAG
561 <        if (j >= MAXFRAG-1)
562 <                f->nfrags--;
560 > #if MAXFRAGB
561 >        if (j >= MAXFRAGB*FRAGBLK) {
562 >                f->nfrags = j--;        /* stop list growth */
563 >                if (bi->nrd <= f->fi[j].nrd)
564 >                        return(0);      /* new one no better than discard */
565 >        }
566   #endif
567 <        if (j % FRAGBLK == 0) {         /* more free list space */
567 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
568 >                register BEAMI  *newp;
569                  if (f->fi == NULL)
570 <                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
570 >                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
571                  else
572 <                        f->fi = (BEAMI *)realloc((char *)f->fi,
572 >                        newp = (BEAMI *)realloc((void *)f->fi,
573                                          (j+FRAGBLK)*sizeof(BEAMI));
574 <                if (f->fi == NULL)
575 <                        error(SYSTEM, "out of memory in hdfreefrag");
574 >                if (newp == NULL) {
575 >                        f->nfrags--;    /* graceful failure */
576 >                        return(0);
577 >                }
578 >                f->fi = newp;
579          }
580          for ( ; ; j--) {                /* insert in descending list */
581                  if (!j || bi->fo < f->fi[j-1].fo) {
# Line 479 | Line 600 | register BEAMI *bi;
600                          }
601                          break;
602                  }
603 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
604 +        bi->nrd = 0;
605 +        bi->fo = 0L;
606 +        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
607 +        return(1);
608   }
609  
610  
611 < long
611 > int
612 > hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
613 > int     fd;
614 > int     *listlen;
615 > register int4   *listsiz;
616 > {
617 >        register struct fraglist        *f;
618 >        register int    i;
619 >
620 >        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
621 >                return(0);              /* listless */
622 >        if (listlen != NULL)
623 >                *listlen = f->nfrags;
624 >        if (listsiz != NULL)
625 >                for (i = f->nfrags, *listsiz = 0; i--; )
626 >                        *listsiz += f->fi[i].nrd;
627 > #if MAXFRAGB
628 >        return(f->nfrags < MAXFRAGB*FRAGBLK);
629 > #else
630 >        return(1);                      /* list never fills up */
631 > #endif
632 > }
633 >
634 >
635 > off_t
636   hdallocfrag(fd, nrays)          /* allocate a file fragment */
637   int     fd;
638   unsigned int4   nrays;
639   {
640          register struct fraglist        *f;
641 <        register int    j, k;
642 <        long    nfo;
641 >        register int    j;
642 >        off_t   nfo;
643  
644          if (nrays == 0)
645                  return(-1L);
646 < #ifdef DEBUG
647 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
498 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
499 < #endif
646 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
647 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
648          f = &hdfragl[fd];
649 <        k = -1;                         /* find closest-sized fragment */
650 <        for (j = f->nfrags; j-- > 0; )
651 <                if (f->fi[j].nrd >= nrays &&
652 <                                (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 */
649 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
650 >                if (f->fi[j].nrd >= nrays)
651 >                        break;
652 >        if (j < 0) {                    /* no fragment -- extend file */
653                  nfo = f->flen;
654                  f->flen += nrays*sizeof(RAYVAL);
655          } else {                        /* else use fragment */
656 <                nfo = f->fi[k].fo;
657 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
658 <                f->fi[k].nrd -= nrays;
656 >                nfo = f->fi[j].fo;
657 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
658 >                f->fi[j].nrd -= nrays;
659          }
660          return(nfo);
661   }
# Line 521 | Line 666 | hdsyncbeam(hp, i)              /* sync beam in memory with beam on
666   register HOLO   *hp;
667   register int    i;
668   {
669 +        int     fragfreed;
670          unsigned int4   nrays;
671          unsigned int    n;
672 <        long    nfo;
672 >        off_t   nfo;
673                                          /* check file status */
674 <        if (hdfragl[hp->fd].writerr)
675 <                return(-1);
676 < #ifdef DEBUG
677 <        if (i < 1 | i > nbeams(hp))
532 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
533 < #endif
674 >        if (hdfragl[hp->fd].writable <= 0)
675 >                return(hdfragl[hp->fd].writable);
676 >        DCHECK(i < 1 | i > nbeams(hp),
677 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
678                                          /* is current fragment OK? */
679          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
680                  return(0);
681 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
682 <                hdfreefrag(hp->fd, &hp->bi[i]);
681 >                                        /* relinquish old fragment? */
682 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
683          if (nrays) {                    /* get and write new fragment */
684                  nfo = hdallocfrag(hp->fd, nrays);
685                  errno = 0;
# Line 543 | Line 687 | register int   i;
687                          error(SYSTEM, "cannot seek on holodeck file");
688                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
689                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
690 <                        hdfragl[hp->fd].writerr++;
690 >                        hdfragl[hp->fd].writable = -1;
691                          hdsync(NULL, 0);        /* sync directories */
692                          error(SYSTEM, "write error in hdsyncbeam");
693                  }
# Line 552 | Line 696 | register int   i;
696                  hp->bi[i].fo = 0L;
697          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
698          hp->bi[i].nrd = nrays;
699 <        markdirty(hp);                  /* section directory now out of date */
699 >        if (!fragfreed)
700 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
701          return(1);
702   }
703  
# Line 570 | Line 715 | register int   i;
715                          nchanged += hdfreebeam(hdlist[i], 0);
716                  return(nchanged);
717          }
718 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
718 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
719                  return(0);
720          if (i == 0) {                   /* clear entire holodeck */
721 +                if (blglob(hp)->nrm == 0)
722 +                        return(0);              /* already clear */
723                  nchanged = 0;
724                  for (i = nbeams(hp); i > 0; i--)
725                          if (hp->bl[i] != NULL)
726                                  nchanged += hdfreebeam(hp, i);
727 +                DCHECK(blglob(hp)->nrm != 0,
728 +                                CONSISTENCY, "bad beam count in hdfreebeam");
729                  return(nchanged);
730          }
731 < #ifdef DEBUG
732 <        if (i < 1 | i > nbeams(hp))
584 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
585 < #endif
731 >        DCHECK(i < 1 | i > nbeams(hp),
732 >                        CONSISTENCY, "bad beam index to hdfreebeam");
733          if (hp->bl[i] == NULL)
734                  return(0);
735                                          /* check for additions */
# Line 590 | Line 737 | register int   i;
737          if (nchanged)
738                  hdsyncbeam(hp, i);              /* write new fragment */
739          blglob(hp)->nrm -= hp->bl[i]->nrm;
740 <        free((char *)hp->bl[i]);                /* free memory */
740 >        free((void *)hp->bl[i]);                /* free memory */
741          hp->bl[i] = NULL;
742          return(nchanged);
743   }
# Line 601 | Line 748 | hdkillbeam(hp, i)              /* delete beam from holodeck */
748   register HOLO   *hp;
749   register int    i;
750   {
604        static BEAM     emptybeam;
751          int     nchanged;
752  
753          if (hp == NULL) {               /* clobber all holodecks */
# Line 611 | Line 757 | register int   i;
757                  return(nchanged);
758          }
759          if (i == 0) {                   /* clobber entire holodeck */
760 +                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
761 +                        return(0);              /* already empty */
762                  nchanged = 0;
763 +                nchanged = 0;
764                  for (i = nbeams(hp); i > 0; i--)
765                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
766                                  nchanged += hdkillbeam(hp, i);
767 < #ifdef DEBUG
768 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
620 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
621 < #endif
767 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
768 >                                CONSISTENCY, "bad beam count in hdkillbeam");
769                  return(nchanged);
770          }
771 < #ifdef DEBUG
772 <        if (i < 1 | i > nbeams(hp))
773 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
774 < #endif
771 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
772 >                        "bad beam index to hdkillbeam");
773 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
774 >                        "hdkillbeam called on read-only holodeck");
775          if (hp->bl[i] != NULL) {        /* free memory */
776                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
777 <                free((char *)hp->bl[i]);
777 >                free((void *)hp->bl[i]);
778 >                hp->bl[i] = NULL;
779          } else
780                  nchanged = hp->bi[i].nrd;
781 <        if (hp->bi[i].nrd) {            /* free file fragment */
782 <                hp->bl[i] = &emptybeam;
783 <                hdsyncbeam(hp, i);
781 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
782 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
783 >                hp->bi[i].nrd = 0;
784 >                hp->bi[i].fo = 0L;
785 >                hdmarkdirty(hp, i);
786          }
637        hp->bl[i] = NULL;
787          return(nchanged);
788   }
789  
# Line 680 | Line 829 | register HOLO  *honly;                 /* NULL means check all */
829          int     freetarget;
830          int     n;
831          register int    i;
832 + #ifdef DEBUG
833 +        unsigned        membefore;
834 +
835 +        membefore = hdmemuse(0);
836 + #endif
837                                                  /* compute free target */
838          freetarget = (honly != NULL) ? blglob(honly)->nrm :
839                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 700 | Line 854 | register HOLO  *honly;                 /* NULL means check all */
854                          break;
855          }
856          hdsync(honly, 0);       /* synchronize directories as necessary */
857 + #ifdef DEBUG
858 +        sprintf(errmsg,
859 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
860 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
861 +        wputs(errmsg);
862 + #endif
863          return(-freetarget);    /* return how far past goal we went */
864   }
865  
# Line 712 | Line 872 | register HOLO  *hp;            /* NULL means clean up all */
872          if (hp == NULL) {               /* NULL means clean up everything */
873                  while (hdlist[0] != NULL)
874                          hddone(hdlist[0]);
875 <                free((char *)hdfragl);
875 >                free((void *)hdfragl);
876                  hdfragl = NULL; nhdfragls = 0;
877                  return;
878          }
# Line 727 | Line 887 | register HOLO  *hp;            /* NULL means clean up all */
887                                  i++;
888                          break;
889                  }
890 <        free((char *)hp->bl);           /* free beam list */
891 <        free((char *)hp);               /* free holodeck struct */
890 >        free((void *)hp->bl);           /* free beam list */
891 >        free((void *)hp);               /* free holodeck struct */
892   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines