| 1 | 
< | 
/* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | 
> | 
/* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ SGI"; | 
| 16 | 
  | 
#define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */ | 
| 17 | 
  | 
#endif | 
| 18 | 
  | 
#ifndef FREEBEAMS | 
| 19 | 
< | 
#define FREEBEAMS       512     /* 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         20      /* maximum fraction to free (%) */ | 
| 22 | 
> | 
#define PCTFREE         15      /* maximum fraction to free (%) */ | 
| 23 | 
  | 
#endif | 
| 24 | 
  | 
#ifndef MAXFRAG | 
| 25 | 
  | 
#define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */ | 
| 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; | 
| 123 | 
  | 
int     fd;                     /* corresponding file descriptor */ | 
| 124 | 
  | 
HDGRID  *hproto;                /* holodeck section grid */ | 
| 125 | 
  | 
{ | 
| 126 | 
+ | 
        long    rtrunc; | 
| 127 | 
  | 
        long    fpos; | 
| 128 | 
  | 
        register HOLO   *hp; | 
| 129 | 
  | 
        register int    n; | 
| 145 | 
  | 
                        error(SYSTEM, "failure loading holodeck directory"); | 
| 146 | 
  | 
                                                /* check that it's clean */ | 
| 147 | 
  | 
                if (hp->bi[nbeams(hp)].fo < 0) | 
| 148 | 
< | 
                        error(USER, "dirty holodeck section"); | 
| 148 | 
> | 
                        error(WARNING, "dirty holodeck section"); | 
| 149 | 
  | 
        } else {                        /* assume we're creating it */ | 
| 150 | 
  | 
                if ((hp = hdalloc(hproto)) == NULL) | 
| 151 | 
  | 
                        goto memerr; | 
| 163 | 
  | 
        hdattach(fd); | 
| 164 | 
  | 
                                        /* check rays on disk */ | 
| 165 | 
  | 
        fpos = hdfilen(fd); | 
| 166 | 
< | 
        biglob(hp)->nrd = 0; | 
| 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) | 
| 170 | 
< | 
                                hp->bi[n].nrd = 0;      /* off end */ | 
| 171 | 
< | 
                        else | 
| 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 | 
| 173 | 
  | 
                                biglob(hp)->nrd += hp->bi[n].nrd; | 
| 174 | 
+ | 
        if (rtrunc) { | 
| 175 | 
+ | 
                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", | 
| 176 | 
+ | 
                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); | 
| 177 | 
+ | 
                error(WARNING, errmsg); | 
| 178 | 
+ | 
        } | 
| 179 | 
  | 
                                        /* add to holodeck list */ | 
| 180 | 
  | 
        for (n = 0; n < HDMAX; n++) | 
| 181 | 
  | 
                if (hdlist[n] == NULL) { | 
| 310 | 
  | 
                hp->bl[i]->tick = hdclock;      /* preempt swap */ | 
| 311 | 
  | 
        if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) | 
| 312 | 
  | 
                hdfreecache(PCTFREE, NULL);     /* free some space */ | 
| 288 | 
– | 
        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) | 
| 292 | 
< | 
                        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) ); | 
| 305 | 
< | 
                if (hp->bl[i] == NULL) | 
| 306 | 
< | 
                        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 */ | 
| 313 | 
< | 
        blglob(hp)->tick = hdclock++; | 
| 334 | 
> | 
        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 335 | 
  | 
        return(p);                              /* point to new rays */ | 
| 315 | 
– | 
memerr: | 
| 316 | 
– | 
        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; | 
| 335 | 
< | 
                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) | 
| 336 | 
< | 
                        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 */ | 
| 345 | 
< | 
        blglob(hp)->tick = hdclock++; | 
| 362 | 
> | 
        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 363 | 
  | 
        return(hp->bl[i]); | 
| 364 | 
  | 
} | 
| 365 | 
  | 
 | 
| 389 | 
  | 
int     (*bf)();                /* callback function (optional) */ | 
| 390 | 
  | 
{ | 
| 391 | 
  | 
        unsigned        origcachesize, memuse; | 
| 375 | 
– | 
        register BEAM   *bp; | 
| 392 | 
  | 
        int     bytesloaded, needbytes, bytes2free; | 
| 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"); | 
| 401 | 
  | 
        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); | 
