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.2 by gregl, Fri Oct 31 15:48:32 1997 UTC vs.
Revision 3.24 by gwlarson, Tue Sep 15 14:26:23 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ SGI";
19   #define FREEBEAMS       1024    /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         12      /* maximum fraction to free (%) */
22 > #define PCTFREE         15      /* maximum fraction to free (%) */
23   #endif
24 #ifndef FFDMAX
25 #define FFDMAX          64      /* max. file descriptor for tracking */
26 #endif
24   #ifndef MAXFRAG
25 < #define MAXFRAG         2048    /* maximum fragments tracked in each file */
25 > #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
26   #endif
27  
28 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
28 > #ifndef BSD
29 > #define write   writebuf        /* safe i/o routines */
30 > #define read    readbuf
31 > #endif
32 >
33 > #define FRAGBLK         256     /* number of fragments to allocate at a time */
34 >
35 > unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
36   unsigned long   hdclock;        /* clock value */
37  
38   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
39  
40 < static struct fragment {
40 > static struct fraglist {
41          short   nlinks;         /* number of holodeck sections using us */
42 <        short   nfrags;         /* number of known fragments */
43 <        BEAMI   fi[MAXFRAG];    /* fragments, descending file position */
42 >        short   writerr;        /* write error encountered */
43 >        int     nfrags;         /* number of known fragments */
44 >        BEAMI   *fi;            /* fragments, descending file position */
45          long    flen;           /* last known file length */
46 < } *hdfrag[FFDMAX];      /* fragment lists, indexed by file descriptor */
46 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
47  
48 + static int      nhdfragls;      /* size of hdfragl array */
49  
50 +
51   hdattach(fd)            /* start tracking file fragments for some section */
52   register int    fd;
53   {
54 <        if (fd >= FFDMAX)
55 <                return;                         /* descriptor out of range */
56 <        if (hdfrag[fd] == NULL) {
57 <                if ((hdfrag[fd] = (struct fragment *)
58 <                                malloc(sizeof(struct fragment))) == NULL)
59 <                        return;                 /* should flag error? */
60 <                hdfrag[fd]->nlinks = 0;
61 <                hdfrag[fd]->nfrags = 0;
54 >        if (fd >= nhdfragls) {
55 >                if (nhdfragls)
56 >                        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");
63 >                bzero((char *)(hdfragl+nhdfragls),
64 >                                (fd+1-nhdfragls)*sizeof(struct fraglist));
65 >                nhdfragls = fd+1;
66          }
67 <        hdfrag[fd]->nlinks++;
68 <        hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */
67 >        hdfragl[fd].nlinks++;
68 >        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
69   }
70  
71  
# Line 64 | Line 75 | register int   fd;
75   hdrelease(fd)           /* stop tracking file fragments for some section */
76   register int    fd;
77   {
78 <        if (fd >= FFDMAX || hdfrag[fd] == NULL)
78 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
79                  return;
80 <        if (!--hdfrag[fd]->nlinks) {
81 <                free((char *)hdfrag[fd]);
82 <                hdfrag[fd] = NULL;
80 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
81 >                free((char *)hdfragl[fd].fi);
82 >                hdfragl[fd].fi = NULL;
83 >                hdfragl[fd].nfrags = 0;
84          }
85   }
86  
87  
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 +
103   HOLO *
104   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
105   int     fd;                     /* corresponding file descriptor */
106   HDGRID  *hproto;                /* holodeck section grid */
107   {
108 +        long    rtrunc;
109          long    fpos;
110          register HOLO   *hp;
111          register int    n;
# Line 97 | Line 125 | HDGRID *hproto;                /* holodeck section grid */
125                  n = nbeams(hp)*sizeof(BEAMI);
126                  if (read(fd, (char *)(hp->bi+1), n) != n)
127                          error(SYSTEM, "failure loading holodeck directory");
128 +                                                /* check that it's clean */
129 +                if (hp->bi[nbeams(hp)].fo < 0)
130 +                        error(WARNING, "dirty holodeck section");
131          } else {                        /* assume we're creating it */
132                  if ((hp = hdalloc(hproto)) == NULL)
133                          goto memerr;
# Line 110 | Line 141 | HDGRID *hproto;                /* holodeck section grid */
141          hp->fd = fd;    
142          hp->dirty = 0;
143          biglob(hp)->fo = fpos + sizeof(HDGRID);
144 <        biglob(hp)->nrd = 0;            /* count rays on disk */
145 <        for (n = nbeams(hp); n > 0; n--)
146 <                biglob(hp)->nrd += hp->bi[n].nrd;
144 >                                        /* start tracking fragments */
145 >        hdattach(fd);
146 >                                        /* check rays on disk */
147 >        fpos = hdfilen(fd);
148 >        biglob(hp)->nrd = rtrunc = 0;
149 >        for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
150 >                if (hp->bi[n].nrd)
151 >                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos) {
152 >                                rtrunc += hp->bi[n].nrd;
153 >                                hp->bi[n].nrd = 0;
154 >                        } else
155 >                                biglob(hp)->nrd += hp->bi[n].nrd;
156 >        if (rtrunc) {
157 >                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
158 >                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
159 >                error(WARNING, errmsg);
160 >        }
161                                          /* add to holodeck list */
162          for (n = 0; n < HDMAX; n++)
163                  if (hdlist[n] == NULL) {
164                          hdlist[n] = hp;
165                          break;
166                  }
122                                        /* start tracking fragments (last) */
123        hdattach(fd);
167                                          /* all done */
168          return(hp);
169   memerr:
# Line 129 | Line 172 | memerr:
172  
173  
174   int
175 < hdsync(hp)                      /* update directory on disk if necessary */
175 > hdsync(hp, all)                 /* update beams and directory on disk */
176   register HOLO   *hp;
177 + int     all;
178   {
179          register int    j, n;
180  
181 <        if (hp == NULL) {               /* do all */
181 >        if (hp == NULL) {               /* do all holodecks */
182                  n = 0;
183                  for (j = 0; hdlist[j] != NULL; j++)
184 <                        n += hdsync(hdlist[j]);
184 >                        n += hdsync(hdlist[j], all);
185                  return(n);
186          }
187 <        if (!hp->dirty)                 /* check first */
187 >                                        /* sync the beams */
188 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
189 >                if (hp->bl[j] != NULL)
190 >                        hdsyncbeam(hp, j);
191 >        if (!hp->dirty)                 /* directory clean? */
192                  return(0);
193 +        errno = 0;
194          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
195                  error(SYSTEM, "cannot seek on holodeck file");
196          n = nbeams(hp)*sizeof(BEAMI);
# Line 152 | Line 201 | register HOLO  *hp;
201   }
202  
203  
204 < long
204 > unsigned
205   hdmemuse(all)           /* return memory usage (in bytes) */
206   int     all;                    /* include overhead (painful) */
207   {
# Line 171 | Line 220 | int    all;                    /* include overhead (painful) */
220                  }
221          }
222          if (all)
223 <                for (j = 0; j < FFDMAX; j++)
224 <                        if (hdfrag[j] != NULL)
225 <                                total += sizeof(struct fragment);
223 >                for (j = 0; j < nhdfragls; j++) {
224 >                        total += sizeof(struct fraglist);
225 >                        if (hdfragl[j].nfrags)
226 >                                total += FRAGBLK*sizeof(BEAMI) *
227 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
228 >                }
229          return(total);
230   }
231  
232  
233   long
234 + hdfilen(fd)             /* return file length for fd */
235 + int     fd;
236 + {
237 +        long    fpos, flen;
238 +
239 +        if (fd < 0)
240 +                return(-1);
241 +        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
242 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
243 +                        return(-1);
244 +                flen = lseek(fd, 0L, 2);
245 +                lseek(fd, fpos, 0);
246 +                return(flen);
247 +        }
248 +        return(hdfragl[fd].flen);
249 + }
250 +
251 +
252 + long
253   hdfiluse(fd, all)       /* compute file usage (in bytes) */
254   int     fd;                     /* open file descriptor to check */
255   int     all;                    /* include overhead and unflushed data */
# Line 221 | Line 292 | int    nr;                     /* number of new rays desired */
292                  hp->bl[i]->tick = hdclock;      /* preempt swap */
293          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
294                  hdfreecache(PCTFREE, NULL);     /* free some space */
295 +        errno = 0;
296          if (hp->bl[i] == NULL) {                /* allocate (and load) */
297                  n = hp->bi[i].nrd + nr;
298                  if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
# Line 244 | Line 316 | int    nr;                     /* number of new rays desired */
316          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
317          hp->bl[i]->nrm += nr;                   /* update in-core structure */
318          bzero((char *)p, nr*sizeof(RAYVAL));
319 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
320 <        blglob(hp)->tick = hdclock;
319 >        hp->bl[i]->tick = hdclock;              /* update LRU clock */
320 >        blglob(hp)->tick = hdclock++;
321          return(p);                              /* point to new rays */
322   memerr:
323          error(SYSTEM, "out of memory in hdnewrays");
# Line 276 | Line 348 | register int   i;
348                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
349                          error(SYSTEM, "error reading beam from holodeck file");
350          }
351 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
352 <        blglob(hp)->tick = hdclock;
351 >        hp->bl[i]->tick = hdclock;      /* update LRU clock */
352 >        blglob(hp)->tick = hdclock++;
353          return(hp->bl[i]);
354   }
355  
356  
357   int
358 < hdgetbi(hp, i)                  /* allocate a file fragment */
359 < register HOLO   *hp;
288 < register int    i;
358 > hdfilord(hb1, hb2)      /* order beams for quick loading */
359 > register HDBEAMI        *hb1, *hb2;
360   {
361 <        int     nrays = hp->bl[i]->nrm;
361 >        register long   c;
362 >                                /* residents go first */
363 >        if (hb2->h->bl[hb2->b] != NULL)
364 >                return(hb1->h->bl[hb1->b] == NULL);
365 >        if (hb1->h->bl[hb1->b] != NULL)
366 >                return(-1);
367 >                                /* otherwise sort by file descriptor */
368 >        if ((c = hb1->h->fd - hb2->h->fd))
369 >                return(c);
370 >                                /* then by position in file */
371 >        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
372 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
373 > }
374  
292        if (hp->bi[i].nrd == nrays)     /* current one will do? */
293                return(0);
375  
376 <        if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */
377 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
376 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
377 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
378 > int     n;                      /* list length */
379 > int     (*bf)();                /* callback function (optional) */
380 > {
381 >        unsigned        origcachesize, memuse;
382 >        int     bytesloaded, needbytes, bytes2free;
383 >        register BEAM   *bp;
384 >        register int    i;
385 >                                        /* precheck consistency */
386 >        for (i = n; i--; )
387 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
388 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
389 >                                        /* sort list for optimal access */
390 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
391 >        bytesloaded = 0;                /* run through loaded beams */
392 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
393 >                bp->tick = hdclock;     /* preempt swap */
394 >                bytesloaded += bp->nrm;
395 >                if (bf != NULL)
396 >                        (*bf)(bp, hb);
397 >        }
398 >        bytesloaded *= sizeof(RAYVAL);
399 >        if ((origcachesize = hdcachesize) > 0) {
400 >                needbytes = 0;          /* figure out memory needs */
401 >                for (i = n; i--; )
402 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
403 >                needbytes *= sizeof(RAYVAL);
404 >                do {                            /* free enough memory */
405 >                        memuse = hdmemuse(0);
406 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
407 >                        if (bytes2free > (int)(memuse - bytesloaded))
408 >                                bytes2free = memuse - bytesloaded;
409 >                } while (bytes2free > 0 &&
410 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
411 >                hdcachesize = 0;                /* load beams w/o swap */
412 >        }
413 >        for (i = 0; i < n; i++)
414 >                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
415 >                        (*bf)(bp, hb+i);
416 >        hdcachesize = origcachesize;    /* resume dynamic swapping */
417 > }
418  
298        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
299                        hdfrag[hp->fd]->flen)           /* EOF special case */
300                hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
419  
420 <        else {                                          /* general case */
421 <                register struct fragment        *f = hdfrag[hp->fd];
422 <                register int    j, k;
423 <                                        /* relinquish old fragment */
424 <                if (hp->bi[i].nrd) {
425 <                        if ((j = ++f->nfrags) >= MAXFRAG)
426 <                                f->nfrags--;
427 <                        for ( ; ; ) {   /* stick it in our descending list */
428 <                                if (!--j || hp->bi[i].fo < f->fi[j-1].fo) {
429 <                                        f->fi[j].fo = hp->bi[i].fo;
430 <                                        f->fi[j].nrd = hp->bi[i].nrd;
431 <                                        break;
432 <                                }
433 <                                copystruct(f->fi+j, f->fi+j-1);
434 <                        }
420 > hdfreefrag(fd, bi)                      /* free a file fragment */
421 > int     fd;
422 > register BEAMI  *bi;
423 > {
424 >        register struct fraglist        *f;
425 >        register int    j, k;
426 >
427 >        if (bi->nrd == 0)
428 >                return;
429 > #ifdef DEBUG
430 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
431 >                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
432 > #endif
433 >        f = &hdfragl[fd];
434 >        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
435 >                for (j = k = 0; k < f->nfrags; j++, k++) {
436 >                        while (f->fi[k].nrd == 0)
437 >                                if (++k >= f->nfrags)
438 >                                        goto endloop;
439 >                        if (k > j)
440 >                                copystruct(f->fi+j, f->fi+k);
441 >                }
442 >        endloop:
443 >                f->nfrags = j;
444 >        }
445 >        j = f->nfrags++;                /* allocate a slot in free list */
446 > #if MAXFRAG
447 >        if (j >= MAXFRAG-1)
448 >                f->nfrags--;
449 > #endif
450 >        if (j % FRAGBLK == 0) {         /* more free list space */
451 >                if (f->fi == NULL)
452 >                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
453 >                else
454 >                        f->fi = (BEAMI *)realloc((char *)f->fi,
455 >                                        (j+FRAGBLK)*sizeof(BEAMI));
456 >                if (f->fi == NULL)
457 >                        error(SYSTEM, "out of memory in hdfreefrag");
458 >        }
459 >        for ( ; ; j--) {                /* insert in descending list */
460 >                if (!j || bi->fo < f->fi[j-1].fo) {
461 >                        f->fi[j].fo = bi->fo;
462 >                        f->fi[j].nrd = bi->nrd;
463 >                        break;
464 >                }
465 >                copystruct(f->fi+j, f->fi+(j-1));
466 >        }
467                                          /* coalesce adjacent fragments */
468 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
469 <                                if (j != k)
470 <                                        copystruct(f->fi+j, f->fi+k);
471 <                                while (k+1 < f->nfrags &&
472 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
473 <                                                == f->fi[j].fo)
474 <                                        f->fi[j].fo -=
475 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
468 >                                                /* successors never empty */
469 >        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
470 >                f->fi[j].nrd += f->fi[j-1].nrd;
471 >                f->fi[j-1].nrd = 0;
472 >        }
473 >        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
474 >                if (f->fi[k].nrd) {
475 >                        if (f->fi[j].fo == f->fi[k].fo +
476 >                                        f->fi[k].nrd*sizeof(RAYVAL)) {
477 >                                f->fi[k].nrd += f->fi[j].nrd;
478 >                                f->fi[j].nrd = 0;
479                          }
480 <                        f->nfrags = j;
480 >                        break;
481                  }
482 <                k = -1;                 /* find closest-sized fragment */
483 <                for (j = f->nfrags; j-- > 0; )
484 <                        if (f->fi[j].nrd >= nrays &&
485 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
486 <                                        && f->fi[k=j].nrd == nrays)
482 > }
483 >
484 >
485 > long
486 > hdallocfrag(fd, nrays)          /* allocate a file fragment */
487 > int     fd;
488 > unsigned int4   nrays;
489 > {
490 >        register struct fraglist        *f;
491 >        register int    j, k;
492 >        long    nfo;
493 >
494 >        if (nrays == 0)
495 >                return(-1L);
496 > #ifdef DEBUG
497 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
498 >                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
499 > #endif
500 >        f = &hdfragl[fd];
501 >        k = -1;                         /* find closest-sized fragment */
502 >        for (j = f->nfrags; j-- > 0; )
503 >                if (f->fi[j].nrd >= nrays &&
504 >                                (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 */
508 <                        hp->bi[i].fo = f->flen;
509 <                        f->flen += nrays*sizeof(RAYVAL);
510 <                } else {                /* else use fragment */
511 <                        hp->bi[i].fo = f->fi[k].fo;
512 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
513 <                                f->nfrags--;
342 <                                for (j = k; j < f->nfrags; j++)
343 <                                        copystruct(f->fi+j, f->fi+j+1);
344 <                        } else {                        /* else shrink it */
345 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
346 <                                f->fi[k].nrd -= nrays;
347 <                        }
348 <                }
507 >        if (k < 0) {                    /* no fragment -- extend file */
508 >                nfo = f->flen;
509 >                f->flen += nrays*sizeof(RAYVAL);
510 >        } else {                        /* else use fragment */
511 >                nfo = f->fi[k].fo;
512 >                f->fi[k].fo += nrays*sizeof(RAYVAL);
513 >                f->fi[k].nrd -= nrays;
514          }
515 +        return(nfo);
516 + }
517 +
518 +
519 + int
520 + hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
521 + register HOLO   *hp;
522 + register int    i;
523 + {
524 +        unsigned int4   nrays;
525 +        unsigned int    n;
526 +        long    nfo;
527 +                                        /* check file status */
528 +        if (hdfragl[hp->fd].writerr)
529 +                return(-1);
530 + #ifdef DEBUG
531 +        if (i < 1 | i > nbeams(hp))
532 +                error(CONSISTENCY, "bad beam index in hdsyncbeam");
533 + #endif
534 +                                        /* is current fragment OK? */
535 +        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
536 +                return(0);
537 +        if (hp->bi[i].nrd)              /* relinquish old fragment */
538 +                hdfreefrag(hp->fd, &hp->bi[i]);
539 +        if (nrays) {                    /* get and write new fragment */
540 +                nfo = hdallocfrag(hp->fd, nrays);
541 +                errno = 0;
542 +                if (lseek(hp->fd, nfo, 0) < 0)
543 +                        error(SYSTEM, "cannot seek on holodeck file");
544 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
545 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
546 +                        hdfragl[hp->fd].writerr++;
547 +                        hdsync(NULL, 0);        /* sync directories */
548 +                        error(SYSTEM, "write error in hdsyncbeam");
549 +                }
550 +                hp->bi[i].fo = nfo;
551 +        } else
552 +                hp->bi[i].fo = 0L;
553          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
554          hp->bi[i].nrd = nrays;
555 <        hp->dirty++;            /* section directory now out of date */
555 >        markdirty(hp);                  /* section directory now out of date */
556          return(1);
557   }
558  
# Line 359 | Line 562 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
562   register HOLO   *hp;
563   register int    i;
564   {
565 <        int     nchanged, n;
565 >        int     nchanged;
566  
567          if (hp == NULL) {               /* clear all holodecks */
568                  nchanged = 0;
# Line 367 | Line 570 | register int   i;
570                          nchanged += hdfreebeam(hdlist[i], 0);
571                  return(nchanged);
572          }
573 +        if (hdfragl[hp->fd].writerr)    /* check for file error */
574 +                return(0);
575          if (i == 0) {                   /* clear entire holodeck */
576                  nchanged = 0;
577 <                for (i = 1; i <= nbeams(hp); i++)
578 <                        nchanged += hdfreebeam(hp, i);
577 >                for (i = nbeams(hp); i > 0; i--)
578 >                        if (hp->bl[i] != NULL)
579 >                                nchanged += hdfreebeam(hp, i);
580                  return(nchanged);
581          }
582 + #ifdef DEBUG
583          if (i < 1 | i > nbeams(hp))
584                  error(CONSISTENCY, "bad beam index to hdfreebeam");
585 + #endif
586          if (hp->bl[i] == NULL)
587                  return(0);
588                                          /* check for additions */
589          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
590 <        if (nchanged) {
591 <                hdgetbi(hp, i);                 /* allocate a file position */
384 <                errno = 0;
385 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
386 <                        error(SYSTEM, "cannot seek on holodeck file");
387 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
388 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
389 <                        error(SYSTEM, "write error in hdfreebeam");
390 <        }
590 >        if (nchanged)
591 >                hdsyncbeam(hp, i);              /* write new fragment */
592          blglob(hp)->nrm -= hp->bl[i]->nrm;
593          free((char *)hp->bl[i]);                /* free memory */
594          hp->bl[i] = NULL;
# Line 395 | Line 596 | register int   i;
596   }
597  
598  
599 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
600 < register HOLO   *ha[];                  /* section list (NULL terminated) */
601 < register int    ba[];                   /* beam index to go with section */
602 < int     n;                              /* length of arrays minus 1 */
599 > int
600 > hdkillbeam(hp, i)               /* delete beam from holodeck */
601 > register HOLO   *hp;
602 > register int    i;
603 > {
604 >        static BEAM     emptybeam;
605 >        int     nchanged;
606 >
607 >        if (hp == NULL) {               /* clobber all holodecks */
608 >                nchanged = 0;
609 >                for (i = 0; hdlist[i] != NULL; i++)
610 >                        nchanged += hdkillbeam(hdlist[i], 0);
611 >                return(nchanged);
612 >        }
613 >        if (i == 0) {                   /* clobber entire holodeck */
614 >                nchanged = 0;
615 >                for (i = nbeams(hp); i > 0; i--)
616 >                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
617 >                                nchanged += hdkillbeam(hp, i);
618 > #ifdef DEBUG
619 >                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
620 >                        error(CONSISTENCY, "bad beam count in hdkillbeam");
621 > #endif
622 >                return(nchanged);
623 >        }
624 > #ifdef DEBUG
625 >        if (i < 1 | i > nbeams(hp))
626 >                error(CONSISTENCY, "bad beam index to hdkillbeam");
627 > #endif
628 >        if (hp->bl[i] != NULL) {        /* free memory */
629 >                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
630 >                free((char *)hp->bl[i]);
631 >        } else
632 >                nchanged = hp->bi[i].nrd;
633 >        if (hp->bi[i].nrd) {            /* free file fragment */
634 >                hp->bl[i] = &emptybeam;
635 >                hdsyncbeam(hp, i);
636 >        }
637 >        hp->bl[i] = NULL;
638 >        return(nchanged);
639 > }
640 >
641 >
642 > int
643 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
644 > register HDBEAMI        *hb;            /* beam list */
645 > int     nents;                          /* current list length */
646 > int     n;                              /* maximum list length */
647   register HOLO   *hp;                    /* section we're adding from */
648   {
649          register int    i, j;
405        int     nents;
406                                        /* find last entry in LRU list */
407        for (j = 0; ha[j] != NULL; j++)
408                ;
409        nents = j;
650                                          /* insert each beam from hp */
651 <        for (i = nbeams(hp); i > 0; i-- ) {
651 >        for (i = 1; i <= nbeams(hp); i++) {
652                  if (hp->bl[i] == NULL)          /* check if loaded */
653                          continue;
654 <                if ((j = ++nents) > n)          /* grow list if we can */
654 > #if 0
655 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
656 >                        continue;
657 > #endif
658 >                if ((j = ++nents) >= n)         /* grow list if we can */
659                          nents--;
660                  for ( ; ; ) {                   /* bubble into place */
661                          if (!--j || hp->bl[i]->tick >=
662 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
663 <                                ha[j] = hp;
664 <                                ba[j] = i;
662 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
663 >                                hb[j].h = hp;
664 >                                hb[j].b = i;
665                                  break;
666                          }
667 <                        ha[j] = ha[j-1];
424 <                        ba[j] = ba[j-1];
667 >                        copystruct(hb+j, hb+(j-1));
668                  }
669          }
670 <        ha[nents] = NULL;               /* all done */
428 <        ba[nents] = 0;
670 >        return(nents);                  /* return new list length */
671   }
672  
673  
674 + int
675   hdfreecache(pct, honly)         /* free up cache space, writing changes */
676   int     pct;                            /* maximum percentage to free */
677   register HOLO   *honly;                 /* NULL means check all */
678   {
679 <        HOLO    *hp[FREEBEAMS+1];
437 <        int     bn[FREEBEAMS+1];
679 >        HDBEAMI hb[FREEBEAMS];
680          int     freetarget;
681 +        int     n;
682          register int    i;
683                                                  /* compute free target */
684          freetarget = (honly != NULL) ? blglob(honly)->nrm :
685                          hdmemuse(0)/sizeof(RAYVAL) ;
686          freetarget = freetarget*pct/100;
687 +        if (freetarget <= 0)
688 +                return(0);
689                                                  /* find least recently used */
690 <        hp[0] = NULL;
446 <        bn[0] = 0;
690 >        n = 0;
691          if (honly != NULL)
692 <                hdlrulist(hp, bn, FREEBEAMS, honly);
692 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
693          else
694                  for (i = 0; hdlist[i] != NULL; i++)
695 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
695 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
696                                                  /* free LRU beams */
697 <        for (i = 0; hp[i] != NULL; i++) {
698 <                hdfreebeam(hp[i], bn[i]);
699 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
697 >        for (i = 0; i < n; i++) {
698 >                hdfreebeam(hb[i].h, hb[i].b);
699 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
700                          break;
701          }
702 <        hdsync(honly);          /* synchronize directories as necessary */
702 >        hdsync(honly, 0);       /* synchronize directories as necessary */
703 >        return(-freetarget);    /* return how far past goal we went */
704   }
705  
706  
# Line 467 | Line 712 | register HOLO  *hp;            /* NULL means clean up all */
712          if (hp == NULL) {               /* NULL means clean up everything */
713                  while (hdlist[0] != NULL)
714                          hddone(hdlist[0]);
715 +                free((char *)hdfragl);
716 +                hdfragl = NULL; nhdfragls = 0;
717                  return;
718          }
719                                          /* flush all data and free memory */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines