1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1999 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
13 |
|
#include "holo.h" |
14 |
|
|
15 |
|
#ifndef CACHESIZE |
16 |
< |
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
16 |
> |
#ifdef BIGMEM |
17 |
> |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
18 |
> |
#else |
19 |
> |
#define CACHESIZE 5 |
20 |
|
#endif |
21 |
+ |
#endif |
22 |
|
#ifndef FREEBEAMS |
23 |
< |
#define FREEBEAMS 512 /* maximum beams to free at a time */ |
23 |
> |
#define FREEBEAMS 1500 /* maximum beams to free at a time */ |
24 |
|
#endif |
25 |
|
#ifndef PCTFREE |
26 |
< |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
26 |
> |
#define PCTFREE 15 /* maximum fraction to free (%) */ |
27 |
|
#endif |
28 |
+ |
#ifndef MAXFRAGB |
29 |
+ |
#define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */ |
30 |
+ |
#endif |
31 |
+ |
#ifndef FF_DEFAULT |
32 |
+ |
/* when to free a beam fragment */ |
33 |
+ |
#define FF_DEFAULT (FF_WRITE|FF_KILL) |
34 |
+ |
#endif |
35 |
+ |
#ifndef MINDIRSEL |
36 |
+ |
/* minimum directory seek length */ |
37 |
+ |
#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) |
38 |
+ |
#endif |
39 |
|
|
25 |
– |
/* define MAXFRAG if you want to limit fragment tracking memory */ |
26 |
– |
|
40 |
|
#ifndef BSD |
41 |
|
#define write writebuf /* safe i/o routines */ |
42 |
|
#define read readbuf |
43 |
|
#endif |
44 |
|
|
45 |
< |
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
45 |
> |
#define FRAGBLK 512 /* number of fragments to allocate at a time */ |
46 |
|
|
47 |
< |
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
48 |
< |
unsigned long hdclock; /* clock value */ |
47 |
> |
int hdfragflags = FF_DEFAULT; /* tells when to free fragments */ |
48 |
> |
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
49 |
> |
unsigned long hdclock; /* clock value */ |
50 |
|
|
51 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
52 |
|
|
53 |
< |
static struct fragment { |
53 |
> |
static struct fraglist { |
54 |
|
short nlinks; /* number of holodeck sections using us */ |
55 |
< |
short nfrags; /* number of known fragments */ |
55 |
> |
short writable; /* 0 read-only, <0 write error encountered */ |
56 |
> |
int nfrags; /* number of known fragments */ |
57 |
|
BEAMI *fi; /* fragments, descending file position */ |
58 |
|
long flen; /* last known file length */ |
59 |
< |
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
59 |
> |
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
60 |
|
|
61 |
< |
static int nhdfrags; /* size of hdfrag array */ |
61 |
> |
static int nhdfragls; /* size of hdfragl array */ |
62 |
|
|
63 |
|
|
64 |
< |
hdattach(fd) /* start tracking file fragments for some section */ |
65 |
< |
register int fd; |
64 |
> |
HOLO * |
65 |
> |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
66 |
> |
HDGRID *hproto; |
67 |
|
{ |
68 |
< |
if (fd >= nhdfrags) { |
69 |
< |
if (nhdfrags) |
70 |
< |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
71 |
< |
(fd+1)*sizeof(struct fragment)); |
72 |
< |
else |
73 |
< |
hdfrag = (struct fragment *)malloc( |
74 |
< |
(fd+1)*sizeof(struct fragment)); |
75 |
< |
if (hdfrag == NULL) |
76 |
< |
error(SYSTEM, "out of memory in hdattach"); |
77 |
< |
bzero((char *)(hdfrag+nhdfrags), |
78 |
< |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
79 |
< |
nhdfrags = fd+1; |
68 |
> |
HOLO hdhead; |
69 |
> |
register HOLO *hp; |
70 |
> |
int n; |
71 |
> |
/* copy grid to temporary header */ |
72 |
> |
bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); |
73 |
> |
/* compute grid vectors and sizes */ |
74 |
> |
hdcompgrid(&hdhead); |
75 |
> |
/* allocate header with directory */ |
76 |
> |
n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); |
77 |
> |
if ((hp = (HOLO *)malloc(n)) == NULL) |
78 |
> |
return(NULL); |
79 |
> |
/* copy header information */ |
80 |
> |
copystruct(hp, &hdhead); |
81 |
> |
/* allocate and clear beam list */ |
82 |
> |
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
83 |
> |
if (hp->bl == NULL) { |
84 |
> |
free((char *)hp); |
85 |
> |
return(NULL); |
86 |
|
} |
87 |
< |
hdfrag[fd].nlinks++; |
88 |
< |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
87 |
> |
bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
88 |
> |
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
89 |
> |
hp->fd = -1; |
90 |
> |
hp->dirty = 0; |
91 |
> |
hp->priv = NULL; |
92 |
> |
/* clear beam directory */ |
93 |
> |
bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
94 |
> |
return(hp); /* all is well */ |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
< |
/* Do we need a routine to locate file fragments given known occupants? */ |
98 |
> |
char * |
99 |
> |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
100 |
> |
char *ptr; |
101 |
> |
unsigned siz; |
102 |
> |
char *rout; |
103 |
> |
{ |
104 |
> |
register char *newp; |
105 |
> |
/* call malloc/realloc */ |
106 |
> |
if (ptr == NULL) newp = (char *)malloc(siz); |
107 |
> |
else newp = (char *)realloc(ptr, siz); |
108 |
> |
/* check success */ |
109 |
> |
if (newp == NULL && rout != NULL) { |
110 |
> |
hdfreecache(25, NULL); /* free some memory */ |
111 |
> |
errno = 0; /* retry */ |
112 |
> |
newp = hdrealloc(ptr, siz, NULL); |
113 |
> |
if (newp == NULL) { /* give up and report error */ |
114 |
> |
sprintf(errmsg, "out of memory in %s", rout); |
115 |
> |
error(SYSTEM, errmsg); |
116 |
> |
} |
117 |
> |
} |
118 |
> |
return(newp); |
119 |
> |
} |
120 |
|
|
121 |
|
|
122 |
< |
hdrelease(fd) /* stop tracking file fragments for some section */ |
122 |
> |
hdattach(fd, wr) /* start tracking file fragments for some section */ |
123 |
|
register int fd; |
124 |
+ |
int wr; |
125 |
|
{ |
126 |
< |
if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) |
127 |
< |
return; |
128 |
< |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
129 |
< |
free((char *)hdfrag[fd].fi); |
130 |
< |
hdfrag[fd].fi = NULL; |
131 |
< |
hdfrag[fd].nfrags = 0; |
126 |
> |
if (fd >= nhdfragls) { |
127 |
> |
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
128 |
> |
(fd+1)*sizeof(struct fraglist), "hdattach"); |
129 |
> |
bzero((char *)(hdfragl+nhdfragls), |
130 |
> |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
131 |
> |
nhdfragls = fd+1; |
132 |
|
} |
133 |
+ |
hdfragl[fd].nlinks++; |
134 |
+ |
hdfragl[fd].writable = wr; /* set writable flag */ |
135 |
+ |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
< |
markdirty(hp) /* mark holodeck section directory dirty */ |
87 |
< |
register HOLO *hp; |
88 |
< |
{ |
89 |
< |
static BEAMI smudge = {0, -1}; |
139 |
> |
/* Do we need a routine to locate file fragments given known occupants? */ |
140 |
|
|
141 |
< |
if (hp->dirty) /* already marked? */ |
141 |
> |
|
142 |
> |
hdrelease(fd) /* stop tracking file fragments for some section */ |
143 |
> |
register int fd; |
144 |
> |
{ |
145 |
> |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
146 |
|
return; |
147 |
< |
hp->dirty = 1; |
148 |
< |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
149 |
< |
|| write(hp->fd, (char *)&smudge, |
150 |
< |
sizeof(BEAMI)) != sizeof(BEAMI)) |
151 |
< |
error(SYSTEM, "seek/write error in markdirty"); |
147 |
> |
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
148 |
> |
free((char *)hdfragl[fd].fi); |
149 |
> |
hdfragl[fd].fi = NULL; |
150 |
> |
hdfragl[fd].nfrags = 0; |
151 |
> |
} |
152 |
|
} |
153 |
|
|
154 |
|
|
157 |
|
int fd; /* corresponding file descriptor */ |
158 |
|
HDGRID *hproto; /* holodeck section grid */ |
159 |
|
{ |
160 |
+ |
long rtrunc; |
161 |
|
long fpos; |
162 |
+ |
int writable; |
163 |
|
register HOLO *hp; |
164 |
|
register int n; |
165 |
|
/* prepare for system errors */ |
179 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
180 |
|
error(SYSTEM, "failure loading holodeck directory"); |
181 |
|
/* check that it's clean */ |
182 |
< |
if (hp->bi[nbeams(hp)].fo < 0) |
183 |
< |
error(USER, "dirty holodeck section"); |
184 |
< |
} else { /* assume we're creating it */ |
182 |
> |
for (n = nbeams(hp); n > 0; n--) |
183 |
> |
if (hp->bi[n].fo < 0) { |
184 |
> |
hp->bi[n].fo = 0; |
185 |
> |
error(WARNING, "dirty holodeck section"); |
186 |
> |
break; |
187 |
> |
} |
188 |
> |
/* check writability */ |
189 |
> |
if (fd < nhdfragls && hdfragl[fd].nlinks) |
190 |
> |
writable = hdfragl[fd].writable; |
191 |
> |
else |
192 |
> |
writable = lseek(fd, fpos, 0) == fpos && |
193 |
> |
write(fd, (char *)hp, sizeof(HDGRID)) == |
194 |
> |
sizeof(HDGRID); |
195 |
> |
} else { /* else assume we're creating it */ |
196 |
|
if ((hp = hdalloc(hproto)) == NULL) |
197 |
|
goto memerr; |
198 |
|
/* write header and skeleton */ |
201 |
|
sizeof(HDGRID) || |
202 |
|
write(fd, (char *)(hp->bi+1), n) != n) |
203 |
|
error(SYSTEM, "cannot write header to holodeck file"); |
204 |
+ |
writable = 1; |
205 |
|
} |
206 |
|
hp->fd = fd; |
207 |
|
hp->dirty = 0; |
208 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
209 |
< |
biglob(hp)->nrd = 0; /* count rays on disk */ |
210 |
< |
for (n = nbeams(hp); n > 0; n--) |
211 |
< |
biglob(hp)->nrd += hp->bi[n].nrd; |
209 |
> |
/* start tracking fragments */ |
210 |
> |
hdattach(fd, writable); |
211 |
> |
/* check rays on disk */ |
212 |
> |
fpos = hdfilen(fd); |
213 |
> |
biglob(hp)->nrd = rtrunc = 0; |
214 |
> |
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
215 |
> |
if (hp->bi[n].nrd) |
216 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
217 |
> |
rtrunc += hp->bi[n].nrd; |
218 |
> |
hp->bi[n].nrd = 0; |
219 |
> |
} else |
220 |
> |
biglob(hp)->nrd += hp->bi[n].nrd; |
221 |
> |
if (rtrunc) { |
222 |
> |
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
223 |
> |
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
224 |
> |
error(WARNING, errmsg); |
225 |
> |
} |
226 |
|
/* add to holodeck list */ |
227 |
|
for (n = 0; n < HDMAX; n++) |
228 |
|
if (hdlist[n] == NULL) { |
229 |
|
hdlist[n] = hp; |
230 |
|
break; |
231 |
|
} |
150 |
– |
/* start tracking fragments (last) */ |
151 |
– |
hdattach(fd); |
232 |
|
/* all done */ |
233 |
|
return(hp); |
234 |
|
memerr: |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
+ |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
240 |
+ |
register HOLO *hp; |
241 |
+ |
int i; |
242 |
+ |
{ |
243 |
+ |
static BEAMI smudge = {0, -1}; |
244 |
+ |
int mindist, minpos; |
245 |
+ |
register int j; |
246 |
+ |
|
247 |
+ |
if (!hp->dirty++) { /* write smudge first time */ |
248 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
249 |
+ |
|| write(hp->fd, (char *)&smudge, |
250 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
251 |
+ |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
252 |
+ |
hp->dirseg[0].s = i; |
253 |
+ |
hp->dirseg[0].n = 1; |
254 |
+ |
return; |
255 |
+ |
} |
256 |
+ |
/* insert into segment list */ |
257 |
+ |
for (j = hp->dirty; j--; ) { |
258 |
+ |
if (!j || hp->dirseg[j-1].s < i) { |
259 |
+ |
hp->dirseg[j].s = i; |
260 |
+ |
hp->dirseg[j].n = 1; |
261 |
+ |
break; |
262 |
+ |
} |
263 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
264 |
+ |
} |
265 |
+ |
do { /* check neighbors */ |
266 |
+ |
mindist = nbeams(hp); /* find closest */ |
267 |
+ |
for (j = hp->dirty; --j; ) |
268 |
+ |
if (hp->dirseg[j].s - |
269 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n) |
270 |
+ |
< mindist) { |
271 |
+ |
minpos = j; |
272 |
+ |
mindist = hp->dirseg[j].s - |
273 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n); |
274 |
+ |
} |
275 |
+ |
/* good enough? */ |
276 |
+ |
if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) |
277 |
+ |
break; |
278 |
+ |
j = minpos - 1; /* coalesce neighbors */ |
279 |
+ |
if (hp->dirseg[j].s + hp->dirseg[j].n < |
280 |
+ |
hp->dirseg[minpos].s + hp->dirseg[minpos].n) |
281 |
+ |
hp->dirseg[j].n = hp->dirseg[minpos].s + |
282 |
+ |
hp->dirseg[minpos].n - hp->dirseg[j].s; |
283 |
+ |
hp->dirty--; |
284 |
+ |
while (++j < hp->dirty) /* close the gap */ |
285 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
286 |
+ |
} while (mindist <= MINDIRSEL); |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
|
290 |
|
int |
291 |
< |
hdsync(hp) /* update directory on disk if necessary */ |
291 |
> |
hdsync(hp, all) /* update beams and directory on disk */ |
292 |
|
register HOLO *hp; |
293 |
+ |
int all; |
294 |
|
{ |
295 |
|
register int j, n; |
296 |
|
|
297 |
< |
if (hp == NULL) { /* do all */ |
297 |
> |
if (hp == NULL) { /* do all holodecks */ |
298 |
|
n = 0; |
299 |
|
for (j = 0; hdlist[j] != NULL; j++) |
300 |
< |
n += hdsync(hdlist[j]); |
300 |
> |
n += hdsync(hdlist[j], all); |
301 |
|
return(n); |
302 |
|
} |
303 |
< |
if (!hp->dirty) /* check first */ |
303 |
> |
/* sync the beams */ |
304 |
> |
for (j = (all ? nbeams(hp) : 0); j > 0; j--) |
305 |
> |
if (hp->bl[j] != NULL) |
306 |
> |
hdsyncbeam(hp, j); |
307 |
> |
if (!hp->dirty) /* directory clean? */ |
308 |
|
return(0); |
309 |
< |
errno = 0; |
310 |
< |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
311 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
312 |
< |
n = nbeams(hp)*sizeof(BEAMI); |
313 |
< |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
314 |
< |
error(SYSTEM, "cannot update holodeck section directory"); |
315 |
< |
hp->dirty = 0; |
309 |
> |
errno = 0; /* write dirty segments */ |
310 |
> |
for (j = 0; j < hp->dirty; j++) { |
311 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
312 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
313 |
> |
error(SYSTEM, "cannot seek on holodeck file"); |
314 |
> |
n = hp->dirseg[j].n * sizeof(BEAMI); |
315 |
> |
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
316 |
> |
error(SYSTEM, "cannot update section directory"); |
317 |
> |
} |
318 |
> |
hp->dirty = 0; /* all clean */ |
319 |
|
return(1); |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
< |
long |
323 |
> |
unsigned |
324 |
|
hdmemuse(all) /* return memory usage (in bytes) */ |
325 |
|
int all; /* include overhead (painful) */ |
326 |
|
{ |
339 |
|
} |
340 |
|
} |
341 |
|
if (all) |
342 |
< |
for (j = 0; j < nhdfrags; j++) { |
343 |
< |
total += sizeof(struct fragment); |
344 |
< |
if (hdfrag[j].nfrags) |
342 |
> |
for (j = 0; j < nhdfragls; j++) { |
343 |
> |
total += sizeof(struct fraglist); |
344 |
> |
if (hdfragl[j].nfrags) |
345 |
|
total += FRAGBLK*sizeof(BEAMI) * |
346 |
< |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
346 |
> |
((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; |
347 |
|
} |
348 |
|
return(total); |
349 |
|
} |
357 |
|
|
358 |
|
if (fd < 0) |
359 |
|
return(-1); |
360 |
< |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) { |
360 |
> |
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
361 |
|
if ((fpos = lseek(fd, 0L, 1)) < 0) |
362 |
|
return(-1); |
363 |
|
flen = lseek(fd, 0L, 2); |
364 |
|
lseek(fd, fpos, 0); |
365 |
|
return(flen); |
366 |
|
} |
367 |
< |
return(hdfrag[fd].flen); |
367 |
> |
return(hdfragl[fd].flen); |
368 |
|
} |
369 |
|
|
370 |
|
|
403 |
|
RAYVAL *p; |
404 |
|
int n; |
405 |
|
|
406 |
< |
if (nr <= 0) |
407 |
< |
return(NULL); |
408 |
< |
if (i < 1 | i > nbeams(hp)) |
270 |
< |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
406 |
> |
if (nr <= 0) return(NULL); |
407 |
> |
CHECK(i < 1 | i > nbeams(hp), |
408 |
> |
CONSISTENCY, "bad beam index given to hdnewrays"); |
409 |
|
if (hp->bl[i] != NULL) |
410 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
411 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
412 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
275 |
– |
errno = 0; |
413 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
414 |
|
n = hp->bi[i].nrd + nr; |
415 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
279 |
< |
goto memerr; |
415 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
416 |
|
blglob(hp)->nrm += n; |
417 |
< |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
417 |
> |
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
418 |
> |
errno = 0; |
419 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
420 |
|
error(SYSTEM, "seek error on holodeck file"); |
421 |
|
n *= sizeof(RAYVAL); |
424 |
|
"error reading beam from holodeck file"); |
425 |
|
} |
426 |
|
} else { /* just grow in memory */ |
427 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
428 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
292 |
< |
if (hp->bl[i] == NULL) |
293 |
< |
goto memerr; |
427 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
428 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
429 |
|
blglob(hp)->nrm += nr; |
430 |
|
} |
431 |
+ |
if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) |
432 |
+ |
hdfreefrag(hp, i); /* relinquish old fragment */ |
433 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
434 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
435 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
436 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
300 |
< |
blglob(hp)->tick = hdclock; |
436 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
437 |
|
return(p); /* point to new rays */ |
302 |
– |
memerr: |
303 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
438 |
|
} |
439 |
|
|
440 |
|
|
445 |
|
{ |
446 |
|
register int n; |
447 |
|
|
448 |
< |
if (i < 1 | i > nbeams(hp)) |
449 |
< |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
448 |
> |
CHECK(i < 1 | i > nbeams(hp), |
449 |
> |
CONSISTENCY, "bad beam index given to hdgetbeam"); |
450 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
451 |
|
if (!(n = hp->bi[i].nrd)) |
452 |
|
return(NULL); |
453 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
454 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
455 |
< |
errno = 0; |
322 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
323 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
455 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
456 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
457 |
+ |
errno = 0; |
458 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
459 |
|
error(SYSTEM, "seek error on holodeck file"); |
460 |
|
n *= sizeof(RAYVAL); |
461 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
462 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
463 |
+ |
if (hdfragflags&FF_READ) |
464 |
+ |
hdfreefrag(hp, i); /* relinquish old frag. */ |
465 |
|
} |
466 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
332 |
< |
blglob(hp)->tick = hdclock; |
466 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
467 |
|
return(hp->bl[i]); |
468 |
|
} |
469 |
|
|
470 |
|
|
471 |
|
int |
472 |
< |
hdsyncbi(hp, i) /* sync beam in memory with beam on disk */ |
473 |
< |
register HOLO *hp; |
340 |
< |
register int i; |
472 |
> |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
473 |
> |
register HDBEAMI *hb1, *hb2; |
474 |
|
{ |
475 |
< |
unsigned int nrays = hp->bl[i]->nrm; |
476 |
< |
long nfo; |
477 |
< |
unsigned int n; |
478 |
< |
/* is current fragment OK? */ |
479 |
< |
if (nrays == hp->bi[i].nrd) |
347 |
< |
return(0); |
348 |
< |
/* check file status */ |
349 |
< |
if (hp->dirty < 0) |
475 |
> |
register long c; |
476 |
> |
/* residents go first */ |
477 |
> |
if (hb2->h->bl[hb2->b] != NULL) |
478 |
> |
return(hb1->h->bl[hb1->b] == NULL); |
479 |
> |
if (hb1->h->bl[hb1->b] != NULL) |
480 |
|
return(-1); |
481 |
+ |
/* otherwise sort by file descriptor */ |
482 |
+ |
if ((c = hb1->h->fd - hb2->h->fd)) |
483 |
+ |
return(c); |
484 |
+ |
/* then by position in file */ |
485 |
+ |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
486 |
+ |
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
487 |
+ |
} |
488 |
|
|
352 |
– |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
353 |
– |
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
489 |
|
|
490 |
< |
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
491 |
< |
hdfrag[hp->fd].flen) /* EOF special case */ |
492 |
< |
hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
490 |
> |
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
491 |
> |
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
492 |
> |
int n; /* list length */ |
493 |
> |
int (*bf)(); /* callback function (optional) */ |
494 |
> |
{ |
495 |
> |
unsigned origcachesize, memuse; |
496 |
> |
int bytesloaded, needbytes, bytes2free; |
497 |
> |
register BEAM *bp; |
498 |
> |
register int i; |
499 |
> |
/* precheck consistency */ |
500 |
> |
if (n <= 0) return; |
501 |
> |
for (i = n; i--; ) |
502 |
> |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
503 |
> |
error(CONSISTENCY, "bad beam in hdloadbeams"); |
504 |
> |
/* sort list for optimal access */ |
505 |
> |
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
506 |
> |
bytesloaded = 0; /* run through loaded beams */ |
507 |
> |
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
508 |
> |
bp->tick = hdclock; /* preempt swap */ |
509 |
> |
bytesloaded += bp->nrm; |
510 |
> |
if (bf != NULL) |
511 |
> |
(*bf)(bp, hb); |
512 |
> |
} |
513 |
> |
bytesloaded *= sizeof(RAYVAL); |
514 |
> |
if ((origcachesize = hdcachesize) > 0) { |
515 |
> |
needbytes = 0; /* figure out memory needs */ |
516 |
> |
for (i = n; i--; ) |
517 |
> |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
518 |
> |
needbytes *= sizeof(RAYVAL); |
519 |
> |
do { /* free enough memory */ |
520 |
> |
memuse = hdmemuse(0); |
521 |
> |
bytes2free = needbytes - (int)(hdcachesize-memuse); |
522 |
> |
if (bytes2free > (int)(memuse - bytesloaded)) |
523 |
> |
bytes2free = memuse - bytesloaded; |
524 |
> |
} while (bytes2free > 0 && |
525 |
> |
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
526 |
> |
hdcachesize = 0; /* load beams w/o swap */ |
527 |
> |
} |
528 |
> |
for (i = 0; i < n; i++) |
529 |
> |
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
530 |
> |
(*bf)(bp, hb+i); |
531 |
> |
hdcachesize = origcachesize; /* resume dynamic swapping */ |
532 |
> |
} |
533 |
|
|
534 |
< |
else { /* general case */ |
535 |
< |
register struct fragment *f = &hdfrag[hp->fd]; |
536 |
< |
register int j, k; |
537 |
< |
/* relinquish old fragment */ |
538 |
< |
if (hp->bi[i].nrd) { |
539 |
< |
j = f->nfrags++; |
540 |
< |
#ifdef MAXFRAG |
541 |
< |
if (j >= MAXFRAG-1) |
542 |
< |
f->nfrags--; |
534 |
> |
|
535 |
> |
int |
536 |
> |
hdfreefrag(hp, i) /* free a file fragment */ |
537 |
> |
HOLO *hp; |
538 |
> |
int i; |
539 |
> |
{ |
540 |
> |
register BEAMI *bi = &hp->bi[i]; |
541 |
> |
register struct fraglist *f; |
542 |
> |
register int j, k; |
543 |
> |
|
544 |
> |
if (bi->nrd <= 0) |
545 |
> |
return(0); |
546 |
> |
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
547 |
> |
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
548 |
> |
f = &hdfragl[hp->fd]; |
549 |
> |
if (!f->writable) |
550 |
> |
return(0); |
551 |
> |
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
552 |
> |
for (j = k = 0; k < f->nfrags; j++, k++) { |
553 |
> |
while (f->fi[k].nrd == 0) |
554 |
> |
if (++k >= f->nfrags) |
555 |
> |
goto endloop; |
556 |
> |
if (k > j) |
557 |
> |
copystruct(f->fi+j, f->fi+k); |
558 |
> |
} |
559 |
> |
endloop: |
560 |
> |
f->nfrags = j; |
561 |
> |
} |
562 |
> |
j = f->nfrags++; /* allocate a slot in free list */ |
563 |
> |
#if MAXFRAGB |
564 |
> |
if (j >= MAXFRAGB*FRAGBLK) { |
565 |
> |
f->nfrags = j--; /* stop list growth */ |
566 |
> |
if (bi->nrd <= f->fi[j].nrd) |
567 |
> |
return(0); /* new one no better than discard */ |
568 |
> |
} |
569 |
|
#endif |
570 |
< |
if (j % FRAGBLK == 0) { /* more frag. space */ |
571 |
< |
if (f->fi == NULL) |
572 |
< |
f->fi = (BEAMI *)malloc( |
573 |
< |
FRAGBLK*sizeof(BEAMI)); |
574 |
< |
else |
575 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
576 |
< |
(j+FRAGBLK)*sizeof(BEAMI)); |
577 |
< |
if (f->fi == NULL) |
578 |
< |
error(SYSTEM, |
579 |
< |
"out of memory in hdsyncbi"); |
580 |
< |
} |
581 |
< |
for ( ; ; j--) { /* insert in descending list */ |
582 |
< |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
583 |
< |
f->fi[j].fo = hp->bi[i].fo; |
584 |
< |
f->fi[j].nrd = hp->bi[i].nrd; |
585 |
< |
break; |
586 |
< |
} |
587 |
< |
copystruct(f->fi+j, f->fi+j-1); |
588 |
< |
} |
570 |
> |
if (j % FRAGBLK == 0) { /* more (or less) free list space */ |
571 |
> |
register BEAMI *newp; |
572 |
> |
if (f->fi == NULL) |
573 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
574 |
> |
else |
575 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
576 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
577 |
> |
if (newp == NULL) { |
578 |
> |
f->nfrags--; /* graceful failure */ |
579 |
> |
return(0); |
580 |
> |
} |
581 |
> |
f->fi = newp; |
582 |
> |
} |
583 |
> |
for ( ; ; j--) { /* insert in descending list */ |
584 |
> |
if (!j || bi->fo < f->fi[j-1].fo) { |
585 |
> |
f->fi[j].fo = bi->fo; |
586 |
> |
f->fi[j].nrd = bi->nrd; |
587 |
> |
break; |
588 |
> |
} |
589 |
> |
copystruct(f->fi+j, f->fi+(j-1)); |
590 |
> |
} |
591 |
|
/* coalesce adjacent fragments */ |
592 |
< |
for (j = k = 0; k < f->nfrags; j++, k++) { |
593 |
< |
if (k > j) |
594 |
< |
copystruct(f->fi+j, f->fi+k); |
595 |
< |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
596 |
< |
f->fi[k+1].nrd*sizeof(RAYVAL) |
597 |
< |
== f->fi[j].fo) { |
598 |
< |
f->fi[j].fo -= |
599 |
< |
f->fi[++k].nrd*sizeof(RAYVAL); |
600 |
< |
f->fi[j].nrd += f->fi[k].nrd; |
601 |
< |
} |
592 |
> |
/* successors never empty */ |
593 |
> |
if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { |
594 |
> |
f->fi[j].nrd += f->fi[j-1].nrd; |
595 |
> |
f->fi[j-1].nrd = 0; |
596 |
> |
} |
597 |
> |
for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */ |
598 |
> |
if (f->fi[k].nrd) { |
599 |
> |
if (f->fi[j].fo == f->fi[k].fo + |
600 |
> |
f->fi[k].nrd*sizeof(RAYVAL)) { |
601 |
> |
f->fi[k].nrd += f->fi[j].nrd; |
602 |
> |
f->fi[j].nrd = 0; |
603 |
|
} |
604 |
< |
f->nfrags = j; |
604 |
> |
break; |
605 |
|
} |
606 |
< |
k = -1; /* find closest-sized fragment */ |
607 |
< |
for (j = nrays ? f->nfrags : 0; j-- > 0; ) |
608 |
< |
if (f->fi[j].nrd >= nrays && |
609 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
610 |
< |
if (f->fi[k=j].nrd == nrays) |
611 |
< |
break; |
612 |
< |
if (k < 0) { /* no fragment -- extend file */ |
613 |
< |
nfo = f->flen; |
614 |
< |
f->flen += nrays*sizeof(RAYVAL); |
615 |
< |
} else { /* else use fragment */ |
616 |
< |
nfo = f->fi[k].fo; |
617 |
< |
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
618 |
< |
f->nfrags--; |
619 |
< |
for (j = k; j < f->nfrags; j++) |
620 |
< |
copystruct(f->fi+j, f->fi+j+1); |
621 |
< |
} else { /* else shrink it */ |
622 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
623 |
< |
f->fi[k].nrd -= nrays; |
624 |
< |
} |
625 |
< |
} |
606 |
> |
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
607 |
> |
bi->nrd = 0; |
608 |
> |
bi->fo = 0L; |
609 |
> |
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
610 |
> |
return(1); |
611 |
> |
} |
612 |
> |
|
613 |
> |
|
614 |
> |
int |
615 |
> |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
616 |
> |
int fd; |
617 |
> |
int *listlen; |
618 |
> |
register int4 *listsiz; |
619 |
> |
{ |
620 |
> |
register struct fraglist *f; |
621 |
> |
register int i; |
622 |
> |
|
623 |
> |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
624 |
> |
return(0); /* listless */ |
625 |
> |
if (listlen != NULL) |
626 |
> |
*listlen = f->nfrags; |
627 |
> |
if (listsiz != NULL) |
628 |
> |
for (i = f->nfrags, *listsiz = 0; i--; ) |
629 |
> |
*listsiz += f->fi[i].nrd; |
630 |
> |
#if MAXFRAGB |
631 |
> |
return(f->nfrags < MAXFRAGB*FRAGBLK); |
632 |
> |
#else |
633 |
> |
return(1); /* list never fills up */ |
634 |
> |
#endif |
635 |
> |
} |
636 |
> |
|
637 |
> |
|
638 |
> |
long |
639 |
> |
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
640 |
> |
int fd; |
641 |
> |
unsigned int4 nrays; |
642 |
> |
{ |
643 |
> |
register struct fraglist *f; |
644 |
> |
register int j; |
645 |
> |
long nfo; |
646 |
> |
|
647 |
> |
if (nrays == 0) |
648 |
> |
return(-1L); |
649 |
> |
DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, |
650 |
> |
CONSISTENCY, "bad file descriptor in hdallocfrag"); |
651 |
> |
f = &hdfragl[fd]; |
652 |
> |
for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ |
653 |
> |
if (f->fi[j].nrd >= nrays) |
654 |
> |
break; |
655 |
> |
if (j < 0) { /* no fragment -- extend file */ |
656 |
> |
nfo = f->flen; |
657 |
> |
f->flen += nrays*sizeof(RAYVAL); |
658 |
> |
} else { /* else use fragment */ |
659 |
> |
nfo = f->fi[j].fo; |
660 |
> |
f->fi[j].fo += nrays*sizeof(RAYVAL); |
661 |
> |
f->fi[j].nrd -= nrays; |
662 |
|
} |
663 |
< |
if (nrays) { /* write the new fragment */ |
663 |
> |
return(nfo); |
664 |
> |
} |
665 |
> |
|
666 |
> |
|
667 |
> |
int |
668 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
669 |
> |
register HOLO *hp; |
670 |
> |
register int i; |
671 |
> |
{ |
672 |
> |
int fragfreed; |
673 |
> |
unsigned int4 nrays; |
674 |
> |
unsigned int n; |
675 |
> |
long nfo; |
676 |
> |
/* check file status */ |
677 |
> |
if (hdfragl[hp->fd].writable <= 0) |
678 |
> |
return(hdfragl[hp->fd].writable); |
679 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
680 |
> |
CONSISTENCY, "bad beam index in hdsyncbeam"); |
681 |
> |
/* is current fragment OK? */ |
682 |
> |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
683 |
> |
return(0); |
684 |
> |
/* relinquish old fragment? */ |
685 |
> |
fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); |
686 |
> |
if (nrays) { /* get and write new fragment */ |
687 |
> |
nfo = hdallocfrag(hp->fd, nrays); |
688 |
|
errno = 0; |
689 |
|
if (lseek(hp->fd, nfo, 0) < 0) |
690 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
691 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
692 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
693 |
< |
hp->dirty = -1; /* avoid recursive error */ |
694 |
< |
error(SYSTEM, "write error in hdsyncbi"); |
693 |
> |
hdfragl[hp->fd].writable = -1; |
694 |
> |
hdsync(NULL, 0); /* sync directories */ |
695 |
> |
error(SYSTEM, "write error in hdsyncbeam"); |
696 |
|
} |
697 |
< |
} |
697 |
> |
hp->bi[i].fo = nfo; |
698 |
> |
} else |
699 |
> |
hp->bi[i].fo = 0L; |
700 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
701 |
|
hp->bi[i].nrd = nrays; |
702 |
< |
hp->bi[i].fo = nfo; |
703 |
< |
markdirty(hp); /* section directory now out of date */ |
702 |
> |
if (!fragfreed) |
703 |
> |
hdmarkdirty(hp, i); /* need to flag dir. ent. */ |
704 |
|
return(1); |
705 |
|
} |
706 |
|
|
718 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
719 |
|
return(nchanged); |
720 |
|
} |
721 |
+ |
if (hdfragl[hp->fd].writable < 0) /* check for file error */ |
722 |
+ |
return(0); |
723 |
|
if (i == 0) { /* clear entire holodeck */ |
724 |
+ |
if (blglob(hp)->nrm == 0) |
725 |
+ |
return(0); /* already clear */ |
726 |
|
nchanged = 0; |
727 |
|
for (i = nbeams(hp); i > 0; i--) |
728 |
|
if (hp->bl[i] != NULL) |
729 |
|
nchanged += hdfreebeam(hp, i); |
730 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
731 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
732 |
|
return(nchanged); |
733 |
|
} |
734 |
< |
if (i < 1 | i > nbeams(hp)) |
735 |
< |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
734 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
735 |
> |
CONSISTENCY, "bad beam index to hdfreebeam"); |
736 |
|
if (hp->bl[i] == NULL) |
737 |
|
return(0); |
738 |
|
/* check for additions */ |
739 |
|
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
740 |
|
if (nchanged) |
741 |
< |
hdsyncbi(hp, i); /* write new fragment */ |
741 |
> |
hdsyncbeam(hp, i); /* write new fragment */ |
742 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
743 |
|
free((char *)hp->bl[i]); /* free memory */ |
744 |
|
hp->bl[i] = NULL; |
751 |
|
register HOLO *hp; |
752 |
|
register int i; |
753 |
|
{ |
481 |
– |
static BEAM emptybeam; |
754 |
|
int nchanged; |
755 |
|
|
756 |
|
if (hp == NULL) { /* clobber all holodecks */ |
760 |
|
return(nchanged); |
761 |
|
} |
762 |
|
if (i == 0) { /* clobber entire holodeck */ |
763 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
764 |
+ |
return(0); /* already empty */ |
765 |
|
nchanged = 0; |
766 |
+ |
nchanged = 0; |
767 |
|
for (i = nbeams(hp); i > 0; i--) |
768 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
769 |
|
nchanged += hdkillbeam(hp, i); |
770 |
< |
#ifdef DEBUG |
771 |
< |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
497 |
< |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
498 |
< |
#endif |
770 |
> |
DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, |
771 |
> |
CONSISTENCY, "bad beam count in hdkillbeam"); |
772 |
|
return(nchanged); |
773 |
|
} |
774 |
< |
if (i < 1 | i > nbeams(hp)) |
775 |
< |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
774 |
> |
DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, |
775 |
> |
"bad beam index to hdkillbeam"); |
776 |
> |
DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, |
777 |
> |
"hdkillbeam called on read-only holodeck"); |
778 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
779 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
780 |
|
free((char *)hp->bl[i]); |
781 |
+ |
hp->bl[i] = NULL; |
782 |
|
} else |
783 |
|
nchanged = hp->bi[i].nrd; |
784 |
< |
if (hp->bi[i].nrd) { /* free file fragment */ |
785 |
< |
hp->bl[i] = &emptybeam; |
786 |
< |
hdsyncbi(hp, i); |
784 |
> |
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
785 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
786 |
> |
hp->bi[i].nrd = 0; |
787 |
> |
hp->bi[i].fo = 0L; |
788 |
> |
hdmarkdirty(hp, i); |
789 |
|
} |
512 |
– |
hp->bl[i] = NULL; |
790 |
|
return(nchanged); |
791 |
|
} |
792 |
|
|
793 |
|
|
794 |
< |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
795 |
< |
register HOLO *ha[]; /* section list (NULL terminated) */ |
796 |
< |
register int ba[]; /* beam index to go with section */ |
797 |
< |
int n; /* length of arrays minus 1 */ |
794 |
> |
int |
795 |
> |
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
796 |
> |
register HDBEAMI *hb; /* beam list */ |
797 |
> |
int nents; /* current list length */ |
798 |
> |
int n; /* maximum list length */ |
799 |
|
register HOLO *hp; /* section we're adding from */ |
800 |
|
{ |
801 |
|
register int i, j; |
524 |
– |
int nents; |
525 |
– |
/* find last entry in LRU list */ |
526 |
– |
for (j = 0; ha[j] != NULL; j++) |
527 |
– |
; |
528 |
– |
nents = j; |
802 |
|
/* insert each beam from hp */ |
803 |
< |
for (i = nbeams(hp); i > 0; i--) { |
803 |
> |
for (i = 1; i <= nbeams(hp); i++) { |
804 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
805 |
|
continue; |
806 |
< |
if ((j = ++nents) > n) /* grow list if we can */ |
806 |
> |
#if 0 |
807 |
> |
if (hp->bl[i]->tick == hdclock) /* preempt swap? */ |
808 |
> |
continue; |
809 |
> |
#endif |
810 |
> |
if ((j = ++nents) >= n) /* grow list if we can */ |
811 |
|
nents--; |
812 |
|
for ( ; ; ) { /* bubble into place */ |
813 |
|
if (!--j || hp->bl[i]->tick >= |
814 |
< |
ha[j-1]->bl[ba[j-1]]->tick) { |
815 |
< |
ha[j] = hp; |
816 |
< |
ba[j] = i; |
814 |
> |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
815 |
> |
hb[j].h = hp; |
816 |
> |
hb[j].b = i; |
817 |
|
break; |
818 |
|
} |
819 |
< |
ha[j] = ha[j-1]; |
543 |
< |
ba[j] = ba[j-1]; |
819 |
> |
copystruct(hb+j, hb+(j-1)); |
820 |
|
} |
821 |
|
} |
822 |
< |
ha[nents] = NULL; /* all done */ |
547 |
< |
ba[nents] = 0; |
822 |
> |
return(nents); /* return new list length */ |
823 |
|
} |
824 |
|
|
825 |
|
|
826 |
+ |
int |
827 |
|
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
828 |
|
int pct; /* maximum percentage to free */ |
829 |
|
register HOLO *honly; /* NULL means check all */ |
830 |
|
{ |
831 |
< |
HOLO *hp[FREEBEAMS+1]; |
556 |
< |
int bn[FREEBEAMS+1]; |
831 |
> |
HDBEAMI hb[FREEBEAMS]; |
832 |
|
int freetarget; |
833 |
+ |
int n; |
834 |
|
register int i; |
835 |
+ |
#ifdef DEBUG |
836 |
+ |
unsigned membefore; |
837 |
+ |
|
838 |
+ |
membefore = hdmemuse(0); |
839 |
+ |
#endif |
840 |
|
/* compute free target */ |
841 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
842 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
843 |
|
freetarget = freetarget*pct/100; |
844 |
+ |
if (freetarget <= 0) |
845 |
+ |
return(0); |
846 |
|
/* find least recently used */ |
847 |
< |
hp[0] = NULL; |
565 |
< |
bn[0] = 0; |
847 |
> |
n = 0; |
848 |
|
if (honly != NULL) |
849 |
< |
hdlrulist(hp, bn, FREEBEAMS, honly); |
849 |
> |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
850 |
|
else |
851 |
|
for (i = 0; hdlist[i] != NULL; i++) |
852 |
< |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
852 |
> |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
853 |
|
/* free LRU beams */ |
854 |
< |
for (i = 0; hp[i] != NULL; i++) { |
855 |
< |
hdfreebeam(hp[i], bn[i]); |
856 |
< |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
854 |
> |
for (i = 0; i < n; i++) { |
855 |
> |
hdfreebeam(hb[i].h, hb[i].b); |
856 |
> |
if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) |
857 |
|
break; |
858 |
|
} |
859 |
< |
hdsync(honly); /* synchronize directories as necessary */ |
859 |
> |
hdsync(honly, 0); /* synchronize directories as necessary */ |
860 |
> |
#ifdef DEBUG |
861 |
> |
sprintf(errmsg, |
862 |
> |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
863 |
> |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
864 |
> |
wputs(errmsg); |
865 |
> |
#endif |
866 |
> |
return(-freetarget); /* return how far past goal we went */ |
867 |
|
} |
868 |
|
|
869 |
|
|
875 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
876 |
|
while (hdlist[0] != NULL) |
877 |
|
hddone(hdlist[0]); |
878 |
+ |
free((char *)hdfragl); |
879 |
+ |
hdfragl = NULL; nhdfragls = 0; |
880 |
|
return; |
881 |
|
} |
882 |
|
/* flush all data and free memory */ |