12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
|
#include "random.h" |
77 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
78 |
|
|
79 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
– |
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
82 |
+ |
|
83 |
|
static void initambfile(int creat); |
84 |
|
static void avsave(AMBVAL *av); |
85 |
|
static AMBVAL *avstore(AMBVAL *aval); |
96 |
|
static int avlmemi(AMBVAL *avaddr); |
97 |
|
static void sortambvals(int always); |
98 |
|
|
99 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
100 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
101 |
+ |
AMBTREE *at, FVECT c0, double s); |
102 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
103 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
104 |
+ |
FVECT uvw[3]); |
105 |
+ |
|
106 |
|
#ifdef F_SETLKW |
107 |
|
static void aflock(int typ); |
108 |
|
#endif |
141 |
|
newa *= (newa > 0); |
142 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
143 |
|
ambacc = newa; |
144 |
< |
if (nambvals > 0) |
144 |
> |
if (ambacc > FTINY && nambvals > 0) |
145 |
|
sortambvals(1); /* rebuild tree */ |
146 |
|
} |
147 |
|
} |
193 |
|
(flen - lastpos)/AMBVALSIZ); |
194 |
|
error(WARNING, errmsg); |
195 |
|
fseek(ambfp, lastpos, SEEK_SET); |
187 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
196 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
189 |
– |
#endif |
197 |
|
} |
198 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
199 |
|
initambfile(1); /* else create new file */ |
203 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
204 |
|
error(SYSTEM, errmsg); |
205 |
|
} |
199 |
– |
#ifdef getc_unlocked |
200 |
– |
flockfile(ambfp); /* application-level lock */ |
201 |
– |
#endif |
206 |
|
#ifdef F_SETLKW |
207 |
|
aflock(F_UNLCK); /* release file */ |
208 |
|
#endif |
223 |
|
lastpos = -1; |
224 |
|
} |
225 |
|
/* free ambient tree */ |
226 |
< |
unloadatree(&atrunk, &avfree); |
226 |
> |
unloadatree(&atrunk, avfree); |
227 |
|
/* reset state variables */ |
228 |
|
avsum = 0.; |
229 |
|
navsum = 0; |
264 |
|
} |
265 |
|
} |
266 |
|
|
263 |
– |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
267 |
|
|
265 |
– |
#ifndef OLDAMB |
266 |
– |
|
267 |
– |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
268 |
– |
|
269 |
– |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
270 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
271 |
– |
AMBTREE *at, FVECT c0, double s); |
272 |
– |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
273 |
– |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
274 |
– |
FVECT uvw[3]); |
275 |
– |
|
268 |
|
void |
269 |
|
multambient( /* compute ambient component & multiply by coef. */ |
270 |
|
COLOR aval, |
274 |
|
{ |
275 |
|
static int rdepth = 0; /* ambient recursion */ |
276 |
|
COLOR acol, caustic; |
277 |
< |
int ok; |
277 |
> |
int i, ok; |
278 |
|
double d, l; |
279 |
|
|
280 |
|
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
300 |
|
goto dumbamb; |
301 |
|
|
302 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
303 |
+ |
FVECT uvd[2]; |
304 |
+ |
float dgrad[2], *dgp = NULL; |
305 |
+ |
|
306 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
307 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
308 |
|
copycolor(acol, aval); |
309 |
|
rdepth++; |
310 |
|
ok = doambient(acol, r, r->rweight, |
311 |
< |
NULL, NULL, NULL, NULL, NULL); |
311 |
> |
uvd, NULL, NULL, dgp, NULL); |
312 |
|
rdepth--; |
313 |
|
if (!ok) |
314 |
|
goto dumbamb; |
315 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
316 |
+ |
FVECT v1; |
317 |
+ |
VCROSS(v1, r->ron, nrm); |
318 |
+ |
d = 1.0; |
319 |
+ |
for (i = 3; i--; ) |
320 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
321 |
+ |
if (d >= 0.05) |
322 |
+ |
scalecolor(acol, d); |
323 |
+ |
} |
324 |
|
copycolor(aval, acol); |
325 |
|
|
326 |
|
/* PMAP: add in caustic */ |
413 |
|
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
414 |
|
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
415 |
|
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
416 |
< |
if (rtst.ro->omod != OVOID && |
416 |
> |
OBJREC *m = findmaterial(rtst.ro); |
417 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
418 |
|
(rtst.clipset == NULL || |
419 |
|
!inset(rtst.clipset, rtst.ro->omod))) |
420 |
|
return(1); /* plug light leak */ |
653 |
|
} |
654 |
|
|
655 |
|
|
649 |
– |
#else /* ! NEWAMB */ |
650 |
– |
|
651 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
652 |
– |
AMBTREE *at, FVECT c0, double s); |
653 |
– |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
654 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
655 |
– |
|
656 |
– |
|
657 |
– |
void |
658 |
– |
multambient( /* compute ambient component & multiply by coef. */ |
659 |
– |
COLOR aval, |
660 |
– |
RAY *r, |
661 |
– |
FVECT nrm |
662 |
– |
) |
663 |
– |
{ |
664 |
– |
static int rdepth = 0; /* ambient recursion */ |
665 |
– |
COLOR acol, caustic; |
666 |
– |
double d, l; |
667 |
– |
|
668 |
– |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
669 |
– |
* as all ambient components accounted for */ |
670 |
– |
if (ambPmap(aval, r, rdepth)) |
671 |
– |
return; |
672 |
– |
|
673 |
– |
/* PMAP: Otherwise factor in ambient from caustic photon map |
674 |
– |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
675 |
– |
* continue with RADIANCE ambient calculation */ |
676 |
– |
copycolor(caustic, aval); |
677 |
– |
ambPmapCaustic(caustic, r, rdepth); |
678 |
– |
|
679 |
– |
if (ambdiv <= 0) /* no ambient calculation */ |
680 |
– |
goto dumbamb; |
681 |
– |
/* check number of bounces */ |
682 |
– |
if (rdepth >= ambounce) |
683 |
– |
goto dumbamb; |
684 |
– |
/* check ambient list */ |
685 |
– |
if (ambincl != -1 && r->ro != NULL && |
686 |
– |
ambincl != inset(ambset, r->ro->omod)) |
687 |
– |
goto dumbamb; |
688 |
– |
|
689 |
– |
if (ambacc <= FTINY) { /* no ambient storage */ |
690 |
– |
copycolor(acol, aval); |
691 |
– |
rdepth++; |
692 |
– |
d = doambient(acol, r, r->rweight, NULL, NULL); |
693 |
– |
rdepth--; |
694 |
– |
if (d <= FTINY) |
695 |
– |
goto dumbamb; |
696 |
– |
copycolor(aval, acol); |
697 |
– |
|
698 |
– |
/* PMAP: add in caustic */ |
699 |
– |
addcolor(aval, caustic); |
700 |
– |
return; |
701 |
– |
} |
702 |
– |
|
703 |
– |
if (tracktime) /* sort to minimize thrashing */ |
704 |
– |
sortambvals(0); |
705 |
– |
/* interpolate ambient value */ |
706 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
707 |
– |
d = sumambient(acol, r, nrm, rdepth, |
708 |
– |
&atrunk, thescene.cuorg, thescene.cusize); |
709 |
– |
|
710 |
– |
if (d > FTINY) { |
711 |
– |
d = 1.0/d; |
712 |
– |
scalecolor(acol, d); |
713 |
– |
multcolor(aval, acol); |
714 |
– |
|
715 |
– |
/* PMAP: add in caustic */ |
716 |
– |
addcolor(aval, caustic); |
717 |
– |
return; |
718 |
– |
} |
719 |
– |
|
720 |
– |
rdepth++; /* need to cache new value */ |
721 |
– |
d = makeambient(acol, r, nrm, rdepth-1); |
722 |
– |
rdepth--; |
723 |
– |
|
724 |
– |
if (d > FTINY) { |
725 |
– |
multcolor(aval, acol); /* got new value */ |
726 |
– |
|
727 |
– |
/* PMAP: add in caustic */ |
728 |
– |
addcolor(aval, caustic); |
729 |
– |
return; |
730 |
– |
} |
731 |
– |
|
732 |
– |
dumbamb: /* return global value */ |
733 |
– |
if ((ambvwt <= 0) | (navsum == 0)) { |
734 |
– |
multcolor(aval, ambval); |
735 |
– |
|
736 |
– |
/* PMAP: add in caustic */ |
737 |
– |
addcolor(aval, caustic); |
738 |
– |
return; |
739 |
– |
} |
740 |
– |
|
741 |
– |
l = bright(ambval); /* average in computations */ |
742 |
– |
if (l > FTINY) { |
743 |
– |
d = (log(l)*(double)ambvwt + avsum) / |
744 |
– |
(double)(ambvwt + navsum); |
745 |
– |
d = exp(d) / l; |
746 |
– |
scalecolor(aval, d); |
747 |
– |
multcolor(aval, ambval); /* apply color of ambval */ |
748 |
– |
} else { |
749 |
– |
d = exp( avsum / (double)navsum ); |
750 |
– |
scalecolor(aval, d); /* neutral color */ |
751 |
– |
} |
752 |
– |
} |
753 |
– |
|
754 |
– |
|
755 |
– |
static double |
756 |
– |
sumambient( /* get interpolated ambient value */ |
757 |
– |
COLOR acol, |
758 |
– |
RAY *r, |
759 |
– |
FVECT rn, |
760 |
– |
int al, |
761 |
– |
AMBTREE *at, |
762 |
– |
FVECT c0, |
763 |
– |
double s |
764 |
– |
) |
765 |
– |
{ |
766 |
– |
double d, e1, e2, wt, wsum; |
767 |
– |
COLOR ct; |
768 |
– |
FVECT ck0; |
769 |
– |
int i; |
770 |
– |
int j; |
771 |
– |
AMBVAL *av; |
772 |
– |
|
773 |
– |
wsum = 0.0; |
774 |
– |
/* do this node */ |
775 |
– |
for (av = at->alist; av != NULL; av = av->next) { |
776 |
– |
double rn_dot = -2.0; |
777 |
– |
if (tracktime) |
778 |
– |
av->latick = ambclock; |
779 |
– |
/* |
780 |
– |
* Ambient level test. |
781 |
– |
*/ |
782 |
– |
if (av->lvl > al || /* list sorted, so this works */ |
783 |
– |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
784 |
– |
break; |
785 |
– |
/* |
786 |
– |
* Ambient radius test. |
787 |
– |
*/ |
788 |
– |
VSUB(ck0, av->pos, r->rop); |
789 |
– |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
790 |
– |
if (e1 > ambacc*ambacc*1.21) |
791 |
– |
continue; |
792 |
– |
/* |
793 |
– |
* Direction test using closest normal. |
794 |
– |
*/ |
795 |
– |
d = DOT(av->dir, r->ron); |
796 |
– |
if (rn != r->ron) { |
797 |
– |
rn_dot = DOT(av->dir, rn); |
798 |
– |
if (rn_dot > 1.0-FTINY) |
799 |
– |
rn_dot = 1.0-FTINY; |
800 |
– |
if (rn_dot >= d-FTINY) { |
801 |
– |
d = rn_dot; |
802 |
– |
rn_dot = -2.0; |
803 |
– |
} |
804 |
– |
} |
805 |
– |
e2 = (1.0 - d) * r->rweight; |
806 |
– |
if (e2 < 0.0) |
807 |
– |
e2 = 0.0; |
808 |
– |
else if (e1 + e2 > ambacc*ambacc*1.21) |
809 |
– |
continue; |
810 |
– |
/* |
811 |
– |
* Ray behind test. |
812 |
– |
*/ |
813 |
– |
d = 0.0; |
814 |
– |
for (j = 0; j < 3; j++) |
815 |
– |
d += (r->rop[j] - av->pos[j]) * |
816 |
– |
(av->dir[j] + r->ron[j]); |
817 |
– |
if (d*0.5 < -minarad*ambacc-.001) |
818 |
– |
continue; |
819 |
– |
/* |
820 |
– |
* Jittering final test reduces image artifacts. |
821 |
– |
*/ |
822 |
– |
e1 = sqrt(e1); |
823 |
– |
e2 = sqrt(e2); |
824 |
– |
wt = e1 + e2; |
825 |
– |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
826 |
– |
continue; |
827 |
– |
/* |
828 |
– |
* Recompute directional error using perturbed normal |
829 |
– |
*/ |
830 |
– |
if (rn_dot > 0.0) { |
831 |
– |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
832 |
– |
wt = e1 + e2; |
833 |
– |
} |
834 |
– |
if (wt <= 1e-3) |
835 |
– |
wt = 1e3; |
836 |
– |
else |
837 |
– |
wt = 1.0 / wt; |
838 |
– |
wsum += wt; |
839 |
– |
extambient(ct, av, r->rop, rn); |
840 |
– |
scalecolor(ct, wt); |
841 |
– |
addcolor(acol, ct); |
842 |
– |
} |
843 |
– |
if (at->kid == NULL) |
844 |
– |
return(wsum); |
845 |
– |
/* do children */ |
846 |
– |
s *= 0.5; |
847 |
– |
for (i = 0; i < 8; i++) { |
848 |
– |
for (j = 0; j < 3; j++) { |
849 |
– |
ck0[j] = c0[j]; |
850 |
– |
if (1<<j & i) |
851 |
– |
ck0[j] += s; |
852 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
853 |
– |
break; |
854 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
855 |
– |
break; |
856 |
– |
} |
857 |
– |
if (j == 3) |
858 |
– |
wsum += sumambient(acol, r, rn, al, |
859 |
– |
at->kid+i, ck0, s); |
860 |
– |
} |
861 |
– |
return(wsum); |
862 |
– |
} |
863 |
– |
|
864 |
– |
|
865 |
– |
static double |
866 |
– |
makeambient( /* make a new ambient value for storage */ |
867 |
– |
COLOR acol, |
868 |
– |
RAY *r, |
869 |
– |
FVECT rn, |
870 |
– |
int al |
871 |
– |
) |
872 |
– |
{ |
873 |
– |
AMBVAL amb; |
874 |
– |
FVECT gp, gd; |
875 |
– |
int i; |
876 |
– |
|
877 |
– |
amb.weight = 1.0; /* compute weight */ |
878 |
– |
for (i = al; i-- > 0; ) |
879 |
– |
amb.weight *= AVGREFL; |
880 |
– |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
881 |
– |
amb.weight = 1.25*r->rweight; |
882 |
– |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
883 |
– |
/* compute ambient */ |
884 |
– |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
885 |
– |
if (amb.rad <= FTINY) { |
886 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
887 |
– |
return(0.0); |
888 |
– |
} |
889 |
– |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
890 |
– |
/* store value */ |
891 |
– |
VCOPY(amb.pos, r->rop); |
892 |
– |
VCOPY(amb.dir, r->ron); |
893 |
– |
amb.lvl = al; |
894 |
– |
copycolor(amb.val, acol); |
895 |
– |
VCOPY(amb.gpos, gp); |
896 |
– |
VCOPY(amb.gdir, gd); |
897 |
– |
/* insert into tree */ |
898 |
– |
avsave(&amb); /* and save to file */ |
899 |
– |
if (rn != r->ron) |
900 |
– |
extambient(acol, &amb, r->rop, rn); /* texture */ |
901 |
– |
return(amb.rad); |
902 |
– |
} |
903 |
– |
|
904 |
– |
|
656 |
|
static void |
906 |
– |
extambient( /* extrapolate value at pv, nv */ |
907 |
– |
COLOR cr, |
908 |
– |
AMBVAL *ap, |
909 |
– |
FVECT pv, |
910 |
– |
FVECT nv |
911 |
– |
) |
912 |
– |
{ |
913 |
– |
FVECT v1; |
914 |
– |
int i; |
915 |
– |
double d; |
916 |
– |
|
917 |
– |
d = 1.0; /* zeroeth order */ |
918 |
– |
/* gradient due to translation */ |
919 |
– |
for (i = 0; i < 3; i++) |
920 |
– |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
921 |
– |
/* gradient due to rotation */ |
922 |
– |
VCROSS(v1, ap->dir, nv); |
923 |
– |
d += DOT(ap->gdir, v1); |
924 |
– |
if (d <= 0.0) { |
925 |
– |
setcolor(cr, 0.0, 0.0, 0.0); |
926 |
– |
return; |
927 |
– |
} |
928 |
– |
copycolor(cr, ap->val); |
929 |
– |
scalecolor(cr, d); |
930 |
– |
} |
931 |
– |
|
932 |
– |
|
933 |
– |
static void |
934 |
– |
avinsert( /* insert ambient value in our tree */ |
935 |
– |
AMBVAL *av |
936 |
– |
) |
937 |
– |
{ |
938 |
– |
AMBTREE *at; |
939 |
– |
AMBVAL *ap; |
940 |
– |
AMBVAL avh; |
941 |
– |
FVECT ck0; |
942 |
– |
double s; |
943 |
– |
int branch; |
944 |
– |
int i; |
945 |
– |
|
946 |
– |
if (av->rad <= FTINY) |
947 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
948 |
– |
at = &atrunk; |
949 |
– |
VCOPY(ck0, thescene.cuorg); |
950 |
– |
s = thescene.cusize; |
951 |
– |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
952 |
– |
if (at->kid == NULL) |
953 |
– |
if ((at->kid = newambtree()) == NULL) |
954 |
– |
error(SYSTEM, "out of memory in avinsert"); |
955 |
– |
s *= 0.5; |
956 |
– |
branch = 0; |
957 |
– |
for (i = 0; i < 3; i++) |
958 |
– |
if (av->pos[i] > ck0[i] + s) { |
959 |
– |
ck0[i] += s; |
960 |
– |
branch |= 1 << i; |
961 |
– |
} |
962 |
– |
at = at->kid + branch; |
963 |
– |
} |
964 |
– |
avh.next = at->alist; /* order by increasing level */ |
965 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
966 |
– |
if ( ap->next->lvl > av->lvl || |
967 |
– |
(ap->next->lvl == av->lvl) & |
968 |
– |
(ap->next->weight <= av->weight) ) |
969 |
– |
break; |
970 |
– |
av->next = ap->next; |
971 |
– |
ap->next = (AMBVAL*)av; |
972 |
– |
at->alist = avh.next; |
973 |
– |
} |
974 |
– |
|
975 |
– |
#endif /* ! NEWAMB */ |
976 |
– |
|
977 |
– |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
978 |
– |
|
979 |
– |
static void |
657 |
|
initambfile( /* initialize ambient file */ |
658 |
|
int cre8 |
659 |
|
) |
754 |
|
} |
755 |
|
atp = atfreelist; |
756 |
|
atfreelist = atp->kid; |
757 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
757 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
758 |
|
return(atp); |
759 |
|
} |
760 |
|
|
780 |
|
/* transfer values at this node */ |
781 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
782 |
|
at->alist = av->next; |
783 |
+ |
av->next = NULL; |
784 |
|
(*f)(av); |
785 |
|
} |
786 |
|
if (at->kid == NULL) |
858 |
|
{ |
859 |
|
AMBVAL **avlpp; |
860 |
|
|
861 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
862 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
861 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
862 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
863 |
|
if (avlpp == NULL) |
864 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
865 |
|
return(avlpp - avlist2); |
902 |
|
} |
903 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
904 |
|
if (avlist2 != NULL) |
905 |
< |
free((void *)avlist2); |
905 |
> |
free(avlist2); |
906 |
|
if (always) { /* rebuild without sorting */ |
907 |
|
oldatrunk = atrunk; |
908 |
|
atrunk.alist = NULL; |
909 |
|
atrunk.kid = NULL; |
910 |
< |
unloadatree(&oldatrunk, &avinsert); |
910 |
> |
unloadatree(&oldatrunk, avinsert); |
911 |
|
} |
912 |
|
} else { /* sort memory by last access time */ |
913 |
|
/* |
924 |
|
eputs(errmsg); |
925 |
|
#endif |
926 |
|
i_avlist = 0; |
927 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
927 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
928 |
|
#ifdef DEBUG |
929 |
|
if (i_avlist < nambvals) |
930 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
931 |
|
#endif |
932 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
933 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
932 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
933 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
934 |
|
for (i = 0; i < nambvals; i++) { |
935 |
|
if (avlist1[i].p == NULL) |
936 |
|
continue; |
946 |
|
avinsert(avlist2[j]); |
947 |
|
avlist1[j].p = NULL; |
948 |
|
} |
949 |
< |
free((void *)avlist1); |
950 |
< |
free((void *)avlist2); |
949 |
> |
free(avlist1); |
950 |
> |
free(avlist2); |
951 |
|
/* compute new sort interval */ |
952 |
|
sortintvl = ambclock - lastsort; |
953 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
996 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
997 |
|
goto seekerr; |
998 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
999 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1000 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
999 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1000 |
> |
ambinp = fopen(ambfile, "rb"); |
1001 |
|
if (ambinp == NULL) |
1002 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1002 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1003 |
|
} |
1004 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1005 |
|
goto seekerr; |
1014 |
|
avstore(&avs); |
1015 |
|
n -= AMBVALSIZ; |
1016 |
|
} |
1017 |
< |
lastpos = flen - n; |
1018 |
< |
/*** seek always as safety measure |
1019 |
< |
if (n) ***/ /* alignment */ |
1342 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1343 |
< |
goto seekerr; |
1017 |
> |
lastpos = flen - n; /* check alignment */ |
1018 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1019 |
> |
goto seekerr; |
1020 |
|
} |
1021 |
|
n = fflush(ambfp); /* calls write() at last */ |
1022 |
< |
if (n != EOF) |
1347 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1348 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1349 |
< |
goto seekerr; |
1350 |
< |
|
1022 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1023 |
|
aflock(F_UNLCK); /* release file */ |
1024 |
|
nunflshed = 0; |
1025 |
|
return(n); |
1026 |
|
seekerr: |
1027 |
|
error(SYSTEM, "seek failed in ambsync"); |
1028 |
< |
return -1; /* pro forma return */ |
1028 |
> |
return(EOF); /* pro forma return */ |
1029 |
|
} |
1030 |
|
|
1031 |
|
#else /* ! F_SETLKW */ |