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.37 by greg, Tue May 28 14:24:44 1996 UTC vs.
Revision 2.47 by greg, Sat Feb 22 02:07:28 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ambient.c - routines dealing with ambient (inter-reflected) component.
6 + *
7 + *  Declarations of external symbols in ambient.h
8   */
9  
10 + /* ====================================================================
11 + * The Radiance Software License, Version 1.0
12 + *
13 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 + *
16 + * Redistribution and use in source and binary forms, with or without
17 + * modification, are permitted provided that the following conditions
18 + * are met:
19 + *
20 + * 1. Redistributions of source code must retain the above copyright
21 + *         notice, this list of conditions and the following disclaimer.
22 + *
23 + * 2. Redistributions in binary form must reproduce the above copyright
24 + *       notice, this list of conditions and the following disclaimer in
25 + *       the documentation and/or other materials provided with the
26 + *       distribution.
27 + *
28 + * 3. The end-user documentation included with the redistribution,
29 + *           if any, must include the following acknowledgment:
30 + *             "This product includes Radiance software
31 + *                 (http://radsite.lbl.gov/)
32 + *                 developed by the Lawrence Berkeley National Laboratory
33 + *               (http://www.lbl.gov/)."
34 + *       Alternately, this acknowledgment may appear in the software itself,
35 + *       if and wherever such third-party acknowledgments normally appear.
36 + *
37 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 + *       and "The Regents of the University of California" must
39 + *       not be used to endorse or promote products derived from this
40 + *       software without prior written permission. For written
41 + *       permission, please contact [email protected].
42 + *
43 + * 5. Products derived from this software may not be called "Radiance",
44 + *       nor may "Radiance" appear in their name, without prior written
45 + *       permission of Lawrence Berkeley National Laboratory.
46 + *
47 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 + * SUCH DAMAGE.
59 + * ====================================================================
60 + *
61 + * This software consists of voluntary contributions made by many
62 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 + * information on Lawrence Berkeley National Laboratory, please see
64 + * <http://www.lbl.gov/>.
65 + */
66 +
67   #include  "ray.h"
68  
13 #include  "octree.h"
14
69   #include  "otypes.h"
70  
71   #include  "ambient.h"
# Line 22 | Line 76 | static char SCCSid[] = "$SunId$ LBL";
76   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
77   #endif
78  
25 typedef struct ambtree {
26        AMBVAL  *alist;         /* ambient value list */
27        struct ambtree  *kid;   /* 8 child nodes */
28 }  AMBTREE;                     /* ambient octree */
29
30 extern CUBE  thescene;          /* contains space boundaries */
31
79   extern char  *shm_boundary;     /* memory sharing boundary */
80  
81   #define  MAXASET        511     /* maximum number of elements in ambient set */
# Line 50 | Line 97 | static int  nunflshed = 0;     /* number of unflushed ambi
97   #endif
98   #endif
99   #ifndef SORT_INTVL
100 < #define SORT_INTVL      (SORT_THRESH*256)
100 > #define SORT_INTVL      (SORT_THRESH<<1)
101   #endif
102   #ifndef MAX_SORT_INTVL
103 < #define MAX_SORT_INTVL  (SORT_INTVL<<4)
103 > #define MAX_SORT_INTVL  (SORT_INTVL<<6)
104   #endif
105  
106 < static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
106 > static double  avsum = 0.;              /* computed ambient value sum (log) */
107 > static unsigned int  navsum = 0;        /* number of values in avsum */
108   static unsigned int  nambvals = 0;      /* total number of indirect values */
109   static unsigned int  nambshare = 0;     /* number of values from file */
110   static unsigned long  ambclock = 0;     /* ambient access clock */
111   static unsigned long  lastsort = 0;     /* time of last value sort */
112   static long  sortintvl = SORT_INTVL;    /* time until next sort */
113 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
114 + static long  lastpos = -1;              /* last flush position */
115  
116   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
117          /*
# Line 69 | Line 119 | static long  sortintvl = SORT_INTVL;   /* time until nex
119           * through memory on a multiprocessor, when we want to avoid
120           * claiming our own memory (copy on write).  Go ahead anyway
121           * if more than two thirds of our values are unshared.
122 +         * Compile with -Dtracktime=0 to turn this code off.
123           */
124 + #ifndef tracktime
125   #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
126 + #endif
127  
128   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
129  
130 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
130 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
131 > #define  freeav(av)     free((void *)av);
132  
133 < extern long  ftell(), lseek();
134 < static int  initambfile(), avsave(), avinsert(), sortambvals();
133 > static void  initambfile(), avsave(), avinsert(), sortambvals(), unloadatree();
134 > static int  avlmemi();
135   static AMBVAL  *avstore();
136   #ifdef  F_SETLKW
137 < static  aflock();
137 > static void  aflock();
138   #endif
139  
140  
141 + void
142   setambres(ar)                           /* set ambient resolution */
143   int  ar;
144   {
# Line 105 | Line 160 | int  ar;
160   }
161  
162  
163 + void
164   setambacc(newa)                         /* set ambient accuracy */
165   double  newa;
166   {
# Line 118 | Line 174 | double  newa;
174   }
175  
176  
177 < setambient(afile)                       /* initialize calculation */
178 < char  *afile;
177 > void
178 > setambient()                            /* initialize calculation */
179   {
180 +        int     readonly = 0;
181          long  pos, flen;
182          AMBVAL  amb;
183 +                                                /* make sure we're fresh */
184 +        ambdone();
185                                                  /* init ambient limits */
186          setambres(ambres);
187          setambacc(ambacc);
188 <        if (afile == NULL)
188 >        if (ambfile == NULL || !ambfile[0])
189                  return;
190          if (ambacc <= FTINY) {
191                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
192 <                                afile);
192 >                                ambfile);
193                  error(WARNING, errmsg);
194                  return;
195          }
196                                                  /* open ambient file */
197 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
198 <                initambfile(0);
197 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
198 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
199 >        if (ambfp != NULL) {
200 >                initambfile(0);                 /* file exists */
201                  pos = ftell(ambfp);
202                  while (readambval(&amb, ambfp))
203                          avinsert(avstore(&amb));
204 <                                                /* align */
204 >                nambshare = nambvals;           /* share loaded values */
205 >                if (readonly) {
206 >                        sprintf(errmsg,
207 >                                "loaded %u values from read-only ambient file",
208 >                                        nambvals);
209 >                        error(WARNING, errmsg);
210 >                        fclose(ambfp);          /* close file so no writes */
211 >                        ambfp = NULL;
212 >                        return;                 /* avoid ambsync() */
213 >                }
214 >                                                /* align file pointer */
215                  pos += (long)nambvals*AMBVALSIZ;
216 <                flen = lseek(fileno(ambfp), 0L, 2);
216 >                flen = lseek(fileno(ambfp), (off_t)0L, 2);
217                  if (flen != pos) {
218 <                        error(WARNING,
218 >                        sprintf(errmsg,
219                          "ignoring last %ld values in ambient file (corrupted)",
220                                          (flen - pos)/AMBVALSIZ);
221 +                        error(WARNING, errmsg);
222                          fseek(ambfp, pos, 0);
223 <                        ftruncate(fileno(ambfp), pos);
223 >                        ftruncate(fileno(ambfp), (off_t)pos);
224                  }
225 <                nambshare = nambvals;
226 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
227 <                initambfile(1);
228 <        else {
157 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
225 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
226 >                initambfile(1);                 /* else create new file */
227 >        } else {
228 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
229                  error(SYSTEM, errmsg);
230          }
231          nunflshed++;    /* lie */
# Line 162 | Line 233 | char  *afile;
233   }
234  
235  
236 + void
237 + ambdone()                       /* close ambient file and free memory */
238 + {
239 +        if (ambfp != NULL) {            /* close ambient file */
240 +                ambsync();
241 +                fclose(ambfp);
242 +                ambfp = NULL;
243 +                if (ambinp != NULL) {  
244 +                        fclose(ambinp);
245 +                        ambinp = NULL;
246 +                }
247 +                lastpos = -1;
248 +        }
249 +                                        /* free ambient tree */
250 +        unloadatree(&atrunk, free);
251 +                                        /* reset state variables */
252 +        avsum = 0.;
253 +        navsum = 0;
254 +        nambvals = 0;
255 +        nambshare = 0;
256 +        ambclock = 0;
257 +        lastsort = 0;
258 +        sortintvl = SORT_INTVL;
259 + }
260 +
261 +
262 + void
263   ambnotify(obj)                  /* record new modifier */
264   OBJECT  obj;
265   {
266          static int  hitlimit = 0;
267 <        register OBJREC  *o = objptr(obj);
267 >        register OBJREC  *o;
268          register char  **amblp;
269  
270 +        if (obj == OVOID) {             /* starting over */
271 +                ambset[0] = 0;
272 +                hitlimit = 0;
273 +                return;
274 +        }
275 +        o = objptr(obj);
276          if (hitlimit || !ismodifier(o->otype))
277                  return;
278          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 184 | Line 288 | OBJECT obj;
288   }
289  
290  
291 + void
292   ambient(acol, r, nrm)           /* compute ambient component for ray */
293   COLOR  acol;
294   register RAY  *r;
295   FVECT  nrm;
296   {
297          static int  rdepth = 0;                 /* ambient recursion */
298 <        double  d;
298 >        double  d, l;
299  
300          if (ambdiv <= 0)                        /* no ambient calculation */
301                  goto dumbamb;
# Line 210 | Line 315 | FVECT  nrm;
315                          goto dumbamb;
316                  return;
317          }
318 <                                                /* resort memory? */
319 <        sortambvals(0);
318 >
319 >        if (tracktime)                          /* sort to minimize thrashing */
320 >                sortambvals(0);
321                                                  /* get ambient value */
322          setcolor(acol, 0.0, 0.0, 0.0);
323          d = sumambient(acol, r, nrm, rdepth,
# Line 227 | Line 333 | FVECT  nrm;
333                  return;
334   dumbamb:                                        /* return global value */
335          copycolor(acol, ambval);
336 <        if (ambvwt <= 0 | nambvals == 0)
336 >        if (ambvwt <= 0 | navsum == 0)
337                  return;
338 <        scalecolor(acol, (double)ambvwt);
339 <        addcolor(acol, avsum);                  /* average in computations */
340 <        d = 1.0/(ambvwt+nambvals);
341 <        scalecolor(acol, d);
338 >        l = bright(ambval);                     /* average in computations */
339 >        if (l > FTINY) {
340 >                d = (log(l)*(double)ambvwt + avsum) /
341 >                                (double)(ambvwt + navsum);
342 >                d = exp(d) / l;
343 >                scalecolor(acol, d);            /* apply color of ambval */
344 >        } else {
345 >                d = exp( avsum / (double)navsum );
346 >                setcolor(acol, d, d, d);        /* neutral color */
347 >        }
348   }
349  
350  
# Line 256 | Line 368 | double s;
368          wsum = 0.0;
369                                          /* do this node */
370          for (av = at->alist; av != NULL; av = av->next) {
371 +                double  rn_dot = -2.0;
372                  if (tracktime)
373 <                        av->latick = ambclock++;
373 >                        av->latick = ambclock;
374                  /*
375                   *  Ambient level test.
376                   */
# Line 278 | Line 391 | double s;
391                  if (e1 > ambacc*ambacc*1.21)
392                          continue;
393                  /*
394 <                 *  Normal direction test.
394 >                 *  Direction test using closest normal.
395                   */
396 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
396 >                d = DOT(av->dir, r->ron);
397 >                if (rn != r->ron) {
398 >                        rn_dot = DOT(av->dir, rn);
399 >                        if (rn_dot > 1.0-FTINY)
400 >                                rn_dot = 1.0-FTINY;
401 >                        if (rn_dot >= d-FTINY) {
402 >                                d = rn_dot;
403 >                                rn_dot = -2.0;
404 >                        }
405 >                }
406 >                e2 = (1.0 - d) * r->rweight;
407                  if (e2 < 0.0) e2 = 0.0;
408                  if (e1 + e2 > ambacc*ambacc*1.21)
409                          continue;
# Line 296 | Line 419 | double s;
419                  /*
420                   *  Jittering final test reduces image artifacts.
421                   */
422 <                wt = sqrt(e1) + sqrt(e2);
422 >                e1 = sqrt(e1);
423 >                e2 = sqrt(e2);
424 >                wt = e1 + e2;
425                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
426                          continue;
427 +                /*
428 +                 *  Recompute directional error using perturbed normal
429 +                 */
430 +                if (rn_dot > 0.0) {
431 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
432 +                        wt = e1 + e2;
433 +                }
434                  if (wt <= 1e-3)
435                          wt = 1e3;
436                  else
# Line 361 | Line 493 | int  al;
493   }
494  
495  
496 + void
497   extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
498   COLOR  cr;
499   register AMBVAL  *ap;
500   FVECT  pv, nv;
501   {
502 <        FVECT  v1, v2;
502 >        FVECT  v1;
503          register int  i;
504          double  d;
505  
# Line 375 | Line 508 | FVECT  pv, nv;
508          for (i = 0; i < 3; i++)
509                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
510                                          /* gradient due to rotation */
511 <        VCOPY(v1, ap->dir);
512 <        fcross(v2, v1, nv);
380 <        d += DOT(ap->gdir, v2);
511 >        VCROSS(v1, ap->dir, nv);
512 >        d += DOT(ap->gdir, v1);
513          if (d <= 0.0) {
514                  setcolor(cr, 0.0, 0.0, 0.0);
515                  return;
# Line 387 | Line 519 | FVECT  pv, nv;
519   }
520  
521  
522 < static
522 > static void
523   initambfile(creat)              /* initialize ambient file */
524   int  creat;
525   {
526 <        extern char  *progname, *octname, VersionID[];
526 >        extern char  *progname, *octname;
527 >        static char  *mybuf = NULL;
528  
529   #ifdef  F_SETLKW
530          aflock(creat ? F_WRLCK : F_RDLCK);
# Line 399 | Line 532 | int  creat;
532   #ifdef MSDOS
533          setmode(fileno(ambfp), O_BINARY);
534   #endif
535 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
535 >        if (mybuf == NULL)
536 >                mybuf = (char *)bmalloc(BUFSIZ+8);
537 >        setbuf(ambfp, mybuf);
538          if (creat) {                    /* new file */
539                  newheader("RADIANCE", ambfp);
540 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
540 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
541                                  progname, colval(ambval,RED),
542                                  colval(ambval,GRN), colval(ambval,BLU),
543 <                                ambounce, ambacc);
544 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
545 <                                ambdiv, ambssamp, ambres,
546 <                                octname==NULL ? "" : octname);
543 >                                ambvwt, ambounce, ambacc);
544 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
545 >                                ambdiv, ambssamp, ambres);
546 >                if (octname != NULL)
547 >                        printargs(1, &octname, ambfp);
548 >                else
549 >                        fputc('\n', ambfp);
550                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
551 +                fputnow(ambfp);
552                  fputformat(AMBFMT, ambfp);
553                  putc('\n', ambfp);
554                  putambmagic(ambfp);
# Line 418 | Line 557 | int  creat;
557   }
558  
559  
560 < static
560 > static void
561   avsave(av)                              /* insert and save an ambient value */
562   AMBVAL  *av;
563   {
# Line 432 | Line 571 | AMBVAL *av;
571                          goto writerr;
572          return;
573   writerr:
574 <        error(SYSTEM, "error writing ambient file");
574 >        error(SYSTEM, "error writing to ambient file");
575   }
576  
577  
# Line 441 | Line 580 | avstore(aval)                          /* allocate memory and store aval */
580   register AMBVAL  *aval;
581   {
582          register AMBVAL  *av;
583 +        double  d;
584  
585          if ((av = newambval()) == NULL)
586                  error(SYSTEM, "out of memory in avstore");
587          copystruct(av, aval);
588          av->latick = ambclock;
589          av->next = NULL;
450        addcolor(avsum, av->val);       /* add to sum for averaging */
590          nambvals++;
591 +        d = bright(av->val);
592 +        if (d > FTINY) {                /* add to log sum for averaging */
593 +                avsum += log(d);
594 +                navsum++;
595 +        }
596          return(av);
597   }
598  
# Line 458 | Line 602 | register AMBVAL  *aval;
602   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
603  
604  
605 < static
462 < AMBTREE *
605 > static AMBTREE *
606   newambtree()                            /* allocate 8 ambient tree structs */
607   {
608          register AMBTREE  *atp, *upperlim;
609  
610          if (atfreelist == NULL) {       /* get more nodes */
611 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
611 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
612                  if (atfreelist == NULL)
613                          return(NULL);
614                                          /* link new free list */
# Line 481 | Line 624 | newambtree()                           /* allocate 8 ambient tree structs */
624   }
625  
626  
627 < static
627 > static void
628   freeambtree(atp)                        /* free 8 ambient tree structs */
629   AMBTREE  *atp;
630   {
# Line 490 | Line 633 | AMBTREE  *atp;
633   }
634  
635  
636 < static
636 > static void
637   avinsert(av)                            /* insert ambient value in our tree */
638   register AMBVAL  *av;
639   {
# Line 530 | Line 673 | register AMBVAL         *av;
673   }
674  
675  
676 < static
676 > static void
677   unloadatree(at, f)                      /* unload an ambient value tree */
678   register AMBTREE  *at;
679 < int     (*f)();
679 > void    (*f)();
680   {
681          register AMBVAL  *av;
682          register int  i;
# Line 551 | Line 694 | int    (*f)();
694   }
695  
696  
697 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
697 > static struct avl {
698 >        AMBVAL  *p;
699 >        unsigned long   t;
700 > }       *avlist1;                       /* ambient value list with ticks */
701 > static AMBVAL   **avlist2;              /* memory positions for sorting */
702   static int      i_avlist;               /* index for lists */
703  
704  
705 < static
705 > static int
706   av2list(av)
707 < AMBVAL  *av;
707 > register AMBVAL *av;
708   {
709   #ifdef DEBUG
710          if (i_avlist >= nambvals)
711                  error(CONSISTENCY, "too many ambient values in av2list1");
712   #endif
713 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
714 <        i_avlist++;
713 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
714 >        avlist1[i_avlist++].t = av->latick;
715   }
716  
717  
718   static int
719 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
720 < AMBVAL  **avp1, **avp2;
719 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
720 > struct avl      *av1, *av2;
721   {
722 <        return((**avp2).latick - (**avp1).latick);
722 >        register long  lc = av2->t - av1->t;
723 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
724   }
725  
726  
727 + /* GW NOTE 2002/10/3:
728 + * I used to compare AMBVAL pointers, but found that this was the
729 + * cause of a serious consistency error with gcc, since the optimizer
730 + * uses some dangerous trick in pointer subtraction that
731 + * assumes pointers differ by exact struct size increments.
732 + */
733   static int
734   aposcmp(avp1, avp2)                     /* compare ambient value positions */
735 < AMBVAL  **avp1, **avp2;
735 > const void      *avp1, *avp2;
736   {
737 <        return(*avp1 - *avp2);
737 >        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
738 >        if (diff < 0)
739 >                return(-1);
740 >        return(diff > 0);
741   }
742  
743 <
587 < #ifdef DEBUG
743 > #if 1
744   static int
745   avlmemi(avaddr)                         /* find list position from address */
746   AMBVAL  *avaddr;
# Line 603 | Line 759 | AMBVAL *avaddr;
759   #endif
760  
761  
762 < static
762 > static void
763   sortambvals(always)                     /* resort ambient values */
764   int     always;
765   {
# Line 611 | Line 767 | int    always;
767          AMBVAL  tav, *tap, *pnext;
768          register int    i, j;
769                                          /* see if it's time yet */
770 <        if (!always && (ambclock < lastsort+sortintvl ||
770 >        if (!always && (ambclock++ < lastsort+sortintvl ||
771                          nambvals < SORT_THRESH))
772                  return;
773          /*
# Line 630 | Line 786 | int    always;
786           * tree will be rebuilt with the new accuracy parameter.
787           */
788          if (tracktime) {                /* allocate pointer arrays to sort */
633                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
789                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
790 <        } else
791 <                avlist1 = avlist2 = NULL;
792 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
793 <                if (avlist1 != NULL)
794 <                        free((char *)avlist1);
790 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
791 >        } else {
792 >                avlist2 = NULL;
793 >                avlist1 = NULL;
794 >        }
795 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
796 >                if (avlist2 != NULL)
797 >                        free((void *)avlist2);
798                  if (always) {           /* rebuild without sorting */
799                          copystruct(&oldatrunk, &atrunk);
800                          atrunk.alist = NULL;
# Line 663 | Line 821 | int    always;
821                  if (i_avlist < nambvals)
822                          error(CONSISTENCY, "missing ambient values in sortambvals");
823   #endif
824 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
824 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
825                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
826                  for (i = 0; i < nambvals; i++) {
827 <                        if (avlist1[i] == NULL)
827 >                        if (avlist1[i].p == NULL)
828                                  continue;
829                          tap = avlist2[i];
830                          copystruct(&tav, tap);
831 <                        for (j = i; (pnext = avlist1[j]) != tap;
831 >                        for (j = i; (pnext = avlist1[j].p) != tap;
832                                          j = avlmemi(pnext)) {
833                                  copystruct(avlist2[j], pnext);
834                                  avinsert(avlist2[j]);
835 <                                avlist1[j] = NULL;
835 >                                avlist1[j].p = NULL;
836                          }
837                          copystruct(avlist2[j], &tav);
838                          avinsert(avlist2[j]);
839 <                        avlist1[j] = NULL;
839 >                        avlist1[j].p = NULL;
840                  }
841 <                free((char *)avlist1);
842 <                free((char *)avlist2);
841 >                free((void *)avlist1);
842 >                free((void *)avlist2);
843                                                  /* compute new sort interval */
844                  sortintvl = ambclock - lastsort;
845                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 700 | Line 858 | int    always;
858  
859   #ifdef  F_SETLKW
860  
861 < static
861 > static void
862   aflock(typ)                     /* lock/unlock ambient file */
863   int  typ;
864   {
# Line 715 | Line 873 | int  typ;
873   int
874   ambsync()                       /* synchronize ambient file */
875   {
718        static FILE  *ambinp = NULL;
719        static long  lastpos = -1;
876          long  flen;
877          AMBVAL  avs;
878          register int  n;
# Line 728 | Line 884 | ambsync()                      /* synchronize ambient file */
884                                  /* gain exclusive access */
885          aflock(F_WRLCK);
886                                  /* see if file has grown */
887 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
887 >        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
888                  goto seekerr;
889          if (n = flen - lastpos) {               /* file has grown */
890                  if (ambinp == NULL) {           /* use duplicate filedes */
# Line 741 | Line 897 | ambsync()                      /* synchronize ambient file */
897                  while (n >= AMBVALSIZ) {        /* load contributed values */
898                          if (!readambval(&avs, ambinp)) {
899                                  sprintf(errmsg,
900 <                                "ambient file corrupted near character %ld",
901 <                                                flen - n);
900 >                        "ambient file \"%s\" corrupted near character %ld",
901 >                                                ambfile, flen - n);
902                                  error(WARNING, errmsg);
903                                  break;
904                          }
# Line 751 | Line 907 | ambsync()                      /* synchronize ambient file */
907                  }
908                  /*** seek always as safety measure
909                  if (n) ***/                     /* alignment */
910 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
910 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
911                                  goto seekerr;
912          }
913   #ifdef  DEBUG
# Line 764 | Line 920 | ambsync()                      /* synchronize ambient file */
920   #endif
921   syncend:
922          n = fflush(ambfp);                      /* calls write() at last */
923 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
923 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
924                  goto seekerr;
925          aflock(F_UNLCK);                        /* release file */
926          nunflshed = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines