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 (%) */ |
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; |
166 |
|
biglob(hp)->nrd = rtrunc = 0; |
167 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
168 |
|
if (hp->bi[n].nrd) |
169 |
< |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) { |
169 |
> |
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
170 |
|
rtrunc += hp->bi[n].nrd; |
171 |
|
hp->bi[n].nrd = 0; |
172 |
|
} else |
310 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
311 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
312 |
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
295 |
– |
errno = 0; |
313 |
|
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
314 |
|
n = hp->bi[i].nrd + nr; |
315 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
299 |
< |
goto memerr; |
315 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
316 |
|
blglob(hp)->nrm += n; |
317 |
|
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
318 |
+ |
errno = 0; |
319 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
320 |
|
error(SYSTEM, "seek error on holodeck file"); |
321 |
|
n *= sizeof(RAYVAL); |
324 |
|
"error reading beam from holodeck file"); |
325 |
|
} |
326 |
|
} else { /* just grow in memory */ |
327 |
< |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
328 |
< |
hdbsiz(hp->bl[i]->nrm + nr) ); |
312 |
< |
if (hp->bl[i] == NULL) |
313 |
< |
goto memerr; |
327 |
> |
hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], |
328 |
> |
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
329 |
|
blglob(hp)->nrm += nr; |
330 |
|
} |
331 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
332 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
333 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
334 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
320 |
< |
blglob(hp)->tick = hdclock++; |
334 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
335 |
|
return(p); /* point to new rays */ |
322 |
– |
memerr: |
323 |
– |
error(SYSTEM, "out of memory in hdnewrays"); |
336 |
|
} |
337 |
|
|
338 |
|
|
350 |
|
return(NULL); |
351 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
352 |
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
353 |
< |
errno = 0; |
342 |
< |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
343 |
< |
error(SYSTEM, "cannot allocate memory for beam"); |
353 |
> |
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
354 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
355 |
+ |
errno = 0; |
356 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
357 |
|
error(SYSTEM, "seek error on holodeck file"); |
358 |
|
n *= sizeof(RAYVAL); |
359 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
360 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
361 |
|
} |
362 |
< |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
352 |
< |
blglob(hp)->tick = hdclock++; |
362 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
363 |
|
return(hp->bl[i]); |
364 |
|
} |
365 |
|
|
393 |
|
register BEAM *bp; |
394 |
|
register int i; |
395 |
|
/* precheck consistency */ |
396 |
+ |
if (n <= 0) return; |
397 |
|
for (i = n; i--; ) |
398 |
|
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
399 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
459 |
|
f->nfrags--; |
460 |
|
#endif |
461 |
|
if (j % FRAGBLK == 0) { /* more free list space */ |
462 |
+ |
register BEAMI *newp; |
463 |
|
if (f->fi == NULL) |
464 |
< |
f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); |
464 |
> |
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
465 |
|
else |
466 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
466 |
> |
newp = (BEAMI *)realloc((char *)f->fi, |
467 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
468 |
< |
if (f->fi == NULL) |
469 |
< |
error(SYSTEM, "out of memory in hdfreefrag"); |
468 |
> |
if (newp == NULL) { |
469 |
> |
f->nfrags--; /* graceful failure */ |
470 |
> |
return; |
471 |
> |
} |
472 |
> |
f->fi = newp; |
473 |
|
} |
474 |
|
for ( ; ; j--) { /* insert in descending list */ |
475 |
|
if (!j || bi->fo < f->fi[j-1].fo) { |
695 |
|
int freetarget; |
696 |
|
int n; |
697 |
|
register int i; |
698 |
+ |
#ifdef DEBUG |
699 |
+ |
unsigned membefore; |
700 |
+ |
|
701 |
+ |
membefore = hdmemuse(0); |
702 |
+ |
#endif |
703 |
|
/* compute free target */ |
704 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
705 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
720 |
|
break; |
721 |
|
} |
722 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
723 |
+ |
#ifdef DEBUG |
724 |
+ |
sprintf(errmsg, |
725 |
+ |
"%dK before, %dK after hdfreecache (%dK total), %d rays short\n", |
726 |
+ |
membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); |
727 |
+ |
wputs(errmsg); |
728 |
+ |
#endif |
729 |
|
return(-freetarget); /* return how far past goal we went */ |
730 |
|
} |
731 |
|
|
743 |
|
return; |
744 |
|
} |
745 |
|
/* flush all data and free memory */ |
746 |
< |
hdflush(hp); |
746 |
> |
hdfreebeam(hp, 0); |
747 |
> |
hdsync(hp, 0); |
748 |
|
/* release fragment resources */ |
749 |
|
hdrelease(hp->fd); |
750 |
|
/* remove hp from active list */ |