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 |
– |
#ifndef FFDMAX |
25 |
– |
#define FFDMAX 64 /* max. file descriptor for tracking */ |
26 |
– |
#endif |
24 |
|
#ifndef MAXFRAG |
25 |
< |
#define MAXFRAG 2048 /* maximum fragments tracked in each file */ |
25 |
> |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
26 |
|
#endif |
27 |
|
|
28 |
< |
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
28 |
> |
#ifndef BSD |
29 |
> |
#define write writebuf /* safe i/o routines */ |
30 |
> |
#define read readbuf |
31 |
> |
#endif |
32 |
> |
|
33 |
> |
#define FRAGBLK 256 /* number of fragments to allocate at a time */ |
34 |
> |
|
35 |
> |
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
36 |
|
unsigned long hdclock; /* clock value */ |
37 |
|
|
38 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
39 |
|
|
40 |
< |
static struct fragment { |
40 |
> |
static struct fraglist { |
41 |
|
short nlinks; /* number of holodeck sections using us */ |
42 |
< |
short nfrags; /* number of known fragments */ |
43 |
< |
BEAMI fi[MAXFRAG]; /* fragments, descending file position */ |
42 |
> |
short writerr; /* write error encountered */ |
43 |
> |
int nfrags; /* number of known fragments */ |
44 |
> |
BEAMI *fi; /* fragments, descending file position */ |
45 |
|
long flen; /* last known file length */ |
46 |
< |
} *hdfrag[FFDMAX]; /* fragment lists, indexed by file descriptor */ |
46 |
> |
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
47 |
|
|
48 |
+ |
static int nhdfragls; /* size of hdfragl array */ |
49 |
|
|
50 |
+ |
|
51 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
52 |
|
register int fd; |
53 |
|
{ |
54 |
< |
if (fd >= FFDMAX) |
55 |
< |
return; /* descriptor out of range */ |
56 |
< |
if (hdfrag[fd] == NULL) { |
57 |
< |
if ((hdfrag[fd] = (struct fragment *) |
58 |
< |
malloc(sizeof(struct fragment))) == NULL) |
59 |
< |
return; /* should flag error? */ |
60 |
< |
hdfrag[fd]->nlinks = 0; |
61 |
< |
hdfrag[fd]->nfrags = 0; |
54 |
> |
if (fd >= nhdfragls) { |
55 |
> |
if (nhdfragls) |
56 |
> |
hdfragl = (struct fraglist *)realloc((char *)hdfragl, |
57 |
> |
(fd+1)*sizeof(struct fraglist)); |
58 |
> |
else |
59 |
> |
hdfragl = (struct fraglist *)malloc( |
60 |
> |
(fd+1)*sizeof(struct fraglist)); |
61 |
> |
if (hdfragl == NULL) |
62 |
> |
error(SYSTEM, "out of memory in hdattach"); |
63 |
> |
bzero((char *)(hdfragl+nhdfragls), |
64 |
> |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
65 |
> |
nhdfragls = fd+1; |
66 |
|
} |
67 |
< |
hdfrag[fd]->nlinks++; |
68 |
< |
hdfrag[fd]->flen = lseek(fd, 0L, 2); /* get file length */ |
67 |
> |
hdfragl[fd].nlinks++; |
68 |
> |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
69 |
|
} |
70 |
|
|
71 |
|
|
75 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
76 |
|
register int fd; |
77 |
|
{ |
78 |
< |
if (fd >= FFDMAX || hdfrag[fd] == NULL) |
78 |
> |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
79 |
|
return; |
80 |
< |
if (!--hdfrag[fd]->nlinks) { |
81 |
< |
free((char *)hdfrag[fd]); |
82 |
< |
hdfrag[fd] = NULL; |
80 |
> |
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
81 |
> |
free((char *)hdfragl[fd].fi); |
82 |
> |
hdfragl[fd].fi = NULL; |
83 |
> |
hdfragl[fd].nfrags = 0; |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
+ |
markdirty(hp) /* mark holodeck section directory dirty */ |
89 |
+ |
register HOLO *hp; |
90 |
+ |
{ |
91 |
+ |
static BEAMI smudge = {0, -1}; |
92 |
+ |
|
93 |
+ |
if (hp->dirty) /* already marked? */ |
94 |
+ |
return; |
95 |
+ |
hp->dirty = 1; |
96 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
97 |
+ |
|| write(hp->fd, (char *)&smudge, |
98 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
99 |
+ |
error(SYSTEM, "seek/write error in markdirty"); |
100 |
+ |
} |
101 |
+ |
|
102 |
+ |
|
103 |
|
HOLO * |
104 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
105 |
|
int fd; /* corresponding file descriptor */ |
124 |
|
n = nbeams(hp)*sizeof(BEAMI); |
125 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
126 |
|
error(SYSTEM, "failure loading holodeck directory"); |
127 |
+ |
/* check that it's clean */ |
128 |
+ |
if (hp->bi[nbeams(hp)].fo < 0) |
129 |
+ |
error(USER, "dirty holodeck section"); |
130 |
|
} else { /* assume we're creating it */ |
131 |
|
if ((hp = hdalloc(hproto)) == NULL) |
132 |
|
goto memerr; |
140 |
|
hp->fd = fd; |
141 |
|
hp->dirty = 0; |
142 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
143 |
< |
biglob(hp)->nrd = 0; /* count rays on disk */ |
144 |
< |
for (n = nbeams(hp); n > 0; n--) |
145 |
< |
biglob(hp)->nrd += hp->bi[n].nrd; |
143 |
> |
/* start tracking fragments */ |
144 |
> |
hdattach(fd); |
145 |
> |
/* check rays on disk */ |
146 |
> |
fpos = hdfilen(fd); |
147 |
> |
biglob(hp)->nrd = 0; |
148 |
> |
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
149 |
> |
if (hp->bi[n].nrd) |
150 |
> |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
151 |
> |
hp->bi[n].nrd = 0; /* off end */ |
152 |
> |
else |
153 |
> |
biglob(hp)->nrd += hp->bi[n].nrd; |
154 |
|
/* add to holodeck list */ |
155 |
|
for (n = 0; n < HDMAX; n++) |
156 |
|
if (hdlist[n] == NULL) { |
157 |
|
hdlist[n] = hp; |
158 |
|
break; |
159 |
|
} |
122 |
– |
/* start tracking fragments (last) */ |
123 |
– |
hdattach(fd); |
160 |
|
/* all done */ |
161 |
|
return(hp); |
162 |
|
memerr: |
165 |
|
|
166 |
|
|
167 |
|
int |
168 |
< |
hdsync(hp) /* update directory on disk if necessary */ |
168 |
> |
hdsync(hp, all) /* update beams and directory on disk */ |
169 |
|
register HOLO *hp; |
170 |
+ |
int all; |
171 |
|
{ |
172 |
|
register int j, n; |
173 |
|
|
174 |
< |
if (hp == NULL) { /* do all */ |
174 |
> |
if (hp == NULL) { /* do all holodecks */ |
175 |
|
n = 0; |
176 |
|
for (j = 0; hdlist[j] != NULL; j++) |
177 |
< |
n += hdsync(hdlist[j]); |
177 |
> |
n += hdsync(hdlist[j], all); |
178 |
|
return(n); |
179 |
|
} |
180 |
< |
if (!hp->dirty) /* check first */ |
180 |
> |
/* sync the beams */ |
181 |
> |
for (j = (all ? nbeams(hp) : 0); j > 0; j--) |
182 |
> |
if (hp->bl[j] != NULL) |
183 |
> |
hdsyncbeam(hp, j); |
184 |
> |
if (!hp->dirty) /* directory clean? */ |
185 |
|
return(0); |
186 |
|
errno = 0; |
187 |
|
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
< |
long |
197 |
> |
unsigned |
198 |
|
hdmemuse(all) /* return memory usage (in bytes) */ |
199 |
|
int all; /* include overhead (painful) */ |
200 |
|
{ |
213 |
|
} |
214 |
|
} |
215 |
|
if (all) |
216 |
< |
for (j = 0; j < FFDMAX; j++) |
217 |
< |
if (hdfrag[j] != NULL) |
218 |
< |
total += sizeof(struct fragment); |
216 |
> |
for (j = 0; j < nhdfragls; j++) { |
217 |
> |
total += sizeof(struct fraglist); |
218 |
> |
if (hdfragl[j].nfrags) |
219 |
> |
total += FRAGBLK*sizeof(BEAMI) * |
220 |
> |
((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; |
221 |
> |
} |
222 |
|
return(total); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
long |
227 |
+ |
hdfilen(fd) /* return file length for fd */ |
228 |
+ |
int fd; |
229 |
+ |
{ |
230 |
+ |
long fpos, flen; |
231 |
+ |
|
232 |
+ |
if (fd < 0) |
233 |
+ |
return(-1); |
234 |
+ |
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
235 |
+ |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
236 |
+ |
return(-1); |
237 |
+ |
flen = lseek(fd, 0L, 2); |
238 |
+ |
lseek(fd, fpos, 0); |
239 |
+ |
return(flen); |
240 |
+ |
} |
241 |
+ |
return(hdfragl[fd].flen); |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
|
245 |
+ |
long |
246 |
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
247 |
|
int fd; /* open file descriptor to check */ |
248 |
|
int all; /* include overhead and unflushed data */ |
309 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
310 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
311 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
312 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
313 |
< |
blglob(hp)->tick = hdclock; |
312 |
> |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
313 |
> |
blglob(hp)->tick = hdclock++; |
314 |
|
return(p); /* point to new rays */ |
315 |
|
memerr: |
316 |
|
error(SYSTEM, "out of memory in hdnewrays"); |
341 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
342 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
343 |
|
} |
344 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
345 |
< |
blglob(hp)->tick = hdclock; |
344 |
> |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
345 |
> |
blglob(hp)->tick = hdclock++; |
346 |
|
return(hp->bl[i]); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
int |
351 |
< |
hdgetbi(hp, i) /* allocate a file fragment */ |
351 |
> |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
352 |
> |
register HDBEAMI *hb1, *hb2; |
353 |
> |
{ |
354 |
> |
register long c; |
355 |
> |
/* residents go first */ |
356 |
> |
if (hb2->h->bl[hb2->b] != NULL) |
357 |
> |
return(hb1->h->bl[hb1->b] == NULL); |
358 |
> |
if (hb1->h->bl[hb1->b] != NULL) |
359 |
> |
return(-1); |
360 |
> |
/* otherwise sort by file descriptor */ |
361 |
> |
if ((c = hb1->h->fd - hb2->h->fd)) |
362 |
> |
return(c); |
363 |
> |
/* then by position in file */ |
364 |
> |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
365 |
> |
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
366 |
> |
} |
367 |
> |
|
368 |
> |
|
369 |
> |
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
370 |
> |
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
371 |
> |
int n; /* list length */ |
372 |
> |
int (*bf)(); /* callback function (optional) */ |
373 |
> |
{ |
374 |
> |
unsigned origcachesize, memuse; |
375 |
> |
register BEAM *bp; |
376 |
> |
int bytesloaded, needbytes, bytes2free; |
377 |
> |
register int i; |
378 |
> |
/* precheck consistency */ |
379 |
> |
for (i = n; i--; ) |
380 |
> |
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
381 |
> |
error(CONSISTENCY, "bad beam in hdloadbeams"); |
382 |
> |
/* sort list for optimal access */ |
383 |
> |
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
384 |
> |
bytesloaded = 0; /* run through loaded beams */ |
385 |
> |
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
386 |
> |
bp->tick = hdclock; /* preempt swap */ |
387 |
> |
bytesloaded += bp->nrm; |
388 |
> |
if (bf != NULL) |
389 |
> |
(*bf)(bp, hb->h, hb->b); |
390 |
> |
} |
391 |
> |
bytesloaded *= sizeof(RAYVAL); |
392 |
> |
if ((origcachesize = hdcachesize) > 0) { |
393 |
> |
needbytes = 0; /* figure out memory needs */ |
394 |
> |
for (i = n; i--; ) |
395 |
> |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
396 |
> |
needbytes *= sizeof(RAYVAL); |
397 |
> |
do { /* free enough memory */ |
398 |
> |
memuse = hdmemuse(0); |
399 |
> |
bytes2free = needbytes - (int)(hdcachesize-memuse); |
400 |
> |
if (bytes2free > (int)(memuse - bytesloaded)) |
401 |
> |
bytes2free = memuse - bytesloaded; |
402 |
> |
} while (bytes2free > 0 && |
403 |
> |
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
404 |
> |
hdcachesize = 0; /* load beams w/o swap */ |
405 |
> |
} |
406 |
> |
for (i = 0; i < n; i++) |
407 |
> |
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
408 |
> |
(*bf)(bp, hb[i].h, hb[i].b); |
409 |
> |
hdcachesize = origcachesize; /* resume dynamic swapping */ |
410 |
> |
} |
411 |
> |
|
412 |
> |
|
413 |
> |
int |
414 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
415 |
|
register HOLO *hp; |
416 |
|
register int i; |
417 |
|
{ |
418 |
< |
int nrays = hp->bl[i]->nrm; |
419 |
< |
|
420 |
< |
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
418 |
> |
unsigned int nrays; |
419 |
> |
long nfo; |
420 |
> |
unsigned int n; |
421 |
> |
/* check file status */ |
422 |
> |
if (hdfragl[hp->fd].writerr) |
423 |
> |
return(-1); |
424 |
> |
#ifdef DEBUG |
425 |
> |
if (i < 1 | i > nbeams(hp)) |
426 |
> |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
427 |
> |
#endif |
428 |
> |
/* is current fragment OK? */ |
429 |
> |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
430 |
|
return(0); |
431 |
< |
|
432 |
< |
if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ |
431 |
> |
/* locate fragment */ |
432 |
> |
if (hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks) /* untracked */ |
433 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
434 |
|
|
435 |
|
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
436 |
< |
hdfrag[hp->fd]->flen) /* EOF special case */ |
437 |
< |
hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
436 |
> |
hdfragl[hp->fd].flen) /* EOF special case */ |
437 |
> |
hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
438 |
|
|
439 |
|
else { /* general case */ |
440 |
< |
register struct fragment *f = hdfrag[hp->fd]; |
440 |
> |
register struct fraglist *f = &hdfragl[hp->fd]; |
441 |
|
register int j, k; |
442 |
< |
/* relinquish old fragment */ |
442 |
> |
n = f->nfrags; /* relinquish old fragment */ |
443 |
|
if (hp->bi[i].nrd) { |
444 |
< |
if ((j = ++f->nfrags) >= MAXFRAG) |
444 |
> |
j = f->nfrags++; |
445 |
> |
#if MAXFRAG |
446 |
> |
if (j >= MAXFRAG-1) |
447 |
|
f->nfrags--; |
448 |
< |
for ( ; ; ) { /* stick it in our descending list */ |
449 |
< |
if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { |
448 |
> |
#endif |
449 |
> |
if (j % FRAGBLK == 0) { /* more frag. space */ |
450 |
> |
if (f->fi == NULL) |
451 |
> |
f->fi = (BEAMI *)malloc( |
452 |
> |
FRAGBLK*sizeof(BEAMI)); |
453 |
> |
else |
454 |
> |
f->fi = (BEAMI *)realloc((char *)f->fi, |
455 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
456 |
> |
if (f->fi == NULL) |
457 |
> |
error(SYSTEM, |
458 |
> |
"out of memory in hdsyncbeam"); |
459 |
> |
} |
460 |
> |
for ( ; ; j--) { /* insert in descending list */ |
461 |
> |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
462 |
|
f->fi[j].fo = hp->bi[i].fo; |
463 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
464 |
|
break; |
465 |
|
} |
466 |
< |
copystruct(f->fi+j, f->fi+j-1); |
466 |
> |
copystruct(f->fi+j, f->fi+(j-1)); |
467 |
|
} |
468 |
|
/* coalesce adjacent fragments */ |
469 |
< |
for (j = k = 0; k < f->nfrags; j++, k++) { |
470 |
< |
if (k > j) |
471 |
< |
copystruct(f->fi+j, f->fi+k); |
472 |
< |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
473 |
< |
f->fi[k+1].nrd*sizeof(RAYVAL) |
325 |
< |
== f->fi[j].fo) { |
326 |
< |
f->fi[j].fo -= |
327 |
< |
f->fi[++k].nrd*sizeof(RAYVAL); |
328 |
< |
f->fi[j].nrd += f->fi[k].nrd; |
329 |
< |
} |
469 |
> |
if (j && f->fi[j-1].fo == f->fi[j].fo + |
470 |
> |
f->fi[j].nrd*sizeof(RAYVAL)) { |
471 |
> |
f->fi[j].nrd += f->fi[j-1].nrd; |
472 |
> |
f->fi[j-1].nrd = 0; |
473 |
> |
n = j-1; |
474 |
|
} |
475 |
< |
f->nfrags = j; |
475 |
> |
if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + |
476 |
> |
f->fi[j+1].nrd*sizeof(RAYVAL)) { |
477 |
> |
f->fi[j+1].nrd += f->fi[j].nrd; |
478 |
> |
f->fi[j].nrd = 0; |
479 |
> |
if (j < n) n = j; |
480 |
> |
} |
481 |
|
} |
482 |
|
k = -1; /* find closest-sized fragment */ |
483 |
< |
for (j = f->nfrags; j-- > 0; ) |
483 |
> |
for (j = (nrays ? f->nfrags : 0); j-- > 0; ) |
484 |
|
if (f->fi[j].nrd >= nrays && |
485 |
|
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
486 |
|
if (f->fi[k=j].nrd == nrays) |
487 |
|
break; |
488 |
|
if (k < 0) { /* no fragment -- extend file */ |
489 |
< |
hp->bi[i].fo = f->flen; |
489 |
> |
nfo = f->flen; |
490 |
|
f->flen += nrays*sizeof(RAYVAL); |
491 |
|
} else { /* else use fragment */ |
492 |
< |
hp->bi[i].fo = f->fi[k].fo; |
493 |
< |
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
494 |
< |
f->nfrags--; |
495 |
< |
for (j = k; j < f->nfrags; j++) |
347 |
< |
copystruct(f->fi+j, f->fi+j+1); |
348 |
< |
} else { /* else shrink it */ |
349 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
350 |
< |
f->fi[k].nrd -= nrays; |
351 |
< |
} |
492 |
> |
nfo = f->fi[k].fo; |
493 |
> |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
494 |
> |
if (!(f->fi[k].nrd -= nrays) && k < n) |
495 |
> |
n = k; |
496 |
|
} |
497 |
+ |
/* delete empty remnants */ |
498 |
+ |
for (j = k = n; k < f->nfrags; j++, k++) { |
499 |
+ |
while (f->fi[k].nrd == 0) |
500 |
+ |
if (++k >= f->nfrags) |
501 |
+ |
goto endloop; |
502 |
+ |
if (k > j) |
503 |
+ |
copystruct(f->fi+j, f->fi+k); |
504 |
+ |
} |
505 |
+ |
endloop: |
506 |
+ |
f->nfrags = j; |
507 |
|
} |
508 |
+ |
if (nrays) { /* write the new fragment */ |
509 |
+ |
errno = 0; |
510 |
+ |
if (lseek(hp->fd, nfo, 0) < 0) |
511 |
+ |
error(SYSTEM, "cannot seek on holodeck file"); |
512 |
+ |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
513 |
+ |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
514 |
+ |
hdfragl[hp->fd].writerr++; |
515 |
+ |
hdsync(hp, 0); /* sync directory */ |
516 |
+ |
error(SYSTEM, "write error in hdsyncbeam"); |
517 |
+ |
} |
518 |
+ |
} |
519 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
520 |
|
hp->bi[i].nrd = nrays; |
521 |
< |
hp->dirty++; /* section directory now out of date */ |
521 |
> |
hp->bi[i].fo = nfo; |
522 |
> |
markdirty(hp); /* section directory now out of date */ |
523 |
|
return(1); |
524 |
|
} |
525 |
|
|
529 |
|
register HOLO *hp; |
530 |
|
register int i; |
531 |
|
{ |
532 |
< |
int nchanged, n; |
532 |
> |
int nchanged; |
533 |
|
|
534 |
|
if (hp == NULL) { /* clear all holodecks */ |
535 |
|
nchanged = 0; |
537 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
538 |
|
return(nchanged); |
539 |
|
} |
540 |
+ |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
541 |
+ |
return(0); |
542 |
|
if (i == 0) { /* clear entire holodeck */ |
543 |
|
nchanged = 0; |
544 |
< |
for (i = 1; i <= nbeams(hp); i++) |
545 |
< |
nchanged += hdfreebeam(hp, i); |
544 |
> |
for (i = nbeams(hp); i > 0; i--) |
545 |
> |
if (hp->bl[i] != NULL) |
546 |
> |
nchanged += hdfreebeam(hp, i); |
547 |
|
return(nchanged); |
548 |
|
} |
549 |
+ |
#ifdef DEBUG |
550 |
|
if (i < 1 | i > nbeams(hp)) |
551 |
|
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
552 |
+ |
#endif |
553 |
|
if (hp->bl[i] == NULL) |
554 |
|
return(0); |
555 |
|
/* check for additions */ |
556 |
|
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
557 |
< |
if (nchanged) { |
558 |
< |
hdgetbi(hp, i); /* allocate a file position */ |
388 |
< |
errno = 0; |
389 |
< |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
390 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
391 |
< |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
392 |
< |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
393 |
< |
error(SYSTEM, "write error in hdfreebeam"); |
394 |
< |
} |
557 |
> |
if (nchanged) |
558 |
> |
hdsyncbeam(hp, i); /* write new fragment */ |
559 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
560 |
|
free((char *)hp->bl[i]); /* free memory */ |
561 |
|
hp->bl[i] = NULL; |
563 |
|
} |
564 |
|
|
565 |
|
|
566 |
< |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
567 |
< |
register HOLO *ha[]; /* section list (NULL terminated) */ |
568 |
< |
register int ba[]; /* beam index to go with section */ |
569 |
< |
int n; /* length of arrays minus 1 */ |
566 |
> |
int |
567 |
> |
hdkillbeam(hp, i) /* delete beam from holodeck */ |
568 |
> |
register HOLO *hp; |
569 |
> |
register int i; |
570 |
> |
{ |
571 |
> |
static BEAM emptybeam; |
572 |
> |
int nchanged; |
573 |
> |
|
574 |
> |
if (hp == NULL) { /* clobber all holodecks */ |
575 |
> |
nchanged = 0; |
576 |
> |
for (i = 0; hdlist[i] != NULL; i++) |
577 |
> |
nchanged += hdkillbeam(hdlist[i], 0); |
578 |
> |
return(nchanged); |
579 |
> |
} |
580 |
> |
if (i == 0) { /* clobber entire holodeck */ |
581 |
> |
nchanged = 0; |
582 |
> |
for (i = nbeams(hp); i > 0; i--) |
583 |
> |
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
584 |
> |
nchanged += hdkillbeam(hp, i); |
585 |
> |
#ifdef DEBUG |
586 |
> |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
587 |
> |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
588 |
> |
#endif |
589 |
> |
return(nchanged); |
590 |
> |
} |
591 |
> |
#ifdef DEBUG |
592 |
> |
if (i < 1 | i > nbeams(hp)) |
593 |
> |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
594 |
> |
#endif |
595 |
> |
if (hp->bl[i] != NULL) { /* free memory */ |
596 |
> |
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
597 |
> |
free((char *)hp->bl[i]); |
598 |
> |
} else |
599 |
> |
nchanged = hp->bi[i].nrd; |
600 |
> |
if (hp->bi[i].nrd) { /* free file fragment */ |
601 |
> |
hp->bl[i] = &emptybeam; |
602 |
> |
hdsyncbeam(hp, i); |
603 |
> |
} |
604 |
> |
hp->bl[i] = NULL; |
605 |
> |
return(nchanged); |
606 |
> |
} |
607 |
> |
|
608 |
> |
|
609 |
> |
int |
610 |
> |
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
611 |
> |
register HDBEAMI *hb; /* beam list */ |
612 |
> |
int nents; /* current list length */ |
613 |
> |
int n; /* maximum list length */ |
614 |
|
register HOLO *hp; /* section we're adding from */ |
615 |
|
{ |
616 |
|
register int i, j; |
409 |
– |
int nents; |
410 |
– |
/* find last entry in LRU list */ |
411 |
– |
for (j = 0; ha[j] != NULL; j++) |
412 |
– |
; |
413 |
– |
nents = j; |
617 |
|
/* insert each beam from hp */ |
618 |
< |
for (i = nbeams(hp); i > 0; i-- ) { |
618 |
> |
for (i = nbeams(hp); i > 0; i--) { |
619 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
620 |
|
continue; |
621 |
< |
if ((j = ++nents) > n) /* grow list if we can */ |
621 |
> |
#if 0 |
622 |
> |
if (hp->bl[i]->tick == hdclock) /* preempt swap? */ |
623 |
> |
continue; |
624 |
> |
#endif |
625 |
> |
if ((j = ++nents) >= n) /* grow list if we can */ |
626 |
|
nents--; |
627 |
|
for ( ; ; ) { /* bubble into place */ |
628 |
|
if (!--j || hp->bl[i]->tick >= |
629 |
< |
ha[j-1]->bl[ba[j-1]]->tick) { |
630 |
< |
ha[j] = hp; |
631 |
< |
ba[j] = i; |
629 |
> |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
630 |
> |
hb[j].h = hp; |
631 |
> |
hb[j].b = i; |
632 |
|
break; |
633 |
|
} |
634 |
< |
ha[j] = ha[j-1]; |
428 |
< |
ba[j] = ba[j-1]; |
634 |
> |
copystruct(hb+j, hb+(j-1)); |
635 |
|
} |
636 |
|
} |
637 |
< |
ha[nents] = NULL; /* all done */ |
432 |
< |
ba[nents] = 0; |
637 |
> |
return(nents); /* return new list length */ |
638 |
|
} |
639 |
|
|
640 |
|
|
641 |
+ |
int |
642 |
|
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
643 |
|
int pct; /* maximum percentage to free */ |
644 |
|
register HOLO *honly; /* NULL means check all */ |
645 |
|
{ |
646 |
< |
HOLO *hp[FREEBEAMS+1]; |
441 |
< |
int bn[FREEBEAMS+1]; |
646 |
> |
HDBEAMI hb[FREEBEAMS]; |
647 |
|
int freetarget; |
648 |
+ |
int n; |
649 |
|
register int i; |
650 |
|
/* compute free target */ |
651 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
652 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
653 |
|
freetarget = freetarget*pct/100; |
654 |
+ |
if (freetarget <= 0) |
655 |
+ |
return(0); |
656 |
|
/* find least recently used */ |
657 |
< |
hp[0] = NULL; |
450 |
< |
bn[0] = 0; |
657 |
> |
n = 0; |
658 |
|
if (honly != NULL) |
659 |
< |
hdlrulist(hp, bn, FREEBEAMS, honly); |
659 |
> |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
660 |
|
else |
661 |
|
for (i = 0; hdlist[i] != NULL; i++) |
662 |
< |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
662 |
> |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
663 |
|
/* free LRU beams */ |
664 |
< |
for (i = 0; hp[i] != NULL; i++) { |
665 |
< |
hdfreebeam(hp[i], bn[i]); |
666 |
< |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
664 |
> |
for (i = 0; i < n; i++) { |
665 |
> |
hdfreebeam(hb[i].h, hb[i].b); |
666 |
> |
if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) |
667 |
|
break; |
668 |
|
} |
669 |
< |
hdsync(honly); /* synchronize directories as necessary */ |
669 |
> |
hdsync(honly, 0); /* synchronize directories as necessary */ |
670 |
> |
return(-freetarget); /* return how far past goal we went */ |
671 |
|
} |
672 |
|
|
673 |
|
|