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.14 by gregl, Thu Dec 18 15:12:23 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 */
24 >
25 > /* define MAXFRAG if you want to limit fragment tracking memory */
26 >
27 > #ifndef BSD
28 > #define write   writebuf        /* safe i/o routines */
29 > #define read    readbuf
30   #endif
27 #ifndef MAXFRAG
28 #define MAXFRAG         2048    /* maximum fragments tracked in each file */
29 #endif
31  
32 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
32 > #define FRAGBLK         64      /* number of fragments to allocate at a time */
33 >
34 > unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
35   unsigned long   hdclock;        /* clock value */
36  
37   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
38  
39   static struct fragment {
40          short   nlinks;         /* number of holodeck sections using us */
41 <        short   nfrags;         /* number of known fragments */
42 <        BEAMI   fi[MAXFRAG];    /* fragments, descending file position */
41 >        short   writerr;        /* write error encountered */
42 >        int     nfrags;         /* number of known fragments */
43 >        BEAMI   *fi;            /* fragments, descending file position */
44          long    flen;           /* last known file length */
45 < } *hdfrag[FFDMAX];      /* fragment lists, indexed by file descriptor */
45 > } *hdfrag;              /* fragment lists, indexed by file descriptor */
46  
47 + static int      nhdfrags;       /* size of hdfrag array */
48  
49 +
50   hdattach(fd)            /* start tracking file fragments for some section */
51   register int    fd;
52   {
53 <        if (fd >= FFDMAX)
54 <                return;                         /* descriptor out of range */
55 <        if (hdfrag[fd] == NULL) {
56 <                if ((hdfrag[fd] = (struct fragment *)
57 <                                malloc(sizeof(struct fragment))) == NULL)
58 <                        return;                 /* should flag error? */
59 <                hdfrag[fd]->nlinks = 0;
60 <                hdfrag[fd]->nfrags = 0;
53 >        if (fd >= nhdfrags) {
54 >                if (nhdfrags)
55 >                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
56 >                                        (fd+1)*sizeof(struct fragment));
57 >                else
58 >                        hdfrag = (struct fragment *)malloc(
59 >                                        (fd+1)*sizeof(struct fragment));
60 >                if (hdfrag == NULL)
61 >                        error(SYSTEM, "out of memory in hdattach");
62 >                bzero((char *)(hdfrag+nhdfrags),
63 >                                (fd+1-nhdfrags)*sizeof(struct fragment));
64 >                nhdfrags = fd+1;
65          }
66 <        hdfrag[fd]->nlinks++;
67 <        hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */
66 >        hdfrag[fd].nlinks++;
67 >        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
68   }
69  
70  
# Line 64 | Line 74 | register int   fd;
74   hdrelease(fd)           /* stop tracking file fragments for some section */
75   register int    fd;
76   {
77 <        if (fd >= FFDMAX || hdfrag[fd] == NULL)
77 >        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
78                  return;
79 <        if (!--hdfrag[fd]->nlinks) {
80 <                free((char *)hdfrag[fd]);
81 <                hdfrag[fd] = NULL;
79 >        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
80 >                free((char *)hdfrag[fd].fi);
81 >                hdfrag[fd].fi = NULL;
82 >                hdfrag[fd].nfrags = 0;
83          }
84   }
85  
86  
87 + markdirty(hp)                   /* mark holodeck section directory dirty */
88 + register HOLO   *hp;
89 + {
90 +        static BEAMI    smudge = {0, -1};
91 +
92 +        if (hp->dirty)          /* already marked? */
93 +                return;
94 +        hp->dirty = 1;
95 +        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
96 +                        || write(hp->fd, (char *)&smudge,
97 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
98 +                error(SYSTEM, "seek/write error in markdirty");
99 + }
100 +
101 +
102   HOLO *
103   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
104   int     fd;                     /* corresponding file descriptor */
# Line 97 | Line 123 | HDGRID *hproto;                /* holodeck section grid */
123                  n = nbeams(hp)*sizeof(BEAMI);
124                  if (read(fd, (char *)(hp->bi+1), n) != n)
125                          error(SYSTEM, "failure loading holodeck directory");
126 +                                                /* check that it's clean */
127 +                if (hp->bi[nbeams(hp)].fo < 0)
128 +                        error(USER, "dirty holodeck section");
129          } else {                        /* assume we're creating it */
130                  if ((hp = hdalloc(hproto)) == NULL)
131                          goto memerr;
# Line 110 | Line 139 | HDGRID *hproto;                /* holodeck section grid */
139          hp->fd = fd;    
140          hp->dirty = 0;
141          biglob(hp)->fo = fpos + sizeof(HDGRID);
142 <        biglob(hp)->nrd = 0;            /* count rays on disk */
143 <        for (n = nbeams(hp); n > 0; n--)
144 <                biglob(hp)->nrd += hp->bi[n].nrd;
142 >                                        /* start tracking fragments */
143 >        hdattach(fd);
144 >                                        /* check rays on disk */
145 >        fpos = hdfilen(fd);
146 >        biglob(hp)->nrd = 0;
147 >        for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
148 >                if (hp->bi[n].nrd)
149 >                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
150 >                                hp->bi[n].nrd = 0;      /* off end */
151 >                        else
152 >                                biglob(hp)->nrd += hp->bi[n].nrd;
153                                          /* add to holodeck list */
154          for (n = 0; n < HDMAX; n++)
155                  if (hdlist[n] == NULL) {
156                          hdlist[n] = hp;
157                          break;
158                  }
122                                        /* start tracking fragments (last) */
123        hdattach(fd);
159                                          /* all done */
160          return(hp);
161   memerr:
# Line 129 | Line 164 | memerr:
164  
165  
166   int
167 < hdsync(hp)                      /* update directory on disk if necessary */
167 > hdsync(hp, all)                 /* update beams and directory on disk */
168   register HOLO   *hp;
169 + int     all;
170   {
171          register int    j, n;
172  
173 <        if (hp == NULL) {               /* do all */
173 >        if (hp == NULL) {               /* do all holodecks */
174                  n = 0;
175                  for (j = 0; hdlist[j] != NULL; j++)
176 <                        n += hdsync(hdlist[j]);
176 >                        n += hdsync(hdlist[j], all);
177                  return(n);
178          }
179 <        if (!hp->dirty)                 /* check first */
179 >                                        /* sync the beams */
180 >        for (j = all ? nbeams(hp) : 0; j > 0; j--)
181 >                if (hp->bl[j] != NULL)
182 >                        hdsyncbeam(hp, j);
183 >        if (!hp->dirty)                 /* directory clean? */
184                  return(0);
185 +        errno = 0;
186          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
187                  error(SYSTEM, "cannot seek on holodeck file");
188          n = nbeams(hp)*sizeof(BEAMI);
# Line 152 | Line 193 | register HOLO  *hp;
193   }
194  
195  
196 < long
196 > unsigned
197   hdmemuse(all)           /* return memory usage (in bytes) */
198   int     all;                    /* include overhead (painful) */
199   {
# Line 171 | Line 212 | int    all;                    /* include overhead (painful) */
212                  }
213          }
214          if (all)
215 <                for (j = 0; j < FFDMAX; j++)
216 <                        if (hdfrag[j] != NULL)
217 <                                total += sizeof(struct fragment);
215 >                for (j = 0; j < nhdfrags; j++) {
216 >                        total += sizeof(struct fragment);
217 >                        if (hdfrag[j].nfrags)
218 >                                total += FRAGBLK*sizeof(BEAMI) *
219 >                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
220 >                }
221          return(total);
222   }
223  
224  
225   long
226 + hdfilen(fd)             /* return file length for fd */
227 + int     fd;
228 + {
229 +        long    fpos, flen;
230 +
231 +        if (fd < 0)
232 +                return(-1);
233 +        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
234 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
235 +                        return(-1);
236 +                flen = lseek(fd, 0L, 2);
237 +                lseek(fd, fpos, 0);
238 +                return(flen);
239 +        }
240 +        return(hdfrag[fd].flen);
241 + }
242 +
243 +
244 + long
245   hdfiluse(fd, all)       /* compute file usage (in bytes) */
246   int     fd;                     /* open file descriptor to check */
247   int     all;                    /* include overhead and unflushed data */
# Line 221 | Line 284 | int    nr;                     /* number of new rays desired */
284                  hp->bl[i]->tick = hdclock;      /* preempt swap */
285          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
286                  hdfreecache(PCTFREE, NULL);     /* free some space */
287 +        errno = 0;
288          if (hp->bl[i] == NULL) {                /* allocate (and load) */
289                  n = hp->bi[i].nrd + nr;
290                  if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
# Line 244 | Line 308 | int    nr;                     /* number of new rays desired */
308          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
309          hp->bl[i]->nrm += nr;                   /* update in-core structure */
310          bzero((char *)p, nr*sizeof(RAYVAL));
311 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
312 <        blglob(hp)->tick = hdclock;
311 >        hp->bl[i]->tick = hdclock;              /* update LRU clock */
312 >        blglob(hp)->tick = hdclock++;
313          return(p);                              /* point to new rays */
314   memerr:
315          error(SYSTEM, "out of memory in hdnewrays");
# Line 276 | Line 340 | register int   i;
340                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
341                          error(SYSTEM, "error reading beam from holodeck file");
342          }
343 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
344 <        blglob(hp)->tick = hdclock;
343 >        hp->bl[i]->tick = hdclock;      /* update LRU clock */
344 >        blglob(hp)->tick = hdclock++;
345          return(hp->bl[i]);
346   }
347  
348  
349   int
350 < hdgetbi(hp, i)                  /* allocate a file fragment */
350 > hdfilord(hb1, hb2)      /* order beams for optimal loading */
351 > register HDBEAMI        *hb1, *hb2;
352 > {
353 >        register int    c;
354 >                                /* sort by file descriptor first */
355 >        if ((c = hb1->h->fd - hb2->h->fd))
356 >                return(c);
357 >                                /* then by position in file */
358 >        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
359 > }
360 >
361 >
362 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
363 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
364 > int     n;                      /* list length */
365 > int     (*bf)();                /* callback function (optional) */
366 > {
367 >        unsigned        origcachesize, memuse;
368 >        register BEAM   *bp;
369 >        int     bytesloaded, needbytes, bytes2free;
370 >        register int    i;
371 >                                        /* precheck consistency */
372 >        for (i = n; i--; )
373 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
374 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
375 >                                        /* sort list for optimal access */
376 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
377 >        if ((origcachesize = hdcachesize) == 0)
378 >                goto loadbeams;
379 >        bytesloaded = needbytes = 0;    /* figure out memory needs */
380 >        for (i = 0; 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 > loadbeams:
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 > #ifdef 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)
461 >                                if (k > j)
462                                          copystruct(f->fi+j, f->fi+k);
463 <                                while (k+1 < f->nfrags &&
464 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
465 <                                                == f->fi[j].fo)
463 >                                while (k+1 < f->nfrags && f->fi[k+1].fo +
464 >                                                f->fi[k+1].nrd*sizeof(RAYVAL)
465 >                                                        == f->fi[j].fo) {
466                                          f->fi[j].fo -=
467                                                  f->fi[++k].nrd*sizeof(RAYVAL);
468 +                                        f->fi[j].nrd += f->fi[k].nrd;
469 +                                }
470                          }
471                          f->nfrags = j;
472                  }
473                  k = -1;                 /* find closest-sized fragment */
474 <                for (j = f->nfrags; j-- > 0; )
474 >                for (j = nrays ? f->nfrags : 0; j-- > 0; )
475                          if (f->fi[j].nrd >= nrays &&
476 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
477 <                                        && f->fi[k=j].nrd == nrays)
478 <                                break;
476 >                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
477 >                                if (f->fi[k=j].nrd == nrays)
478 >                                        break;
479                  if (k < 0) {            /* no fragment -- extend file */
480 <                        hp->bi[i].fo = f->flen;
480 >                        nfo = f->flen;
481                          f->flen += nrays*sizeof(RAYVAL);
482                  } else {                /* else use fragment */
483 <                        hp->bi[i].fo = f->fi[k].fo;
483 >                        nfo = f->fi[k].fo;
484                          if (f->fi[k].nrd == nrays) {    /* delete fragment */
485                                  f->nfrags--;
486                                  for (j = k; j < f->nfrags; j++)
487 <                                        copystruct(f->fi+j, f->fi+j+1);
487 >                                        copystruct(f->fi+j, f->fi+(j+1));
488                          } else {                        /* else shrink it */
489                                  f->fi[k].fo += nrays*sizeof(RAYVAL);
490                                  f->fi[k].nrd -= nrays;
491                          }
492                  }
493          }
494 +        if (nrays) {            /* write the new fragment */
495 +                errno = 0;
496 +                if (lseek(hp->fd, nfo, 0) < 0)
497 +                        error(SYSTEM, "cannot seek on holodeck file");
498 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
499 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
500 +                        hdfrag[hp->fd].writerr++;
501 +                        hdsync(hp, 0);          /* sync directory */
502 +                        error(SYSTEM, "write error in hdsyncbeam");
503 +                }
504 +        }
505          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
506          hp->bi[i].nrd = nrays;
507 <        hp->dirty++;            /* section directory now out of date */
507 >        hp->bi[i].fo = nfo;
508 >        markdirty(hp);          /* section directory now out of date */
509          return(1);
510   }
511  
# Line 359 | Line 515 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
515   register HOLO   *hp;
516   register int    i;
517   {
518 <        int     nchanged, n;
518 >        int     nchanged;
519  
520          if (hp == NULL) {               /* clear all holodecks */
521                  nchanged = 0;
# Line 367 | Line 523 | register int   i;
523                          nchanged += hdfreebeam(hdlist[i], 0);
524                  return(nchanged);
525          }
526 +        if (hdfrag[hp->fd].writerr)     /* check for file error */
527 +                return(0);
528          if (i == 0) {                   /* clear entire holodeck */
529                  nchanged = 0;
530 <                for (i = 1; i <= nbeams(hp); i++)
531 <                        nchanged += hdfreebeam(hp, i);
530 >                for (i = nbeams(hp); i > 0; i--)
531 >                        if (hp->bl[i] != NULL)
532 >                                nchanged += hdfreebeam(hp, i);
533                  return(nchanged);
534          }
535 + #ifdef DEBUG
536          if (i < 1 | i > nbeams(hp))
537                  error(CONSISTENCY, "bad beam index to hdfreebeam");
538 + #endif
539          if (hp->bl[i] == NULL)
540                  return(0);
541                                          /* check for additions */
542          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
543 <        if (nchanged) {
544 <                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 <        }
543 >        if (nchanged)
544 >                hdsyncbeam(hp, i);              /* write new fragment */
545          blglob(hp)->nrm -= hp->bl[i]->nrm;
546          free((char *)hp->bl[i]);                /* free memory */
547          hp->bl[i] = NULL;
# Line 395 | Line 549 | register int   i;
549   }
550  
551  
552 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
553 < register HOLO   *ha[];                  /* section list (NULL terminated) */
554 < register int    ba[];                   /* beam index to go with section */
555 < int     n;                              /* length of arrays minus 1 */
552 > int
553 > hdkillbeam(hp, i)               /* delete beam from holodeck */
554 > register HOLO   *hp;
555 > register int    i;
556 > {
557 >        static BEAM     emptybeam;
558 >        int     nchanged;
559 >
560 >        if (hp == NULL) {               /* clobber all holodecks */
561 >                nchanged = 0;
562 >                for (i = 0; hdlist[i] != NULL; i++)
563 >                        nchanged += hdkillbeam(hdlist[i], 0);
564 >                return(nchanged);
565 >        }
566 >        if (i == 0) {                   /* clobber entire holodeck */
567 >                nchanged = 0;
568 >                for (i = nbeams(hp); i > 0; i--)
569 >                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
570 >                                nchanged += hdkillbeam(hp, i);
571 > #ifdef DEBUG
572 >                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
573 >                        error(CONSISTENCY, "bad beam count in hdkillbeam");
574 > #endif
575 >                return(nchanged);
576 >        }
577 > #ifdef DEBUG
578 >        if (i < 1 | i > nbeams(hp))
579 >                error(CONSISTENCY, "bad beam index to hdkillbeam");
580 > #endif
581 >        if (hp->bl[i] != NULL) {        /* free memory */
582 >                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
583 >                free((char *)hp->bl[i]);
584 >        } else
585 >                nchanged = hp->bi[i].nrd;
586 >        if (hp->bi[i].nrd) {            /* free file fragment */
587 >                hp->bl[i] = &emptybeam;
588 >                hdsyncbeam(hp, i);
589 >        }
590 >        hp->bl[i] = NULL;
591 >        return(nchanged);
592 > }
593 >
594 >
595 > int
596 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
597 > register HDBEAMI        *hb;            /* beam list */
598 > int     nents;                          /* current list length */
599 > int     n;                              /* maximum list length */
600   register HOLO   *hp;                    /* section we're adding from */
601   {
602          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;
603                                          /* insert each beam from hp */
604 <        for (i = nbeams(hp); i > 0; i-- ) {
604 >        for (i = nbeams(hp); i > 0; i--) {
605                  if (hp->bl[i] == NULL)          /* check if loaded */
606                          continue;
607 <                if ((j = ++nents) > n)          /* grow list if we can */
607 > #if 0
608 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
609 >                        continue;
610 > #endif
611 >                if ((j = ++nents) >= n)         /* grow list if we can */
612                          nents--;
613                  for ( ; ; ) {                   /* bubble into place */
614                          if (!--j || hp->bl[i]->tick >=
615 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
616 <                                ha[j] = hp;
617 <                                ba[j] = i;
615 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
616 >                                hb[j].h = hp;
617 >                                hb[j].b = i;
618                                  break;
619                          }
620 <                        ha[j] = ha[j-1];
424 <                        ba[j] = ba[j-1];
620 >                        copystruct(hb+j, hb+(j-1));
621                  }
622          }
623 <        ha[nents] = NULL;               /* all done */
428 <        ba[nents] = 0;
623 >        return(nents);                  /* return new list length */
624   }
625  
626  
627 + int
628   hdfreecache(pct, honly)         /* free up cache space, writing changes */
629   int     pct;                            /* maximum percentage to free */
630   register HOLO   *honly;                 /* NULL means check all */
631   {
632 <        HOLO    *hp[FREEBEAMS+1];
437 <        int     bn[FREEBEAMS+1];
632 >        HDBEAMI hb[FREEBEAMS];
633          int     freetarget;
634 +        int     n;
635          register int    i;
636                                                  /* compute free target */
637          freetarget = (honly != NULL) ? blglob(honly)->nrm :
638                          hdmemuse(0)/sizeof(RAYVAL) ;
639          freetarget = freetarget*pct/100;
640 +        if (freetarget <= 0)
641 +                return(0);
642                                                  /* find least recently used */
643 <        hp[0] = NULL;
446 <        bn[0] = 0;
643 >        n = 0;
644          if (honly != NULL)
645 <                hdlrulist(hp, bn, FREEBEAMS, honly);
645 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
646          else
647                  for (i = 0; hdlist[i] != NULL; i++)
648 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
648 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
649                                                  /* free LRU beams */
650 <        for (i = 0; hp[i] != NULL; i++) {
651 <                hdfreebeam(hp[i], bn[i]);
652 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
650 >        for (i = 0; i < n; i++) {
651 >                hdfreebeam(hb[i].h, hb[i].b);
652 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
653                          break;
654          }
655 <        hdsync(honly);          /* synchronize directories as necessary */
655 >        hdsync(honly, 0);       /* synchronize directories as necessary */
656 >        return(-freetarget);    /* return how far past goal we went */
657   }
658  
659  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines