1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
126 |
|
error(SYSTEM, "failure loading holodeck directory"); |
127 |
|
/* check that it's clean */ |
128 |
|
if (hp->bi[nbeams(hp)].fo < 0) |
129 |
< |
error(USER, "dirty holodeck section"); |
129 |
> |
error(WARNING, "dirty holodeck section"); |
130 |
|
} else { /* assume we're creating it */ |
131 |
|
if ((hp = hdalloc(hproto)) == NULL) |
132 |
|
goto memerr; |
410 |
|
} |
411 |
|
|
412 |
|
|
413 |
+ |
hdfreefrag(fd, bi) /* free a file fragment */ |
414 |
+ |
int fd; |
415 |
+ |
register BEAMI *bi; |
416 |
+ |
{ |
417 |
+ |
register struct fraglist *f; |
418 |
+ |
register int j, k; |
419 |
+ |
|
420 |
+ |
if (bi->nrd == 0) |
421 |
+ |
return; |
422 |
+ |
#ifdef DEBUG |
423 |
+ |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
424 |
+ |
error(CONSISTENCY, "bad file descriptor in hdfreefrag"); |
425 |
+ |
#endif |
426 |
+ |
f = &hdfragl[fd]; |
427 |
+ |
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
428 |
+ |
for (j = k = 0; k < f->nfrags; j++, k++) { |
429 |
+ |
while (f->fi[k].nrd == 0) |
430 |
+ |
if (++k >= f->nfrags) |
431 |
+ |
goto endloop; |
432 |
+ |
if (k > j) |
433 |
+ |
copystruct(f->fi+j, f->fi+k); |
434 |
+ |
} |
435 |
+ |
endloop: |
436 |
+ |
f->nfrags = j; |
437 |
+ |
} |
438 |
+ |
j = f->nfrags++; /* allocate a slot in free list */ |
439 |
+ |
#if MAXFRAG |
440 |
+ |
if (j >= MAXFRAG-1) |
441 |
+ |
f->nfrags--; |
442 |
+ |
#endif |
443 |
+ |
if (j % FRAGBLK == 0) { /* more free list space */ |
444 |
+ |
if (f->fi == NULL) |
445 |
+ |
f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI)); |
446 |
+ |
else |
447 |
+ |
f->fi = (BEAMI *)realloc((char *)f->fi, |
448 |
+ |
(j+FRAGBLK)*sizeof(BEAMI)); |
449 |
+ |
if (f->fi == NULL) |
450 |
+ |
error(SYSTEM, "out of memory in hdfreefrag"); |
451 |
+ |
} |
452 |
+ |
for ( ; ; j--) { /* insert in descending list */ |
453 |
+ |
if (!j || bi->fo < f->fi[j-1].fo) { |
454 |
+ |
f->fi[j].fo = bi->fo; |
455 |
+ |
f->fi[j].nrd = bi->nrd; |
456 |
+ |
break; |
457 |
+ |
} |
458 |
+ |
copystruct(f->fi+j, f->fi+(j-1)); |
459 |
+ |
} |
460 |
+ |
/* coalesce adjacent fragments */ |
461 |
+ |
/* successors never empty */ |
462 |
+ |
if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { |
463 |
+ |
f->fi[j].nrd += f->fi[j-1].nrd; |
464 |
+ |
f->fi[j-1].nrd = 0; |
465 |
+ |
} |
466 |
+ |
for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */ |
467 |
+ |
if (f->fi[k].nrd) { |
468 |
+ |
if (f->fi[j].fo == f->fi[k].fo + |
469 |
+ |
f->fi[k].nrd*sizeof(RAYVAL)) { |
470 |
+ |
f->fi[k].nrd += f->fi[j].nrd; |
471 |
+ |
f->fi[j].nrd = 0; |
472 |
+ |
} |
473 |
+ |
break; |
474 |
+ |
} |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
|
478 |
+ |
long |
479 |
+ |
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
480 |
+ |
int fd; |
481 |
+ |
unsigned int4 nrays; |
482 |
+ |
{ |
483 |
+ |
register struct fraglist *f; |
484 |
+ |
register int j, k; |
485 |
+ |
long nfo; |
486 |
+ |
|
487 |
+ |
if (nrays == 0) |
488 |
+ |
return(-1L); |
489 |
+ |
#ifdef DEBUG |
490 |
+ |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
491 |
+ |
error(CONSISTENCY, "bad file descriptor in hdallocfrag"); |
492 |
+ |
#endif |
493 |
+ |
f = &hdfragl[fd]; |
494 |
+ |
k = -1; /* find closest-sized fragment */ |
495 |
+ |
for (j = f->nfrags; j-- > 0; ) |
496 |
+ |
if (f->fi[j].nrd >= nrays && |
497 |
+ |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
498 |
+ |
if (f->fi[k=j].nrd == nrays) |
499 |
+ |
break; |
500 |
+ |
if (k < 0) { /* no fragment -- extend file */ |
501 |
+ |
nfo = f->flen; |
502 |
+ |
f->flen += nrays*sizeof(RAYVAL); |
503 |
+ |
} else { /* else use fragment */ |
504 |
+ |
nfo = f->fi[k].fo; |
505 |
+ |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
506 |
+ |
f->fi[k].nrd -= nrays; |
507 |
+ |
} |
508 |
+ |
return(nfo); |
509 |
+ |
} |
510 |
+ |
|
511 |
+ |
|
512 |
|
int |
513 |
|
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
514 |
|
register HOLO *hp; |
515 |
|
register int i; |
516 |
|
{ |
517 |
< |
unsigned int nrays; |
419 |
< |
long nfo; |
517 |
> |
unsigned int4 nrays; |
518 |
|
unsigned int n; |
519 |
+ |
long nfo; |
520 |
|
/* check file status */ |
521 |
|
if (hdfragl[hp->fd].writerr) |
522 |
|
return(-1); |
527 |
|
/* is current fragment OK? */ |
528 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
529 |
|
return(0); |
530 |
< |
/* locate fragment */ |
531 |
< |
if (hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks) /* untracked */ |
532 |
< |
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
533 |
< |
|
435 |
< |
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
436 |
< |
hdfragl[hp->fd].flen) /* EOF special case */ |
437 |
< |
hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
438 |
< |
|
439 |
< |
else { /* general case */ |
440 |
< |
register struct fraglist *f = &hdfragl[hp->fd]; |
441 |
< |
register int j, k; |
442 |
< |
n = f->nfrags; /* relinquish old fragment */ |
443 |
< |
if (hp->bi[i].nrd) { |
444 |
< |
j = f->nfrags++; |
445 |
< |
#if MAXFRAG |
446 |
< |
if (j >= MAXFRAG-1) |
447 |
< |
f->nfrags--; |
448 |
< |
#endif |
449 |
< |
if (j % FRAGBLK == 0) { /* more frag. space */ |
450 |
< |
if (f->fi == NULL) |
451 |
< |
f->fi = (BEAMI *)malloc( |
452 |
< |
FRAGBLK*sizeof(BEAMI)); |
453 |
< |
else |
454 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
455 |
< |
(j+FRAGBLK)*sizeof(BEAMI)); |
456 |
< |
if (f->fi == NULL) |
457 |
< |
error(SYSTEM, |
458 |
< |
"out of memory in hdsyncbeam"); |
459 |
< |
} |
460 |
< |
for ( ; ; j--) { /* insert in descending list */ |
461 |
< |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
462 |
< |
f->fi[j].fo = hp->bi[i].fo; |
463 |
< |
f->fi[j].nrd = hp->bi[i].nrd; |
464 |
< |
break; |
465 |
< |
} |
466 |
< |
copystruct(f->fi+j, f->fi+(j-1)); |
467 |
< |
} |
468 |
< |
/* coalesce adjacent fragments */ |
469 |
< |
if (j && f->fi[j-1].fo == f->fi[j].fo + |
470 |
< |
f->fi[j].nrd*sizeof(RAYVAL)) { |
471 |
< |
f->fi[j].nrd += f->fi[j-1].nrd; |
472 |
< |
f->fi[j-1].nrd = 0; |
473 |
< |
n = j-1; |
474 |
< |
} |
475 |
< |
if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + |
476 |
< |
f->fi[j+1].nrd*sizeof(RAYVAL)) { |
477 |
< |
f->fi[j+1].nrd += f->fi[j].nrd; |
478 |
< |
f->fi[j].nrd = 0; |
479 |
< |
if (j < n) n = j; |
480 |
< |
} |
481 |
< |
} |
482 |
< |
k = -1; /* find closest-sized fragment */ |
483 |
< |
for (j = (nrays ? f->nfrags : 0); j-- > 0; ) |
484 |
< |
if (f->fi[j].nrd >= nrays && |
485 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
486 |
< |
if (f->fi[k=j].nrd == nrays) |
487 |
< |
break; |
488 |
< |
if (k < 0) { /* no fragment -- extend file */ |
489 |
< |
nfo = f->flen; |
490 |
< |
f->flen += nrays*sizeof(RAYVAL); |
491 |
< |
} else { /* else use fragment */ |
492 |
< |
nfo = f->fi[k].fo; |
493 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
494 |
< |
if (!(f->fi[k].nrd -= nrays) && k < n) |
495 |
< |
n = k; |
496 |
< |
} |
497 |
< |
/* delete empty remnants */ |
498 |
< |
for (j = k = n; k < f->nfrags; j++, k++) { |
499 |
< |
while (f->fi[k].nrd == 0) |
500 |
< |
if (++k >= f->nfrags) |
501 |
< |
goto endloop; |
502 |
< |
if (k > j) |
503 |
< |
copystruct(f->fi+j, f->fi+k); |
504 |
< |
} |
505 |
< |
endloop: |
506 |
< |
f->nfrags = j; |
507 |
< |
} |
508 |
< |
if (nrays) { /* write the new fragment */ |
530 |
> |
if (hp->bi[i].nrd) /* relinquish old fragment */ |
531 |
> |
hdfreefrag(hp->fd, &hp->bi[i]); |
532 |
> |
if (nrays) { /* get and write new fragment */ |
533 |
> |
nfo = hdallocfrag(hp->fd, nrays); |
534 |
|
errno = 0; |
535 |
|
if (lseek(hp->fd, nfo, 0) < 0) |
536 |
|
error(SYSTEM, "cannot seek on holodeck file"); |
537 |
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
538 |
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
539 |
|
hdfragl[hp->fd].writerr++; |
540 |
< |
hdsync(hp, 0); /* sync directory */ |
540 |
> |
hdsync(NULL, 0); /* sync directories */ |
541 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
542 |
|
} |
543 |
< |
} |
543 |
> |
hp->bi[i].fo = nfo; |
544 |
> |
} else |
545 |
> |
hp->bi[i].fo = 0L; |
546 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
547 |
|
hp->bi[i].nrd = nrays; |
548 |
< |
hp->bi[i].fo = nfo; |
522 |
< |
markdirty(hp); /* section directory now out of date */ |
548 |
> |
markdirty(hp); /* section directory now out of date */ |
549 |
|
return(1); |
550 |
|
} |
551 |
|
|