16 |
|
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
|
#endif |
18 |
|
#ifndef FREEBEAMS |
19 |
< |
#define FREEBEAMS 1024 /* maximum beams to free at a time */ |
19 |
> |
#define FREEBEAMS 512 /* maximum beams to free at a time */ |
20 |
|
#endif |
21 |
|
#ifndef PCTFREE |
22 |
< |
#define PCTFREE 12 /* maximum fraction to free (%) */ |
22 |
> |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
23 |
|
#endif |
24 |
|
|
25 |
< |
#define MAXFRAG 8192 /* maximum fragments tracked per open file */ |
25 |
> |
/* define MAXFRAG if you want to limit fragment tracking memory */ |
26 |
|
|
27 |
+ |
#ifndef BSD |
28 |
+ |
#define write writebuf /* safe i/o routines */ |
29 |
+ |
#define read readbuf |
30 |
+ |
#endif |
31 |
+ |
|
32 |
|
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
33 |
|
|
34 |
|
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
73 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
74 |
|
register int fd; |
75 |
|
{ |
76 |
< |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) |
76 |
> |
if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) |
77 |
|
return; |
78 |
|
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
79 |
|
free((char *)hdfrag[fd].fi); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
+ |
markdirty(hp) /* mark holodeck section directory dirty */ |
87 |
+ |
register HOLO *hp; |
88 |
+ |
{ |
89 |
+ |
static BEAMI smudge = {0, -1}; |
90 |
+ |
|
91 |
+ |
if (hp->dirty) /* already marked? */ |
92 |
+ |
return; |
93 |
+ |
hp->dirty = 1; |
94 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
95 |
+ |
|| write(hp->fd, (char *)&smudge, |
96 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
97 |
+ |
error(SYSTEM, "seek/write error in markdirty"); |
98 |
+ |
} |
99 |
+ |
|
100 |
+ |
|
101 |
|
HOLO * |
102 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
103 |
|
int fd; /* corresponding file descriptor */ |
122 |
|
n = nbeams(hp)*sizeof(BEAMI); |
123 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
124 |
|
error(SYSTEM, "failure loading holodeck directory"); |
125 |
+ |
/* check that it's clean */ |
126 |
+ |
if (hp->bi[nbeams(hp)].fo < 0) |
127 |
+ |
error(USER, "dirty holodeck section"); |
128 |
|
} else { /* assume we're creating it */ |
129 |
|
if ((hp = hdalloc(hproto)) == NULL) |
130 |
|
goto memerr; |
211 |
|
|
212 |
|
|
213 |
|
long |
214 |
+ |
hdfilen(fd) /* return file length for fd */ |
215 |
+ |
int fd; |
216 |
+ |
{ |
217 |
+ |
long fpos, flen; |
218 |
+ |
|
219 |
+ |
if (fd < 0) |
220 |
+ |
return(-1); |
221 |
+ |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) { |
222 |
+ |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
223 |
+ |
return(-1); |
224 |
+ |
flen = lseek(fd, 0L, 2); |
225 |
+ |
lseek(fd, fpos, 0); |
226 |
+ |
return(flen); |
227 |
+ |
} |
228 |
+ |
return(hdfrag[fd].flen); |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
|
232 |
+ |
long |
233 |
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
234 |
|
int fd; /* open file descriptor to check */ |
235 |
|
int all; /* include overhead and unflushed data */ |
356 |
|
register int j, k; |
357 |
|
/* relinquish old fragment */ |
358 |
|
if (hp->bi[i].nrd) { |
359 |
< |
j = ++f->nfrags; |
359 |
> |
j = f->nfrags++; |
360 |
|
#ifdef MAXFRAG |
361 |
< |
if (j >= MAXFRAG) |
361 |
> |
if (j >= MAXFRAG-1) |
362 |
|
f->nfrags--; |
321 |
– |
else |
363 |
|
#endif |
364 |
< |
if (j % FRAGBLK == 1) { /* more frag. space */ |
364 |
> |
if (j % FRAGBLK == 0) { /* more frag. space */ |
365 |
|
if (f->fi == NULL) |
366 |
|
f->fi = (BEAMI *)malloc( |
367 |
|
FRAGBLK*sizeof(BEAMI)); |
368 |
|
else |
369 |
|
f->fi = (BEAMI *)realloc((char *)f->fi, |
370 |
< |
(FRAGBLK-1+j)*sizeof(BEAMI)); |
370 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
371 |
|
if (f->fi == NULL) |
372 |
|
error(SYSTEM, |
373 |
|
"out of memory in hdgetbi"); |
374 |
|
} |
375 |
< |
for ( ; ; ) { /* stick it in our descending list */ |
376 |
< |
if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { |
375 |
> |
for ( ; ; j--) { /* insert in descending list */ |
376 |
> |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
377 |
|
f->fi[j].fo = hp->bi[i].fo; |
378 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
379 |
|
break; |
417 |
|
} |
418 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
419 |
|
hp->bi[i].nrd = nrays; |
420 |
< |
hp->dirty++; /* section directory now out of date */ |
420 |
> |
markdirty(hp); /* section directory now out of date */ |
421 |
|
return(1); |
422 |
|
} |
423 |
|
|
435 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
436 |
|
return(nchanged); |
437 |
|
} |
438 |
+ |
if (hp->fd < 0) /* check for recursive error */ |
439 |
+ |
return(-1); |
440 |
|
if (i == 0) { /* clear entire holodeck */ |
441 |
|
nchanged = 0; |
442 |
< |
for (i = 1; i <= nbeams(hp); i++) |
443 |
< |
nchanged += hdfreebeam(hp, i); |
442 |
> |
for (i = nbeams(hp); i > 0; i--) |
443 |
> |
if (hp->bl[i] != NULL) |
444 |
> |
nchanged += hdfreebeam(hp, i); |
445 |
|
return(nchanged); |
446 |
|
} |
447 |
|
if (i < 1 | i > nbeams(hp)) |
456 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
457 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
458 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
459 |
< |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
459 |
> |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
460 |
> |
hp->fd = -1; /* avoid recursive error */ |
461 |
|
error(SYSTEM, "write error in hdfreebeam"); |
462 |
+ |
} |
463 |
|
} |
464 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
465 |
|
free((char *)hp->bl[i]); /* free memory */ |
468 |
|
} |
469 |
|
|
470 |
|
|
471 |
+ |
int |
472 |
+ |
hdkillbeam(hp, i) /* delete beam from holodeck */ |
473 |
+ |
register HOLO *hp; |
474 |
+ |
register int i; |
475 |
+ |
{ |
476 |
+ |
static BEAM emptybeam; |
477 |
+ |
int nchanged, n; |
478 |
+ |
|
479 |
+ |
if (hp == NULL) { /* clobber all holodecks */ |
480 |
+ |
nchanged = 0; |
481 |
+ |
for (i = 0; hdlist[i] != NULL; i++) |
482 |
+ |
nchanged += hdkillbeam(hdlist[i], 0); |
483 |
+ |
return(nchanged); |
484 |
+ |
} |
485 |
+ |
if (i == 0) { /* clobber entire holodeck */ |
486 |
+ |
nchanged = 0; |
487 |
+ |
for (i = nbeams(hp); i > 0; i--) |
488 |
+ |
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
489 |
+ |
nchanged += hdkillbeam(hp, i); |
490 |
+ |
#ifdef DEBUG |
491 |
+ |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
492 |
+ |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
493 |
+ |
#endif |
494 |
+ |
return(nchanged); |
495 |
+ |
} |
496 |
+ |
if (i < 1 | i > nbeams(hp)) |
497 |
+ |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
498 |
+ |
if (hp->bl[i] != NULL) { /* free memory */ |
499 |
+ |
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
500 |
+ |
free((char *)hp->bl[i]); |
501 |
+ |
} else |
502 |
+ |
nchanged = hp->bi[i].nrd; |
503 |
+ |
if (hp->bi[i].nrd) { /* free file fragment */ |
504 |
+ |
hp->bl[i] = &emptybeam; |
505 |
+ |
hdgetbi(hp, i); |
506 |
+ |
} |
507 |
+ |
hp->bl[i] = NULL; |
508 |
+ |
return(nchanged); |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
|
512 |
|
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
513 |
|
register HOLO *ha[]; /* section list (NULL terminated) */ |
514 |
|
register int ba[]; /* beam index to go with section */ |
522 |
|
; |
523 |
|
nents = j; |
524 |
|
/* insert each beam from hp */ |
525 |
< |
for (i = nbeams(hp); i > 0; i-- ) { |
525 |
> |
for (i = nbeams(hp); i > 0; i--) { |
526 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
527 |
|
continue; |
528 |
|
if ((j = ++nents) > n) /* grow list if we can */ |