ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.39
Committed: Tue Mar 9 08:40:11 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.38: +12 -10 lines
Log Message:
changed default fragment handling flags
fixed problem with inconsistent directory updating

File Contents

# User Rev Content
1 gwlarson 3.32 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2 gregl 3.1
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 gwlarson 3.35 #ifdef BIGMEM
17 gwlarson 3.38 #define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */
18 gwlarson 3.35 #else
19 gwlarson 3.38 #define CACHESIZE 5
20 gwlarson 3.35 #endif
21 gregl 3.1 #endif
22     #ifndef FREEBEAMS
23 gwlarson 3.25 #define FREEBEAMS 1500 /* maximum beams to free at a time */
24 gregl 3.1 #endif
25     #ifndef PCTFREE
26 gregl 3.21 #define PCTFREE 15 /* maximum fraction to free (%) */
27 gregl 3.1 #endif
28 gwlarson 3.31 #ifndef MAXFRAGB
29     #define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */
30 gregl 3.15 #endif
31 gwlarson 3.32 #ifndef FF_DEFAULT
32     /* when to free a beam fragment */
33 gwlarson 3.39 #define FF_DEFAULT (FF_WRITE|FF_KILL)
34 gwlarson 3.32 #endif
35 gwlarson 3.29 #ifndef MINDIRSEL
36     /* minimum directory seek length */
37     #define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI))
38     #endif
39 gregl 3.1
40 gregl 3.6 #ifndef BSD
41     #define write writebuf /* safe i/o routines */
42     #define read readbuf
43     #endif
44 gregl 3.4
45 gwlarson 3.31 #define FRAGBLK 512 /* number of fragments to allocate at a time */
46 gregl 3.4
47 gwlarson 3.32 int hdfragflags = FF_DEFAULT; /* tells when to free fragments */
48 gregl 3.14 unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */
49 gwlarson 3.32 unsigned long hdclock; /* clock value */
50 gregl 3.1
51     HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */
52    
53 gregl 3.16 static struct fraglist {
54 gregl 3.1 short nlinks; /* number of holodeck sections using us */
55 gregl 3.13 short writerr; /* write error encountered */
56     int nfrags; /* number of known fragments */
57 gregl 3.4 BEAMI *fi; /* fragments, descending file position */
58 gregl 3.1 long flen; /* last known file length */
59 gregl 3.16 } *hdfragl; /* fragment lists, indexed by file descriptor */
60 gregl 3.1
61 gregl 3.16 static int nhdfragls; /* size of hdfragl array */
62 gregl 3.1
63 gregl 3.4
64 gwlarson 3.26 char *
65     hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */
66     char *ptr;
67     unsigned siz;
68     char *rout;
69     {
70     register char *newp;
71     /* call malloc/realloc */
72     if (ptr == NULL) newp = (char *)malloc(siz);
73     else newp = (char *)realloc(ptr, siz);
74     /* check success */
75     if (newp == NULL && rout != NULL) {
76     hdfreecache(25, NULL); /* free some memory */
77     errno = 0; /* retry */
78     newp = hdrealloc(ptr, siz, NULL);
79     if (newp == NULL) { /* give up and report error */
80     sprintf(errmsg, "out of memory in %s", rout);
81     error(SYSTEM, errmsg);
82     }
83     }
84     return(newp);
85     }
86    
87    
88 gregl 3.1 hdattach(fd) /* start tracking file fragments for some section */
89     register int fd;
90     {
91 gregl 3.16 if (fd >= nhdfragls) {
92 gwlarson 3.26 hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
93     (fd+1)*sizeof(struct fraglist), "hdattach");
94 gregl 3.16 bzero((char *)(hdfragl+nhdfragls),
95     (fd+1-nhdfragls)*sizeof(struct fraglist));
96     nhdfragls = fd+1;
97 gregl 3.1 }
98 gregl 3.16 hdfragl[fd].nlinks++;
99     hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */
100 gregl 3.1 }
101    
102    
103     /* Do we need a routine to locate file fragments given known occupants? */
104    
105    
106     hdrelease(fd) /* stop tracking file fragments for some section */
107     register int fd;
108     {
109 gregl 3.16 if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
110 gregl 3.1 return;
111 gregl 3.16 if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
112     free((char *)hdfragl[fd].fi);
113     hdfragl[fd].fi = NULL;
114     hdfragl[fd].nfrags = 0;
115 gregl 3.1 }
116     }
117    
118    
119     HOLO *
120     hdinit(fd, hproto) /* initialize a holodeck section in a file */
121     int fd; /* corresponding file descriptor */
122     HDGRID *hproto; /* holodeck section grid */
123     {
124 gwlarson 3.22 long rtrunc;
125 gregl 3.1 long fpos;
126     register HOLO *hp;
127     register int n;
128     /* prepare for system errors */
129     errno = 0;
130     if ((fpos = lseek(fd, 0L, 1)) < 0)
131     error(SYSTEM, "cannot determine holodeck file position");
132     if (hproto == NULL) { /* assume we're loading it */
133     HDGRID hpr;
134     /* load header */
135     if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID))
136     error(SYSTEM, "cannot load holodeck header");
137     /* allocate grid */
138     if ((hp = hdalloc(&hpr)) == NULL)
139     goto memerr;
140     /* load beam directory */
141     n = nbeams(hp)*sizeof(BEAMI);
142     if (read(fd, (char *)(hp->bi+1), n) != n)
143     error(SYSTEM, "failure loading holodeck directory");
144 gregl 3.6 /* check that it's clean */
145 gwlarson 3.28 for (n = nbeams(hp); n > 0; n--)
146     if (hp->bi[n].fo < 0) {
147     hp->bi[n].fo = 0;
148     error(WARNING, "dirty holodeck section");
149     break;
150     }
151 gregl 3.1 } else { /* assume we're creating it */
152     if ((hp = hdalloc(hproto)) == NULL)
153     goto memerr;
154     /* write header and skeleton */
155     n = nbeams(hp)*sizeof(BEAMI);
156     if (write(fd, (char *)hproto, sizeof(HDGRID)) !=
157     sizeof(HDGRID) ||
158     write(fd, (char *)(hp->bi+1), n) != n)
159     error(SYSTEM, "cannot write header to holodeck file");
160     }
161     hp->fd = fd;
162     hp->dirty = 0;
163     biglob(hp)->fo = fpos + sizeof(HDGRID);
164 gregl 3.13 /* start tracking fragments */
165     hdattach(fd);
166     /* check rays on disk */
167     fpos = hdfilen(fd);
168 gwlarson 3.22 biglob(hp)->nrd = rtrunc = 0;
169 gregl 3.13 for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
170     if (hp->bi[n].nrd)
171 gwlarson 3.27 if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
172 gwlarson 3.22 rtrunc += hp->bi[n].nrd;
173     hp->bi[n].nrd = 0;
174     } else
175 gregl 3.13 biglob(hp)->nrd += hp->bi[n].nrd;
176 gwlarson 3.22 if (rtrunc) {
177     sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
178     rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
179     error(WARNING, errmsg);
180     }
181 gregl 3.1 /* add to holodeck list */
182     for (n = 0; n < HDMAX; n++)
183     if (hdlist[n] == NULL) {
184     hdlist[n] = hp;
185     break;
186     }
187     /* all done */
188     return(hp);
189     memerr:
190     error(SYSTEM, "cannot allocate holodeck grid");
191     }
192    
193    
194 gwlarson 3.37 hdmarkdirty(hp, i) /* mark holodeck directory position dirty */
195 gwlarson 3.28 register HOLO *hp;
196     int i;
197     {
198     static BEAMI smudge = {0, -1};
199     int mindist, minpos;
200     register int j;
201    
202     if (!hp->dirty++) { /* write smudge first time */
203     if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
204     || write(hp->fd, (char *)&smudge,
205     sizeof(BEAMI)) != sizeof(BEAMI))
206 gwlarson 3.37 error(SYSTEM, "seek/write error in hdmarkdirty");
207 gwlarson 3.28 hp->dirseg[0].s = i;
208     hp->dirseg[0].n = 1;
209     return;
210     }
211     /* insert into segment list */
212     for (j = hp->dirty; j--; ) {
213     if (!j || hp->dirseg[j-1].s < i) {
214     hp->dirseg[j].s = i;
215     hp->dirseg[j].n = 1;
216     break;
217     }
218     copystruct(hp->dirseg+j, hp->dirseg+(j-1));
219     }
220 gwlarson 3.29 do { /* check neighbors */
221     mindist = nbeams(hp); /* find closest */
222     for (j = hp->dirty; --j; )
223     if (hp->dirseg[j].s -
224     (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
225     < mindist) {
226     minpos = j;
227     mindist = hp->dirseg[j].s -
228 gwlarson 3.28 (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
229 gwlarson 3.29 }
230     /* good enough? */
231     if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
232     break;
233 gwlarson 3.28 j = minpos - 1; /* coalesce neighbors */
234     if (hp->dirseg[j].s + hp->dirseg[j].n <
235 gwlarson 3.29 hp->dirseg[minpos].s + hp->dirseg[minpos].n)
236 gwlarson 3.28 hp->dirseg[j].n = hp->dirseg[minpos].s +
237     hp->dirseg[minpos].n - hp->dirseg[j].s;
238 gwlarson 3.29 hp->dirty--;
239     while (++j < hp->dirty) /* close the gap */
240 gwlarson 3.28 copystruct(hp->dirseg+j, hp->dirseg+(j+1));
241 gwlarson 3.29 } while (mindist <= MINDIRSEL);
242 gwlarson 3.28 }
243    
244    
245 gregl 3.1 int
246 gregl 3.12 hdsync(hp, all) /* update beams and directory on disk */
247 gregl 3.1 register HOLO *hp;
248 gregl 3.12 int all;
249 gregl 3.1 {
250     register int j, n;
251    
252 gregl 3.12 if (hp == NULL) { /* do all holodecks */
253 gregl 3.1 n = 0;
254     for (j = 0; hdlist[j] != NULL; j++)
255 gregl 3.12 n += hdsync(hdlist[j], all);
256 gregl 3.1 return(n);
257     }
258 gregl 3.12 /* sync the beams */
259 gregl 3.15 for (j = (all ? nbeams(hp) : 0); j > 0; j--)
260 gregl 3.12 if (hp->bl[j] != NULL)
261     hdsyncbeam(hp, j);
262 gregl 3.13 if (!hp->dirty) /* directory clean? */
263 gregl 3.1 return(0);
264 gwlarson 3.28 errno = 0; /* write dirty segments */
265     for (j = 0; j < hp->dirty; j++) {
266     if (lseek(hp->fd, biglob(hp)->fo +
267     (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
268     error(SYSTEM, "cannot seek on holodeck file");
269     n = hp->dirseg[j].n * sizeof(BEAMI);
270     if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
271     error(SYSTEM, "cannot update section directory");
272     }
273     hp->dirty = 0; /* all clean */
274 gregl 3.1 return(1);
275     }
276    
277    
278 gregl 3.14 unsigned
279 gregl 3.1 hdmemuse(all) /* return memory usage (in bytes) */
280     int all; /* include overhead (painful) */
281     {
282     long total = 0;
283     register int i, j;
284    
285     for (j = 0; hdlist[j] != NULL; j++) {
286     total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
287     if (all) {
288     total += sizeof(HOLO) + sizeof(BEAM *) +
289     nbeams(hdlist[j]) *
290     (sizeof(BEAM *)+sizeof(BEAMI));
291     for (i = nbeams(hdlist[j]); i > 0; i--)
292     if (hdlist[j]->bl[i] != NULL)
293     total += sizeof(BEAM);
294     }
295     }
296     if (all)
297 gregl 3.16 for (j = 0; j < nhdfragls; j++) {
298     total += sizeof(struct fraglist);
299     if (hdfragl[j].nfrags)
300 gregl 3.4 total += FRAGBLK*sizeof(BEAMI) *
301 gregl 3.16 ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
302 gregl 3.4 }
303 gregl 3.1 return(total);
304 gregl 3.8 }
305    
306    
307     long
308     hdfilen(fd) /* return file length for fd */
309     int fd;
310     {
311     long fpos, flen;
312    
313     if (fd < 0)
314     return(-1);
315 gregl 3.16 if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
316 gregl 3.8 if ((fpos = lseek(fd, 0L, 1)) < 0)
317     return(-1);
318     flen = lseek(fd, 0L, 2);
319     lseek(fd, fpos, 0);
320     return(flen);
321     }
322 gregl 3.16 return(hdfragl[fd].flen);
323 gregl 3.1 }
324    
325    
326     long
327     hdfiluse(fd, all) /* compute file usage (in bytes) */
328     int fd; /* open file descriptor to check */
329     int all; /* include overhead and unflushed data */
330     {
331     long total = 0;
332     register int i, j;
333    
334     for (j = 0; hdlist[j] != NULL; j++) {
335     if (hdlist[j]->fd != fd)
336     continue;
337     total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
338     if (all) {
339     for (i = nbeams(hdlist[j]); i > 0; i--)
340     if (hdlist[j]->bl[i] != NULL)
341     total += sizeof(RAYVAL) *
342     (hdlist[j]->bl[i]->nrm -
343     hdlist[j]->bi[i].nrd);
344     total += sizeof(HDGRID) +
345     nbeams(hdlist[j])*sizeof(BEAMI);
346     }
347     }
348     return(total); /* does not include fragments */
349     }
350    
351    
352     RAYVAL *
353     hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */
354     register HOLO *hp;
355     register int i;
356     int nr; /* number of new rays desired */
357     {
358     RAYVAL *p;
359     int n;
360    
361 gwlarson 3.32 if (nr <= 0) return(NULL);
362     CHECK(i < 1 | i > nbeams(hp),
363     CONSISTENCY, "bad beam index given to hdnewrays");
364 gregl 3.1 if (hp->bl[i] != NULL)
365     hp->bl[i]->tick = hdclock; /* preempt swap */
366     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
367     hdfreecache(PCTFREE, NULL); /* free some space */
368     if (hp->bl[i] == NULL) { /* allocate (and load) */
369     n = hp->bi[i].nrd + nr;
370 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
371 gregl 3.1 blglob(hp)->nrm += n;
372 gwlarson 3.32 if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
373 gwlarson 3.26 errno = 0;
374 gregl 3.1 if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
375     error(SYSTEM, "seek error on holodeck file");
376     n *= sizeof(RAYVAL);
377     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
378     error(SYSTEM,
379     "error reading beam from holodeck file");
380     }
381     } else { /* just grow in memory */
382 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
383     hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
384 gregl 3.1 blglob(hp)->nrm += nr;
385     }
386 gwlarson 3.32 if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
387     hdfreefrag(hp, i); /* relinquish old fragment */
388 gregl 3.1 p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
389     hp->bl[i]->nrm += nr; /* update in-core structure */
390     bzero((char *)p, nr*sizeof(RAYVAL));
391 gwlarson 3.25 blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
392 gregl 3.1 return(p); /* point to new rays */
393     }
394    
395    
396     BEAM *
397     hdgetbeam(hp, i) /* get beam (from file if necessary) */
398     register HOLO *hp;
399     register int i;
400     {
401     register int n;
402    
403 gwlarson 3.32 CHECK(i < 1 | i > nbeams(hp),
404     CONSISTENCY, "bad beam index given to hdgetbeam");
405 gregl 3.1 if (hp->bl[i] == NULL) { /* load from disk */
406     if (!(n = hp->bi[i].nrd))
407     return(NULL);
408     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
409     hdfreecache(PCTFREE, NULL); /* get free space */
410 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
411     blglob(hp)->nrm += hp->bl[i]->nrm = n;
412 gregl 3.1 errno = 0;
413     if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
414     error(SYSTEM, "seek error on holodeck file");
415     n *= sizeof(RAYVAL);
416     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
417     error(SYSTEM, "error reading beam from holodeck file");
418 gwlarson 3.32 if (hdfragflags&FF_READ)
419     hdfreefrag(hp, i); /* relinquish old frag. */
420 gregl 3.1 }
421 gwlarson 3.25 blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
422 gregl 3.1 return(hp->bl[i]);
423     }
424    
425    
426     int
427 gregl 3.17 hdfilord(hb1, hb2) /* order beams for quick loading */
428 gregl 3.14 register HDBEAMI *hb1, *hb2;
429     {
430 gregl 3.17 register long c;
431     /* residents go first */
432     if (hb2->h->bl[hb2->b] != NULL)
433     return(hb1->h->bl[hb1->b] == NULL);
434     if (hb1->h->bl[hb1->b] != NULL)
435     return(-1);
436     /* otherwise sort by file descriptor */
437 gregl 3.14 if ((c = hb1->h->fd - hb2->h->fd))
438     return(c);
439     /* then by position in file */
440 gregl 3.17 c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
441     return(c > 0 ? 1 : c < 0 ? -1 : 0);
442 gregl 3.14 }
443    
444    
445     hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */
446     register HDBEAMI *hb; /* list gets sorted by hdfilord() */
447     int n; /* list length */
448     int (*bf)(); /* callback function (optional) */
449     {
450     unsigned origcachesize, memuse;
451 gwlarson 3.23 int bytesloaded, needbytes, bytes2free;
452 gregl 3.14 register BEAM *bp;
453     register int i;
454     /* precheck consistency */
455 gwlarson 3.25 if (n <= 0) return;
456 gregl 3.14 for (i = n; i--; )
457 gwlarson 3.32 if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
458 gregl 3.14 error(CONSISTENCY, "bad beam in hdloadbeams");
459     /* sort list for optimal access */
460     qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
461 gregl 3.17 bytesloaded = 0; /* run through loaded beams */
462     for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
463 gwlarson 3.23 bp->tick = hdclock; /* preempt swap */
464 gregl 3.17 bytesloaded += bp->nrm;
465     if (bf != NULL)
466 gwlarson 3.23 (*bf)(bp, hb);
467 gregl 3.17 }
468     bytesloaded *= sizeof(RAYVAL);
469 gregl 3.15 if ((origcachesize = hdcachesize) > 0) {
470 gregl 3.17 needbytes = 0; /* figure out memory needs */
471 gregl 3.15 for (i = n; i--; )
472 gregl 3.17 needbytes += hb[i].h->bi[hb[i].b].nrd;
473 gregl 3.15 needbytes *= sizeof(RAYVAL);
474     do { /* free enough memory */
475     memuse = hdmemuse(0);
476 gregl 3.17 bytes2free = needbytes - (int)(hdcachesize-memuse);
477     if (bytes2free > (int)(memuse - bytesloaded))
478 gregl 3.15 bytes2free = memuse - bytesloaded;
479     } while (bytes2free > 0 &&
480     hdfreecache(100*bytes2free/memuse, NULL) < 0);
481 gregl 3.17 hdcachesize = 0; /* load beams w/o swap */
482 gregl 3.15 }
483 gregl 3.14 for (i = 0; i < n; i++)
484     if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
485 gwlarson 3.23 (*bf)(bp, hb+i);
486 gregl 3.14 hdcachesize = origcachesize; /* resume dynamic swapping */
487     }
488    
489    
490 gwlarson 3.33 int
491 gwlarson 3.32 hdfreefrag(hp, i) /* free a file fragment */
492     HOLO *hp;
493     int i;
494 gregl 3.19 {
495 gwlarson 3.32 register BEAMI *bi = &hp->bi[i];
496 gregl 3.19 register struct fraglist *f;
497     register int j, k;
498    
499 gwlarson 3.32 if (bi->nrd <= 0)
500 gwlarson 3.33 return(0);
501 gwlarson 3.32 DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
502     CONSISTENCY, "bad file descriptor in hdfreefrag");
503     f = &hdfragl[hp->fd];
504 gregl 3.19 if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
505     for (j = k = 0; k < f->nfrags; j++, k++) {
506     while (f->fi[k].nrd == 0)
507     if (++k >= f->nfrags)
508     goto endloop;
509     if (k > j)
510     copystruct(f->fi+j, f->fi+k);
511     }
512     endloop:
513     f->nfrags = j;
514     }
515     j = f->nfrags++; /* allocate a slot in free list */
516 gwlarson 3.31 #if MAXFRAGB
517     if (j >= MAXFRAGB*FRAGBLK) {
518     f->nfrags = j--; /* stop list growth */
519     if (bi->nrd <= f->fi[j].nrd)
520 gwlarson 3.33 return(0); /* new one no better than discard */
521 gwlarson 3.31 }
522 gregl 3.19 #endif
523 gwlarson 3.32 if (j % FRAGBLK == 0) { /* more (or less) free list space */
524 gwlarson 3.26 register BEAMI *newp;
525 gregl 3.19 if (f->fi == NULL)
526 gwlarson 3.26 newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
527 gregl 3.19 else
528 gwlarson 3.26 newp = (BEAMI *)realloc((char *)f->fi,
529 gregl 3.19 (j+FRAGBLK)*sizeof(BEAMI));
530 gwlarson 3.26 if (newp == NULL) {
531     f->nfrags--; /* graceful failure */
532 gwlarson 3.33 return(0);
533 gwlarson 3.26 }
534     f->fi = newp;
535 gregl 3.19 }
536     for ( ; ; j--) { /* insert in descending list */
537     if (!j || bi->fo < f->fi[j-1].fo) {
538     f->fi[j].fo = bi->fo;
539     f->fi[j].nrd = bi->nrd;
540     break;
541     }
542     copystruct(f->fi+j, f->fi+(j-1));
543     }
544     /* coalesce adjacent fragments */
545     /* successors never empty */
546     if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
547     f->fi[j].nrd += f->fi[j-1].nrd;
548     f->fi[j-1].nrd = 0;
549     }
550     for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */
551     if (f->fi[k].nrd) {
552     if (f->fi[j].fo == f->fi[k].fo +
553     f->fi[k].nrd*sizeof(RAYVAL)) {
554     f->fi[k].nrd += f->fi[j].nrd;
555     f->fi[j].nrd = 0;
556     }
557     break;
558     }
559 gwlarson 3.32 biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */
560     bi->nrd = 0;
561 gwlarson 3.34 bi->fo = 0L;
562 gwlarson 3.39 hdmarkdirty(hp, i); /* assume we'll reallocate */
563 gwlarson 3.33 return(1);
564 gregl 3.19 }
565    
566    
567 gwlarson 3.32 int
568     hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */
569     int fd;
570     int *listlen;
571     register int4 *listsiz;
572     {
573     register struct fraglist *f;
574     register int i;
575    
576     if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
577     return(0); /* listless */
578     if (listlen != NULL)
579     *listlen = f->nfrags;
580     if (listsiz != NULL)
581     for (i = f->nfrags, *listsiz = 0; i--; )
582     *listsiz += f->fi[i].nrd;
583     #if MAXFRAGB
584     return(f->nfrags < MAXFRAGB*FRAGBLK);
585     #else
586     return(1); /* list never fills up */
587     #endif
588     }
589    
590    
591 gregl 3.19 long
592     hdallocfrag(fd, nrays) /* allocate a file fragment */
593     int fd;
594     unsigned int4 nrays;
595     {
596     register struct fraglist *f;
597 gwlarson 3.33 register int j;
598 gregl 3.19 long nfo;
599    
600     if (nrays == 0)
601     return(-1L);
602 gwlarson 3.32 DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
603     CONSISTENCY, "bad file descriptor in hdallocfrag");
604 gregl 3.19 f = &hdfragl[fd];
605 gwlarson 3.33 for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */
606     if (f->fi[j].nrd >= nrays)
607     break;
608     if (j < 0) { /* no fragment -- extend file */
609 gregl 3.19 nfo = f->flen;
610     f->flen += nrays*sizeof(RAYVAL);
611     } else { /* else use fragment */
612 gwlarson 3.33 nfo = f->fi[j].fo;
613     f->fi[j].fo += nrays*sizeof(RAYVAL);
614     f->fi[j].nrd -= nrays;
615 gregl 3.19 }
616     return(nfo);
617     }
618    
619    
620 gregl 3.14 int
621 gregl 3.12 hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */
622 gregl 3.1 register HOLO *hp;
623     register int i;
624     {
625 gwlarson 3.39 int fragfreed;
626 gregl 3.19 unsigned int4 nrays;
627     unsigned int n;
628 gregl 3.11 long nfo;
629 gregl 3.13 /* check file status */
630 gregl 3.16 if (hdfragl[hp->fd].writerr)
631 gregl 3.13 return(-1);
632 gwlarson 3.32 DCHECK(i < 1 | i > nbeams(hp),
633     CONSISTENCY, "bad beam index in hdsyncbeam");
634 gregl 3.11 /* is current fragment OK? */
635 gregl 3.12 if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
636 gregl 3.1 return(0);
637 gwlarson 3.39 /* relinquish old fragment? */
638     fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
639 gregl 3.19 if (nrays) { /* get and write new fragment */
640     nfo = hdallocfrag(hp->fd, nrays);
641 gregl 3.11 errno = 0;
642     if (lseek(hp->fd, nfo, 0) < 0)
643     error(SYSTEM, "cannot seek on holodeck file");
644     n = hp->bl[i]->nrm * sizeof(RAYVAL);
645     if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
646 gregl 3.16 hdfragl[hp->fd].writerr++;
647 gregl 3.18 hdsync(NULL, 0); /* sync directories */
648 gregl 3.12 error(SYSTEM, "write error in hdsyncbeam");
649 gregl 3.11 }
650 gregl 3.19 hp->bi[i].fo = nfo;
651     } else
652     hp->bi[i].fo = 0L;
653 gwlarson 3.34 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
654     hp->bi[i].nrd = nrays;
655 gwlarson 3.39 if (!fragfreed)
656     hdmarkdirty(hp, i); /* need to flag dir. ent. */
657 gregl 3.1 return(1);
658     }
659    
660    
661     int
662     hdfreebeam(hp, i) /* free beam, writing if dirty */
663     register HOLO *hp;
664     register int i;
665     {
666 gregl 3.11 int nchanged;
667 gregl 3.1
668     if (hp == NULL) { /* clear all holodecks */
669     nchanged = 0;
670     for (i = 0; hdlist[i] != NULL; i++)
671     nchanged += hdfreebeam(hdlist[i], 0);
672     return(nchanged);
673     }
674 gregl 3.16 if (hdfragl[hp->fd].writerr) /* check for file error */
675 gregl 3.13 return(0);
676 gregl 3.1 if (i == 0) { /* clear entire holodeck */
677 gwlarson 3.36 if (blglob(hp)->nrm == 0)
678     return(0); /* already clear */
679 gregl 3.1 nchanged = 0;
680 gregl 3.10 for (i = nbeams(hp); i > 0; i--)
681     if (hp->bl[i] != NULL)
682     nchanged += hdfreebeam(hp, i);
683 gwlarson 3.36 DCHECK(blglob(hp)->nrm != 0,
684     CONSISTENCY, "bad beam count in hdfreebeam");
685 gregl 3.1 return(nchanged);
686     }
687 gwlarson 3.32 DCHECK(i < 1 | i > nbeams(hp),
688     CONSISTENCY, "bad beam index to hdfreebeam");
689 gregl 3.1 if (hp->bl[i] == NULL)
690     return(0);
691     /* check for additions */
692     nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
693 gregl 3.11 if (nchanged)
694 gregl 3.12 hdsyncbeam(hp, i); /* write new fragment */
695 gregl 3.1 blglob(hp)->nrm -= hp->bl[i]->nrm;
696     free((char *)hp->bl[i]); /* free memory */
697     hp->bl[i] = NULL;
698     return(nchanged);
699     }
700    
701    
702 gregl 3.10 int
703     hdkillbeam(hp, i) /* delete beam from holodeck */
704     register HOLO *hp;
705     register int i;
706     {
707 gregl 3.11 int nchanged;
708 gregl 3.10
709     if (hp == NULL) { /* clobber all holodecks */
710     nchanged = 0;
711     for (i = 0; hdlist[i] != NULL; i++)
712     nchanged += hdkillbeam(hdlist[i], 0);
713     return(nchanged);
714     }
715     if (i == 0) { /* clobber entire holodeck */
716 gwlarson 3.36 if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
717     return(0); /* already empty */
718     nchanged = 0;
719 gregl 3.10 nchanged = 0;
720     for (i = nbeams(hp); i > 0; i--)
721     if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
722     nchanged += hdkillbeam(hp, i);
723 gwlarson 3.32 DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
724     CONSISTENCY, "bad beam count in hdkillbeam");
725 gregl 3.10 return(nchanged);
726     }
727 gwlarson 3.32 DCHECK(i < 1 | i > nbeams(hp),
728     CONSISTENCY, "bad beam index to hdkillbeam");
729 gregl 3.10 if (hp->bl[i] != NULL) { /* free memory */
730     blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
731     free((char *)hp->bl[i]);
732 gwlarson 3.39 hp->bl[i] = NULL;
733 gregl 3.10 } else
734     nchanged = hp->bi[i].nrd;
735 gwlarson 3.39 if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
736     biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */
737     hp->bi[i].nrd = 0;
738 gwlarson 3.32 hp->bi[i].fo = 0L;
739 gwlarson 3.39 hdmarkdirty(hp, i);
740 gregl 3.10 }
741     return(nchanged);
742     }
743    
744    
745 gregl 3.14 int
746     hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */
747     register HDBEAMI *hb; /* beam list */
748     int nents; /* current list length */
749     int n; /* maximum list length */
750 gregl 3.1 register HOLO *hp; /* section we're adding from */
751     {
752     register int i, j;
753     /* insert each beam from hp */
754 gregl 3.21 for (i = 1; i <= nbeams(hp); i++) {
755 gregl 3.1 if (hp->bl[i] == NULL) /* check if loaded */
756     continue;
757 gregl 3.14 #if 0
758     if (hp->bl[i]->tick == hdclock) /* preempt swap? */
759     continue;
760     #endif
761     if ((j = ++nents) >= n) /* grow list if we can */
762 gregl 3.1 nents--;
763     for ( ; ; ) { /* bubble into place */
764     if (!--j || hp->bl[i]->tick >=
765 gregl 3.14 hb[j-1].h->bl[hb[j-1].b]->tick) {
766     hb[j].h = hp;
767     hb[j].b = i;
768 gregl 3.1 break;
769     }
770 gregl 3.14 copystruct(hb+j, hb+(j-1));
771 gregl 3.1 }
772     }
773 gregl 3.14 return(nents); /* return new list length */
774 gregl 3.1 }
775    
776    
777 gregl 3.14 int
778 gregl 3.1 hdfreecache(pct, honly) /* free up cache space, writing changes */
779     int pct; /* maximum percentage to free */
780     register HOLO *honly; /* NULL means check all */
781     {
782 gregl 3.14 HDBEAMI hb[FREEBEAMS];
783 gregl 3.1 int freetarget;
784 gregl 3.14 int n;
785 gregl 3.1 register int i;
786 gwlarson 3.25 #ifdef DEBUG
787     unsigned membefore;
788    
789     membefore = hdmemuse(0);
790     #endif
791 gregl 3.1 /* compute free target */
792     freetarget = (honly != NULL) ? blglob(honly)->nrm :
793     hdmemuse(0)/sizeof(RAYVAL) ;
794     freetarget = freetarget*pct/100;
795 gregl 3.14 if (freetarget <= 0)
796     return(0);
797 gregl 3.1 /* find least recently used */
798 gregl 3.14 n = 0;
799 gregl 3.1 if (honly != NULL)
800 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, honly);
801 gregl 3.1 else
802     for (i = 0; hdlist[i] != NULL; i++)
803 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
804 gregl 3.1 /* free LRU beams */
805 gregl 3.14 for (i = 0; i < n; i++) {
806     hdfreebeam(hb[i].h, hb[i].b);
807     if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
808 gregl 3.1 break;
809     }
810 gregl 3.12 hdsync(honly, 0); /* synchronize directories as necessary */
811 gwlarson 3.25 #ifdef DEBUG
812     sprintf(errmsg,
813     "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
814     membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
815     wputs(errmsg);
816     #endif
817 gregl 3.14 return(-freetarget); /* return how far past goal we went */
818 gregl 3.1 }
819    
820    
821     hddone(hp) /* clean up holodeck section and free */
822     register HOLO *hp; /* NULL means clean up all */
823     {
824     register int i;
825    
826     if (hp == NULL) { /* NULL means clean up everything */
827     while (hdlist[0] != NULL)
828     hddone(hdlist[0]);
829 gwlarson 3.24 free((char *)hdfragl);
830     hdfragl = NULL; nhdfragls = 0;
831 gregl 3.1 return;
832     }
833     /* flush all data and free memory */
834 gwlarson 3.37 hdflush(hp);
835 gregl 3.1 /* release fragment resources */
836     hdrelease(hp->fd);
837     /* remove hp from active list */
838     for (i = 0; hdlist[i] != NULL; i++)
839     if (hdlist[i] == hp) {
840     while ((hdlist[i] = hdlist[i+1]) != NULL)
841     i++;
842     break;
843     }
844     free((char *)hp->bl); /* free beam list */
845     free((char *)hp); /* free holodeck struct */
846     }