1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
21 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
22 |
|
|
23 |
|
typedef struct ambtree { |
24 |
|
AMBVAL *alist; /* ambient value list */ |
27 |
|
|
28 |
|
extern CUBE thescene; /* contains space boundaries */ |
29 |
|
|
30 |
+ |
extern char *shm_boundary; /* memory sharing boundary */ |
31 |
+ |
|
32 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
34 |
|
|
40 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
41 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
42 |
|
|
43 |
+ |
#ifndef SORT_THRESH |
44 |
+ |
#ifdef BIGMEM |
45 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
46 |
+ |
#else |
47 |
+ |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
48 |
+ |
#endif |
49 |
+ |
#endif |
50 |
+ |
#ifndef SORT_INTVL |
51 |
+ |
#define SORT_INTVL (SORT_THRESH*256) |
52 |
+ |
#endif |
53 |
+ |
#ifndef MAX_SORT_INTVL |
54 |
+ |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
55 |
+ |
#endif |
56 |
+ |
|
57 |
+ |
static unsigned long ambclock = 0; /* ambient access clock */ |
58 |
+ |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
59 |
+ |
static unsigned long lastsort = 0; /* time of last value sort */ |
60 |
+ |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
61 |
+ |
|
62 |
+ |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
63 |
+ |
/* |
64 |
+ |
* Track access times unless we are sharing ambient values |
65 |
+ |
* through memory on a multiprocessor, when we want to avoid |
66 |
+ |
* claiming our own memory (copy on write). |
67 |
+ |
*/ |
68 |
+ |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
69 |
+ |
|
70 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
71 |
|
|
72 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
73 |
|
|
45 |
– |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
– |
|
74 |
|
extern long ftell(), lseek(); |
75 |
< |
static int initambfile(), avsave(), avinsert(); |
75 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
76 |
> |
static AMBVAL *avstore(); |
77 |
> |
#ifdef F_SETLKW |
78 |
> |
static aflock(); |
79 |
> |
#endif |
80 |
|
|
81 |
|
|
82 |
|
setambres(ar) /* set ambient resolution */ |
83 |
|
int ar; |
84 |
|
{ |
85 |
+ |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
86 |
|
/* set min & max radii */ |
87 |
|
if (ar <= 0) { |
88 |
< |
minarad = 0.0; |
88 |
> |
minarad = 0; |
89 |
|
maxarad = thescene.cusize / 2.0; |
90 |
|
} else { |
91 |
|
minarad = thescene.cusize / ar; |
92 |
< |
maxarad = 16.0 * minarad; /* heuristic */ |
92 |
> |
maxarad = 16 * minarad; /* heuristic */ |
93 |
|
if (maxarad > thescene.cusize / 2.0) |
94 |
|
maxarad = thescene.cusize / 2.0; |
95 |
|
} |
96 |
< |
if (maxarad <= FTINY) |
97 |
< |
maxarad = .001; |
96 |
> |
if (minarad <= FTINY) |
97 |
> |
minarad = 10*FTINY; |
98 |
> |
if (maxarad <= minarad) |
99 |
> |
maxarad = 64 * minarad; |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
+ |
setambacc(newa) /* set ambient accuracy */ |
104 |
+ |
double newa; |
105 |
+ |
{ |
106 |
+ |
static double oldambacc = -1.0; |
107 |
+ |
|
108 |
+ |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
109 |
+ |
if (oldambacc < -FTINY) |
110 |
+ |
oldambacc = ambacc; /* do nothing first call */ |
111 |
+ |
if (fabs(newa - oldambacc) < 0.01) |
112 |
+ |
return; /* insignificant -- don't bother */ |
113 |
+ |
if (ambacc <= FTINY) |
114 |
+ |
return; /* cannot build new tree */ |
115 |
+ |
/* else need to rebuild tree */ |
116 |
+ |
sortambvals(1); |
117 |
+ |
oldambacc = ambacc; /* remeber setting for next call */ |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
|
121 |
|
setambient(afile) /* initialize calculation */ |
122 |
|
char *afile; |
123 |
|
{ |
125 |
|
AMBVAL amb; |
126 |
|
/* init ambient limits */ |
127 |
|
setambres(ambres); |
128 |
+ |
setambacc(ambacc); |
129 |
+ |
if (afile == NULL) |
130 |
+ |
return; |
131 |
+ |
if (ambacc <= FTINY) { |
132 |
+ |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
133 |
+ |
afile); |
134 |
+ |
error(WARNING, errmsg); |
135 |
+ |
return; |
136 |
+ |
} |
137 |
|
/* open ambient file */ |
138 |
< |
if (afile != NULL) { |
139 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
140 |
< |
initambfile(0); |
141 |
< |
headlen = ftell(ambfp); |
142 |
< |
while (readambval(&amb, ambfp)) |
143 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
144 |
< |
thescene.cusize); |
145 |
< |
/* align */ |
146 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
147 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
148 |
< |
initambfile(1); |
149 |
< |
else { |
89 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
90 |
< |
afile); |
91 |
< |
error(SYSTEM, errmsg); |
92 |
< |
} |
93 |
< |
nunflshed++; /* lie */ |
94 |
< |
ambsync(); |
138 |
> |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
139 |
> |
initambfile(0); |
140 |
> |
headlen = ftell(ambfp); |
141 |
> |
while (readambval(&amb, ambfp)) |
142 |
> |
avinsert(avstore(&amb)); |
143 |
> |
/* align */ |
144 |
> |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
145 |
> |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
146 |
> |
initambfile(1); |
147 |
> |
else { |
148 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
149 |
> |
error(SYSTEM, errmsg); |
150 |
|
} |
151 |
+ |
nunflshed++; /* lie */ |
152 |
+ |
ambsync(); |
153 |
|
} |
154 |
|
|
155 |
|
|
200 |
|
goto dumbamb; |
201 |
|
return; |
202 |
|
} |
203 |
+ |
/* resort memory? */ |
204 |
+ |
sortambvals(0); |
205 |
|
/* get ambient value */ |
206 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
207 |
|
d = sumambient(acol, r, rdepth, |
234 |
|
int i; |
235 |
|
register int j; |
236 |
|
register AMBVAL *av; |
237 |
< |
/* do this node */ |
237 |
> |
|
238 |
|
wsum = 0.0; |
239 |
+ |
/* do this node */ |
240 |
|
for (av = at->alist; av != NULL; av = av->next) { |
241 |
+ |
if (tracktime) |
242 |
+ |
av->latick = ambclock++; |
243 |
|
/* |
244 |
|
* Ambient level test. |
245 |
|
*/ |
246 |
< |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
246 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
247 |
> |
break; |
248 |
> |
if (av->weight < r->rweight-FTINY) |
249 |
|
continue; |
250 |
|
/* |
251 |
|
* Ambient radius test. |
252 |
|
*/ |
253 |
< |
e1 = 0.0; |
254 |
< |
for (j = 0; j < 3; j++) { |
255 |
< |
d = av->pos[j] - r->rop[j]; |
256 |
< |
e1 += d * d; |
257 |
< |
} |
253 |
> |
d = av->pos[0] - r->rop[0]; |
254 |
> |
e1 = d * d; |
255 |
> |
d = av->pos[1] - r->rop[1]; |
256 |
> |
e1 += d * d; |
257 |
> |
d = av->pos[2] - r->rop[2]; |
258 |
> |
e1 += d * d; |
259 |
|
e1 /= av->rad * av->rad; |
260 |
|
if (e1 > ambacc*ambacc*1.21) |
261 |
|
continue; |
373 |
|
{ |
374 |
|
extern char *progname, *octname, VersionID[]; |
375 |
|
|
376 |
+ |
#ifdef F_SETLKW |
377 |
+ |
aflock(creat ? F_WRLCK : F_RDLCK); |
378 |
+ |
#endif |
379 |
|
#ifdef MSDOS |
380 |
|
setmode(fileno(ambfp), O_BINARY); |
381 |
|
#endif |
382 |
< |
setbuf(ambfp, bmalloc(BUFSIZ)); |
382 |
> |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
383 |
|
if (creat) { /* new file */ |
384 |
+ |
newheader("RADIANCE", ambfp); |
385 |
|
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
386 |
|
progname, colval(ambval,RED), |
387 |
|
colval(ambval,GRN), colval(ambval,BLU), |
402 |
|
avsave(av) /* insert and save an ambient value */ |
403 |
|
AMBVAL *av; |
404 |
|
{ |
405 |
< |
avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); |
405 |
> |
avinsert(avstore(av)); |
406 |
|
if (ambfp == NULL) |
407 |
|
return; |
408 |
|
if (writambval(av, ambfp) < 0) |
416 |
|
} |
417 |
|
|
418 |
|
|
419 |
+ |
static AMBVAL * |
420 |
+ |
avstore(aval) /* allocate memory and store aval */ |
421 |
+ |
register AMBVAL *aval; |
422 |
+ |
{ |
423 |
+ |
register AMBVAL *av; |
424 |
+ |
|
425 |
+ |
if ((av = newambval()) == NULL) |
426 |
+ |
error(SYSTEM, "out of memory in avstore"); |
427 |
+ |
copystruct(av, aval); |
428 |
+ |
av->latick = ambclock; |
429 |
+ |
av->next = NULL; |
430 |
+ |
nambvals++; |
431 |
+ |
return(av); |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
|
435 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
436 |
+ |
|
437 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
438 |
+ |
|
439 |
+ |
|
440 |
|
static |
441 |
< |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
442 |
< |
AMBVAL *aval; |
353 |
< |
register AMBTREE *at; |
354 |
< |
FVECT c0; |
355 |
< |
double s; |
441 |
> |
AMBTREE * |
442 |
> |
newambtree() /* allocate 8 ambient tree structs */ |
443 |
|
{ |
444 |
+ |
register AMBTREE *atp, *upperlim; |
445 |
+ |
|
446 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
447 |
+ |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
448 |
+ |
if (atfreelist == NULL) |
449 |
+ |
return(NULL); |
450 |
+ |
/* link new free list */ |
451 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
452 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
453 |
+ |
atp->kid = atp + 8; |
454 |
+ |
atp->kid = NULL; |
455 |
+ |
} |
456 |
+ |
atp = atfreelist; |
457 |
+ |
atfreelist = atp->kid; |
458 |
+ |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
459 |
+ |
return(atp); |
460 |
+ |
} |
461 |
+ |
|
462 |
+ |
|
463 |
+ |
static |
464 |
+ |
freeambtree(atp) /* free 8 ambient tree structs */ |
465 |
+ |
AMBTREE *atp; |
466 |
+ |
{ |
467 |
+ |
atp->kid = atfreelist; |
468 |
+ |
atfreelist = atp; |
469 |
+ |
} |
470 |
+ |
|
471 |
+ |
|
472 |
+ |
static |
473 |
+ |
avinsert(av) /* insert ambient value in our tree */ |
474 |
+ |
register AMBVAL *av; |
475 |
+ |
{ |
476 |
+ |
register AMBTREE *at; |
477 |
+ |
register AMBVAL *ap; |
478 |
+ |
AMBVAL avh; |
479 |
|
FVECT ck0; |
480 |
+ |
double s; |
481 |
|
int branch; |
359 |
– |
register AMBVAL *av; |
482 |
|
register int i; |
483 |
|
|
484 |
< |
if ((av = newambval()) == NULL) |
485 |
< |
goto memerr; |
486 |
< |
copystruct(av, aval); |
487 |
< |
VCOPY(ck0, c0); |
484 |
> |
if (av->rad <= FTINY) |
485 |
> |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
486 |
> |
at = &atrunk; |
487 |
> |
VCOPY(ck0, thescene.cuorg); |
488 |
> |
s = thescene.cusize; |
489 |
|
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
490 |
|
if (at->kid == NULL) |
491 |
|
if ((at->kid = newambtree()) == NULL) |
492 |
< |
goto memerr; |
492 |
> |
error(SYSTEM, "out of memory in avinsert"); |
493 |
|
s *= 0.5; |
494 |
|
branch = 0; |
495 |
|
for (i = 0; i < 3; i++) |
499 |
|
} |
500 |
|
at = at->kid + branch; |
501 |
|
} |
502 |
< |
av->next = at->alist; |
503 |
< |
at->alist = av; |
504 |
< |
return; |
505 |
< |
memerr: |
506 |
< |
error(SYSTEM, "out of memory in avinsert"); |
502 |
> |
avh.next = at->alist; /* order by increasing level */ |
503 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
504 |
> |
if (ap->next->lvl >= av->lvl) |
505 |
> |
break; |
506 |
> |
av->next = ap->next; |
507 |
> |
ap->next = av; |
508 |
> |
at->alist = avh.next; |
509 |
|
} |
510 |
|
|
511 |
|
|
512 |
< |
#ifdef NIX |
512 |
> |
static |
513 |
> |
unloadatree(at, f) /* unload an ambient value tree */ |
514 |
> |
register AMBTREE *at; |
515 |
> |
int (*f)(); |
516 |
> |
{ |
517 |
> |
register AMBVAL *av; |
518 |
> |
register int i; |
519 |
> |
/* transfer values at this node */ |
520 |
> |
for (av = at->alist; av != NULL; av = at->alist) { |
521 |
> |
at->alist = av->next; |
522 |
> |
(*f)(av); |
523 |
> |
} |
524 |
> |
if (at->kid == NULL) |
525 |
> |
return; |
526 |
> |
for (i = 0; i < 8; i++) /* transfer and free children */ |
527 |
> |
unloadatree(at->kid+i, f); |
528 |
> |
freeambtree(at->kid); |
529 |
> |
at->kid = NULL; |
530 |
> |
} |
531 |
|
|
532 |
< |
int |
533 |
< |
ambsync() /* flush ambient file */ |
532 |
> |
|
533 |
> |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
534 |
> |
static int i_avlist; /* index for lists */ |
535 |
> |
|
536 |
> |
|
537 |
> |
static |
538 |
> |
av2list(av) |
539 |
> |
AMBVAL *av; |
540 |
|
{ |
541 |
< |
if (nunflshed == 0) |
542 |
< |
return(0); |
543 |
< |
nunflshed = 0; |
544 |
< |
return(fflush(ambfp)); |
541 |
> |
#ifdef DEBUG |
542 |
> |
if (i_avlist >= nambvals) |
543 |
> |
error(CONSISTENCY, "too many ambient values in av2list1"); |
544 |
> |
#endif |
545 |
> |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
546 |
> |
i_avlist++; |
547 |
|
} |
548 |
|
|
549 |
+ |
|
550 |
+ |
static int |
551 |
+ |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
552 |
+ |
AMBVAL **avp1, **avp2; |
553 |
+ |
{ |
554 |
+ |
return((**avp2).latick - (**avp1).latick); |
555 |
+ |
} |
556 |
+ |
|
557 |
+ |
|
558 |
+ |
static int |
559 |
+ |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
560 |
+ |
AMBVAL **avp1, **avp2; |
561 |
+ |
{ |
562 |
+ |
return(*avp1 - *avp2); |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
|
566 |
+ |
#ifdef DEBUG |
567 |
+ |
static int |
568 |
+ |
avlmemi(avaddr) /* find list position from address */ |
569 |
+ |
AMBVAL *avaddr; |
570 |
+ |
{ |
571 |
+ |
register AMBVAL **avlpp; |
572 |
+ |
|
573 |
+ |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
574 |
+ |
nambvals, sizeof(AMBVAL *), aposcmp); |
575 |
+ |
if (avlpp == NULL) |
576 |
+ |
error(CONSISTENCY, "address not found in avlmemi"); |
577 |
+ |
return(avlpp - avlist2); |
578 |
+ |
} |
579 |
|
#else |
580 |
+ |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
581 |
+ |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
582 |
+ |
#endif |
583 |
|
|
584 |
+ |
|
585 |
+ |
static |
586 |
+ |
sortambvals(always) /* resort ambient values */ |
587 |
+ |
int always; |
588 |
+ |
{ |
589 |
+ |
AMBTREE oldatrunk; |
590 |
+ |
AMBVAL tav, *tap, *pnext; |
591 |
+ |
register int i, j; |
592 |
+ |
/* see if it's time yet */ |
593 |
+ |
if (!always && (ambclock < lastsort+sortintvl || |
594 |
+ |
nambvals < SORT_THRESH)) |
595 |
+ |
return; |
596 |
+ |
/* |
597 |
+ |
* The idea here is to minimize memory thrashing |
598 |
+ |
* in VM systems by improving reference locality. |
599 |
+ |
* We do this by periodically sorting our stored ambient |
600 |
+ |
* values in memory in order of most recently to least |
601 |
+ |
* recently accessed. This ordering was chosen so that new |
602 |
+ |
* ambient values (which tend to be less important) go into |
603 |
+ |
* higher memory with the infrequently accessed values. |
604 |
+ |
* Since we expect our values to need sorting less |
605 |
+ |
* frequently as the process continues, we double our |
606 |
+ |
* waiting interval after each call. |
607 |
+ |
* This routine is also called by setambacc() with |
608 |
+ |
* the "always" parameter set to 1 so that the ambient |
609 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
610 |
+ |
*/ |
611 |
+ |
if (tracktime) { /* allocate pointer arrays to sort */ |
612 |
+ |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
613 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
614 |
+ |
} else |
615 |
+ |
avlist1 = avlist2 = NULL; |
616 |
+ |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
617 |
+ |
if (avlist1 != NULL) |
618 |
+ |
free((char *)avlist1); |
619 |
+ |
if (always) { /* rebuild without sorting */ |
620 |
+ |
copystruct(&oldatrunk, &atrunk); |
621 |
+ |
atrunk.alist = NULL; |
622 |
+ |
atrunk.kid = NULL; |
623 |
+ |
unloadatree(&oldatrunk, avinsert); |
624 |
+ |
} |
625 |
+ |
} else { /* sort memory by last access time */ |
626 |
+ |
/* |
627 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
628 |
+ |
* We have to sort an array of pointers by MRA and also |
629 |
+ |
* by memory position. We then copy values in "loops" |
630 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
631 |
+ |
* everyone at least twice, and this is an expensive process |
632 |
+ |
* when we're thrashing, which is when we need to do it. |
633 |
+ |
*/ |
634 |
+ |
#ifdef DEBUG |
635 |
+ |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
636 |
+ |
nambvals, ambclock); |
637 |
+ |
eputs(errmsg); |
638 |
+ |
#endif |
639 |
+ |
i_avlist = 0; |
640 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
641 |
+ |
#ifdef DEBUG |
642 |
+ |
if (i_avlist < nambvals) |
643 |
+ |
error(CONSISTENCY, "missing ambient values in sortambvals"); |
644 |
+ |
#endif |
645 |
+ |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
646 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
647 |
+ |
for (i = 0; i < nambvals; i++) { |
648 |
+ |
if (avlist1[i] == NULL) |
649 |
+ |
continue; |
650 |
+ |
tap = avlist2[i]; |
651 |
+ |
copystruct(&tav, tap); |
652 |
+ |
for (j = i; (pnext = avlist1[j]) != tap; |
653 |
+ |
j = avlmemi(pnext)) { |
654 |
+ |
copystruct(avlist2[j], pnext); |
655 |
+ |
avinsert(avlist2[j]); |
656 |
+ |
avlist1[j] = NULL; |
657 |
+ |
} |
658 |
+ |
copystruct(avlist2[j], &tav); |
659 |
+ |
avinsert(avlist2[j]); |
660 |
+ |
avlist1[j] = NULL; |
661 |
+ |
} |
662 |
+ |
free((char *)avlist1); |
663 |
+ |
free((char *)avlist2); |
664 |
+ |
/* compute new sort interval */ |
665 |
+ |
sortintvl = ambclock - lastsort; |
666 |
+ |
if (sortintvl > MAX_SORT_INTVL) |
667 |
+ |
sortintvl = MAX_SORT_INTVL; |
668 |
+ |
else |
669 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
670 |
+ |
#ifdef DEBUG |
671 |
+ |
eputs("done\n"); |
672 |
+ |
#endif |
673 |
+ |
} |
674 |
+ |
if (ambclock >= MAXACLOCK) |
675 |
+ |
ambclock = MAXACLOCK/2; |
676 |
+ |
lastsort = ambclock; |
677 |
+ |
} |
678 |
+ |
|
679 |
+ |
|
680 |
+ |
#ifdef F_SETLKW |
681 |
+ |
|
682 |
+ |
static |
683 |
+ |
aflock(typ) /* lock/unlock ambient file */ |
684 |
+ |
int typ; |
685 |
+ |
{ |
686 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
687 |
+ |
|
688 |
+ |
fls.l_type = typ; |
689 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
690 |
+ |
error(SYSTEM, "cannot (un)lock ambient file"); |
691 |
+ |
} |
692 |
+ |
|
693 |
+ |
|
694 |
|
int |
695 |
|
ambsync() /* synchronize ambient file */ |
696 |
|
{ |
697 |
|
static FILE *ambinp = NULL; |
698 |
|
static long lastpos = -1; |
405 |
– |
struct flock fls; |
699 |
|
long flen; |
700 |
|
AMBVAL avs; |
701 |
|
register int n; |
702 |
|
|
703 |
|
if (nunflshed == 0) |
704 |
|
return(0); |
705 |
< |
/* gain exclusive access */ |
413 |
< |
fls.l_type = F_WRLCK; |
414 |
< |
fls.l_whence = 0; |
415 |
< |
fls.l_start = 0L; |
416 |
< |
fls.l_len = 0L; |
417 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
418 |
< |
error(SYSTEM, "cannot lock ambient file"); |
419 |
< |
if (lastpos < 0) /* initializing */ |
705 |
> |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
706 |
|
goto syncend; |
707 |
+ |
/* gain exclusive access */ |
708 |
+ |
aflock(F_WRLCK); |
709 |
|
/* see if file has grown */ |
710 |
|
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
711 |
< |
error(SYSTEM, "cannot seek on ambient file"); |
711 |
> |
goto seekerr; |
712 |
|
if (n = flen - lastpos) { /* file has grown */ |
713 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
714 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
716 |
|
error(SYSTEM, "fdopen failed in ambsync"); |
717 |
|
} |
718 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
719 |
< |
error(SYSTEM, "fseek failed in ambsync"); |
719 |
> |
goto seekerr; |
720 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
721 |
|
readambval(&avs, ambinp); |
722 |
< |
avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize); |
722 |
> |
avinsert(avstore(&avs)); |
723 |
|
n -= AMBVALSIZ; |
724 |
|
} |
725 |
< |
if (n) /* alignment */ |
726 |
< |
lseek(fileno(ambfp), flen-n, 0); |
725 |
> |
/*** seek always as safety measure |
726 |
> |
if (n) ***/ /* alignment */ |
727 |
> |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
728 |
> |
goto seekerr; |
729 |
|
} |
730 |
+ |
#ifdef DEBUG |
731 |
+ |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
732 |
+ |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
733 |
+ |
ambfp->_ptr - ambfp->_base, |
734 |
+ |
nunflshed*AMBVALSIZ); |
735 |
+ |
error(CONSISTENCY, errmsg); |
736 |
+ |
} |
737 |
+ |
#endif |
738 |
|
syncend: |
739 |
|
n = fflush(ambfp); /* calls write() at last */ |
740 |
< |
lastpos = lseek(fileno(ambfp), 0L, 1); |
741 |
< |
fls.l_type = F_UNLCK; /* release file */ |
742 |
< |
fcntl(fileno(ambfp), F_SETLKW, &fls); |
740 |
> |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
741 |
> |
goto seekerr; |
742 |
> |
aflock(F_UNLCK); /* release file */ |
743 |
|
nunflshed = 0; |
744 |
|
return(n); |
745 |
+ |
seekerr: |
746 |
+ |
error(SYSTEM, "seek failed in ambsync"); |
747 |
+ |
} |
748 |
+ |
|
749 |
+ |
#else |
750 |
+ |
|
751 |
+ |
int |
752 |
+ |
ambsync() /* flush ambient file */ |
753 |
+ |
{ |
754 |
+ |
if (nunflshed == 0) |
755 |
+ |
return(0); |
756 |
+ |
nunflshed = 0; |
757 |
+ |
return(fflush(ambfp)); |
758 |
|
} |
759 |
|
|
760 |
|
#endif |