| 402 | 
  | 
        bytesloaded = 0;                /* run through loaded beams */ | 
| 403 | 
  | 
        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { | 
| 404 | 
< | 
                bp->tick = hdclock;             /* preempt swap */ | 
| 404 | 
> | 
                bp->tick = hdclock;     /* preempt swap */ | 
| 405 | 
  | 
                bytesloaded += bp->nrm; | 
| 406 | 
  | 
                if (bf != NULL) | 
| 407 | 
< | 
                        (*bf)(bp, hb->h, hb->b); | 
| 407 | 
> | 
                        (*bf)(bp, hb); | 
| 408 | 
  | 
        } | 
| 409 | 
  | 
        bytesloaded *= sizeof(RAYVAL); | 
| 410 | 
  | 
        if ((origcachesize = hdcachesize) > 0) { | 
| 423 | 
  | 
        } | 
| 424 | 
  | 
        for (i = 0; i < n; i++) | 
| 425 | 
  | 
                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) | 
| 426 | 
< | 
                        (*bf)(bp, hb[i].h, hb[i].b); | 
| 426 | 
> | 
                        (*bf)(bp, hb+i); | 
| 427 | 
  | 
        hdcachesize = origcachesize;    /* resume dynamic swapping */ | 
| 428 | 
  | 
} | 
| 429 | 
  | 
 | 
| 430 | 
  | 
 | 
| 431 | 
+ | 
hdfreefrag(fd, bi)                      /* free a file fragment */ | 
| 432 | 
+ | 
int     fd; | 
| 433 | 
+ | 
register BEAMI  *bi; | 
| 434 | 
+ | 
{ | 
| 435 | 
+ | 
        register struct fraglist        *f; | 
| 436 | 
+ | 
        register int    j, k; | 
| 437 | 
+ | 
 | 
| 438 | 
+ | 
        if (bi->nrd == 0) | 
| 439 | 
+ | 
                return; | 
| 440 | 
+ | 
#ifdef DEBUG | 
| 441 | 
+ | 
        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) | 
| 442 | 
+ | 
                error(CONSISTENCY, "bad file descriptor in hdfreefrag"); | 
| 443 | 
+ | 
#endif | 
| 444 | 
+ | 
        f = &hdfragl[fd]; | 
| 445 | 
+ | 
        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ | 
| 446 | 
+ | 
                for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 447 | 
+ | 
                        while (f->fi[k].nrd == 0) | 
| 448 | 
+ | 
                                if (++k >= f->nfrags) | 
| 449 | 
+ | 
                                        goto endloop; | 
| 450 | 
+ | 
                        if (k > j) | 
| 451 | 
+ | 
                                copystruct(f->fi+j, f->fi+k); | 
| 452 | 
+ | 
                } | 
| 453 | 
+ | 
        endloop: | 
| 454 | 
+ | 
                f->nfrags = j; | 
| 455 | 
+ | 
        } | 
| 456 | 
+ | 
        j = f->nfrags++;                /* allocate a slot in free list */ | 
| 457 | 
+ | 
#if MAXFRAG | 
| 458 | 
+ | 
        if (j >= MAXFRAG-1) | 
| 459 | 
+ | 
                f->nfrags--; | 
| 460 | 
+ | 
#endif | 
| 461 | 
+ | 
        if (j % FRAGBLK == 0) {         /* more free list space */ | 
| 462 | 
+ | 
                register BEAMI  *newp; | 
| 463 | 
+ | 
                if (f->fi == NULL) | 
| 464 | 
+ | 
                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); | 
| 465 | 
+ | 
                else | 
| 466 | 
+ | 
                        newp = (BEAMI *)realloc((char *)f->fi, | 
| 467 | 
+ | 
                                        (j+FRAGBLK)*sizeof(BEAMI)); | 
| 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) { | 
| 476 | 
+ | 
                        f->fi[j].fo = bi->fo; | 
| 477 | 
+ | 
                        f->fi[j].nrd = bi->nrd; | 
| 478 | 
+ | 
                        break; | 
| 479 | 
+ | 
                } | 
