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.10 by gregl, Thu Dec 11 11:29:35 1997 UTC vs.
Revision 3.19 by gregl, Sun Jan 4 12:32:53 1998 UTC

# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ SGI";
21   #ifndef PCTFREE
22   #define PCTFREE         20      /* maximum fraction to free (%) */
23   #endif
24 + #ifndef MAXFRAG
25 + #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
26 + #endif
27  
25 /* define MAXFRAG if you want to limit fragment tracking memory */
26
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 */
33 > #define FRAGBLK         256     /* number of fragments to allocate at a time */
34  
35 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
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 */
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;              /* fragment lists, indexed by file descriptor */
46 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
47  
48 < static int      nhdfrags;       /* size of hdfrag array */
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 >= nhdfrags) {
55 <                if (nhdfrags)
56 <                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
57 <                                        (fd+1)*sizeof(struct fragment));
54 >        if (fd >= nhdfragls) {
55 >                if (nhdfragls)
56 >                        hdfragl = (struct fraglist *)realloc((char *)hdfragl,
57 >                                        (fd+1)*sizeof(struct fraglist));
58                  else
59 <                        hdfrag = (struct fragment *)malloc(
60 <                                        (fd+1)*sizeof(struct fragment));
61 <                if (hdfrag == NULL)
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 *)(hdfrag+nhdfrags),
64 <                                (fd+1-nhdfrags)*sizeof(struct fragment));
65 <                nhdfrags = fd+1;
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 73 | Line 75 | register int   fd;
75   hdrelease(fd)           /* stop tracking file fragments for some section */
76   register int    fd;
77   {
78 <        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
78 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
79                  return;
80 <        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
81 <                free((char *)hdfrag[fd].fi);
82 <                hdfrag[fd].fi = NULL;
83 <                hdfrag[fd].nfrags = 0;
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  
# Line 138 | 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                  }
150                                        /* start tracking fragments (last) */
151        hdattach(fd);
160                                          /* all done */
161          return(hp);
162   memerr:
# Line 157 | 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)
# Line 181 | 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 200 | Line 213 | int    all;                    /* include overhead (painful) */
213                  }
214          }
215          if (all)
216 <                for (j = 0; j < nhdfrags; j++) {
217 <                        total += sizeof(struct fragment);
218 <                        if (hdfrag[j].nfrags)
216 >                for (j = 0; j < nhdfragls; j++) {
217 >                        total += sizeof(struct fraglist);
218 >                        if (hdfragl[j].nfrags)
219                                  total += FRAGBLK*sizeof(BEAMI) *
220 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
220 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
221                  }
222          return(total);
223   }
# Line 218 | Line 231 | int    fd;
231  
232          if (fd < 0)
233                  return(-1);
234 <        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
234 >        if (fd >= nhdfragls || !hdfragl[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);
241 >        return(hdfragl[fd].flen);
242   }
243  
244  
# Line 296 | 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 328 | 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 */
352 < register HOLO   *hp;
340 < register int    i;
351 > hdfilord(hb1, hb2)      /* order beams for quick loading */
352 > register HDBEAMI        *hb1, *hb2;
353   {
354 <        int     nrays = hp->bl[i]->nrm;
354 >        register long   c;
355 >                                /* residents go first */
356 >        if (hb2->h->bl[hb2->b] != NULL)
357 >                return(hb1->h->bl[hb1->b] == NULL);
358 >        if (hb1->h->bl[hb1->b] != NULL)
359 >                return(-1);
360 >                                /* otherwise sort by file descriptor */
361 >        if ((c = hb1->h->fd - hb2->h->fd))
362 >                return(c);
363 >                                /* then by position in file */
364 >        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
365 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
366 > }
367  
344        if (hp->bi[i].nrd == nrays)     /* current one will do? */
345                return(0);
368  
369 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
370 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
369 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
370 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
371 > int     n;                      /* list length */
372 > int     (*bf)();                /* callback function (optional) */
373 > {
374 >        unsigned        origcachesize, memuse;
375 >        register BEAM   *bp;
376 >        int     bytesloaded, needbytes, bytes2free;
377 >        register int    i;
378 >                                        /* precheck consistency */
379 >        for (i = n; i--; )
380 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
381 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
382 >                                        /* sort list for optimal access */
383 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
384 >        bytesloaded = 0;                /* run through loaded beams */
385 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
386 >                bp->tick = hdclock;             /* preempt swap */
387 >                bytesloaded += bp->nrm;
388 >                if (bf != NULL)
389 >                        (*bf)(bp, hb->h, hb->b);
390 >        }
391 >        bytesloaded *= sizeof(RAYVAL);
392 >        if ((origcachesize = hdcachesize) > 0) {
393 >                needbytes = 0;          /* figure out memory needs */
394 >                for (i = n; i--; )
395 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
396 >                needbytes *= sizeof(RAYVAL);
397 >                do {                            /* free enough memory */
398 >                        memuse = hdmemuse(0);
399 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
400 >                        if (bytes2free > (int)(memuse - bytesloaded))
401 >                                bytes2free = memuse - bytesloaded;
402 >                } while (bytes2free > 0 &&
403 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
404 >                hdcachesize = 0;                /* load beams w/o swap */
405 >        }
406 >        for (i = 0; i < n; i++)
407 >                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
408 >                        (*bf)(bp, hb[i].h, hb[i].b);
409 >        hdcachesize = origcachesize;    /* resume dynamic swapping */
410 > }
411  
350        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
351                        hdfrag[hp->fd].flen)            /* EOF special case */
352                hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
412  
413 <        else {                                          /* general case */
414 <                register struct fragment        *f = &hdfrag[hp->fd];
415 <                register int    j, k;
416 <                                        /* relinquish old fragment */
417 <                if (hp->bi[i].nrd) {
418 <                        j = f->nfrags++;
419 < #ifdef MAXFRAG
420 <                        if (j >= MAXFRAG-1)
421 <                                f->nfrags--;
413 > hdfreefrag(fd, bi)                      /* free a file fragment */
414 > int     fd;
415 > register BEAMI  *bi;
416 > {
417 >        register struct fraglist        *f;
418 >        register int    j, k;
419 >
420 >        if (bi->nrd == 0)
421 >                return;
422 > #ifdef DEBUG
423 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
424 >                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
425   #endif
426 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
427 <                                if (f->fi == NULL)
428 <                                        f->fi = (BEAMI *)malloc(
429 <                                                        FRAGBLK*sizeof(BEAMI));
430 <                                else
431 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
432 <                                                (j+FRAGBLK)*sizeof(BEAMI));
433 <                                if (f->fi == NULL)
434 <                                        error(SYSTEM,
435 <                                                "out of memory in hdgetbi");
436 <                        }
437 <                        for ( ; ; j--) {        /* insert in descending list */
438 <                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
439 <                                        f->fi[j].fo = hp->bi[i].fo;
440 <                                        f->fi[j].nrd = hp->bi[i].nrd;
441 <                                        break;
442 <                                }
443 <                                copystruct(f->fi+j, f->fi+j-1);
444 <                        }
426 >        f = &hdfragl[fd];
427 >        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
428 >                for (j = k = 0; k < f->nfrags; j++, k++) {
429 >                        while (f->fi[k].nrd == 0)
430 >                                if (++k >= f->nfrags)
431 >                                        goto endloop;
432 >                        if (k > j)
433 >                                copystruct(f->fi+j, f->fi+k);
434 >                }
435 >        endloop:
436 >                f->nfrags = j;
437 >        }
438 >        j = f->nfrags++;                /* allocate a slot in free list */
439 > #if MAXFRAG
440 >        if (j >= MAXFRAG-1)
441 >                f->nfrags--;
442 > #endif
443 >        if (j % FRAGBLK == 0) {         /* more free list space */
444 >                if (f->fi == NULL)
445 >                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
446 >                else
447 >                        f->fi = (BEAMI *)realloc((char *)f->fi,
448 >                                        (j+FRAGBLK)*sizeof(BEAMI));
449 >                if (f->fi == NULL)
450 >                        error(SYSTEM, "out of memory in hdfreefrag");
451 >        }
452 >        for ( ; ; j--) {                /* insert in descending list */
453 >                if (!j || bi->fo < f->fi[j-1].fo) {
454 >                        f->fi[j].fo = bi->fo;
455 >                        f->fi[j].nrd = bi->nrd;
456 >                        break;
457 >                }
458 >                copystruct(f->fi+j, f->fi+(j-1));
459 >        }
460                                          /* coalesce adjacent fragments */
461 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
462 <                                if (k > j)
463 <                                        copystruct(f->fi+j, f->fi+k);
464 <                                while (k+1 < f->nfrags && f->fi[k+1].fo +
465 <                                                f->fi[k+1].nrd*sizeof(RAYVAL)
466 <                                                        == f->fi[j].fo) {
467 <                                        f->fi[j].fo -=
468 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
469 <                                        f->fi[j].nrd += f->fi[k].nrd;
470 <                                }
461 >                                                /* successors never empty */
462 >        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
463 >                f->fi[j].nrd += f->fi[j-1].nrd;
464 >                f->fi[j-1].nrd = 0;
465 >        }
466 >        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
467 >                if (f->fi[k].nrd) {
468 >                        if (f->fi[j].fo == f->fi[k].fo +
469 >                                        f->fi[k].nrd*sizeof(RAYVAL)) {
470 >                                f->fi[k].nrd += f->fi[j].nrd;
471 >                                f->fi[j].nrd = 0;
472                          }
473 <                        f->nfrags = j;
473 >                        break;
474                  }
475 <                k = -1;                 /* find closest-sized fragment */
476 <                for (j = f->nfrags; j-- > 0; )
477 <                        if (f->fi[j].nrd >= nrays &&
478 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
479 <                                if (f->fi[k=j].nrd == nrays)
480 <                                        break;
481 <                if (k < 0) {            /* no fragment -- extend file */
482 <                        hp->bi[i].fo = f->flen;
483 <                        f->flen += nrays*sizeof(RAYVAL);
484 <                } else {                /* else use fragment */
485 <                        hp->bi[i].fo = f->fi[k].fo;
486 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
487 <                                f->nfrags--;
488 <                                for (j = k; j < f->nfrags; j++)
489 <                                        copystruct(f->fi+j, f->fi+j+1);
490 <                        } else {                        /* else shrink it */
491 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
492 <                                f->fi[k].nrd -= nrays;
493 <                        }
494 <                }
475 > }
476 >
477 >
478 > long
479 > hdallocfrag(fd, nrays)          /* allocate a file fragment */
480 > int     fd;
481 > unsigned int4   nrays;
482 > {
483 >        register struct fraglist        *f;
484 >        register int    j, k;
485 >        long    nfo;
486 >
487 >        if (nrays == 0)
488 >                return(-1L);
489 > #ifdef DEBUG
490 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
491 >                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
492 > #endif
493 >        f = &hdfragl[fd];
494 >        k = -1;                         /* find closest-sized fragment */
495 >        for (j = f->nfrags; j-- > 0; )
496 >                if (f->fi[j].nrd >= nrays &&
497 >                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
498 >                        if (f->fi[k=j].nrd == nrays)
499 >                                break;
500 >        if (k < 0) {                    /* no fragment -- extend file */
501 >                nfo = f->flen;
502 >                f->flen += nrays*sizeof(RAYVAL);
503 >        } else {                        /* else use fragment */
504 >                nfo = f->fi[k].fo;
505 >                f->fi[k].fo += nrays*sizeof(RAYVAL);
506 >                f->fi[k].nrd -= nrays;
507          }
508 +        return(nfo);
509 + }
510 +
511 +
512 + int
513 + hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
514 + register HOLO   *hp;
515 + register int    i;
516 + {
517 +        unsigned int4   nrays;
518 +        unsigned int    n;
519 +        long    nfo;
520 +                                        /* check file status */
521 +        if (hdfragl[hp->fd].writerr)
522 +                return(-1);
523 + #ifdef DEBUG
524 +        if (i < 1 | i > nbeams(hp))
525 +                error(CONSISTENCY, "bad beam index in hdsyncbeam");
526 + #endif
527 +                                        /* is current fragment OK? */
528 +        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
529 +                return(0);
530 +        if (hp->bi[i].nrd)              /* relinquish old fragment */
531 +                hdfreefrag(hp->fd, &hp->bi[i]);
532 +        if (nrays) {                    /* get and write new fragment */
533 +                nfo = hdallocfrag(hp->fd, nrays);
534 +                errno = 0;
535 +                if (lseek(hp->fd, nfo, 0) < 0)
536 +                        error(SYSTEM, "cannot seek on holodeck file");
537 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
538 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
539 +                        hdfragl[hp->fd].writerr++;
540 +                        hdsync(NULL, 0);        /* sync directories */
541 +                        error(SYSTEM, "write error in hdsyncbeam");
542 +                }
543 +                hp->bi[i].fo = nfo;
544 +        } else
545 +                hp->bi[i].fo = 0L;
546          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
547          hp->bi[i].nrd = nrays;
548 <        markdirty(hp);          /* section directory now out of date */
548 >        markdirty(hp);                  /* section directory now out of date */
549          return(1);
550   }
551  
# Line 427 | Line 555 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
555   register HOLO   *hp;
556   register int    i;
557   {
558 <        int     nchanged, n;
558 >        int     nchanged;
559  
560          if (hp == NULL) {               /* clear all holodecks */
561                  nchanged = 0;
# Line 435 | Line 563 | register int   i;
563                          nchanged += hdfreebeam(hdlist[i], 0);
564                  return(nchanged);
565          }
566 <        if (hp->fd < 0)                 /* check for recursive error */
567 <                return(-1);
566 >        if (hdfragl[hp->fd].writerr)    /* check for file error */
567 >                return(0);
568          if (i == 0) {                   /* clear entire holodeck */
569                  nchanged = 0;
570                  for (i = nbeams(hp); i > 0; i--)
# Line 444 | Line 572 | register int   i;
572                                  nchanged += hdfreebeam(hp, i);
573                  return(nchanged);
574          }
575 + #ifdef DEBUG
576          if (i < 1 | i > nbeams(hp))
577                  error(CONSISTENCY, "bad beam index to hdfreebeam");
578 + #endif
579          if (hp->bl[i] == NULL)
580                  return(0);
581                                          /* check for additions */
582          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
583 <        if (nchanged) {
584 <                hdgetbi(hp, i);                 /* allocate a file position */
455 <                errno = 0;
456 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
457 <                        error(SYSTEM, "cannot seek on holodeck file");
458 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
459 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
460 <                        hp->fd = -1;            /* avoid recursive error */
461 <                        error(SYSTEM, "write error in hdfreebeam");
462 <                }
463 <        }
583 >        if (nchanged)
584 >                hdsyncbeam(hp, i);              /* write new fragment */
585          blglob(hp)->nrm -= hp->bl[i]->nrm;
586          free((char *)hp->bl[i]);                /* free memory */
587          hp->bl[i] = NULL;
# Line 474 | Line 595 | register HOLO  *hp;
595   register int    i;
596   {
597          static BEAM     emptybeam;
598 <        int     nchanged, n;
598 >        int     nchanged;
599  
600          if (hp == NULL) {               /* clobber all holodecks */
601                  nchanged = 0;
# Line 493 | Line 614 | register int   i;
614   #endif
615                  return(nchanged);
616          }
617 + #ifdef DEBUG
618          if (i < 1 | i > nbeams(hp))
619                  error(CONSISTENCY, "bad beam index to hdkillbeam");
620 + #endif
621          if (hp->bl[i] != NULL) {        /* free memory */
622                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
623                  free((char *)hp->bl[i]);
# Line 502 | Line 625 | register int   i;
625                  nchanged = hp->bi[i].nrd;
626          if (hp->bi[i].nrd) {            /* free file fragment */
627                  hp->bl[i] = &emptybeam;
628 <                hdgetbi(hp, i);
628 >                hdsyncbeam(hp, i);
629          }
630          hp->bl[i] = NULL;
631          return(nchanged);
632   }
633  
634  
635 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
636 < register HOLO   *ha[];                  /* section list (NULL terminated) */
637 < register int    ba[];                   /* beam index to go with section */
638 < int     n;                              /* length of arrays minus 1 */
635 > int
636 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
637 > register HDBEAMI        *hb;            /* beam list */
638 > int     nents;                          /* current list length */
639 > int     n;                              /* maximum list length */
640   register HOLO   *hp;                    /* section we're adding from */
641   {
642          register int    i, j;
519        int     nents;
520                                        /* find last entry in LRU list */
521        for (j = 0; ha[j] != NULL; j++)
522                ;
523        nents = j;
643                                          /* insert each beam from hp */
644          for (i = nbeams(hp); i > 0; i--) {
645                  if (hp->bl[i] == NULL)          /* check if loaded */
646                          continue;
647 <                if ((j = ++nents) > n)          /* grow list if we can */
647 > #if 0
648 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
649 >                        continue;
650 > #endif
651 >                if ((j = ++nents) >= n)         /* grow list if we can */
652                          nents--;
653                  for ( ; ; ) {                   /* bubble into place */
654                          if (!--j || hp->bl[i]->tick >=
655 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
656 <                                ha[j] = hp;
657 <                                ba[j] = i;
655 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
656 >                                hb[j].h = hp;
657 >                                hb[j].b = i;
658                                  break;
659                          }
660 <                        ha[j] = ha[j-1];
538 <                        ba[j] = ba[j-1];
660 >                        copystruct(hb+j, hb+(j-1));
661                  }
662          }
663 <        ha[nents] = NULL;               /* all done */
542 <        ba[nents] = 0;
663 >        return(nents);                  /* return new list length */
664   }
665  
666  
667 + int
668   hdfreecache(pct, honly)         /* free up cache space, writing changes */
669   int     pct;                            /* maximum percentage to free */
670   register HOLO   *honly;                 /* NULL means check all */
671   {
672 <        HOLO    *hp[FREEBEAMS+1];
551 <        int     bn[FREEBEAMS+1];
672 >        HDBEAMI hb[FREEBEAMS];
673          int     freetarget;
674 +        int     n;
675          register int    i;
676                                                  /* compute free target */
677          freetarget = (honly != NULL) ? blglob(honly)->nrm :
678                          hdmemuse(0)/sizeof(RAYVAL) ;
679          freetarget = freetarget*pct/100;
680 +        if (freetarget <= 0)
681 +                return(0);
682                                                  /* find least recently used */
683 <        hp[0] = NULL;
560 <        bn[0] = 0;
683 >        n = 0;
684          if (honly != NULL)
685 <                hdlrulist(hp, bn, FREEBEAMS, honly);
685 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
686          else
687                  for (i = 0; hdlist[i] != NULL; i++)
688 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
688 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
689                                                  /* free LRU beams */
690 <        for (i = 0; hp[i] != NULL; i++) {
691 <                hdfreebeam(hp[i], bn[i]);
692 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
690 >        for (i = 0; i < n; i++) {
691 >                hdfreebeam(hb[i].h, hb[i].b);
692 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
693                          break;
694          }
695 <        hdsync(honly);          /* synchronize directories as necessary */
695 >        hdsync(honly, 0);       /* synchronize directories as necessary */
696 >        return(-freetarget);    /* return how far past goal we went */
697   }
698  
699  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines