ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.14
Committed: Thu Dec 18 15:12:23 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.13: +90 -34 lines
Log Message:
wrote hdloadbeams() routine to load many beams at once

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Routines for managing holodeck files
9     *
10     * 9/30/97 GWLarson
11     */
12    
13     #include "holo.h"
14    
15     #ifndef CACHESIZE
16     #define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */
17     #endif
18     #ifndef FREEBEAMS
19 gregl 3.5 #define FREEBEAMS 512 /* maximum beams to free at a time */
20 gregl 3.1 #endif
21     #ifndef PCTFREE
22 gregl 3.5 #define PCTFREE 20 /* maximum fraction to free (%) */
23 gregl 3.1 #endif
24    
25 gregl 3.7 /* define MAXFRAG if you want to limit fragment tracking memory */
26    
27 gregl 3.6 #ifndef BSD
28     #define write writebuf /* safe i/o routines */
29     #define read readbuf
30     #endif
31 gregl 3.4
32     #define FRAGBLK 64 /* number of fragments to allocate at a time */
33    
34 gregl 3.14 unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */
35 gregl 3.1 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 gregl 3.13 short writerr; /* write error encountered */
42     int nfrags; /* number of known fragments */
43 gregl 3.4 BEAMI *fi; /* fragments, descending file position */
44 gregl 3.1 long flen; /* last known file length */
45 gregl 3.4 } *hdfrag; /* fragment lists, indexed by file descriptor */
46 gregl 3.1
47 gregl 3.4 static int nhdfrags; /* size of hdfrag array */
48 gregl 3.1
49 gregl 3.4
50 gregl 3.1 hdattach(fd) /* start tracking file fragments for some section */
51     register int fd;
52     {
53 gregl 3.4 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 gregl 3.1 }
66 gregl 3.4 hdfrag[fd].nlinks++;
67     hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */
68 gregl 3.1 }
69    
70    
71     /* Do we need a routine to locate file fragments given known occupants? */
72    
73    
74     hdrelease(fd) /* stop tracking file fragments for some section */
75     register int fd;
76     {
77 gregl 3.8 if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
78 gregl 3.1 return;
79 gregl 3.4 if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
80     free((char *)hdfrag[fd].fi);
81     hdfrag[fd].fi = NULL;
82     hdfrag[fd].nfrags = 0;
83 gregl 3.1 }
84     }
85    
86    
87 gregl 3.6 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 gregl 3.1 HOLO *
103     hdinit(fd, hproto) /* initialize a holodeck section in a file */
104     int fd; /* corresponding file descriptor */
105     HDGRID *hproto; /* holodeck section grid */
106     {
107     long fpos;
108     register HOLO *hp;
109     register int n;
110     /* prepare for system errors */
111     errno = 0;
112     if ((fpos = lseek(fd, 0L, 1)) < 0)
113     error(SYSTEM, "cannot determine holodeck file position");
114     if (hproto == NULL) { /* assume we're loading it */
115     HDGRID hpr;
116     /* load header */
117     if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID))
118     error(SYSTEM, "cannot load holodeck header");
119     /* allocate grid */
120     if ((hp = hdalloc(&hpr)) == NULL)
121     goto memerr;
122     /* load beam directory */
123     n = nbeams(hp)*sizeof(BEAMI);
124     if (read(fd, (char *)(hp->bi+1), n) != n)
125     error(SYSTEM, "failure loading holodeck directory");
126 gregl 3.6 /* check that it's clean */
127     if (hp->bi[nbeams(hp)].fo < 0)
128     error(USER, "dirty holodeck section");
129 gregl 3.1 } else { /* assume we're creating it */
130     if ((hp = hdalloc(hproto)) == NULL)
131     goto memerr;
132     /* write header and skeleton */
133     n = nbeams(hp)*sizeof(BEAMI);
134     if (write(fd, (char *)hproto, sizeof(HDGRID)) !=
135     sizeof(HDGRID) ||
136     write(fd, (char *)(hp->bi+1), n) != n)
137     error(SYSTEM, "cannot write header to holodeck file");
138     }
139     hp->fd = fd;
140     hp->dirty = 0;
141     biglob(hp)->fo = fpos + sizeof(HDGRID);
142 gregl 3.13 /* 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 gregl 3.1 /* add to holodeck list */
154     for (n = 0; n < HDMAX; n++)
155     if (hdlist[n] == NULL) {
156     hdlist[n] = hp;
157     break;
158     }
159     /* all done */
160     return(hp);
161     memerr:
162     error(SYSTEM, "cannot allocate holodeck grid");
163     }
164    
165    
166     int
167 gregl 3.12 hdsync(hp, all) /* update beams and directory on disk */
168 gregl 3.1 register HOLO *hp;
169 gregl 3.12 int all;
170 gregl 3.1 {
171     register int j, n;
172    
173 gregl 3.12 if (hp == NULL) { /* do all holodecks */
174 gregl 3.1 n = 0;
175     for (j = 0; hdlist[j] != NULL; j++)
176 gregl 3.12 n += hdsync(hdlist[j], all);
177 gregl 3.1 return(n);
178     }
179 gregl 3.12 /* sync the beams */
180     for (j = all ? nbeams(hp) : 0; j > 0; j--)
181     if (hp->bl[j] != NULL)
182     hdsyncbeam(hp, j);
183 gregl 3.13 if (!hp->dirty) /* directory clean? */
184 gregl 3.1 return(0);
185 gregl 3.3 errno = 0;
186 gregl 3.1 if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
187     error(SYSTEM, "cannot seek on holodeck file");
188     n = nbeams(hp)*sizeof(BEAMI);
189     if (write(hp->fd, (char *)(hp->bi+1), n) != n)
190     error(SYSTEM, "cannot update holodeck section directory");
191     hp->dirty = 0;
192     return(1);
193     }
194    
195    
196 gregl 3.14 unsigned
197 gregl 3.1 hdmemuse(all) /* return memory usage (in bytes) */
198     int all; /* include overhead (painful) */
199     {
200     long total = 0;
201     register int i, j;
202    
203     for (j = 0; hdlist[j] != NULL; j++) {
204     total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
205     if (all) {
206     total += sizeof(HOLO) + sizeof(BEAM *) +
207     nbeams(hdlist[j]) *
208     (sizeof(BEAM *)+sizeof(BEAMI));
209     for (i = nbeams(hdlist[j]); i > 0; i--)
210     if (hdlist[j]->bl[i] != NULL)
211     total += sizeof(BEAM);
212     }
213     }
214     if (all)
215 gregl 3.4 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 gregl 3.1 return(total);
222 gregl 3.8 }
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 gregl 3.1 }
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 */
248     {
249     long total = 0;
250     register int i, j;
251    
252     for (j = 0; hdlist[j] != NULL; j++) {
253     if (hdlist[j]->fd != fd)
254     continue;
255     total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
256     if (all) {
257     for (i = nbeams(hdlist[j]); i > 0; i--)
258     if (hdlist[j]->bl[i] != NULL)
259     total += sizeof(RAYVAL) *
260     (hdlist[j]->bl[i]->nrm -
261     hdlist[j]->bi[i].nrd);
262     total += sizeof(HDGRID) +
263     nbeams(hdlist[j])*sizeof(BEAMI);
264     }
265     }
266     return(total); /* does not include fragments */
267     }
268    
269    
270     RAYVAL *
271     hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */
272     register HOLO *hp;
273     register int i;
274     int nr; /* number of new rays desired */
275     {
276     RAYVAL *p;
277     int n;
278    
279     if (nr <= 0)
280     return(NULL);
281     if (i < 1 | i > nbeams(hp))
282     error(CONSISTENCY, "bad beam index given to hdnewrays");
283     if (hp->bl[i] != NULL)
284     hp->bl[i]->tick = hdclock; /* preempt swap */
285     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
286     hdfreecache(PCTFREE, NULL); /* free some space */
287 gregl 3.3 errno = 0;
288 gregl 3.1 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)
291     goto memerr;
292     blglob(hp)->nrm += n;
293     if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
294     if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
295     error(SYSTEM, "seek error on holodeck file");
296     n *= sizeof(RAYVAL);
297     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
298     error(SYSTEM,
299     "error reading beam from holodeck file");
300     }
301     } else { /* just grow in memory */
302     hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
303     hdbsiz(hp->bl[i]->nrm + nr) );
304     if (hp->bl[i] == NULL)
305     goto memerr;
306     blglob(hp)->nrm += nr;
307     }
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 gregl 3.14 hp->bl[i]->tick = hdclock; /* update LRU clock */
312     blglob(hp)->tick = hdclock++;
313 gregl 3.1 return(p); /* point to new rays */
314     memerr:
315     error(SYSTEM, "out of memory in hdnewrays");
316     }
317    
318    
319     BEAM *
320     hdgetbeam(hp, i) /* get beam (from file if necessary) */
321     register HOLO *hp;
322     register int i;
323     {
324     register int n;
325    
326     if (i < 1 | i > nbeams(hp))
327     error(CONSISTENCY, "bad beam index given to hdgetbeam");
328     if (hp->bl[i] == NULL) { /* load from disk */
329     if (!(n = hp->bi[i].nrd))
330     return(NULL);
331     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
332     hdfreecache(PCTFREE, NULL); /* get free space */
333     errno = 0;
334     if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
335     error(SYSTEM, "cannot allocate memory for beam");
336     blglob(hp)->nrm += hp->bl[i]->nrm = n;
337     if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
338     error(SYSTEM, "seek error on holodeck file");
339     n *= sizeof(RAYVAL);
340     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
341     error(SYSTEM, "error reading beam from holodeck file");
342     }
343 gregl 3.14 hp->bl[i]->tick = hdclock; /* update LRU clock */
344     blglob(hp)->tick = hdclock++;
345 gregl 3.1 return(hp->bl[i]);
346     }
347    
348    
349     int
350 gregl 3.14 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 gregl 3.12 hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */
406 gregl 3.1 register HOLO *hp;
407     register int i;
408     {
409 gregl 3.12 unsigned int nrays;
410 gregl 3.11 long nfo;
411     unsigned int n;
412 gregl 3.13 /* check file status */
413     if (hdfrag[hp->fd].writerr)
414     return(-1);
415 gregl 3.12 #ifdef DEBUG
416     if (i < 1 | i > nbeams(hp))
417     error(CONSISTENCY, "bad beam index in hdsyncbeam");
418     #endif
419 gregl 3.11 /* is current fragment OK? */
420 gregl 3.12 if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
421 gregl 3.1 return(0);
422 gregl 3.13 /* locate fragment */
423 gregl 3.4 if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
424 gregl 3.1 hp->bi[i].fo = lseek(hp->fd, 0L, 2);
425    
426     else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
427 gregl 3.4 hdfrag[hp->fd].flen) /* EOF special case */
428 gregl 3.11 hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
429 gregl 3.1
430     else { /* general case */
431 gregl 3.4 register struct fragment *f = &hdfrag[hp->fd];
432 gregl 3.1 register int j, k;
433     /* relinquish old fragment */
434 gregl 3.2 if (hp->bi[i].nrd) {
435 gregl 3.5 j = f->nfrags++;
436 gregl 3.4 #ifdef MAXFRAG
437 gregl 3.5 if (j >= MAXFRAG-1)
438 gregl 3.2 f->nfrags--;
439 gregl 3.4 #endif
440 gregl 3.5 if (j % FRAGBLK == 0) { /* more frag. space */
441 gregl 3.4 if (f->fi == NULL)
442     f->fi = (BEAMI *)malloc(
443     FRAGBLK*sizeof(BEAMI));
444     else
445     f->fi = (BEAMI *)realloc((char *)f->fi,
446 gregl 3.5 (j+FRAGBLK)*sizeof(BEAMI));
447 gregl 3.4 if (f->fi == NULL)
448     error(SYSTEM,
449 gregl 3.12 "out of memory in hdsyncbeam");
450 gregl 3.4 }
451 gregl 3.5 for ( ; ; j--) { /* insert in descending list */
452     if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
453 gregl 3.2 f->fi[j].fo = hp->bi[i].fo;
454     f->fi[j].nrd = hp->bi[i].nrd;
455     break;
456     }
457 gregl 3.14 copystruct(f->fi+j, f->fi+(j-1));
458 gregl 3.1 }
459     /* coalesce adjacent fragments */
460 gregl 3.2 for (j = k = 0; k < f->nfrags; j++, k++) {
461 gregl 3.3 if (k > j)
462 gregl 3.2 copystruct(f->fi+j, f->fi+k);
463 gregl 3.3 while (k+1 < f->nfrags && f->fi[k+1].fo +
464     f->fi[k+1].nrd*sizeof(RAYVAL)
465     == f->fi[j].fo) {
466 gregl 3.2 f->fi[j].fo -=
467     f->fi[++k].nrd*sizeof(RAYVAL);
468 gregl 3.3 f->fi[j].nrd += f->fi[k].nrd;
469     }
470 gregl 3.2 }
471     f->nfrags = j;
472 gregl 3.1 }
473     k = -1; /* find closest-sized fragment */
474 gregl 3.11 for (j = nrays ? f->nfrags : 0; j-- > 0; )
475 gregl 3.1 if (f->fi[j].nrd >= nrays &&
476 gregl 3.3 (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
477     if (f->fi[k=j].nrd == nrays)
478     break;
479 gregl 3.1 if (k < 0) { /* no fragment -- extend file */
480 gregl 3.11 nfo = f->flen;
481 gregl 3.1 f->flen += nrays*sizeof(RAYVAL);
482     } else { /* else use fragment */
483 gregl 3.11 nfo = f->fi[k].fo;
484 gregl 3.1 if (f->fi[k].nrd == nrays) { /* delete fragment */
485     f->nfrags--;
486     for (j = k; j < f->nfrags; j++)
487 gregl 3.14 copystruct(f->fi+j, f->fi+(j+1));
488 gregl 3.1 } else { /* else shrink it */
489     f->fi[k].fo += nrays*sizeof(RAYVAL);
490     f->fi[k].nrd -= nrays;
491     }
492     }
493     }
494 gregl 3.11 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 gregl 3.13 hdfrag[hp->fd].writerr++;
501     hdsync(hp, 0); /* sync directory */
502 gregl 3.12 error(SYSTEM, "write error in hdsyncbeam");
503 gregl 3.11 }
504     }
505 gregl 3.1 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
506     hp->bi[i].nrd = nrays;
507 gregl 3.11 hp->bi[i].fo = nfo;
508 gregl 3.6 markdirty(hp); /* section directory now out of date */
509 gregl 3.1 return(1);
510     }
511    
512    
513     int
514     hdfreebeam(hp, i) /* free beam, writing if dirty */
515     register HOLO *hp;
516     register int i;
517     {
518 gregl 3.11 int nchanged;
519 gregl 3.1
520     if (hp == NULL) { /* clear all holodecks */
521     nchanged = 0;
522     for (i = 0; hdlist[i] != NULL; i++)
523     nchanged += hdfreebeam(hdlist[i], 0);
524     return(nchanged);
525     }
526 gregl 3.13 if (hdfrag[hp->fd].writerr) /* check for file error */
527     return(0);
528 gregl 3.1 if (i == 0) { /* clear entire holodeck */
529     nchanged = 0;
530 gregl 3.10 for (i = nbeams(hp); i > 0; i--)
531     if (hp->bl[i] != NULL)
532     nchanged += hdfreebeam(hp, i);
533 gregl 3.1 return(nchanged);
534     }
535 gregl 3.12 #ifdef DEBUG
536 gregl 3.1 if (i < 1 | i > nbeams(hp))
537     error(CONSISTENCY, "bad beam index to hdfreebeam");
538 gregl 3.12 #endif
539 gregl 3.1 if (hp->bl[i] == NULL)
540     return(0);
541     /* check for additions */
542     nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
543 gregl 3.11 if (nchanged)
544 gregl 3.12 hdsyncbeam(hp, i); /* write new fragment */
545 gregl 3.1 blglob(hp)->nrm -= hp->bl[i]->nrm;
546     free((char *)hp->bl[i]); /* free memory */
547     hp->bl[i] = NULL;
548     return(nchanged);
549     }
550    
551    
552 gregl 3.10 int
553     hdkillbeam(hp, i) /* delete beam from holodeck */
554     register HOLO *hp;
555     register int i;
556     {
557     static BEAM emptybeam;
558 gregl 3.11 int nchanged;
559 gregl 3.10
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 gregl 3.12 #ifdef DEBUG
578 gregl 3.10 if (i < 1 | i > nbeams(hp))
579     error(CONSISTENCY, "bad beam index to hdkillbeam");
580 gregl 3.12 #endif
581 gregl 3.10 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 gregl 3.12 hdsyncbeam(hp, i);
589 gregl 3.10 }
590     hp->bl[i] = NULL;
591     return(nchanged);
592     }
593    
594    
595 gregl 3.14 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 gregl 3.1 register HOLO *hp; /* section we're adding from */
601     {
602     register int i, j;
603     /* insert each beam from hp */
604 gregl 3.10 for (i = nbeams(hp); i > 0; i--) {
605 gregl 3.1 if (hp->bl[i] == NULL) /* check if loaded */
606     continue;
607 gregl 3.14 #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 gregl 3.1 nents--;
613     for ( ; ; ) { /* bubble into place */
614     if (!--j || hp->bl[i]->tick >=
615 gregl 3.14 hb[j-1].h->bl[hb[j-1].b]->tick) {
616     hb[j].h = hp;
617     hb[j].b = i;
618 gregl 3.1 break;
619     }
620 gregl 3.14 copystruct(hb+j, hb+(j-1));
621 gregl 3.1 }
622     }
623 gregl 3.14 return(nents); /* return new list length */
624 gregl 3.1 }
625    
626    
627 gregl 3.14 int
628 gregl 3.1 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 gregl 3.14 HDBEAMI hb[FREEBEAMS];
633 gregl 3.1 int freetarget;
634 gregl 3.14 int n;
635 gregl 3.1 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 gregl 3.14 if (freetarget <= 0)
641     return(0);
642 gregl 3.1 /* find least recently used */
643 gregl 3.14 n = 0;
644 gregl 3.1 if (honly != NULL)
645 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, honly);
646 gregl 3.1 else
647     for (i = 0; hdlist[i] != NULL; i++)
648 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
649 gregl 3.1 /* free LRU beams */
650 gregl 3.14 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 gregl 3.1 break;
654     }
655 gregl 3.12 hdsync(honly, 0); /* synchronize directories as necessary */
656 gregl 3.14 return(-freetarget); /* return how far past goal we went */
657 gregl 3.1 }
658    
659    
660     hddone(hp) /* clean up holodeck section and free */
661     register HOLO *hp; /* NULL means clean up all */
662     {
663     register int i;
664    
665     if (hp == NULL) { /* NULL means clean up everything */
666     while (hdlist[0] != NULL)
667     hddone(hdlist[0]);
668     return;
669     }
670     /* flush all data and free memory */
671     hdflush(hp);
672     /* release fragment resources */
673     hdrelease(hp->fd);
674     /* remove hp from active list */
675     for (i = 0; hdlist[i] != NULL; i++)
676     if (hdlist[i] == hp) {
677     while ((hdlist[i] = hdlist[i+1]) != NULL)
678     i++;
679     break;
680     }
681     free((char *)hp->bl); /* free beam list */
682     free((char *)hp); /* free holodeck struct */
683     }