| 480 | 
+ | 
                copystruct(f->fi+j, f->fi+(j-1)); | 
| 481 | 
+ | 
        } | 
| 482 | 
+ | 
                                        /* coalesce adjacent fragments */ | 
| 483 | 
+ | 
                                                /* successors never empty */ | 
| 484 | 
+ | 
        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { | 
| 485 | 
+ | 
                f->fi[j].nrd += f->fi[j-1].nrd; | 
| 486 | 
+ | 
                f->fi[j-1].nrd = 0; | 
| 487 | 
+ | 
        } | 
| 488 | 
+ | 
        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */ | 
| 489 | 
+ | 
                if (f->fi[k].nrd) { | 
| 490 | 
+ | 
                        if (f->fi[j].fo == f->fi[k].fo + | 
| 491 | 
+ | 
                                        f->fi[k].nrd*sizeof(RAYVAL)) { | 
| 492 | 
+ | 
                                f->fi[k].nrd += f->fi[j].nrd; | 
| 493 | 
+ | 
                                f->fi[j].nrd = 0; | 
| 494 | 
+ | 
                        } | 
| 495 | 
+ | 
                        break; | 
| 496 | 
+ | 
                } | 
| 497 | 
+ | 
} | 
| 498 | 
+ | 
 | 
| 499 | 
+ | 
 | 
| 500 | 
+ | 
long | 
| 501 | 
+ | 
hdallocfrag(fd, nrays)          /* allocate a file fragment */ | 
| 502 | 
+ | 
int     fd; | 
| 503 | 
+ | 
unsigned int4   nrays; | 
| 504 | 
+ | 
{ | 
| 505 | 
+ | 
        register struct fraglist        *f; | 
| 506 | 
+ | 
        register int    j, k; | 
| 507 | 
+ | 
        long    nfo; | 
| 508 | 
+ | 
 | 
| 509 | 
+ | 
        if (nrays == 0) | 
| 510 | 
+ | 
                return(-1L); | 
| 511 | 
+ | 
#ifdef DEBUG | 
| 512 | 
+ | 
        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) | 
| 513 | 
+ | 
                error(CONSISTENCY, "bad file descriptor in hdallocfrag"); | 
| 514 | 
+ | 
#endif | 
| 515 | 
+ | 
        f = &hdfragl[fd]; | 
| 516 | 
+ | 
        k = -1;                         /* find closest-sized fragment */ | 
| 517 | 
+ | 
        for (j = f->nfrags; j-- > 0; ) | 
| 518 | 
+ | 
                if (f->fi[j].nrd >= nrays && | 
| 519 | 
+ | 
                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 520 | 
+ | 
                        if (f->fi[k=j].nrd == nrays) | 
| 521 | 
+ | 
                                break; | 
| 522 | 
+ | 
        if (k < 0) {                    /* no fragment -- extend file */ | 
| 523 | 
+ | 
                nfo = f->flen; | 
| 524 | 
+ | 
                f->flen += nrays*sizeof(RAYVAL); | 
| 525 | 
+ | 
        } else {                        /* else use fragment */ | 
| 526 | 
+ | 
                nfo = f->fi[k].fo; | 
| 527 | 
+ | 
                f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 528 | 
+ | 
                f->fi[k].nrd -= nrays; | 
| 529 | 
+ | 
        } | 
| 530 | 
+ | 
        return(nfo); | 
| 531 | 
+ | 
} | 
| 532 | 
+ | 
 | 
| 533 | 
+ | 
 | 
| 534 | 
  | 
int | 
| 535 | 
  | 
hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */ | 
| 536 | 
  | 
register HOLO   *hp; | 
| 537 | 
  | 
register int    i; | 
| 538 | 
  | 
{ | 
| 539 | 
< | 
        unsigned int    nrays; | 
| 419 | 
< | 
        long    nfo; | 
| 539 | 
> | 
        unsigned int4   nrays; | 
| 540 | 
  | 
        unsigned int    n; | 
| 541 | 
+ | 
        long    nfo; | 
| 542 | 
  | 
                                        /* check file status */ | 
| 543 | 
  | 
        if (hdfragl[hp->fd].writerr) | 
| 544 | 
  | 
                return(-1); | 
| 549 | 
  | 
                                        /* is current fragment OK? */ | 
| 550 | 
  | 
        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) | 
