9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "ray.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" |
17 |
– |
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
|
#ifndef OCTSCALE |
24 |
|
|
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 4095 /* 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 */ |
38 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
39 |
|
|
40 |
|
#ifndef SORT_THRESH |
41 |
< |
#ifdef BIGMEM |
39 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
40 |
< |
#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 |
78 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
|
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
< |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
82 |
< |
static int avlmemi(); |
83 |
< |
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 void aflock(); |
97 |
> |
static void aflock(int typ); |
98 |
|
#endif |
99 |
|
|
100 |
|
|
101 |
< |
void |
102 |
< |
setambres(ar) /* set ambient resolution */ |
103 |
< |
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 */ |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
< |
void |
125 |
< |
setambacc(newa) /* set ambient accuracy */ |
126 |
< |
double newa; |
124 |
> |
extern void |
125 |
> |
setambacc( /* set ambient accuracy */ |
126 |
> |
double newa |
127 |
> |
) |
128 |
|
{ |
129 |
|
double ambdiff; |
130 |
|
|
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
< |
void |
140 |
< |
setambient() /* initialize calculation */ |
139 |
> |
extern void |
140 |
> |
setambient(void) /* initialize calculation */ |
141 |
|
{ |
142 |
|
int readonly = 0; |
143 |
< |
long pos, flen; |
143 |
> |
long flen; |
144 |
|
AMBVAL amb; |
145 |
|
/* make sure we're fresh */ |
146 |
|
ambdone(); |
160 |
|
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
161 |
|
if (ambfp != NULL) { |
162 |
|
initambfile(0); /* file exists */ |
163 |
< |
pos = ftell(ambfp); |
163 |
> |
lastpos = ftell(ambfp); |
164 |
|
while (readambval(&amb, ambfp)) |
165 |
|
avinsert(avstore(&amb)); |
166 |
|
nambshare = nambvals; /* share loaded values */ |
174 |
|
return; /* avoid ambsync() */ |
175 |
|
} |
176 |
|
/* align file pointer */ |
177 |
< |
pos += (long)nambvals*AMBVALSIZ; |
178 |
< |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
179 |
< |
if (flen != pos) { |
177 |
> |
lastpos += (long)nambvals*AMBVALSIZ; |
178 |
> |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
179 |
> |
if (flen != lastpos) { |
180 |
|
sprintf(errmsg, |
181 |
|
"ignoring last %ld values in ambient file (corrupted)", |
182 |
< |
(flen - pos)/AMBVALSIZ); |
182 |
> |
(flen - lastpos)/AMBVALSIZ); |
183 |
|
error(WARNING, errmsg); |
184 |
< |
fseek(ambfp, pos, 0); |
185 |
< |
ftruncate(fileno(ambfp), (off_t)pos); |
184 |
> |
fseek(ambfp, lastpos, SEEK_SET); |
185 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
> |
ftruncate(fileno(ambfp), (off_t)lastpos); |
187 |
> |
#endif |
188 |
|
} |
189 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
|
initambfile(1); /* else create new file */ |
191 |
+ |
fflush(ambfp); |
192 |
+ |
lastpos = ftell(ambfp); |
193 |
|
} else { |
194 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
195 |
|
error(SYSTEM, errmsg); |
196 |
|
} |
197 |
< |
nunflshed++; /* lie */ |
198 |
< |
ambsync(); |
197 |
> |
#ifdef F_SETLKW |
198 |
> |
aflock(F_UNLCK); /* release file */ |
199 |
> |
#endif |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
< |
void |
204 |
< |
ambdone() /* close ambient file and free memory */ |
203 |
> |
extern void |
204 |
> |
ambdone(void) /* close ambient file and free memory */ |
205 |
|
{ |
206 |
|
if (ambfp != NULL) { /* close ambient file */ |
207 |
|
ambsync(); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
< |
void |
230 |
< |
ambnotify(obj) /* record new modifier */ |
231 |
< |
OBJECT obj; |
229 |
> |
extern void |
230 |
> |
ambnotify( /* record new modifier */ |
231 |
> |
OBJECT obj |
232 |
> |
) |
233 |
|
{ |
234 |
|
static int hitlimit = 0; |
235 |
|
register OBJREC *o; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
< |
void |
260 |
< |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
261 |
< |
COLOR acol; |
262 |
< |
register RAY *r; |
263 |
< |
FVECT nrm; |
259 |
> |
extern void |
260 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
261 |
> |
COLOR aval, |
262 |
> |
register RAY *r, |
263 |
> |
FVECT nrm |
264 |
> |
) |
265 |
|
{ |
266 |
|
static int rdepth = 0; /* ambient recursion */ |
267 |
+ |
COLOR acol; |
268 |
|
double d, l; |
269 |
|
|
270 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
278 |
|
goto dumbamb; |
279 |
|
|
280 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
281 |
+ |
copycolor(acol, aval); |
282 |
|
rdepth++; |
283 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
284 |
|
rdepth--; |
285 |
|
if (d <= FTINY) |
286 |
|
goto dumbamb; |
287 |
+ |
copycolor(aval, acol); |
288 |
|
return; |
289 |
|
} |
290 |
|
|
291 |
|
if (tracktime) /* sort to minimize thrashing */ |
292 |
|
sortambvals(0); |
293 |
< |
/* get ambient value */ |
293 |
> |
/* interpolate ambient value */ |
294 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
295 |
|
d = sumambient(acol, r, nrm, rdepth, |
296 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
297 |
|
if (d > FTINY) { |
298 |
< |
scalecolor(acol, 1.0/d); |
298 |
> |
d = 1.0/d; |
299 |
> |
scalecolor(acol, d); |
300 |
> |
multcolor(aval, acol); |
301 |
|
return; |
302 |
|
} |
303 |
|
rdepth++; /* need to cache new value */ |
304 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
305 |
|
rdepth--; |
306 |
< |
if (d > FTINY) |
306 |
> |
if (d > FTINY) { |
307 |
> |
multcolor(aval, acol); /* got new value */ |
308 |
|
return; |
309 |
+ |
} |
310 |
|
dumbamb: /* return global value */ |
311 |
< |
copycolor(acol, ambval); |
312 |
< |
if (ambvwt <= 0 | navsum == 0) |
311 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
312 |
> |
multcolor(aval, ambval); |
313 |
|
return; |
314 |
+ |
} |
315 |
|
l = bright(ambval); /* average in computations */ |
316 |
|
if (l > FTINY) { |
317 |
|
d = (log(l)*(double)ambvwt + avsum) / |
318 |
|
(double)(ambvwt + navsum); |
319 |
|
d = exp(d) / l; |
320 |
< |
scalecolor(acol, d); /* apply color of ambval */ |
320 |
> |
scalecolor(aval, d); |
321 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
322 |
|
} else { |
323 |
|
d = exp( avsum / (double)navsum ); |
324 |
< |
setcolor(acol, d, d, d); /* neutral color */ |
324 |
> |
scalecolor(aval, d); /* neutral color */ |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
< |
double |
330 |
< |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
331 |
< |
COLOR acol; |
332 |
< |
register RAY *r; |
333 |
< |
FVECT rn; |
334 |
< |
int al; |
335 |
< |
AMBTREE *at; |
336 |
< |
FVECT c0; |
337 |
< |
double s; |
329 |
> |
extern double |
330 |
> |
sumambient( /* get interpolated ambient value */ |
331 |
> |
COLOR acol, |
332 |
> |
register RAY *r, |
333 |
> |
FVECT rn, |
334 |
> |
int al, |
335 |
> |
AMBTREE *at, |
336 |
> |
FVECT c0, |
337 |
> |
double s |
338 |
> |
) |
339 |
|
{ |
340 |
|
double d, e1, e2, wt, wsum; |
341 |
|
COLOR ct; |
355 |
|
*/ |
356 |
|
if (av->lvl > al) /* list sorted, so this works */ |
357 |
|
break; |
358 |
< |
if (av->weight < r->rweight-FTINY) |
358 |
> |
if (av->weight < 0.9*r->rweight) |
359 |
|
continue; |
360 |
|
/* |
361 |
|
* Ambient radius test. |
434 |
|
break; |
435 |
|
} |
436 |
|
if (j == 3) |
437 |
< |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
437 |
> |
wsum += sumambient(acol, r, rn, al, |
438 |
> |
at->kid+i, ck0, s); |
439 |
|
} |
440 |
|
return(wsum); |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
< |
double |
445 |
< |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
446 |
< |
COLOR acol; |
447 |
< |
register RAY *r; |
448 |
< |
FVECT rn; |
449 |
< |
int al; |
444 |
> |
extern double |
445 |
> |
makeambient( /* make a new ambient value for storage */ |
446 |
> |
COLOR acol, |
447 |
> |
RAY *r, |
448 |
> |
FVECT rn, |
449 |
> |
int al |
450 |
> |
) |
451 |
|
{ |
452 |
|
AMBVAL amb; |
453 |
|
FVECT gp, gd; |
454 |
< |
/* compute weight */ |
455 |
< |
amb.weight = pow(AVGREFL, (double)al); |
456 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
457 |
< |
amb.weight = r->rweight; |
454 |
> |
int i; |
455 |
> |
|
456 |
> |
amb.weight = 1.0; /* compute weight */ |
457 |
> |
for (i = al; i-- > 0; ) |
458 |
> |
amb.weight *= AVGREFL; |
459 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
460 |
> |
amb.weight = 1.25*r->rweight; |
461 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
462 |
|
/* compute ambient */ |
463 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
464 |
< |
if (amb.rad <= FTINY) |
464 |
> |
if (amb.rad <= FTINY) { |
465 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
466 |
|
return(0.0); |
467 |
< |
/* store it */ |
467 |
> |
} |
468 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
469 |
> |
/* store value */ |
470 |
|
VCOPY(amb.pos, r->rop); |
471 |
|
VCOPY(amb.dir, r->ron); |
472 |
|
amb.lvl = al; |
481 |
|
} |
482 |
|
|
483 |
|
|
484 |
< |
void |
485 |
< |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
486 |
< |
COLOR cr; |
487 |
< |
register AMBVAL *ap; |
488 |
< |
FVECT pv, nv; |
484 |
> |
extern void |
485 |
> |
extambient( /* extrapolate value at pv, nv */ |
486 |
> |
COLOR cr, |
487 |
> |
register AMBVAL *ap, |
488 |
> |
FVECT pv, |
489 |
> |
FVECT nv |
490 |
> |
) |
491 |
|
{ |
492 |
|
FVECT v1; |
493 |
|
register int i; |
510 |
|
|
511 |
|
|
512 |
|
static void |
513 |
< |
initambfile(creat) /* initialize ambient file */ |
514 |
< |
int creat; |
513 |
> |
initambfile( /* initialize ambient file */ |
514 |
> |
int cre8 |
515 |
> |
) |
516 |
|
{ |
517 |
|
extern char *progname, *octname; |
518 |
|
static char *mybuf = NULL; |
519 |
|
|
520 |
|
#ifdef F_SETLKW |
521 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
521 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
522 |
|
#endif |
523 |
< |
#ifdef MSDOS |
478 |
< |
setmode(fileno(ambfp), O_BINARY); |
479 |
< |
#endif |
523 |
> |
SET_FILE_BINARY(ambfp); |
524 |
|
if (mybuf == NULL) |
525 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
526 |
|
setbuf(ambfp, mybuf); |
527 |
< |
if (creat) { /* new file */ |
527 |
> |
if (cre8) { /* new file */ |
528 |
|
newheader("RADIANCE", ambfp); |
529 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
530 |
|
progname, colval(ambval,RED), |
533 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
534 |
|
ambdiv, ambssamp, ambres); |
535 |
|
if (octname != NULL) |
536 |
< |
printargs(1, &octname, ambfp); |
537 |
< |
else |
494 |
< |
fputc('\n', ambfp); |
536 |
> |
fputs(octname, ambfp); |
537 |
> |
fputc('\n', ambfp); |
538 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
539 |
|
fputnow(ambfp); |
540 |
|
fputformat(AMBFMT, ambfp); |
541 |
< |
putc('\n', ambfp); |
541 |
> |
fputc('\n', ambfp); |
542 |
|
putambmagic(ambfp); |
543 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
544 |
|
error(USER, "bad ambient file"); |
546 |
|
|
547 |
|
|
548 |
|
static void |
549 |
< |
avsave(av) /* insert and save an ambient value */ |
550 |
< |
AMBVAL *av; |
549 |
> |
avsave( /* insert and save an ambient value */ |
550 |
> |
AMBVAL *av |
551 |
> |
) |
552 |
|
{ |
553 |
|
avinsert(avstore(av)); |
554 |
|
if (ambfp == NULL) |
565 |
|
|
566 |
|
|
567 |
|
static AMBVAL * |
568 |
< |
avstore(aval) /* allocate memory and store aval */ |
569 |
< |
register AMBVAL *aval; |
568 |
> |
avstore( /* allocate memory and store aval */ |
569 |
> |
register AMBVAL *aval |
570 |
> |
) |
571 |
|
{ |
572 |
|
register AMBVAL *av; |
573 |
|
double d; |
574 |
|
|
575 |
|
if ((av = newambval()) == NULL) |
576 |
|
error(SYSTEM, "out of memory in avstore"); |
577 |
< |
copystruct(av, aval); |
577 |
> |
*av = *aval; |
578 |
|
av->latick = ambclock; |
579 |
|
av->next = NULL; |
580 |
|
nambvals++; |
593 |
|
|
594 |
|
|
595 |
|
static AMBTREE * |
596 |
< |
newambtree() /* allocate 8 ambient tree structs */ |
596 |
> |
newambtree(void) /* allocate 8 ambient tree structs */ |
597 |
|
{ |
598 |
|
register AMBTREE *atp, *upperlim; |
599 |
|
|
609 |
|
} |
610 |
|
atp = atfreelist; |
611 |
|
atfreelist = atp->kid; |
612 |
< |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
612 |
> |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
613 |
|
return(atp); |
614 |
|
} |
615 |
|
|
616 |
|
|
617 |
|
static void |
618 |
< |
freeambtree(atp) /* free 8 ambient tree structs */ |
619 |
< |
AMBTREE *atp; |
618 |
> |
freeambtree( /* free 8 ambient tree structs */ |
619 |
> |
AMBTREE *atp |
620 |
> |
) |
621 |
|
{ |
622 |
|
atp->kid = atfreelist; |
623 |
|
atfreelist = atp; |
625 |
|
|
626 |
|
|
627 |
|
static void |
628 |
< |
avinsert(av) /* insert ambient value in our tree */ |
629 |
< |
register AMBVAL *av; |
628 |
> |
avinsert( /* insert ambient value in our tree */ |
629 |
> |
void *av |
630 |
> |
) |
631 |
|
{ |
632 |
|
register AMBTREE *at; |
633 |
|
register AMBVAL *ap; |
637 |
|
int branch; |
638 |
|
register int i; |
639 |
|
|
640 |
< |
if (av->rad <= FTINY) |
640 |
> |
if (((AMBVAL*)av)->rad <= FTINY) |
641 |
|
error(CONSISTENCY, "zero ambient radius in avinsert"); |
642 |
|
at = &atrunk; |
643 |
|
VCOPY(ck0, thescene.cuorg); |
644 |
|
s = thescene.cusize; |
645 |
< |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
645 |
> |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
646 |
|
if (at->kid == NULL) |
647 |
|
if ((at->kid = newambtree()) == NULL) |
648 |
|
error(SYSTEM, "out of memory in avinsert"); |
649 |
|
s *= 0.5; |
650 |
|
branch = 0; |
651 |
|
for (i = 0; i < 3; i++) |
652 |
< |
if (av->pos[i] > ck0[i] + s) { |
652 |
> |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
653 |
|
ck0[i] += s; |
654 |
|
branch |= 1 << i; |
655 |
|
} |
657 |
|
} |
658 |
|
avh.next = at->alist; /* order by increasing level */ |
659 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
660 |
< |
if (ap->next->lvl >= av->lvl) |
660 |
> |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
661 |
|
break; |
662 |
< |
av->next = ap->next; |
663 |
< |
ap->next = av; |
662 |
> |
((AMBVAL*)av)->next = ap->next; |
663 |
> |
ap->next = (AMBVAL*)av; |
664 |
|
at->alist = avh.next; |
665 |
|
} |
666 |
|
|
667 |
|
|
668 |
|
static void |
669 |
< |
unloadatree(at, f) /* unload an ambient value tree */ |
670 |
< |
register AMBTREE *at; |
671 |
< |
void (*f)(); |
669 |
> |
unloadatree( /* unload an ambient value tree */ |
670 |
> |
register AMBTREE *at, |
671 |
> |
unloadtf_t *f |
672 |
> |
) |
673 |
|
{ |
674 |
|
register AMBVAL *av; |
675 |
|
register int i; |
694 |
|
static AMBVAL **avlist2; /* memory positions for sorting */ |
695 |
|
static int i_avlist; /* index for lists */ |
696 |
|
|
697 |
+ |
static int alatcmp(const void *av1, const void *av2); |
698 |
|
|
699 |
< |
static int |
700 |
< |
av2list(av) |
701 |
< |
register AMBVAL *av; |
699 |
> |
static void |
700 |
> |
av2list( |
701 |
> |
void *av |
702 |
> |
) |
703 |
|
{ |
704 |
|
#ifdef DEBUG |
705 |
|
if (i_avlist >= nambvals) |
706 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
707 |
|
#endif |
708 |
< |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
709 |
< |
avlist1[i_avlist++].t = av->latick; |
708 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
709 |
> |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
710 |
|
} |
711 |
|
|
712 |
|
|
713 |
|
static int |
714 |
< |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
715 |
< |
struct avl *av1, *av2; |
714 |
> |
alatcmp( /* compare ambient values for MRA */ |
715 |
> |
const void *av1, |
716 |
> |
const void *av2 |
717 |
> |
) |
718 |
|
{ |
719 |
< |
register long lc = av2->t - av1->t; |
719 |
> |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
720 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
721 |
|
} |
722 |
|
|
728 |
|
* assumes pointers differ by exact struct size increments. |
729 |
|
*/ |
730 |
|
static int |
731 |
< |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
732 |
< |
const void *avp1, *avp2; |
731 |
> |
aposcmp( /* compare ambient value positions */ |
732 |
> |
const void *avp1, |
733 |
> |
const void *avp2 |
734 |
> |
) |
735 |
|
{ |
736 |
|
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
737 |
|
if (diff < 0) |
741 |
|
|
742 |
|
#if 1 |
743 |
|
static int |
744 |
< |
avlmemi(avaddr) /* find list position from address */ |
745 |
< |
AMBVAL *avaddr; |
744 |
> |
avlmemi( /* find list position from address */ |
745 |
> |
AMBVAL *avaddr |
746 |
> |
) |
747 |
|
{ |
748 |
|
register AMBVAL **avlpp; |
749 |
|
|
760 |
|
|
761 |
|
|
762 |
|
static void |
763 |
< |
sortambvals(always) /* resort ambient values */ |
764 |
< |
int always; |
763 |
> |
sortambvals( /* resort ambient values */ |
764 |
> |
int always |
765 |
> |
) |
766 |
|
{ |
767 |
|
AMBTREE oldatrunk; |
768 |
|
AMBVAL tav, *tap, *pnext; |
797 |
|
if (avlist2 != NULL) |
798 |
|
free((void *)avlist2); |
799 |
|
if (always) { /* rebuild without sorting */ |
800 |
< |
copystruct(&oldatrunk, &atrunk); |
800 |
> |
oldatrunk = atrunk; |
801 |
|
atrunk.alist = NULL; |
802 |
|
atrunk.kid = NULL; |
803 |
|
unloadatree(&oldatrunk, avinsert); |
828 |
|
if (avlist1[i].p == NULL) |
829 |
|
continue; |
830 |
|
tap = avlist2[i]; |
831 |
< |
copystruct(&tav, tap); |
831 |
> |
tav = *tap; |
832 |
|
for (j = i; (pnext = avlist1[j].p) != tap; |
833 |
|
j = avlmemi(pnext)) { |
834 |
< |
copystruct(avlist2[j], pnext); |
834 |
> |
*(avlist2[j]) = *pnext; |
835 |
|
avinsert(avlist2[j]); |
836 |
|
avlist1[j].p = NULL; |
837 |
|
} |
838 |
< |
copystruct(avlist2[j], &tav); |
838 |
> |
*(avlist2[j]) = tav; |
839 |
|
avinsert(avlist2[j]); |
840 |
|
avlist1[j].p = NULL; |
841 |
|
} |
860 |
|
#ifdef F_SETLKW |
861 |
|
|
862 |
|
static void |
863 |
< |
aflock(typ) /* lock/unlock ambient file */ |
864 |
< |
int typ; |
863 |
> |
aflock( /* lock/unlock ambient file */ |
864 |
> |
int typ |
865 |
> |
) |
866 |
|
{ |
867 |
|
static struct flock fls; /* static so initialized to zeroes */ |
868 |
|
|
869 |
+ |
if (typ == fls.l_type) /* already called? */ |
870 |
+ |
return; |
871 |
|
fls.l_type = typ; |
872 |
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
873 |
|
error(SYSTEM, "cannot (un)lock ambient file"); |
874 |
|
} |
875 |
|
|
876 |
|
|
877 |
< |
int |
878 |
< |
ambsync() /* synchronize ambient file */ |
877 |
> |
extern int |
878 |
> |
ambsync(void) /* synchronize ambient file */ |
879 |
|
{ |
880 |
|
long flen; |
881 |
|
AMBVAL avs; |
882 |
|
register int n; |
883 |
|
|
884 |
< |
if (nunflshed == 0) |
884 |
> |
if (ambfp == NULL) /* no ambient file? */ |
885 |
|
return(0); |
886 |
< |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
887 |
< |
goto syncend; |
829 |
< |
/* gain exclusive access */ |
830 |
< |
aflock(F_WRLCK); |
886 |
> |
/* gain appropriate access */ |
887 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
888 |
|
/* see if file has grown */ |
889 |
< |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
889 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
890 |
|
goto seekerr; |
891 |
< |
if (n = flen - lastpos) { /* file has grown */ |
891 |
> |
if ((n = flen - lastpos) > 0) { /* file has grown */ |
892 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
893 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
894 |
|
if (ambinp == NULL) |
895 |
|
error(SYSTEM, "fdopen failed in ambsync"); |
896 |
|
} |
897 |
< |
if (fseek(ambinp, lastpos, 0) < 0) |
897 |
> |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
898 |
|
goto seekerr; |
899 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
900 |
|
if (!readambval(&avs, ambinp)) { |
907 |
|
avinsert(avstore(&avs)); |
908 |
|
n -= AMBVALSIZ; |
909 |
|
} |
910 |
+ |
lastpos = flen - n; |
911 |
|
/*** seek always as safety measure |
912 |
|
if (n) ***/ /* alignment */ |
913 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
913 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
914 |
|
goto seekerr; |
915 |
|
} |
858 |
– |
#ifdef DEBUG |
859 |
– |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
860 |
– |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
861 |
– |
ambfp->_ptr - ambfp->_base, |
862 |
– |
nunflshed*AMBVALSIZ); |
863 |
– |
error(CONSISTENCY, errmsg); |
864 |
– |
} |
865 |
– |
#endif |
866 |
– |
syncend: |
916 |
|
n = fflush(ambfp); /* calls write() at last */ |
917 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
917 |
> |
if (n != EOF) |
918 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
919 |
> |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
920 |
|
goto seekerr; |
921 |
+ |
|
922 |
|
aflock(F_UNLCK); /* release file */ |
923 |
|
nunflshed = 0; |
924 |
|
return(n); |
925 |
|
seekerr: |
926 |
|
error(SYSTEM, "seek failed in ambsync"); |
927 |
+ |
return -1; /* pro forma return */ |
928 |
|
} |
929 |
|
|
930 |
|
#else |
931 |
|
|
932 |
< |
int |
933 |
< |
ambsync() /* flush ambient file */ |
932 |
> |
extern int |
933 |
> |
ambsync(void) /* flush ambient file */ |
934 |
|
{ |
935 |
< |
if (nunflshed == 0) |
935 |
> |
if (ambfp == NULL) |
936 |
|
return(0); |
937 |
|
nunflshed = 0; |
938 |
|
return(fflush(ambfp)); |