1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
16 |
|
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
|
#endif |
18 |
|
#ifndef FREEBEAMS |
19 |
< |
#define FREEBEAMS 512 /* 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 20 /* maximum fraction to free (%) */ |
22 |
> |
#define PCTFREE 15 /* maximum fraction to free (%) */ |
23 |
|
#endif |
24 |
|
#ifndef MAXFRAG |
25 |
|
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
48 |
|
static int nhdfragls; /* size of hdfragl array */ |
49 |
|
|
50 |
|
|
51 |
+ |
char * |
52 |
+ |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
53 |
+ |
char *ptr; |
54 |
+ |
unsigned siz; |
55 |
+ |
char *rout; |
56 |
+ |
{ |
57 |
+ |
register char *newp; |
58 |
+ |
/* call malloc/realloc */ |
59 |
+ |
if (ptr == NULL) newp = (char *)malloc(siz); |
60 |
+ |
else newp = (char *)realloc(ptr, siz); |
61 |
+ |
/* check success */ |
62 |
+ |
if (newp == NULL && rout != NULL) { |
63 |
+ |
hdfreecache(25, NULL); /* free some memory */ |
64 |
+ |
errno = 0; /* retry */ |
65 |
+ |
newp = hdrealloc(ptr, siz, NULL); |
66 |
+ |
if (newp == NULL) { /* give up and report error */ |
67 |
+ |
sprintf(errmsg, "out of memory in %s", rout); |
68 |
+ |
error(SYSTEM, errmsg); |
69 |
+ |
} |
70 |
+ |
} |
71 |
+ |
return(newp); |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
|
75 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
76 |
|
register int fd; |
77 |
|
{ |
78 |
|
if (fd >= nhdfragls) { |
79 |
< |
if (nhdfragls) |
80 |
< |
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"); |
79 |
> |
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
80 |
> |
(fd+1)*sizeof(struct fraglist), "hdattach"); |
81 |
|
bzero((char *)(hdfragl+nhdfragls), |
82 |
|
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
83 |
|
nhdfragls = fd+1; |
103 |
|
} |
104 |
|
|
105 |
|
|
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 |
– |
|
106 |
|
HOLO * |
107 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
108 |
|
int fd; /* corresponding file descriptor */ |
109 |
|
HDGRID *hproto; /* holodeck section grid */ |
110 |
|
{ |
111 |
+ |
long rtrunc; |
112 |
|
long fpos; |
113 |
|
register HOLO *hp; |
114 |
|
register int n; |
129 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
130 |
|
error(SYSTEM, "failure loading holodeck directory"); |
131 |
|
/* check that it's clean */ |
132 |
< |
if (hp->bi[nbeams(hp)].fo < 0) |
133 |
< |
error(USER, "dirty holodeck section"); |
132 |
> |
for (n = nbeams(hp); n > 0; n--) |
133 |
> |
if (hp->bi[n].fo < 0) { |
134 |
> |
hp->bi[n].fo = 0; |
135 |
> |
error(WARNING, "dirty holodeck section"); |
136 |
> |
break; |
137 |
> |
} |
138 |
|
} else { /* assume we're creating it */ |
139 |
|
if ((hp = hdalloc(hproto)) == NULL) |
140 |
|
goto memerr; |
152 |
|
hdattach(fd); |
153 |
|
/* check rays on disk */ |
154 |
|
fpos = hdfilen(fd); |
155 |
< |
biglob(hp)->nrd = 0; |
155 |
> |
biglob(hp)->nrd = rtrunc = 0; |
156 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
157 |
|
if (hp->bi[n].nrd) |
158 |
< |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
159 |
< |
hp->bi[n].nrd = 0; /* off end */ |
160 |
< |
else |
158 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
159 |
> |
rtrunc += hp->bi[n].nrd; |
160 |
> |
hp->bi[n].nrd = 0; |
161 |
> |
} else |
162 |
|
biglob(hp)->nrd += hp->bi[n].nrd; |
163 |
+ |
if (rtrunc) { |
164 |
+ |
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
165 |
+ |
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
166 |
+ |
error(WARNING, errmsg); |
167 |
+ |
} |
168 |
|
/* add to holodeck list */ |
169 |
|
for (n = 0; n < HDMAX; n++) |
170 |
|
if (hdlist[n] == NULL) { |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
markdirty(hp, i) /* mark holodeck directory position dirty */ |
182 |
+ |
register HOLO *hp; |
183 |
+ |
int i; |
184 |
+ |
{ |
185 |
+ |
static BEAMI smudge = {0, -1}; |
186 |
+ |
int mindist, minpos; |
187 |
+ |
register int j; |
188 |
+ |
|
189 |
+ |
if (!hp->dirty++) { /* write smudge first time */ |
190 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
191 |
+ |
|| write(hp->fd, (char *)&smudge, |
192 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
193 |
+ |
error(SYSTEM, "seek/write error in markdirty"); |
194 |
+ |
hp->dirseg[0].s = i; |
195 |
+ |
hp->dirseg[0].n = 1; |
196 |
+ |
return; |
197 |
+ |
} |
198 |
+ |
/* insert into segment list */ |
199 |
+ |
for (j = hp->dirty; j--; ) { |
200 |
+ |
if (!j || hp->dirseg[j-1].s < i) { |
201 |
+ |
hp->dirseg[j].s = i; |
202 |
+ |
hp->dirseg[j].n = 1; |
203 |
+ |
break; |
204 |
+ |
} |
205 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
206 |
+ |
} |
207 |
+ |
mindist = nbeams(hp); /* find closest neighbors */ |
208 |
+ |
for (j = hp->dirty; --j; ) |
209 |
+ |
if (hp->dirseg[j].s - (hp->dirseg[j-1].s + hp->dirseg[j-1].n) |
210 |
+ |
< mindist) { |
211 |
+ |
mindist = hp->dirseg[j].s - |
212 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n); |
213 |
+ |
minpos = j; |
214 |
+ |
} |
215 |
+ |
if (hp->dirty > MAXDIRSE || mindist <= BUFSIZ/sizeof(BEAMI)) { |
216 |
+ |
j = minpos - 1; /* coalesce neighbors */ |
217 |
+ |
if (hp->dirseg[j].s + hp->dirseg[j].n < |
218 |
+ |
hp->dirseg[minpos].s + hp->dirseg[minpos].n) { |
219 |
+ |
hp->dirseg[j].n = hp->dirseg[minpos].s + |
220 |
+ |
hp->dirseg[minpos].n - hp->dirseg[j].s; |
221 |
+ |
} |
222 |
+ |
hp->dirty--; /* close the gap */ |
223 |
+ |
for (j = minpos; j < hp->dirty; j++) |
224 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
225 |
+ |
} |
226 |
+ |
} |
227 |
+ |
|
228 |
+ |
|
229 |
|
int |
230 |
|
hdsync(hp, all) /* update beams and directory on disk */ |
231 |
|
register HOLO *hp; |
245 |
|
hdsyncbeam(hp, j); |
246 |
|
if (!hp->dirty) /* directory clean? */ |
247 |
|
return(0); |
248 |
< |
errno = 0; |
249 |
< |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
250 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
251 |
< |
n = nbeams(hp)*sizeof(BEAMI); |
252 |
< |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
253 |
< |
error(SYSTEM, "cannot update holodeck section directory"); |
254 |
< |
hp->dirty = 0; |
248 |
> |
errno = 0; /* write dirty segments */ |
249 |
> |
for (j = 0; j < hp->dirty; j++) { |
250 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
251 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
252 |
> |
error(SYSTEM, "cannot seek on holodeck file"); |
253 |
> |
n = hp->dirseg[j].n * sizeof(BEAMI); |
254 |
> |
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
255 |
> |
error(SYSTEM, "cannot update section directory"); |
256 |
> |
} |
257 |
> |
hp->dirty = 0; /* all clean */ |
258 |
|
return(1); |
259 |
|
} |
260 |
|
|
350 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
351 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
352 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
288 |
– |
errno = 0; |
353 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
354 |
|
n = hp->bi[i].nrd + nr; |
355 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
292 |
< |
goto memerr; |
355 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
356 |
|
blglob(hp)->nrm += n; |
357 |
|
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
358 |
+ |
errno = 0; |
359 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
360 |
|
error(SYSTEM, "seek error on holodeck file"); |
361 |
|
n *= sizeof(RAYVAL); |
364 |
|
"error reading beam from holodeck file"); |
365 |
|
} |
366 |
|
} else { /* just grow in memory */ |
367 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
368 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
305 |
< |
if (hp->bl[i] == NULL) |
306 |
< |
goto memerr; |
367 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
368 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
369 |
|
blglob(hp)->nrm += nr; |
370 |
|
} |
371 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
372 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
373 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
374 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
313 |
< |
blglob(hp)->tick = hdclock++; |
374 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
375 |
|
return(p); /* point to new rays */ |
315 |
– |
memerr: |
316 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
376 |
|
} |
377 |
|
|
378 |
|
|
390 |
|
return(NULL); |
391 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
392 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
393 |
< |
errno = 0; |
335 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
336 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
393 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
394 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
395 |
+ |
errno = 0; |
396 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
397 |
|
error(SYSTEM, "seek error on holodeck file"); |
398 |
|
n *= sizeof(RAYVAL); |
399 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
400 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
401 |
|
} |
402 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
345 |
< |
blglob(hp)->tick = hdclock++; |
402 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
403 |
|
return(hp->bl[i]); |
404 |
|
} |
405 |
|
|
429 |
|
int (*bf)(); /* callback function (optional) */ |
430 |
|
{ |
431 |
|
unsigned origcachesize, memuse; |
375 |
– |
register BEAM *bp; |
432 |
|
int bytesloaded, needbytes, bytes2free; |
433 |
+ |
register BEAM *bp; |
434 |
|
register int i; |
435 |
|
/* precheck consistency */ |
436 |
+ |
if (n <= 0) return; |
437 |
|
for (i = n; i--; ) |
438 |
|
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
439 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
441 |
|
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
442 |
|
bytesloaded = 0; /* run through loaded beams */ |
443 |
|
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
444 |
< |
bp->tick = hdclock; /* preempt swap */ |
444 |
> |
bp->tick = hdclock; /* preempt swap */ |
445 |
|
bytesloaded += bp->nrm; |
446 |
|
if (bf != NULL) |
447 |
< |
(*bf)(bp, hb->h, hb->b); |
447 |
> |
(*bf)(bp, hb); |
448 |
|
} |
449 |
|
bytesloaded *= sizeof(RAYVAL); |
450 |
|
if ((origcachesize = hdcachesize) > 0) { |
463 |
|
} |
464 |
|
for (i = 0; i < n; i++) |
465 |
|
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
466 |
< |
(*bf)(bp, hb[i].h, hb[i].b); |
466 |
> |
(*bf)(bp, hb+i); |
467 |
|
hdcachesize = origcachesize; /* resume dynamic swapping */ |
468 |
|
} |
469 |
|
|
499 |
|
f->nfrags--; |
500 |
|
#endif |
501 |
|
if (j % FRAGBLK == 0) { /* more free list space */ |
502 |
+ |
register BEAMI *newp; |
503 |
|
if (f->fi == NULL) |
504 |
< |
f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); |
504 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
505 |
|
else |
506 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
506 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
507 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
508 |
< |
if (f->fi == NULL) |
509 |
< |
error(SYSTEM, "out of memory in hdfreefrag"); |
508 |
> |
if (newp == NULL) { |
509 |
> |
f->nfrags--; /* graceful failure */ |
510 |
> |
return; |
511 |
> |
} |
512 |
> |
f->fi = newp; |
513 |
|
} |
514 |
|
for ( ; ; j--) { /* insert in descending list */ |
515 |
|
if (!j || bi->fo < f->fi[j-1].fo) { |
607 |
|
hp->bi[i].fo = 0L; |
608 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
609 |
|
hp->bi[i].nrd = nrays; |
610 |
< |
markdirty(hp); /* section directory now out of date */ |
610 |
> |
markdirty(hp, i); /* section directory now out of date */ |
611 |
|
return(1); |
612 |
|
} |
613 |
|
|
703 |
|
{ |
704 |
|
register int i, j; |
705 |
|
/* insert each beam from hp */ |
706 |
< |
for (i = nbeams(hp); i > 0; i--) { |
706 |
> |
for (i = 1; i <= nbeams(hp); i++) { |
707 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
708 |
|
continue; |
709 |
|
#if 0 |
735 |
|
int freetarget; |
736 |
|
int n; |
737 |
|
register int i; |
738 |
+ |
#ifdef DEBUG |
739 |
+ |
unsigned membefore; |
740 |
+ |
|
741 |
+ |
membefore = hdmemuse(0); |
742 |
+ |
#endif |
743 |
|
/* compute free target */ |
744 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
745 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
760 |
|
break; |
761 |
|
} |
762 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
763 |
+ |
#ifdef DEBUG |
764 |
+ |
sprintf(errmsg, |
765 |
+ |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
766 |
+ |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
767 |
+ |
wputs(errmsg); |
768 |
+ |
#endif |
769 |
|
return(-freetarget); /* return how far past goal we went */ |
770 |
|
} |
771 |
|
|
778 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
779 |
|
while (hdlist[0] != NULL) |
780 |
|
hddone(hdlist[0]); |
781 |
+ |
free((char *)hdfragl); |
782 |
+ |
hdfragl = NULL; nhdfragls = 0; |
783 |
|
return; |
784 |
|
} |
785 |
|
/* flush all data and free memory */ |
786 |
< |
hdflush(hp); |
786 |
> |
hdfreebeam(hp, 0); |
787 |
> |
hdsync(hp, 0); |
788 |
|
/* release fragment resources */ |
789 |
|
hdrelease(hp->fd); |
790 |
|
/* remove hp from active list */ |