ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.11
Committed: Thu Dec 11 19:57:58 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.10: +30 -25 lines
Log Message:
made beam writing safer in the event of signals

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