ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.59
Committed: Fri Sep 3 23:52:42 2010 UTC (13 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R1, rad4R2P1
Changes since 3.58: +3 -2 lines
Log Message:
Fixed portability issue for 64-bit systems

File Contents

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