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.44 by gwlarson, Wed Jun 17 13:29:55 1998 UTC vs.
Revision 2.57 by greg, Fri Nov 5 17:36:55 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 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"
17 <
17 > #include  "resolu.h"
18   #include  "ambient.h"
18
19   #include  "random.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23   #endif
24  
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
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        2047    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 43 | Line 38 | static FILE  *ambfp = NULL;    /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40   #ifndef SORT_THRESH
41 < #ifdef BIGMEM
47 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
48 < #else
41 > #ifdef SMLMEM
42   #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
43 + #else
44 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# Line 63 | Line 58 | static unsigned int  nambshare = 0;    /* number of value
58   static unsigned long  ambclock = 0;     /* ambient access clock */
59   static unsigned long  lastsort = 0;     /* time of last value sort */
60   static long  sortintvl = SORT_INTVL;    /* time until next sort */
61 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
62 + static long  lastpos = -1;              /* last flush position */
63  
64   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
65          /*
# Line 78 | Line 75 | static long  sortintvl = SORT_INTVL;   /* time until nex
75  
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
78 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 > #define  freeav(av)     free((void *)av);
80  
81 < static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
82 < static AMBVAL  *avstore();
81 > static void initambfile(int creat);
82 > static void avsave(AMBVAL *av);
83 > static AMBVAL *avstore(AMBVAL  *aval);
84 > static AMBTREE *newambtree(void);
85 > static void freeambtree(AMBTREE  *atp);
86 >
87 > typedef void unloadtf_t(void *);
88 > static unloadtf_t avinsert;
89 > static unloadtf_t av2list;
90 > static void unloadatree(AMBTREE  *at, unloadtf_t *f);
91 >
92 > static int aposcmp(const void *avp1, const void *avp2);
93 > static int avlmemi(AMBVAL *avaddr);
94 > static void sortambvals(int always);
95 >
96   #ifdef  F_SETLKW
97 < static  aflock();
97 > static void aflock(int  typ);
98   #endif
99  
100  
101 < setambres(ar)                           /* set ambient resolution */
102 < int  ar;
101 > extern void
102 > setambres(                              /* set ambient resolution */
103 >        int  ar
104 > )
105   {
106          ambres = ar < 0 ? 0 : ar;               /* may be done already */
107                                                  /* set min & max radii */
# Line 108 | Line 121 | int  ar;
121   }
122  
123  
124 < setambacc(newa)                         /* set ambient accuracy */
125 < double  newa;
124 > extern void
125 > setambacc(                              /* set ambient accuracy */
126 >        double  newa
127 > )
128   {
129          double  ambdiff;
130  
# Line 121 | Line 136 | double  newa;
136   }
137  
138  
139 < setambient(afile)                       /* initialize calculation */
140 < char  *afile;
139 > extern void
140 > setambient(void)                                /* initialize calculation */
141   {
142 +        int     readonly = 0;
143          long  pos, flen;
144          AMBVAL  amb;
145 +                                                /* make sure we're fresh */
146 +        ambdone();
147                                                  /* init ambient limits */
148          setambres(ambres);
149          setambacc(ambacc);
150 <        if (afile == NULL)
150 >        if (ambfile == NULL || !ambfile[0])
151                  return;
152          if (ambacc <= FTINY) {
153                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
154 <                                afile);
154 >                                ambfile);
155                  error(WARNING, errmsg);
156                  return;
157          }
158                                                  /* open ambient file */
159 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
160 <                initambfile(0);
159 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
160 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161 >        if (ambfp != NULL) {
162 >                initambfile(0);                 /* file exists */
163                  pos = ftell(ambfp);
164                  while (readambval(&amb, ambfp))
165                          avinsert(avstore(&amb));
166 <                                                /* align */
166 >                nambshare = nambvals;           /* share loaded values */
167 >                if (readonly) {
168 >                        sprintf(errmsg,
169 >                                "loaded %u values from read-only ambient file",
170 >                                        nambvals);
171 >                        error(WARNING, errmsg);
172 >                        fclose(ambfp);          /* close file so no writes */
173 >                        ambfp = NULL;
174 >                        return;                 /* avoid ambsync() */
175 >                }
176 >                                                /* align file pointer */
177                  pos += (long)nambvals*AMBVALSIZ;
178 <                flen = lseek(fileno(ambfp), 0L, 2);
178 >                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179                  if (flen != pos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182                                          (flen - pos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184                          fseek(ambfp, pos, 0);
185 <                        ftruncate(fileno(ambfp), pos);
185 > #ifndef _WIN32 /* XXX we need a replacement for that one */
186 >                        ftruncate(fileno(ambfp), (off_t)pos);
187 > #endif
188                  }
189 <                nambshare = nambvals;
190 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
191 <                initambfile(1);
192 <        else {
161 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
189 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190 >                initambfile(1);                 /* else create new file */
191 >        } else {
192 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
193                  error(SYSTEM, errmsg);
194          }
195          nunflshed++;    /* lie */
# Line 166 | Line 197 | char  *afile;
197   }
198  
199  
200 < ambnotify(obj)                  /* record new modifier */
201 < OBJECT  obj;
200 > extern void
201 > ambdone(void)                   /* close ambient file and free memory */
202   {
203 +        if (ambfp != NULL) {            /* close ambient file */
204 +                ambsync();
205 +                fclose(ambfp);
206 +                ambfp = NULL;
207 +                if (ambinp != NULL) {  
208 +                        fclose(ambinp);
209 +                        ambinp = NULL;
210 +                }
211 +                lastpos = -1;
212 +        }
213 +                                        /* free ambient tree */
214 +        unloadatree(&atrunk, free);
215 +                                        /* reset state variables */
216 +        avsum = 0.;
217 +        navsum = 0;
218 +        nambvals = 0;
219 +        nambshare = 0;
220 +        ambclock = 0;
221 +        lastsort = 0;
222 +        sortintvl = SORT_INTVL;
223 + }
224 +
225 +
226 + extern void
227 + ambnotify(                      /* record new modifier */
228 +        OBJECT  obj
229 + )
230 + {
231          static int  hitlimit = 0;
232 <        register OBJREC  *o = objptr(obj);
232 >        register OBJREC  *o;
233          register char  **amblp;
234  
235 +        if (obj == OVOID) {             /* starting over */
236 +                ambset[0] = 0;
237 +                hitlimit = 0;
238 +                return;
239 +        }
240 +        o = objptr(obj);
241          if (hitlimit || !ismodifier(o->otype))
242                  return;
243          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 188 | Line 253 | OBJECT obj;
253   }
254  
255  
256 < ambient(acol, r, nrm)           /* compute ambient component for ray */
257 < COLOR  acol;
258 < register RAY  *r;
259 < FVECT  nrm;
256 > extern void
257 > ambient(                /* compute ambient component for ray */
258 >        COLOR  acol,
259 >        register RAY  *r,
260 >        FVECT  nrm
261 > )
262   {
263          static int  rdepth = 0;                 /* ambient recursion */
264          double  d, l;
# Line 232 | Line 299 | FVECT  nrm;
299                  return;
300   dumbamb:                                        /* return global value */
301          copycolor(acol, ambval);
302 <        if (ambvwt <= 0 | navsum == 0)
302 >        if ((ambvwt <= 0) | (navsum == 0))
303                  return;
304          l = bright(ambval);                     /* average in computations */
305          if (l > FTINY) {
# Line 247 | Line 314 | dumbamb:                                       /* return global value */
314   }
315  
316  
317 < double
318 < sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
319 < COLOR  acol;
320 < register RAY  *r;
321 < FVECT  rn;
322 < int  al;
323 < AMBTREE  *at;
324 < FVECT  c0;
325 < double  s;
317 > extern double
318 > sumambient(     /* get interpolated ambient value */
319 >        COLOR  acol,
320 >        register RAY  *r,
321 >        FVECT  rn,
322 >        int  al,
323 >        AMBTREE  *at,
324 >        FVECT  c0,
325 >        double  s
326 > )
327   {
328          double  d, e1, e2, wt, wsum;
329          COLOR  ct;
# Line 267 | Line 335 | double s;
335          wsum = 0.0;
336                                          /* do this node */
337          for (av = at->alist; av != NULL; av = av->next) {
338 +                double  rn_dot = -2.0;
339                  if (tracktime)
340                          av->latick = ambclock;
341                  /*
# Line 289 | Line 358 | double s;
358                  if (e1 > ambacc*ambacc*1.21)
359                          continue;
360                  /*
361 <                 *  Normal direction test.
361 >                 *  Direction test using closest normal.
362                   */
363 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
363 >                d = DOT(av->dir, r->ron);
364 >                if (rn != r->ron) {
365 >                        rn_dot = DOT(av->dir, rn);
366 >                        if (rn_dot > 1.0-FTINY)
367 >                                rn_dot = 1.0-FTINY;
368 >                        if (rn_dot >= d-FTINY) {
369 >                                d = rn_dot;
370 >                                rn_dot = -2.0;
371 >                        }
372 >                }
373 >                e2 = (1.0 - d) * r->rweight;
374                  if (e2 < 0.0) e2 = 0.0;
375                  if (e1 + e2 > ambacc*ambacc*1.21)
376                          continue;
# Line 307 | Line 386 | double s;
386                  /*
387                   *  Jittering final test reduces image artifacts.
388                   */
389 <                wt = sqrt(e1) + sqrt(e2);
389 >                e1 = sqrt(e1);
390 >                e2 = sqrt(e2);
391 >                wt = e1 + e2;
392                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
393                          continue;
394 +                /*
395 +                 *  Recompute directional error using perturbed normal
396 +                 */
397 +                if (rn_dot > 0.0) {
398 +                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
399 +                        wt = e1 + e2;
400 +                }
401                  if (wt <= 1e-3)
402                          wt = 1e3;
403                  else
# Line 340 | Line 428 | double s;
428   }
429  
430  
431 < double
432 < makeambient(acol, r, rn, al)    /* make a new ambient value */
433 < COLOR  acol;
434 < register RAY  *r;
435 < FVECT  rn;
436 < int  al;
431 > extern double
432 > makeambient(    /* make a new ambient value */
433 >        COLOR  acol,
434 >        register RAY  *r,
435 >        FVECT  rn,
436 >        int  al
437 > )
438   {
439          AMBVAL  amb;
440          FVECT   gp, gd;
# Line 372 | Line 461 | int  al;
461   }
462  
463  
464 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
465 < COLOR  cr;
466 < register AMBVAL  *ap;
467 < FVECT  pv, nv;
464 > extern void
465 > extambient(             /* extrapolate value at pv, nv */
466 >        COLOR  cr,
467 >        register AMBVAL  *ap,
468 >        FVECT  pv,
469 >        FVECT  nv
470 > )
471   {
472 <        FVECT  v1, v2;
472 >        FVECT  v1;
473          register int  i;
474          double  d;
475  
# Line 386 | Line 478 | FVECT  pv, nv;
478          for (i = 0; i < 3; i++)
479                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
480                                          /* gradient due to rotation */
481 <        VCOPY(v1, ap->dir);
482 <        fcross(v2, v1, nv);
391 <        d += DOT(ap->gdir, v2);
481 >        VCROSS(v1, ap->dir, nv);
482 >        d += DOT(ap->gdir, v1);
483          if (d <= 0.0) {
484                  setcolor(cr, 0.0, 0.0, 0.0);
485                  return;
# Line 398 | Line 489 | FVECT  pv, nv;
489   }
490  
491  
492 < static
493 < initambfile(creat)              /* initialize ambient file */
494 < int  creat;
492 > static void
493 > initambfile(            /* initialize ambient file */
494 >        int  creat
495 > )
496   {
497 <        extern char  *progname, *octname, VersionID[];
497 >        extern char  *progname, *octname;
498 >        static char  *mybuf = NULL;
499  
500   #ifdef  F_SETLKW
501          aflock(creat ? F_WRLCK : F_RDLCK);
502   #endif
503 < #ifdef MSDOS
504 <        setmode(fileno(ambfp), O_BINARY);
505 < #endif
506 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
503 >        SET_FILE_BINARY(ambfp);
504 >        if (mybuf == NULL)
505 >                mybuf = (char *)bmalloc(BUFSIZ+8);
506 >        setbuf(ambfp, mybuf);
507          if (creat) {                    /* new file */
508                  newheader("RADIANCE", ambfp);
509                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
510                                  progname, colval(ambval,RED),
511                                  colval(ambval,GRN), colval(ambval,BLU),
512                                  ambvwt, ambounce, ambacc);
513 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
514 <                                ambdiv, ambssamp, ambres,
515 <                                octname==NULL ? "" : octname);
513 >                fprintf(ambfp, "-ad %d -as %d -ar %d ",
514 >                                ambdiv, ambssamp, ambres);
515 >                if (octname != NULL)
516 >                        printargs(1, &octname, ambfp);
517 >                else
518 >                        fputc('\n', ambfp);
519                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
520 +                fputnow(ambfp);
521                  fputformat(AMBFMT, ambfp);
522                  putc('\n', ambfp);
523                  putambmagic(ambfp);
# Line 429 | Line 526 | int  creat;
526   }
527  
528  
529 < static
530 < avsave(av)                              /* insert and save an ambient value */
531 < AMBVAL  *av;
529 > static void
530 > avsave(                         /* insert and save an ambient value */
531 >        AMBVAL  *av
532 > )
533   {
534          avinsert(avstore(av));
535          if (ambfp == NULL)
# Line 443 | Line 541 | AMBVAL *av;
541                          goto writerr;
542          return;
543   writerr:
544 <        error(SYSTEM, "error writing ambient file");
544 >        error(SYSTEM, "error writing to ambient file");
545   }
546  
547  
548   static AMBVAL *
549 < avstore(aval)                           /* allocate memory and store aval */
550 < register AMBVAL  *aval;
549 > avstore(                                /* allocate memory and store aval */
550 >        register AMBVAL  *aval
551 > )
552   {
553          register AMBVAL  *av;
554          double  d;
555  
556          if ((av = newambval()) == NULL)
557                  error(SYSTEM, "out of memory in avstore");
558 <        copystruct(av, aval);
558 >        *av = *aval;
559          av->latick = ambclock;
560          av->next = NULL;
561          nambvals++;
# Line 474 | Line 573 | register AMBVAL  *aval;
573   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
574  
575  
576 < static
577 < AMBTREE *
479 < newambtree()                            /* allocate 8 ambient tree structs */
576 > static AMBTREE *
577 > newambtree(void)                                /* allocate 8 ambient tree structs */
578   {
579          register AMBTREE  *atp, *upperlim;
580  
581          if (atfreelist == NULL) {       /* get more nodes */
582 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
582 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
583                  if (atfreelist == NULL)
584                          return(NULL);
585                                          /* link new free list */
# Line 492 | Line 590 | newambtree()                           /* allocate 8 ambient tree structs */
590          }
591          atp = atfreelist;
592          atfreelist = atp->kid;
593 <        bzero((char *)atp, 8*sizeof(AMBTREE));
593 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
594          return(atp);
595   }
596  
597  
598 < static
599 < freeambtree(atp)                        /* free 8 ambient tree structs */
600 < AMBTREE  *atp;
598 > static void
599 > freeambtree(                    /* free 8 ambient tree structs */
600 >        AMBTREE  *atp
601 > )
602   {
603          atp->kid = atfreelist;
604          atfreelist = atp;
605   }
606  
607  
608 < static
609 < avinsert(av)                            /* insert ambient value in our tree */
610 < register AMBVAL  *av;
608 > static void
609 > avinsert(                               /* insert ambient value in our tree */
610 >        void *av
611 > )
612   {
613          register AMBTREE  *at;
614          register AMBVAL  *ap;
# Line 518 | Line 618 | register AMBVAL         *av;
618          int  branch;
619          register int  i;
620  
621 <        if (av->rad <= FTINY)
621 >        if (((AMBVAL*)av)->rad <= FTINY)
622                  error(CONSISTENCY, "zero ambient radius in avinsert");
623          at = &atrunk;
624          VCOPY(ck0, thescene.cuorg);
625          s = thescene.cusize;
626 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
626 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
627                  if (at->kid == NULL)
628                          if ((at->kid = newambtree()) == NULL)
629                                  error(SYSTEM, "out of memory in avinsert");
630                  s *= 0.5;
631                  branch = 0;
632                  for (i = 0; i < 3; i++)
633 <                        if (av->pos[i] > ck0[i] + s) {
633 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
634                                  ck0[i] += s;
635                                  branch |= 1 << i;
636                          }
# Line 538 | Line 638 | register AMBVAL         *av;
638          }
639          avh.next = at->alist;           /* order by increasing level */
640          for (ap = &avh; ap->next != NULL; ap = ap->next)
641 <                if (ap->next->lvl >= av->lvl)
641 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
642                          break;
643 <        av->next = ap->next;
644 <        ap->next = av;
643 >        ((AMBVAL*)av)->next = ap->next;
644 >        ap->next = (AMBVAL*)av;
645          at->alist = avh.next;
646   }
647  
648  
649 < static
650 < unloadatree(at, f)                      /* unload an ambient value tree */
651 < register AMBTREE  *at;
652 < int     (*f)();
649 > static void
650 > unloadatree(                    /* unload an ambient value tree */
651 >        register AMBTREE  *at,
652 >        unloadtf_t *f
653 > )
654   {
655          register AMBVAL  *av;
656          register int  i;
# Line 574 | Line 675 | static struct avl {
675   static AMBVAL   **avlist2;              /* memory positions for sorting */
676   static int      i_avlist;               /* index for lists */
677  
678 + static int alatcmp(const void *av1, const void *av2);
679  
680 < static
681 < av2list(av)
682 < register AMBVAL *av;
680 > static void
681 > av2list(
682 >        void *av
683 > )
684   {
685   #ifdef DEBUG
686          if (i_avlist >= nambvals)
687                  error(CONSISTENCY, "too many ambient values in av2list1");
688   #endif
689 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
690 <        avlist1[i_avlist++].t = av->latick;
689 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
690 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
691   }
692  
693  
694   static int
695 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
696 < struct avl      *av1, *av2;
695 > alatcmp(                        /* compare ambient values for MRA */
696 >        const void *av1,
697 >        const void *av2
698 > )
699   {
700 <        register long  lc = av2->t - av1->t;
700 >        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
701          return(lc<0 ? -1 : lc>0 ? 1 : 0);
702   }
703  
704  
705 + /* GW NOTE 2002/10/3:
706 + * I used to compare AMBVAL pointers, but found that this was the
707 + * cause of a serious consistency error with gcc, since the optimizer
708 + * uses some dangerous trick in pointer subtraction that
709 + * assumes pointers differ by exact struct size increments.
710 + */
711   static int
712 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
713 < AMBVAL  **avp1, **avp2;
712 > aposcmp(                        /* compare ambient value positions */
713 >        const void      *avp1,
714 >        const void      *avp2
715 > )
716   {
717 <        return(*avp1 - *avp2);
717 >        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
718 >        if (diff < 0)
719 >                return(-1);
720 >        return(diff > 0);
721   }
722  
607
723   #if 1
724   static int
725 < avlmemi(avaddr)                         /* find list position from address */
726 < AMBVAL  *avaddr;
725 > avlmemi(                                /* find list position from address */
726 >        AMBVAL  *avaddr
727 > )
728   {
729          register AMBVAL  **avlpp;
730  
# Line 624 | Line 740 | AMBVAL *avaddr;
740   #endif
741  
742  
743 < static
744 < sortambvals(always)                     /* resort ambient values */
745 < int     always;
743 > static void
744 > sortambvals(                    /* resort ambient values */
745 >        int     always
746 > )
747   {
748          AMBTREE  oldatrunk;
749          AMBVAL  tav, *tap, *pnext;
# Line 659 | Line 776 | int    always;
776          }
777          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
778                  if (avlist2 != NULL)
779 <                        free((char *)avlist2);
779 >                        free((void *)avlist2);
780                  if (always) {           /* rebuild without sorting */
781 <                        copystruct(&oldatrunk, &atrunk);
781 >                        oldatrunk = atrunk;
782                          atrunk.alist = NULL;
783                          atrunk.kid = NULL;
784                          unloadatree(&oldatrunk, avinsert);
# Line 692 | Line 809 | int    always;
809                          if (avlist1[i].p == NULL)
810                                  continue;
811                          tap = avlist2[i];
812 <                        copystruct(&tav, tap);
812 >                        tav = *tap;
813                          for (j = i; (pnext = avlist1[j].p) != tap;
814                                          j = avlmemi(pnext)) {
815 <                                copystruct(avlist2[j], pnext);
815 >                                *(avlist2[j]) = *pnext;
816                                  avinsert(avlist2[j]);
817                                  avlist1[j].p = NULL;
818                          }
819 <                        copystruct(avlist2[j], &tav);
819 >                        *(avlist2[j]) = tav;
820                          avinsert(avlist2[j]);
821                          avlist1[j].p = NULL;
822                  }
823 <                free((char *)avlist1);
824 <                free((char *)avlist2);
823 >                free((void *)avlist1);
824 >                free((void *)avlist2);
825                                                  /* compute new sort interval */
826                  sortintvl = ambclock - lastsort;
827                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 723 | Line 840 | int    always;
840  
841   #ifdef  F_SETLKW
842  
843 < static
844 < aflock(typ)                     /* lock/unlock ambient file */
845 < int  typ;
843 > static void
844 > aflock(                 /* lock/unlock ambient file */
845 >        int  typ
846 > )
847   {
848          static struct flock  fls;       /* static so initialized to zeroes */
849  
# Line 735 | Line 853 | int  typ;
853   }
854  
855  
856 < int
857 < ambsync()                       /* synchronize ambient file */
856 > extern int
857 > ambsync(void)                   /* synchronize ambient file */
858   {
741        static FILE  *ambinp = NULL;
742        static long  lastpos = -1;
859          long  flen;
860          AMBVAL  avs;
861          register int  n;
# Line 751 | Line 867 | ambsync()                      /* synchronize ambient file */
867                                  /* gain exclusive access */
868          aflock(F_WRLCK);
869                                  /* see if file has grown */
870 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
870 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
871                  goto seekerr;
872 <        if (n = flen - lastpos) {               /* file has grown */
872 >        if ( (n = flen - lastpos) ) {           /* file has grown */
873                  if (ambinp == NULL) {           /* use duplicate filedes */
874                          ambinp = fdopen(dup(fileno(ambfp)), "r");
875                          if (ambinp == NULL)
# Line 764 | Line 880 | ambsync()                      /* synchronize ambient file */
880                  while (n >= AMBVALSIZ) {        /* load contributed values */
881                          if (!readambval(&avs, ambinp)) {
882                                  sprintf(errmsg,
883 <                                "ambient file corrupted near character %ld",
884 <                                                flen - n);
883 >                        "ambient file \"%s\" corrupted near character %ld",
884 >                                                ambfile, flen - n);
885                                  error(WARNING, errmsg);
886                                  break;
887                          }
# Line 774 | Line 890 | ambsync()                      /* synchronize ambient file */
890                  }
891                  /*** seek always as safety measure
892                  if (n) ***/                     /* alignment */
893 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
893 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
894                                  goto seekerr;
895          }
896   #ifdef  DEBUG
# Line 787 | Line 903 | ambsync()                      /* synchronize ambient file */
903   #endif
904   syncend:
905          n = fflush(ambfp);                      /* calls write() at last */
906 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
906 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
907                  goto seekerr;
908          aflock(F_UNLCK);                        /* release file */
909          nunflshed = 0;
910          return(n);
911   seekerr:
912          error(SYSTEM, "seek failed in ambsync");
913 +        return -1; /* pro forma return */
914   }
915  
916   #else
917  
918 < int
919 < ambsync()                       /* flush ambient file */
918 > extern int
919 > ambsync(void)                   /* flush ambient file */
920   {
921          if (nunflshed == 0)
922                  return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines