16 |
|
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
|
#endif |
18 |
|
#ifndef FREEBEAMS |
19 |
< |
#define FREEBEAMS 1024 /* maximum beams to free at a time */ |
19 |
> |
#define FREEBEAMS 512 /* maximum beams to free at a time */ |
20 |
|
#endif |
21 |
|
#ifndef PCTFREE |
22 |
< |
#define PCTFREE 12 /* maximum fraction to free (%) */ |
22 |
> |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
23 |
|
#endif |
24 |
– |
#ifndef FFDMAX |
25 |
– |
#define FFDMAX 64 /* max. file descriptor for tracking */ |
26 |
– |
#endif |
27 |
– |
#ifndef MAXFRAG |
28 |
– |
#define MAXFRAG 2048 /* maximum fragments tracked in each file */ |
29 |
– |
#endif |
24 |
|
|
25 |
+ |
#define MAXFRAG (128*FRAGBLK) /* maximum fragments per file */ |
26 |
+ |
|
27 |
+ |
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
28 |
+ |
|
29 |
|
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
30 |
|
unsigned long hdclock; /* clock value */ |
31 |
|
|
34 |
|
static struct fragment { |
35 |
|
short nlinks; /* number of holodeck sections using us */ |
36 |
|
short nfrags; /* number of known fragments */ |
37 |
< |
BEAMI fi[MAXFRAG]; /* fragments, descending file position */ |
37 |
> |
BEAMI *fi; /* fragments, descending file position */ |
38 |
|
long flen; /* last known file length */ |
39 |
< |
} *hdfrag[FFDMAX]; /* fragment lists, indexed by file descriptor */ |
39 |
> |
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
40 |
|
|
41 |
+ |
static int nhdfrags; /* size of hdfrag array */ |
42 |
|
|
43 |
+ |
|
44 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
45 |
|
register int fd; |
46 |
|
{ |
47 |
< |
if (fd >= FFDMAX) |
48 |
< |
return; /* descriptor out of range */ |
49 |
< |
if (hdfrag[fd] == NULL) { |
50 |
< |
if ((hdfrag[fd] = (struct fragment *) |
51 |
< |
malloc(sizeof(struct fragment))) == NULL) |
52 |
< |
return; /* should flag error? */ |
53 |
< |
hdfrag[fd]->nlinks = 0; |
54 |
< |
hdfrag[fd]->nfrags = 0; |
47 |
> |
if (fd >= nhdfrags) { |
48 |
> |
if (nhdfrags) |
49 |
> |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
50 |
> |
(fd+1)*sizeof(struct fragment)); |
51 |
> |
else |
52 |
> |
hdfrag = (struct fragment *)malloc( |
53 |
> |
(fd+1)*sizeof(struct fragment)); |
54 |
> |
if (hdfrag == NULL) |
55 |
> |
error(SYSTEM, "out of memory in hdattach"); |
56 |
> |
bzero((char *)(hdfrag+nhdfrags), |
57 |
> |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
58 |
> |
nhdfrags = fd+1; |
59 |
|
} |
60 |
< |
hdfrag[fd]->nlinks++; |
61 |
< |
hdfrag[fd]->flen = lseek(fd, 0L, 2); /* get file length */ |
60 |
> |
hdfrag[fd].nlinks++; |
61 |
> |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
62 |
|
} |
63 |
|
|
64 |
|
|
68 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
69 |
|
register int fd; |
70 |
|
{ |
71 |
< |
if (fd >= FFDMAX || hdfrag[fd] == NULL) |
71 |
> |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) |
72 |
|
return; |
73 |
< |
if (!--hdfrag[fd]->nlinks) { |
74 |
< |
free((char *)hdfrag[fd]); |
75 |
< |
hdfrag[fd] = NULL; |
73 |
> |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
74 |
> |
free((char *)hdfrag[fd].fi); |
75 |
> |
hdfrag[fd].fi = NULL; |
76 |
> |
hdfrag[fd].nfrags = 0; |
77 |
|
} |
78 |
|
} |
79 |
|
|
147 |
|
} |
148 |
|
if (!hp->dirty) /* check first */ |
149 |
|
return(0); |
150 |
+ |
errno = 0; |
151 |
|
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
152 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
153 |
|
n = nbeams(hp)*sizeof(BEAMI); |
177 |
|
} |
178 |
|
} |
179 |
|
if (all) |
180 |
< |
for (j = 0; j < FFDMAX; j++) |
181 |
< |
if (hdfrag[j] != NULL) |
182 |
< |
total += sizeof(struct fragment); |
180 |
> |
for (j = 0; j < nhdfrags; j++) { |
181 |
> |
total += sizeof(struct fragment); |
182 |
> |
if (hdfrag[j].nfrags) |
183 |
> |
total += FRAGBLK*sizeof(BEAMI) * |
184 |
> |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
185 |
> |
} |
186 |
|
return(total); |
187 |
|
} |
188 |
|
|
230 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
231 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
232 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
233 |
+ |
errno = 0; |
234 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
235 |
|
n = hp->bi[i].nrd + nr; |
236 |
|
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
302 |
|
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
303 |
|
return(0); |
304 |
|
|
305 |
< |
if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ |
305 |
> |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
306 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
307 |
|
|
308 |
|
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
309 |
< |
hdfrag[hp->fd]->flen) /* EOF special case */ |
310 |
< |
hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
309 |
> |
hdfrag[hp->fd].flen) /* EOF special case */ |
310 |
> |
hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
311 |
|
|
312 |
|
else { /* general case */ |
313 |
< |
register struct fragment *f = hdfrag[hp->fd]; |
313 |
> |
register struct fragment *f = &hdfrag[hp->fd]; |
314 |
|
register int j, k; |
315 |
|
/* relinquish old fragment */ |
316 |
|
if (hp->bi[i].nrd) { |
317 |
< |
if ((j = ++f->nfrags) >= MAXFRAG) |
317 |
> |
j = f->nfrags++; |
318 |
> |
#ifdef MAXFRAG |
319 |
> |
if (j >= MAXFRAG-1) |
320 |
|
f->nfrags--; |
321 |
< |
for ( ; ; ) { /* stick it in our descending list */ |
322 |
< |
if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { |
321 |
> |
#endif |
322 |
> |
if (j % FRAGBLK == 0) { /* more frag. space */ |
323 |
> |
if (f->fi == NULL) |
324 |
> |
f->fi = (BEAMI *)malloc( |
325 |
> |
FRAGBLK*sizeof(BEAMI)); |
326 |
> |
else |
327 |
> |
f->fi = (BEAMI *)realloc((char *)f->fi, |
328 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
329 |
> |
if (f->fi == NULL) |
330 |
> |
error(SYSTEM, |
331 |
> |
"out of memory in hdgetbi"); |
332 |
> |
} |
333 |
> |
for ( ; ; j--) { /* insert in descending list */ |
334 |
> |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
335 |
|
f->fi[j].fo = hp->bi[i].fo; |
336 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
337 |
|
break; |
340 |
|
} |
341 |
|
/* coalesce adjacent fragments */ |
342 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
343 |
< |
if (j != k) |
343 |
> |
if (k > j) |
344 |
|
copystruct(f->fi+j, f->fi+k); |
345 |
< |
while (k+1 < f->nfrags && |
346 |
< |
f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL) |
347 |
< |
== f->fi[j].fo) |
345 |
> |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
346 |
> |
f->fi[k+1].nrd*sizeof(RAYVAL) |
347 |
> |
== f->fi[j].fo) { |
348 |
|
f->fi[j].fo -= |
349 |
|
f->fi[++k].nrd*sizeof(RAYVAL); |
350 |
+ |
f->fi[j].nrd += f->fi[k].nrd; |
351 |
+ |
} |
352 |
|
} |
353 |
|
f->nfrags = j; |
354 |
|
} |
355 |
|
k = -1; /* find closest-sized fragment */ |
356 |
|
for (j = f->nfrags; j-- > 0; ) |
357 |
|
if (f->fi[j].nrd >= nrays && |
358 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd) |
359 |
< |
&& f->fi[k=j].nrd == nrays) |
360 |
< |
break; |
358 |
> |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
359 |
> |
if (f->fi[k=j].nrd == nrays) |
360 |
> |
break; |
361 |
|
if (k < 0) { /* no fragment -- extend file */ |
362 |
|
hp->bi[i].fo = f->flen; |
363 |
|
f->flen += nrays*sizeof(RAYVAL); |