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 */ |
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 |
27 |
– |
#ifndef MAXFRAG |
28 |
– |
#define MAXFRAG 2048 /* maximum fragments tracked in each file */ |
29 |
– |
#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 |
|
|
39 |
|
static struct fragment { |
40 |
|
short nlinks; /* number of holodeck sections using us */ |
41 |
|
short nfrags; /* number of known fragments */ |
42 |
< |
BEAMI fi[MAXFRAG]; /* fragments, descending file position */ |
42 |
> |
BEAMI *fi; /* fragments, descending file position */ |
43 |
|
long flen; /* last known file length */ |
44 |
< |
} *hdfrag[FFDMAX]; /* fragment lists, indexed by file descriptor */ |
44 |
> |
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
45 |
|
|
46 |
+ |
static int nhdfrags; /* size of hdfrag array */ |
47 |
|
|
48 |
+ |
|
49 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
50 |
|
register int fd; |
51 |
|
{ |
52 |
< |
if (fd >= FFDMAX) |
53 |
< |
return; /* descriptor out of range */ |
54 |
< |
if (hdfrag[fd] == NULL) { |
55 |
< |
if ((hdfrag[fd] = (struct fragment *) |
56 |
< |
malloc(sizeof(struct fragment))) == NULL) |
57 |
< |
return; /* should flag error? */ |
58 |
< |
hdfrag[fd]->nlinks = 0; |
59 |
< |
hdfrag[fd]->nfrags = 0; |
52 |
> |
if (fd >= nhdfrags) { |
53 |
> |
if (nhdfrags) |
54 |
> |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
55 |
> |
(fd+1)*sizeof(struct fragment)); |
56 |
> |
else |
57 |
> |
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; |
64 |
|
} |
65 |
< |
hdfrag[fd]->nlinks++; |
66 |
< |
hdfrag[fd]->flen = lseek(fd, 0L, 2); /* get file length */ |
65 |
> |
hdfrag[fd].nlinks++; |
66 |
> |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
67 |
|
} |
68 |
|
|
69 |
|
|
73 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
74 |
|
register int fd; |
75 |
|
{ |
76 |
< |
if (fd >= FFDMAX || hdfrag[fd] == NULL) |
76 |
> |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) |
77 |
|
return; |
78 |
< |
if (!--hdfrag[fd]->nlinks) { |
79 |
< |
free((char *)hdfrag[fd]); |
80 |
< |
hdfrag[fd] = NULL; |
78 |
> |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
79 |
> |
free((char *)hdfrag[fd].fi); |
80 |
> |
hdfrag[fd].fi = NULL; |
81 |
> |
hdfrag[fd].nfrags = 0; |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
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 |
+ |
|
101 |
|
HOLO * |
102 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
103 |
|
int fd; /* corresponding file descriptor */ |
122 |
|
n = nbeams(hp)*sizeof(BEAMI); |
123 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
124 |
|
error(SYSTEM, "failure loading holodeck directory"); |
125 |
+ |
/* check that it's clean */ |
126 |
+ |
if (hp->bi[nbeams(hp)].fo < 0) |
127 |
+ |
error(USER, "dirty holodeck section"); |
128 |
|
} else { /* assume we're creating it */ |
129 |
|
if ((hp = hdalloc(hproto)) == NULL) |
130 |
|
goto memerr; |
170 |
|
} |
171 |
|
if (!hp->dirty) /* check first */ |
172 |
|
return(0); |
173 |
+ |
errno = 0; |
174 |
|
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
175 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
176 |
|
n = nbeams(hp)*sizeof(BEAMI); |
200 |
|
} |
201 |
|
} |
202 |
|
if (all) |
203 |
< |
for (j = 0; j < FFDMAX; j++) |
204 |
< |
if (hdfrag[j] != NULL) |
205 |
< |
total += sizeof(struct fragment); |
203 |
> |
for (j = 0; j < nhdfrags; j++) { |
204 |
> |
total += sizeof(struct fragment); |
205 |
> |
if (hdfrag[j].nfrags) |
206 |
> |
total += FRAGBLK*sizeof(BEAMI) * |
207 |
> |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
208 |
> |
} |
209 |
|
return(total); |
210 |
|
} |
211 |
|
|
253 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
254 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
255 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
256 |
+ |
errno = 0; |
257 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
258 |
|
n = hp->bi[i].nrd + nr; |
259 |
|
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
325 |
|
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
326 |
|
return(0); |
327 |
|
|
328 |
< |
if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ |
328 |
> |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
329 |
|
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
330 |
|
|
331 |
|
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
332 |
< |
hdfrag[hp->fd]->flen) /* EOF special case */ |
333 |
< |
hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
332 |
> |
hdfrag[hp->fd].flen) /* EOF special case */ |
333 |
> |
hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
334 |
|
|
335 |
|
else { /* general case */ |
336 |
< |
register struct fragment *f = hdfrag[hp->fd]; |
336 |
> |
register struct fragment *f = &hdfrag[hp->fd]; |
337 |
|
register int j, k; |
338 |
|
/* relinquish old fragment */ |
339 |
|
if (hp->bi[i].nrd) { |
340 |
< |
if ((j = ++f->nfrags) >= MAXFRAG) |
340 |
> |
j = f->nfrags++; |
341 |
> |
#ifdef MAXFRAG |
342 |
> |
if (j >= MAXFRAG-1) |
343 |
|
f->nfrags--; |
344 |
< |
for ( ; ; ) { /* stick it in our descending list */ |
345 |
< |
if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { |
344 |
> |
#endif |
345 |
> |
if (j % FRAGBLK == 0) { /* more frag. space */ |
346 |
> |
if (f->fi == NULL) |
347 |
> |
f->fi = (BEAMI *)malloc( |
348 |
> |
FRAGBLK*sizeof(BEAMI)); |
349 |
> |
else |
350 |
> |
f->fi = (BEAMI *)realloc((char *)f->fi, |
351 |
> |
(j+FRAGBLK)*sizeof(BEAMI)); |
352 |
> |
if (f->fi == NULL) |
353 |
> |
error(SYSTEM, |
354 |
> |
"out of memory in hdgetbi"); |
355 |
> |
} |
356 |
> |
for ( ; ; j--) { /* insert in descending list */ |
357 |
> |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
358 |
|
f->fi[j].fo = hp->bi[i].fo; |
359 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
360 |
|
break; |
363 |
|
} |
364 |
|
/* coalesce adjacent fragments */ |
365 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
366 |
< |
if (j != k) |
366 |
> |
if (k > j) |
367 |
|
copystruct(f->fi+j, f->fi+k); |
368 |
< |
while (k+1 < f->nfrags && |
369 |
< |
f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL) |
370 |
< |
== f->fi[j].fo) |
368 |
> |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
369 |
> |
f->fi[k+1].nrd*sizeof(RAYVAL) |
370 |
> |
== f->fi[j].fo) { |
371 |
|
f->fi[j].fo -= |
372 |
|
f->fi[++k].nrd*sizeof(RAYVAL); |
373 |
+ |
f->fi[j].nrd += f->fi[k].nrd; |
374 |
+ |
} |
375 |
|
} |
376 |
|
f->nfrags = j; |
377 |
|
} |
378 |
|
k = -1; /* find closest-sized fragment */ |
379 |
|
for (j = f->nfrags; j-- > 0; ) |
380 |
|
if (f->fi[j].nrd >= nrays && |
381 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd) |
382 |
< |
&& f->fi[k=j].nrd == nrays) |
383 |
< |
break; |
381 |
> |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
382 |
> |
if (f->fi[k=j].nrd == nrays) |
383 |
> |
break; |
384 |
|
if (k < 0) { /* no fragment -- extend file */ |
385 |
|
hp->bi[i].fo = f->flen; |
386 |
|
f->flen += nrays*sizeof(RAYVAL); |
398 |
|
} |
399 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
400 |
|
hp->bi[i].nrd = nrays; |
401 |
< |
hp->dirty++; /* section directory now out of date */ |
401 |
> |
markdirty(hp); /* section directory now out of date */ |
402 |
|
return(1); |
403 |
|
} |
404 |
|
|