ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.55
Committed: Thu Jan 1 11:21:55 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.54: +132 -87 lines
Log Message:
Ansification and prototypes.

File Contents

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