1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for managing holodeck files |
6 |
|
* |
7 |
|
* 9/30/97 GWLarson |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
+ |
#include <string.h> |
13 |
+ |
|
14 |
|
#include "holo.h" |
15 |
|
|
16 |
|
#ifndef CACHESIZE |
17 |
< |
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
> |
#ifdef SMLMEM |
18 |
> |
#define CACHESIZE 5 |
19 |
> |
#else |
20 |
> |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
21 |
|
#endif |
22 |
+ |
#endif |
23 |
|
#ifndef FREEBEAMS |
24 |
|
#define FREEBEAMS 1500 /* maximum beams to free at a time */ |
25 |
|
#endif |
26 |
|
#ifndef PCTFREE |
27 |
|
#define PCTFREE 15 /* maximum fraction to free (%) */ |
28 |
|
#endif |
29 |
< |
#ifndef MAXFRAG |
30 |
< |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
29 |
> |
#ifndef MAXFRAGB |
30 |
> |
#define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */ |
31 |
|
#endif |
32 |
+ |
#ifndef FF_DEFAULT |
33 |
+ |
/* when to free a beam fragment */ |
34 |
+ |
#define FF_DEFAULT (FF_WRITE|FF_KILL) |
35 |
+ |
#endif |
36 |
|
#ifndef MINDIRSEL |
37 |
|
/* minimum directory seek length */ |
38 |
|
#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) |
43 |
|
#define read readbuf |
44 |
|
#endif |
45 |
|
|
46 |
< |
#define FRAGBLK 256 /* number of fragments to allocate at a time */ |
46 |
> |
#define FRAGBLK 512 /* number of fragments to allocate at a time */ |
47 |
|
|
48 |
+ |
int hdfragflags = FF_DEFAULT; /* tells when to free fragments */ |
49 |
|
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
50 |
< |
unsigned long hdclock; /* clock value */ |
50 |
> |
unsigned long hdclock; /* clock value */ |
51 |
|
|
52 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
53 |
|
|
54 |
|
static struct fraglist { |
55 |
|
short nlinks; /* number of holodeck sections using us */ |
56 |
< |
short writerr; /* write error encountered */ |
56 |
> |
short writable; /* 0 read-only, <0 write error encountered */ |
57 |
|
int nfrags; /* number of known fragments */ |
58 |
|
BEAMI *fi; /* fragments, descending file position */ |
59 |
< |
long flen; /* last known file length */ |
59 |
> |
off_t flen; /* last known file length */ |
60 |
|
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
61 |
|
|
62 |
|
static int nhdfragls; /* size of hdfragl array */ |
63 |
|
|
64 |
|
|
65 |
+ |
HOLO * |
66 |
+ |
hdalloc(hproto) /* allocate and set holodeck section based on grid */ |
67 |
+ |
HDGRID *hproto; |
68 |
+ |
{ |
69 |
+ |
HOLO hdhead; |
70 |
+ |
register HOLO *hp; |
71 |
+ |
int n; |
72 |
+ |
/* copy grid to temporary header */ |
73 |
+ |
memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID)); |
74 |
+ |
/* compute grid vectors and sizes */ |
75 |
+ |
hdcompgrid(&hdhead); |
76 |
+ |
/* allocate header with directory */ |
77 |
+ |
n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI); |
78 |
+ |
if ((hp = (HOLO *)malloc(n)) == NULL) |
79 |
+ |
return(NULL); |
80 |
+ |
/* copy header information */ |
81 |
+ |
*hp = hdhead; |
82 |
+ |
/* allocate and clear beam list */ |
83 |
+ |
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
84 |
+ |
if (hp->bl == NULL) { |
85 |
+ |
free((void *)hp); |
86 |
+ |
return(NULL); |
87 |
+ |
} |
88 |
+ |
memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
89 |
+ |
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
90 |
+ |
hp->fd = -1; |
91 |
+ |
hp->dirty = 0; |
92 |
+ |
hp->priv = NULL; |
93 |
+ |
/* clear beam directory */ |
94 |
+ |
memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI)); |
95 |
+ |
return(hp); /* all is well */ |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
|
99 |
|
char * |
100 |
|
hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ |
101 |
|
char *ptr; |
105 |
|
register char *newp; |
106 |
|
/* call malloc/realloc */ |
107 |
|
if (ptr == NULL) newp = (char *)malloc(siz); |
108 |
< |
else newp = (char *)realloc(ptr, siz); |
108 |
> |
else newp = (char *)realloc((void *)ptr, siz); |
109 |
|
/* check success */ |
110 |
|
if (newp == NULL && rout != NULL) { |
111 |
|
hdfreecache(25, NULL); /* free some memory */ |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
< |
hdattach(fd) /* start tracking file fragments for some section */ |
123 |
> |
hdattach(fd, wr) /* start tracking file fragments for some section */ |
124 |
|
register int fd; |
125 |
+ |
int wr; |
126 |
|
{ |
127 |
|
if (fd >= nhdfragls) { |
128 |
|
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
129 |
|
(fd+1)*sizeof(struct fraglist), "hdattach"); |
130 |
< |
bzero((char *)(hdfragl+nhdfragls), |
131 |
< |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
130 |
> |
memset((void *)(hdfragl+nhdfragls), |
131 |
> |
'\0', (fd+1-nhdfragls)*sizeof(struct fraglist)); |
132 |
|
nhdfragls = fd+1; |
133 |
|
} |
134 |
|
hdfragl[fd].nlinks++; |
135 |
< |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
135 |
> |
hdfragl[fd].writable = wr; /* set writable flag */ |
136 |
> |
hdfragl[fd].flen = lseek(fd, (off_t)0, 2); /* get file length */ |
137 |
|
} |
138 |
|
|
139 |
|
|
146 |
|
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
147 |
|
return; |
148 |
|
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
149 |
< |
free((char *)hdfragl[fd].fi); |
149 |
> |
free((void *)hdfragl[fd].fi); |
150 |
|
hdfragl[fd].fi = NULL; |
151 |
|
hdfragl[fd].nfrags = 0; |
152 |
|
} |
158 |
|
int fd; /* corresponding file descriptor */ |
159 |
|
HDGRID *hproto; /* holodeck section grid */ |
160 |
|
{ |
161 |
< |
long rtrunc; |
162 |
< |
long fpos; |
161 |
> |
off_t rtrunc; |
162 |
> |
off_t fpos; |
163 |
> |
int writable; |
164 |
|
register HOLO *hp; |
165 |
|
register int n; |
166 |
|
/* prepare for system errors */ |
167 |
|
errno = 0; |
168 |
< |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
168 |
> |
if ((fpos = lseek(fd, (off_t)0, 1)) < 0) |
169 |
|
error(SYSTEM, "cannot determine holodeck file position"); |
170 |
|
if (hproto == NULL) { /* assume we're loading it */ |
171 |
|
HDGRID hpr; |
186 |
|
error(WARNING, "dirty holodeck section"); |
187 |
|
break; |
188 |
|
} |
189 |
< |
} else { /* assume we're creating it */ |
189 |
> |
/* check writability */ |
190 |
> |
if (fd < nhdfragls && hdfragl[fd].nlinks) |
191 |
> |
writable = hdfragl[fd].writable; |
192 |
> |
else |
193 |
> |
writable = lseek(fd, fpos, 0) == fpos && |
194 |
> |
write(fd, (char *)hp, sizeof(HDGRID)) == |
195 |
> |
sizeof(HDGRID); |
196 |
> |
} else { /* else assume we're creating it */ |
197 |
|
if ((hp = hdalloc(hproto)) == NULL) |
198 |
|
goto memerr; |
199 |
|
/* write header and skeleton */ |
202 |
|
sizeof(HDGRID) || |
203 |
|
write(fd, (char *)(hp->bi+1), n) != n) |
204 |
|
error(SYSTEM, "cannot write header to holodeck file"); |
205 |
+ |
writable = 1; |
206 |
|
} |
207 |
|
hp->fd = fd; |
208 |
|
hp->dirty = 0; |
209 |
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
210 |
|
/* start tracking fragments */ |
211 |
< |
hdattach(fd); |
211 |
> |
hdattach(fd, writable); |
212 |
|
/* check rays on disk */ |
213 |
|
fpos = hdfilen(fd); |
214 |
|
biglob(hp)->nrd = rtrunc = 0; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
< |
markdirty(hp, i) /* mark holodeck directory position dirty */ |
240 |
> |
hdmarkdirty(hp, i) /* mark holodeck directory position dirty */ |
241 |
|
register HOLO *hp; |
242 |
|
int i; |
243 |
|
{ |
249 |
|
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 |
250 |
|
|| write(hp->fd, (char *)&smudge, |
251 |
|
sizeof(BEAMI)) != sizeof(BEAMI)) |
252 |
< |
error(SYSTEM, "seek/write error in markdirty"); |
252 |
> |
error(SYSTEM, "seek/write error in hdmarkdirty"); |
253 |
|
hp->dirseg[0].s = i; |
254 |
|
hp->dirseg[0].n = 1; |
255 |
|
return; |
261 |
|
hp->dirseg[j].n = 1; |
262 |
|
break; |
263 |
|
} |
264 |
< |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
264 |
> |
*(hp->dirseg+j) = *(hp->dirseg+(j-1)); |
265 |
|
} |
266 |
|
do { /* check neighbors */ |
267 |
|
mindist = nbeams(hp); /* find closest */ |
283 |
|
hp->dirseg[minpos].n - hp->dirseg[j].s; |
284 |
|
hp->dirty--; |
285 |
|
while (++j < hp->dirty) /* close the gap */ |
286 |
< |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
286 |
> |
*(hp->dirseg+j) = *(hp->dirseg+(j+1)); |
287 |
|
} while (mindist <= MINDIRSEL); |
288 |
|
} |
289 |
|
|
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
< |
long |
353 |
> |
off_t |
354 |
|
hdfilen(fd) /* return file length for fd */ |
355 |
|
int fd; |
356 |
|
{ |
357 |
< |
long fpos, flen; |
357 |
> |
off_t fpos, flen; |
358 |
|
|
359 |
|
if (fd < 0) |
360 |
|
return(-1); |
361 |
|
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
362 |
< |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
362 |
> |
if ((fpos = lseek(fd, (off_t)0, 1)) < 0) |
363 |
|
return(-1); |
364 |
< |
flen = lseek(fd, 0L, 2); |
364 |
> |
flen = lseek(fd, (off_t)0, 2); |
365 |
|
lseek(fd, fpos, 0); |
366 |
|
return(flen); |
367 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
long |
372 |
> |
off_t |
373 |
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
374 |
|
int fd; /* open file descriptor to check */ |
375 |
|
int all; /* include overhead and unflushed data */ |
376 |
|
{ |
377 |
< |
long total = 0; |
377 |
> |
off_t total = 0; |
378 |
|
register int i, j; |
379 |
|
|
380 |
|
for (j = 0; hdlist[j] != NULL; j++) { |
404 |
|
RAYVAL *p; |
405 |
|
int n; |
406 |
|
|
407 |
< |
if (nr <= 0) |
408 |
< |
return(NULL); |
409 |
< |
if (i < 1 | i > nbeams(hp)) |
355 |
< |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
407 |
> |
if (nr <= 0) return(NULL); |
408 |
> |
CHECK(i < 1 | i > nbeams(hp), |
409 |
> |
CONSISTENCY, "bad beam index given to hdnewrays"); |
410 |
|
if (hp->bl[i] != NULL) |
411 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
412 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
415 |
|
n = hp->bi[i].nrd + nr; |
416 |
|
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
417 |
|
blglob(hp)->nrm += n; |
418 |
< |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
418 |
> |
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
419 |
|
errno = 0; |
420 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
421 |
|
error(SYSTEM, "seek error on holodeck file"); |
429 |
|
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
430 |
|
blglob(hp)->nrm += nr; |
431 |
|
} |
432 |
+ |
if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) |
433 |
+ |
hdfreefrag(hp, i); /* relinquish old fragment */ |
434 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
435 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
436 |
< |
bzero((char *)p, nr*sizeof(RAYVAL)); |
436 |
> |
memset((void *)p, '\0', nr*sizeof(RAYVAL)); |
437 |
|
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
438 |
|
return(p); /* point to new rays */ |
439 |
|
} |
446 |
|
{ |
447 |
|
register int n; |
448 |
|
|
449 |
< |
if (i < 1 | i > nbeams(hp)) |
450 |
< |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
449 |
> |
CHECK(i < 1 | i > nbeams(hp), |
450 |
> |
CONSISTENCY, "bad beam index given to hdgetbeam"); |
451 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
452 |
|
if (!(n = hp->bi[i].nrd)) |
453 |
|
return(NULL); |
461 |
|
n *= sizeof(RAYVAL); |
462 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
463 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
464 |
+ |
if (hdfragflags&FF_READ) |
465 |
+ |
hdfreefrag(hp, i); /* relinquish old frag. */ |
466 |
|
} |
467 |
|
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
468 |
|
return(hp->bl[i]); |
473 |
|
hdfilord(hb1, hb2) /* order beams for quick loading */ |
474 |
|
register HDBEAMI *hb1, *hb2; |
475 |
|
{ |
476 |
< |
register long c; |
476 |
> |
register off_t c; |
477 |
|
/* residents go first */ |
478 |
|
if (hb2->h->bl[hb2->b] != NULL) |
479 |
|
return(hb1->h->bl[hb1->b] == NULL); |
480 |
|
if (hb1->h->bl[hb1->b] != NULL) |
481 |
|
return(-1); |
482 |
|
/* otherwise sort by file descriptor */ |
483 |
< |
if ((c = hb1->h->fd - hb2->h->fd)) |
484 |
< |
return(c); |
483 |
> |
if (hb1->h->fd != hb2->h->fd) |
484 |
> |
return(hb1->h->fd - hb2->h->fd); |
485 |
|
/* then by position in file */ |
486 |
|
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
487 |
|
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
491 |
|
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
492 |
|
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
493 |
|
int n; /* list length */ |
494 |
< |
int (*bf)(); /* callback function (optional) */ |
494 |
> |
void (*bf)(); /* callback function (optional) */ |
495 |
|
{ |
496 |
|
unsigned origcachesize, memuse; |
497 |
|
int bytesloaded, needbytes, bytes2free; |
500 |
|
/* precheck consistency */ |
501 |
|
if (n <= 0) return; |
502 |
|
for (i = n; i--; ) |
503 |
< |
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
503 |
> |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
504 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
505 |
|
/* sort list for optimal access */ |
506 |
< |
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
506 |
> |
qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord); |
507 |
|
bytesloaded = 0; /* run through loaded beams */ |
508 |
|
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
509 |
|
bp->tick = hdclock; /* preempt swap */ |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
hdfreefrag(fd, bi) /* free a file fragment */ |
537 |
< |
int fd; |
538 |
< |
register BEAMI *bi; |
536 |
> |
int |
537 |
> |
hdfreefrag(hp, i) /* free a file fragment */ |
538 |
> |
HOLO *hp; |
539 |
> |
int i; |
540 |
|
{ |
541 |
+ |
register BEAMI *bi = &hp->bi[i]; |
542 |
|
register struct fraglist *f; |
543 |
|
register int j, k; |
544 |
|
|
545 |
< |
if (bi->nrd == 0) |
546 |
< |
return; |
547 |
< |
#ifdef DEBUG |
548 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
549 |
< |
error(CONSISTENCY, "bad file descriptor in hdfreefrag"); |
550 |
< |
#endif |
551 |
< |
f = &hdfragl[fd]; |
545 |
> |
if (bi->nrd <= 0) |
546 |
> |
return(0); |
547 |
> |
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
548 |
> |
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
549 |
> |
f = &hdfragl[hp->fd]; |
550 |
> |
if (!f->writable) |
551 |
> |
return(0); |
552 |
|
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
553 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
554 |
|
while (f->fi[k].nrd == 0) |
555 |
|
if (++k >= f->nfrags) |
556 |
|
goto endloop; |
557 |
|
if (k > j) |
558 |
< |
copystruct(f->fi+j, f->fi+k); |
558 |
> |
*(f->fi+j) = *(f->fi+k); |
559 |
|
} |
560 |
|
endloop: |
561 |
|
f->nfrags = j; |
562 |
|
} |
563 |
|
j = f->nfrags++; /* allocate a slot in free list */ |
564 |
< |
#if MAXFRAG |
565 |
< |
if (j >= MAXFRAG-1) |
566 |
< |
f->nfrags--; |
564 |
> |
#if MAXFRAGB |
565 |
> |
if (j >= MAXFRAGB*FRAGBLK) { |
566 |
> |
f->nfrags = j--; /* stop list growth */ |
567 |
> |
if (bi->nrd <= f->fi[j].nrd) |
568 |
> |
return(0); /* new one no better than discard */ |
569 |
> |
} |
570 |
|
#endif |
571 |
< |
if (j % FRAGBLK == 0) { /* more free list space */ |
571 |
> |
if (j % FRAGBLK == 0) { /* more (or less) free list space */ |
572 |
|
register BEAMI *newp; |
573 |
|
if (f->fi == NULL) |
574 |
|
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
575 |
|
else |
576 |
< |
newp = (BEAMI *)realloc((char *)f->fi, |
576 |
> |
newp = (BEAMI *)realloc((void *)f->fi, |
577 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
578 |
|
if (newp == NULL) { |
579 |
|
f->nfrags--; /* graceful failure */ |
580 |
< |
return; |
580 |
> |
return(0); |
581 |
|
} |
582 |
|
f->fi = newp; |
583 |
|
} |
587 |
|
f->fi[j].nrd = bi->nrd; |
588 |
|
break; |
589 |
|
} |
590 |
< |
copystruct(f->fi+j, f->fi+(j-1)); |
590 |
> |
*(f->fi+j) = *(f->fi+(j-1)); |
591 |
|
} |
592 |
|
/* coalesce adjacent fragments */ |
593 |
|
/* successors never empty */ |
604 |
|
} |
605 |
|
break; |
606 |
|
} |
607 |
+ |
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
608 |
+ |
bi->nrd = 0; |
609 |
+ |
bi->fo = 0; |
610 |
+ |
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
611 |
+ |
return(1); |
612 |
|
} |
613 |
|
|
614 |
|
|
615 |
< |
long |
615 |
> |
int |
616 |
> |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
617 |
> |
int fd; |
618 |
> |
int *listlen; |
619 |
> |
register int32 *listsiz; |
620 |
> |
{ |
621 |
> |
register struct fraglist *f; |
622 |
> |
register int i; |
623 |
> |
|
624 |
> |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
625 |
> |
return(0); /* listless */ |
626 |
> |
if (listlen != NULL) |
627 |
> |
*listlen = f->nfrags; |
628 |
> |
if (listsiz != NULL) |
629 |
> |
for (i = f->nfrags, *listsiz = 0; i--; ) |
630 |
> |
*listsiz += f->fi[i].nrd; |
631 |
> |
#if MAXFRAGB |
632 |
> |
return(f->nfrags < MAXFRAGB*FRAGBLK); |
633 |
> |
#else |
634 |
> |
return(1); /* list never fills up */ |
635 |
> |
#endif |
636 |
> |
} |
637 |
> |
|
638 |
> |
|
639 |
> |
off_t |
640 |
|
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
641 |
|
int fd; |
642 |
< |
unsigned int4 nrays; |
642 |
> |
uint32 nrays; |
643 |
|
{ |
644 |
|
register struct fraglist *f; |
645 |
< |
register int j, k; |
646 |
< |
long nfo; |
645 |
> |
register int j; |
646 |
> |
off_t nfo; |
647 |
|
|
648 |
|
if (nrays == 0) |
649 |
|
return(-1L); |
650 |
< |
#ifdef DEBUG |
651 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
560 |
< |
error(CONSISTENCY, "bad file descriptor in hdallocfrag"); |
561 |
< |
#endif |
650 |
> |
DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, |
651 |
> |
CONSISTENCY, "bad file descriptor in hdallocfrag"); |
652 |
|
f = &hdfragl[fd]; |
653 |
< |
k = -1; /* find closest-sized fragment */ |
654 |
< |
for (j = f->nfrags; j-- > 0; ) |
655 |
< |
if (f->fi[j].nrd >= nrays && |
656 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
567 |
< |
if (f->fi[k=j].nrd == nrays) |
568 |
< |
break; |
569 |
< |
if (k < 0) { /* no fragment -- extend file */ |
653 |
> |
for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ |
654 |
> |
if (f->fi[j].nrd >= nrays) |
655 |
> |
break; |
656 |
> |
if (j < 0) { /* no fragment -- extend file */ |
657 |
|
nfo = f->flen; |
658 |
|
f->flen += nrays*sizeof(RAYVAL); |
659 |
|
} else { /* else use fragment */ |
660 |
< |
nfo = f->fi[k].fo; |
661 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
662 |
< |
f->fi[k].nrd -= nrays; |
660 |
> |
nfo = f->fi[j].fo; |
661 |
> |
f->fi[j].fo += nrays*sizeof(RAYVAL); |
662 |
> |
f->fi[j].nrd -= nrays; |
663 |
|
} |
664 |
|
return(nfo); |
665 |
|
} |
670 |
|
register HOLO *hp; |
671 |
|
register int i; |
672 |
|
{ |
673 |
< |
unsigned int4 nrays; |
673 |
> |
int fragfreed; |
674 |
> |
uint32 nrays; |
675 |
|
unsigned int n; |
676 |
< |
long nfo; |
676 |
> |
off_t nfo; |
677 |
|
/* check file status */ |
678 |
< |
if (hdfragl[hp->fd].writerr) |
679 |
< |
return(-1); |
680 |
< |
#ifdef DEBUG |
681 |
< |
if (i < 1 | i > nbeams(hp)) |
594 |
< |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
595 |
< |
#endif |
678 |
> |
if (hdfragl[hp->fd].writable <= 0) |
679 |
> |
return(hdfragl[hp->fd].writable); |
680 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
681 |
> |
CONSISTENCY, "bad beam index in hdsyncbeam"); |
682 |
|
/* is current fragment OK? */ |
683 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
684 |
|
return(0); |
685 |
< |
if (hp->bi[i].nrd) /* relinquish old fragment */ |
686 |
< |
hdfreefrag(hp->fd, &hp->bi[i]); |
685 |
> |
/* relinquish old fragment? */ |
686 |
> |
fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); |
687 |
|
if (nrays) { /* get and write new fragment */ |
688 |
|
nfo = hdallocfrag(hp->fd, nrays); |
689 |
|
errno = 0; |
691 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
692 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
693 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
694 |
< |
hdfragl[hp->fd].writerr++; |
694 |
> |
hdfragl[hp->fd].writable = -1; |
695 |
|
hdsync(NULL, 0); /* sync directories */ |
696 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
697 |
|
} |
698 |
|
hp->bi[i].fo = nfo; |
699 |
|
} else |
700 |
< |
hp->bi[i].fo = 0L; |
700 |
> |
hp->bi[i].fo = 0; |
701 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
702 |
|
hp->bi[i].nrd = nrays; |
703 |
< |
markdirty(hp, i); /* section directory now out of date */ |
703 |
> |
if (!fragfreed) |
704 |
> |
hdmarkdirty(hp, i); /* need to flag dir. ent. */ |
705 |
|
return(1); |
706 |
|
} |
707 |
|
|
719 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
720 |
|
return(nchanged); |
721 |
|
} |
722 |
< |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
722 |
> |
if (hdfragl[hp->fd].writable < 0) /* check for file error */ |
723 |
|
return(0); |
724 |
|
if (i == 0) { /* clear entire holodeck */ |
725 |
+ |
if (blglob(hp)->nrm == 0) |
726 |
+ |
return(0); /* already clear */ |
727 |
|
nchanged = 0; |
728 |
|
for (i = nbeams(hp); i > 0; i--) |
729 |
|
if (hp->bl[i] != NULL) |
730 |
|
nchanged += hdfreebeam(hp, i); |
731 |
+ |
DCHECK(blglob(hp)->nrm != 0, |
732 |
+ |
CONSISTENCY, "bad beam count in hdfreebeam"); |
733 |
|
return(nchanged); |
734 |
|
} |
735 |
< |
#ifdef DEBUG |
736 |
< |
if (i < 1 | i > nbeams(hp)) |
646 |
< |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
647 |
< |
#endif |
735 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
736 |
> |
CONSISTENCY, "bad beam index to hdfreebeam"); |
737 |
|
if (hp->bl[i] == NULL) |
738 |
|
return(0); |
739 |
|
/* check for additions */ |
741 |
|
if (nchanged) |
742 |
|
hdsyncbeam(hp, i); /* write new fragment */ |
743 |
|
blglob(hp)->nrm -= hp->bl[i]->nrm; |
744 |
< |
free((char *)hp->bl[i]); /* free memory */ |
744 |
> |
free((void *)hp->bl[i]); /* free memory */ |
745 |
|
hp->bl[i] = NULL; |
746 |
|
return(nchanged); |
747 |
|
} |
752 |
|
register HOLO *hp; |
753 |
|
register int i; |
754 |
|
{ |
666 |
– |
static BEAM emptybeam; |
755 |
|
int nchanged; |
756 |
|
|
757 |
|
if (hp == NULL) { /* clobber all holodecks */ |
761 |
|
return(nchanged); |
762 |
|
} |
763 |
|
if (i == 0) { /* clobber entire holodeck */ |
764 |
+ |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
765 |
+ |
return(0); /* already empty */ |
766 |
|
nchanged = 0; |
767 |
+ |
nchanged = 0; |
768 |
|
for (i = nbeams(hp); i > 0; i--) |
769 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
770 |
|
nchanged += hdkillbeam(hp, i); |
771 |
< |
#ifdef DEBUG |
772 |
< |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
682 |
< |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
683 |
< |
#endif |
771 |
> |
DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, |
772 |
> |
CONSISTENCY, "bad beam count in hdkillbeam"); |
773 |
|
return(nchanged); |
774 |
|
} |
775 |
< |
#ifdef DEBUG |
776 |
< |
if (i < 1 | i > nbeams(hp)) |
777 |
< |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
778 |
< |
#endif |
775 |
> |
DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, |
776 |
> |
"bad beam index to hdkillbeam"); |
777 |
> |
DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, |
778 |
> |
"hdkillbeam called on read-only holodeck"); |
779 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
780 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
781 |
< |
free((char *)hp->bl[i]); |
781 |
> |
free((void *)hp->bl[i]); |
782 |
> |
hp->bl[i] = NULL; |
783 |
|
} else |
784 |
|
nchanged = hp->bi[i].nrd; |
785 |
< |
if (hp->bi[i].nrd) { /* free file fragment */ |
786 |
< |
hp->bl[i] = &emptybeam; |
787 |
< |
hdsyncbeam(hp, i); |
785 |
> |
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
786 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
787 |
> |
hp->bi[i].nrd = 0; |
788 |
> |
hp->bi[i].fo = 0; |
789 |
> |
hdmarkdirty(hp, i); |
790 |
|
} |
699 |
– |
hp->bl[i] = NULL; |
791 |
|
return(nchanged); |
792 |
|
} |
793 |
|
|
817 |
|
hb[j].b = i; |
818 |
|
break; |
819 |
|
} |
820 |
< |
copystruct(hb+j, hb+(j-1)); |
820 |
> |
*(hb+j) = *(hb+(j-1)); |
821 |
|
} |
822 |
|
} |
823 |
|
return(nents); /* return new list length */ |
876 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
877 |
|
while (hdlist[0] != NULL) |
878 |
|
hddone(hdlist[0]); |
879 |
< |
free((char *)hdfragl); |
879 |
> |
free((void *)hdfragl); |
880 |
|
hdfragl = NULL; nhdfragls = 0; |
881 |
|
return; |
882 |
|
} |
883 |
|
/* flush all data and free memory */ |
884 |
< |
hdfreebeam(hp, 0); |
794 |
< |
hdsync(hp, 0); |
884 |
> |
hdflush(hp); |
885 |
|
/* release fragment resources */ |
886 |
|
hdrelease(hp->fd); |
887 |
|
/* remove hp from active list */ |
891 |
|
i++; |
892 |
|
break; |
893 |
|
} |
894 |
< |
free((char *)hp->bl); /* free beam list */ |
895 |
< |
free((char *)hp); /* free holodeck struct */ |
894 |
> |
free((void *)hp->bl); /* free beam list */ |
895 |
> |
free((void *)hp); /* free holodeck struct */ |
896 |
|
} |