1 |
< |
/* Copyright (c) 1998 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 17 /* default cache size (Mbytes, 0==inf) */ |
18 |
> |
#else |
19 |
> |
#define CACHESIZE 5 |
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 MAXFRAG |
29 |
< |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
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_WRITE|FF_KILL) |
34 |
+ |
#endif |
35 |
+ |
#ifndef MINDIRSEL |
36 |
+ |
/* minimum directory seek length */ |
37 |
+ |
#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) |
38 |
+ |
#endif |
39 |
|
|
40 |
|
#ifndef BSD |
41 |
|
#define write writebuf /* safe i/o routines */ |
42 |
|
#define read readbuf |
43 |
|
#endif |
44 |
|
|
45 |
< |
#define FRAGBLK 256 /* number of fragments to allocate at a time */ |
45 |
> |
#define FRAGBLK 512 /* number of fragments to allocate at a time */ |
46 |
|
|
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 */ |
49 |
> |
unsigned long hdclock; /* clock value */ |
50 |
|
|
51 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
52 |
|
|
61 |
|
static int nhdfragls; /* size of hdfragl array */ |
62 |
|
|
63 |
|
|
64 |
+ |
HOLO * |
65 |
+ |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
66 |
+ |
HDGRID *hproto; |
67 |
+ |
{ |
68 |
+ |
HOLO hdhead; |
69 |
+ |
register HOLO *hp; |
70 |
+ |
int n; |
71 |
+ |
/* copy grid to temporary header */ |
72 |
+ |
bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); |
73 |
+ |
/* compute grid vectors and sizes */ |
74 |
+ |
hdcompgrid(&hdhead); |
75 |
+ |
/* allocate header with directory */ |
76 |
+ |
n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); |
77 |
+ |
if ((hp = (HOLO *)malloc(n)) == NULL) |
78 |
+ |
return(NULL); |
79 |
+ |
/* copy header information */ |
80 |
+ |
copystruct(hp, &hdhead); |
81 |
+ |
/* allocate and clear beam list */ |
82 |
+ |
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
83 |
+ |
if (hp->bl == NULL) { |
84 |
+ |
free((char *)hp); |
85 |
+ |
return(NULL); |
86 |
+ |
} |
87 |
+ |
bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
88 |
+ |
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
89 |
+ |
hp->fd = -1; |
90 |
+ |
hp->dirty = 0; |
91 |
+ |
hp->priv = NULL; |
92 |
+ |
/* clear beam directory */ |
93 |
+ |
bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
94 |
+ |
return(hp); /* all is well */ |
95 |
+ |
} |
96 |
+ |
|
97 |
+ |
|
98 |
+ |
char * |
99 |
+ |
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
100 |
+ |
char *ptr; |
101 |
+ |
unsigned siz; |
102 |
+ |
char *rout; |
103 |
+ |
{ |
104 |
+ |
register char *newp; |
105 |
+ |
/* call malloc/realloc */ |
106 |
+ |
if (ptr == NULL) newp = (char *)malloc(siz); |
107 |
+ |
else newp = (char *)realloc(ptr, siz); |
108 |
+ |
/* check success */ |
109 |
+ |
if (newp == NULL && rout != NULL) { |
110 |
+ |
hdfreecache(25, NULL); /* free some memory */ |
111 |
+ |
errno = 0; /* retry */ |
112 |
+ |
newp = hdrealloc(ptr, siz, NULL); |
113 |
+ |
if (newp == NULL) { /* give up and report error */ |
114 |
+ |
sprintf(errmsg, "out of memory in %s", rout); |
115 |
+ |
error(SYSTEM, errmsg); |
116 |
+ |
} |
117 |
+ |
} |
118 |
+ |
return(newp); |
119 |
+ |
} |
120 |
+ |
|
121 |
+ |
|
122 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
123 |
|
register int fd; |
124 |
|
{ |
125 |
|
if (fd >= nhdfragls) { |
126 |
< |
if (nhdfragls) |
127 |
< |
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"); |
126 |
> |
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
127 |
> |
(fd+1)*sizeof(struct fraglist), "hdattach"); |
128 |
|
bzero((char *)(hdfragl+nhdfragls), |
129 |
|
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
130 |
|
nhdfragls = fd+1; |
150 |
|
} |
151 |
|
|
152 |
|
|
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 |
– |
|
153 |
|
HOLO * |
154 |
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
155 |
|
int fd; /* corresponding file descriptor */ |
156 |
|
HDGRID *hproto; /* holodeck section grid */ |
157 |
|
{ |
158 |
+ |
long rtrunc; |
159 |
|
long fpos; |
160 |
|
register HOLO *hp; |
161 |
|
register int n; |
176 |
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
177 |
|
error(SYSTEM, "failure loading holodeck directory"); |
178 |
|
/* check that it's clean */ |
179 |
< |
if (hp->bi[nbeams(hp)].fo < 0) |
180 |
< |
error(WARNING, "dirty holodeck section"); |
179 |
> |
for (n = nbeams(hp); n > 0; n--) |
180 |
> |
if (hp->bi[n].fo < 0) { |
181 |
> |
hp->bi[n].fo = 0; |
182 |
> |
error(WARNING, "dirty holodeck section"); |
183 |
> |
break; |
184 |
> |
} |
185 |
|
} else { /* assume we're creating it */ |
186 |
|
if ((hp = hdalloc(hproto)) == NULL) |
187 |
|
goto memerr; |
199 |
|
hdattach(fd); |
200 |
|
/* check rays on disk */ |
201 |
|
fpos = hdfilen(fd); |
202 |
< |
biglob(hp)->nrd = 0; |
202 |
> |
biglob(hp)->nrd = rtrunc = 0; |
203 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
204 |
|
if (hp->bi[n].nrd) |
205 |
< |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
206 |
< |
hp->bi[n].nrd = 0; /* off end */ |
207 |
< |
else |
205 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
206 |
> |
rtrunc += hp->bi[n].nrd; |
207 |
> |
hp->bi[n].nrd = 0; |
208 |
> |
} else |
209 |
|
biglob(hp)->nrd += hp->bi[n].nrd; |
210 |
+ |
if (rtrunc) { |
211 |
+ |
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
212 |
+ |
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
213 |
+ |
error(WARNING, errmsg); |
214 |
+ |
} |
215 |
|
/* add to holodeck list */ |
216 |
|
for (n = 0; n < HDMAX; n++) |
217 |
|
if (hdlist[n] == NULL) { |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
+ |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
229 |
+ |
register HOLO *hp; |
230 |
+ |
int i; |
231 |
+ |
{ |
232 |
+ |
static BEAMI smudge = {0, -1}; |
233 |
+ |
int mindist, minpos; |
234 |
+ |
register int j; |
235 |
+ |
|
236 |
+ |
if (!hp->dirty++) { /* write smudge first time */ |
237 |
+ |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
238 |
+ |
|| write(hp->fd, (char *)&smudge, |
239 |
+ |
sizeof(BEAMI)) != sizeof(BEAMI)) |
240 |
+ |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
241 |
+ |
hp->dirseg[0].s = i; |
242 |
+ |
hp->dirseg[0].n = 1; |
243 |
+ |
return; |
244 |
+ |
} |
245 |
+ |
/* insert into segment list */ |
246 |
+ |
for (j = hp->dirty; j--; ) { |
247 |
+ |
if (!j || hp->dirseg[j-1].s < i) { |
248 |
+ |
hp->dirseg[j].s = i; |
249 |
+ |
hp->dirseg[j].n = 1; |
250 |
+ |
break; |
251 |
+ |
} |
252 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
253 |
+ |
} |
254 |
+ |
do { /* check neighbors */ |
255 |
+ |
mindist = nbeams(hp); /* find closest */ |
256 |
+ |
for (j = hp->dirty; --j; ) |
257 |
+ |
if (hp->dirseg[j].s - |
258 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n) |
259 |
+ |
< mindist) { |
260 |
+ |
minpos = j; |
261 |
+ |
mindist = hp->dirseg[j].s - |
262 |
+ |
(hp->dirseg[j-1].s + hp->dirseg[j-1].n); |
263 |
+ |
} |
264 |
+ |
/* good enough? */ |
265 |
+ |
if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) |
266 |
+ |
break; |
267 |
+ |
j = minpos - 1; /* coalesce neighbors */ |
268 |
+ |
if (hp->dirseg[j].s + hp->dirseg[j].n < |
269 |
+ |
hp->dirseg[minpos].s + hp->dirseg[minpos].n) |
270 |
+ |
hp->dirseg[j].n = hp->dirseg[minpos].s + |
271 |
+ |
hp->dirseg[minpos].n - hp->dirseg[j].s; |
272 |
+ |
hp->dirty--; |
273 |
+ |
while (++j < hp->dirty) /* close the gap */ |
274 |
+ |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
275 |
+ |
} while (mindist <= MINDIRSEL); |
276 |
+ |
} |
277 |
+ |
|
278 |
+ |
|
279 |
|
int |
280 |
|
hdsync(hp, all) /* update beams and directory on disk */ |
281 |
|
register HOLO *hp; |
295 |
|
hdsyncbeam(hp, j); |
296 |
|
if (!hp->dirty) /* directory clean? */ |
297 |
|
return(0); |
298 |
< |
errno = 0; |
299 |
< |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
300 |
< |
error(SYSTEM, "cannot seek on holodeck file"); |
301 |
< |
n = nbeams(hp)*sizeof(BEAMI); |
302 |
< |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
303 |
< |
error(SYSTEM, "cannot update holodeck section directory"); |
304 |
< |
hp->dirty = 0; |
298 |
> |
errno = 0; /* write dirty segments */ |
299 |
> |
for (j = 0; j < hp->dirty; j++) { |
300 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
301 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
302 |
> |
error(SYSTEM, "cannot seek on holodeck file"); |
303 |
> |
n = hp->dirseg[j].n * sizeof(BEAMI); |
304 |
> |
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
305 |
> |
error(SYSTEM, "cannot update section directory"); |
306 |
> |
} |
307 |
> |
hp->dirty = 0; /* all clean */ |
308 |
|
return(1); |
309 |
|
} |
310 |
|
|
392 |
|
RAYVAL *p; |
393 |
|
int n; |
394 |
|
|
395 |
< |
if (nr <= 0) |
396 |
< |
return(NULL); |
397 |
< |
if (i < 1 | i > nbeams(hp)) |
283 |
< |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
395 |
> |
if (nr <= 0) return(NULL); |
396 |
> |
CHECK(i < 1 | i > nbeams(hp), |
397 |
> |
CONSISTENCY, "bad beam index given to hdnewrays"); |
398 |
|
if (hp->bl[i] != NULL) |
399 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
400 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
401 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
288 |
– |
errno = 0; |
402 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
403 |
|
n = hp->bi[i].nrd + nr; |
404 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
292 |
< |
goto memerr; |
404 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
405 |
|
blglob(hp)->nrm += n; |
406 |
< |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
406 |
> |
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
407 |
> |
errno = 0; |
408 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
409 |
|
error(SYSTEM, "seek error on holodeck file"); |
410 |
|
n *= sizeof(RAYVAL); |
413 |
|
"error reading beam from holodeck file"); |
414 |
|
} |
415 |
|
} else { /* just grow in memory */ |
416 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
417 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
305 |
< |
if (hp->bl[i] == NULL) |
306 |
< |
goto memerr; |
416 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
417 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
418 |
|
blglob(hp)->nrm += nr; |
419 |
|
} |
420 |
+ |
if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) |
421 |
+ |
hdfreefrag(hp, i); /* relinquish old fragment */ |
422 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
423 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
424 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
425 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
313 |
< |
blglob(hp)->tick = hdclock++; |
425 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
426 |
|
return(p); /* point to new rays */ |
315 |
– |
memerr: |
316 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
427 |
|
} |
428 |
|
|
429 |
|
|
434 |
|
{ |
435 |
|
register int n; |
436 |
|
|
437 |
< |
if (i < 1 | i > nbeams(hp)) |
438 |
< |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
437 |
> |
CHECK(i < 1 | i > nbeams(hp), |
438 |
> |
CONSISTENCY, "bad beam index given to hdgetbeam"); |
439 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
440 |
|
if (!(n = hp->bi[i].nrd)) |
441 |
|
return(NULL); |
442 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
443 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
444 |
< |
errno = 0; |
335 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
336 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
444 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
445 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
446 |
+ |
errno = 0; |
447 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
448 |
|
error(SYSTEM, "seek error on holodeck file"); |
449 |
|
n *= sizeof(RAYVAL); |
450 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
451 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
452 |
+ |
if (hdfragflags&FF_READ) |
453 |
+ |
hdfreefrag(hp, i); /* relinquish old frag. */ |
454 |
|
} |
455 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
345 |
< |
blglob(hp)->tick = hdclock++; |
455 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
456 |
|
return(hp->bl[i]); |
457 |
|
} |
458 |
|
|
482 |
|
int (*bf)(); /* callback function (optional) */ |
483 |
|
{ |
484 |
|
unsigned origcachesize, memuse; |
375 |
– |
register BEAM *bp; |
485 |
|
int bytesloaded, needbytes, bytes2free; |
486 |
+ |
register BEAM *bp; |
487 |
|
register int i; |
488 |
|
/* precheck consistency */ |
489 |
+ |
if (n <= 0) return; |
490 |
|
for (i = n; i--; ) |
491 |
< |
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
491 |
> |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
492 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
493 |
|
/* sort list for optimal access */ |
494 |
|
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
495 |
|
bytesloaded = 0; /* run through loaded beams */ |
496 |
|
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
497 |
< |
bp->tick = hdclock; /* preempt swap */ |
497 |
> |
bp->tick = hdclock; /* preempt swap */ |
498 |
|
bytesloaded += bp->nrm; |
499 |
|
if (bf != NULL) |
500 |
< |
(*bf)(bp, hb->h, hb->b); |
500 |
> |
(*bf)(bp, hb); |
501 |
|
} |
502 |
|
bytesloaded *= sizeof(RAYVAL); |
503 |
|
if ((origcachesize = hdcachesize) > 0) { |
516 |
|
} |
517 |
|
for (i = 0; i < n; i++) |
518 |
|
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
519 |
< |
(*bf)(bp, hb[i].h, hb[i].b); |
519 |
> |
(*bf)(bp, hb+i); |
520 |
|
hdcachesize = origcachesize; /* resume dynamic swapping */ |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
< |
hdfreefrag(fd, bi) /* free a file fragment */ |
525 |
< |
int fd; |
526 |
< |
register BEAMI *bi; |
524 |
> |
int |
525 |
> |
hdfreefrag(hp, i) /* free a file fragment */ |
526 |
> |
HOLO *hp; |
527 |
> |
int i; |
528 |
|
{ |
529 |
+ |
register BEAMI *bi = &hp->bi[i]; |
530 |
|
register struct fraglist *f; |
531 |
|
register int j, k; |
532 |
|
|
533 |
< |
if (bi->nrd == 0) |
534 |
< |
return; |
535 |
< |
#ifdef DEBUG |
536 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
537 |
< |
error(CONSISTENCY, "bad file descriptor in hdfreefrag"); |
425 |
< |
#endif |
426 |
< |
f = &hdfragl[fd]; |
533 |
> |
if (bi->nrd <= 0) |
534 |
> |
return(0); |
535 |
> |
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
536 |
> |
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
537 |
> |
f = &hdfragl[hp->fd]; |
538 |
|
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
539 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
540 |
|
while (f->fi[k].nrd == 0) |
547 |
|
f->nfrags = j; |
548 |
|
} |
549 |
|
j = f->nfrags++; /* allocate a slot in free list */ |
550 |
< |
#if MAXFRAG |
551 |
< |
if (j >= MAXFRAG-1) |
552 |
< |
f->nfrags--; |
550 |
> |
#if MAXFRAGB |
551 |
> |
if (j >= MAXFRAGB*FRAGBLK) { |
552 |
> |
f->nfrags = j--; /* stop list growth */ |
553 |
> |
if (bi->nrd <= f->fi[j].nrd) |
554 |
> |
return(0); /* new one no better than discard */ |
555 |
> |
} |
556 |
|
#endif |
557 |
< |
if (j % FRAGBLK == 0) { /* more free list space */ |
557 |
> |
if (j % FRAGBLK == 0) { /* more (or less) free list space */ |
558 |
> |
register BEAMI *newp; |
559 |
|
if (f->fi == NULL) |
560 |
< |
f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); |
560 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
561 |
|
else |
562 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
562 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
563 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
564 |
< |
if (f->fi == NULL) |
565 |
< |
error(SYSTEM, "out of memory in hdfreefrag"); |
564 |
> |
if (newp == NULL) { |
565 |
> |
f->nfrags--; /* graceful failure */ |
566 |
> |
return(0); |
567 |
> |
} |
568 |
> |
f->fi = newp; |
569 |
|
} |
570 |
|
for ( ; ; j--) { /* insert in descending list */ |
571 |
|
if (!j || bi->fo < f->fi[j-1].fo) { |
590 |
|
} |
591 |
|
break; |
592 |
|
} |
593 |
+ |
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
594 |
+ |
bi->nrd = 0; |
595 |
+ |
bi->fo = 0L; |
596 |
+ |
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
597 |
+ |
return(1); |
598 |
|
} |
599 |
|
|
600 |
|
|
601 |
+ |
int |
602 |
+ |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
603 |
+ |
int fd; |
604 |
+ |
int *listlen; |
605 |
+ |
register int4 *listsiz; |
606 |
+ |
{ |
607 |
+ |
register struct fraglist *f; |
608 |
+ |
register int i; |
609 |
+ |
|
610 |
+ |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
611 |
+ |
return(0); /* listless */ |
612 |
+ |
if (listlen != NULL) |
613 |
+ |
*listlen = f->nfrags; |
614 |
+ |
if (listsiz != NULL) |
615 |
+ |
for (i = f->nfrags, *listsiz = 0; i--; ) |
616 |
+ |
*listsiz += f->fi[i].nrd; |
617 |
+ |
#if MAXFRAGB |
618 |
+ |
return(f->nfrags < MAXFRAGB*FRAGBLK); |
619 |
+ |
#else |
620 |
+ |
return(1); /* list never fills up */ |
621 |
+ |
#endif |
622 |
+ |
} |
623 |
+ |
|
624 |
+ |
|
625 |
|
long |
626 |
|
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
627 |
|
int fd; |
628 |
|
unsigned int4 nrays; |
629 |
|
{ |
630 |
|
register struct fraglist *f; |
631 |
< |
register int j, k; |
631 |
> |
register int j; |
632 |
|
long nfo; |
633 |
|
|
634 |
|
if (nrays == 0) |
635 |
|
return(-1L); |
636 |
< |
#ifdef DEBUG |
637 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
491 |
< |
error(CONSISTENCY, "bad file descriptor in hdallocfrag"); |
492 |
< |
#endif |
636 |
> |
DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, |
637 |
> |
CONSISTENCY, "bad file descriptor in hdallocfrag"); |
638 |
|
f = &hdfragl[fd]; |
639 |
< |
k = -1; /* find closest-sized fragment */ |
640 |
< |
for (j = f->nfrags; j-- > 0; ) |
641 |
< |
if (f->fi[j].nrd >= nrays && |
642 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
498 |
< |
if (f->fi[k=j].nrd == nrays) |
499 |
< |
break; |
500 |
< |
if (k < 0) { /* no fragment -- extend file */ |
639 |
> |
for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ |
640 |
> |
if (f->fi[j].nrd >= nrays) |
641 |
> |
break; |
642 |
> |
if (j < 0) { /* no fragment -- extend file */ |
643 |
|
nfo = f->flen; |
644 |
|
f->flen += nrays*sizeof(RAYVAL); |
645 |
|
} else { /* else use fragment */ |
646 |
< |
nfo = f->fi[k].fo; |
647 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
648 |
< |
f->fi[k].nrd -= nrays; |
646 |
> |
nfo = f->fi[j].fo; |
647 |
> |
f->fi[j].fo += nrays*sizeof(RAYVAL); |
648 |
> |
f->fi[j].nrd -= nrays; |
649 |
|
} |
650 |
|
return(nfo); |
651 |
|
} |
656 |
|
register HOLO *hp; |
657 |
|
register int i; |
658 |
|
{ |
659 |
+ |
int fragfreed; |
660 |
|
unsigned int4 nrays; |
661 |
|
unsigned int n; |
662 |
|
long nfo; |
663 |
|
/* check file status */ |
664 |
|
if (hdfragl[hp->fd].writerr) |
665 |
|
return(-1); |
666 |
< |
#ifdef DEBUG |
667 |
< |
if (i < 1 | i > nbeams(hp)) |
525 |
< |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
526 |
< |
#endif |
666 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
667 |
> |
CONSISTENCY, "bad beam index in hdsyncbeam"); |
668 |
|
/* is current fragment OK? */ |
669 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
670 |
|
return(0); |
671 |
< |
if (hp->bi[i].nrd) /* relinquish old fragment */ |
672 |
< |
hdfreefrag(hp->fd, &hp->bi[i]); |
671 |
> |
/* relinquish old fragment? */ |
672 |
> |
fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); |
673 |
|
if (nrays) { /* get and write new fragment */ |
674 |
|
nfo = hdallocfrag(hp->fd, nrays); |
675 |
|
errno = 0; |
686 |
|
hp->bi[i].fo = 0L; |
687 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
688 |
|
hp->bi[i].nrd = nrays; |
689 |
< |
markdirty(hp); /* section directory now out of date */ |
689 |
> |
if (!fragfreed) |
690 |
> |
hdmarkdirty(hp, i); /* need to flag dir. ent. */ |
691 |
|
return(1); |
692 |
|
} |
693 |
|
|
708 |
|
if (hdfragl[hp->fd].writerr) /* check for file error */ |
709 |
|
return(0); |
710 |
|
if (i == 0) { /* clear entire holodeck */ |
711 |
+ |
if (blglob(hp)->nrm == 0) |
712 |
+ |
return(0); /* already clear */ |
713 |
|
nchanged = 0; |
714 |
|
for (i = nbeams(hp); i > 0; i--) |
715 |
|
if (hp->bl[i] != NULL) |
716 |
|
nchanged += hdfreebeam(hp, i); |
717 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
718 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
719 |
|
return(nchanged); |
720 |
|
} |
721 |
< |
#ifdef DEBUG |
722 |
< |
if (i < 1 | i > nbeams(hp)) |
577 |
< |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
578 |
< |
#endif |
721 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
722 |
> |
CONSISTENCY, "bad beam index to hdfreebeam"); |
723 |
|
if (hp->bl[i] == NULL) |
724 |
|
return(0); |
725 |
|
/* check for additions */ |
738 |
|
register HOLO *hp; |
739 |
|
register int i; |
740 |
|
{ |
597 |
– |
static BEAM emptybeam; |
741 |
|
int nchanged; |
742 |
|
|
743 |
|
if (hp == NULL) { /* clobber all holodecks */ |
747 |
|
return(nchanged); |
748 |
|
} |
749 |
|
if (i == 0) { /* clobber entire holodeck */ |
750 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
751 |
+ |
return(0); /* already empty */ |
752 |
|
nchanged = 0; |
753 |
+ |
nchanged = 0; |
754 |
|
for (i = nbeams(hp); i > 0; i--) |
755 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
756 |
|
nchanged += hdkillbeam(hp, i); |
757 |
< |
#ifdef DEBUG |
758 |
< |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
613 |
< |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
614 |
< |
#endif |
757 |
> |
DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, |
758 |
> |
CONSISTENCY, "bad beam count in hdkillbeam"); |
759 |
|
return(nchanged); |
760 |
|
} |
761 |
< |
#ifdef DEBUG |
762 |
< |
if (i < 1 | i > nbeams(hp)) |
619 |
< |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
620 |
< |
#endif |
761 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
762 |
> |
CONSISTENCY, "bad beam index to hdkillbeam"); |
763 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
764 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
765 |
|
free((char *)hp->bl[i]); |
766 |
+ |
hp->bl[i] = NULL; |
767 |
|
} else |
768 |
|
nchanged = hp->bi[i].nrd; |
769 |
< |
if (hp->bi[i].nrd) { /* free file fragment */ |
770 |
< |
hp->bl[i] = &emptybeam; |
771 |
< |
hdsyncbeam(hp, i); |
769 |
> |
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
770 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
771 |
> |
hp->bi[i].nrd = 0; |
772 |
> |
hp->bi[i].fo = 0L; |
773 |
> |
hdmarkdirty(hp, i); |
774 |
|
} |
630 |
– |
hp->bl[i] = NULL; |
775 |
|
return(nchanged); |
776 |
|
} |
777 |
|
|
785 |
|
{ |
786 |
|
register int i, j; |
787 |
|
/* insert each beam from hp */ |
788 |
< |
for (i = nbeams(hp); i > 0; i--) { |
788 |
> |
for (i = 1; i <= nbeams(hp); i++) { |
789 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
790 |
|
continue; |
791 |
|
#if 0 |
817 |
|
int freetarget; |
818 |
|
int n; |
819 |
|
register int i; |
820 |
+ |
#ifdef DEBUG |
821 |
+ |
unsigned membefore; |
822 |
+ |
|
823 |
+ |
membefore = hdmemuse(0); |
824 |
+ |
#endif |
825 |
|
/* compute free target */ |
826 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
827 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
842 |
|
break; |
843 |
|
} |
844 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
845 |
+ |
#ifdef DEBUG |
846 |
+ |
sprintf(errmsg, |
847 |
+ |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
848 |
+ |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
849 |
+ |
wputs(errmsg); |
850 |
+ |
#endif |
851 |
|
return(-freetarget); /* return how far past goal we went */ |
852 |
|
} |
853 |
|
|
860 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
861 |
|
while (hdlist[0] != NULL) |
862 |
|
hddone(hdlist[0]); |
863 |
+ |
free((char *)hdfragl); |
864 |
+ |
hdfragl = NULL; nhdfragls = 0; |
865 |
|
return; |
866 |
|
} |
867 |
|
/* flush all data and free memory */ |