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 1500 /* maximum beams to free at a time */ |
20 |
|
#endif |
21 |
|
#ifndef PCTFREE |
22 |
|
#define PCTFREE 15 /* maximum fraction to free (%) */ |
23 |
|
#endif |
24 |
|
#ifndef MAXFRAG |
25 |
< |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
25 |
> |
#define MAXFRAG 8191 /* maximum fragments/file to track (0==inf) */ |
26 |
|
#endif |
27 |
+ |
#ifndef MINDIRSEL |
28 |
+ |
/* minimum directory seek length */ |
29 |
+ |
#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) |
30 |
+ |
#endif |
31 |
|
|
32 |
|
#ifndef BSD |
33 |
|
#define write writebuf /* safe i/o routines */ |
52 |
|
static int nhdfragls; /* size of hdfragl array */ |
53 |
|
|
54 |
|
|
55 |
+ |
char * |
56 |
+ |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
57 |
+ |
char *ptr; |
58 |
+ |
unsigned siz; |
59 |
+ |
char *rout; |
60 |
+ |
{ |
61 |
+ |
register char *newp; |
62 |
+ |
/* call malloc/realloc */ |
63 |
+ |
if (ptr == NULL) newp = (char *)malloc(siz); |
64 |
+ |
else newp = (char *)realloc(ptr, siz); |
65 |
+ |
/* check success */ |
66 |
+ |
if (newp == NULL && rout != NULL) { |
67 |
+ |
hdfreecache(25, NULL); /* free some memory */ |
68 |
+ |
errno = 0; /* retry */ |
69 |
+ |
newp = hdrealloc(ptr, siz, NULL); |
70 |
+ |
if (newp == NULL) { /* give up and report error */ |
71 |
+ |
sprintf(errmsg, "out of memory in %s", rout); |
72 |
+ |
error(SYSTEM, errmsg); |
73 |
+ |
} |
74 |
+ |
} |
75 |
+ |
return(newp); |
76 |
+ |
} |
77 |
+ |
|
78 |
+ |
|
79 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
80 |
|
register int fd; |
81 |
|
{ |
82 |
|
if (fd >= nhdfragls) { |
83 |
< |
if (nhdfragls) |
84 |
< |
hdfragl = (struct fraglist *)realloc((char *)hdfragl, |
57 |
< |
(fd+1)*sizeof(struct fraglist)); |
58 |
< |
else |
59 |
< |
hdfragl = (struct fraglist *)malloc( |
60 |
< |
(fd+1)*sizeof(struct fraglist)); |
61 |
< |
if (hdfragl == NULL) |
62 |
< |
error(SYSTEM, "out of memory in hdattach"); |
83 |
> |
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
84 |
> |
(fd+1)*sizeof(struct fraglist), "hdattach"); |
85 |
|
bzero((char *)(hdfragl+nhdfragls), |
86 |
|
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
87 |
|
nhdfragls = fd+1; |
107 |
|
} |
108 |
|
|
109 |
|
|
88 |
– |
markdirty(hp) /* mark holodeck section directory dirty */ |
89 |
– |
register HOLO *hp; |
90 |
– |
{ |
91 |
– |
static BEAMI smudge = {0, -1}; |
92 |
– |
|
93 |
– |
if (hp->dirty) /* already marked? */ |
94 |
– |
return; |
95 |
– |
hp->dirty = 1; |
96 |
– |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
97 |
– |
|| write(hp->fd, (char *)&smudge, |
98 |
– |
sizeof(BEAMI)) != sizeof(BEAMI)) |
99 |
– |
error(SYSTEM, "seek/write error in markdirty"); |
100 |
– |
} |
101 |
– |
|
102 |
– |
|
110 |
|
HOLO * |
111 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
112 |
|
int fd; /* corresponding file descriptor */ |
133 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
134 |
|
error(SYSTEM, "failure loading holodeck directory"); |
135 |
|
/* check that it's clean */ |
136 |
< |
if (hp->bi[nbeams(hp)].fo < 0) |
137 |
< |
error(WARNING, "dirty holodeck section"); |
136 |
> |
for (n = nbeams(hp); n > 0; n--) |
137 |
> |
if (hp->bi[n].fo < 0) { |
138 |
> |
hp->bi[n].fo = 0; |
139 |
> |
error(WARNING, "dirty holodeck section"); |
140 |
> |
break; |
141 |
> |
} |
142 |
|
} else { /* assume we're creating it */ |
143 |
|
if ((hp = hdalloc(hproto)) == NULL) |
144 |
|
goto memerr; |
159 |
|
biglob(hp)->nrd = rtrunc = 0; |
160 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
161 |
|
if (hp->bi[n].nrd) |
162 |
< |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) { |
162 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
163 |
|
rtrunc += hp->bi[n].nrd; |
164 |
|
hp->bi[n].nrd = 0; |
165 |
|
} else |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
+ |
markdirty(hp, i) /* mark holodeck directory position dirty */ |
186 |
+ |
register HOLO *hp; |
187 |
+ |
int i; |
188 |
+ |
{ |
189 |
+ |
static BEAMI smudge = {0, -1}; |
190 |
+ |
int mindist, minpos; |
191 |
+ |
register int j; |
192 |
+ |
|
193 |
+ |
if (!hp->dirty++) { /* write smudge first time */ |
194 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
195 |
+ |
|| write(hp->fd, (char *)&smudge, |
196 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
197 |
+ |
error(SYSTEM, "seek/write error in markdirty"); |
198 |
+ |
hp->dirseg[0].s = i; |
199 |
+ |
hp->dirseg[0].n = 1; |
200 |
+ |
return; |
201 |
+ |
} |
202 |
+ |
/* insert into segment list */ |
203 |
+ |
for (j = hp->dirty; j--; ) { |
204 |
+ |
if (!j || hp->dirseg[j-1].s < i) { |
205 |
+ |
hp->dirseg[j].s = i; |
206 |
+ |
hp->dirseg[j].n = 1; |
207 |
+ |
break; |
208 |
+ |
} |
209 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
210 |
+ |
} |
211 |
+ |
do { /* check neighbors */ |
212 |
+ |
mindist = nbeams(hp); /* find closest */ |
213 |
+ |
for (j = hp->dirty; --j; ) |
214 |
+ |
if (hp->dirseg[j].s - |
215 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n) |
216 |
+ |
< mindist) { |
217 |
+ |
minpos = j; |
218 |
+ |
mindist = hp->dirseg[j].s - |
219 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n); |
220 |
+ |
} |
221 |
+ |
/* good enough? */ |
222 |
+ |
if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) |
223 |
+ |
break; |
224 |
+ |
j = minpos - 1; /* coalesce neighbors */ |
225 |
+ |
if (hp->dirseg[j].s + hp->dirseg[j].n < |
226 |
+ |
hp->dirseg[minpos].s + hp->dirseg[minpos].n) |
227 |
+ |
hp->dirseg[j].n = hp->dirseg[minpos].s + |
228 |
+ |
hp->dirseg[minpos].n - hp->dirseg[j].s; |
229 |
+ |
hp->dirty--; |
230 |
+ |
while (++j < hp->dirty) /* close the gap */ |
231 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
232 |
+ |
} while (mindist <= MINDIRSEL); |
233 |
+ |
} |
234 |
+ |
|
235 |
+ |
|
236 |
|
int |
237 |
|
hdsync(hp, all) /* update beams and directory on disk */ |
238 |
|
register HOLO *hp; |
252 |
|
hdsyncbeam(hp, j); |
253 |
|
if (!hp->dirty) /* directory clean? */ |
254 |
|
return(0); |
255 |
< |
errno = 0; |
256 |
< |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
257 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
258 |
< |
n = nbeams(hp)*sizeof(BEAMI); |
259 |
< |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
260 |
< |
error(SYSTEM, "cannot update holodeck section directory"); |
261 |
< |
hp->dirty = 0; |
255 |
> |
errno = 0; /* write dirty segments */ |
256 |
> |
for (j = 0; j < hp->dirty; j++) { |
257 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
258 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
259 |
> |
error(SYSTEM, "cannot seek on holodeck file"); |
260 |
> |
n = hp->dirseg[j].n * sizeof(BEAMI); |
261 |
> |
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
262 |
> |
error(SYSTEM, "cannot update section directory"); |
263 |
> |
} |
264 |
> |
hp->dirty = 0; /* all clean */ |
265 |
|
return(1); |
266 |
|
} |
267 |
|
|
357 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
358 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
359 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
295 |
– |
errno = 0; |
360 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
361 |
|
n = hp->bi[i].nrd + nr; |
362 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
299 |
< |
goto memerr; |
362 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
363 |
|
blglob(hp)->nrm += n; |
364 |
|
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
365 |
+ |
errno = 0; |
366 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
367 |
|
error(SYSTEM, "seek error on holodeck file"); |
368 |
|
n *= sizeof(RAYVAL); |
371 |
|
"error reading beam from holodeck file"); |
372 |
|
} |
373 |
|
} else { /* just grow in memory */ |
374 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
375 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
312 |
< |
if (hp->bl[i] == NULL) |
313 |
< |
goto memerr; |
374 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
375 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
376 |
|
blglob(hp)->nrm += nr; |
377 |
|
} |
378 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
379 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
380 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
381 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
320 |
< |
blglob(hp)->tick = hdclock++; |
381 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
382 |
|
return(p); /* point to new rays */ |
322 |
– |
memerr: |
323 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
383 |
|
} |
384 |
|
|
385 |
|
|
397 |
|
return(NULL); |
398 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
399 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
400 |
< |
errno = 0; |
342 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
343 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
400 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
401 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
402 |
+ |
errno = 0; |
403 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
404 |
|
error(SYSTEM, "seek error on holodeck file"); |
405 |
|
n *= sizeof(RAYVAL); |
406 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
407 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
408 |
|
} |
409 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
352 |
< |
blglob(hp)->tick = hdclock++; |
409 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
410 |
|
return(hp->bl[i]); |
411 |
|
} |
412 |
|
|
440 |
|
register BEAM *bp; |
441 |
|
register int i; |
442 |
|
/* precheck consistency */ |
443 |
+ |
if (n <= 0) return; |
444 |
|
for (i = n; i--; ) |
445 |
|
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
446 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
506 |
|
f->nfrags--; |
507 |
|
#endif |
508 |
|
if (j % FRAGBLK == 0) { /* more free list space */ |
509 |
+ |
register BEAMI *newp; |
510 |
|
if (f->fi == NULL) |
511 |
< |
f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); |
511 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
512 |
|
else |
513 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
513 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
514 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
515 |
< |
if (f->fi == NULL) |
516 |
< |
error(SYSTEM, "out of memory in hdfreefrag"); |
515 |
> |
if (newp == NULL) { |
516 |
> |
f->nfrags--; /* graceful failure */ |
517 |
> |
return; |
518 |
> |
} |
519 |
> |
f->fi = newp; |
520 |
|
} |
521 |
|
for ( ; ; j--) { /* insert in descending list */ |
522 |
|
if (!j || bi->fo < f->fi[j-1].fo) { |
614 |
|
hp->bi[i].fo = 0L; |
615 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
616 |
|
hp->bi[i].nrd = nrays; |
617 |
< |
markdirty(hp); /* section directory now out of date */ |
617 |
> |
markdirty(hp, i); /* section directory now out of date */ |
618 |
|
return(1); |
619 |
|
} |
620 |
|
|
742 |
|
int freetarget; |
743 |
|
int n; |
744 |
|
register int i; |
745 |
+ |
#ifdef DEBUG |
746 |
+ |
unsigned membefore; |
747 |
+ |
|
748 |
+ |
membefore = hdmemuse(0); |
749 |
+ |
#endif |
750 |
|
/* compute free target */ |
751 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
752 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
767 |
|
break; |
768 |
|
} |
769 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
770 |
+ |
#ifdef DEBUG |
771 |
+ |
sprintf(errmsg, |
772 |
+ |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
773 |
+ |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
774 |
+ |
wputs(errmsg); |
775 |
+ |
#endif |
776 |
|
return(-freetarget); /* return how far past goal we went */ |
777 |
|
} |
778 |
|
|
785 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
786 |
|
while (hdlist[0] != NULL) |
787 |
|
hddone(hdlist[0]); |
788 |
+ |
free((char *)hdfragl); |
789 |
+ |
hdfragl = NULL; nhdfragls = 0; |
790 |
|
return; |
791 |
|
} |
792 |
|
/* flush all data and free memory */ |
793 |
< |
hdflush(hp); |
793 |
> |
hdfreebeam(hp, 0); |
794 |
> |
hdsync(hp, 0); |
795 |
|
/* release fragment resources */ |
796 |
|
hdrelease(hp->fd); |
797 |
|
/* remove hp from active list */ |