| 551 | 
  | 
                return(0); | 
| 552 | 
< | 
                                        /* locate fragment */ | 
| 553 | 
< | 
        if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == | 
| 554 | 
< | 
                        hdfragl[hp->fd].flen)           /* EOF special case */ | 
| 555 | 
< | 
                hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); | 
| 435 | 
< | 
 | 
| 436 | 
< | 
        else {                                          /* general case */ | 
| 437 | 
< | 
                register struct fraglist        *f = &hdfragl[hp->fd]; | 
| 438 | 
< | 
                register int    j, k; | 
| 439 | 
< | 
                n = f->nfrags;          /* relinquish old fragment */ | 
| 440 | 
< | 
                if (hp->bi[i].nrd) { | 
| 441 | 
< | 
                        j = f->nfrags++; | 
| 442 | 
< | 
#if MAXFRAG | 
| 443 | 
< | 
                        if (j >= MAXFRAG-1) | 
| 444 | 
< | 
                                f->nfrags--; | 
| 445 | 
< | 
#endif | 
| 446 | 
< | 
                        if (j % FRAGBLK == 0) {         /* more frag. space */ | 
| 447 | 
< | 
                                if (f->fi == NULL) | 
| 448 | 
< | 
                                        f->fi = (BEAMI *)malloc( | 
| 449 | 
< | 
                                                        FRAGBLK*sizeof(BEAMI)); | 
| 450 | 
< | 
                                else | 
| 451 | 
< | 
                                        f->fi = (BEAMI *)realloc((char *)f->fi, | 
| 452 | 
< | 
                                                (j+FRAGBLK)*sizeof(BEAMI)); | 
| 453 | 
< | 
                                if (f->fi == NULL) | 
| 454 | 
< | 
                                        error(SYSTEM, | 
| 455 | 
< | 
                                                "out of memory in hdsyncbeam"); | 
| 456 | 
< | 
                        } | 
| 457 | 
< | 
                        for ( ; ; j--) {        /* insert in descending list */ | 
| 458 | 
< | 
                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) { | 
| 459 | 
< | 
                                        f->fi[j].fo = hp->bi[i].fo; | 
| 460 | 
< | 
                                        f->fi[j].nrd = hp->bi[i].nrd; | 
| 461 | 
< | 
                                        break; | 
| 462 | 
< | 
                                } | 
| 463 | 
< | 
                                copystruct(f->fi+j, f->fi+(j-1)); | 
| 464 | 
< | 
                        } | 
| 465 | 
< | 
                                        /* coalesce adjacent fragments */ | 
| 466 | 
< | 
                        if (j && f->fi[j-1].fo == f->fi[j].fo + | 
| 467 | 
< | 
                                        f->fi[j].nrd*sizeof(RAYVAL)) { | 
| 468 | 
< | 
                                f->fi[j].nrd += f->fi[j-1].nrd; | 
| 469 | 
< | 
                                f->fi[j-1].nrd = 0; | 
| 470 | 
< | 
                                n = j-1; | 
| 471 | 
< | 
                        } | 
| 472 | 
< | 
                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + | 
| 473 | 
< | 
                                        f->fi[j+1].nrd*sizeof(RAYVAL)) { | 
| 474 | 
< | 
                                f->fi[j+1].nrd += f->fi[j].nrd; | 
| 475 | 
< | 
                                f->fi[j].nrd = 0; | 
| 476 | 
< | 
                                if (j < n) n = j; | 
| 477 | 
< | 
                        } | 
| 478 | 
< | 
                } | 
| 479 | 
< | 
                k = -1;                 /* find closest-sized fragment */ | 
| 480 | 
< | 
                for (j = (nrays ? f->nfrags : 0); j-- > 0; ) | 
