| 21 |  | #ifndef PCTFREE | 
| 22 |  | #define PCTFREE         20      /* maximum fraction to free (%) */ | 
| 23 |  | #endif | 
| 24 | + | #ifndef MAXFRAG | 
| 25 | + | #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */ | 
| 26 | + | #endif | 
| 27 |  |  | 
| 25 | – | /* define MAXFRAG if you want to limit fragment tracking memory */ | 
| 26 | – |  | 
| 28 |  | #ifndef BSD | 
| 29 |  | #define write   writebuf        /* safe i/o routines */ | 
| 30 |  | #define read    readbuf | 
| 31 |  | #endif | 
| 32 |  |  | 
| 33 | < | #define FRAGBLK         64      /* number of fragments to allocate at a time */ | 
| 33 | > | #define FRAGBLK         256     /* number of fragments to allocate at a time */ | 
| 34 |  |  | 
| 35 | < | int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */ | 
| 35 | > | unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */ | 
| 36 |  | unsigned long   hdclock;        /* clock value */ | 
| 37 |  |  | 
| 38 |  | HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */ | 
| 39 |  |  | 
| 40 | < | static struct fragment { | 
| 40 | > | static struct fraglist { | 
| 41 |  | short   nlinks;         /* number of holodeck sections using us */ | 
| 42 | < | short   nfrags;         /* number of known fragments */ | 
| 42 | > | short   writerr;        /* write error encountered */ | 
| 43 | > | int     nfrags;         /* number of known fragments */ | 
| 44 |  | BEAMI   *fi;            /* fragments, descending file position */ | 
| 45 |  | long    flen;           /* last known file length */ | 
| 46 | < | } *hdfrag;              /* fragment lists, indexed by file descriptor */ | 
| 46 | > | } *hdfragl;             /* fragment lists, indexed by file descriptor */ | 
| 47 |  |  | 
| 48 | < | static int      nhdfrags;       /* size of hdfrag array */ | 
| 48 | > | static int      nhdfragls;      /* size of hdfragl array */ | 
| 49 |  |  | 
| 50 |  |  | 
| 51 |  | hdattach(fd)            /* start tracking file fragments for some section */ | 
| 52 |  | register int    fd; | 
| 53 |  | { | 
| 54 | < | if (fd >= nhdfrags) { | 
| 55 | < | if (nhdfrags) | 
| 56 | < | hdfrag = (struct fragment *)realloc((char *)hdfrag, | 
| 57 | < | (fd+1)*sizeof(struct fragment)); | 
| 54 | > | if (fd >= nhdfragls) { | 
| 55 | > | if (nhdfragls) | 
| 56 | > | hdfragl = (struct fraglist *)realloc((char *)hdfragl, | 
| 57 | > | (fd+1)*sizeof(struct fraglist)); | 
| 58 |  | else | 
| 59 | < | hdfrag = (struct fragment *)malloc( | 
| 60 | < | (fd+1)*sizeof(struct fragment)); | 
| 61 | < | if (hdfrag == NULL) | 
| 59 | > | hdfragl = (struct fraglist *)malloc( | 
| 60 | > | (fd+1)*sizeof(struct fraglist)); | 
| 61 | > | if (hdfragl == NULL) | 
| 62 |  | error(SYSTEM, "out of memory in hdattach"); | 
| 63 | < | bzero((char *)(hdfrag+nhdfrags), | 
| 64 | < | (fd+1-nhdfrags)*sizeof(struct fragment)); | 
| 65 | < | nhdfrags = fd+1; | 
| 63 | > | bzero((char *)(hdfragl+nhdfragls), | 
| 64 | > | (fd+1-nhdfragls)*sizeof(struct fraglist)); | 
| 65 | > | nhdfragls = fd+1; | 
| 66 |  | } | 
| 67 | < | hdfrag[fd].nlinks++; | 
| 68 | < | hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */ | 
| 67 | > | hdfragl[fd].nlinks++; | 
| 68 | > | hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */ | 
| 69 |  | } | 
| 70 |  |  | 
| 71 |  |  | 
| 75 |  | hdrelease(fd)           /* stop tracking file fragments for some section */ | 
| 76 |  | register int    fd; | 
| 77 |  | { | 
| 78 | < | if (fd >= nhdfrags || !hdfrag[fd].nlinks) | 
| 78 | > | if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) | 
| 79 |  | return; | 
| 80 | < | if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { | 
| 81 | < | free((char *)hdfrag[fd].fi); | 
| 82 | < | hdfrag[fd].fi = NULL; | 
| 83 | < | hdfrag[fd].nfrags = 0; | 
| 80 | > | if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { | 
| 81 | > | free((char *)hdfragl[fd].fi); | 
| 82 | > | hdfragl[fd].fi = NULL; | 
| 83 | > | hdfragl[fd].nfrags = 0; | 
| 84 |  | } | 
| 85 |  | } | 
| 86 |  |  | 
| 140 |  | hp->fd = fd; | 
| 141 |  | hp->dirty = 0; | 
| 142 |  | biglob(hp)->fo = fpos + sizeof(HDGRID); | 
| 143 | < | biglob(hp)->nrd = 0;            /* count rays on disk */ | 
| 144 | < | for (n = nbeams(hp); n > 0; n--) | 
| 145 | < | biglob(hp)->nrd += hp->bi[n].nrd; | 
| 143 | > | /* start tracking fragments */ | 
| 144 | > | hdattach(fd); | 
| 145 | > | /* check rays on disk */ | 
| 146 | > | fpos = hdfilen(fd); | 
| 147 | > | biglob(hp)->nrd = 0; | 
| 148 | > | for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) | 
| 149 | > | if (hp->bi[n].nrd) | 
| 150 | > | if (hp->bi[n].fo + hp->bi[n].nrd > fpos) | 
| 151 | > | hp->bi[n].nrd = 0;      /* off end */ | 
| 152 | > | else | 
| 153 | > | biglob(hp)->nrd += hp->bi[n].nrd; | 
| 154 |  | /* add to holodeck list */ | 
| 155 |  | for (n = 0; n < HDMAX; n++) | 
| 156 |  | if (hdlist[n] == NULL) { | 
| 157 |  | hdlist[n] = hp; | 
| 158 |  | break; | 
| 159 |  | } | 
| 150 | – | /* start tracking fragments (last) */ | 
| 151 | – | hdattach(fd); | 
| 160 |  | /* all done */ | 
| 161 |  | return(hp); | 
| 162 |  | memerr: | 
| 165 |  |  | 
| 166 |  |  | 
| 167 |  | int | 
| 168 | < | hdsync(hp)                      /* update directory on disk if necessary */ | 
| 168 | > | hdsync(hp, all)                 /* update beams and directory on disk */ | 
| 169 |  | register HOLO   *hp; | 
| 170 | + | int     all; | 
| 171 |  | { | 
| 172 |  | register int    j, n; | 
| 173 |  |  | 
| 174 | < | if (hp == NULL) {               /* do all */ | 
| 174 | > | if (hp == NULL) {               /* do all holodecks */ | 
| 175 |  | n = 0; | 
| 176 |  | for (j = 0; hdlist[j] != NULL; j++) | 
| 177 | < | n += hdsync(hdlist[j]); | 
| 177 | > | n += hdsync(hdlist[j], all); | 
| 178 |  | return(n); | 
| 179 |  | } | 
| 180 | < | if (!hp->dirty)                 /* check first */ | 
| 180 | > | /* sync the beams */ | 
| 181 | > | for (j = (all ? nbeams(hp) : 0); j > 0; j--) | 
| 182 | > | if (hp->bl[j] != NULL) | 
| 183 | > | hdsyncbeam(hp, j); | 
| 184 | > | if (!hp->dirty)                 /* directory clean? */ | 
| 185 |  | return(0); | 
| 186 |  | errno = 0; | 
| 187 |  | if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) | 
| 194 |  | } | 
| 195 |  |  | 
| 196 |  |  | 
| 197 | < | long | 
| 197 | > | unsigned | 
| 198 |  | hdmemuse(all)           /* return memory usage (in bytes) */ | 
| 199 |  | int     all;                    /* include overhead (painful) */ | 
| 200 |  | { | 
| 213 |  | } | 
| 214 |  | } | 
| 215 |  | if (all) | 
| 216 | < | for (j = 0; j < nhdfrags; j++) { | 
| 217 | < | total += sizeof(struct fragment); | 
| 218 | < | if (hdfrag[j].nfrags) | 
| 216 | > | for (j = 0; j < nhdfragls; j++) { | 
| 217 | > | total += sizeof(struct fraglist); | 
| 218 | > | if (hdfragl[j].nfrags) | 
| 219 |  | total += FRAGBLK*sizeof(BEAMI) * | 
| 220 | < | ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; | 
| 220 | > | ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; | 
| 221 |  | } | 
| 222 |  | return(total); | 
| 223 |  | } | 
| 224 |  |  | 
| 225 |  |  | 
| 226 |  | long | 
| 227 | + | hdfilen(fd)             /* return file length for fd */ | 
| 228 | + | int     fd; | 
| 229 | + | { | 
| 230 | + | long    fpos, flen; | 
| 231 | + |  | 
| 232 | + | if (fd < 0) | 
| 233 | + | return(-1); | 
| 234 | + | if (fd >= nhdfragls || !hdfragl[fd].nlinks) { | 
| 235 | + | if ((fpos = lseek(fd, 0L, 1)) < 0) | 
| 236 | + | return(-1); | 
| 237 | + | flen = lseek(fd, 0L, 2); | 
| 238 | + | lseek(fd, fpos, 0); | 
| 239 | + | return(flen); | 
| 240 | + | } | 
| 241 | + | return(hdfragl[fd].flen); | 
| 242 | + | } | 
| 243 | + |  | 
| 244 | + |  | 
| 245 | + | long | 
| 246 |  | hdfiluse(fd, all)       /* compute file usage (in bytes) */ | 
| 247 |  | int     fd;                     /* open file descriptor to check */ | 
| 248 |  | int     all;                    /* include overhead and unflushed data */ | 
| 309 |  | p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; | 
| 310 |  | hp->bl[i]->nrm += nr;                   /* update in-core structure */ | 
| 311 |  | bzero((char *)p, nr*sizeof(RAYVAL)); | 
| 312 | < | hp->bl[i]->tick = ++hdclock;            /* update LRU clock */ | 
| 313 | < | blglob(hp)->tick = hdclock; | 
| 312 | > | hp->bl[i]->tick = hdclock;              /* update LRU clock */ | 
| 313 | > | blglob(hp)->tick = hdclock++; | 
| 314 |  | return(p);                              /* point to new rays */ | 
| 315 |  | memerr: | 
| 316 |  | error(SYSTEM, "out of memory in hdnewrays"); | 
| 341 |  | if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 342 |  | error(SYSTEM, "error reading beam from holodeck file"); | 
| 343 |  | } | 
| 344 | < | hp->bl[i]->tick = ++hdclock;    /* update LRU clock */ | 
| 345 | < | blglob(hp)->tick = hdclock; | 
| 344 | > | hp->bl[i]->tick = hdclock;      /* update LRU clock */ | 
| 345 | > | blglob(hp)->tick = hdclock++; | 
| 346 |  | return(hp->bl[i]); | 
| 347 |  | } | 
| 348 |  |  | 
| 349 |  |  | 
| 350 |  | int | 
| 351 | < | hdgetbi(hp, i)                  /* allocate a file fragment */ | 
| 351 | > | hdfilord(hb1, hb2)      /* order beams for optimal loading */ | 
| 352 | > | register HDBEAMI        *hb1, *hb2; | 
| 353 | > | { | 
| 354 | > | register int    c; | 
| 355 | > | /* sort by file descriptor first */ | 
| 356 | > | if ((c = hb1->h->fd - hb2->h->fd)) | 
| 357 | > | return(c); | 
| 358 | > | /* then by position in file */ | 
| 359 | > | return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo); | 
| 360 | > | } | 
| 361 | > |  | 
| 362 | > |  | 
| 363 | > | hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */ | 
| 364 | > | register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */ | 
| 365 | > | int     n;                      /* list length */ | 
| 366 | > | int     (*bf)();                /* callback function (optional) */ | 
| 367 | > | { | 
| 368 | > | unsigned        origcachesize, memuse; | 
| 369 | > | register BEAM   *bp; | 
| 370 | > | int     bytesloaded, needbytes, bytes2free; | 
| 371 | > | register int    i; | 
| 372 | > | /* precheck consistency */ | 
| 373 | > | for (i = n; i--; ) | 
| 374 | > | if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) | 
| 375 | > | error(CONSISTENCY, "bad beam in hdloadbeams"); | 
| 376 | > | /* sort list for optimal access */ | 
| 377 | > | qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); | 
| 378 | > | bytesloaded = needbytes = 0;    /* figure out memory needs */ | 
| 379 | > | if ((origcachesize = hdcachesize) > 0) { | 
| 380 | > | for (i = n; i--; ) | 
| 381 | > | if ((bp = hb[i].h->bl[hb[i].b]) != NULL) { | 
| 382 | > | bp->tick = hdclock;     /* preempt swap */ | 
| 383 | > | bytesloaded += bp->nrm; | 
| 384 | > | } else                          /* prepare to load */ | 
| 385 | > | needbytes += hb[i].h->bi[hb[i].b].nrd; | 
| 386 | > | bytesloaded *= sizeof(RAYVAL); | 
| 387 | > | needbytes *= sizeof(RAYVAL); | 
| 388 | > | do {                            /* free enough memory */ | 
| 389 | > | memuse = hdmemuse(0); | 
| 390 | > | bytes2free = needbytes - (signed)(hdcachesize-memuse); | 
| 391 | > | if (bytes2free > (signed)(memuse - bytesloaded)) | 
| 392 | > | bytes2free = memuse - bytesloaded; | 
| 393 | > | } while (bytes2free > 0 && | 
| 394 | > | hdfreecache(100*bytes2free/memuse, NULL) < 0); | 
| 395 | > | } | 
| 396 | > | hdcachesize = 0;                /* load the ordered beams w/o swap */ | 
| 397 | > | for (i = 0; i < n; i++) | 
| 398 | > | if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) | 
| 399 | > | (*bf)(bp, hb[i].h, hb[i].b); | 
| 400 | > | hdcachesize = origcachesize;    /* resume dynamic swapping */ | 
| 401 | > | } | 
| 402 | > |  | 
| 403 | > |  | 
| 404 | > | int | 
| 405 | > | hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */ | 
| 406 |  | register HOLO   *hp; | 
| 407 |  | register int    i; | 
| 408 |  | { | 
| 409 | < | int     nrays = hp->bl[i]->nrm; | 
| 410 | < |  | 
| 411 | < | if (hp->bi[i].nrd == nrays)     /* current one will do? */ | 
| 409 | > | unsigned int    nrays; | 
| 410 | > | long    nfo; | 
| 411 | > | unsigned int    n; | 
| 412 | > | /* check file status */ | 
| 413 | > | if (hdfragl[hp->fd].writerr) | 
| 414 | > | return(-1); | 
| 415 | > | #ifdef DEBUG | 
| 416 | > | if (i < 1 | i > nbeams(hp)) | 
| 417 | > | error(CONSISTENCY, "bad beam index in hdsyncbeam"); | 
| 418 | > | #endif | 
| 419 | > | /* is current fragment OK? */ | 
| 420 | > | if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) | 
| 421 |  | return(0); | 
| 422 | < |  | 
| 423 | < | if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ | 
| 422 | > | /* locate fragment */ | 
| 423 | > | if (hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks) /* untracked */ | 
| 424 |  | hp->bi[i].fo = lseek(hp->fd, 0L, 2); | 
| 425 |  |  | 
| 426 |  | else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == | 
| 427 | < | hdfrag[hp->fd].flen)            /* EOF special case */ | 
| 428 | < | hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); | 
| 427 | > | hdfragl[hp->fd].flen)           /* EOF special case */ | 
| 428 | > | hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); | 
| 429 |  |  | 
| 430 |  | else {                                          /* general case */ | 
| 431 | < | register struct fragment        *f = &hdfrag[hp->fd]; | 
| 431 | > | register struct fraglist        *f = &hdfragl[hp->fd]; | 
| 432 |  | register int    j, k; | 
| 433 | < | /* relinquish old fragment */ | 
| 433 | > | n = f->nfrags;          /* relinquish old fragment */ | 
| 434 |  | if (hp->bi[i].nrd) { | 
| 435 |  | j = f->nfrags++; | 
| 436 | < | #ifdef MAXFRAG | 
| 436 | > | #if MAXFRAG | 
| 437 |  | if (j >= MAXFRAG-1) | 
| 438 |  | f->nfrags--; | 
| 439 |  | #endif | 
| 446 |  | (j+FRAGBLK)*sizeof(BEAMI)); | 
| 447 |  | if (f->fi == NULL) | 
| 448 |  | error(SYSTEM, | 
| 449 | < | "out of memory in hdgetbi"); | 
| 449 | > | "out of memory in hdsyncbeam"); | 
| 450 |  | } | 
| 451 |  | for ( ; ; j--) {        /* insert in descending list */ | 
| 452 |  | if (!j || hp->bi[i].fo < f->fi[j-1].fo) { | 
| 454 |  | f->fi[j].nrd = hp->bi[i].nrd; | 
| 455 |  | break; | 
| 456 |  | } | 
| 457 | < | copystruct(f->fi+j, f->fi+j-1); | 
| 457 | > | copystruct(f->fi+j, f->fi+(j-1)); | 
| 458 |  | } | 
| 459 |  | /* coalesce adjacent fragments */ | 
| 460 | < | for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 461 | < | if (k > j) | 
| 462 | < | copystruct(f->fi+j, f->fi+k); | 
| 463 | < | while (k+1 < f->nfrags && f->fi[k+1].fo + | 
| 464 | < | f->fi[k+1].nrd*sizeof(RAYVAL) | 
| 370 | < | == f->fi[j].fo) { | 
| 371 | < | f->fi[j].fo -= | 
| 372 | < | f->fi[++k].nrd*sizeof(RAYVAL); | 
| 373 | < | f->fi[j].nrd += f->fi[k].nrd; | 
| 374 | < | } | 
| 460 | > | if (j && f->fi[j-1].fo == f->fi[j].fo + | 
| 461 | > | f->fi[j].nrd*sizeof(RAYVAL)) { | 
| 462 | > | f->fi[j].nrd += f->fi[j-1].nrd; | 
| 463 | > | f->fi[j-1].nrd = 0; | 
| 464 | > | n = j-1; | 
| 465 |  | } | 
| 466 | < | f->nfrags = j; | 
| 466 | > | if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + | 
| 467 | > | f->fi[j+1].nrd*sizeof(RAYVAL)) { | 
| 468 | > | f->fi[j+1].nrd += f->fi[j].nrd; | 
| 469 | > | f->fi[j].nrd = 0; | 
| 470 | > | if (j < n) n = j; | 
| 471 | > | } | 
| 472 |  | } | 
| 473 |  | k = -1;                 /* find closest-sized fragment */ | 
| 474 | < | for (j = f->nfrags; j-- > 0; ) | 
| 474 | > | for (j = (nrays ? f->nfrags : 0); j-- > 0; ) | 
| 475 |  | if (f->fi[j].nrd >= nrays && | 
| 476 |  | (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 477 |  | if (f->fi[k=j].nrd == nrays) | 
| 478 |  | break; | 
| 479 |  | if (k < 0) {            /* no fragment -- extend file */ | 
| 480 | < | hp->bi[i].fo = f->flen; | 
| 480 | > | nfo = f->flen; | 
| 481 |  | f->flen += nrays*sizeof(RAYVAL); | 
| 482 |  | } else {                /* else use fragment */ | 
| 483 | < | hp->bi[i].fo = f->fi[k].fo; | 
| 484 | < | if (f->fi[k].nrd == nrays) {    /* delete fragment */ | 
| 485 | < | f->nfrags--; | 
| 486 | < | for (j = k; j < f->nfrags; j++) | 
| 392 | < | copystruct(f->fi+j, f->fi+j+1); | 
| 393 | < | } else {                        /* else shrink it */ | 
| 394 | < | f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 395 | < | f->fi[k].nrd -= nrays; | 
| 396 | < | } | 
| 483 | > | nfo = f->fi[k].fo; | 
| 484 | > | f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 485 | > | if (!(f->fi[k].nrd -= nrays) && k < n) | 
| 486 | > | n = k; | 
| 487 |  | } | 
| 488 | + | /* delete empty remnants */ | 
| 489 | + | for (j = k = n; k < f->nfrags; j++, k++) { | 
| 490 | + | while (f->fi[k].nrd == 0) | 
| 491 | + | if (++k >= f->nfrags) | 
| 492 | + | goto endloop; | 
| 493 | + | if (k > j) | 
| 494 | + | copystruct(f->fi+j, f->fi+k); | 
| 495 | + | } | 
| 496 | + | endloop: | 
| 497 | + | f->nfrags = j; | 
| 498 |  | } | 
| 499 | + | if (nrays) {            /* write the new fragment */ | 
| 500 | + | errno = 0; | 
| 501 | + | if (lseek(hp->fd, nfo, 0) < 0) | 
| 502 | + | error(SYSTEM, "cannot seek on holodeck file"); | 
| 503 | + | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 504 | + | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { | 
| 505 | + | hdfragl[hp->fd].writerr++; | 
| 506 | + | hdsync(hp, 0);          /* sync directory */ | 
| 507 | + | error(SYSTEM, "write error in hdsyncbeam"); | 
| 508 | + | } | 
| 509 | + | } | 
| 510 |  | biglob(hp)->nrd += nrays - hp->bi[i].nrd; | 
| 511 |  | hp->bi[i].nrd = nrays; | 
| 512 | + | hp->bi[i].fo = nfo; | 
| 513 |  | markdirty(hp);          /* section directory now out of date */ | 
| 514 |  | return(1); | 
| 515 |  | } | 
| 520 |  | register HOLO   *hp; | 
| 521 |  | register int    i; | 
| 522 |  | { | 
| 523 | < | int     nchanged, n; | 
| 523 | > | int     nchanged; | 
| 524 |  |  | 
| 525 |  | if (hp == NULL) {               /* clear all holodecks */ | 
| 526 |  | nchanged = 0; | 
| 528 |  | nchanged += hdfreebeam(hdlist[i], 0); | 
| 529 |  | return(nchanged); | 
| 530 |  | } | 
| 531 | + | if (hdfragl[hp->fd].writerr)    /* check for file error */ | 
| 532 | + | return(0); | 
| 533 |  | if (i == 0) {                   /* clear entire holodeck */ | 
| 534 |  | nchanged = 0; | 
| 535 | < | for (i = 1; i <= nbeams(hp); i++) | 
| 536 | < | nchanged += hdfreebeam(hp, i); | 
| 535 | > | for (i = nbeams(hp); i > 0; i--) | 
| 536 | > | if (hp->bl[i] != NULL) | 
| 537 | > | nchanged += hdfreebeam(hp, i); | 
| 538 |  | return(nchanged); | 
| 539 |  | } | 
| 540 | + | #ifdef DEBUG | 
| 541 |  | if (i < 1 | i > nbeams(hp)) | 
| 542 |  | error(CONSISTENCY, "bad beam index to hdfreebeam"); | 
| 543 | + | #endif | 
| 544 |  | if (hp->bl[i] == NULL) | 
| 545 |  | return(0); | 
| 546 |  | /* check for additions */ | 
| 547 |  | nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; | 
| 548 | < | if (nchanged) { | 
| 549 | < | hdgetbi(hp, i);                 /* allocate a file position */ | 
| 433 | < | errno = 0; | 
| 434 | < | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 435 | < | error(SYSTEM, "cannot seek on holodeck file"); | 
| 436 | < | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 437 | < | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 438 | < | error(SYSTEM, "write error in hdfreebeam"); | 
| 439 | < | } | 
| 548 | > | if (nchanged) | 
| 549 | > | hdsyncbeam(hp, i);              /* write new fragment */ | 
| 550 |  | blglob(hp)->nrm -= hp->bl[i]->nrm; | 
| 551 |  | free((char *)hp->bl[i]);                /* free memory */ | 
| 552 |  | hp->bl[i] = NULL; | 
| 554 |  | } | 
| 555 |  |  | 
| 556 |  |  | 
| 557 | < | hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */ | 
| 558 | < | register HOLO   *ha[];                  /* section list (NULL terminated) */ | 
| 559 | < | register int    ba[];                   /* beam index to go with section */ | 
| 560 | < | int     n;                              /* length of arrays minus 1 */ | 
| 557 | > | int | 
| 558 | > | hdkillbeam(hp, i)               /* delete beam from holodeck */ | 
| 559 | > | register HOLO   *hp; | 
| 560 | > | register int    i; | 
| 561 | > | { | 
| 562 | > | static BEAM     emptybeam; | 
| 563 | > | int     nchanged; | 
| 564 | > |  | 
| 565 | > | if (hp == NULL) {               /* clobber all holodecks */ | 
| 566 | > | nchanged = 0; | 
| 567 | > | for (i = 0; hdlist[i] != NULL; i++) | 
| 568 | > | nchanged += hdkillbeam(hdlist[i], 0); | 
| 569 | > | return(nchanged); | 
| 570 | > | } | 
| 571 | > | if (i == 0) {                   /* clobber entire holodeck */ | 
| 572 | > | nchanged = 0; | 
| 573 | > | for (i = nbeams(hp); i > 0; i--) | 
| 574 | > | if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) | 
| 575 | > | nchanged += hdkillbeam(hp, i); | 
| 576 | > | #ifdef DEBUG | 
| 577 | > | if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) | 
| 578 | > | error(CONSISTENCY, "bad beam count in hdkillbeam"); | 
| 579 | > | #endif | 
| 580 | > | return(nchanged); | 
| 581 | > | } | 
| 582 | > | #ifdef DEBUG | 
| 583 | > | if (i < 1 | i > nbeams(hp)) | 
| 584 | > | error(CONSISTENCY, "bad beam index to hdkillbeam"); | 
| 585 | > | #endif | 
| 586 | > | if (hp->bl[i] != NULL) {        /* free memory */ | 
| 587 | > | blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; | 
| 588 | > | free((char *)hp->bl[i]); | 
| 589 | > | } else | 
| 590 | > | nchanged = hp->bi[i].nrd; | 
| 591 | > | if (hp->bi[i].nrd) {            /* free file fragment */ | 
| 592 | > | hp->bl[i] = &emptybeam; | 
| 593 | > | hdsyncbeam(hp, i); | 
| 594 | > | } | 
| 595 | > | hp->bl[i] = NULL; | 
| 596 | > | return(nchanged); | 
| 597 | > | } | 
| 598 | > |  | 
| 599 | > |  | 
| 600 | > | int | 
| 601 | > | hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */ | 
| 602 | > | register HDBEAMI        *hb;            /* beam list */ | 
| 603 | > | int     nents;                          /* current list length */ | 
| 604 | > | int     n;                              /* maximum list length */ | 
| 605 |  | register HOLO   *hp;                    /* section we're adding from */ | 
| 606 |  | { | 
| 607 |  | register int    i, j; | 
| 454 | – | int     nents; | 
| 455 | – | /* find last entry in LRU list */ | 
| 456 | – | for (j = 0; ha[j] != NULL; j++) | 
| 457 | – | ; | 
| 458 | – | nents = j; | 
| 608 |  | /* insert each beam from hp */ | 
| 609 | < | for (i = nbeams(hp); i > 0; i-- ) { | 
| 609 | > | for (i = nbeams(hp); i > 0; i--) { | 
| 610 |  | if (hp->bl[i] == NULL)          /* check if loaded */ | 
| 611 |  | continue; | 
| 612 | < | if ((j = ++nents) > n)          /* grow list if we can */ | 
| 612 | > | #if 0 | 
| 613 | > | if (hp->bl[i]->tick == hdclock) /* preempt swap? */ | 
| 614 | > | continue; | 
| 615 | > | #endif | 
| 616 | > | if ((j = ++nents) >= n)         /* grow list if we can */ | 
| 617 |  | nents--; | 
| 618 |  | for ( ; ; ) {                   /* bubble into place */ | 
| 619 |  | if (!--j || hp->bl[i]->tick >= | 
| 620 | < | ha[j-1]->bl[ba[j-1]]->tick) { | 
| 621 | < | ha[j] = hp; | 
| 622 | < | ba[j] = i; | 
| 620 | > | hb[j-1].h->bl[hb[j-1].b]->tick) { | 
| 621 | > | hb[j].h = hp; | 
| 622 | > | hb[j].b = i; | 
| 623 |  | break; | 
| 624 |  | } | 
| 625 | < | ha[j] = ha[j-1]; | 
| 473 | < | ba[j] = ba[j-1]; | 
| 625 | > | copystruct(hb+j, hb+(j-1)); | 
| 626 |  | } | 
| 627 |  | } | 
| 628 | < | ha[nents] = NULL;               /* all done */ | 
| 477 | < | ba[nents] = 0; | 
| 628 | > | return(nents);                  /* return new list length */ | 
| 629 |  | } | 
| 630 |  |  | 
| 631 |  |  | 
| 632 | + | int | 
| 633 |  | hdfreecache(pct, honly)         /* free up cache space, writing changes */ | 
| 634 |  | int     pct;                            /* maximum percentage to free */ | 
| 635 |  | register HOLO   *honly;                 /* NULL means check all */ | 
| 636 |  | { | 
| 637 | < | HOLO    *hp[FREEBEAMS+1]; | 
| 486 | < | int     bn[FREEBEAMS+1]; | 
| 637 | > | HDBEAMI hb[FREEBEAMS]; | 
| 638 |  | int     freetarget; | 
| 639 | + | int     n; | 
| 640 |  | register int    i; | 
| 641 |  | /* compute free target */ | 
| 642 |  | freetarget = (honly != NULL) ? blglob(honly)->nrm : | 
| 643 |  | hdmemuse(0)/sizeof(RAYVAL) ; | 
| 644 |  | freetarget = freetarget*pct/100; | 
| 645 | + | if (freetarget <= 0) | 
| 646 | + | return(0); | 
| 647 |  | /* find least recently used */ | 
| 648 | < | hp[0] = NULL; | 
| 495 | < | bn[0] = 0; | 
| 648 | > | n = 0; | 
| 649 |  | if (honly != NULL) | 
| 650 | < | hdlrulist(hp, bn, FREEBEAMS, honly); | 
| 650 | > | n = hdlrulist(hb, n, FREEBEAMS, honly); | 
| 651 |  | else | 
| 652 |  | for (i = 0; hdlist[i] != NULL; i++) | 
| 653 | < | hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); | 
| 653 | > | n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); | 
| 654 |  | /* free LRU beams */ | 
| 655 | < | for (i = 0; hp[i] != NULL; i++) { | 
| 656 | < | hdfreebeam(hp[i], bn[i]); | 
| 657 | < | if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) | 
| 655 | > | for (i = 0; i < n; i++) { | 
| 656 | > | hdfreebeam(hb[i].h, hb[i].b); | 
| 657 | > | if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) | 
| 658 |  | break; | 
| 659 |  | } | 
| 660 | < | hdsync(honly);          /* synchronize directories as necessary */ | 
| 660 | > | hdsync(honly, 0);       /* synchronize directories as necessary */ | 
| 661 | > | return(-freetarget);    /* return how far past goal we went */ | 
| 662 |  | } | 
| 663 |  |  | 
| 664 |  |  |