1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines for managing holodeck files |
9 |
* |
10 |
* 9/30/97 GWLarson |
11 |
*/ |
12 |
|
13 |
#include "holo.h" |
14 |
|
15 |
#ifndef CACHESIZE |
16 |
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
#endif |
18 |
#ifndef FREEBEAMS |
19 |
#define FREEBEAMS 512 /* maximum beams to free at a time */ |
20 |
#endif |
21 |
#ifndef PCTFREE |
22 |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
23 |
#endif |
24 |
|
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) */ |
35 |
unsigned long hdclock; /* clock value */ |
36 |
|
37 |
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
38 |
|
39 |
static struct fragment { |
40 |
short nlinks; /* number of holodeck sections using us */ |
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 */ |
46 |
|
47 |
static int nhdfrags; /* size of hdfrag array */ |
48 |
|
49 |
|
50 |
hdattach(fd) /* start tracking file fragments for some section */ |
51 |
register int fd; |
52 |
{ |
53 |
if (fd >= nhdfrags) { |
54 |
if (nhdfrags) |
55 |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
56 |
(fd+1)*sizeof(struct fragment)); |
57 |
else |
58 |
hdfrag = (struct fragment *)malloc( |
59 |
(fd+1)*sizeof(struct fragment)); |
60 |
if (hdfrag == NULL) |
61 |
error(SYSTEM, "out of memory in hdattach"); |
62 |
bzero((char *)(hdfrag+nhdfrags), |
63 |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
64 |
nhdfrags = fd+1; |
65 |
} |
66 |
hdfrag[fd].nlinks++; |
67 |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
68 |
} |
69 |
|
70 |
|
71 |
/* Do we need a routine to locate file fragments given known occupants? */ |
72 |
|
73 |
|
74 |
hdrelease(fd) /* stop tracking file fragments for some section */ |
75 |
register int fd; |
76 |
{ |
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); |
81 |
hdfrag[fd].fi = NULL; |
82 |
hdfrag[fd].nfrags = 0; |
83 |
} |
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 */ |
105 |
HDGRID *hproto; /* holodeck section grid */ |
106 |
{ |
107 |
long fpos; |
108 |
register HOLO *hp; |
109 |
register int n; |
110 |
/* prepare for system errors */ |
111 |
errno = 0; |
112 |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
113 |
error(SYSTEM, "cannot determine holodeck file position"); |
114 |
if (hproto == NULL) { /* assume we're loading it */ |
115 |
HDGRID hpr; |
116 |
/* load header */ |
117 |
if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID)) |
118 |
error(SYSTEM, "cannot load holodeck header"); |
119 |
/* allocate grid */ |
120 |
if ((hp = hdalloc(&hpr)) == NULL) |
121 |
goto memerr; |
122 |
/* load beam directory */ |
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; |
132 |
/* write header and skeleton */ |
133 |
n = nbeams(hp)*sizeof(BEAMI); |
134 |
if (write(fd, (char *)hproto, sizeof(HDGRID)) != |
135 |
sizeof(HDGRID) || |
136 |
write(fd, (char *)(hp->bi+1), n) != n) |
137 |
error(SYSTEM, "cannot write header to holodeck file"); |
138 |
} |
139 |
hp->fd = fd; |
140 |
hp->dirty = 0; |
141 |
biglob(hp)->fo = fpos + sizeof(HDGRID); |
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 |
} |
159 |
/* all done */ |
160 |
return(hp); |
161 |
memerr: |
162 |
error(SYSTEM, "cannot allocate holodeck grid"); |
163 |
} |
164 |
|
165 |
|
166 |
int |
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 holodecks */ |
174 |
n = 0; |
175 |
for (j = 0; hdlist[j] != NULL; j++) |
176 |
n += hdsync(hdlist[j], all); |
177 |
return(n); |
178 |
} |
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) |
187 |
error(SYSTEM, "cannot seek on holodeck file"); |
188 |
n = nbeams(hp)*sizeof(BEAMI); |
189 |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
190 |
error(SYSTEM, "cannot update holodeck section directory"); |
191 |
hp->dirty = 0; |
192 |
return(1); |
193 |
} |
194 |
|
195 |
|
196 |
long |
197 |
hdmemuse(all) /* return memory usage (in bytes) */ |
198 |
int all; /* include overhead (painful) */ |
199 |
{ |
200 |
long total = 0; |
201 |
register int i, j; |
202 |
|
203 |
for (j = 0; hdlist[j] != NULL; j++) { |
204 |
total += blglob(hdlist[j])->nrm * sizeof(RAYVAL); |
205 |
if (all) { |
206 |
total += sizeof(HOLO) + sizeof(BEAM *) + |
207 |
nbeams(hdlist[j]) * |
208 |
(sizeof(BEAM *)+sizeof(BEAMI)); |
209 |
for (i = nbeams(hdlist[j]); i > 0; i--) |
210 |
if (hdlist[j]->bl[i] != NULL) |
211 |
total += sizeof(BEAM); |
212 |
} |
213 |
} |
214 |
if (all) |
215 |
for (j = 0; j < nhdfrags; j++) { |
216 |
total += sizeof(struct fragment); |
217 |
if (hdfrag[j].nfrags) |
218 |
total += FRAGBLK*sizeof(BEAMI) * |
219 |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
220 |
} |
221 |
return(total); |
222 |
} |
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 */ |
248 |
{ |
249 |
long total = 0; |
250 |
register int i, j; |
251 |
|
252 |
for (j = 0; hdlist[j] != NULL; j++) { |
253 |
if (hdlist[j]->fd != fd) |
254 |
continue; |
255 |
total += biglob(hdlist[j])->nrd * sizeof(RAYVAL); |
256 |
if (all) { |
257 |
for (i = nbeams(hdlist[j]); i > 0; i--) |
258 |
if (hdlist[j]->bl[i] != NULL) |
259 |
total += sizeof(RAYVAL) * |
260 |
(hdlist[j]->bl[i]->nrm - |
261 |
hdlist[j]->bi[i].nrd); |
262 |
total += sizeof(HDGRID) + |
263 |
nbeams(hdlist[j])*sizeof(BEAMI); |
264 |
} |
265 |
} |
266 |
return(total); /* does not include fragments */ |
267 |
} |
268 |
|
269 |
|
270 |
RAYVAL * |
271 |
hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */ |
272 |
register HOLO *hp; |
273 |
register int i; |
274 |
int nr; /* number of new rays desired */ |
275 |
{ |
276 |
RAYVAL *p; |
277 |
int n; |
278 |
|
279 |
if (nr <= 0) |
280 |
return(NULL); |
281 |
if (i < 1 | i > nbeams(hp)) |
282 |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
283 |
if (hp->bl[i] != NULL) |
284 |
hp->bl[i]->tick = hdclock; /* preempt swap */ |
285 |
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
286 |
hdfreecache(PCTFREE, NULL); /* free some space */ |
287 |
errno = 0; |
288 |
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
289 |
n = hp->bi[i].nrd + nr; |
290 |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
291 |
goto memerr; |
292 |
blglob(hp)->nrm += n; |
293 |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
294 |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
295 |
error(SYSTEM, "seek error on holodeck file"); |
296 |
n *= sizeof(RAYVAL); |
297 |
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
298 |
error(SYSTEM, |
299 |
"error reading beam from holodeck file"); |
300 |
} |
301 |
} else { /* just grow in memory */ |
302 |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
303 |
hdbsiz(hp->bl[i]->nrm + nr) ); |
304 |
if (hp->bl[i] == NULL) |
305 |
goto memerr; |
306 |
blglob(hp)->nrm += nr; |
307 |
} |
308 |
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
309 |
hp->bl[i]->nrm += nr; /* update in-core structure */ |
310 |
bzero((char *)p, nr*sizeof(RAYVAL)); |
311 |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
312 |
blglob(hp)->tick = hdclock; |
313 |
return(p); /* point to new rays */ |
314 |
memerr: |
315 |
error(SYSTEM, "out of memory in hdnewrays"); |
316 |
} |
317 |
|
318 |
|
319 |
BEAM * |
320 |
hdgetbeam(hp, i) /* get beam (from file if necessary) */ |
321 |
register HOLO *hp; |
322 |
register int i; |
323 |
{ |
324 |
register int n; |
325 |
|
326 |
if (i < 1 | i > nbeams(hp)) |
327 |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
328 |
if (hp->bl[i] == NULL) { /* load from disk */ |
329 |
if (!(n = hp->bi[i].nrd)) |
330 |
return(NULL); |
331 |
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
332 |
hdfreecache(PCTFREE, NULL); /* get free space */ |
333 |
errno = 0; |
334 |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
335 |
error(SYSTEM, "cannot allocate memory for beam"); |
336 |
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
337 |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
338 |
error(SYSTEM, "seek error on holodeck file"); |
339 |
n *= sizeof(RAYVAL); |
340 |
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
341 |
error(SYSTEM, "error reading beam from holodeck file"); |
342 |
} |
343 |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
344 |
blglob(hp)->tick = hdclock; |
345 |
return(hp->bl[i]); |
346 |
} |
347 |
|
348 |
|
349 |
int |
350 |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
351 |
register HOLO *hp; |
352 |
register int i; |
353 |
{ |
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 |
/* 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 = (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++; |
381 |
#ifdef MAXFRAG |
382 |
if (j >= MAXFRAG-1) |
383 |
f->nfrags--; |
384 |
#endif |
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 |
(j+FRAGBLK)*sizeof(BEAMI)); |
392 |
if (f->fi == NULL) |
393 |
error(SYSTEM, |
394 |
"out of memory in hdsyncbeam"); |
395 |
} |
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; |
401 |
} |
402 |
copystruct(f->fi+j, f->fi+j-1); |
403 |
} |
404 |
/* coalesce adjacent fragments */ |
405 |
for (j = k = 0; k < f->nfrags; j++, k++) { |
406 |
if (k > j) |
407 |
copystruct(f->fi+j, f->fi+k); |
408 |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
409 |
f->fi[k+1].nrd*sizeof(RAYVAL) |
410 |
== f->fi[j].fo) { |
411 |
f->fi[j].fo -= |
412 |
f->fi[++k].nrd*sizeof(RAYVAL); |
413 |
f->fi[j].nrd += f->fi[k].nrd; |
414 |
} |
415 |
} |
416 |
f->nfrags = j; |
417 |
} |
418 |
k = -1; /* find closest-sized fragment */ |
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 |
nfo = f->flen; |
426 |
f->flen += nrays*sizeof(RAYVAL); |
427 |
} else { /* else use fragment */ |
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++) |
432 |
copystruct(f->fi+j, f->fi+j+1); |
433 |
} else { /* else shrink it */ |
434 |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
435 |
f->fi[k].nrd -= nrays; |
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->bi[i].fo = nfo; |
453 |
markdirty(hp); /* section directory now out of date */ |
454 |
return(1); |
455 |
} |
456 |
|
457 |
|
458 |
int |
459 |
hdfreebeam(hp, i) /* free beam, writing if dirty */ |
460 |
register HOLO *hp; |
461 |
register int i; |
462 |
{ |
463 |
int nchanged; |
464 |
|
465 |
if (hp == NULL) { /* clear all holodecks */ |
466 |
nchanged = 0; |
467 |
for (i = 0; hdlist[i] != NULL; i++) |
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 = 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 |
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; |
493 |
return(nchanged); |
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 */ |
543 |
int n; /* length of arrays minus 1 */ |
544 |
register HOLO *hp; /* section we're adding from */ |
545 |
{ |
546 |
register int i, j; |
547 |
int nents; |
548 |
/* find last entry in LRU list */ |
549 |
for (j = 0; ha[j] != NULL; j++) |
550 |
; |
551 |
nents = j; |
552 |
/* insert each beam from hp */ |
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 */ |
557 |
nents--; |
558 |
for ( ; ; ) { /* bubble into place */ |
559 |
if (!--j || hp->bl[i]->tick >= |
560 |
ha[j-1]->bl[ba[j-1]]->tick) { |
561 |
ha[j] = hp; |
562 |
ba[j] = i; |
563 |
break; |
564 |
} |
565 |
ha[j] = ha[j-1]; |
566 |
ba[j] = ba[j-1]; |
567 |
} |
568 |
} |
569 |
ha[nents] = NULL; /* all done */ |
570 |
ba[nents] = 0; |
571 |
} |
572 |
|
573 |
|
574 |
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
575 |
int pct; /* maximum percentage to free */ |
576 |
register HOLO *honly; /* NULL means check all */ |
577 |
{ |
578 |
HOLO *hp[FREEBEAMS+1]; |
579 |
int bn[FREEBEAMS+1]; |
580 |
int freetarget; |
581 |
register int i; |
582 |
/* compute free target */ |
583 |
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
584 |
hdmemuse(0)/sizeof(RAYVAL) ; |
585 |
freetarget = freetarget*pct/100; |
586 |
/* find least recently used */ |
587 |
hp[0] = NULL; |
588 |
bn[0] = 0; |
589 |
if (honly != NULL) |
590 |
hdlrulist(hp, bn, FREEBEAMS, honly); |
591 |
else |
592 |
for (i = 0; hdlist[i] != NULL; i++) |
593 |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
594 |
/* free LRU beams */ |
595 |
for (i = 0; hp[i] != NULL; i++) { |
596 |
hdfreebeam(hp[i], bn[i]); |
597 |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
598 |
break; |
599 |
} |
600 |
hdsync(honly, 0); /* synchronize directories as necessary */ |
601 |
} |
602 |
|
603 |
|
604 |
hddone(hp) /* clean up holodeck section and free */ |
605 |
register HOLO *hp; /* NULL means clean up all */ |
606 |
{ |
607 |
register int i; |
608 |
|
609 |
if (hp == NULL) { /* NULL means clean up everything */ |
610 |
while (hdlist[0] != NULL) |
611 |
hddone(hdlist[0]); |
612 |
return; |
613 |
} |
614 |
/* flush all data and free memory */ |
615 |
hdflush(hp); |
616 |
/* release fragment resources */ |
617 |
hdrelease(hp->fd); |
618 |
/* remove hp from active list */ |
619 |
for (i = 0; hdlist[i] != NULL; i++) |
620 |
if (hdlist[i] == hp) { |
621 |
while ((hdlist[i] = hdlist[i+1]) != NULL) |
622 |
i++; |
623 |
break; |
624 |
} |
625 |
free((char *)hp->bl); /* free beam list */ |
626 |
free((char *)hp); /* free holodeck struct */ |
627 |
} |