| 481 | 
< | 
                        if (f->fi[j].nrd >= nrays && | 
| 482 | 
< | 
                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 483 | 
< | 
                                if (f->fi[k=j].nrd == nrays) | 
| 484 | 
< | 
                                        break; | 
| 485 | 
< | 
                if (k < 0) {            /* no fragment -- extend file */ | 
| 486 | 
< | 
                        nfo = f->flen; | 
| 487 | 
< | 
                        f->flen += nrays*sizeof(RAYVAL); | 
| 488 | 
< | 
                } else {                /* else use fragment */ | 
| 489 | 
< | 
                        nfo = f->fi[k].fo; | 
| 490 | 
< | 
                        f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 491 | 
< | 
                        if (!(f->fi[k].nrd -= nrays) && k < n) | 
| 492 | 
< | 
                                n = k; | 
| 493 | 
< | 
                } | 
| 494 | 
< | 
                                        /* delete empty remnants */ | 
| 495 | 
< | 
                for (j = k = n; k < f->nfrags; j++, k++) { | 
| 496 | 
< | 
                        while (f->fi[k].nrd == 0) | 
| 497 | 
< | 
                                if (++k >= f->nfrags) | 
| 498 | 
< | 
                                        goto endloop; | 
| 499 | 
< | 
                        if (k > j) | 
| 500 | 
< | 
                                copystruct(f->fi+j, f->fi+k); | 
| 501 | 
< | 
                } | 
| 502 | 
< | 
        endloop: | 
| 503 | 
< | 
                f->nfrags = j; | 
| 504 | 
< | 
        } | 
| 505 | 
< | 
        if (nrays) {            /* write the new fragment */ | 
| 552 | 
> | 
        if (hp->bi[i].nrd)              /* relinquish old fragment */ | 
| 553 | 
> | 
                hdfreefrag(hp->fd, &hp->bi[i]); | 
| 554 | 
> | 
        if (nrays) {                    /* get and write new fragment */ | 
| 555 | 
> | 
                nfo = hdallocfrag(hp->fd, nrays); | 
| 556 | 
  | 
                errno = 0; | 
| 557 | 
  | 
                if (lseek(hp->fd, nfo, 0) < 0) | 
| 558 | 
  | 
                        error(SYSTEM, "cannot seek on holodeck file"); | 
| 562 | 
  | 
                        hdsync(NULL, 0);        /* sync directories */ | 
| 563 | 
  | 
                        error(SYSTEM, "write error in hdsyncbeam"); | 
| 564 | 
  | 
                } | 
| 565 | 
< | 
        } | 
| 565 | 
> | 
                hp->bi[i].fo = nfo; | 
| 566 | 
> | 
        } else | 
| 567 | 
> | 
                hp->bi[i].fo = 0L; | 
| 568 | 
  | 
        biglob(hp)->nrd += nrays - hp->bi[i].nrd; | 
| 569 | 
  | 
        hp->bi[i].nrd = nrays; | 
| 570 | 
< | 
        hp->bi[i].fo = nfo; | 
| 519 | 
< | 
        markdirty(hp);          /* section directory now out of date */ | 
| 570 | 
> | 
        markdirty(hp);                  /* section directory now out of date */ | 
| 571 | 
  | 
        return(1); | 
| 572 | 
  | 
} | 
| 573 | 
  | 
 | 
| 663 | 
  | 
{ | 
| 664 | 
  | 
        register int    i, j; | 
| 665 | 
  | 
                                        /* insert each beam from hp */ | 
| 666 | 
< | 
        for (i = nbeams(hp); i > 0; i--) { | 
| 666 | 
> | 
        for (i = 1; i <= nbeams(hp); i++) { | 
| 667 | 
  | 
                if (hp->bl[i] == NULL)          /* check if loaded */ | 
| 668 | 
  | 
                        continue; | 
| 669 | 
  | 
#if 0 | 
| 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 | 
  | 
 | 
| 738 | 
  | 
        if (hp == NULL) {               /* NULL means clean up everything */ | 
| 739 | 
  | 
                while (hdlist[0] != NULL) | 
| 740 | 
  | 
                        hddone(hdlist[0]); | 
| 741 | 
+ | 
                free((char *)hdfragl); | 
| 742 | 
+ | 
                hdfragl = NULL; nhdfragls = 0; | 
| 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 */ |