| 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 |
< |
#ifdef BIGMEM |
| 14 |
< |
#define CACHESIZE 17 /* default cache size (Mbytes, 0==inf) */ |
| 15 |
< |
#else |
| 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 |
| 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 */ |
| 70 |
|
register HOLO *hp; |
| 71 |
|
int n; |
| 72 |
|
/* copy grid to temporary header */ |
| 73 |
< |
bcopy((char *)hproto, (char *)&hdhead, sizeof(HDGRID)); |
| 73 |
> |
memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID)); |
| 74 |
|
/* compute grid vectors and sizes */ |
| 75 |
|
hdcompgrid(&hdhead); |
| 76 |
|
/* allocate header with directory */ |
| 78 |
|
if ((hp = (HOLO *)malloc(n)) == NULL) |
| 79 |
|
return(NULL); |
| 80 |
|
/* copy header information */ |
| 81 |
< |
copystruct(hp, &hdhead); |
| 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 |
< |
bzero((char *)hp->bl, (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM)); |
| 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 |
< |
bzero((char *)hp->bi, (nbeams(hp)+1)*sizeof(BEAMI)); |
| 94 |
> |
memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI)); |
| 95 |
|
return(hp); /* all is well */ |
| 96 |
|
} |
| 97 |
|
|
| 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].writable = wr; /* set writable flag */ |
| 136 |
< |
hdfragl[fd].flen = lseek(fd, (off_t)0L, 2); /* get file length */ |
| 136 |
> |
hdfragl[fd].flen = lseek(fd, (off_t)0, 2); /* get file length */ |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 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, (off_t)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; |
| 190 |
|
if (fd < nhdfragls && hdfragl[fd].nlinks) |
| 191 |
|
writable = hdfragl[fd].writable; |
| 192 |
|
else |
| 193 |
< |
writable = lseek(fd, (off_t)fpos, 0) == fpos && |
| 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 */ |
| 246 |
|
register int j; |
| 247 |
|
|
| 248 |
|
if (!hp->dirty++) { /* write smudge first time */ |
| 249 |
< |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo+(i-1)*sizeof(BEAMI)), 0) < 0 |
| 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 hdmarkdirty"); |
| 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 |
|
|
| 309 |
|
return(0); |
| 310 |
|
errno = 0; /* write dirty segments */ |
| 311 |
|
for (j = 0; j < hp->dirty; j++) { |
| 312 |
< |
if (lseek(hp->fd, (off_t)(biglob(hp)->fo + |
| 313 |
< |
(hp->dirseg[j].s-1)*sizeof(BEAMI)), 0) < 0) |
| 312 |
> |
if (lseek(hp->fd, biglob(hp)->fo + |
| 313 |
> |
(hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) |
| 314 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
| 315 |
|
n = hp->dirseg[j].n * sizeof(BEAMI); |
| 316 |
|
if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) |
| 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, (off_t)0L, 1)) < 0) |
| 362 |
> |
if ((fpos = lseek(fd, (off_t)0, 1)) < 0) |
| 363 |
|
return(-1); |
| 364 |
< |
flen = lseek(fd, (off_t)0L, 2); |
| 365 |
< |
lseek(fd, (off_t)fpos, 0); |
| 364 |
> |
flen = lseek(fd, (off_t)0, 2); |
| 365 |
> |
lseek(fd, fpos, 0); |
| 366 |
|
return(flen); |
| 367 |
|
} |
| 368 |
|
return(hdfragl[fd].flen); |
| 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++) { |
| 417 |
|
blglob(hp)->nrm += n; |
| 418 |
|
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
| 419 |
|
errno = 0; |
| 420 |
< |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
| 420 |
> |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
| 421 |
|
error(SYSTEM, "seek error on holodeck file"); |
| 422 |
|
n *= sizeof(RAYVAL); |
| 423 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
| 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 |
|
} |
| 456 |
|
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); |
| 457 |
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
| 458 |
|
errno = 0; |
| 459 |
< |
if (lseek(hp->fd, (off_t)hp->bi[i].fo, 0) < 0) |
| 459 |
> |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
| 460 |
|
error(SYSTEM, "seek error on holodeck file"); |
| 461 |
|
n *= sizeof(RAYVAL); |
| 462 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
| 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; |
| 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 */ |
| 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; |
| 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 */ |
| 606 |
|
} |
| 607 |
|
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
| 608 |
|
bi->nrd = 0; |
| 609 |
< |
bi->fo = 0L; |
| 609 |
> |
bi->fo = 0; |
| 610 |
|
hdmarkdirty(hp, i); /* assume we'll reallocate */ |
| 611 |
|
return(1); |
| 612 |
|
} |
| 616 |
|
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
| 617 |
|
int fd; |
| 618 |
|
int *listlen; |
| 619 |
< |
register int4 *listsiz; |
| 619 |
> |
register int32 *listsiz; |
| 620 |
|
{ |
| 621 |
|
register struct fraglist *f; |
| 622 |
|
register int i; |
| 636 |
|
} |
| 637 |
|
|
| 638 |
|
|
| 639 |
< |
long |
| 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; |
| 646 |
< |
long nfo; |
| 646 |
> |
off_t nfo; |
| 647 |
|
|
| 648 |
|
if (nrays == 0) |
| 649 |
|
return(-1L); |
| 671 |
|
register int i; |
| 672 |
|
{ |
| 673 |
|
int fragfreed; |
| 674 |
< |
unsigned int4 nrays; |
| 674 |
> |
uint32 nrays; |
| 675 |
|
unsigned int n; |
| 676 |
< |
long nfo; |
| 676 |
> |
off_t nfo; |
| 677 |
|
/* check file status */ |
| 678 |
|
if (hdfragl[hp->fd].writable <= 0) |
| 679 |
|
return(hdfragl[hp->fd].writable); |
| 687 |
|
if (nrays) { /* get and write new fragment */ |
| 688 |
|
nfo = hdallocfrag(hp->fd, nrays); |
| 689 |
|
errno = 0; |
| 690 |
< |
if (lseek(hp->fd, (off_t)nfo, 0) < 0) |
| 690 |
> |
if (lseek(hp->fd, nfo, 0) < 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) { |
| 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 |
|
if (!fragfreed) |
| 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 = 0L; |
| 788 |
> |
hp->bi[i].fo = 0; |
| 789 |
|
hdmarkdirty(hp, i); |
| 790 |
|
} |
| 791 |
|
return(nchanged); |
| 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 */ |