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.35 by greg, Tue Nov 21 14:28:22 1995 UTC vs.
Revision 2.53 by schorsch, Mon Jul 21 22:30:19 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 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
12 > #include <string.h>
13  
14 + #include  "platform.h"
15 + #include  "ray.h"
16   #include  "otypes.h"
16
17   #include  "ambient.h"
18
18   #include  "random.h"
19  
20   #ifndef  OCTSCALE
21   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
22   #endif
24 #ifndef  AMBVWT
25 #define  AMBVWT         250     /* relative ambient value weight (# calcs) */
26 #endif
23  
28 typedef struct ambtree {
29        AMBVAL  *alist;         /* ambient value list */
30        struct ambtree  *kid;   /* 8 child nodes */
31 }  AMBTREE;                     /* ambient octree */
32
33 extern CUBE  thescene;          /* contains space boundaries */
34
24   extern char  *shm_boundary;     /* memory sharing boundary */
25  
26   #define  MAXASET        511     /* maximum number of elements in ambient set */
# Line 46 | Line 35 | static FILE  *ambfp = NULL;    /* ambient file pointer */
35   static int  nunflshed = 0;      /* number of unflushed ambient values */
36  
37   #ifndef SORT_THRESH
38 < #ifdef BIGMEM
50 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
51 < #else
38 > #ifdef SMLMEM
39   #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
40 + #else
41 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
42   #endif
43   #endif
44   #ifndef SORT_INTVL
45 < #define SORT_INTVL      (SORT_THRESH*256)
45 > #define SORT_INTVL      (SORT_THRESH<<1)
46   #endif
47   #ifndef MAX_SORT_INTVL
48 < #define MAX_SORT_INTVL  (SORT_INTVL<<4)
48 > #define MAX_SORT_INTVL  (SORT_INTVL<<6)
49   #endif
50  
51 < static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
51 > static double  avsum = 0.;              /* computed ambient value sum (log) */
52 > static unsigned int  navsum = 0;        /* number of values in avsum */
53   static unsigned int  nambvals = 0;      /* total number of indirect values */
54   static unsigned int  nambshare = 0;     /* number of values from file */
55   static unsigned long  ambclock = 0;     /* ambient access clock */
56   static unsigned long  lastsort = 0;     /* time of last value sort */
57   static long  sortintvl = SORT_INTVL;    /* time until next sort */
58 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
59 + static long  lastpos = -1;              /* last flush position */
60  
61   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
62          /*
# Line 72 | Line 64 | static long  sortintvl = SORT_INTVL;   /* time until nex
64           * through memory on a multiprocessor, when we want to avoid
65           * claiming our own memory (copy on write).  Go ahead anyway
66           * if more than two thirds of our values are unshared.
67 +         * Compile with -Dtracktime=0 to turn this code off.
68           */
69 + #ifndef tracktime
70   #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
71 + #endif
72  
73   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
74  
75 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
75 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
76 > #define  freeav(av)     free((void *)av);
77  
78 < extern long  ftell(), lseek();
79 < static int  initambfile(), avsave(), avinsert(), sortambvals();
78 > static void  initambfile(), avsave(), avinsert(), sortambvals(), unloadatree();
79 > static int  avlmemi();
80   static AMBVAL  *avstore();
81   #ifdef  F_SETLKW
82 < static  aflock();
82 > static void  aflock();
83   #endif
84  
85  
86 + void
87   setambres(ar)                           /* set ambient resolution */
88   int  ar;
89   {
# Line 108 | Line 105 | int  ar;
105   }
106  
107  
108 + void
109   setambacc(newa)                         /* set ambient accuracy */
110   double  newa;
111   {
# Line 121 | Line 119 | double  newa;
119   }
120  
121  
122 < setambient(afile)                       /* initialize calculation */
123 < char  *afile;
122 > void
123 > setambient()                            /* initialize calculation */
124   {
125 <        long  headlen;
125 >        int     readonly = 0;
126 >        long  pos, flen;
127          AMBVAL  amb;
128 +                                                /* make sure we're fresh */
129 +        ambdone();
130                                                  /* init ambient limits */
131          setambres(ambres);
132          setambacc(ambacc);
133 <        if (afile == NULL)
133 >        if (ambfile == NULL || !ambfile[0])
134                  return;
135          if (ambacc <= FTINY) {
136                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
137 <                                afile);
137 >                                ambfile);
138                  error(WARNING, errmsg);
139                  return;
140          }
141                                                  /* open ambient file */
142 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
143 <                initambfile(0);
144 <                headlen = ftell(ambfp);
142 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
143 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
144 >        if (ambfp != NULL) {
145 >                initambfile(0);                 /* file exists */
146 >                pos = ftell(ambfp);
147                  while (readambval(&amb, ambfp))
148                          avinsert(avstore(&amb));
149 <                                                /* align */
150 <                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
151 <                nambshare = nambvals;
152 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
153 <                initambfile(1);
154 <        else {
155 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
149 >                nambshare = nambvals;           /* share loaded values */
150 >                if (readonly) {
151 >                        sprintf(errmsg,
152 >                                "loaded %u values from read-only ambient file",
153 >                                        nambvals);
154 >                        error(WARNING, errmsg);
155 >                        fclose(ambfp);          /* close file so no writes */
156 >                        ambfp = NULL;
157 >                        return;                 /* avoid ambsync() */
158 >                }
159 >                                                /* align file pointer */
160 >                pos += (long)nambvals*AMBVALSIZ;
161 >                flen = lseek(fileno(ambfp), (off_t)0L, 2);
162 >                if (flen != pos) {
163 >                        sprintf(errmsg,
164 >                        "ignoring last %ld values in ambient file (corrupted)",
165 >                                        (flen - pos)/AMBVALSIZ);
166 >                        error(WARNING, errmsg);
167 >                        fseek(ambfp, pos, 0);
168 > #ifndef _WIN32 /* XXX we need a replacement for that one */
169 >                        ftruncate(fileno(ambfp), (off_t)pos);
170 > #endif
171 >                }
172 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
173 >                initambfile(1);                 /* else create new file */
174 >        } else {
175 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
176                  error(SYSTEM, errmsg);
177          }
178          nunflshed++;    /* lie */
# Line 157 | Line 180 | char  *afile;
180   }
181  
182  
183 + void
184 + ambdone()                       /* close ambient file and free memory */
185 + {
186 +        if (ambfp != NULL) {            /* close ambient file */
187 +                ambsync();
188 +                fclose(ambfp);
189 +                ambfp = NULL;
190 +                if (ambinp != NULL) {  
191 +                        fclose(ambinp);
192 +                        ambinp = NULL;
193 +                }
194 +                lastpos = -1;
195 +        }
196 +                                        /* free ambient tree */
197 +        unloadatree(&atrunk, free);
198 +                                        /* reset state variables */
199 +        avsum = 0.;
200 +        navsum = 0;
201 +        nambvals = 0;
202 +        nambshare = 0;
203 +        ambclock = 0;
204 +        lastsort = 0;
205 +        sortintvl = SORT_INTVL;
206 + }
207 +
208 +
209 + void
210   ambnotify(obj)                  /* record new modifier */
211   OBJECT  obj;
212   {
213          static int  hitlimit = 0;
214 <        register OBJREC  *o = objptr(obj);
214 >        register OBJREC  *o;
215          register char  **amblp;
216  
217 +        if (obj == OVOID) {             /* starting over */
218 +                ambset[0] = 0;
219 +                hitlimit = 0;
220 +                return;
221 +        }
222 +        o = objptr(obj);
223          if (hitlimit || !ismodifier(o->otype))
224                  return;
225          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 179 | Line 235 | OBJECT obj;
235   }
236  
237  
238 + void
239   ambient(acol, r, nrm)           /* compute ambient component for ray */
240   COLOR  acol;
241   register RAY  *r;
242   FVECT  nrm;
243   {
244          static int  rdepth = 0;                 /* ambient recursion */
245 <        double  d;
245 >        double  d, l;
246  
247          if (ambdiv <= 0)                        /* no ambient calculation */
248                  goto dumbamb;
# Line 205 | Line 262 | FVECT  nrm;
262                          goto dumbamb;
263                  return;
264          }
265 <                                                /* resort memory? */
266 <        sortambvals(0);
265 >
266 >        if (tracktime)                          /* sort to minimize thrashing */
267 >                sortambvals(0);
268                                                  /* get ambient value */
269          setcolor(acol, 0.0, 0.0, 0.0);
270          d = sumambient(acol, r, nrm, rdepth,
# Line 222 | Line 280 | FVECT  nrm;
280                  return;
281   dumbamb:                                        /* return global value */
282          copycolor(acol, ambval);
283 < #if  AMBVWT
226 <        if (nambvals == 0)
283 >        if (ambvwt <= 0 | navsum == 0)
284                  return;
285 <        scalecolor(acol, (double)AMBVWT);
286 <        addcolor(acol, avsum);                  /* average in computations */
287 <        d = 1.0/(AMBVWT+nambvals);
288 <        scalecolor(acol, d);
289 < #endif
285 >        l = bright(ambval);                     /* average in computations */
286 >        if (l > FTINY) {
287 >                d = (log(l)*(double)ambvwt + avsum) /
288 >                                (double)(ambvwt + navsum);
289 >                d = exp(d) / l;
290 >                scalecolor(acol, d);            /* apply color of ambval */
291 >        } else {
292 >                d = exp( avsum / (double)navsum );
293 >                setcolor(acol, d, d, d);        /* neutral color */
294 >        }
295   }
296  
297  
# Line 253 | Line 315 | double s;
315          wsum = 0.0;
316                                          /* do this node */
317          for (av = at->alist; av != NULL; av = av->next) {
318 +                double  rn_dot = -2.0;
319                  if (tracktime)
320 <                        av->latick = ambclock++;
320 >                        av->latick = ambclock;
321                  /*
322                   *  Ambient level test.
323                   */
# Line 275 | Line 338 | double s;
338                  if (e1 > ambacc*ambacc*1.21)
339                          continue;
340                  /*
341 <                 *  Normal direction test.
341 >                 *  Direction test using closest normal.
342                   */
343 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
343 >                d = DOT(av->dir, r->ron);
344 >                if (rn != r->ron) {
345 >                        rn_dot = DOT(av->dir, rn);
346 >                        if (rn_dot > 1.0-FTINY)
347 >                                rn_dot = 1.0-FTINY;
348 >                        if (rn_dot >= d-FTINY) {
349 >                                d = rn_dot;
350 >                                rn_dot = -2.0;
351 >                        }
352 >                }
353 >                e2 = (1.0 - d) * r->rweight;
354                  if (e2 < 0.0) e2 = 0.0;
355                  if (e1 + e2 > ambacc*ambacc*1.21)
356                          continue;
# Line 293 | Line 366 | double s;
366                  /*
367                   *  Jittering final test reduces image artifacts.
368                   */
369 <                wt = sqrt(e1) + sqrt(e2);
369 >                e1 = sqrt(e1);
370 >                e2 = sqrt(e2);
371 >                wt = e1 + e2;
372                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
373                          continue;
374 +                /*
375 +                 *  Recompute directional error using perturbed normal
376 +                 */
377 +                if (rn_dot > 0.0) {
378 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
379 +                        wt = e1 + e2;
380 +                }
381                  if (wt <= 1e-3)
382                          wt = 1e3;
383                  else
# Line 358 | Line 440 | int  al;
440   }
441  
442  
443 + void
444   extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
445   COLOR  cr;
446   register AMBVAL  *ap;
447   FVECT  pv, nv;
448   {
449 <        FVECT  v1, v2;
449 >        FVECT  v1;
450          register int  i;
451          double  d;
452  
# Line 372 | Line 455 | FVECT  pv, nv;
455          for (i = 0; i < 3; i++)
456                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
457                                          /* gradient due to rotation */
458 <        VCOPY(v1, ap->dir);
459 <        fcross(v2, v1, nv);
377 <        d += DOT(ap->gdir, v2);
458 >        VCROSS(v1, ap->dir, nv);
459 >        d += DOT(ap->gdir, v1);
460          if (d <= 0.0) {
461                  setcolor(cr, 0.0, 0.0, 0.0);
462                  return;
# Line 384 | Line 466 | FVECT  pv, nv;
466   }
467  
468  
469 < static
469 > static void
470   initambfile(creat)              /* initialize ambient file */
471   int  creat;
472   {
473 <        extern char  *progname, *octname, VersionID[];
473 >        extern char  *progname, *octname;
474 >        static char  *mybuf = NULL;
475  
476   #ifdef  F_SETLKW
477          aflock(creat ? F_WRLCK : F_RDLCK);
478   #endif
479 < #ifdef MSDOS
480 <        setmode(fileno(ambfp), O_BINARY);
481 < #endif
482 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
479 >        SET_FILE_BINARY(ambfp);
480 >        if (mybuf == NULL)
481 >                mybuf = (char *)bmalloc(BUFSIZ+8);
482 >        setbuf(ambfp, mybuf);
483          if (creat) {                    /* new file */
484                  newheader("RADIANCE", ambfp);
485 <                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
485 >                fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
486                                  progname, colval(ambval,RED),
487                                  colval(ambval,GRN), colval(ambval,BLU),
488 <                                ambounce, ambacc);
489 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
490 <                                ambdiv, ambssamp, ambres,
491 <                                octname==NULL ? "" : octname);
488 >                                ambvwt, ambounce, ambacc);
489 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
490 >                                ambdiv, ambssamp, ambres);
491 >                if (octname != NULL)
492 >                        printargs(1, &octname, ambfp);
493 >                else
494 >                        fputc('\n', ambfp);
495                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
496 +                fputnow(ambfp);
497                  fputformat(AMBFMT, ambfp);
498                  putc('\n', ambfp);
499                  putambmagic(ambfp);
# Line 415 | Line 502 | int  creat;
502   }
503  
504  
505 < static
505 > static void
506   avsave(av)                              /* insert and save an ambient value */
507   AMBVAL  *av;
508   {
# Line 429 | Line 516 | AMBVAL *av;
516                          goto writerr;
517          return;
518   writerr:
519 <        error(SYSTEM, "error writing ambient file");
519 >        error(SYSTEM, "error writing to ambient file");
520   }
521  
522  
# Line 438 | Line 525 | avstore(aval)                          /* allocate memory and store aval */
525   register AMBVAL  *aval;
526   {
527          register AMBVAL  *av;
528 +        double  d;
529  
530          if ((av = newambval()) == NULL)
531                  error(SYSTEM, "out of memory in avstore");
532 <        copystruct(av, aval);
532 >        *av = *aval;
533          av->latick = ambclock;
534          av->next = NULL;
447        addcolor(avsum, av->val);       /* add to sum for averaging */
535          nambvals++;
536 +        d = bright(av->val);
537 +        if (d > FTINY) {                /* add to log sum for averaging */
538 +                avsum += log(d);
539 +                navsum++;
540 +        }
541          return(av);
542   }
543  
# Line 455 | Line 547 | register AMBVAL  *aval;
547   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
548  
549  
550 < static
459 < AMBTREE *
550 > static AMBTREE *
551   newambtree()                            /* allocate 8 ambient tree structs */
552   {
553          register AMBTREE  *atp, *upperlim;
554  
555          if (atfreelist == NULL) {       /* get more nodes */
556 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
556 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
557                  if (atfreelist == NULL)
558                          return(NULL);
559                                          /* link new free list */
# Line 473 | Line 564 | newambtree()                           /* allocate 8 ambient tree structs */
564          }
565          atp = atfreelist;
566          atfreelist = atp->kid;
567 <        bzero((char *)atp, 8*sizeof(AMBTREE));
567 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
568          return(atp);
569   }
570  
571  
572 < static
572 > static void
573   freeambtree(atp)                        /* free 8 ambient tree structs */
574   AMBTREE  *atp;
575   {
# Line 487 | Line 578 | AMBTREE  *atp;
578   }
579  
580  
581 < static
581 > static void
582   avinsert(av)                            /* insert ambient value in our tree */
583   register AMBVAL  *av;
584   {
# Line 527 | Line 618 | register AMBVAL         *av;
618   }
619  
620  
621 < static
621 > static void
622   unloadatree(at, f)                      /* unload an ambient value tree */
623   register AMBTREE  *at;
624 < int     (*f)();
624 > void    (*f)();
625   {
626          register AMBVAL  *av;
627          register int  i;
# Line 548 | Line 639 | int    (*f)();
639   }
640  
641  
642 < static AMBVAL   **avlist1, **avlist2;   /* ambient value lists for sorting */
642 > static struct avl {
643 >        AMBVAL  *p;
644 >        unsigned long   t;
645 > }       *avlist1;                       /* ambient value list with ticks */
646 > static AMBVAL   **avlist2;              /* memory positions for sorting */
647   static int      i_avlist;               /* index for lists */
648  
649  
650 < static
650 > static int
651   av2list(av)
652 < AMBVAL  *av;
652 > register AMBVAL *av;
653   {
654   #ifdef DEBUG
655          if (i_avlist >= nambvals)
656                  error(CONSISTENCY, "too many ambient values in av2list1");
657   #endif
658 <        avlist1[i_avlist] = avlist2[i_avlist] = av;
659 <        i_avlist++;
658 >        avlist1[i_avlist].p = avlist2[i_avlist] = av;
659 >        avlist1[i_avlist++].t = av->latick;
660   }
661  
662  
663   static int
664 < alatcmp(avp1, avp2)                     /* compare ambient values for MRA */
665 < AMBVAL  **avp1, **avp2;
664 > alatcmp(av1, av2)                       /* compare ambient values for MRA */
665 > struct avl      *av1, *av2;
666   {
667 <        return((**avp2).latick - (**avp1).latick);
667 >        register long  lc = av2->t - av1->t;
668 >        return(lc<0 ? -1 : lc>0 ? 1 : 0);
669   }
670  
671  
672 + /* GW NOTE 2002/10/3:
673 + * I used to compare AMBVAL pointers, but found that this was the
674 + * cause of a serious consistency error with gcc, since the optimizer
675 + * uses some dangerous trick in pointer subtraction that
676 + * assumes pointers differ by exact struct size increments.
677 + */
678   static int
679   aposcmp(avp1, avp2)                     /* compare ambient value positions */
680 < AMBVAL  **avp1, **avp2;
680 > const void      *avp1, *avp2;
681   {
682 <        return(*avp1 - *avp2);
682 >        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
683 >        if (diff < 0)
684 >                return(-1);
685 >        return(diff > 0);
686   }
687  
688 <
584 < #ifdef DEBUG
688 > #if 1
689   static int
690   avlmemi(avaddr)                         /* find list position from address */
691   AMBVAL  *avaddr;
# Line 600 | Line 704 | AMBVAL *avaddr;
704   #endif
705  
706  
707 < static
707 > static void
708   sortambvals(always)                     /* resort ambient values */
709   int     always;
710   {
# Line 608 | Line 712 | int    always;
712          AMBVAL  tav, *tap, *pnext;
713          register int    i, j;
714                                          /* see if it's time yet */
715 <        if (!always && (ambclock < lastsort+sortintvl ||
715 >        if (!always && (ambclock++ < lastsort+sortintvl ||
716                          nambvals < SORT_THRESH))
717                  return;
718          /*
# Line 627 | Line 731 | int    always;
731           * tree will be rebuilt with the new accuracy parameter.
732           */
733          if (tracktime) {                /* allocate pointer arrays to sort */
630                avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
734                  avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
735 <        } else
736 <                avlist1 = avlist2 = NULL;
737 <        if (avlist2 == NULL) {          /* no time tracking -- rebuild tree? */
738 <                if (avlist1 != NULL)
739 <                        free((char *)avlist1);
735 >                avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
736 >        } else {
737 >                avlist2 = NULL;
738 >                avlist1 = NULL;
739 >        }
740 >        if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
741 >                if (avlist2 != NULL)
742 >                        free((void *)avlist2);
743                  if (always) {           /* rebuild without sorting */
744 <                        copystruct(&oldatrunk, &atrunk);
744 >                        oldatrunk = atrunk;
745                          atrunk.alist = NULL;
746                          atrunk.kid = NULL;
747                          unloadatree(&oldatrunk, avinsert);
# Line 660 | Line 766 | int    always;
766                  if (i_avlist < nambvals)
767                          error(CONSISTENCY, "missing ambient values in sortambvals");
768   #endif
769 <                qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
769 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
770                  qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
771                  for (i = 0; i < nambvals; i++) {
772 <                        if (avlist1[i] == NULL)
772 >                        if (avlist1[i].p == NULL)
773                                  continue;
774                          tap = avlist2[i];
775 <                        copystruct(&tav, tap);
776 <                        for (j = i; (pnext = avlist1[j]) != tap;
775 >                        tav = *tap;
776 >                        for (j = i; (pnext = avlist1[j].p) != tap;
777                                          j = avlmemi(pnext)) {
778 <                                copystruct(avlist2[j], pnext);
778 >                                *(avlist2[j]) = *pnext;
779                                  avinsert(avlist2[j]);
780 <                                avlist1[j] = NULL;
780 >                                avlist1[j].p = NULL;
781                          }
782 <                        copystruct(avlist2[j], &tav);
782 >                        *(avlist2[j]) = tav;
783                          avinsert(avlist2[j]);
784 <                        avlist1[j] = NULL;
784 >                        avlist1[j].p = NULL;
785                  }
786 <                free((char *)avlist1);
787 <                free((char *)avlist2);
786 >                free((void *)avlist1);
787 >                free((void *)avlist2);
788                                                  /* compute new sort interval */
789                  sortintvl = ambclock - lastsort;
790                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 697 | Line 803 | int    always;
803  
804   #ifdef  F_SETLKW
805  
806 < static
806 > static void
807   aflock(typ)                     /* lock/unlock ambient file */
808   int  typ;
809   {
# Line 712 | Line 818 | int  typ;
818   int
819   ambsync()                       /* synchronize ambient file */
820   {
715        static FILE  *ambinp = NULL;
716        static long  lastpos = -1;
821          long  flen;
822          AMBVAL  avs;
823          register int  n;
# Line 725 | Line 829 | ambsync()                      /* synchronize ambient file */
829                                  /* gain exclusive access */
830          aflock(F_WRLCK);
831                                  /* see if file has grown */
832 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
832 >        if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0)
833                  goto seekerr;
834          if (n = flen - lastpos) {               /* file has grown */
835                  if (ambinp == NULL) {           /* use duplicate filedes */
# Line 736 | Line 840 | ambsync()                      /* synchronize ambient file */
840                  if (fseek(ambinp, lastpos, 0) < 0)
841                          goto seekerr;
842                  while (n >= AMBVALSIZ) {        /* load contributed values */
843 <                        readambval(&avs, ambinp);
843 >                        if (!readambval(&avs, ambinp)) {
844 >                                sprintf(errmsg,
845 >                        "ambient file \"%s\" corrupted near character %ld",
846 >                                                ambfile, flen - n);
847 >                                error(WARNING, errmsg);
848 >                                break;
849 >                        }
850                          avinsert(avstore(&avs));
851                          n -= AMBVALSIZ;
852                  }
853                  /*** seek always as safety measure
854                  if (n) ***/                     /* alignment */
855 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
855 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0)
856                                  goto seekerr;
857          }
858   #ifdef  DEBUG
# Line 755 | Line 865 | ambsync()                      /* synchronize ambient file */
865   #endif
866   syncend:
867          n = fflush(ambfp);                      /* calls write() at last */
868 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
868 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0)
869                  goto seekerr;
870          aflock(F_UNLCK);                        /* release file */
871          nunflshed = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines