ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.49 by greg, Thu May 15 05:13:35 2003 UTC vs.
Revision 2.70 by greg, Thu Sep 22 02:15:56 2011 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 < #include  "ray.h"
12 > #include <string.h>
13  
14 + #include  "platform.h"
15 + #include  "ray.h"
16   #include  "otypes.h"
17 <
17 > #include  "resolu.h"
18   #include  "ambient.h"
17
19   #include  "random.h"
20  
21   #ifndef  OCTSCALE
# Line 23 | Line 24 | static const char      RCSid[] = "$Id$";
24  
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 75 | Line 78 | static long  lastpos = -1;             /* last flush position */
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79   #define  freeav(av)     free((void *)av);
80  
81 < static void  initambfile(), avsave(), avinsert(), sortambvals(), unloadatree();
82 < static int  avlmemi();
83 < static AMBVAL  *avstore();
81 > static void initambfile(int creat);
82 > static void avsave(AMBVAL *av);
83 > static AMBVAL *avstore(AMBVAL  *aval);
84 > static AMBTREE *newambtree(void);
85 > static void freeambtree(AMBTREE  *atp);
86 >
87 > typedef void unloadtf_t(void *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
91 >
92 > static int aposcmp(const void *avp1, const void *avp2);
93 > static int avlmemi(AMBVAL *avaddr);
94 > static void sortambvals(int always);
95 >
96   #ifdef  F_SETLKW
97 < static void  aflock();
97 > static void aflock(int  typ);
98   #endif
99  
100  
101 < void
102 < setambres(ar)                           /* set ambient resolution */
103 < int  ar;
101 > extern void
102 > setambres(                              /* set ambient resolution */
103 >        int  ar
104 > )
105   {
106          ambres = ar < 0 ? 0 : ar;               /* may be done already */
107                                                  /* set min & max radii */
# Line 105 | Line 121 | int  ar;
121   }
122  
123  
124 < void
125 < setambacc(newa)                         /* set ambient accuracy */
126 < double  newa;
124 > extern void
125 > setambacc(                              /* set ambient accuracy */
126 >        double  newa
127 > )
128   {
129          double  ambdiff;
130  
# Line 119 | Line 136 | double  newa;
136   }
137  
138  
139 < void
140 < setambient()                            /* initialize calculation */
139 > extern void
140 > setambient(void)                                /* initialize calculation */
141   {
142          int     readonly = 0;
143 <        long  pos, flen;
143 >        long    flen;
144          AMBVAL  amb;
145                                                  /* make sure we're fresh */
146          ambdone();
# Line 143 | Line 160 | setambient()                           /* initialize calculation */
160                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161          if (ambfp != NULL) {
162                  initambfile(0);                 /* file exists */
163 <                pos = ftell(ambfp);
163 >                lastpos = ftell(ambfp);
164                  while (readambval(&amb, ambfp))
165                          avinsert(avstore(&amb));
166                  nambshare = nambvals;           /* share loaded values */
# Line 157 | Line 174 | setambient()                           /* initialize calculation */
174                          return;                 /* avoid ambsync() */
175                  }
176                                                  /* align file pointer */
177 <                pos += (long)nambvals*AMBVALSIZ;
178 <                flen = lseek(fileno(ambfp), (off_t)0L, 2);
179 <                if (flen != pos) {
177 >                lastpos += (long)nambvals*AMBVALSIZ;
178 >                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179 >                if (flen != lastpos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182 <                                        (flen - pos)/AMBVALSIZ);
182 >                                        (flen - lastpos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184 <                        fseek(ambfp, pos, 0);
185 <                        ftruncate(fileno(ambfp), (off_t)pos);
184 >                        fseek(ambfp, lastpos, SEEK_SET);
185 > #ifndef _WIN32 /* XXX we need a replacement for that one */
186 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
187 > #endif
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
191 +                fflush(ambfp);
192 +                lastpos = ftell(ambfp);
193          } else {
194                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
197 <        nunflshed++;    /* lie */
198 <        ambsync();
197 > #ifdef  F_SETLKW
198 >        aflock(F_UNLCK);                        /* release file */
199 > #endif
200   }
201  
202  
203 < void
204 < ambdone()                       /* close ambient file and free memory */
203 > extern void
204 > ambdone(void)                   /* close ambient file and free memory */
205   {
206          if (ambfp != NULL) {            /* close ambient file */
207                  ambsync();
# Line 204 | Line 226 | ambdone()                      /* close ambient file and free memory */
226   }
227  
228  
229 < void
230 < ambnotify(obj)                  /* record new modifier */
231 < OBJECT  obj;
229 > extern void
230 > ambnotify(                      /* record new modifier */
231 >        OBJECT  obj
232 > )
233   {
234          static int  hitlimit = 0;
235 <        register OBJREC  *o;
236 <        register char  **amblp;
235 >        OBJREC   *o;
236 >        char  **amblp;
237  
238          if (obj == OVOID) {             /* starting over */
239                  ambset[0] = 0;
# Line 233 | Line 256 | OBJECT obj;
256   }
257  
258  
259 < void
260 < ambient(acol, r, nrm)           /* compute ambient component for ray */
261 < COLOR  acol;
262 < register RAY  *r;
263 < FVECT  nrm;
259 > extern void
260 > multambient(            /* compute ambient component & multiply by coef. */
261 >        COLOR  aval,
262 >        RAY  *r,
263 >        FVECT  nrm
264 > )
265   {
266          static int  rdepth = 0;                 /* ambient recursion */
267 +        COLOR   acol;
268          double  d, l;
269  
270          if (ambdiv <= 0)                        /* no ambient calculation */
# Line 253 | Line 278 | FVECT  nrm;
278                  goto dumbamb;
279  
280          if (ambacc <= FTINY) {                  /* no ambient storage */
281 +                copycolor(acol, aval);
282                  rdepth++;
283                  d = doambient(acol, r, r->rweight, NULL, NULL);
284                  rdepth--;
285                  if (d <= FTINY)
286                          goto dumbamb;
287 +                copycolor(aval, acol);
288                  return;
289          }
290  
291          if (tracktime)                          /* sort to minimize thrashing */
292                  sortambvals(0);
293 <                                                /* get ambient value */
293 >                                                /* interpolate ambient value */
294          setcolor(acol, 0.0, 0.0, 0.0);
295          d = sumambient(acol, r, nrm, rdepth,
296                          &atrunk, thescene.cuorg, thescene.cusize);
297          if (d > FTINY) {
298 <                scalecolor(acol, 1.0/d);
298 >                d = 1.0/d;
299 >                scalecolor(acol, d);
300 >                multcolor(aval, acol);
301                  return;
302          }
303          rdepth++;                               /* need to cache new value */
304          d = makeambient(acol, r, nrm, rdepth-1);
305          rdepth--;
306 <        if (d > FTINY)
306 >        if (d > FTINY) {
307 >                multcolor(aval, acol);          /* got new value */
308                  return;
309 +        }
310   dumbamb:                                        /* return global value */
311 <        copycolor(acol, ambval);
312 <        if (ambvwt <= 0 | navsum == 0)
311 >        if ((ambvwt <= 0) | (navsum == 0)) {
312 >                multcolor(aval, ambval);
313                  return;
314 +        }
315          l = bright(ambval);                     /* average in computations */
316          if (l > FTINY) {
317                  d = (log(l)*(double)ambvwt + avsum) /
318                                  (double)(ambvwt + navsum);
319                  d = exp(d) / l;
320 <                scalecolor(acol, d);            /* apply color of ambval */
320 >                scalecolor(aval, d);
321 >                multcolor(aval, ambval);        /* apply color of ambval */
322          } else {
323                  d = exp( avsum / (double)navsum );
324 <                setcolor(acol, d, d, d);        /* neutral color */
324 >                scalecolor(aval, d);            /* neutral color */
325          }
326   }
327  
328  
329 < double
330 < sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
331 < COLOR  acol;
332 < register RAY  *r;
333 < FVECT  rn;
334 < int  al;
335 < AMBTREE  *at;
336 < FVECT  c0;
337 < double  s;
329 > extern double
330 > sumambient(     /* get interpolated ambient value */
331 >        COLOR  acol,
332 >        RAY  *r,
333 >        FVECT  rn,
334 >        int  al,
335 >        AMBTREE  *at,
336 >        FVECT  c0,
337 >        double  s
338 > )
339   {
340          double  d, e1, e2, wt, wsum;
341          COLOR  ct;
342          FVECT  ck0;
343          int  i;
344 <        register int  j;
345 <        register AMBVAL  *av;
344 >        int  j;
345 >        AMBVAL   *av;
346  
347          wsum = 0.0;
348                                          /* do this node */
# Line 321 | Line 355 | double s;
355                   */
356                  if (av->lvl > al)       /* list sorted, so this works */
357                          break;
358 <                if (av->weight < r->rweight-FTINY)
358 >                if (av->weight < 0.9*r->rweight)
359                          continue;
360                  /*
361                   *  Ambient radius test.
362                   */
363 <                d = av->pos[0] - r->rop[0];
364 <                e1 = d * d;
331 <                d = av->pos[1] - r->rop[1];
332 <                e1 += d * d;
333 <                d = av->pos[2] - r->rop[2];
334 <                e1 += d * d;
335 <                e1 /= av->rad * av->rad;
363 >                VSUB(ck0, av->pos, r->rop);
364 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
365                  if (e1 > ambacc*ambacc*1.21)
366                          continue;
367                  /*
# Line 349 | Line 378 | double s;
378                          }
379                  }
380                  e2 = (1.0 - d) * r->rweight;
381 <                if (e2 < 0.0) e2 = 0.0;
382 <                if (e1 + e2 > ambacc*ambacc*1.21)
381 >                if (e2 < 0.0)
382 >                        e2 = 0.0;
383 >                else if (e1 + e2 > ambacc*ambacc*1.21)
384                          continue;
385                  /*
386                   *  Ray behind test.
# Line 400 | Line 430 | double s;
430                                  break;
431                  }
432                  if (j == 3)
433 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
433 >                        wsum += sumambient(acol, r, rn, al,
434 >                                                at->kid+i, ck0, s);
435          }
436          return(wsum);
437   }
438  
439  
440 < double
441 < makeambient(acol, r, rn, al)    /* make a new ambient value */
442 < COLOR  acol;
443 < register RAY  *r;
444 < FVECT  rn;
445 < int  al;
440 > extern double
441 > makeambient(            /* make a new ambient value for storage */
442 >        COLOR  acol,
443 >        RAY  *r,
444 >        FVECT  rn,
445 >        int  al
446 > )
447   {
448          AMBVAL  amb;
449          FVECT   gp, gd;
450 <                                                /* compute weight */
451 <        amb.weight = pow(AVGREFL, (double)al);
452 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
453 <                amb.weight = r->rweight;
450 >        int     i;
451 >
452 >        amb.weight = 1.0;                       /* compute weight */
453 >        for (i = al; i-- > 0; )
454 >                amb.weight *= AVGREFL;
455 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
456 >                amb.weight = 1.25*r->rweight;
457 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
458                                                  /* compute ambient */
459          amb.rad = doambient(acol, r, amb.weight, gp, gd);
460 <        if (amb.rad <= FTINY)
460 >        if (amb.rad <= FTINY) {
461 >                setcolor(acol, 0.0, 0.0, 0.0);
462                  return(0.0);
463 <                                                /* store it */
463 >        }
464 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
465 >                                                /* store value */
466          VCOPY(amb.pos, r->rop);
467          VCOPY(amb.dir, r->ron);
468          amb.lvl = al;
# Line 438 | Line 477 | int  al;
477   }
478  
479  
480 < void
481 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
482 < COLOR  cr;
483 < register AMBVAL  *ap;
484 < FVECT  pv, nv;
480 > extern void
481 > extambient(             /* extrapolate value at pv, nv */
482 >        COLOR  cr,
483 >        AMBVAL   *ap,
484 >        FVECT  pv,
485 >        FVECT  nv
486 > )
487   {
488          FVECT  v1;
489 <        register int  i;
489 >        int  i;
490          double  d;
491  
492          d = 1.0;                        /* zeroeth order */
# Line 465 | Line 506 | FVECT  pv, nv;
506  
507  
508   static void
509 < initambfile(creat)              /* initialize ambient file */
510 < int  creat;
509 > initambfile(            /* initialize ambient file */
510 >        int  cre8
511 > )
512   {
513          extern char  *progname, *octname;
514          static char  *mybuf = NULL;
515  
516   #ifdef  F_SETLKW
517 <        aflock(creat ? F_WRLCK : F_RDLCK);
517 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
518   #endif
519 < #ifdef MSDOS
478 <        setmode(fileno(ambfp), O_BINARY);
479 < #endif
519 >        SET_FILE_BINARY(ambfp);
520          if (mybuf == NULL)
521                  mybuf = (char *)bmalloc(BUFSIZ+8);
522          setbuf(ambfp, mybuf);
523 <        if (creat) {                    /* new file */
523 >        if (cre8) {                     /* new file */
524                  newheader("RADIANCE", ambfp);
525                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
526                                  progname, colval(ambval,RED),
# Line 489 | Line 529 | int  creat;
529                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
530                                  ambdiv, ambssamp, ambres);
531                  if (octname != NULL)
532 <                        printargs(1, &octname, ambfp);
533 <                else
494 <                        fputc('\n', ambfp);
532 >                        fputs(octname, ambfp);
533 >                fputc('\n', ambfp);
534                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
535                  fputnow(ambfp);
536                  fputformat(AMBFMT, ambfp);
537 <                putc('\n', ambfp);
537 >                fputc('\n', ambfp);
538                  putambmagic(ambfp);
539          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
540                  error(USER, "bad ambient file");
# Line 503 | Line 542 | int  creat;
542  
543  
544   static void
545 < avsave(av)                              /* insert and save an ambient value */
546 < AMBVAL  *av;
545 > avsave(                         /* insert and save an ambient value */
546 >        AMBVAL  *av
547 > )
548   {
549          avinsert(avstore(av));
550          if (ambfp == NULL)
# Line 521 | Line 561 | writerr:
561  
562  
563   static AMBVAL *
564 < avstore(aval)                           /* allocate memory and store aval */
565 < register AMBVAL  *aval;
564 > avstore(                                /* allocate memory and store aval */
565 >        AMBVAL  *aval
566 > )
567   {
568 <        register AMBVAL  *av;
568 >        AMBVAL  *av;
569          double  d;
570  
571          if ((av = newambval()) == NULL)
572                  error(SYSTEM, "out of memory in avstore");
573 <        copystruct(av, aval);
573 >        *av = *aval;
574          av->latick = ambclock;
575          av->next = NULL;
576          nambvals++;
# Line 548 | Line 589 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
589  
590  
591   static AMBTREE *
592 < newambtree()                            /* allocate 8 ambient tree structs */
592 > newambtree(void)                                /* allocate 8 ambient tree structs */
593   {
594 <        register AMBTREE  *atp, *upperlim;
594 >        AMBTREE  *atp, *upperlim;
595  
596          if (atfreelist == NULL) {       /* get more nodes */
597                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 564 | Line 605 | newambtree()                           /* allocate 8 ambient tree structs */
605          }
606          atp = atfreelist;
607          atfreelist = atp->kid;
608 <        bzero((char *)atp, 8*sizeof(AMBTREE));
608 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
609          return(atp);
610   }
611  
612  
613   static void
614 < freeambtree(atp)                        /* free 8 ambient tree structs */
615 < AMBTREE  *atp;
614 > freeambtree(                    /* free 8 ambient tree structs */
615 >        AMBTREE  *atp
616 > )
617   {
618          atp->kid = atfreelist;
619          atfreelist = atp;
# Line 579 | Line 621 | AMBTREE  *atp;
621  
622  
623   static void
624 < avinsert(av)                            /* insert ambient value in our tree */
625 < register AMBVAL  *av;
624 > avinsert(                               /* insert ambient value in our tree */
625 >        void *av
626 > )
627   {
628 <        register AMBTREE  *at;
629 <        register AMBVAL  *ap;
628 >        AMBTREE  *at;
629 >        AMBVAL  *ap;
630          AMBVAL  avh;
631          FVECT  ck0;
632          double  s;
633          int  branch;
634 <        register int  i;
634 >        int  i;
635  
636 <        if (av->rad <= FTINY)
636 >        if (((AMBVAL*)av)->rad <= FTINY)
637                  error(CONSISTENCY, "zero ambient radius in avinsert");
638          at = &atrunk;
639          VCOPY(ck0, thescene.cuorg);
640          s = thescene.cusize;
641 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
641 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
642                  if (at->kid == NULL)
643                          if ((at->kid = newambtree()) == NULL)
644                                  error(SYSTEM, "out of memory in avinsert");
645                  s *= 0.5;
646                  branch = 0;
647                  for (i = 0; i < 3; i++)
648 <                        if (av->pos[i] > ck0[i] + s) {
648 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
649                                  ck0[i] += s;
650                                  branch |= 1 << i;
651                          }
# Line 610 | Line 653 | register AMBVAL         *av;
653          }
654          avh.next = at->alist;           /* order by increasing level */
655          for (ap = &avh; ap->next != NULL; ap = ap->next)
656 <                if (ap->next->lvl >= av->lvl)
656 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
657                          break;
658 <        av->next = ap->next;
659 <        ap->next = av;
658 >        ((AMBVAL*)av)->next = ap->next;
659 >        ap->next = (AMBVAL*)av;
660          at->alist = avh.next;
661   }
662  
663  
664   static void
665 < unloadatree(at, f)                      /* unload an ambient value tree */
666 < register AMBTREE  *at;
667 < void    (*f)();
665 > unloadatree(                    /* unload an ambient value tree */
666 >        AMBTREE  *at,
667 >        unloadtf_t *f
668 > )
669   {
670 <        register AMBVAL  *av;
671 <        register int  i;
670 >        AMBVAL  *av;
671 >        int  i;
672                                          /* transfer values at this node */
673          for (av = at->alist; av != NULL; av = at->alist) {
674                  at->alist = av->next;
# Line 646 | Line 690 | static struct avl {
690   static AMBVAL   **avlist2;              /* memory positions for sorting */
691   static int      i_avlist;               /* index for lists */
692  
693 + static int alatcmp(const void *av1, const void *av2);
694  
695 < static int
696 < av2list(av)
697 < register AMBVAL *av;
695 > static void
696 > av2list(
697 >        void *av
698 > )
699   {
700   #ifdef DEBUG
701          if (i_avlist >= nambvals)
702                  error(CONSISTENCY, "too many ambient values in av2list1");
703   #endif
704 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
705 <        avlist1[i_avlist++].t = av->latick;
704 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
705 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
706   }
707  
708  
709   static int
710 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
711 < struct avl      *av1, *av2;
710 > alatcmp(                        /* compare ambient values for MRA */
711 >        const void *av1,
712 >        const void *av2
713 > )
714   {
715 <        register long  lc = av2->t - av1->t;
715 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
716          return(lc<0 ? -1 : lc>0 ? 1 : 0);
717   }
718  
# Line 676 | Line 724 | struct avl     *av1, *av2;
724   * assumes pointers differ by exact struct size increments.
725   */
726   static int
727 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
728 < const void      *avp1, *avp2;
727 > aposcmp(                        /* compare ambient value positions */
728 >        const void      *avp1,
729 >        const void      *avp2
730 > )
731   {
732 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
732 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
733          if (diff < 0)
734                  return(-1);
735          return(diff > 0);
# Line 687 | Line 737 | const void     *avp1, *avp2;
737  
738   #if 1
739   static int
740 < avlmemi(avaddr)                         /* find list position from address */
741 < AMBVAL  *avaddr;
740 > avlmemi(                                /* find list position from address */
741 >        AMBVAL  *avaddr
742 > )
743   {
744 <        register AMBVAL  **avlpp;
744 >        AMBVAL  **avlpp;
745  
746          avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
747                          nambvals, sizeof(AMBVAL *), aposcmp);
# Line 705 | Line 756 | AMBVAL *avaddr;
756  
757  
758   static void
759 < sortambvals(always)                     /* resort ambient values */
760 < int     always;
759 > sortambvals(                    /* resort ambient values */
760 >        int     always
761 > )
762   {
763          AMBTREE  oldatrunk;
764          AMBVAL  tav, *tap, *pnext;
765 <        register int    i, j;
765 >        int     i, j;
766                                          /* see if it's time yet */
767          if (!always && (ambclock++ < lastsort+sortintvl ||
768                          nambvals < SORT_THRESH))
# Line 741 | Line 793 | int    always;
793                  if (avlist2 != NULL)
794                          free((void *)avlist2);
795                  if (always) {           /* rebuild without sorting */
796 <                        copystruct(&oldatrunk, &atrunk);
796 >                        oldatrunk = atrunk;
797                          atrunk.alist = NULL;
798                          atrunk.kid = NULL;
799                          unloadatree(&oldatrunk, avinsert);
# Line 772 | Line 824 | int    always;
824                          if (avlist1[i].p == NULL)
825                                  continue;
826                          tap = avlist2[i];
827 <                        copystruct(&tav, tap);
827 >                        tav = *tap;
828                          for (j = i; (pnext = avlist1[j].p) != tap;
829                                          j = avlmemi(pnext)) {
830 <                                copystruct(avlist2[j], pnext);
830 >                                *(avlist2[j]) = *pnext;
831                                  avinsert(avlist2[j]);
832                                  avlist1[j].p = NULL;
833                          }
834 <                        copystruct(avlist2[j], &tav);
834 >                        *(avlist2[j]) = tav;
835                          avinsert(avlist2[j]);
836                          avlist1[j].p = NULL;
837                  }
# Line 804 | Line 856 | int    always;
856   #ifdef  F_SETLKW
857  
858   static void
859 < aflock(typ)                     /* lock/unlock ambient file */
860 < int  typ;
859 > aflock(                 /* lock/unlock ambient file */
860 >        int  typ
861 > )
862   {
863          static struct flock  fls;       /* static so initialized to zeroes */
864  
865 +        if (typ == fls.l_type)          /* already called? */
866 +                return;
867          fls.l_type = typ;
868          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
869                  error(SYSTEM, "cannot (un)lock ambient file");
870   }
871  
872  
873 < int
874 < ambsync()                       /* synchronize ambient file */
873 > extern int
874 > ambsync(void)                   /* synchronize ambient file */
875   {
876          long  flen;
877          AMBVAL  avs;
878 <        register int  n;
878 >        int  n;
879  
880 <        if (nunflshed == 0)
880 >        if (ambfp == NULL)      /* no ambient file? */
881                  return(0);
882 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
883 <                goto syncend;
829 <                                /* gain exclusive access */
830 <        aflock(F_WRLCK);
882 >                                /* gain appropriate access */
883 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
884                                  /* see if file has grown */
885 <        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
885 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
886                  goto seekerr;
887 <        if (n = flen - lastpos) {               /* file has grown */
887 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
888                  if (ambinp == NULL) {           /* use duplicate filedes */
889                          ambinp = fdopen(dup(fileno(ambfp)), "r");
890                          if (ambinp == NULL)
891                                  error(SYSTEM, "fdopen failed in ambsync");
892                  }
893 <                if (fseek(ambinp, lastpos, 0) < 0)
893 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
894                          goto seekerr;
895                  while (n >= AMBVALSIZ) {        /* load contributed values */
896                          if (!readambval(&avs, ambinp)) {
# Line 850 | Line 903 | ambsync()                      /* synchronize ambient file */
903                          avinsert(avstore(&avs));
904                          n -= AMBVALSIZ;
905                  }
906 +                lastpos = flen - n;
907                  /*** seek always as safety measure
908                  if (n) ***/                     /* alignment */
909 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
909 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
910                                  goto seekerr;
911          }
858 #ifdef  DEBUG
859        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
860                sprintf(errmsg, "ambient file buffer at %d rather than %d",
861                                ambfp->_ptr - ambfp->_base,
862                                nunflshed*AMBVALSIZ);
863                error(CONSISTENCY, errmsg);
864        }
865 #endif
866 syncend:
912          n = fflush(ambfp);                      /* calls write() at last */
913 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
913 >        if (n != EOF)
914 >                lastpos += (long)nunflshed*AMBVALSIZ;
915 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
916                  goto seekerr;
917 +                
918          aflock(F_UNLCK);                        /* release file */
919          nunflshed = 0;
920          return(n);
921   seekerr:
922          error(SYSTEM, "seek failed in ambsync");
923 +        return -1; /* pro forma return */
924   }
925  
926   #else
927  
928 < int
929 < ambsync()                       /* flush ambient file */
928 > extern int
929 > ambsync(void)                   /* flush ambient file */
930   {
931 <        if (nunflshed == 0)
931 >        if (ambfp == NULL)
932                  return(0);
933          nunflshed = 0;
934          return(fflush(ambfp));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines