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.82 by greg, Sat Apr 26 02:59:16 2014 UTC vs.
Revision 2.106 by greg, Thu Nov 8 00:54:07 2018 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 14 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  "platform.h"
13   #include  "ray.h"
14   #include  "otypes.h"
15 + #include  "otspecial.h"
16   #include  "resolu.h"
17   #include  "ambient.h"
18   #include  "random.h"
19 + #include  "pmapamb.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 52 | Line 52 | static int  nunflshed = 0;     /* number of unflushed ambi
52   #endif
53  
54  
55 static double  qambacc = 0.;            /* ambient accuracy to the 1/4 power */
55   static double  avsum = 0.;              /* computed ambient value sum (log) */
56   static unsigned int  navsum = 0;        /* number of values in avsum */
57   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 78 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
81 #define  freeav(av)     free((void *)av);
80  
81   static void initambfile(int creat);
82   static void avsave(AMBVAL *av);
# Line 110 | Line 108 | setambres(                             /* set ambient resolution */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110                  minarad = 0;
111 <                maxarad = thescene.cusize / 2.0;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114 <                maxarad = 64 * minarad;                 /* heuristic */
115 <                if (maxarad > thescene.cusize / 2.0)
116 <                        maxarad = thescene.cusize / 2.0;
114 >                maxarad = 64.0 * minarad;               /* heuristic */
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118          if (minarad <= FTINY)
119 <                minarad = 10*FTINY;
119 >                minarad = 10.0*FTINY;
120          if (maxarad <= minarad)
121 <                maxarad = 64 * minarad;
121 >                maxarad = 64.0 * minarad;
122   }
123  
124  
# Line 129 | Line 127 | setambacc(                             /* set ambient accuracy */
127          double  newa
128   )
129   {
130 <        double  olda = qambacc*qambacc*qambacc*qambacc;
130 >        static double   olda;           /* remember previous setting here */
131          
132          newa *= (newa > 0);
133          if (fabs(newa - olda) >= .05*(newa + olda)) {
134 <                qambacc = sqrt(sqrt(ambacc = newa));
134 >                ambacc = newa;
135                  if (nambvals > 0)
136                          sortambvals(1);         /* rebuild tree */
137          }
# Line 166 | Line 164 | setambient(void)                               /* initialize calculation */
164                  initambfile(0);                 /* file exists */
165                  lastpos = ftell(ambfp);
166                  while (readambval(&amb, ambfp))
167 <                        avinsert(avstore(&amb));
167 >                        avstore(&amb);
168                  nambshare = nambvals;           /* share loaded values */
169                  if (readonly) {
170                          sprintf(errmsg,
# Line 186 | Line 184 | setambient(void)                               /* initialize calculation */
184                                          (flen - lastpos)/AMBVALSIZ);
185                          error(WARNING, errmsg);
186                          fseek(ambfp, lastpos, SEEK_SET);
189 #ifndef _WIN32 /* XXX we need a replacement for that one */
187                          ftruncate(fileno(ambfp), (off_t)lastpos);
191 #endif
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
# Line 198 | Line 194 | setambient(void)                               /* initialize calculation */
194                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
201 #ifdef getc_unlocked
202        flockfile(ambfp);                       /* application-level lock */
203 #endif
197   #ifdef  F_SETLKW
198          aflock(F_UNLCK);                        /* release file */
199   #endif
# Line 221 | Line 214 | ambdone(void)                  /* close ambient file and free memory
214                  lastpos = -1;
215          }
216                                          /* free ambient tree */
217 <        unloadatree(&atrunk, &avfree);
217 >        unloadatree(&atrunk, avfree);
218                                          /* reset state variables */
219          avsum = 0.;
220          navsum = 0;
# Line 264 | Line 257 | ambnotify(                     /* record new modifier */
257  
258   /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
259  
260 < #ifdef NEWAMB
260 > #ifndef OLDAMB
261  
262   #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263  
264 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265   static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266                                  AMBTREE *at, FVECT c0, double s);
267   static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 < static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269                                  FVECT uvw[3]);
270  
271   void
# Line 282 | Line 276 | multambient(           /* compute ambient component & multiply
276   )
277   {
278          static int  rdepth = 0;                 /* ambient recursion */
279 <        COLOR   acol;
280 <        int     ok;
279 >        COLOR   acol, caustic;
280 >        int     i, ok;
281          double  d, l;
282  
283 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
284 +         * ambient. Return as all ambient components accounted for, else
285 +         * continue. */
286 +        if (ambPmap(aval, r, rdepth))
287 +                return;
288 +
289 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
290 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
291 +         * with RADIANCE ambient calculation */
292 +        copycolor(caustic, aval);
293 +        ambPmapCaustic(caustic, r, rdepth);
294 +        
295          if (ambdiv <= 0)                        /* no ambient calculation */
296                  goto dumbamb;
297                                                  /* check number of bounces */
# Line 297 | Line 303 | multambient(           /* compute ambient component & multiply
303                  goto dumbamb;
304  
305          if (ambacc <= FTINY) {                  /* no ambient storage */
306 +                FVECT   uvd[2];
307 +                float   dgrad[2], *dgp = NULL;
308 +
309 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
310 +                        dgp = dgrad;            /* compute rotational grad. */
311                  copycolor(acol, aval);
312                  rdepth++;
313 <                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
313 >                ok = doambient(acol, r, r->rweight,
314 >                                uvd, NULL, NULL, dgp, NULL);
315                  rdepth--;
316                  if (!ok)
317                          goto dumbamb;
318 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
319 +                        FVECT   v1;
320 +                        VCROSS(v1, r->ron, nrm);
321 +                        d = 1.0;
322 +                        for (i = 3; i--; )
323 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
324 +                        if (d >= 0.05)
325 +                                scalecolor(acol, d);
326 +                }
327                  copycolor(aval, acol);
328 +
329 +                /* PMAP: add in caustic */
330 +                addcolor(aval, caustic);
331                  return;
332          }
333  
# Line 313 | Line 337 | multambient(           /* compute ambient component & multiply
337          setcolor(acol, 0.0, 0.0, 0.0);
338          d = sumambient(acol, r, nrm, rdepth,
339                          &atrunk, thescene.cuorg, thescene.cusize);
340 +                        
341          if (d > FTINY) {
342                  d = 1.0/d;
343                  scalecolor(acol, d);
344                  multcolor(aval, acol);
345 +
346 +                /* PMAP: add in caustic */
347 +                addcolor(aval, caustic);
348                  return;
349          }
350 +        
351          rdepth++;                               /* need to cache new value */
352          ok = makeambient(acol, r, nrm, rdepth-1);
353          rdepth--;
354 +        
355          if (ok) {
356                  multcolor(aval, acol);          /* computed new value */
357 +
358 +                /* PMAP: add in caustic */
359 +                addcolor(aval, caustic);
360                  return;
361          }
362 +        
363   dumbamb:                                        /* return global value */
364          if ((ambvwt <= 0) | (navsum == 0)) {
365                  multcolor(aval, ambval);
366 +                
367 +                /* PMAP: add in caustic */
368 +                addcolor(aval, caustic);
369                  return;
370          }
371 <        l = bright(ambval);                     /* average in computations */
371 >        
372 >        l = bright(ambval);                     /* average in computations */  
373          if (l > FTINY) {
374                  d = (log(l)*(double)ambvwt + avsum) /
375                                  (double)(ambvwt + navsum);
# Line 345 | Line 383 | dumbamb:                                       /* return global value */
383   }
384  
385  
386 < double
386 > /* Plug a potential leak where ambient cache value is occluded */
387 > static int
388 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
389 > {
390 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
391 >        RAY             rtst;
392 >        FVECT           vdif;
393 >        double          normdot, ndotd, nadotd;
394 >        double          a, b, c, t[2];
395 >
396 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
397 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
398 >                return(0);
399 >        /*
400 >         * Generate test ray, targeting 20 degrees above sample point plane
401 >         * along surface normal from cache position.  This should be high
402 >         * enough to miss local geometry we don't really care about.
403 >         */
404 >        VSUB(vdif, ap->pos, r->rop);
405 >        normdot = DOT(anorm, r->ron);
406 >        ndotd = DOT(vdif, r->ron);
407 >        nadotd = DOT(vdif, anorm);
408 >        a = normdot*normdot - cost70sq;
409 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
410 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
411 >        if (quadratic(t, a, b, c) != 2)
412 >                return(1);                      /* should rarely happen */
413 >        if (t[1] <= FTINY)
414 >                return(0);                      /* should fail behind test */
415 >        rayorigin(&rtst, SHADOW, r, NULL);
416 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
417 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
418 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
419 >                OBJREC  *m = findmaterial(rtst.ro);
420 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
421 >                                (rtst.clipset == NULL ||
422 >                                        !inset(rtst.clipset, rtst.ro->omod)))
423 >                        return(1);              /* plug light leak */
424 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
425 >                rtst.rmax -= rtst.rot;
426 >                rayclear(&rtst);
427 >        }
428 >        return(0);                              /* seems we're OK */
429 > }
430 >
431 >
432 > static double
433   sumambient(             /* get interpolated ambient value */
434          COLOR  acol,
435          RAY  *r,
# Line 357 | Line 441 | sumambient(            /* get interpolated ambient value */
441   )
442   {                       /* initial limit is 10 degrees plus ambacc radians */
443          const double    minangle = 10.0 * PI/180.;
444 <        const double    maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13)
361 <                                        + PI/2.;
444 >        double          maxangle = minangle + ambacc;
445          double          wsum = 0.0;
446          FVECT           ck0;
447          int             i, j;
448          AMBVAL          *av;
449 +
450 +        if (at->kid != NULL) {          /* sum children first */                                
451 +                s *= 0.5;
452 +                for (i = 0; i < 8; i++) {
453 +                        for (j = 0; j < 3; j++) {
454 +                                ck0[j] = c0[j];
455 +                                if (1<<j & i)
456 +                                        ck0[j] += s;
457 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
458 +                                        break;
459 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
460 +                                        break;
461 +                        }
462 +                        if (j == 3)
463 +                                wsum += sumambient(acol, r, rn, al,
464 +                                                        at->kid+i, ck0, s);
465 +                }
466 +                                        /* good enough? */
467 +                if (wsum >= 0.05 && s > minarad*10.0)
468 +                        return(wsum);
469 +        }
470 +                                        /* adjust maximum angle */
471 +        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
472 +                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
473                                          /* sum this node */
474          for (av = at->alist; av != NULL; av = av->next) {
475 <                double  d, delta_r2, delta_t2;
475 >                double  u, v, d, delta_r2, delta_t2;
476                  COLOR   ct;
477                  FVECT   uvw[3];
478                                          /* record access */
# Line 374 | Line 481 | sumambient(            /* get interpolated ambient value */
481                  /*
482                   *  Ambient level test
483                   */
484 <                if (av->lvl > al)       /* list sorted, so this works */
484 >                if (av->lvl > al ||     /* list sorted, so this works */
485 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
486                          break;
379                if (av->weight < 0.9*r->rweight)
380                        continue;
487                  /*
488                   *  Direction test using unperturbed normal
489                   */
# Line 391 | Line 497 | sumambient(            /* get interpolated ambient value */
497                  /*
498                   *  Modified ray behind test
499                   */
500 <                VSUB(ck0, av->pos, r->rop);
500 >                VSUB(ck0, r->rop, av->pos);
501                  d = DOT(ck0, uvw[2]);
502 <                if (d < -minarad*qambacc-.001)
502 >                if (d < -minarad*ambacc-.001)
503                          continue;
504                  d /= av->rad[0];
505                  delta_t2 = d*d;
506 <                if (delta_t2 >= qambacc*qambacc)
506 >                if (delta_t2 >= ambacc*ambacc)
507                          continue;
508                  /*
509                   *  Elliptical radii test based on Hessian
510                   */
511                  decodedir(uvw[0], av->udir);
512                  VCROSS(uvw[1], uvw[2], uvw[0]);
513 <                d = DOT(ck0, uvw[0]) / av->rad[0];
513 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
514                  delta_t2 += d*d;
515 <                d = DOT(ck0, uvw[1]) / av->rad[1];
515 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
516                  delta_t2 += d*d;
517 <                if (delta_t2 >= qambacc*qambacc)
517 >                if (delta_t2 >= ambacc*ambacc)
518                          continue;
519                  /*
520 +                 *  Test for potential light leak
521 +                 */
522 +                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
523 +                        continue;
524 +                /*
525                   *  Extrapolate value and compute final weight (hat function)
526                   */
527 <                extambient(ct, av, r->rop, rn, uvw);
527 >                if (!extambient(ct, av, r->rop, rn, uvw))
528 >                        continue;
529                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
530 <                        tfunc(qambacc, sqrt(delta_t2), 0.0);
530 >                        tfunc(ambacc, sqrt(delta_t2), 0.0);
531                  scalecolor(ct, d);
532                  addcolor(acol, ct);
533                  wsum += d;
534          }
423        if (at->kid == NULL)
424                return(wsum);
425                                        /* sum children */
426        s *= 0.5;
427        for (i = 0; i < 8; i++) {
428                for (j = 0; j < 3; j++) {
429                        ck0[j] = c0[j];
430                        if (1<<j & i)
431                                ck0[j] += s;
432                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
433                                break;
434                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
435                                break;
436                }
437                if (j == 3)
438                        wsum += sumambient(acol, r, rn, al,
439                                                at->kid+i, ck0, s);
440        }
535          return(wsum);
536   }
537  
538  
539 < int
539 > static int
540   makeambient(            /* make a new ambient value for storage */
541          COLOR  acol,
542          RAY  *r,
# Line 461 | Line 555 | makeambient(           /* make a new ambient value for storage
555                  amb.weight = 1.25*r->rweight;
556          setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
557                                                  /* compute ambient */
558 <        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
558 >        i = doambient(acol, r, amb.weight,
559 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
560          scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
561          if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
562                  return(i);
# Line 481 | Line 576 | makeambient(           /* make a new ambient value for storage
576   }
577  
578  
579 < void
579 > static int
580   extambient(             /* extrapolate value at pv, nv */
581          COLOR  cr,
582          AMBVAL   *ap,
# Line 490 | Line 585 | extambient(            /* extrapolate value at pv, nv */
585          FVECT  uvw[3]
586   )
587   {
588 +        const double    min_d = 0.05;
589          static FVECT    my_uvw[3];
590          FVECT           v1;
591          int             i;
# Line 509 | Line 605 | extambient(            /* extrapolate value at pv, nv */
605          for (i = 3; i--; )
606                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
607          
608 <        if (d <= 0.0) {
609 <                setcolor(cr, 0.0, 0.0, 0.0);
514 <                return;
515 <        }
608 >        if (d < min_d)                  /* should not use if we can avoid it */
609 >                d = min_d;
610          copycolor(cr, ap->val);
611          scalecolor(cr, d);
612 +        return(d > min_d);
613   }
614  
615  
# Line 536 | Line 631 | avinsert(                              /* insert ambient value in our tree */
631          at = &atrunk;
632          VCOPY(ck0, thescene.cuorg);
633          s = thescene.cusize;
634 <        while (s*(OCTSCALE/2) > av->rad[1]*qambacc) {
634 >        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
635                  if (at->kid == NULL)
636                          if ((at->kid = newambtree()) == NULL)
637                                  error(SYSTEM, "out of memory in avinsert");
# Line 551 | Line 646 | avinsert(                              /* insert ambient value in our tree */
646          }
647          avh.next = at->alist;           /* order by increasing level */
648          for (ap = &avh; ap->next != NULL; ap = ap->next)
649 <                if (ap->next->lvl >= av->lvl)
649 >                if ( ap->next->lvl > av->lvl ||
650 >                                (ap->next->lvl == av->lvl) &
651 >                                (ap->next->weight <= av->weight) )
652                          break;
653          av->next = ap->next;
654          ap->next = (AMBVAL*)av;
# Line 575 | Line 672 | multambient(           /* compute ambient component & multiply
672   )
673   {
674          static int  rdepth = 0;                 /* ambient recursion */
675 <        COLOR   acol;
675 >        COLOR   acol, caustic;
676          double  d, l;
677  
678 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679 +         * as all ambient components accounted for */
680 +        if (ambPmap(aval, r, rdepth))
681 +                return;
682 +
683 +        /* PMAP: Otherwise factor in ambient from caustic photon map
684 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685 +         * continue with RADIANCE ambient calculation */
686 +        copycolor(caustic, aval);
687 +        ambPmapCaustic(caustic, r, rdepth);
688 +        
689          if (ambdiv <= 0)                        /* no ambient calculation */
690                  goto dumbamb;
691                                                  /* check number of bounces */
# Line 595 | Line 703 | multambient(           /* compute ambient component & multiply
703                  rdepth--;
704                  if (d <= FTINY)
705                          goto dumbamb;
706 <                copycolor(aval, acol);
706 >                copycolor(aval, acol);          
707 >        
708 >           /* PMAP: add in caustic */
709 >                addcolor(aval, caustic);        
710                  return;
711          }
712  
# Line 605 | Line 716 | multambient(           /* compute ambient component & multiply
716          setcolor(acol, 0.0, 0.0, 0.0);
717          d = sumambient(acol, r, nrm, rdepth,
718                          &atrunk, thescene.cuorg, thescene.cusize);
719 +                        
720          if (d > FTINY) {
721                  d = 1.0/d;
722                  scalecolor(acol, d);
723                  multcolor(aval, acol);
724 +                
725 +                /* PMAP: add in caustic */
726 +                addcolor(aval, caustic);        
727                  return;
728          }
729 +        
730          rdepth++;                               /* need to cache new value */
731          d = makeambient(acol, r, nrm, rdepth-1);
732          rdepth--;
733 +        
734          if (d > FTINY) {
735                  multcolor(aval, acol);          /* got new value */
736 +
737 +                /* PMAP: add in caustic */
738 +                addcolor(aval, caustic);                        
739                  return;
740          }
741 +        
742   dumbamb:                                        /* return global value */
743          if ((ambvwt <= 0) | (navsum == 0)) {
744                  multcolor(aval, ambval);
745 +
746 +                /* PMAP: add in caustic */
747 +                addcolor(aval, caustic);        
748                  return;
749          }
750 +        
751          l = bright(ambval);                     /* average in computations */
752          if (l > FTINY) {
753                  d = (log(l)*(double)ambvwt + avsum) /
# Line 664 | Line 789 | sumambient(    /* get interpolated ambient value */
789                  /*
790                   *  Ambient level test.
791                   */
792 <                if (av->lvl > al)       /* list sorted, so this works */
792 >                if (av->lvl > al ||     /* list sorted, so this works */
793 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                          break;
669                if (av->weight < 0.9*r->rweight)
670                        continue;
795                  /*
796                   *  Ambient radius test.
797                   */
# Line 849 | Line 973 | avinsert(                              /* insert ambient value in our tree */
973          }
974          avh.next = at->alist;           /* order by increasing level */
975          for (ap = &avh; ap->next != NULL; ap = ap->next)
976 <                if (ap->next->lvl >= av->lvl)
976 >                if ( ap->next->lvl > av->lvl ||
977 >                                (ap->next->lvl == av->lvl) &
978 >                                (ap->next->weight <= av->weight) )
979                          break;
980          av->next = ap->next;
981          ap->next = (AMBVAL*)av;
# Line 901 | Line 1027 | avsave(                                /* insert and save an ambient value */
1027          AMBVAL  *av
1028   )
1029   {
1030 <        avinsert(avstore(av));
1030 >        avstore(av);
1031          if (ambfp == NULL)
1032                  return;
1033          if (writambval(av, ambfp) < 0)
# Line 916 | Line 1042 | writerr:
1042  
1043  
1044   static AMBVAL *
1045 < avstore(                                /* allocate memory and store aval */
1045 > avstore(                                /* allocate memory and save aval */
1046          AMBVAL  *aval
1047   )
1048   {
# Line 934 | Line 1060 | avstore(                               /* allocate memory and store aval */
1060                  avsum += log(d);
1061                  navsum++;
1062          }
1063 +        avinsert(av);                   /* insert in our cache tree */
1064          return(av);
1065   }
1066  
# Line 960 | Line 1087 | newambtree(void)                               /* allocate 8 ambient tree structs
1087          }
1088          atp = atfreelist;
1089          atfreelist = atp->kid;
1090 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1090 >        memset(atp, 0, 8*sizeof(AMBTREE));
1091          return(atp);
1092   }
1093  
# Line 986 | Line 1113 | unloadatree(                   /* unload an ambient value tree */
1113                                          /* transfer values at this node */
1114          for (av = at->alist; av != NULL; av = at->alist) {
1115                  at->alist = av->next;
1116 +                av->next = NULL;
1117                  (*f)(av);
1118          }
1119          if (at->kid == NULL)
# Line 1063 | Line 1191 | avlmemi(                               /* find list position from address */
1191   {
1192          AMBVAL  **avlpp;
1193  
1194 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1195 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1194 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1196          if (avlpp == NULL)
1197                  error(CONSISTENCY, "address not found in avlmemi");
1198          return(avlpp - avlist2);
# Line 1107 | Line 1235 | sortambvals(                   /* resort ambient values */
1235          }
1236          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237                  if (avlist2 != NULL)
1238 <                        free((void *)avlist2);
1238 >                        free(avlist2);
1239                  if (always) {           /* rebuild without sorting */
1240                          oldatrunk = atrunk;
1241                          atrunk.alist = NULL;
1242                          atrunk.kid = NULL;
1243 <                        unloadatree(&oldatrunk, &avinsert);
1243 >                        unloadatree(&oldatrunk, avinsert);
1244                  }
1245          } else {                        /* sort memory by last access time */
1246                  /*
# Line 1129 | Line 1257 | sortambvals(                   /* resort ambient values */
1257                  eputs(errmsg);
1258   #endif
1259                  i_avlist = 0;
1260 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1260 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1261   #ifdef DEBUG
1262                  if (i_avlist < nambvals)
1263                          error(CONSISTENCY, "missing ambient values in sortambvals");
1264   #endif
1265 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1266 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1265 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267                  for (i = 0; i < nambvals; i++) {
1268                          if (avlist1[i].p == NULL)
1269                                  continue;
# Line 1151 | Line 1279 | sortambvals(                   /* resort ambient values */
1279                          avinsert(avlist2[j]);
1280                          avlist1[j].p = NULL;
1281                  }
1282 <                free((void *)avlist1);
1283 <                free((void *)avlist2);
1282 >                free(avlist1);
1283 >                free(avlist2);
1284                                                  /* compute new sort interval */
1285                  sortintvl = ambclock - lastsort;
1286                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1201 | Line 1329 | ambsync(void)                  /* synchronize ambient file */
1329          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1330                  goto seekerr;
1331          if ((n = flen - lastpos) > 0) {         /* file has grown */
1332 <                if (ambinp == NULL) {           /* use duplicate filedes */
1333 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1332 >                if (ambinp == NULL) {           /* get new file pointer */
1333 >                        ambinp = fopen(ambfile, "rb");
1334                          if (ambinp == NULL)
1335 <                                error(SYSTEM, "fdopen failed in ambsync");
1335 >                                error(SYSTEM, "fopen failed in ambsync");
1336                  }
1337                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1338                          goto seekerr;
# Line 1216 | Line 1344 | ambsync(void)                  /* synchronize ambient file */
1344                                  error(WARNING, errmsg);
1345                                  break;
1346                          }
1347 <                        avinsert(avstore(&avs));
1347 >                        avstore(&avs);
1348                          n -= AMBVALSIZ;
1349                  }
1350 <                lastpos = flen - n;
1351 <                /*** seek always as safety measure
1352 <                if (n) ***/                     /* alignment */
1225 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1226 <                                goto seekerr;
1350 >                lastpos = flen - n;             /* check alignment */
1351 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1352 >                        goto seekerr;
1353          }
1354          n = fflush(ambfp);                      /* calls write() at last */
1355 <        if (n != EOF)
1230 <                lastpos += (long)nunflshed*AMBVALSIZ;
1231 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1232 <                goto seekerr;
1233 <                
1355 >        lastpos += (long)nunflshed*AMBVALSIZ;
1356          aflock(F_UNLCK);                        /* release file */
1357          nunflshed = 0;
1358          return(n);
1359   seekerr:
1360          error(SYSTEM, "seek failed in ambsync");
1361 <        return -1; /* pro forma return */
1361 >        return(EOF);    /* pro forma return */
1362   }
1363  
1364   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines