1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
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 32 /* default cache size (Mbytes, 0==inf) */ |
18 |
> |
#else |
19 |
> |
#define CACHESIZE 12 |
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_ALLOC|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 writerr; /* 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 |
+ |
char * |
65 |
+ |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
66 |
+ |
char *ptr; |
67 |
+ |
unsigned siz; |
68 |
+ |
char *rout; |
69 |
+ |
{ |
70 |
+ |
register char *newp; |
71 |
+ |
/* call malloc/realloc */ |
72 |
+ |
if (ptr == NULL) newp = (char *)malloc(siz); |
73 |
+ |
else newp = (char *)realloc(ptr, siz); |
74 |
+ |
/* check success */ |
75 |
+ |
if (newp == NULL && rout != NULL) { |
76 |
+ |
hdfreecache(25, NULL); /* free some memory */ |
77 |
+ |
errno = 0; /* retry */ |
78 |
+ |
newp = hdrealloc(ptr, siz, NULL); |
79 |
+ |
if (newp == NULL) { /* give up and report error */ |
80 |
+ |
sprintf(errmsg, "out of memory in %s", rout); |
81 |
+ |
error(SYSTEM, errmsg); |
82 |
+ |
} |
83 |
+ |
} |
84 |
+ |
return(newp); |
85 |
+ |
} |
86 |
+ |
|
87 |
+ |
|
88 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
89 |
|
register int fd; |
90 |
|
{ |
91 |
< |
if (fd >= nhdfrags) { |
92 |
< |
if (nhdfrags) |
93 |
< |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
94 |
< |
(fd+1)*sizeof(struct fragment)); |
95 |
< |
else |
96 |
< |
hdfrag = (struct fragment *)malloc( |
58 |
< |
(fd+1)*sizeof(struct fragment)); |
59 |
< |
if (hdfrag == NULL) |
60 |
< |
error(SYSTEM, "out of memory in hdattach"); |
61 |
< |
bzero((char *)(hdfrag+nhdfrags), |
62 |
< |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
63 |
< |
nhdfrags = fd+1; |
91 |
> |
if (fd >= nhdfragls) { |
92 |
> |
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
93 |
> |
(fd+1)*sizeof(struct fraglist), "hdattach"); |
94 |
> |
bzero((char *)(hdfragl+nhdfragls), |
95 |
> |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
96 |
> |
nhdfragls = fd+1; |
97 |
|
} |
98 |
< |
hdfrag[fd].nlinks++; |
99 |
< |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
98 |
> |
hdfragl[fd].nlinks++; |
99 |
> |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
100 |
|
} |
101 |
|
|
102 |
|
|
106 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
107 |
|
register int fd; |
108 |
|
{ |
109 |
< |
if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) |
109 |
> |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
110 |
|
return; |
111 |
< |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
112 |
< |
free((char *)hdfrag[fd].fi); |
113 |
< |
hdfrag[fd].fi = NULL; |
114 |
< |
hdfrag[fd].nfrags = 0; |
111 |
> |
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
112 |
> |
free((char *)hdfragl[fd].fi); |
113 |
> |
hdfragl[fd].fi = NULL; |
114 |
> |
hdfragl[fd].nfrags = 0; |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
|
|
86 |
– |
markdirty(hp) /* mark holodeck section directory dirty */ |
87 |
– |
register HOLO *hp; |
88 |
– |
{ |
89 |
– |
static BEAMI smudge = {0, -1}; |
90 |
– |
|
91 |
– |
if (hp->dirty) /* already marked? */ |
92 |
– |
return; |
93 |
– |
hp->dirty = 1; |
94 |
– |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
95 |
– |
|| write(hp->fd, (char *)&smudge, |
96 |
– |
sizeof(BEAMI)) != sizeof(BEAMI)) |
97 |
– |
error(SYSTEM, "seek/write error in markdirty"); |
98 |
– |
} |
99 |
– |
|
100 |
– |
|
119 |
|
HOLO * |
120 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
121 |
|
int fd; /* corresponding file descriptor */ |
122 |
|
HDGRID *hproto; /* holodeck section grid */ |
123 |
|
{ |
124 |
+ |
long rtrunc; |
125 |
|
long fpos; |
126 |
|
register HOLO *hp; |
127 |
|
register int n; |
142 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
143 |
|
error(SYSTEM, "failure loading holodeck directory"); |
144 |
|
/* check that it's clean */ |
145 |
< |
if (hp->bi[nbeams(hp)].fo < 0) |
146 |
< |
error(USER, "dirty holodeck section"); |
145 |
> |
for (n = nbeams(hp); n > 0; n--) |
146 |
> |
if (hp->bi[n].fo < 0) { |
147 |
> |
hp->bi[n].fo = 0; |
148 |
> |
error(WARNING, "dirty holodeck section"); |
149 |
> |
break; |
150 |
> |
} |
151 |
|
} else { /* assume we're creating it */ |
152 |
|
if ((hp = hdalloc(hproto)) == NULL) |
153 |
|
goto memerr; |
161 |
|
hp->fd = fd; |
162 |
|
hp->dirty = 0; |
163 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
164 |
< |
biglob(hp)->nrd = 0; /* count rays on disk */ |
165 |
< |
for (n = nbeams(hp); n > 0; n--) |
166 |
< |
biglob(hp)->nrd += hp->bi[n].nrd; |
164 |
> |
/* start tracking fragments */ |
165 |
> |
hdattach(fd); |
166 |
> |
/* check rays on disk */ |
167 |
> |
fpos = hdfilen(fd); |
168 |
> |
biglob(hp)->nrd = rtrunc = 0; |
169 |
> |
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
170 |
> |
if (hp->bi[n].nrd) |
171 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
172 |
> |
rtrunc += hp->bi[n].nrd; |
173 |
> |
hp->bi[n].nrd = 0; |
174 |
> |
} else |
175 |
> |
biglob(hp)->nrd += hp->bi[n].nrd; |
176 |
> |
if (rtrunc) { |
177 |
> |
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
178 |
> |
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
179 |
> |
error(WARNING, errmsg); |
180 |
> |
} |
181 |
|
/* add to holodeck list */ |
182 |
|
for (n = 0; n < HDMAX; n++) |
183 |
|
if (hdlist[n] == NULL) { |
184 |
|
hdlist[n] = hp; |
185 |
|
break; |
186 |
|
} |
150 |
– |
/* start tracking fragments (last) */ |
151 |
– |
hdattach(fd); |
187 |
|
/* all done */ |
188 |
|
return(hp); |
189 |
|
memerr: |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
+ |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
195 |
+ |
register HOLO *hp; |
196 |
+ |
int i; |
197 |
+ |
{ |
198 |
+ |
static BEAMI smudge = {0, -1}; |
199 |
+ |
int mindist, minpos; |
200 |
+ |
register int j; |
201 |
+ |
|
202 |
+ |
if (!hp->dirty++) { /* write smudge first time */ |
203 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
204 |
+ |
|| write(hp->fd, (char *)&smudge, |
205 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
206 |
+ |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
207 |
+ |
hp->dirseg[0].s = i; |
208 |
+ |
hp->dirseg[0].n = 1; |
209 |
+ |
return; |
210 |
+ |
} |
211 |
+ |
/* insert into segment list */ |
212 |
+ |
for (j = hp->dirty; j--; ) { |
213 |
+ |
if (!j || hp->dirseg[j-1].s < i) { |
214 |
+ |
hp->dirseg[j].s = i; |
215 |
+ |
hp->dirseg[j].n = 1; |
216 |
+ |
break; |
217 |
+ |
} |
218 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
219 |
+ |
} |
220 |
+ |
do { /* check neighbors */ |
221 |
+ |
mindist = nbeams(hp); /* find closest */ |
222 |
+ |
for (j = hp->dirty; --j; ) |
223 |
+ |
if (hp->dirseg[j].s - |
224 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n) |
225 |
+ |
< mindist) { |
226 |
+ |
minpos = j; |
227 |
+ |
mindist = hp->dirseg[j].s - |
228 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n); |
229 |
+ |
} |
230 |
+ |
/* good enough? */ |
231 |
+ |
if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) |
232 |
+ |
break; |
233 |
+ |
j = minpos - 1; /* coalesce neighbors */ |
234 |
+ |
if (hp->dirseg[j].s + hp->dirseg[j].n < |
235 |
+ |
hp->dirseg[minpos].s + hp->dirseg[minpos].n) |
236 |
+ |
hp->dirseg[j].n = hp->dirseg[minpos].s + |
237 |
+ |
hp->dirseg[minpos].n - hp->dirseg[j].s; |
238 |
+ |
hp->dirty--; |
239 |
+ |
while (++j < hp->dirty) /* close the gap */ |
240 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
241 |
+ |
} while (mindist <= MINDIRSEL); |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
|
245 |
|
int |
246 |
|
hdsync(hp, all) /* update beams and directory on disk */ |
247 |
|
register HOLO *hp; |
256 |
|
return(n); |
257 |
|
} |
258 |
|
/* sync the beams */ |
259 |
< |
for (j = all ? nbeams(hp) : 0; j > 0; j--) |
259 |
> |
for (j = (all ? nbeams(hp) : 0); j > 0; j--) |
260 |
|
if (hp->bl[j] != NULL) |
261 |
|
hdsyncbeam(hp, j); |
262 |
< |
if (!hp->dirty) /* directory dirty? */ |
262 |
> |
if (!hp->dirty) /* directory clean? */ |
263 |
|
return(0); |
264 |
< |
errno = 0; |
265 |
< |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
266 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
267 |
< |
n = nbeams(hp)*sizeof(BEAMI); |
268 |
< |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
269 |
< |
error(SYSTEM, "cannot update holodeck section directory"); |
270 |
< |
hp->dirty = 0; |
264 |
> |
errno = 0; /* write dirty segments */ |
265 |
> |
for (j = 0; j < hp->dirty; j++) { |
266 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
267 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
268 |
> |
error(SYSTEM, "cannot seek on holodeck file"); |
269 |
> |
n = hp->dirseg[j].n * sizeof(BEAMI); |
270 |
> |
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
271 |
> |
error(SYSTEM, "cannot update section directory"); |
272 |
> |
} |
273 |
> |
hp->dirty = 0; /* all clean */ |
274 |
|
return(1); |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
< |
long |
278 |
> |
unsigned |
279 |
|
hdmemuse(all) /* return memory usage (in bytes) */ |
280 |
|
int all; /* include overhead (painful) */ |
281 |
|
{ |
294 |
|
} |
295 |
|
} |
296 |
|
if (all) |
297 |
< |
for (j = 0; j < nhdfrags; j++) { |
298 |
< |
total += sizeof(struct fragment); |
299 |
< |
if (hdfrag[j].nfrags) |
297 |
> |
for (j = 0; j < nhdfragls; j++) { |
298 |
> |
total += sizeof(struct fraglist); |
299 |
> |
if (hdfragl[j].nfrags) |
300 |
|
total += FRAGBLK*sizeof(BEAMI) * |
301 |
< |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
301 |
> |
((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; |
302 |
|
} |
303 |
|
return(total); |
304 |
|
} |
312 |
|
|
313 |
|
if (fd < 0) |
314 |
|
return(-1); |
315 |
< |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) { |
315 |
> |
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
316 |
|
if ((fpos = lseek(fd, 0L, 1)) < 0) |
317 |
|
return(-1); |
318 |
|
flen = lseek(fd, 0L, 2); |
319 |
|
lseek(fd, fpos, 0); |
320 |
|
return(flen); |
321 |
|
} |
322 |
< |
return(hdfrag[fd].flen); |
322 |
> |
return(hdfragl[fd].flen); |
323 |
|
} |
324 |
|
|
325 |
|
|
358 |
|
RAYVAL *p; |
359 |
|
int n; |
360 |
|
|
361 |
< |
if (nr <= 0) |
362 |
< |
return(NULL); |
363 |
< |
if (i < 1 | i > nbeams(hp)) |
275 |
< |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
361 |
> |
if (nr <= 0) return(NULL); |
362 |
> |
CHECK(i < 1 | i > nbeams(hp), |
363 |
> |
CONSISTENCY, "bad beam index given to hdnewrays"); |
364 |
|
if (hp->bl[i] != NULL) |
365 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
366 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
367 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
280 |
– |
errno = 0; |
368 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
369 |
|
n = hp->bi[i].nrd + nr; |
370 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
284 |
< |
goto memerr; |
370 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
371 |
|
blglob(hp)->nrm += n; |
372 |
< |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
372 |
> |
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
373 |
> |
errno = 0; |
374 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
375 |
|
error(SYSTEM, "seek error on holodeck file"); |
376 |
|
n *= sizeof(RAYVAL); |
379 |
|
"error reading beam from holodeck file"); |
380 |
|
} |
381 |
|
} else { /* just grow in memory */ |
382 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
383 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
297 |
< |
if (hp->bl[i] == NULL) |
298 |
< |
goto memerr; |
382 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
383 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
384 |
|
blglob(hp)->nrm += nr; |
385 |
|
} |
386 |
+ |
if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) |
387 |
+ |
hdfreefrag(hp, i); /* relinquish old fragment */ |
388 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
389 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
390 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
391 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
305 |
< |
blglob(hp)->tick = hdclock; |
391 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
392 |
|
return(p); /* point to new rays */ |
307 |
– |
memerr: |
308 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
393 |
|
} |
394 |
|
|
395 |
|
|
400 |
|
{ |
401 |
|
register int n; |
402 |
|
|
403 |
< |
if (i < 1 | i > nbeams(hp)) |
404 |
< |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
403 |
> |
CHECK(i < 1 | i > nbeams(hp), |
404 |
> |
CONSISTENCY, "bad beam index given to hdgetbeam"); |
405 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
406 |
|
if (!(n = hp->bi[i].nrd)) |
407 |
|
return(NULL); |
408 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
409 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
410 |
< |
errno = 0; |
327 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
328 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
410 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
411 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
412 |
+ |
errno = 0; |
413 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
414 |
|
error(SYSTEM, "seek error on holodeck file"); |
415 |
|
n *= sizeof(RAYVAL); |
416 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
417 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
418 |
+ |
if (hdfragflags&FF_READ) |
419 |
+ |
hdfreefrag(hp, i); /* relinquish old frag. */ |
420 |
|
} |
421 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
337 |
< |
blglob(hp)->tick = hdclock; |
421 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
422 |
|
return(hp->bl[i]); |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
|
int |
427 |
< |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
428 |
< |
register HOLO *hp; |
345 |
< |
register int i; |
427 |
> |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
428 |
> |
register HDBEAMI *hb1, *hb2; |
429 |
|
{ |
430 |
< |
unsigned int nrays; |
431 |
< |
long nfo; |
432 |
< |
unsigned int n; |
433 |
< |
#ifdef DEBUG |
434 |
< |
if (i < 1 | i > nbeams(hp)) |
352 |
< |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
353 |
< |
#endif |
354 |
< |
/* is current fragment OK? */ |
355 |
< |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
356 |
< |
return(0); |
357 |
< |
/* check file status */ |
358 |
< |
if (hp->dirty < 0) |
430 |
> |
register long c; |
431 |
> |
/* residents go first */ |
432 |
> |
if (hb2->h->bl[hb2->b] != NULL) |
433 |
> |
return(hb1->h->bl[hb1->b] == NULL); |
434 |
> |
if (hb1->h->bl[hb1->b] != NULL) |
435 |
|
return(-1); |
436 |
+ |
/* otherwise sort by file descriptor */ |
437 |
+ |
if ((c = hb1->h->fd - hb2->h->fd)) |
438 |
+ |
return(c); |
439 |
+ |
/* then by position in file */ |
440 |
+ |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
441 |
+ |
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
442 |
+ |
} |
443 |
|
|
361 |
– |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
362 |
– |
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
444 |
|
|
445 |
< |
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
446 |
< |
hdfrag[hp->fd].flen) /* EOF special case */ |
447 |
< |
hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
445 |
> |
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
446 |
> |
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
447 |
> |
int n; /* list length */ |
448 |
> |
int (*bf)(); /* callback function (optional) */ |
449 |
> |
{ |
450 |
> |
unsigned origcachesize, memuse; |
451 |
> |
int bytesloaded, needbytes, bytes2free; |
452 |
> |
register BEAM *bp; |
453 |
> |
register int i; |
454 |
> |
/* precheck consistency */ |
455 |
> |
if (n <= 0) return; |
456 |
> |
for (i = n; i--; ) |
457 |
> |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
458 |
> |
error(CONSISTENCY, "bad beam in hdloadbeams"); |
459 |
> |
/* sort list for optimal access */ |
460 |
> |
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
461 |
> |
bytesloaded = 0; /* run through loaded beams */ |
462 |
> |
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
463 |
> |
bp->tick = hdclock; /* preempt swap */ |
464 |
> |
bytesloaded += bp->nrm; |
465 |
> |
if (bf != NULL) |
466 |
> |
(*bf)(bp, hb); |
467 |
> |
} |
468 |
> |
bytesloaded *= sizeof(RAYVAL); |
469 |
> |
if ((origcachesize = hdcachesize) > 0) { |
470 |
> |
needbytes = 0; /* figure out memory needs */ |
471 |
> |
for (i = n; i--; ) |
472 |
> |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
473 |
> |
needbytes *= sizeof(RAYVAL); |
474 |
> |
do { /* free enough memory */ |
475 |
> |
memuse = hdmemuse(0); |
476 |
> |
bytes2free = needbytes - (int)(hdcachesize-memuse); |
477 |
> |
if (bytes2free > (int)(memuse - bytesloaded)) |
478 |
> |
bytes2free = memuse - bytesloaded; |
479 |
> |
} while (bytes2free > 0 && |
480 |
> |
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
481 |
> |
hdcachesize = 0; /* load beams w/o swap */ |
482 |
> |
} |
483 |
> |
for (i = 0; i < n; i++) |
484 |
> |
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
485 |
> |
(*bf)(bp, hb+i); |
486 |
> |
hdcachesize = origcachesize; /* resume dynamic swapping */ |
487 |
> |
} |
488 |
|
|
489 |
< |
else { /* general case */ |
490 |
< |
register struct fragment *f = &hdfrag[hp->fd]; |
491 |
< |
register int j, k; |
492 |
< |
/* relinquish old fragment */ |
493 |
< |
if (hp->bi[i].nrd) { |
494 |
< |
j = f->nfrags++; |
495 |
< |
#ifdef MAXFRAG |
496 |
< |
if (j >= MAXFRAG-1) |
497 |
< |
f->nfrags--; |
489 |
> |
|
490 |
> |
int |
491 |
> |
hdfreefrag(hp, i) /* free a file fragment */ |
492 |
> |
HOLO *hp; |
493 |
> |
int i; |
494 |
> |
{ |
495 |
> |
register BEAMI *bi = &hp->bi[i]; |
496 |
> |
register struct fraglist *f; |
497 |
> |
register int j, k; |
498 |
> |
|
499 |
> |
if (bi->nrd <= 0) |
500 |
> |
return(0); |
501 |
> |
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
502 |
> |
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
503 |
> |
f = &hdfragl[hp->fd]; |
504 |
> |
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
505 |
> |
for (j = k = 0; k < f->nfrags; j++, k++) { |
506 |
> |
while (f->fi[k].nrd == 0) |
507 |
> |
if (++k >= f->nfrags) |
508 |
> |
goto endloop; |
509 |
> |
if (k > j) |
510 |
> |
copystruct(f->fi+j, f->fi+k); |
511 |
> |
} |
512 |
> |
endloop: |
513 |
> |
f->nfrags = j; |
514 |
> |
} |
515 |
> |
j = f->nfrags++; /* allocate a slot in free list */ |
516 |
> |
#if MAXFRAGB |
517 |
> |
if (j >= MAXFRAGB*FRAGBLK) { |
518 |
> |
f->nfrags = j--; /* stop list growth */ |
519 |
> |
if (bi->nrd <= f->fi[j].nrd) |
520 |
> |
return(0); /* new one no better than discard */ |
521 |
> |
} |
522 |
|
#endif |
523 |
< |
if (j % FRAGBLK == 0) { /* more frag. space */ |
524 |
< |
if (f->fi == NULL) |
525 |
< |
f->fi = (BEAMI *)malloc( |
526 |
< |
FRAGBLK*sizeof(BEAMI)); |
527 |
< |
else |
528 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
529 |
< |
(j+FRAGBLK)*sizeof(BEAMI)); |
530 |
< |
if (f->fi == NULL) |
531 |
< |
error(SYSTEM, |
532 |
< |
"out of memory in hdsyncbeam"); |
533 |
< |
} |
534 |
< |
for ( ; ; j--) { /* insert in descending list */ |
535 |
< |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
536 |
< |
f->fi[j].fo = hp->bi[i].fo; |
537 |
< |
f->fi[j].nrd = hp->bi[i].nrd; |
538 |
< |
break; |
539 |
< |
} |
540 |
< |
copystruct(f->fi+j, f->fi+j-1); |
541 |
< |
} |
523 |
> |
if (j % FRAGBLK == 0) { /* more (or less) free list space */ |
524 |
> |
register BEAMI *newp; |
525 |
> |
if (f->fi == NULL) |
526 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
527 |
> |
else |
528 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
529 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
530 |
> |
if (newp == NULL) { |
531 |
> |
f->nfrags--; /* graceful failure */ |
532 |
> |
return(0); |
533 |
> |
} |
534 |
> |
f->fi = newp; |
535 |
> |
} |
536 |
> |
for ( ; ; j--) { /* insert in descending list */ |
537 |
> |
if (!j || bi->fo < f->fi[j-1].fo) { |
538 |
> |
f->fi[j].fo = bi->fo; |
539 |
> |
f->fi[j].nrd = bi->nrd; |
540 |
> |
break; |
541 |
> |
} |
542 |
> |
copystruct(f->fi+j, f->fi+(j-1)); |
543 |
> |
} |
544 |
|
/* coalesce adjacent fragments */ |
545 |
< |
for (j = k = 0; k < f->nfrags; j++, k++) { |
546 |
< |
if (k > j) |
547 |
< |
copystruct(f->fi+j, f->fi+k); |
548 |
< |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
549 |
< |
f->fi[k+1].nrd*sizeof(RAYVAL) |
550 |
< |
== f->fi[j].fo) { |
551 |
< |
f->fi[j].fo -= |
552 |
< |
f->fi[++k].nrd*sizeof(RAYVAL); |
553 |
< |
f->fi[j].nrd += f->fi[k].nrd; |
554 |
< |
} |
545 |
> |
/* successors never empty */ |
546 |
> |
if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { |
547 |
> |
f->fi[j].nrd += f->fi[j-1].nrd; |
548 |
> |
f->fi[j-1].nrd = 0; |
549 |
> |
} |
550 |
> |
for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */ |
551 |
> |
if (f->fi[k].nrd) { |
552 |
> |
if (f->fi[j].fo == f->fi[k].fo + |
553 |
> |
f->fi[k].nrd*sizeof(RAYVAL)) { |
554 |
> |
f->fi[k].nrd += f->fi[j].nrd; |
555 |
> |
f->fi[j].nrd = 0; |
556 |
|
} |
557 |
< |
f->nfrags = j; |
557 |
> |
break; |
558 |
|
} |
559 |
< |
k = -1; /* find closest-sized fragment */ |
560 |
< |
for (j = nrays ? f->nfrags : 0; j-- > 0; ) |
561 |
< |
if (f->fi[j].nrd >= nrays && |
562 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
563 |
< |
if (f->fi[k=j].nrd == nrays) |
564 |
< |
break; |
565 |
< |
if (k < 0) { /* no fragment -- extend file */ |
566 |
< |
nfo = f->flen; |
567 |
< |
f->flen += nrays*sizeof(RAYVAL); |
568 |
< |
} else { /* else use fragment */ |
569 |
< |
nfo = f->fi[k].fo; |
570 |
< |
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
571 |
< |
f->nfrags--; |
572 |
< |
for (j = k; j < f->nfrags; j++) |
573 |
< |
copystruct(f->fi+j, f->fi+j+1); |
574 |
< |
} else { /* else shrink it */ |
575 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
576 |
< |
f->fi[k].nrd -= nrays; |
577 |
< |
} |
578 |
< |
} |
559 |
> |
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
560 |
> |
bi->nrd = 0; |
561 |
> |
bi->fo = 0L; |
562 |
> |
return(1); |
563 |
> |
} |
564 |
> |
|
565 |
> |
|
566 |
> |
int |
567 |
> |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
568 |
> |
int fd; |
569 |
> |
int *listlen; |
570 |
> |
register int4 *listsiz; |
571 |
> |
{ |
572 |
> |
register struct fraglist *f; |
573 |
> |
register int i; |
574 |
> |
|
575 |
> |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
576 |
> |
return(0); /* listless */ |
577 |
> |
if (listlen != NULL) |
578 |
> |
*listlen = f->nfrags; |
579 |
> |
if (listsiz != NULL) |
580 |
> |
for (i = f->nfrags, *listsiz = 0; i--; ) |
581 |
> |
*listsiz += f->fi[i].nrd; |
582 |
> |
#if MAXFRAGB |
583 |
> |
return(f->nfrags < MAXFRAGB*FRAGBLK); |
584 |
> |
#else |
585 |
> |
return(1); /* list never fills up */ |
586 |
> |
#endif |
587 |
> |
} |
588 |
> |
|
589 |
> |
|
590 |
> |
long |
591 |
> |
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
592 |
> |
int fd; |
593 |
> |
unsigned int4 nrays; |
594 |
> |
{ |
595 |
> |
register struct fraglist *f; |
596 |
> |
register int j; |
597 |
> |
long nfo; |
598 |
> |
|
599 |
> |
if (nrays == 0) |
600 |
> |
return(-1L); |
601 |
> |
DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, |
602 |
> |
CONSISTENCY, "bad file descriptor in hdallocfrag"); |
603 |
> |
f = &hdfragl[fd]; |
604 |
> |
for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ |
605 |
> |
if (f->fi[j].nrd >= nrays) |
606 |
> |
break; |
607 |
> |
if (j < 0) { /* no fragment -- extend file */ |
608 |
> |
nfo = f->flen; |
609 |
> |
f->flen += nrays*sizeof(RAYVAL); |
610 |
> |
} else { /* else use fragment */ |
611 |
> |
nfo = f->fi[j].fo; |
612 |
> |
f->fi[j].fo += nrays*sizeof(RAYVAL); |
613 |
> |
f->fi[j].nrd -= nrays; |
614 |
|
} |
615 |
< |
if (nrays) { /* write the new fragment */ |
615 |
> |
return(nfo); |
616 |
> |
} |
617 |
> |
|
618 |
> |
|
619 |
> |
int |
620 |
> |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
621 |
> |
register HOLO *hp; |
622 |
> |
register int i; |
623 |
> |
{ |
624 |
> |
unsigned int4 nrays; |
625 |
> |
unsigned int n; |
626 |
> |
long nfo; |
627 |
> |
/* check file status */ |
628 |
> |
if (hdfragl[hp->fd].writerr) |
629 |
> |
return(-1); |
630 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
631 |
> |
CONSISTENCY, "bad beam index in hdsyncbeam"); |
632 |
> |
/* is current fragment OK? */ |
633 |
> |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
634 |
> |
return(0); |
635 |
> |
if (hdfragflags&FF_WRITE && hp->bi[i].nrd) |
636 |
> |
hdfreefrag(hp, i); /* relinquish old fragment */ |
637 |
> |
if (nrays) { /* get and write new fragment */ |
638 |
> |
nfo = hdallocfrag(hp->fd, nrays); |
639 |
|
errno = 0; |
640 |
|
if (lseek(hp->fd, nfo, 0) < 0) |
641 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
642 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
643 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
644 |
< |
hp->dirty = -1; /* avoid recursive error */ |
644 |
> |
hdfragl[hp->fd].writerr++; |
645 |
> |
hdsync(NULL, 0); /* sync directories */ |
646 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
647 |
|
} |
648 |
< |
} |
648 |
> |
hp->bi[i].fo = nfo; |
649 |
> |
} else |
650 |
> |
hp->bi[i].fo = 0L; |
651 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
652 |
|
hp->bi[i].nrd = nrays; |
653 |
< |
hp->bi[i].fo = nfo; |
445 |
< |
markdirty(hp); /* section directory now out of date */ |
653 |
> |
hdmarkdirty(hp, i); /* section directory now out of date */ |
654 |
|
return(1); |
655 |
|
} |
656 |
|
|
668 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
669 |
|
return(nchanged); |
670 |
|
} |
671 |
+ |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
672 |
+ |
return(0); |
673 |
|
if (i == 0) { /* clear entire holodeck */ |
674 |
+ |
if (blglob(hp)->nrm == 0) |
675 |
+ |
return(0); /* already clear */ |
676 |
|
nchanged = 0; |
677 |
|
for (i = nbeams(hp); i > 0; i--) |
678 |
|
if (hp->bl[i] != NULL) |
679 |
|
nchanged += hdfreebeam(hp, i); |
680 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
681 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
682 |
|
return(nchanged); |
683 |
|
} |
684 |
< |
#ifdef DEBUG |
685 |
< |
if (i < 1 | i > nbeams(hp)) |
472 |
< |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
473 |
< |
#endif |
684 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
685 |
> |
CONSISTENCY, "bad beam index to hdfreebeam"); |
686 |
|
if (hp->bl[i] == NULL) |
687 |
|
return(0); |
688 |
|
/* check for additions */ |
701 |
|
register HOLO *hp; |
702 |
|
register int i; |
703 |
|
{ |
492 |
– |
static BEAM emptybeam; |
704 |
|
int nchanged; |
705 |
|
|
706 |
|
if (hp == NULL) { /* clobber all holodecks */ |
710 |
|
return(nchanged); |
711 |
|
} |
712 |
|
if (i == 0) { /* clobber entire holodeck */ |
713 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
714 |
+ |
return(0); /* already empty */ |
715 |
|
nchanged = 0; |
716 |
+ |
nchanged = 0; |
717 |
|
for (i = nbeams(hp); i > 0; i--) |
718 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
719 |
|
nchanged += hdkillbeam(hp, i); |
720 |
< |
#ifdef DEBUG |
721 |
< |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
508 |
< |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
509 |
< |
#endif |
720 |
> |
DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, |
721 |
> |
CONSISTENCY, "bad beam count in hdkillbeam"); |
722 |
|
return(nchanged); |
723 |
|
} |
724 |
< |
#ifdef DEBUG |
725 |
< |
if (i < 1 | i > nbeams(hp)) |
514 |
< |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
515 |
< |
#endif |
724 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
725 |
> |
CONSISTENCY, "bad beam index to hdkillbeam"); |
726 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
727 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
728 |
|
free((char *)hp->bl[i]); |
729 |
|
} else |
730 |
|
nchanged = hp->bi[i].nrd; |
731 |
< |
if (hp->bi[i].nrd) { /* free file fragment */ |
732 |
< |
hp->bl[i] = &emptybeam; |
733 |
< |
hdsyncbeam(hp, i); |
731 |
> |
if (hp->bi[i].nrd) { |
732 |
> |
if (hdfragflags&FF_KILL) |
733 |
> |
hdfreefrag(hp, i); |
734 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; |
735 |
> |
hp->bi[i].nrd = 0; /* make sure it's gone */ |
736 |
> |
hp->bi[i].fo = 0L; |
737 |
|
} |
738 |
|
hp->bl[i] = NULL; |
739 |
|
return(nchanged); |
740 |
|
} |
741 |
|
|
742 |
|
|
743 |
< |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
744 |
< |
register HOLO *ha[]; /* section list (NULL terminated) */ |
745 |
< |
register int ba[]; /* beam index to go with section */ |
746 |
< |
int n; /* length of arrays minus 1 */ |
743 |
> |
int |
744 |
> |
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
745 |
> |
register HDBEAMI *hb; /* beam list */ |
746 |
> |
int nents; /* current list length */ |
747 |
> |
int n; /* maximum list length */ |
748 |
|
register HOLO *hp; /* section we're adding from */ |
749 |
|
{ |
750 |
|
register int i, j; |
537 |
– |
int nents; |
538 |
– |
/* find last entry in LRU list */ |
539 |
– |
for (j = 0; ha[j] != NULL; j++) |
540 |
– |
; |
541 |
– |
nents = j; |
751 |
|
/* insert each beam from hp */ |
752 |
< |
for (i = nbeams(hp); i > 0; i--) { |
752 |
> |
for (i = 1; i <= nbeams(hp); i++) { |
753 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
754 |
|
continue; |
755 |
< |
if ((j = ++nents) > n) /* grow list if we can */ |
755 |
> |
#if 0 |
756 |
> |
if (hp->bl[i]->tick == hdclock) /* preempt swap? */ |
757 |
> |
continue; |
758 |
> |
#endif |
759 |
> |
if ((j = ++nents) >= n) /* grow list if we can */ |
760 |
|
nents--; |
761 |
|
for ( ; ; ) { /* bubble into place */ |
762 |
|
if (!--j || hp->bl[i]->tick >= |
763 |
< |
ha[j-1]->bl[ba[j-1]]->tick) { |
764 |
< |
ha[j] = hp; |
765 |
< |
ba[j] = i; |
763 |
> |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
764 |
> |
hb[j].h = hp; |
765 |
> |
hb[j].b = i; |
766 |
|
break; |
767 |
|
} |
768 |
< |
ha[j] = ha[j-1]; |
556 |
< |
ba[j] = ba[j-1]; |
768 |
> |
copystruct(hb+j, hb+(j-1)); |
769 |
|
} |
770 |
|
} |
771 |
< |
ha[nents] = NULL; /* all done */ |
560 |
< |
ba[nents] = 0; |
771 |
> |
return(nents); /* return new list length */ |
772 |
|
} |
773 |
|
|
774 |
|
|
775 |
+ |
int |
776 |
|
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
777 |
|
int pct; /* maximum percentage to free */ |
778 |
|
register HOLO *honly; /* NULL means check all */ |
779 |
|
{ |
780 |
< |
HOLO *hp[FREEBEAMS+1]; |
569 |
< |
int bn[FREEBEAMS+1]; |
780 |
> |
HDBEAMI hb[FREEBEAMS]; |
781 |
|
int freetarget; |
782 |
+ |
int n; |
783 |
|
register int i; |
784 |
+ |
#ifdef DEBUG |
785 |
+ |
unsigned membefore; |
786 |
+ |
|
787 |
+ |
membefore = hdmemuse(0); |
788 |
+ |
#endif |
789 |
|
/* compute free target */ |
790 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
791 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
792 |
|
freetarget = freetarget*pct/100; |
793 |
+ |
if (freetarget <= 0) |
794 |
+ |
return(0); |
795 |
|
/* find least recently used */ |
796 |
< |
hp[0] = NULL; |
578 |
< |
bn[0] = 0; |
796 |
> |
n = 0; |
797 |
|
if (honly != NULL) |
798 |
< |
hdlrulist(hp, bn, FREEBEAMS, honly); |
798 |
> |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
799 |
|
else |
800 |
|
for (i = 0; hdlist[i] != NULL; i++) |
801 |
< |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
801 |
> |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
802 |
|
/* free LRU beams */ |
803 |
< |
for (i = 0; hp[i] != NULL; i++) { |
804 |
< |
hdfreebeam(hp[i], bn[i]); |
805 |
< |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
803 |
> |
for (i = 0; i < n; i++) { |
804 |
> |
hdfreebeam(hb[i].h, hb[i].b); |
805 |
> |
if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) |
806 |
|
break; |
807 |
|
} |
808 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
809 |
+ |
#ifdef DEBUG |
810 |
+ |
sprintf(errmsg, |
811 |
+ |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
812 |
+ |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
813 |
+ |
wputs(errmsg); |
814 |
+ |
#endif |
815 |
+ |
return(-freetarget); /* return how far past goal we went */ |
816 |
|
} |
817 |
|
|
818 |
|
|
824 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
825 |
|
while (hdlist[0] != NULL) |
826 |
|
hddone(hdlist[0]); |
827 |
+ |
free((char *)hdfragl); |
828 |
+ |
hdfragl = NULL; nhdfragls = 0; |
829 |
|
return; |
830 |
|
} |
831 |
|
/* flush all data and free memory */ |