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