ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.35
Committed: Tue Nov 21 14:28:22 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +6 -3 lines
Log Message:
improved logic for when to track time on multiprocessors

File Contents

# Content
1 /* Copyright (c) 1995 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * ambient.c - routines dealing with ambient (inter-reflected) component.
9 */
10
11 #include "ray.h"
12
13 #include "octree.h"
14
15 #include "otypes.h"
16
17 #include "ambient.h"
18
19 #include "random.h"
20
21 #ifndef OCTSCALE
22 #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */
23 #endif
24 #ifndef AMBVWT
25 #define AMBVWT 250 /* relative ambient value weight (# calcs) */
26 #endif
27
28 typedef struct ambtree {
29 AMBVAL *alist; /* ambient value list */
30 struct ambtree *kid; /* 8 child nodes */
31 } AMBTREE; /* ambient octree */
32
33 extern CUBE thescene; /* contains space boundaries */
34
35 extern char *shm_boundary; /* memory sharing boundary */
36
37 #define MAXASET 511 /* maximum number of elements in ambient set */
38 OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
39
40 double maxarad; /* maximum ambient radius */
41 double minarad; /* minimum ambient radius */
42
43 static AMBTREE atrunk; /* our ambient trunk node */
44
45 static FILE *ambfp = NULL; /* ambient file pointer */
46 static int nunflshed = 0; /* number of unflushed ambient values */
47
48 #ifndef SORT_THRESH
49 #ifdef BIGMEM
50 #define SORT_THRESH ((9L<<20)/sizeof(AMBVAL))
51 #else
52 #define SORT_THRESH ((3L<<20)/sizeof(AMBVAL))
53 #endif
54 #endif
55 #ifndef SORT_INTVL
56 #define SORT_INTVL (SORT_THRESH*256)
57 #endif
58 #ifndef MAX_SORT_INTVL
59 #define MAX_SORT_INTVL (SORT_INTVL<<4)
60 #endif
61
62 static COLOR avsum = BLKCOLOR; /* computed ambient value sum */
63 static unsigned int nambvals = 0; /* total number of indirect values */
64 static unsigned int nambshare = 0; /* number of values from file */
65 static unsigned long ambclock = 0; /* ambient access clock */
66 static unsigned long lastsort = 0; /* time of last value sort */
67 static long sortintvl = SORT_INTVL; /* time until next sort */
68
69 #define MAXACLOCK (1L<<30) /* clock turnover value */
70 /*
71 * Track access times unless we are sharing ambient values
72 * through memory on a multiprocessor, when we want to avoid
73 * claiming our own memory (copy on write). Go ahead anyway
74 * if more than two thirds of our values are unshared.
75 */
76 #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare)
77
78 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
79
80 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
81
82 extern long ftell(), lseek();
83 static int initambfile(), avsave(), avinsert(), sortambvals();
84 static AMBVAL *avstore();
85 #ifdef F_SETLKW
86 static aflock();
87 #endif
88
89
90 setambres(ar) /* set ambient resolution */
91 int ar;
92 {
93 ambres = ar < 0 ? 0 : ar; /* may be done already */
94 /* set min & max radii */
95 if (ar <= 0) {
96 minarad = 0;
97 maxarad = thescene.cusize / 2.0;
98 } else {
99 minarad = thescene.cusize / ar;
100 maxarad = 64 * minarad; /* heuristic */
101 if (maxarad > thescene.cusize / 2.0)
102 maxarad = thescene.cusize / 2.0;
103 }
104 if (minarad <= FTINY)
105 minarad = 10*FTINY;
106 if (maxarad <= minarad)
107 maxarad = 64 * minarad;
108 }
109
110
111 setambacc(newa) /* set ambient accuracy */
112 double newa;
113 {
114 double ambdiff;
115
116 if (newa < 0.0)
117 newa = 0.0;
118 ambdiff = fabs(newa - ambacc);
119 if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
120 sortambvals(1); /* rebuild tree */
121 }
122
123
124 setambient(afile) /* initialize calculation */
125 char *afile;
126 {
127 long headlen;
128 AMBVAL amb;
129 /* init ambient limits */
130 setambres(ambres);
131 setambacc(ambacc);
132 if (afile == NULL)
133 return;
134 if (ambacc <= FTINY) {
135 sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
136 afile);
137 error(WARNING, errmsg);
138 return;
139 }
140 /* open ambient file */
141 if ((ambfp = fopen(afile, "r+")) != NULL) {
142 initambfile(0);
143 headlen = ftell(ambfp);
144 while (readambval(&amb, ambfp))
145 avinsert(avstore(&amb));
146 /* align */
147 fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
148 nambshare = nambvals;
149 } else if ((ambfp = fopen(afile, "w+")) != NULL)
150 initambfile(1);
151 else {
152 sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
153 error(SYSTEM, errmsg);
154 }
155 nunflshed++; /* lie */
156 ambsync();
157 }
158
159
160 ambnotify(obj) /* record new modifier */
161 OBJECT obj;
162 {
163 static int hitlimit = 0;
164 register OBJREC *o = objptr(obj);
165 register char **amblp;
166
167 if (hitlimit || !ismodifier(o->otype))
168 return;
169 for (amblp = amblist; *amblp != NULL; amblp++)
170 if (!strcmp(o->oname, *amblp)) {
171 if (ambset[0] >= MAXASET) {
172 error(WARNING, "too many modifiers in ambient list");
173 hitlimit++;
174 return; /* should this be fatal? */
175 }
176 insertelem(ambset, obj);
177 return;
178 }
179 }
180
181
182 ambient(acol, r, nrm) /* compute ambient component for ray */
183 COLOR acol;
184 register RAY *r;
185 FVECT nrm;
186 {
187 static int rdepth = 0; /* ambient recursion */
188 double d;
189
190 if (ambdiv <= 0) /* no ambient calculation */
191 goto dumbamb;
192 /* check number of bounces */
193 if (rdepth >= ambounce)
194 goto dumbamb;
195 /* check ambient list */
196 if (ambincl != -1 && r->ro != NULL &&
197 ambincl != inset(ambset, r->ro->omod))
198 goto dumbamb;
199
200 if (ambacc <= FTINY) { /* no ambient storage */
201 rdepth++;
202 d = doambient(acol, r, r->rweight, NULL, NULL);
203 rdepth--;
204 if (d <= FTINY)
205 goto dumbamb;
206 return;
207 }
208 /* resort memory? */
209 sortambvals(0);
210 /* get ambient value */
211 setcolor(acol, 0.0, 0.0, 0.0);
212 d = sumambient(acol, r, nrm, rdepth,
213 &atrunk, thescene.cuorg, thescene.cusize);
214 if (d > FTINY) {
215 scalecolor(acol, 1.0/d);
216 return;
217 }
218 rdepth++; /* need to cache new value */
219 d = makeambient(acol, r, nrm, rdepth-1);
220 rdepth--;
221 if (d > FTINY)
222 return;
223 dumbamb: /* return global value */
224 copycolor(acol, ambval);
225 #if AMBVWT
226 if (nambvals == 0)
227 return;
228 scalecolor(acol, (double)AMBVWT);
229 addcolor(acol, avsum); /* average in computations */
230 d = 1.0/(AMBVWT+nambvals);
231 scalecolor(acol, d);
232 #endif
233 }
234
235
236 double
237 sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */
238 COLOR acol;
239 register RAY *r;
240 FVECT rn;
241 int al;
242 AMBTREE *at;
243 FVECT c0;
244 double s;
245 {
246 double d, e1, e2, wt, wsum;
247 COLOR ct;
248 FVECT ck0;
249 int i;
250 register int j;
251 register AMBVAL *av;
252
253 wsum = 0.0;
254 /* do this node */
255 for (av = at->alist; av != NULL; av = av->next) {
256 if (tracktime)
257 av->latick = ambclock++;
258 /*
259 * Ambient level test.
260 */
261 if (av->lvl > al) /* list sorted, so this works */
262 break;
263 if (av->weight < r->rweight-FTINY)
264 continue;
265 /*
266 * Ambient radius test.
267 */
268 d = av->pos[0] - r->rop[0];
269 e1 = d * d;
270 d = av->pos[1] - r->rop[1];
271 e1 += d * d;
272 d = av->pos[2] - r->rop[2];
273 e1 += d * d;
274 e1 /= av->rad * av->rad;
275 if (e1 > ambacc*ambacc*1.21)
276 continue;
277 /*
278 * Normal direction test.
279 */
280 e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
281 if (e2 < 0.0) e2 = 0.0;
282 if (e1 + e2 > ambacc*ambacc*1.21)
283 continue;
284 /*
285 * Ray behind test.
286 */
287 d = 0.0;
288 for (j = 0; j < 3; j++)
289 d += (r->rop[j] - av->pos[j]) *
290 (av->dir[j] + r->ron[j]);
291 if (d*0.5 < -minarad*ambacc-.001)
292 continue;
293 /*
294 * Jittering final test reduces image artifacts.
295 */
296 wt = sqrt(e1) + sqrt(e2);
297 if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
298 continue;
299 if (wt <= 1e-3)
300 wt = 1e3;
301 else
302 wt = 1.0 / wt;
303 wsum += wt;
304 extambient(ct, av, r->rop, rn);
305 scalecolor(ct, wt);
306 addcolor(acol, ct);
307 }
308 if (at->kid == NULL)
309 return(wsum);
310 /* do children */
311 s *= 0.5;
312 for (i = 0; i < 8; i++) {
313 for (j = 0; j < 3; j++) {
314 ck0[j] = c0[j];
315 if (1<<j & i)
316 ck0[j] += s;
317 if (r->rop[j] < ck0[j] - OCTSCALE*s)
318 break;
319 if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
320 break;
321 }
322 if (j == 3)
323 wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
324 }
325 return(wsum);
326 }
327
328
329 double
330 makeambient(acol, r, rn, al) /* make a new ambient value */
331 COLOR acol;
332 register RAY *r;
333 FVECT rn;
334 int al;
335 {
336 AMBVAL amb;
337 FVECT gp, gd;
338 /* compute weight */
339 amb.weight = pow(AVGREFL, (double)al);
340 if (r->rweight < 0.1*amb.weight) /* heuristic */
341 amb.weight = r->rweight;
342 /* compute ambient */
343 amb.rad = doambient(acol, r, amb.weight, gp, gd);
344 if (amb.rad <= FTINY)
345 return(0.0);
346 /* store it */
347 VCOPY(amb.pos, r->rop);
348 VCOPY(amb.dir, r->ron);
349 amb.lvl = al;
350 copycolor(amb.val, acol);
351 VCOPY(amb.gpos, gp);
352 VCOPY(amb.gdir, gd);
353 /* insert into tree */
354 avsave(&amb); /* and save to file */
355 if (rn != r->ron)
356 extambient(acol, &amb, r->rop, rn); /* texture */
357 return(amb.rad);
358 }
359
360
361 extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
362 COLOR cr;
363 register AMBVAL *ap;
364 FVECT pv, nv;
365 {
366 FVECT v1, v2;
367 register int i;
368 double d;
369
370 d = 1.0; /* zeroeth order */
371 /* gradient due to translation */
372 for (i = 0; i < 3; i++)
373 d += ap->gpos[i]*(pv[i]-ap->pos[i]);
374 /* gradient due to rotation */
375 VCOPY(v1, ap->dir);
376 fcross(v2, v1, nv);
377 d += DOT(ap->gdir, v2);
378 if (d <= 0.0) {
379 setcolor(cr, 0.0, 0.0, 0.0);
380 return;
381 }
382 copycolor(cr, ap->val);
383 scalecolor(cr, d);
384 }
385
386
387 static
388 initambfile(creat) /* initialize ambient file */
389 int creat;
390 {
391 extern char *progname, *octname, VersionID[];
392
393 #ifdef F_SETLKW
394 aflock(creat ? F_WRLCK : F_RDLCK);
395 #endif
396 #ifdef MSDOS
397 setmode(fileno(ambfp), O_BINARY);
398 #endif
399 setbuf(ambfp, bmalloc(BUFSIZ+8));
400 if (creat) { /* new file */
401 newheader("RADIANCE", ambfp);
402 fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
403 progname, colval(ambval,RED),
404 colval(ambval,GRN), colval(ambval,BLU),
405 ambounce, ambacc);
406 fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
407 ambdiv, ambssamp, ambres,
408 octname==NULL ? "" : octname);
409 fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
410 fputformat(AMBFMT, ambfp);
411 putc('\n', ambfp);
412 putambmagic(ambfp);
413 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
414 error(USER, "bad ambient file");
415 }
416
417
418 static
419 avsave(av) /* insert and save an ambient value */
420 AMBVAL *av;
421 {
422 avinsert(avstore(av));
423 if (ambfp == NULL)
424 return;
425 if (writambval(av, ambfp) < 0)
426 goto writerr;
427 if (++nunflshed >= AMBFLUSH)
428 if (ambsync() == EOF)
429 goto writerr;
430 return;
431 writerr:
432 error(SYSTEM, "error writing ambient file");
433 }
434
435
436 static AMBVAL *
437 avstore(aval) /* allocate memory and store aval */
438 register AMBVAL *aval;
439 {
440 register AMBVAL *av;
441
442 if ((av = newambval()) == NULL)
443 error(SYSTEM, "out of memory in avstore");
444 copystruct(av, aval);
445 av->latick = ambclock;
446 av->next = NULL;
447 addcolor(avsum, av->val); /* add to sum for averaging */
448 nambvals++;
449 return(av);
450 }
451
452
453 #define ATALLOCSZ 512 /* #/8 trees to allocate at once */
454
455 static AMBTREE *atfreelist = NULL; /* free ambient tree structures */
456
457
458 static
459 AMBTREE *
460 newambtree() /* allocate 8 ambient tree structs */
461 {
462 register AMBTREE *atp, *upperlim;
463
464 if (atfreelist == NULL) { /* get more nodes */
465 atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
466 if (atfreelist == NULL)
467 return(NULL);
468 /* link new free list */
469 upperlim = atfreelist + 8*(ATALLOCSZ-1);
470 for (atp = atfreelist; atp < upperlim; atp += 8)
471 atp->kid = atp + 8;
472 atp->kid = NULL;
473 }
474 atp = atfreelist;
475 atfreelist = atp->kid;
476 bzero((char *)atp, 8*sizeof(AMBTREE));
477 return(atp);
478 }
479
480
481 static
482 freeambtree(atp) /* free 8 ambient tree structs */
483 AMBTREE *atp;
484 {
485 atp->kid = atfreelist;
486 atfreelist = atp;
487 }
488
489
490 static
491 avinsert(av) /* insert ambient value in our tree */
492 register AMBVAL *av;
493 {
494 register AMBTREE *at;
495 register AMBVAL *ap;
496 AMBVAL avh;
497 FVECT ck0;
498 double s;
499 int branch;
500 register int i;
501
502 if (av->rad <= FTINY)
503 error(CONSISTENCY, "zero ambient radius in avinsert");
504 at = &atrunk;
505 VCOPY(ck0, thescene.cuorg);
506 s = thescene.cusize;
507 while (s*(OCTSCALE/2) > av->rad*ambacc) {
508 if (at->kid == NULL)
509 if ((at->kid = newambtree()) == NULL)
510 error(SYSTEM, "out of memory in avinsert");
511 s *= 0.5;
512 branch = 0;
513 for (i = 0; i < 3; i++)
514 if (av->pos[i] > ck0[i] + s) {
515 ck0[i] += s;
516 branch |= 1 << i;
517 }
518 at = at->kid + branch;
519 }
520 avh.next = at->alist; /* order by increasing level */
521 for (ap = &avh; ap->next != NULL; ap = ap->next)
522 if (ap->next->lvl >= av->lvl)
523 break;
524 av->next = ap->next;
525 ap->next = av;
526 at->alist = avh.next;
527 }
528
529
530 static
531 unloadatree(at, f) /* unload an ambient value tree */
532 register AMBTREE *at;
533 int (*f)();
534 {
535 register AMBVAL *av;
536 register int i;
537 /* transfer values at this node */
538 for (av = at->alist; av != NULL; av = at->alist) {
539 at->alist = av->next;
540 (*f)(av);
541 }
542 if (at->kid == NULL)
543 return;
544 for (i = 0; i < 8; i++) /* transfer and free children */
545 unloadatree(at->kid+i, f);
546 freeambtree(at->kid);
547 at->kid = NULL;
548 }
549
550
551 static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */
552 static int i_avlist; /* index for lists */
553
554
555 static
556 av2list(av)
557 AMBVAL *av;
558 {
559 #ifdef DEBUG
560 if (i_avlist >= nambvals)
561 error(CONSISTENCY, "too many ambient values in av2list1");
562 #endif
563 avlist1[i_avlist] = avlist2[i_avlist] = av;
564 i_avlist++;
565 }
566
567
568 static int
569 alatcmp(avp1, avp2) /* compare ambient values for MRA */
570 AMBVAL **avp1, **avp2;
571 {
572 return((**avp2).latick - (**avp1).latick);
573 }
574
575
576 static int
577 aposcmp(avp1, avp2) /* compare ambient value positions */
578 AMBVAL **avp1, **avp2;
579 {
580 return(*avp1 - *avp2);
581 }
582
583
584 #ifdef DEBUG
585 static int
586 avlmemi(avaddr) /* find list position from address */
587 AMBVAL *avaddr;
588 {
589 register AMBVAL **avlpp;
590
591 avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
592 nambvals, sizeof(AMBVAL *), aposcmp);
593 if (avlpp == NULL)
594 error(CONSISTENCY, "address not found in avlmemi");
595 return(avlpp - avlist2);
596 }
597 #else
598 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
599 nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
600 #endif
601
602
603 static
604 sortambvals(always) /* resort ambient values */
605 int always;
606 {
607 AMBTREE oldatrunk;
608 AMBVAL tav, *tap, *pnext;
609 register int i, j;
610 /* see if it's time yet */
611 if (!always && (ambclock < lastsort+sortintvl ||
612 nambvals < SORT_THRESH))
613 return;
614 /*
615 * The idea here is to minimize memory thrashing
616 * in VM systems by improving reference locality.
617 * We do this by periodically sorting our stored ambient
618 * values in memory in order of most recently to least
619 * recently accessed. This ordering was chosen so that new
620 * ambient values (which tend to be less important) go into
621 * higher memory with the infrequently accessed values.
622 * Since we expect our values to need sorting less
623 * frequently as the process continues, we double our
624 * waiting interval after each call.
625 * This routine is also called by setambacc() with
626 * the "always" parameter set to 1 so that the ambient
627 * tree will be rebuilt with the new accuracy parameter.
628 */
629 if (tracktime) { /* allocate pointer arrays to sort */
630 avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
631 avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
632 } else
633 avlist1 = avlist2 = NULL;
634 if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */
635 if (avlist1 != NULL)
636 free((char *)avlist1);
637 if (always) { /* rebuild without sorting */
638 copystruct(&oldatrunk, &atrunk);
639 atrunk.alist = NULL;
640 atrunk.kid = NULL;
641 unloadatree(&oldatrunk, avinsert);
642 }
643 } else { /* sort memory by last access time */
644 /*
645 * Sorting memory is tricky because it isn't contiguous.
646 * We have to sort an array of pointers by MRA and also
647 * by memory position. We then copy values in "loops"
648 * to minimize memory hits. Nevertheless, we will visit
649 * everyone at least twice, and this is an expensive process
650 * when we're thrashing, which is when we need to do it.
651 */
652 #ifdef DEBUG
653 sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
654 nambvals, ambclock);
655 eputs(errmsg);
656 #endif
657 i_avlist = 0;
658 unloadatree(&atrunk, av2list); /* empty current tree */
659 #ifdef DEBUG
660 if (i_avlist < nambvals)
661 error(CONSISTENCY, "missing ambient values in sortambvals");
662 #endif
663 qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
664 qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
665 for (i = 0; i < nambvals; i++) {
666 if (avlist1[i] == NULL)
667 continue;
668 tap = avlist2[i];
669 copystruct(&tav, tap);
670 for (j = i; (pnext = avlist1[j]) != tap;
671 j = avlmemi(pnext)) {
672 copystruct(avlist2[j], pnext);
673 avinsert(avlist2[j]);
674 avlist1[j] = NULL;
675 }
676 copystruct(avlist2[j], &tav);
677 avinsert(avlist2[j]);
678 avlist1[j] = NULL;
679 }
680 free((char *)avlist1);
681 free((char *)avlist2);
682 /* compute new sort interval */
683 sortintvl = ambclock - lastsort;
684 if (sortintvl >= MAX_SORT_INTVL/2)
685 sortintvl = MAX_SORT_INTVL;
686 else
687 sortintvl <<= 1; /* wait twice as long next */
688 #ifdef DEBUG
689 eputs("done\n");
690 #endif
691 }
692 if (ambclock >= MAXACLOCK)
693 ambclock = MAXACLOCK/2;
694 lastsort = ambclock;
695 }
696
697
698 #ifdef F_SETLKW
699
700 static
701 aflock(typ) /* lock/unlock ambient file */
702 int typ;
703 {
704 static struct flock fls; /* static so initialized to zeroes */
705
706 fls.l_type = typ;
707 if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
708 error(SYSTEM, "cannot (un)lock ambient file");
709 }
710
711
712 int
713 ambsync() /* synchronize ambient file */
714 {
715 static FILE *ambinp = NULL;
716 static long lastpos = -1;
717 long flen;
718 AMBVAL avs;
719 register int n;
720
721 if (nunflshed == 0)
722 return(0);
723 if (lastpos < 0) /* initializing (locked in initambfile) */
724 goto syncend;
725 /* gain exclusive access */
726 aflock(F_WRLCK);
727 /* see if file has grown */
728 if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
729 goto seekerr;
730 if (n = flen - lastpos) { /* file has grown */
731 if (ambinp == NULL) { /* use duplicate filedes */
732 ambinp = fdopen(dup(fileno(ambfp)), "r");
733 if (ambinp == NULL)
734 error(SYSTEM, "fdopen failed in ambsync");
735 }
736 if (fseek(ambinp, lastpos, 0) < 0)
737 goto seekerr;
738 while (n >= AMBVALSIZ) { /* load contributed values */
739 readambval(&avs, ambinp);
740 avinsert(avstore(&avs));
741 n -= AMBVALSIZ;
742 }
743 /*** seek always as safety measure
744 if (n) ***/ /* alignment */
745 if (lseek(fileno(ambfp), flen-n, 0) < 0)
746 goto seekerr;
747 }
748 #ifdef DEBUG
749 if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
750 sprintf(errmsg, "ambient file buffer at %d rather than %d",
751 ambfp->_ptr - ambfp->_base,
752 nunflshed*AMBVALSIZ);
753 error(CONSISTENCY, errmsg);
754 }
755 #endif
756 syncend:
757 n = fflush(ambfp); /* calls write() at last */
758 if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
759 goto seekerr;
760 aflock(F_UNLCK); /* release file */
761 nunflshed = 0;
762 return(n);
763 seekerr:
764 error(SYSTEM, "seek failed in ambsync");
765 }
766
767 #else
768
769 int
770 ambsync() /* flush ambient file */
771 {
772 if (nunflshed == 0)
773 return(0);
774 nunflshed = 0;
775 return(fflush(ambfp));
776 }
777
778 #endif