| 7 |
|
* 9/30/97 GWLarson |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
#include "copyright.h" |
| 11 |
+ |
|
| 12 |
+ |
#include <string.h> |
| 13 |
+ |
|
| 14 |
|
#include "holo.h" |
| 15 |
|
|
| 16 |
+ |
#include "platform.h" |
| 17 |
+ |
|
| 18 |
|
#ifndef CACHESIZE |
| 19 |
< |
#ifdef BIGMEM |
| 14 |
< |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
| 15 |
< |
#else |
| 19 |
> |
#ifdef SMLMEM |
| 20 |
|
#define CACHESIZE 5 |
| 21 |
+ |
#else |
| 22 |
+ |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
| 23 |
|
#endif |
| 24 |
|
#endif |
| 25 |
|
#ifndef FREEBEAMS |
| 58 |
|
short writable; /* 0 read-only, <0 write error encountered */ |
| 59 |
|
int nfrags; /* number of known fragments */ |
| 60 |
|
BEAMI *fi; /* fragments, descending file position */ |
| 61 |
< |
long flen; /* last known file length */ |
| 61 |
> |
off_t flen; /* last known file length */ |
| 62 |
|
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
| 63 |
|
|
| 64 |
|
static int nhdfragls; /* size of hdfragl array */ |
| 72 |
|
register HOLO *hp; |
| 73 |
|
int n; |
| 74 |
|
/* copy grid to temporary header */ |
| 75 |
< |
bcopy((void *)hproto, (void *)&hdhead, sizeof(HDGRID)); |
| 75 |
> |
memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID)); |
| 76 |
|
/* compute grid vectors and sizes */ |
| 77 |
|
hdcompgrid(&hdhead); |
| 78 |
|
/* allocate header with directory */ |
| 80 |
|
if ((hp = (HOLO *)malloc(n)) == NULL) |
| 81 |
|
return(NULL); |
| 82 |
|
/* copy header information */ |
| 83 |
< |
copystruct(hp, &hdhead); |
| 83 |
> |
*hp = hdhead; |
| 84 |
|
/* allocate and clear beam list */ |
| 85 |
|
hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 86 |
|
if (hp->bl == NULL) { |
| 87 |
|
free((void *)hp); |
| 88 |
|
return(NULL); |
| 89 |
|
} |
| 90 |
< |
bzero((void *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 90 |
> |
memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 91 |
|
hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1); /* set blglob(hp) */ |
| 92 |
|
hp->fd = -1; |
| 93 |
|
hp->dirty = 0; |
| 94 |
|
hp->priv = NULL; |
| 95 |
|
/* clear beam directory */ |
| 96 |
< |
bzero((void *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
| 96 |
> |
memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI)); |
| 97 |
|
return(hp); /* all is well */ |
| 98 |
|
} |
| 99 |
|
|
| 129 |
|
if (fd >= nhdfragls) { |
| 130 |
|
hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, |
| 131 |
|
(fd+1)*sizeof(struct fraglist), "hdattach"); |
| 132 |
< |
bzero((void *)(hdfragl+nhdfragls), |
| 133 |
< |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
| 132 |
> |
memset((void *)(hdfragl+nhdfragls), |
| 133 |
> |
'\0', (fd+1-nhdfragls)*sizeof(struct fraglist)); |
| 134 |
|
nhdfragls = fd+1; |
| 135 |
|
} |
| 136 |
|
hdfragl[fd].nlinks++; |
| 137 |
|
hdfragl[fd].writable = wr; /* set writable flag */ |
| 138 |
< |
hdfragl[fd].flen = lseek(fd, (off_t)0L, 2); /* get file length */ |
| 138 |
> |
/* get file length */ |
| 139 |
> |
hdfragl[fd].flen = lseek(fd, (off_t)0, SEEK_END); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 146 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
| 147 |
|
register int fd; |
| 148 |
|
{ |
| 149 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
| 149 |
> |
if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks) |
| 150 |
|
return; |
| 151 |
|
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
| 152 |
|
free((void *)hdfragl[fd].fi); |
| 161 |
|
int fd; /* corresponding file descriptor */ |
| 162 |
|
HDGRID *hproto; /* holodeck section grid */ |
| 163 |
|
{ |
| 164 |
< |
long rtrunc; |
| 165 |
< |
long fpos; |
| 164 |
> |
off_t rtrunc; |
| 165 |
> |
off_t fpos; |
| 166 |
|
int writable; |
| 167 |
|
register HOLO *hp; |
| 168 |
|
register int n; |
| 169 |
|
/* prepare for system errors */ |
| 170 |
|
errno = 0; |
| 171 |
< |
if ((fpos = lseek(fd, (off_t)0L, 1)) < 0) |
| 171 |
> |
if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0) |
| 172 |
|
error(SYSTEM, "cannot determine holodeck file position"); |
| 173 |
|
if (hproto == NULL) { /* assume we're loading it */ |
| 174 |
|
HDGRID hpr; |
| 193 |
|
if (fd < nhdfragls && hdfragl[fd].nlinks) |
| 194 |
|
writable = hdfragl[fd].writable; |
| 195 |
|
else |
| 196 |
< |
writable = lseek(fd, (off_t)fpos, 0) == fpos && |
| 196 |
> |
writable = lseek(fd, fpos, SEEK_SET) == fpos && |
| 197 |
|
write(fd, (char *)hp, sizeof(HDGRID)) == |
| 198 |
|
sizeof(HDGRID); |
| 199 |
|
} else { /* else assume we're creating it */ |
| 216 |
|
fpos = hdfilen(fd); |
| 217 |
|
biglob(hp)->nrd = rtrunc = 0; |
| 218 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
| 219 |
< |
if (hp->bi[n].nrd) |
| 219 |
> |
if (hp->bi[n].nrd) { |
| 220 |
|
if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { |
| 221 |
|
rtrunc += hp->bi[n].nrd; |
| 222 |
|
hp->bi[n].nrd = 0; |
| 223 |
|
} else |
| 224 |
|
biglob(hp)->nrd += hp->bi[n].nrd; |
| 225 |
+ |
} |
| 226 |
|
if (rtrunc) { |
| 227 |
|
sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", |
| 228 |
|
rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); |
| 250 |
|
register int j; |
| 251 |
|
|
| 252 |
|
if (!hp->dirty++) { /* write smudge first time */ |
| 253 |
< |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo+(i-1)*sizeof(BEAMI)), 0) < 0 |
| 254 |
< |
|| write(hp->fd, (char *)&smudge, |
| 253 |
> |
if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), |
| 254 |
> |
SEEK_SET) < 0 || |
| 255 |
> |
write(hp->fd, (char *)&smudge, |
| 256 |
|
sizeof(BEAMI)) != sizeof(BEAMI)) |
| 257 |
|
error(SYSTEM, "seek/write error in hdmarkdirty"); |
| 258 |
|
hp->dirseg[0].s = i; |
| 266 |
|
hp->dirseg[j].n = 1; |
| 267 |
|
break; |
| 268 |
|
} |
| 269 |
< |
copystruct(hp->dirseg+j, hp->dirseg+(j-1)); |
| 269 |
> |
*(hp->dirseg+j) = *(hp->dirseg+(j-1)); |
| 270 |
|
} |
| 271 |
|
do { /* check neighbors */ |
| 272 |
|
mindist = nbeams(hp); /* find closest */ |
| 288 |
|
hp->dirseg[minpos].n - hp->dirseg[j].s; |
| 289 |
|
hp->dirty--; |
| 290 |
|
while (++j < hp->dirty) /* close the gap */ |
| 291 |
< |
copystruct(hp->dirseg+j, hp->dirseg+(j+1)); |
| 291 |
> |
*(hp->dirseg+j) = *(hp->dirseg+(j+1)); |
| 292 |
|
} while (mindist <= MINDIRSEL); |
| 293 |
|
} |
| 294 |
|
|
| 314 |
|
return(0); |
| 315 |
|
errno = 0; /* write dirty segments */ |
| 316 |
|
for (j = 0; j < hp->dirty; j++) { |
| 317 |
< |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo + |
| 318 |
< |
(hp->dirseg[j].s-1)*sizeof(BEAMI)), 0) < 0) |
| 317 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
| 318 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0) |
| 319 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
| 320 |
|
n = hp->dirseg[j].n * sizeof(BEAMI); |
| 321 |
|
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
| 355 |
|
} |
| 356 |
|
|
| 357 |
|
|
| 358 |
< |
long |
| 358 |
> |
off_t |
| 359 |
|
hdfilen(fd) /* return file length for fd */ |
| 360 |
|
int fd; |
| 361 |
|
{ |
| 362 |
< |
long fpos, flen; |
| 362 |
> |
off_t fpos, flen; |
| 363 |
|
|
| 364 |
|
if (fd < 0) |
| 365 |
|
return(-1); |
| 366 |
|
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
| 367 |
< |
if ((fpos = lseek(fd, (off_t)0L, 1)) < 0) |
| 367 |
> |
if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0) |
| 368 |
|
return(-1); |
| 369 |
< |
flen = lseek(fd, (off_t)0L, 2); |
| 370 |
< |
lseek(fd, (off_t)fpos, 0); |
| 369 |
> |
flen = lseek(fd, (off_t)0, SEEK_END); |
| 370 |
> |
lseek(fd, fpos, SEEK_SET); |
| 371 |
|
return(flen); |
| 372 |
|
} |
| 373 |
|
return(hdfragl[fd].flen); |
| 374 |
|
} |
| 375 |
|
|
| 376 |
|
|
| 377 |
< |
long |
| 377 |
> |
off_t |
| 378 |
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
| 379 |
|
int fd; /* open file descriptor to check */ |
| 380 |
|
int all; /* include overhead and unflushed data */ |
| 381 |
|
{ |
| 382 |
< |
long total = 0; |
| 382 |
> |
off_t total = 0; |
| 383 |
|
register int i, j; |
| 384 |
|
|
| 385 |
|
for (j = 0; hdlist[j] != NULL; j++) { |
| 410 |
|
int n; |
| 411 |
|
|
| 412 |
|
if (nr <= 0) return(NULL); |
| 413 |
< |
CHECK(i < 1 | i > nbeams(hp), |
| 413 |
> |
CHECK((i < 1) | (i > nbeams(hp)), |
| 414 |
|
CONSISTENCY, "bad beam index given to hdnewrays"); |
| 415 |
|
if (hp->bl[i] != NULL) |
| 416 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
| 422 |
|
blglob(hp)->nrm += n; |
| 423 |
|
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
| 424 |
|
errno = 0; |
| 425 |
< |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
| 425 |
> |
if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) |
| 426 |
|
error(SYSTEM, "seek error on holodeck file"); |
| 427 |
|
n *= sizeof(RAYVAL); |
| 428 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
| 438 |
|
hdfreefrag(hp, i); /* relinquish old fragment */ |
| 439 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
| 440 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
| 441 |
< |
bzero((void *)p, nr*sizeof(RAYVAL)); |
| 441 |
> |
memset((void *)p, '\0', nr*sizeof(RAYVAL)); |
| 442 |
|
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
| 443 |
|
return(p); /* point to new rays */ |
| 444 |
|
} |
| 451 |
|
{ |
| 452 |
|
register int n; |
| 453 |
|
|
| 454 |
< |
CHECK(i < 1 | i > nbeams(hp), |
| 454 |
> |
CHECK((i < 1) | (i > nbeams(hp)), |
| 455 |
|
CONSISTENCY, "bad beam index given to hdgetbeam"); |
| 456 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
| 457 |
|
if (!(n = hp->bi[i].nrd)) |
| 461 |
|
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
| 462 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
| 463 |
|
errno = 0; |
| 464 |
< |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
| 464 |
> |
if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) |
| 465 |
|
error(SYSTEM, "seek error on holodeck file"); |
| 466 |
|
n *= sizeof(RAYVAL); |
| 467 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
| 478 |
|
hdfilord(hb1, hb2) /* order beams for quick loading */ |
| 479 |
|
register HDBEAMI *hb1, *hb2; |
| 480 |
|
{ |
| 481 |
< |
register long c; |
| 481 |
> |
register off_t c; |
| 482 |
|
/* residents go first */ |
| 483 |
|
if (hb2->h->bl[hb2->b] != NULL) |
| 484 |
|
return(hb1->h->bl[hb1->b] == NULL); |
| 485 |
|
if (hb1->h->bl[hb1->b] != NULL) |
| 486 |
|
return(-1); |
| 487 |
|
/* otherwise sort by file descriptor */ |
| 488 |
< |
if ((c = hb1->h->fd - hb2->h->fd)) |
| 489 |
< |
return(c); |
| 488 |
> |
if (hb1->h->fd != hb2->h->fd) |
| 489 |
> |
return(hb1->h->fd - hb2->h->fd); |
| 490 |
|
/* then by position in file */ |
| 491 |
|
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
| 492 |
|
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
| 496 |
|
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
| 497 |
|
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
| 498 |
|
int n; /* list length */ |
| 499 |
< |
int (*bf)(); /* callback function (optional) */ |
| 499 |
> |
void (*bf)(); /* callback function (optional) */ |
| 500 |
|
{ |
| 501 |
|
unsigned origcachesize, memuse; |
| 502 |
|
int bytesloaded, needbytes, bytes2free; |
| 505 |
|
/* precheck consistency */ |
| 506 |
|
if (n <= 0) return; |
| 507 |
|
for (i = n; i--; ) |
| 508 |
< |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
| 508 |
> |
if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h))) |
| 509 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
| 510 |
|
/* sort list for optimal access */ |
| 511 |
|
qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord); |
| 560 |
|
if (++k >= f->nfrags) |
| 561 |
|
goto endloop; |
| 562 |
|
if (k > j) |
| 563 |
< |
copystruct(f->fi+j, f->fi+k); |
| 563 |
> |
*(f->fi+j) = *(f->fi+k); |
| 564 |
|
} |
| 565 |
|
endloop: |
| 566 |
|
f->nfrags = j; |
| 592 |
|
f->fi[j].nrd = bi->nrd; |
| 593 |
|
break; |
| 594 |
|
} |
| 595 |
< |
copystruct(f->fi+j, f->fi+(j-1)); |
| 595 |
> |
*(f->fi+j) = *(f->fi+(j-1)); |
| 596 |
|
} |
| 597 |
|
/* coalesce adjacent fragments */ |
| 598 |
|
/* successors never empty */ |
| 611 |
|
} |
| 612 |
|
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
| 613 |
|
bi->nrd = 0; |
| 614 |
< |
bi->fo = 0L; |
| 614 |
> |
bi->fo = 0; |
| 615 |
|
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
| 616 |
|
return(1); |
| 617 |
|
} |
| 621 |
|
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
| 622 |
|
int fd; |
| 623 |
|
int *listlen; |
| 624 |
< |
register int4 *listsiz; |
| 624 |
> |
register int32 *listsiz; |
| 625 |
|
{ |
| 626 |
|
register struct fraglist *f; |
| 627 |
|
register int i; |
| 628 |
|
|
| 629 |
< |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
| 629 |
> |
if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks) |
| 630 |
|
return(0); /* listless */ |
| 631 |
|
if (listlen != NULL) |
| 632 |
|
*listlen = f->nfrags; |
| 641 |
|
} |
| 642 |
|
|
| 643 |
|
|
| 644 |
< |
long |
| 644 |
> |
off_t |
| 645 |
|
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
| 646 |
|
int fd; |
| 647 |
< |
unsigned int4 nrays; |
| 647 |
> |
uint32 nrays; |
| 648 |
|
{ |
| 649 |
|
register struct fraglist *f; |
| 650 |
|
register int j; |
| 651 |
< |
long nfo; |
| 651 |
> |
off_t nfo; |
| 652 |
|
|
| 653 |
|
if (nrays == 0) |
| 654 |
|
return(-1L); |
| 676 |
|
register int i; |
| 677 |
|
{ |
| 678 |
|
int fragfreed; |
| 679 |
< |
unsigned int4 nrays; |
| 679 |
> |
uint32 nrays; |
| 680 |
|
unsigned int n; |
| 681 |
< |
long nfo; |
| 681 |
> |
off_t nfo; |
| 682 |
|
/* check file status */ |
| 683 |
|
if (hdfragl[hp->fd].writable <= 0) |
| 684 |
|
return(hdfragl[hp->fd].writable); |
| 692 |
|
if (nrays) { /* get and write new fragment */ |
| 693 |
|
nfo = hdallocfrag(hp->fd, nrays); |
| 694 |
|
errno = 0; |
| 695 |
< |
if (lseek(hp->fd, (off_t)nfo, 0) < 0) |
| 695 |
> |
if (lseek(hp->fd, nfo, SEEK_SET) < 0) |
| 696 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
| 697 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
| 698 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
| 702 |
|
} |
| 703 |
|
hp->bi[i].fo = nfo; |
| 704 |
|
} else |
| 705 |
< |
hp->bi[i].fo = 0L; |
| 705 |
> |
hp->bi[i].fo = 0; |
| 706 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
| 707 |
|
hp->bi[i].nrd = nrays; |
| 708 |
|
if (!fragfreed) |
| 766 |
|
return(nchanged); |
| 767 |
|
} |
| 768 |
|
if (i == 0) { /* clobber entire holodeck */ |
| 769 |
< |
if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0) |
| 769 |
> |
if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0)) |
| 770 |
|
return(0); /* already empty */ |
| 771 |
|
nchanged = 0; |
| 772 |
|
nchanged = 0; |
| 790 |
|
if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { |
| 791 |
|
biglob(hp)->nrd -= hp->bi[i].nrd; /* free failed */ |
| 792 |
|
hp->bi[i].nrd = 0; |
| 793 |
< |
hp->bi[i].fo = 0L; |
| 793 |
> |
hp->bi[i].fo = 0; |
| 794 |
|
hdmarkdirty(hp, i); |
| 795 |
|
} |
| 796 |
|
return(nchanged); |
| 822 |
|
hb[j].b = i; |
| 823 |
|
break; |
| 824 |
|
} |
| 825 |
< |
copystruct(hb+j, hb+(j-1)); |
| 825 |
> |
*(hb+j) = *(hb+(j-1)); |
| 826 |
|
} |
| 827 |
|
} |
| 828 |
|
return(nents); /* return new list length */ |