ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.49
Committed: Mon Jun 30 14:59:11 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.48: +11 -7 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

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