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.80 by greg, Fri Apr 25 18:38:47 2014 UTC vs.
Revision 2.102 by greg, Sun Apr 24 16:21:32 2016 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 17 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  "resolu.h"
16   #include  "ambient.h"
17   #include  "random.h"
18 + #include  "pmapamb.h"
19  
20   #ifndef  OCTSCALE
21   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 52 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #endif
52  
53  
55 static double  qambacc = 0.;            /* ambient accuracy to the 1/4 power */
54   static double  avsum = 0.;              /* computed ambient value sum (log) */
55   static unsigned int  navsum = 0;        /* number of values in avsum */
56   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 78 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
81 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 110 | Line 107 | setambres(                             /* set ambient resolution */
107                                                  /* set min & max radii */
108          if (ar <= 0) {
109                  minarad = 0;
110 <                maxarad = thescene.cusize / 2.0;
110 >                maxarad = thescene.cusize*0.2;
111          } else {
112                  minarad = thescene.cusize / ar;
113 <                maxarad = 64 * minarad;                 /* heuristic */
114 <                if (maxarad > thescene.cusize / 2.0)
115 <                        maxarad = thescene.cusize / 2.0;
113 >                maxarad = 64.0 * minarad;               /* heuristic */
114 >                if (maxarad > thescene.cusize*0.2)
115 >                        maxarad = thescene.cusize*0.2;
116          }
117          if (minarad <= FTINY)
118 <                minarad = 10*FTINY;
118 >                minarad = 10.0*FTINY;
119          if (maxarad <= minarad)
120 <                maxarad = 64 * minarad;
120 >                maxarad = 64.0 * minarad;
121   }
122  
123  
# Line 129 | Line 126 | setambacc(                             /* set ambient accuracy */
126          double  newa
127   )
128   {
129 <        double  ambdiff;
130 <
131 <        if (newa < 0.0)
132 <                newa = 0.0;
133 <        ambdiff = fabs(newa - ambacc);
137 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY) {
138 <                qambacc = sqrt(sqrt(ambacc));
129 >        static double   olda;           /* remember previous setting here */
130 >        
131 >        newa *= (newa > 0);
132 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
133 >                ambacc = newa;
134                  if (nambvals > 0)
135                          sortambvals(1);         /* rebuild tree */
136          }
# Line 152 | Line 147 | setambient(void)                               /* initialize calculation */
147          ambdone();
148                                                  /* init ambient limits */
149          setambres(ambres);
150 <        qambacc = sqrt(sqrt(ambacc *= (ambacc > FTINY)));
150 >        setambacc(ambacc);
151          if (ambfile == NULL || !ambfile[0])
152                  return;
153          if (ambacc <= FTINY) {
# Line 168 | Line 163 | setambient(void)                               /* initialize calculation */
163                  initambfile(0);                 /* file exists */
164                  lastpos = ftell(ambfp);
165                  while (readambval(&amb, ambfp))
166 <                        avinsert(avstore(&amb));
166 >                        avstore(&amb);
167                  nambshare = nambvals;           /* share loaded values */
168                  if (readonly) {
169                          sprintf(errmsg,
# Line 188 | Line 183 | setambient(void)                               /* initialize calculation */
183                                          (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185                          fseek(ambfp, lastpos, SEEK_SET);
191 #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)lastpos);
193 #endif
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
# Line 200 | Line 193 | setambient(void)                               /* initialize calculation */
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
203 #ifdef getc_unlocked
204        flockfile(ambfp);                       /* application-level lock */
205 #endif
196   #ifdef  F_SETLKW
197          aflock(F_UNLCK);                        /* release file */
198   #endif
# Line 223 | Line 213 | ambdone(void)                  /* close ambient file and free memory
213                  lastpos = -1;
214          }
215                                          /* free ambient tree */
216 <        unloadatree(&atrunk, &avfree);
216 >        unloadatree(&atrunk, avfree);
217                                          /* reset state variables */
218          avsum = 0.;
219          navsum = 0;
# Line 266 | Line 256 | ambnotify(                     /* record new modifier */
256  
257   /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
258  
259 < #ifdef NEWAMB
259 > #ifndef OLDAMB
260  
261   #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
262  
263 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
264   static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
265                                  AMBTREE *at, FVECT c0, double s);
266   static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
267 < static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
267 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268                                  FVECT uvw[3]);
269  
270   void
# Line 284 | Line 275 | multambient(           /* compute ambient component & multiply
275   )
276   {
277          static int  rdepth = 0;                 /* ambient recursion */
278 <        COLOR   acol;
278 >        COLOR   acol, caustic;
279          int     ok;
280          double  d, l;
281  
282 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
283 +         * ambient. Return as all ambient components accounted for, else
284 +         * continue. */
285 +        if (ambPmap(aval, r, rdepth))
286 +                return;
287 +
288 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
289 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
290 +         * with RADIANCE ambient calculation */
291 +        copycolor(caustic, aval);
292 +        ambPmapCaustic(caustic, r, rdepth);
293 +        
294          if (ambdiv <= 0)                        /* no ambient calculation */
295                  goto dumbamb;
296                                                  /* check number of bounces */
# Line 301 | Line 304 | multambient(           /* compute ambient component & multiply
304          if (ambacc <= FTINY) {                  /* no ambient storage */
305                  copycolor(acol, aval);
306                  rdepth++;
307 <                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
307 >                ok = doambient(acol, r, r->rweight,
308 >                                NULL, NULL, NULL, NULL, NULL);
309                  rdepth--;
310                  if (!ok)
311                          goto dumbamb;
312                  copycolor(aval, acol);
313 +
314 +                /* PMAP: add in caustic */
315 +                addcolor(aval, caustic);
316                  return;
317          }
318  
# Line 315 | Line 322 | multambient(           /* compute ambient component & multiply
322          setcolor(acol, 0.0, 0.0, 0.0);
323          d = sumambient(acol, r, nrm, rdepth,
324                          &atrunk, thescene.cuorg, thescene.cusize);
325 +                        
326          if (d > FTINY) {
327                  d = 1.0/d;
328                  scalecolor(acol, d);
329                  multcolor(aval, acol);
330 +
331 +                /* PMAP: add in caustic */
332 +                addcolor(aval, caustic);
333                  return;
334          }
335 +        
336          rdepth++;                               /* need to cache new value */
337          ok = makeambient(acol, r, nrm, rdepth-1);
338          rdepth--;
339 +        
340          if (ok) {
341                  multcolor(aval, acol);          /* computed new value */
342 +
343 +                /* PMAP: add in caustic */
344 +                addcolor(aval, caustic);
345                  return;
346          }
347 +        
348   dumbamb:                                        /* return global value */
349          if ((ambvwt <= 0) | (navsum == 0)) {
350                  multcolor(aval, ambval);
351 +                
352 +                /* PMAP: add in caustic */
353 +                addcolor(aval, caustic);
354                  return;
355          }
356 <        l = bright(ambval);                     /* average in computations */
356 >        
357 >        l = bright(ambval);                     /* average in computations */  
358          if (l > FTINY) {
359                  d = (log(l)*(double)ambvwt + avsum) /
360                                  (double)(ambvwt + navsum);
# Line 347 | Line 368 | dumbamb:                                       /* return global value */
368   }
369  
370  
371 < double
371 > /* Plug a potential leak where ambient cache value is occluded */
372 > static int
373 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
374 > {
375 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
376 >        RAY             rtst;
377 >        FVECT           vdif;
378 >        double          normdot, ndotd, nadotd;
379 >        double          a, b, c, t[2];
380 >
381 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
382 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
383 >                return(0);
384 >        /*
385 >         * Generate test ray, targeting 20 degrees above sample point plane
386 >         * along surface normal from cache position.  This should be high
387 >         * enough to miss local geometry we don't really care about.
388 >         */
389 >        VSUB(vdif, ap->pos, r->rop);
390 >        normdot = DOT(anorm, r->ron);
391 >        ndotd = DOT(vdif, r->ron);
392 >        nadotd = DOT(vdif, anorm);
393 >        a = normdot*normdot - cost70sq;
394 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
395 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
396 >        if (quadratic(t, a, b, c) != 2)
397 >                return(1);                      /* should rarely happen */
398 >        if (t[1] <= FTINY)
399 >                return(0);                      /* should fail behind test */
400 >        rayorigin(&rtst, SHADOW, r, NULL);
401 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
402 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
403 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
404 >                if (rtst.ro->omod != OVOID &&
405 >                                (rtst.clipset == NULL ||
406 >                                        !inset(rtst.clipset, rtst.ro->omod)))
407 >                        return(1);              /* plug light leak */
408 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
409 >                rtst.rmax -= rtst.rot;
410 >                rayclear(&rtst);
411 >        }
412 >        return(0);                              /* seems we're OK */
413 > }
414 >
415 >
416 > static double
417   sumambient(             /* get interpolated ambient value */
418          COLOR  acol,
419          RAY  *r,
# Line 359 | Line 425 | sumambient(            /* get interpolated ambient value */
425   )
426   {                       /* initial limit is 10 degrees plus ambacc radians */
427          const double    minangle = 10.0 * PI/180.;
428 <        const double    maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13)
363 <                                        + PI/2.;
428 >        double          maxangle = minangle + ambacc;
429          double          wsum = 0.0;
430          FVECT           ck0;
431          int             i, j;
432          AMBVAL          *av;
433 +
434 +        if (at->kid != NULL) {          /* sum children first */                                
435 +                s *= 0.5;
436 +                for (i = 0; i < 8; i++) {
437 +                        for (j = 0; j < 3; j++) {
438 +                                ck0[j] = c0[j];
439 +                                if (1<<j & i)
440 +                                        ck0[j] += s;
441 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
442 +                                        break;
443 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
444 +                                        break;
445 +                        }
446 +                        if (j == 3)
447 +                                wsum += sumambient(acol, r, rn, al,
448 +                                                        at->kid+i, ck0, s);
449 +                }
450 +                                        /* good enough? */
451 +                if (wsum >= 0.05 && s > minarad*10.0)
452 +                        return(wsum);
453 +        }
454 +                                        /* adjust maximum angle */
455 +        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
456 +                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
457                                          /* sum this node */
458          for (av = at->alist; av != NULL; av = av->next) {
459 <                double  d, delta_r2, delta_t2;
459 >                double  u, v, d, delta_r2, delta_t2;
460                  COLOR   ct;
461                  FVECT   uvw[3];
462                                          /* record access */
# Line 376 | Line 465 | sumambient(            /* get interpolated ambient value */
465                  /*
466                   *  Ambient level test
467                   */
468 <                if (av->lvl > al)       /* list sorted, so this works */
468 >                if (av->lvl > al ||     /* list sorted, so this works */
469 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
470                          break;
381                if (av->weight < 0.9*r->rweight)
382                        continue;
471                  /*
472                   *  Direction test using unperturbed normal
473                   */
# Line 391 | Line 479 | sumambient(            /* get interpolated ambient value */
479                  if (delta_r2 >= maxangle*maxangle)
480                          continue;
481                  /*
482 +                 *  Modified ray behind test
483 +                 */
484 +                VSUB(ck0, r->rop, av->pos);
485 +                d = DOT(ck0, uvw[2]);
486 +                if (d < -minarad*ambacc-.001)
487 +                        continue;
488 +                d /= av->rad[0];
489 +                delta_t2 = d*d;
490 +                if (delta_t2 >= ambacc*ambacc)
491 +                        continue;
492 +                /*
493                   *  Elliptical radii test based on Hessian
494                   */
495                  decodedir(uvw[0], av->udir);
496                  VCROSS(uvw[1], uvw[2], uvw[0]);
497 <                VSUB(ck0, av->pos, r->rop);
399 <                d = DOT(ck0, uvw[0]) / av->rad[0];
400 <                delta_t2 = d*d;
401 <                d = DOT(ck0, uvw[1]) / av->rad[1];
497 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
498                  delta_t2 += d*d;
499 <                d = DOT(ck0, uvw[2]) / av->rad[0];
499 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
500                  delta_t2 += d*d;
501 <                if (delta_t2 >= qambacc*qambacc)
501 >                if (delta_t2 >= ambacc*ambacc)
502                          continue;
503                  /*
504 +                 *  Test for potential light leak
505 +                 */
506 +                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
507 +                        continue;
508 +                /*
509                   *  Extrapolate value and compute final weight (hat function)
510                   */
511 <                extambient(ct, av, r->rop, rn, uvw);
511 >                if (!extambient(ct, av, r->rop, rn, uvw))
512 >                        continue;
513                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
514 <                        tfunc(qambacc, sqrt(delta_t2), 0.0);
514 >                        tfunc(ambacc, sqrt(delta_t2), 0.0);
515                  scalecolor(ct, d);
516                  addcolor(acol, ct);
517                  wsum += d;
518          }
417        if (at->kid == NULL)
418                return(wsum);
419                                        /* sum children */
420        s *= 0.5;
421        for (i = 0; i < 8; i++) {
422                for (j = 0; j < 3; j++) {
423                        ck0[j] = c0[j];
424                        if (1<<j & i)
425                                ck0[j] += s;
426                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
427                                break;
428                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
429                                break;
430                }
431                if (j == 3)
432                        wsum += sumambient(acol, r, rn, al,
433                                                at->kid+i, ck0, s);
434        }
519          return(wsum);
520   }
521  
522  
523 < int
523 > static int
524   makeambient(            /* make a new ambient value for storage */
525          COLOR  acol,
526          RAY  *r,
# Line 455 | Line 539 | makeambient(           /* make a new ambient value for storage
539                  amb.weight = 1.25*r->rweight;
540          setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
541                                                  /* compute ambient */
542 <        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
542 >        i = doambient(acol, r, amb.weight,
543 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
544          scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
545          if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
546                  return(i);
# Line 475 | Line 560 | makeambient(           /* make a new ambient value for storage
560   }
561  
562  
563 < void
563 > static int
564   extambient(             /* extrapolate value at pv, nv */
565          COLOR  cr,
566          AMBVAL   *ap,
# Line 484 | Line 569 | extambient(            /* extrapolate value at pv, nv */
569          FVECT  uvw[3]
570   )
571   {
572 +        const double    min_d = 0.05;
573          static FVECT    my_uvw[3];
574          FVECT           v1;
575          int             i;
# Line 503 | Line 589 | extambient(            /* extrapolate value at pv, nv */
589          for (i = 3; i--; )
590                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
591          
592 <        if (d <= 0.0) {
593 <                setcolor(cr, 0.0, 0.0, 0.0);
508 <                return;
509 <        }
592 >        if (d < min_d)                  /* should not use if we can avoid it */
593 >                d = min_d;
594          copycolor(cr, ap->val);
595          scalecolor(cr, d);
596 +        return(d > min_d);
597   }
598  
599  
# Line 530 | Line 615 | avinsert(                              /* insert ambient value in our tree */
615          at = &atrunk;
616          VCOPY(ck0, thescene.cuorg);
617          s = thescene.cusize;
618 <        while (s*(OCTSCALE/2) > av->rad[1]*qambacc) {
618 >        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
619                  if (at->kid == NULL)
620                          if ((at->kid = newambtree()) == NULL)
621                                  error(SYSTEM, "out of memory in avinsert");
# Line 545 | Line 630 | avinsert(                              /* insert ambient value in our tree */
630          }
631          avh.next = at->alist;           /* order by increasing level */
632          for (ap = &avh; ap->next != NULL; ap = ap->next)
633 <                if (ap->next->lvl >= av->lvl)
633 >                if ( ap->next->lvl > av->lvl ||
634 >                                (ap->next->lvl == av->lvl) &
635 >                                (ap->next->weight <= av->weight) )
636                          break;
637          av->next = ap->next;
638          ap->next = (AMBVAL*)av;
# Line 569 | Line 656 | multambient(           /* compute ambient component & multiply
656   )
657   {
658          static int  rdepth = 0;                 /* ambient recursion */
659 <        COLOR   acol;
659 >        COLOR   acol, caustic;
660          double  d, l;
661  
662 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
663 +         * as all ambient components accounted for */
664 +        if (ambPmap(aval, r, rdepth))
665 +                return;
666 +
667 +        /* PMAP: Otherwise factor in ambient from caustic photon map
668 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
669 +         * continue with RADIANCE ambient calculation */
670 +        copycolor(caustic, aval);
671 +        ambPmapCaustic(caustic, r, rdepth);
672 +        
673          if (ambdiv <= 0)                        /* no ambient calculation */
674                  goto dumbamb;
675                                                  /* check number of bounces */
# Line 589 | Line 687 | multambient(           /* compute ambient component & multiply
687                  rdepth--;
688                  if (d <= FTINY)
689                          goto dumbamb;
690 <                copycolor(aval, acol);
690 >                copycolor(aval, acol);          
691 >        
692 >           /* PMAP: add in caustic */
693 >                addcolor(aval, caustic);        
694                  return;
695          }
696  
# Line 599 | Line 700 | multambient(           /* compute ambient component & multiply
700          setcolor(acol, 0.0, 0.0, 0.0);
701          d = sumambient(acol, r, nrm, rdepth,
702                          &atrunk, thescene.cuorg, thescene.cusize);
703 +                        
704          if (d > FTINY) {
705                  d = 1.0/d;
706                  scalecolor(acol, d);
707                  multcolor(aval, acol);
708 +                
709 +                /* PMAP: add in caustic */
710 +                addcolor(aval, caustic);        
711                  return;
712          }
713 +        
714          rdepth++;                               /* need to cache new value */
715          d = makeambient(acol, r, nrm, rdepth-1);
716          rdepth--;
717 +        
718          if (d > FTINY) {
719                  multcolor(aval, acol);          /* got new value */
720 +
721 +                /* PMAP: add in caustic */
722 +                addcolor(aval, caustic);                        
723                  return;
724          }
725 +        
726   dumbamb:                                        /* return global value */
727          if ((ambvwt <= 0) | (navsum == 0)) {
728                  multcolor(aval, ambval);
729 +
730 +                /* PMAP: add in caustic */
731 +                addcolor(aval, caustic);        
732                  return;
733          }
734 +        
735          l = bright(ambval);                     /* average in computations */
736          if (l > FTINY) {
737                  d = (log(l)*(double)ambvwt + avsum) /
# Line 658 | Line 773 | sumambient(    /* get interpolated ambient value */
773                  /*
774                   *  Ambient level test.
775                   */
776 <                if (av->lvl > al)       /* list sorted, so this works */
776 >                if (av->lvl > al ||     /* list sorted, so this works */
777 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
778                          break;
663                if (av->weight < 0.9*r->rweight)
664                        continue;
779                  /*
780                   *  Ambient radius test.
781                   */
# Line 843 | Line 957 | avinsert(                              /* insert ambient value in our tree */
957          }
958          avh.next = at->alist;           /* order by increasing level */
959          for (ap = &avh; ap->next != NULL; ap = ap->next)
960 <                if (ap->next->lvl >= av->lvl)
960 >                if ( ap->next->lvl > av->lvl ||
961 >                                (ap->next->lvl == av->lvl) &
962 >                                (ap->next->weight <= av->weight) )
963                          break;
964          av->next = ap->next;
965          ap->next = (AMBVAL*)av;
# Line 895 | Line 1011 | avsave(                                /* insert and save an ambient value */
1011          AMBVAL  *av
1012   )
1013   {
1014 <        avinsert(avstore(av));
1014 >        avstore(av);
1015          if (ambfp == NULL)
1016                  return;
1017          if (writambval(av, ambfp) < 0)
# Line 910 | Line 1026 | writerr:
1026  
1027  
1028   static AMBVAL *
1029 < avstore(                                /* allocate memory and store aval */
1029 > avstore(                                /* allocate memory and save aval */
1030          AMBVAL  *aval
1031   )
1032   {
# Line 928 | Line 1044 | avstore(                               /* allocate memory and store aval */
1044                  avsum += log(d);
1045                  navsum++;
1046          }
1047 +        avinsert(av);                   /* insert in our cache tree */
1048          return(av);
1049   }
1050  
# Line 954 | Line 1071 | newambtree(void)                               /* allocate 8 ambient tree structs
1071          }
1072          atp = atfreelist;
1073          atfreelist = atp->kid;
1074 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1074 >        memset(atp, 0, 8*sizeof(AMBTREE));
1075          return(atp);
1076   }
1077  
# Line 980 | Line 1097 | unloadatree(                   /* unload an ambient value tree */
1097                                          /* transfer values at this node */
1098          for (av = at->alist; av != NULL; av = at->alist) {
1099                  at->alist = av->next;
1100 +                av->next = NULL;
1101                  (*f)(av);
1102          }
1103          if (at->kid == NULL)
# Line 1057 | Line 1175 | avlmemi(                               /* find list position from address */
1175   {
1176          AMBVAL  **avlpp;
1177  
1178 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1179 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1178 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1179 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1180          if (avlpp == NULL)
1181                  error(CONSISTENCY, "address not found in avlmemi");
1182          return(avlpp - avlist2);
# Line 1101 | Line 1219 | sortambvals(                   /* resort ambient values */
1219          }
1220          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1221                  if (avlist2 != NULL)
1222 <                        free((void *)avlist2);
1222 >                        free(avlist2);
1223                  if (always) {           /* rebuild without sorting */
1224                          oldatrunk = atrunk;
1225                          atrunk.alist = NULL;
1226                          atrunk.kid = NULL;
1227 <                        unloadatree(&oldatrunk, &avinsert);
1227 >                        unloadatree(&oldatrunk, avinsert);
1228                  }
1229          } else {                        /* sort memory by last access time */
1230                  /*
# Line 1123 | Line 1241 | sortambvals(                   /* resort ambient values */
1241                  eputs(errmsg);
1242   #endif
1243                  i_avlist = 0;
1244 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1244 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1245   #ifdef DEBUG
1246                  if (i_avlist < nambvals)
1247                          error(CONSISTENCY, "missing ambient values in sortambvals");
1248   #endif
1249 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1250 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1249 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1250 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1251                  for (i = 0; i < nambvals; i++) {
1252                          if (avlist1[i].p == NULL)
1253                                  continue;
# Line 1145 | Line 1263 | sortambvals(                   /* resort ambient values */
1263                          avinsert(avlist2[j]);
1264                          avlist1[j].p = NULL;
1265                  }
1266 <                free((void *)avlist1);
1267 <                free((void *)avlist2);
1266 >                free(avlist1);
1267 >                free(avlist2);
1268                                                  /* compute new sort interval */
1269                  sortintvl = ambclock - lastsort;
1270                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1210 | Line 1328 | ambsync(void)                  /* synchronize ambient file */
1328                                  error(WARNING, errmsg);
1329                                  break;
1330                          }
1331 <                        avinsert(avstore(&avs));
1331 >                        avstore(&avs);
1332                          n -= AMBVALSIZ;
1333                  }
1334                  lastpos = flen - n;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines