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.15 by gregl, Mon Dec 29 15:17:45 1997 UTC

# 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       512     /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         12      /* maximum fraction to free (%) */
22 > #define PCTFREE         20      /* 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         131000  /* 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         64      /* 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 {
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 > } *hdfrag;              /* fragment lists, indexed by file descriptor */
47  
48 + static int      nhdfrags;       /* size of hdfrag 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 >= nhdfrags) {
55 >                if (nhdfrags)
56 >                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
57 >                                        (fd+1)*sizeof(struct fragment));
58 >                else
59 >                        hdfrag = (struct fragment *)malloc(
60 >                                        (fd+1)*sizeof(struct fragment));
61 >                if (hdfrag == NULL)
62 >                        error(SYSTEM, "out of memory in hdattach");
63 >                bzero((char *)(hdfrag+nhdfrags),
64 >                                (fd+1-nhdfrags)*sizeof(struct fragment));
65 >                nhdfrags = fd+1;
66          }
67 <        hdfrag[fd]->nlinks++;
68 <        hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */
67 >        hdfrag[fd].nlinks++;
68 >        hdfrag[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 >= nhdfrags || !hdfrag[fd].nlinks)
79                  return;
80 <        if (!--hdfrag[fd]->nlinks) {
81 <                free((char *)hdfrag[fd]);
82 <                hdfrag[fd] = NULL;
80 >        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
81 >                free((char *)hdfrag[fd].fi);
82 >                hdfrag[fd].fi = NULL;
83 >                hdfrag[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 */
# Line 97 | Line 124 | HDGRID *hproto;                /* holodeck section grid */
124                  n = nbeams(hp)*sizeof(BEAMI);
125                  if (read(fd, (char *)(hp->bi+1), n) != n)
126                          error(SYSTEM, "failure loading holodeck directory");
127 +                                                /* check that it's clean */
128 +                if (hp->bi[nbeams(hp)].fo < 0)
129 +                        error(USER, "dirty holodeck section");
130          } else {                        /* assume we're creating it */
131                  if ((hp = hdalloc(hproto)) == NULL)
132                          goto memerr;
# Line 110 | Line 140 | HDGRID *hproto;                /* holodeck section grid */
140          hp->fd = fd;    
141          hp->dirty = 0;
142          biglob(hp)->fo = fpos + sizeof(HDGRID);
143 <        biglob(hp)->nrd = 0;            /* count rays on disk */
144 <        for (n = nbeams(hp); n > 0; n--)
145 <                biglob(hp)->nrd += hp->bi[n].nrd;
143 >                                        /* start tracking fragments */
144 >        hdattach(fd);
145 >                                        /* check rays on disk */
146 >        fpos = hdfilen(fd);
147 >        biglob(hp)->nrd = 0;
148 >        for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
149 >                if (hp->bi[n].nrd)
150 >                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
151 >                                hp->bi[n].nrd = 0;      /* off end */
152 >                        else
153 >                                biglob(hp)->nrd += hp->bi[n].nrd;
154                                          /* add to holodeck list */
155          for (n = 0; n < HDMAX; n++)
156                  if (hdlist[n] == NULL) {
157                          hdlist[n] = hp;
158                          break;
159                  }
122                                        /* start tracking fragments (last) */
123        hdattach(fd);
160                                          /* all done */
161          return(hp);
162   memerr:
# Line 129 | Line 165 | memerr:
165  
166  
167   int
168 < hdsync(hp)                      /* update directory on disk if necessary */
168 > hdsync(hp, all)                 /* update beams and directory on disk */
169   register HOLO   *hp;
170 + int     all;
171   {
172          register int    j, n;
173  
174 <        if (hp == NULL) {               /* do all */
174 >        if (hp == NULL) {               /* do all holodecks */
175                  n = 0;
176                  for (j = 0; hdlist[j] != NULL; j++)
177 <                        n += hdsync(hdlist[j]);
177 >                        n += hdsync(hdlist[j], all);
178                  return(n);
179          }
180 <        if (!hp->dirty)                 /* check first */
180 >                                        /* sync the beams */
181 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
182 >                if (hp->bl[j] != NULL)
183 >                        hdsyncbeam(hp, j);
184 >        if (!hp->dirty)                 /* directory clean? */
185                  return(0);
186 +        errno = 0;
187          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
188                  error(SYSTEM, "cannot seek on holodeck file");
189          n = nbeams(hp)*sizeof(BEAMI);
# Line 152 | Line 194 | register HOLO  *hp;
194   }
195  
196  
197 < long
197 > unsigned
198   hdmemuse(all)           /* return memory usage (in bytes) */
199   int     all;                    /* include overhead (painful) */
200   {
# Line 171 | Line 213 | int    all;                    /* include overhead (painful) */
213                  }
214          }
215          if (all)
216 <                for (j = 0; j < FFDMAX; j++)
217 <                        if (hdfrag[j] != NULL)
218 <                                total += sizeof(struct fragment);
216 >                for (j = 0; j < nhdfrags; j++) {
217 >                        total += sizeof(struct fragment);
218 >                        if (hdfrag[j].nfrags)
219 >                                total += FRAGBLK*sizeof(BEAMI) *
220 >                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
221 >                }
222          return(total);
223   }
224  
225  
226   long
227 + hdfilen(fd)             /* return file length for fd */
228 + int     fd;
229 + {
230 +        long    fpos, flen;
231 +
232 +        if (fd < 0)
233 +                return(-1);
234 +        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
235 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
236 +                        return(-1);
237 +                flen = lseek(fd, 0L, 2);
238 +                lseek(fd, fpos, 0);
239 +                return(flen);
240 +        }
241 +        return(hdfrag[fd].flen);
242 + }
243 +
244 +
245 + long
246   hdfiluse(fd, all)       /* compute file usage (in bytes) */
247   int     fd;                     /* open file descriptor to check */
248   int     all;                    /* include overhead and unflushed data */
# Line 221 | Line 285 | int    nr;                     /* number of new rays desired */
285                  hp->bl[i]->tick = hdclock;      /* preempt swap */
286          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
287                  hdfreecache(PCTFREE, NULL);     /* free some space */
288 +        errno = 0;
289          if (hp->bl[i] == NULL) {                /* allocate (and load) */
290                  n = hp->bi[i].nrd + nr;
291                  if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
# Line 244 | Line 309 | int    nr;                     /* number of new rays desired */
309          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
310          hp->bl[i]->nrm += nr;                   /* update in-core structure */
311          bzero((char *)p, nr*sizeof(RAYVAL));
312 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
313 <        blglob(hp)->tick = hdclock;
312 >        hp->bl[i]->tick = hdclock;              /* update LRU clock */
313 >        blglob(hp)->tick = hdclock++;
314          return(p);                              /* point to new rays */
315   memerr:
316          error(SYSTEM, "out of memory in hdnewrays");
# Line 276 | Line 341 | register int   i;
341                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
342                          error(SYSTEM, "error reading beam from holodeck file");
343          }
344 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
345 <        blglob(hp)->tick = hdclock;
344 >        hp->bl[i]->tick = hdclock;      /* update LRU clock */
345 >        blglob(hp)->tick = hdclock++;
346          return(hp->bl[i]);
347   }
348  
349  
350   int
351 < hdgetbi(hp, i)                  /* allocate a file fragment */
351 > hdfilord(hb1, hb2)      /* order beams for optimal loading */
352 > register HDBEAMI        *hb1, *hb2;
353 > {
354 >        register int    c;
355 >                                /* sort by file descriptor first */
356 >        if ((c = hb1->h->fd - hb2->h->fd))
357 >                return(c);
358 >                                /* then by position in file */
359 >        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
360 > }
361 >
362 >
363 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
364 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
365 > int     n;                      /* list length */
366 > int     (*bf)();                /* callback function (optional) */
367 > {
368 >        unsigned        origcachesize, memuse;
369 >        register BEAM   *bp;
370 >        int     bytesloaded, needbytes, bytes2free;
371 >        register int    i;
372 >                                        /* precheck consistency */
373 >        for (i = n; i--; )
374 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
375 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
376 >                                        /* sort list for optimal access */
377 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
378 >        bytesloaded = needbytes = 0;    /* figure out memory needs */
379 >        if ((origcachesize = hdcachesize) > 0) {
380 >                for (i = n; i--; )
381 >                        if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
382 >                                bp->tick = hdclock;     /* preempt swap */
383 >                                bytesloaded += bp->nrm;
384 >                        } else                          /* prepare to load */
385 >                                needbytes += hb[i].h->bi[hb[i].b].nrd;
386 >                bytesloaded *= sizeof(RAYVAL);
387 >                needbytes *= sizeof(RAYVAL);
388 >                do {                            /* free enough memory */
389 >                        memuse = hdmemuse(0);
390 >                        bytes2free = needbytes - (signed)(hdcachesize-memuse);
391 >                        if (bytes2free > (signed)(memuse - bytesloaded))
392 >                                bytes2free = memuse - bytesloaded;
393 >                } while (bytes2free > 0 &&
394 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
395 >        }
396 >        hdcachesize = 0;                /* load the ordered beams w/o swap */
397 >        for (i = 0; i < n; i++)
398 >                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
399 >                        (*bf)(bp, hb[i].h, hb[i].b);
400 >        hdcachesize = origcachesize;    /* resume dynamic swapping */
401 > }
402 >
403 >
404 > int
405 > hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
406   register HOLO   *hp;
407   register int    i;
408   {
409 <        int     nrays = hp->bl[i]->nrm;
410 <
411 <        if (hp->bi[i].nrd == nrays)     /* current one will do? */
409 >        unsigned int    nrays;
410 >        long    nfo;
411 >        unsigned int    n;
412 >                                        /* check file status */
413 >        if (hdfrag[hp->fd].writerr)
414 >                return(-1);
415 > #ifdef DEBUG
416 >        if (i < 1 | i > nbeams(hp))
417 >                error(CONSISTENCY, "bad beam index in hdsyncbeam");
418 > #endif
419 >                                        /* is current fragment OK? */
420 >        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
421                  return(0);
422 <
423 <        if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */
422 >                                        /* locate fragment */
423 >        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
424                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
425  
426          else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
427 <                        hdfrag[hp->fd]->flen)           /* EOF special case */
428 <                hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
427 >                        hdfrag[hp->fd].flen)            /* EOF special case */
428 >                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
429  
430          else {                                          /* general case */
431 <                register struct fragment        *f = hdfrag[hp->fd];
431 >                register struct fragment        *f = &hdfrag[hp->fd];
432                  register int    j, k;
433                                          /* relinquish old fragment */
434                  if (hp->bi[i].nrd) {
435 <                        if ((j = ++f->nfrags) >= MAXFRAG)
435 >                        j = f->nfrags++;
436 > #if MAXFRAG
437 >                        if (j >= MAXFRAG-1)
438                                  f->nfrags--;
439 <                        for ( ; ; ) {   /* stick it in our descending list */
440 <                                if (!--j || hp->bi[i].fo < f->fi[j-1].fo) {
439 > #endif
440 >                        if (j % FRAGBLK == 0) {         /* more frag. space */
441 >                                if (f->fi == NULL)
442 >                                        f->fi = (BEAMI *)malloc(
443 >                                                        FRAGBLK*sizeof(BEAMI));
444 >                                else
445 >                                        f->fi = (BEAMI *)realloc((char *)f->fi,
446 >                                                (j+FRAGBLK)*sizeof(BEAMI));
447 >                                if (f->fi == NULL)
448 >                                        error(SYSTEM,
449 >                                                "out of memory in hdsyncbeam");
450 >                        }
451 >                        for ( ; ; j--) {        /* insert in descending list */
452 >                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
453                                          f->fi[j].fo = hp->bi[i].fo;
454                                          f->fi[j].nrd = hp->bi[i].nrd;
455                                          break;
456                                  }
457 <                                copystruct(f->fi+j, f->fi+j-1);
457 >                                copystruct(f->fi+j, f->fi+(j-1));
458                          }
459                                          /* coalesce adjacent fragments */
460 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
461 <                                if (j != k)
462 <                                        copystruct(f->fi+j, f->fi+k);
463 <                                while (k+1 < f->nfrags &&
322 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
323 <                                                == f->fi[j].fo)
324 <                                        f->fi[j].fo -=
325 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
460 >                        if (j && f->fi[j-1].fo == f->fi[j].fo +
461 >                                        f->fi[j].nrd*sizeof(RAYVAL)) {
462 >                                f->fi[j].nrd += f->fi[j-1].nrd;
463 >                                f->fi[j-1].nrd = 0;
464                          }
465 <                        f->nfrags = j;
465 >                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo +
466 >                                        f->fi[j+1].nrd*sizeof(RAYVAL)) {
467 >                                f->fi[j+1].nrd += f->fi[j].nrd;
468 >                                f->fi[j].nrd = 0;
469 >                        }
470                  }
471                  k = -1;                 /* find closest-sized fragment */
472 <                for (j = f->nfrags; j-- > 0; )
472 >                for (j = (nrays ? f->nfrags : 0); j-- > 0; )
473                          if (f->fi[j].nrd >= nrays &&
474 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
475 <                                        && f->fi[k=j].nrd == nrays)
476 <                                break;
474 >                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
475 >                                if (f->fi[k=j].nrd == nrays)
476 >                                        break;
477                  if (k < 0) {            /* no fragment -- extend file */
478 <                        hp->bi[i].fo = f->flen;
478 >                        nfo = f->flen;
479                          f->flen += nrays*sizeof(RAYVAL);
480                  } else {                /* else use fragment */
481 <                        hp->bi[i].fo = f->fi[k].fo;
482 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
483 <                                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 <                        }
481 >                        nfo = f->fi[k].fo;
482 >                        f->fi[k].fo += nrays*sizeof(RAYVAL);
483 >                        f->fi[k].nrd -= nrays;
484                  }
485 +                                        /* delete empty remnants */
486 +                for (j = k = 0; k < f->nfrags; j++, k++) {
487 +                        while (f->fi[k].nrd == 0)
488 +                                if (++k >= f->nfrags)
489 +                                        goto endloop;
490 +                        if (k > j)
491 +                                copystruct(f->fi+j, f->fi+k);
492 +                }
493 +        endloop:
494 +                f->nfrags = j;
495          }
496 +        if (nrays) {            /* write the new fragment */
497 +                errno = 0;
498 +                if (lseek(hp->fd, nfo, 0) < 0)
499 +                        error(SYSTEM, "cannot seek on holodeck file");
500 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
501 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
502 +                        hdfrag[hp->fd].writerr++;
503 +                        hdsync(hp, 0);          /* sync directory */
504 +                        error(SYSTEM, "write error in hdsyncbeam");
505 +                }
506 +        }
507          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
508          hp->bi[i].nrd = nrays;
509 <        hp->dirty++;            /* section directory now out of date */
509 >        hp->bi[i].fo = nfo;
510 >        markdirty(hp);          /* section directory now out of date */
511          return(1);
512   }
513  
# Line 359 | Line 517 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
517   register HOLO   *hp;
518   register int    i;
519   {
520 <        int     nchanged, n;
520 >        int     nchanged;
521  
522          if (hp == NULL) {               /* clear all holodecks */
523                  nchanged = 0;
# Line 367 | Line 525 | register int   i;
525                          nchanged += hdfreebeam(hdlist[i], 0);
526                  return(nchanged);
527          }
528 +        if (hdfrag[hp->fd].writerr)     /* check for file error */
529 +                return(0);
530          if (i == 0) {                   /* clear entire holodeck */
531                  nchanged = 0;
532 <                for (i = 1; i <= nbeams(hp); i++)
533 <                        nchanged += hdfreebeam(hp, i);
532 >                for (i = nbeams(hp); i > 0; i--)
533 >                        if (hp->bl[i] != NULL)
534 >                                nchanged += hdfreebeam(hp, i);
535                  return(nchanged);
536          }
537 + #ifdef DEBUG
538          if (i < 1 | i > nbeams(hp))
539                  error(CONSISTENCY, "bad beam index to hdfreebeam");
540 + #endif
541          if (hp->bl[i] == NULL)
542                  return(0);
543                                          /* check for additions */
544          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
545 <        if (nchanged) {
546 <                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 <        }
545 >        if (nchanged)
546 >                hdsyncbeam(hp, i);              /* write new fragment */
547          blglob(hp)->nrm -= hp->bl[i]->nrm;
548          free((char *)hp->bl[i]);                /* free memory */
549          hp->bl[i] = NULL;
# Line 395 | Line 551 | register int   i;
551   }
552  
553  
554 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
555 < register HOLO   *ha[];                  /* section list (NULL terminated) */
556 < register int    ba[];                   /* beam index to go with section */
557 < int     n;                              /* length of arrays minus 1 */
554 > int
555 > hdkillbeam(hp, i)               /* delete beam from holodeck */
556 > register HOLO   *hp;
557 > register int    i;
558 > {
559 >        static BEAM     emptybeam;
560 >        int     nchanged;
561 >
562 >        if (hp == NULL) {               /* clobber all holodecks */
563 >                nchanged = 0;
564 >                for (i = 0; hdlist[i] != NULL; i++)
565 >                        nchanged += hdkillbeam(hdlist[i], 0);
566 >                return(nchanged);
567 >        }
568 >        if (i == 0) {                   /* clobber entire holodeck */
569 >                nchanged = 0;
570 >                for (i = nbeams(hp); i > 0; i--)
571 >                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
572 >                                nchanged += hdkillbeam(hp, i);
573 > #ifdef DEBUG
574 >                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
575 >                        error(CONSISTENCY, "bad beam count in hdkillbeam");
576 > #endif
577 >                return(nchanged);
578 >        }
579 > #ifdef DEBUG
580 >        if (i < 1 | i > nbeams(hp))
581 >                error(CONSISTENCY, "bad beam index to hdkillbeam");
582 > #endif
583 >        if (hp->bl[i] != NULL) {        /* free memory */
584 >                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
585 >                free((char *)hp->bl[i]);
586 >        } else
587 >                nchanged = hp->bi[i].nrd;
588 >        if (hp->bi[i].nrd) {            /* free file fragment */
589 >                hp->bl[i] = &emptybeam;
590 >                hdsyncbeam(hp, i);
591 >        }
592 >        hp->bl[i] = NULL;
593 >        return(nchanged);
594 > }
595 >
596 >
597 > int
598 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
599 > register HDBEAMI        *hb;            /* beam list */
600 > int     nents;                          /* current list length */
601 > int     n;                              /* maximum list length */
602   register HOLO   *hp;                    /* section we're adding from */
603   {
604          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;
605                                          /* insert each beam from hp */
606 <        for (i = nbeams(hp); i > 0; i-- ) {
606 >        for (i = nbeams(hp); i > 0; i--) {
607                  if (hp->bl[i] == NULL)          /* check if loaded */
608                          continue;
609 <                if ((j = ++nents) > n)          /* grow list if we can */
609 > #if 0
610 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
611 >                        continue;
612 > #endif
613 >                if ((j = ++nents) >= n)         /* grow list if we can */
614                          nents--;
615                  for ( ; ; ) {                   /* bubble into place */
616                          if (!--j || hp->bl[i]->tick >=
617 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
618 <                                ha[j] = hp;
619 <                                ba[j] = i;
617 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
618 >                                hb[j].h = hp;
619 >                                hb[j].b = i;
620                                  break;
621                          }
622 <                        ha[j] = ha[j-1];
424 <                        ba[j] = ba[j-1];
622 >                        copystruct(hb+j, hb+(j-1));
623                  }
624          }
625 <        ha[nents] = NULL;               /* all done */
428 <        ba[nents] = 0;
625 >        return(nents);                  /* return new list length */
626   }
627  
628  
629 + int
630   hdfreecache(pct, honly)         /* free up cache space, writing changes */
631   int     pct;                            /* maximum percentage to free */
632   register HOLO   *honly;                 /* NULL means check all */
633   {
634 <        HOLO    *hp[FREEBEAMS+1];
437 <        int     bn[FREEBEAMS+1];
634 >        HDBEAMI hb[FREEBEAMS];
635          int     freetarget;
636 +        int     n;
637          register int    i;
638                                                  /* compute free target */
639          freetarget = (honly != NULL) ? blglob(honly)->nrm :
640                          hdmemuse(0)/sizeof(RAYVAL) ;
641          freetarget = freetarget*pct/100;
642 +        if (freetarget <= 0)
643 +                return(0);
644                                                  /* find least recently used */
645 <        hp[0] = NULL;
446 <        bn[0] = 0;
645 >        n = 0;
646          if (honly != NULL)
647 <                hdlrulist(hp, bn, FREEBEAMS, honly);
647 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
648          else
649                  for (i = 0; hdlist[i] != NULL; i++)
650 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
650 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
651                                                  /* free LRU beams */
652 <        for (i = 0; hp[i] != NULL; i++) {
653 <                hdfreebeam(hp[i], bn[i]);
654 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
652 >        for (i = 0; i < n; i++) {
653 >                hdfreebeam(hb[i].h, hb[i].b);
654 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
655                          break;
656          }
657 <        hdsync(honly);          /* synchronize directories as necessary */
657 >        hdsync(honly, 0);       /* synchronize directories as necessary */
658 >        return(-freetarget);    /* return how far past goal we went */
659   }
660  
661  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines