ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.12
Committed: Fri Dec 12 19:52:31 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.11: +25 -12 lines
Log Message:
modified hdsync() to optionally sync all unflushed beams

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.1 int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */
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 gregl 3.4 BEAMI *fi; /* fragments, descending file position */
43 gregl 3.1 long flen; /* last known file length */
44 gregl 3.4 } *hdfrag; /* fragment lists, indexed by file descriptor */
45 gregl 3.1
46 gregl 3.4 static int nhdfrags; /* size of hdfrag array */
47 gregl 3.1
48 gregl 3.4
49 gregl 3.1 hdattach(fd) /* start tracking file fragments for some section */
50     register int fd;
51     {
52 gregl 3.4 if (fd >= nhdfrags) {
53     if (nhdfrags)
54     hdfrag = (struct fragment *)realloc((char *)hdfrag,
55     (fd+1)*sizeof(struct fragment));
56     else
57     hdfrag = (struct fragment *)malloc(
58     (fd+1)*sizeof(struct fragment));
59     if (hdfrag == NULL)
60     error(SYSTEM, "out of memory in hdattach");
61     bzero((char *)(hdfrag+nhdfrags),
62     (fd+1-nhdfrags)*sizeof(struct fragment));
63     nhdfrags = fd+1;
64 gregl 3.1 }
65 gregl 3.4 hdfrag[fd].nlinks++;
66     hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */
67 gregl 3.1 }
68    
69    
70     /* Do we need a routine to locate file fragments given known occupants? */
71    
72    
73     hdrelease(fd) /* stop tracking file fragments for some section */
74     register int fd;
75     {
76 gregl 3.8 if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
77 gregl 3.1 return;
78 gregl 3.4 if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
79     free((char *)hdfrag[fd].fi);
80     hdfrag[fd].fi = NULL;
81     hdfrag[fd].nfrags = 0;
82 gregl 3.1 }
83     }
84    
85    
86 gregl 3.6 markdirty(hp) /* mark holodeck section directory dirty */
87     register HOLO *hp;
88     {
89     static BEAMI smudge = {0, -1};
90    
91     if (hp->dirty) /* already marked? */
92     return;
93     hp->dirty = 1;
94     if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
95     || write(hp->fd, (char *)&smudge,
96     sizeof(BEAMI)) != sizeof(BEAMI))
97     error(SYSTEM, "seek/write error in markdirty");
98     }
99    
100    
101 gregl 3.1 HOLO *
102     hdinit(fd, hproto) /* initialize a holodeck section in a file */
103     int fd; /* corresponding file descriptor */
104     HDGRID *hproto; /* holodeck section grid */
105     {
106     long fpos;
107     register HOLO *hp;
108     register int n;
109     /* prepare for system errors */
110     errno = 0;
111     if ((fpos = lseek(fd, 0L, 1)) < 0)
112     error(SYSTEM, "cannot determine holodeck file position");
113     if (hproto == NULL) { /* assume we're loading it */
114     HDGRID hpr;
115     /* load header */
116     if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID))
117     error(SYSTEM, "cannot load holodeck header");
118     /* allocate grid */
119     if ((hp = hdalloc(&hpr)) == NULL)
120     goto memerr;
121     /* load beam directory */
122     n = nbeams(hp)*sizeof(BEAMI);
123     if (read(fd, (char *)(hp->bi+1), n) != n)
124     error(SYSTEM, "failure loading holodeck directory");
125 gregl 3.6 /* check that it's clean */
126     if (hp->bi[nbeams(hp)].fo < 0)
127     error(USER, "dirty holodeck section");
128 gregl 3.1 } else { /* assume we're creating it */
129     if ((hp = hdalloc(hproto)) == NULL)
130     goto memerr;
131     /* write header and skeleton */
132     n = nbeams(hp)*sizeof(BEAMI);
133     if (write(fd, (char *)hproto, sizeof(HDGRID)) !=
134     sizeof(HDGRID) ||
135     write(fd, (char *)(hp->bi+1), n) != n)
136     error(SYSTEM, "cannot write header to holodeck file");
137     }
138     hp->fd = fd;
139     hp->dirty = 0;
140     biglob(hp)->fo = fpos + sizeof(HDGRID);
141     biglob(hp)->nrd = 0; /* count rays on disk */
142     for (n = nbeams(hp); n > 0; n--)
143     biglob(hp)->nrd += hp->bi[n].nrd;
144     /* add to holodeck list */
145     for (n = 0; n < HDMAX; n++)
146     if (hdlist[n] == NULL) {
147     hdlist[n] = hp;
148     break;
149     }
150     /* start tracking fragments (last) */
151     hdattach(fd);
152     /* all done */
153     return(hp);
154     memerr:
155     error(SYSTEM, "cannot allocate holodeck grid");
156     }
157    
158    
159     int
160 gregl 3.12 hdsync(hp, all) /* update beams and directory on disk */
161 gregl 3.1 register HOLO *hp;
162 gregl 3.12 int all;
163 gregl 3.1 {
164     register int j, n;
165    
166 gregl 3.12 if (hp == NULL) { /* do all holodecks */
167 gregl 3.1 n = 0;
168     for (j = 0; hdlist[j] != NULL; j++)
169 gregl 3.12 n += hdsync(hdlist[j], all);
170 gregl 3.1 return(n);
171     }
172 gregl 3.12 /* sync the beams */
173     for (j = all ? nbeams(hp) : 0; j > 0; j--)
174     if (hp->bl[j] != NULL)
175     hdsyncbeam(hp, j);
176     if (!hp->dirty) /* directory dirty? */
177 gregl 3.1 return(0);
178 gregl 3.3 errno = 0;
179 gregl 3.1 if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
180     error(SYSTEM, "cannot seek on holodeck file");
181     n = nbeams(hp)*sizeof(BEAMI);
182     if (write(hp->fd, (char *)(hp->bi+1), n) != n)
183     error(SYSTEM, "cannot update holodeck section directory");
184     hp->dirty = 0;
185     return(1);
186     }
187    
188    
189     long
190     hdmemuse(all) /* return memory usage (in bytes) */
191     int all; /* include overhead (painful) */
192     {
193     long total = 0;
194     register int i, j;
195    
196     for (j = 0; hdlist[j] != NULL; j++) {
197     total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
198     if (all) {
199     total += sizeof(HOLO) + sizeof(BEAM *) +
200     nbeams(hdlist[j]) *
201     (sizeof(BEAM *)+sizeof(BEAMI));
202     for (i = nbeams(hdlist[j]); i > 0; i--)
203     if (hdlist[j]->bl[i] != NULL)
204     total += sizeof(BEAM);
205     }
206     }
207     if (all)
208 gregl 3.4 for (j = 0; j < nhdfrags; j++) {
209     total += sizeof(struct fragment);
210     if (hdfrag[j].nfrags)
211     total += FRAGBLK*sizeof(BEAMI) *
212     ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
213     }
214 gregl 3.1 return(total);
215 gregl 3.8 }
216    
217    
218     long
219     hdfilen(fd) /* return file length for fd */
220     int fd;
221     {
222     long fpos, flen;
223    
224     if (fd < 0)
225     return(-1);
226     if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
227     if ((fpos = lseek(fd, 0L, 1)) < 0)
228     return(-1);
229     flen = lseek(fd, 0L, 2);
230     lseek(fd, fpos, 0);
231     return(flen);
232     }
233     return(hdfrag[fd].flen);
234 gregl 3.1 }
235    
236    
237     long
238     hdfiluse(fd, all) /* compute file usage (in bytes) */
239     int fd; /* open file descriptor to check */
240     int all; /* include overhead and unflushed data */
241     {
242     long total = 0;
243     register int i, j;
244    
245     for (j = 0; hdlist[j] != NULL; j++) {
246     if (hdlist[j]->fd != fd)
247     continue;
248     total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
249     if (all) {
250     for (i = nbeams(hdlist[j]); i > 0; i--)
251     if (hdlist[j]->bl[i] != NULL)
252     total += sizeof(RAYVAL) *
253     (hdlist[j]->bl[i]->nrm -
254     hdlist[j]->bi[i].nrd);
255     total += sizeof(HDGRID) +
256     nbeams(hdlist[j])*sizeof(BEAMI);
257     }
258     }
259     return(total); /* does not include fragments */
260     }
261    
262    
263     RAYVAL *
264     hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */
265     register HOLO *hp;
266     register int i;
267     int nr; /* number of new rays desired */
268     {
269     RAYVAL *p;
270     int n;
271    
272     if (nr <= 0)
273     return(NULL);
274     if (i < 1 | i > nbeams(hp))
275     error(CONSISTENCY, "bad beam index given to hdnewrays");
276     if (hp->bl[i] != NULL)
277     hp->bl[i]->tick = hdclock; /* preempt swap */
278     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
279     hdfreecache(PCTFREE, NULL); /* free some space */
280 gregl 3.3 errno = 0;
281 gregl 3.1 if (hp->bl[i] == NULL) { /* allocate (and load) */
282     n = hp->bi[i].nrd + nr;
283     if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
284     goto memerr;
285     blglob(hp)->nrm += n;
286     if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
287     if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
288     error(SYSTEM, "seek error on holodeck file");
289     n *= sizeof(RAYVAL);
290     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
291     error(SYSTEM,
292     "error reading beam from holodeck file");
293     }
294     } else { /* just grow in memory */
295     hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
296     hdbsiz(hp->bl[i]->nrm + nr) );
297     if (hp->bl[i] == NULL)
298     goto memerr;
299     blglob(hp)->nrm += nr;
300     }
301     p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
302     hp->bl[i]->nrm += nr; /* update in-core structure */
303     bzero((char *)p, nr*sizeof(RAYVAL));
304     hp->bl[i]->tick = ++hdclock; /* update LRU clock */
305     blglob(hp)->tick = hdclock;
306     return(p); /* point to new rays */
307     memerr:
308     error(SYSTEM, "out of memory in hdnewrays");
309     }
310    
311    
312     BEAM *
313     hdgetbeam(hp, i) /* get beam (from file if necessary) */
314     register HOLO *hp;
315     register int i;
316     {
317     register int n;
318    
319     if (i < 1 | i > nbeams(hp))
320     error(CONSISTENCY, "bad beam index given to hdgetbeam");
321     if (hp->bl[i] == NULL) { /* load from disk */
322     if (!(n = hp->bi[i].nrd))
323     return(NULL);
324     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
325     hdfreecache(PCTFREE, NULL); /* get free space */
326     errno = 0;
327     if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
328     error(SYSTEM, "cannot allocate memory for beam");
329     blglob(hp)->nrm += hp->bl[i]->nrm = n;
330     if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
331     error(SYSTEM, "seek error on holodeck file");
332     n *= sizeof(RAYVAL);
333     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
334     error(SYSTEM, "error reading beam from holodeck file");
335     }
336     hp->bl[i]->tick = ++hdclock; /* update LRU clock */
337     blglob(hp)->tick = hdclock;
338     return(hp->bl[i]);
339     }
340    
341    
342     int
343 gregl 3.12 hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */
344 gregl 3.1 register HOLO *hp;
345     register int i;
346     {
347 gregl 3.12 unsigned int nrays;
348 gregl 3.11 long nfo;
349     unsigned int n;
350 gregl 3.12 #ifdef DEBUG
351     if (i < 1 | i > nbeams(hp))
352     error(CONSISTENCY, "bad beam index in hdsyncbeam");
353     #endif
354 gregl 3.11 /* is current fragment OK? */
355 gregl 3.12 if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
356 gregl 3.1 return(0);
357 gregl 3.11 /* check file status */
358     if (hp->dirty < 0)
359     return(-1);
360 gregl 3.1
361 gregl 3.4 if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
362 gregl 3.1 hp->bi[i].fo = lseek(hp->fd, 0L, 2);
363    
364     else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
365 gregl 3.4 hdfrag[hp->fd].flen) /* EOF special case */
366 gregl 3.11 hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
367 gregl 3.1
368     else { /* general case */
369 gregl 3.4 register struct fragment *f = &hdfrag[hp->fd];
370 gregl 3.1 register int j, k;
371     /* relinquish old fragment */
372 gregl 3.2 if (hp->bi[i].nrd) {
373 gregl 3.5 j = f->nfrags++;
374 gregl 3.4 #ifdef MAXFRAG
375 gregl 3.5 if (j >= MAXFRAG-1)
376 gregl 3.2 f->nfrags--;
377 gregl 3.4 #endif
378 gregl 3.5 if (j % FRAGBLK == 0) { /* more frag. space */
379 gregl 3.4 if (f->fi == NULL)
380     f->fi = (BEAMI *)malloc(
381     FRAGBLK*sizeof(BEAMI));
382     else
383     f->fi = (BEAMI *)realloc((char *)f->fi,
384 gregl 3.5 (j+FRAGBLK)*sizeof(BEAMI));
385 gregl 3.4 if (f->fi == NULL)
386     error(SYSTEM,
387 gregl 3.12 "out of memory in hdsyncbeam");
388 gregl 3.4 }
389 gregl 3.5 for ( ; ; j--) { /* insert in descending list */
390     if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
391 gregl 3.2 f->fi[j].fo = hp->bi[i].fo;
392     f->fi[j].nrd = hp->bi[i].nrd;
393     break;
394     }
395     copystruct(f->fi+j, f->fi+j-1);
396 gregl 3.1 }
397     /* coalesce adjacent fragments */
398 gregl 3.2 for (j = k = 0; k < f->nfrags; j++, k++) {
399 gregl 3.3 if (k > j)
400 gregl 3.2 copystruct(f->fi+j, f->fi+k);
401 gregl 3.3 while (k+1 < f->nfrags && f->fi[k+1].fo +
402     f->fi[k+1].nrd*sizeof(RAYVAL)
403     == f->fi[j].fo) {
404 gregl 3.2 f->fi[j].fo -=
405     f->fi[++k].nrd*sizeof(RAYVAL);
406 gregl 3.3 f->fi[j].nrd += f->fi[k].nrd;
407     }
408 gregl 3.2 }
409     f->nfrags = j;
410 gregl 3.1 }
411     k = -1; /* find closest-sized fragment */
412 gregl 3.11 for (j = nrays ? f->nfrags : 0; j-- > 0; )
413 gregl 3.1 if (f->fi[j].nrd >= nrays &&
414 gregl 3.3 (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
415     if (f->fi[k=j].nrd == nrays)
416     break;
417 gregl 3.1 if (k < 0) { /* no fragment -- extend file */
418 gregl 3.11 nfo = f->flen;
419 gregl 3.1 f->flen += nrays*sizeof(RAYVAL);
420     } else { /* else use fragment */
421 gregl 3.11 nfo = f->fi[k].fo;
422 gregl 3.1 if (f->fi[k].nrd == nrays) { /* delete fragment */
423     f->nfrags--;
424     for (j = k; j < f->nfrags; j++)
425     copystruct(f->fi+j, f->fi+j+1);
426     } else { /* else shrink it */
427     f->fi[k].fo += nrays*sizeof(RAYVAL);
428     f->fi[k].nrd -= nrays;
429     }
430     }
431     }
432 gregl 3.11 if (nrays) { /* write the new fragment */
433     errno = 0;
434     if (lseek(hp->fd, nfo, 0) < 0)
435     error(SYSTEM, "cannot seek on holodeck file");
436     n = hp->bl[i]->nrm * sizeof(RAYVAL);
437     if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
438     hp->dirty = -1; /* avoid recursive error */
439 gregl 3.12 error(SYSTEM, "write error in hdsyncbeam");
440 gregl 3.11 }
441     }
442 gregl 3.1 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
443     hp->bi[i].nrd = nrays;
444 gregl 3.11 hp->bi[i].fo = nfo;
445 gregl 3.6 markdirty(hp); /* section directory now out of date */
446 gregl 3.1 return(1);
447     }
448    
449    
450     int
451     hdfreebeam(hp, i) /* free beam, writing if dirty */
452     register HOLO *hp;
453     register int i;
454     {
455 gregl 3.11 int nchanged;
456 gregl 3.1
457     if (hp == NULL) { /* clear all holodecks */
458     nchanged = 0;
459     for (i = 0; hdlist[i] != NULL; i++)
460     nchanged += hdfreebeam(hdlist[i], 0);
461     return(nchanged);
462     }
463     if (i == 0) { /* clear entire holodeck */
464     nchanged = 0;
465 gregl 3.10 for (i = nbeams(hp); i > 0; i--)
466     if (hp->bl[i] != NULL)
467     nchanged += hdfreebeam(hp, i);
468 gregl 3.1 return(nchanged);
469     }
470 gregl 3.12 #ifdef DEBUG
471 gregl 3.1 if (i < 1 | i > nbeams(hp))
472     error(CONSISTENCY, "bad beam index to hdfreebeam");
473 gregl 3.12 #endif
474 gregl 3.1 if (hp->bl[i] == NULL)
475     return(0);
476     /* check for additions */
477     nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
478 gregl 3.11 if (nchanged)
479 gregl 3.12 hdsyncbeam(hp, i); /* write new fragment */
480 gregl 3.1 blglob(hp)->nrm -= hp->bl[i]->nrm;
481     free((char *)hp->bl[i]); /* free memory */
482     hp->bl[i] = NULL;
483     return(nchanged);
484     }
485    
486    
487 gregl 3.10 int
488     hdkillbeam(hp, i) /* delete beam from holodeck */
489     register HOLO *hp;
490     register int i;
491     {
492     static BEAM emptybeam;
493 gregl 3.11 int nchanged;
494 gregl 3.10
495     if (hp == NULL) { /* clobber all holodecks */
496     nchanged = 0;
497     for (i = 0; hdlist[i] != NULL; i++)
498     nchanged += hdkillbeam(hdlist[i], 0);
499     return(nchanged);
500     }
501     if (i == 0) { /* clobber entire holodeck */
502     nchanged = 0;
503     for (i = nbeams(hp); i > 0; i--)
504     if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
505     nchanged += hdkillbeam(hp, i);
506     #ifdef DEBUG
507     if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
508     error(CONSISTENCY, "bad beam count in hdkillbeam");
509     #endif
510     return(nchanged);
511     }
512 gregl 3.12 #ifdef DEBUG
513 gregl 3.10 if (i < 1 | i > nbeams(hp))
514     error(CONSISTENCY, "bad beam index to hdkillbeam");
515 gregl 3.12 #endif
516 gregl 3.10 if (hp->bl[i] != NULL) { /* free memory */
517     blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
518     free((char *)hp->bl[i]);
519     } else
520     nchanged = hp->bi[i].nrd;
521     if (hp->bi[i].nrd) { /* free file fragment */
522     hp->bl[i] = &emptybeam;
523 gregl 3.12 hdsyncbeam(hp, i);
524 gregl 3.10 }
525     hp->bl[i] = NULL;
526     return(nchanged);
527     }
528    
529    
530 gregl 3.1 hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */
531     register HOLO *ha[]; /* section list (NULL terminated) */
532     register int ba[]; /* beam index to go with section */
533     int n; /* length of arrays minus 1 */
534     register HOLO *hp; /* section we're adding from */
535     {
536     register int i, j;
537     int nents;
538     /* find last entry in LRU list */
539     for (j = 0; ha[j] != NULL; j++)
540     ;
541     nents = j;
542     /* insert each beam from hp */
543 gregl 3.10 for (i = nbeams(hp); i > 0; i--) {
544 gregl 3.1 if (hp->bl[i] == NULL) /* check if loaded */
545     continue;
546     if ((j = ++nents) > n) /* grow list if we can */
547     nents--;
548     for ( ; ; ) { /* bubble into place */
549     if (!--j || hp->bl[i]->tick >=
550     ha[j-1]->bl[ba[j-1]]->tick) {
551     ha[j] = hp;
552     ba[j] = i;
553     break;
554     }
555     ha[j] = ha[j-1];
556     ba[j] = ba[j-1];
557     }
558     }
559     ha[nents] = NULL; /* all done */
560     ba[nents] = 0;
561     }
562    
563    
564     hdfreecache(pct, honly) /* free up cache space, writing changes */
565     int pct; /* maximum percentage to free */
566     register HOLO *honly; /* NULL means check all */
567     {
568     HOLO *hp[FREEBEAMS+1];
569     int bn[FREEBEAMS+1];
570     int freetarget;
571     register int i;
572     /* compute free target */
573     freetarget = (honly != NULL) ? blglob(honly)->nrm :
574     hdmemuse(0)/sizeof(RAYVAL) ;
575     freetarget = freetarget*pct/100;
576     /* find least recently used */
577     hp[0] = NULL;
578     bn[0] = 0;
579     if (honly != NULL)
580     hdlrulist(hp, bn, FREEBEAMS, honly);
581     else
582     for (i = 0; hdlist[i] != NULL; i++)
583     hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
584     /* free LRU beams */
585     for (i = 0; hp[i] != NULL; i++) {
586     hdfreebeam(hp[i], bn[i]);
587     if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
588     break;
589     }
590 gregl 3.12 hdsync(honly, 0); /* synchronize directories as necessary */
591 gregl 3.1 }
592    
593    
594     hddone(hp) /* clean up holodeck section and free */
595     register HOLO *hp; /* NULL means clean up all */
596     {
597     register int i;
598    
599     if (hp == NULL) { /* NULL means clean up everything */
600     while (hdlist[0] != NULL)
601     hddone(hdlist[0]);
602     return;
603     }
604     /* flush all data and free memory */
605     hdflush(hp);
606     /* release fragment resources */
607     hdrelease(hp->fd);
608     /* remove hp from active list */
609     for (i = 0; hdlist[i] != NULL; i++)
610     if (hdlist[i] == hp) {
611     while ((hdlist[i] = hdlist[i+1]) != NULL)
612     i++;
613     break;
614     }
615     free((char *)hp->bl); /* free beam list */
616     free((char *)hp); /* free holodeck struct */
617     }