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) */ |
38 |
|
|
39 |
|
static struct fragment { |
40 |
|
short nlinks; /* number of holodeck sections using us */ |
41 |
< |
short nfrags; /* number of known fragments */ |
41 |
> |
short writerr; /* write error encountered */ |
42 |
> |
int nfrags; /* number of known fragments */ |
43 |
|
BEAMI *fi; /* fragments, descending file position */ |
44 |
|
long flen; /* last known file length */ |
45 |
|
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
74 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
75 |
|
register int fd; |
76 |
|
{ |
77 |
< |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) |
77 |
> |
if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) |
78 |
|
return; |
79 |
|
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
80 |
|
free((char *)hdfrag[fd].fi); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
+ |
markdirty(hp) /* mark holodeck section directory dirty */ |
88 |
+ |
register HOLO *hp; |
89 |
+ |
{ |
90 |
+ |
static BEAMI smudge = {0, -1}; |
91 |
+ |
|
92 |
+ |
if (hp->dirty) /* already marked? */ |
93 |
+ |
return; |
94 |
+ |
hp->dirty = 1; |
95 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
96 |
+ |
|| write(hp->fd, (char *)&smudge, |
97 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
98 |
+ |
error(SYSTEM, "seek/write error in markdirty"); |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
|
102 |
|
HOLO * |
103 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
104 |
|
int fd; /* corresponding file descriptor */ |
123 |
|
n = nbeams(hp)*sizeof(BEAMI); |
124 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
125 |
|
error(SYSTEM, "failure loading holodeck directory"); |
126 |
+ |
/* check that it's clean */ |
127 |
+ |
if (hp->bi[nbeams(hp)].fo < 0) |
128 |
+ |
error(USER, "dirty holodeck section"); |
129 |
|
} else { /* assume we're creating it */ |
130 |
|
if ((hp = hdalloc(hproto)) == NULL) |
131 |
|
goto memerr; |
139 |
|
hp->fd = fd; |
140 |
|
hp->dirty = 0; |
141 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
142 |
< |
biglob(hp)->nrd = 0; /* count rays on disk */ |
143 |
< |
for (n = nbeams(hp); n > 0; n--) |
144 |
< |
biglob(hp)->nrd += hp->bi[n].nrd; |
142 |
> |
/* start tracking fragments */ |
143 |
> |
hdattach(fd); |
144 |
> |
/* check rays on disk */ |
145 |
> |
fpos = hdfilen(fd); |
146 |
> |
biglob(hp)->nrd = 0; |
147 |
> |
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
148 |
> |
if (hp->bi[n].nrd) |
149 |
> |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
150 |
> |
hp->bi[n].nrd = 0; /* off end */ |
151 |
> |
else |
152 |
> |
biglob(hp)->nrd += hp->bi[n].nrd; |
153 |
|
/* add to holodeck list */ |
154 |
|
for (n = 0; n < HDMAX; n++) |
155 |
|
if (hdlist[n] == NULL) { |
156 |
|
hdlist[n] = hp; |
157 |
|
break; |
158 |
|
} |
127 |
– |
/* start tracking fragments (last) */ |
128 |
– |
hdattach(fd); |
159 |
|
/* all done */ |
160 |
|
return(hp); |
161 |
|
memerr: |
164 |
|
|
165 |
|
|
166 |
|
int |
167 |
< |
hdsync(hp) /* update directory on disk if necessary */ |
167 |
> |
hdsync(hp, all) /* update beams and directory on disk */ |
168 |
|
register HOLO *hp; |
169 |
+ |
int all; |
170 |
|
{ |
171 |
|
register int j, n; |
172 |
|
|
173 |
< |
if (hp == NULL) { /* do all */ |
173 |
> |
if (hp == NULL) { /* do all holodecks */ |
174 |
|
n = 0; |
175 |
|
for (j = 0; hdlist[j] != NULL; j++) |
176 |
< |
n += hdsync(hdlist[j]); |
176 |
> |
n += hdsync(hdlist[j], all); |
177 |
|
return(n); |
178 |
|
} |
179 |
< |
if (!hp->dirty) /* check first */ |
179 |
> |
/* sync the beams */ |
180 |
> |
for (j = all ? nbeams(hp) : 0; j > 0; j--) |
181 |
> |
if (hp->bl[j] != NULL) |
182 |
> |
hdsyncbeam(hp, j); |
183 |
> |
if (!hp->dirty) /* directory clean? */ |
184 |
|
return(0); |
185 |
|
errno = 0; |
186 |
|
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
223 |
|
|
224 |
|
|
225 |
|
long |
226 |
+ |
hdfilen(fd) /* return file length for fd */ |
227 |
+ |
int fd; |
228 |
+ |
{ |
229 |
+ |
long fpos, flen; |
230 |
+ |
|
231 |
+ |
if (fd < 0) |
232 |
+ |
return(-1); |
233 |
+ |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) { |
234 |
+ |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
235 |
+ |
return(-1); |
236 |
+ |
flen = lseek(fd, 0L, 2); |
237 |
+ |
lseek(fd, fpos, 0); |
238 |
+ |
return(flen); |
239 |
+ |
} |
240 |
+ |
return(hdfrag[fd].flen); |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
|
244 |
+ |
long |
245 |
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
246 |
|
int fd; /* open file descriptor to check */ |
247 |
|
int all; /* include overhead and unflushed data */ |
347 |
|
|
348 |
|
|
349 |
|
int |
350 |
< |
hdgetbi(hp, i) /* allocate a file fragment */ |
350 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
351 |
|
register HOLO *hp; |
352 |
|
register int i; |
353 |
|
{ |
354 |
< |
int nrays = hp->bl[i]->nrm; |
355 |
< |
|
356 |
< |
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
354 |
> |
unsigned int nrays; |
355 |
> |
long nfo; |
356 |
> |
unsigned int n; |
357 |
> |
/* check file status */ |
358 |
> |
if (hdfrag[hp->fd].writerr) |
359 |
> |
return(-1); |
360 |
> |
#ifdef DEBUG |
361 |
> |
if (i < 1 | i > nbeams(hp)) |
362 |
> |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
363 |
> |
#endif |
364 |
> |
/* is current fragment OK? */ |
365 |
> |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
366 |
|
return(0); |
367 |
< |
|
367 |
> |
/* locate fragment */ |
368 |
|
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
369 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
370 |
|
|
371 |
|
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
372 |
|
hdfrag[hp->fd].flen) /* EOF special case */ |
373 |
< |
hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
373 |
> |
hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
374 |
|
|
375 |
|
else { /* general case */ |
376 |
|
register struct fragment *f = &hdfrag[hp->fd]; |
377 |
|
register int j, k; |
378 |
|
/* relinquish old fragment */ |
379 |
|
if (hp->bi[i].nrd) { |
380 |
< |
j = ++f->nfrags; |
380 |
> |
j = f->nfrags++; |
381 |
|
#ifdef MAXFRAG |
382 |
< |
if (j >= MAXFRAG) |
382 |
> |
if (j >= MAXFRAG-1) |
383 |
|
f->nfrags--; |
321 |
– |
else |
384 |
|
#endif |
385 |
< |
if (j % FRAGBLK == 1) { /* more frag. space */ |
385 |
> |
if (j % FRAGBLK == 0) { /* more frag. space */ |
386 |
|
if (f->fi == NULL) |
387 |
|
f->fi = (BEAMI *)malloc( |
388 |
|
FRAGBLK*sizeof(BEAMI)); |
389 |
|
else |
390 |
|
f->fi = (BEAMI *)realloc((char *)f->fi, |
391 |
< |
(FRAGBLK-1+j)*sizeof(BEAMI)); |
391 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
392 |
|
if (f->fi == NULL) |
393 |
|
error(SYSTEM, |
394 |
< |
"out of memory in hdgetbi"); |
394 |
> |
"out of memory in hdsyncbeam"); |
395 |
|
} |
396 |
< |
for ( ; ; ) { /* stick it in our descending list */ |
397 |
< |
if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { |
396 |
> |
for ( ; ; j--) { /* insert in descending list */ |
397 |
> |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
398 |
|
f->fi[j].fo = hp->bi[i].fo; |
399 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
400 |
|
break; |
416 |
|
f->nfrags = j; |
417 |
|
} |
418 |
|
k = -1; /* find closest-sized fragment */ |
419 |
< |
for (j = f->nfrags; j-- > 0; ) |
419 |
> |
for (j = nrays ? f->nfrags : 0; j-- > 0; ) |
420 |
|
if (f->fi[j].nrd >= nrays && |
421 |
|
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
422 |
|
if (f->fi[k=j].nrd == nrays) |
423 |
|
break; |
424 |
|
if (k < 0) { /* no fragment -- extend file */ |
425 |
< |
hp->bi[i].fo = f->flen; |
425 |
> |
nfo = f->flen; |
426 |
|
f->flen += nrays*sizeof(RAYVAL); |
427 |
|
} else { /* else use fragment */ |
428 |
< |
hp->bi[i].fo = f->fi[k].fo; |
428 |
> |
nfo = f->fi[k].fo; |
429 |
|
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
430 |
|
f->nfrags--; |
431 |
|
for (j = k; j < f->nfrags; j++) |
436 |
|
} |
437 |
|
} |
438 |
|
} |
439 |
+ |
if (nrays) { /* write the new fragment */ |
440 |
+ |
errno = 0; |
441 |
+ |
if (lseek(hp->fd, nfo, 0) < 0) |
442 |
+ |
error(SYSTEM, "cannot seek on holodeck file"); |
443 |
+ |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
444 |
+ |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
445 |
+ |
hdfrag[hp->fd].writerr++; |
446 |
+ |
hdsync(hp, 0); /* sync directory */ |
447 |
+ |
error(SYSTEM, "write error in hdsyncbeam"); |
448 |
+ |
} |
449 |
+ |
} |
450 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
451 |
|
hp->bi[i].nrd = nrays; |
452 |
< |
hp->dirty++; /* section directory now out of date */ |
452 |
> |
hp->bi[i].fo = nfo; |
453 |
> |
markdirty(hp); /* section directory now out of date */ |
454 |
|
return(1); |
455 |
|
} |
456 |
|
|
460 |
|
register HOLO *hp; |
461 |
|
register int i; |
462 |
|
{ |
463 |
< |
int nchanged, n; |
463 |
> |
int nchanged; |
464 |
|
|
465 |
|
if (hp == NULL) { /* clear all holodecks */ |
466 |
|
nchanged = 0; |
468 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
469 |
|
return(nchanged); |
470 |
|
} |
471 |
+ |
if (hdfrag[hp->fd].writerr) /* check for file error */ |
472 |
+ |
return(0); |
473 |
|
if (i == 0) { /* clear entire holodeck */ |
474 |
|
nchanged = 0; |
475 |
< |
for (i = 1; i <= nbeams(hp); i++) |
476 |
< |
nchanged += hdfreebeam(hp, i); |
475 |
> |
for (i = nbeams(hp); i > 0; i--) |
476 |
> |
if (hp->bl[i] != NULL) |
477 |
> |
nchanged += hdfreebeam(hp, i); |
478 |
|
return(nchanged); |
479 |
|
} |
480 |
+ |
#ifdef DEBUG |
481 |
|
if (i < 1 | i > nbeams(hp)) |
482 |
|
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
483 |
+ |
#endif |
484 |
|
if (hp->bl[i] == NULL) |
485 |
|
return(0); |
486 |
|
/* check for additions */ |
487 |
|
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
488 |
< |
if (nchanged) { |
489 |
< |
hdgetbi(hp, i); /* allocate a file position */ |
411 |
< |
errno = 0; |
412 |
< |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
413 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
414 |
< |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
415 |
< |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
416 |
< |
error(SYSTEM, "write error in hdfreebeam"); |
417 |
< |
} |
488 |
> |
if (nchanged) |
489 |
> |
hdsyncbeam(hp, i); /* write new fragment */ |
490 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
491 |
|
free((char *)hp->bl[i]); /* free memory */ |
492 |
|
hp->bl[i] = NULL; |
494 |
|
} |
495 |
|
|
496 |
|
|
497 |
+ |
int |
498 |
+ |
hdkillbeam(hp, i) /* delete beam from holodeck */ |
499 |
+ |
register HOLO *hp; |
500 |
+ |
register int i; |
501 |
+ |
{ |
502 |
+ |
static BEAM emptybeam; |
503 |
+ |
int nchanged; |
504 |
+ |
|
505 |
+ |
if (hp == NULL) { /* clobber all holodecks */ |
506 |
+ |
nchanged = 0; |
507 |
+ |
for (i = 0; hdlist[i] != NULL; i++) |
508 |
+ |
nchanged += hdkillbeam(hdlist[i], 0); |
509 |
+ |
return(nchanged); |
510 |
+ |
} |
511 |
+ |
if (i == 0) { /* clobber entire holodeck */ |
512 |
+ |
nchanged = 0; |
513 |
+ |
for (i = nbeams(hp); i > 0; i--) |
514 |
+ |
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
515 |
+ |
nchanged += hdkillbeam(hp, i); |
516 |
+ |
#ifdef DEBUG |
517 |
+ |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
518 |
+ |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
519 |
+ |
#endif |
520 |
+ |
return(nchanged); |
521 |
+ |
} |
522 |
+ |
#ifdef DEBUG |
523 |
+ |
if (i < 1 | i > nbeams(hp)) |
524 |
+ |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
525 |
+ |
#endif |
526 |
+ |
if (hp->bl[i] != NULL) { /* free memory */ |
527 |
+ |
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
528 |
+ |
free((char *)hp->bl[i]); |
529 |
+ |
} else |
530 |
+ |
nchanged = hp->bi[i].nrd; |
531 |
+ |
if (hp->bi[i].nrd) { /* free file fragment */ |
532 |
+ |
hp->bl[i] = &emptybeam; |
533 |
+ |
hdsyncbeam(hp, i); |
534 |
+ |
} |
535 |
+ |
hp->bl[i] = NULL; |
536 |
+ |
return(nchanged); |
537 |
+ |
} |
538 |
+ |
|
539 |
+ |
|
540 |
|
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
541 |
|
register HOLO *ha[]; /* section list (NULL terminated) */ |
542 |
|
register int ba[]; /* beam index to go with section */ |
550 |
|
; |
551 |
|
nents = j; |
552 |
|
/* insert each beam from hp */ |
553 |
< |
for (i = nbeams(hp); i > 0; i-- ) { |
553 |
> |
for (i = nbeams(hp); i > 0; i--) { |
554 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
555 |
|
continue; |
556 |
|
if ((j = ++nents) > n) /* grow list if we can */ |
597 |
|
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
598 |
|
break; |
599 |
|
} |
600 |
< |
hdsync(honly); /* synchronize directories as necessary */ |
600 |
> |
hdsync(honly, 0); /* synchronize directories as necessary */ |
601 |
|
} |
602 |
|
|
603 |
|
|