ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.34
Committed: Mon Nov 6 12:03:13 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +12 -7 lines
Log Message:
added texturing to ambient value computation using extambient()

File Contents

# User Rev Content
1 greg 2.27 /* Copyright (c) 1995 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * ambient.c - routines dealing with ambient (inter-reflected) component.
9     */
10    
11     #include "ray.h"
12    
13     #include "octree.h"
14    
15 greg 1.8 #include "otypes.h"
16    
17 greg 1.14 #include "ambient.h"
18    
19 greg 1.1 #include "random.h"
20    
21 greg 2.32 #ifndef OCTSCALE
22 greg 2.29 #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */
23 greg 2.32 #endif
24     #ifndef AMBVWT
25     #define AMBVWT 250 /* relative ambient value weight (# calcs) */
26     #endif
27 greg 1.1
28 greg 1.14 typedef struct ambtree {
29 greg 2.12 AMBVAL *alist; /* ambient value list */
30     struct ambtree *kid; /* 8 child nodes */
31 greg 1.14 } AMBTREE; /* ambient octree */
32    
33 greg 1.1 extern CUBE thescene; /* contains space boundaries */
34    
35 greg 2.27 extern char *shm_boundary; /* memory sharing boundary */
36 greg 2.26
37 greg 2.12 #define MAXASET 511 /* maximum number of elements in ambient set */
38     OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
39 greg 1.1
40 greg 2.12 double maxarad; /* maximum ambient radius */
41     double minarad; /* minimum ambient radius */
42 greg 1.1
43 greg 2.12 static AMBTREE atrunk; /* our ambient trunk node */
44 greg 1.1
45     static FILE *ambfp = NULL; /* ambient file pointer */
46 greg 2.16 static int nunflshed = 0; /* number of unflushed ambient values */
47 greg 1.1
48 greg 2.26 #ifndef SORT_THRESH
49     #ifdef BIGMEM
50 greg 2.27 #define SORT_THRESH ((9L<<20)/sizeof(AMBVAL))
51 greg 2.26 #else
52 greg 2.27 #define SORT_THRESH ((3L<<20)/sizeof(AMBVAL))
53 greg 2.26 #endif
54     #endif
55     #ifndef SORT_INTVL
56 greg 2.29 #define SORT_INTVL (SORT_THRESH*256)
57 greg 2.26 #endif
58 greg 2.28 #ifndef MAX_SORT_INTVL
59 greg 2.29 #define MAX_SORT_INTVL (SORT_INTVL<<4)
60 greg 2.28 #endif
61 greg 2.26
62 greg 2.32 static COLOR avsum = BLKCOLOR; /* computed ambient value sum */
63     static unsigned int nambvals = 0; /* number of computed ambient values */
64 greg 2.27 static unsigned long ambclock = 0; /* ambient access clock */
65     static unsigned long lastsort = 0; /* time of last value sort */
66 greg 2.26 static long sortintvl = SORT_INTVL; /* time until next sort */
67    
68     #define MAXACLOCK (1L<<30) /* clock turnover value */
69 greg 2.27 /*
70     * Track access times unless we are sharing ambient values
71     * through memory on a multiprocessor, when we want to avoid
72     * claiming our own memory (copy on write).
73     */
74 greg 2.26 #define tracktime (shm_boundary == NULL || ambfp == NULL)
75    
76 greg 2.12 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
77 greg 2.6
78 greg 2.12 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
79 greg 1.1
80 greg 2.7 extern long ftell(), lseek();
81 greg 2.26 static int initambfile(), avsave(), avinsert(), sortambvals();
82 greg 2.20 static AMBVAL *avstore();
83 greg 2.19 #ifdef F_SETLKW
84     static aflock();
85     #endif
86 greg 1.1
87 greg 2.7
88 greg 2.3 setambres(ar) /* set ambient resolution */
89     int ar;
90     {
91 greg 2.21 ambres = ar < 0 ? 0 : ar; /* may be done already */
92 greg 2.3 /* set min & max radii */
93     if (ar <= 0) {
94 greg 2.25 minarad = 0;
95 greg 2.3 maxarad = thescene.cusize / 2.0;
96     } else {
97     minarad = thescene.cusize / ar;
98 greg 2.30 maxarad = 64 * minarad; /* heuristic */
99 greg 2.3 if (maxarad > thescene.cusize / 2.0)
100     maxarad = thescene.cusize / 2.0;
101     }
102 greg 2.25 if (minarad <= FTINY)
103     minarad = 10*FTINY;
104     if (maxarad <= minarad)
105     maxarad = 64 * minarad;
106 greg 2.3 }
107    
108    
109 greg 2.21 setambacc(newa) /* set ambient accuracy */
110 greg 2.20 double newa;
111     {
112 greg 2.33 double ambdiff;
113 greg 2.20
114 greg 2.33 if (newa < 0.0)
115     newa = 0.0;
116     ambdiff = fabs(newa - ambacc);
117     if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
118     sortambvals(1); /* rebuild tree */
119 greg 2.20 }
120    
121    
122 greg 1.1 setambient(afile) /* initialize calculation */
123     char *afile;
124     {
125 greg 2.9 long headlen;
126 greg 2.12 AMBVAL amb;
127 greg 2.3 /* init ambient limits */
128     setambres(ambres);
129 greg 2.21 setambacc(ambacc);
130 greg 2.19 if (afile == NULL)
131     return;
132 greg 2.20 if (ambacc <= FTINY) {
133 greg 2.21 sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
134 greg 2.20 afile);
135     error(WARNING, errmsg);
136     return;
137     }
138 greg 2.3 /* open ambient file */
139 greg 2.19 if ((ambfp = fopen(afile, "r+")) != NULL) {
140     initambfile(0);
141     headlen = ftell(ambfp);
142     while (readambval(&amb, ambfp))
143 greg 2.21 avinsert(avstore(&amb));
144 greg 2.19 /* align */
145     fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
146     } else if ((ambfp = fopen(afile, "w+")) != NULL)
147     initambfile(1);
148     else {
149     sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
150     error(SYSTEM, errmsg);
151 greg 2.16 }
152 greg 2.19 nunflshed++; /* lie */
153     ambsync();
154 greg 1.8 }
155    
156    
157     ambnotify(obj) /* record new modifier */
158 greg 2.12 OBJECT obj;
159 greg 1.8 {
160 greg 1.11 static int hitlimit = 0;
161 greg 2.12 register OBJREC *o = objptr(obj);
162 greg 1.8 register char **amblp;
163    
164 greg 1.11 if (hitlimit || !ismodifier(o->otype))
165 greg 1.8 return;
166     for (amblp = amblist; *amblp != NULL; amblp++)
167     if (!strcmp(o->oname, *amblp)) {
168 greg 1.11 if (ambset[0] >= MAXASET) {
169     error(WARNING, "too many modifiers in ambient list");
170     hitlimit++;
171     return; /* should this be fatal? */
172     }
173 greg 1.8 insertelem(ambset, obj);
174     return;
175 greg 1.1 }
176     }
177    
178    
179 greg 2.34 ambient(acol, r, nrm) /* compute ambient component for ray */
180 greg 1.1 COLOR acol;
181     register RAY *r;
182 greg 2.34 FVECT nrm;
183 greg 1.1 {
184     static int rdepth = 0; /* ambient recursion */
185 greg 2.12 double d;
186 greg 1.1
187     if (ambdiv <= 0) /* no ambient calculation */
188     goto dumbamb;
189     /* check number of bounces */
190 greg 1.16 if (rdepth >= ambounce)
191 greg 1.1 goto dumbamb;
192     /* check ambient list */
193     if (ambincl != -1 && r->ro != NULL &&
194     ambincl != inset(ambset, r->ro->omod))
195     goto dumbamb;
196    
197     if (ambacc <= FTINY) { /* no ambient storage */
198 greg 1.16 rdepth++;
199     d = doambient(acol, r, r->rweight, NULL, NULL);
200     rdepth--;
201 greg 2.32 if (d <= FTINY)
202 greg 1.1 goto dumbamb;
203 greg 1.16 return;
204 greg 1.1 }
205 greg 2.27 /* resort memory? */
206 greg 2.28 sortambvals(0);
207 greg 1.1 /* get ambient value */
208     setcolor(acol, 0.0, 0.0, 0.0);
209 greg 2.34 d = sumambient(acol, r, nrm, rdepth,
210 greg 1.16 &atrunk, thescene.cuorg, thescene.cusize);
211 greg 2.32 if (d > FTINY) {
212 greg 1.16 scalecolor(acol, 1.0/d);
213 greg 2.32 return;
214 greg 1.16 }
215 greg 2.33 rdepth++; /* need to cache new value */
216 greg 2.34 d = makeambient(acol, r, nrm, rdepth-1);
217 greg 2.32 rdepth--;
218 greg 1.16 if (d > FTINY)
219     return;
220 greg 1.1 dumbamb: /* return global value */
221     copycolor(acol, ambval);
222 greg 2.32 #if AMBVWT
223     if (nambvals == 0)
224     return;
225     scalecolor(acol, (double)AMBVWT);
226     addcolor(acol, avsum); /* average in computations */
227     d = 1.0/(AMBVWT+nambvals);
228     scalecolor(acol, d);
229     #endif
230 greg 1.1 }
231    
232    
233     double
234 greg 2.34 sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */
235 greg 1.1 COLOR acol;
236     register RAY *r;
237 greg 2.34 FVECT rn;
238 greg 1.16 int al;
239 greg 2.12 AMBTREE *at;
240 greg 1.1 FVECT c0;
241 greg 2.12 double s;
242 greg 1.1 {
243 greg 2.12 double d, e1, e2, wt, wsum;
244 greg 1.1 COLOR ct;
245     FVECT ck0;
246     int i;
247     register int j;
248 greg 2.12 register AMBVAL *av;
249 greg 2.29
250     wsum = 0.0;
251 greg 1.7 /* do this node */
252 greg 1.1 for (av = at->alist; av != NULL; av = av->next) {
253 greg 2.26 if (tracktime)
254     av->latick = ambclock++;
255 greg 1.1 /*
256 greg 1.16 * Ambient level test.
257 greg 1.1 */
258 greg 2.23 if (av->lvl > al) /* list sorted, so this works */
259     break;
260     if (av->weight < r->rweight-FTINY)
261 greg 1.1 continue;
262     /*
263     * Ambient radius test.
264     */
265 greg 2.29 d = av->pos[0] - r->rop[0];
266     e1 = d * d;
267     d = av->pos[1] - r->rop[1];
268     e1 += d * d;
269     d = av->pos[2] - r->rop[2];
270     e1 += d * d;
271 greg 1.1 e1 /= av->rad * av->rad;
272     if (e1 > ambacc*ambacc*1.21)
273     continue;
274     /*
275     * Normal direction test.
276     */
277     e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
278     if (e2 < 0.0) e2 = 0.0;
279     if (e1 + e2 > ambacc*ambacc*1.21)
280     continue;
281     /*
282     * Ray behind test.
283     */
284     d = 0.0;
285     for (j = 0; j < 3; j++)
286     d += (r->rop[j] - av->pos[j]) *
287     (av->dir[j] + r->ron[j]);
288 greg 1.18 if (d*0.5 < -minarad*ambacc-.001)
289 greg 1.1 continue;
290     /*
291     * Jittering final test reduces image artifacts.
292     */
293     wt = sqrt(e1) + sqrt(e2);
294 greg 2.31 if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
295 greg 1.1 continue;
296     if (wt <= 1e-3)
297     wt = 1e3;
298     else
299     wt = 1.0 / wt;
300     wsum += wt;
301 greg 2.34 extambient(ct, av, r->rop, rn);
302 greg 1.1 scalecolor(ct, wt);
303     addcolor(acol, ct);
304 greg 1.7 }
305     if (at->kid == NULL)
306     return(wsum);
307     /* do children */
308     s *= 0.5;
309     for (i = 0; i < 8; i++) {
310     for (j = 0; j < 3; j++) {
311     ck0[j] = c0[j];
312     if (1<<j & i)
313     ck0[j] += s;
314     if (r->rop[j] < ck0[j] - OCTSCALE*s)
315     break;
316     if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
317     break;
318     }
319     if (j == 3)
320 greg 2.34 wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
321 greg 1.1 }
322     return(wsum);
323     }
324    
325    
326     double
327 greg 2.34 makeambient(acol, r, rn, al) /* make a new ambient value */
328 greg 1.1 COLOR acol;
329     register RAY *r;
330 greg 2.34 FVECT rn;
331 greg 1.16 int al;
332 greg 1.1 {
333 greg 2.12 AMBVAL amb;
334 greg 1.14 FVECT gp, gd;
335 greg 1.16 /* compute weight */
336     amb.weight = pow(AVGREFL, (double)al);
337 greg 2.33 if (r->rweight < 0.1*amb.weight) /* heuristic */
338 greg 1.16 amb.weight = r->rweight;
339     /* compute ambient */
340     amb.rad = doambient(acol, r, amb.weight, gp, gd);
341 greg 2.33 if (amb.rad <= FTINY)
342 greg 1.1 return(0.0);
343     /* store it */
344     VCOPY(amb.pos, r->rop);
345     VCOPY(amb.dir, r->ron);
346 greg 1.16 amb.lvl = al;
347 greg 1.1 copycolor(amb.val, acol);
348 greg 1.14 VCOPY(amb.gpos, gp);
349     VCOPY(amb.gdir, gd);
350 greg 1.1 /* insert into tree */
351 greg 2.7 avsave(&amb); /* and save to file */
352 greg 2.34 if (rn != r->ron)
353     extambient(acol, &amb, r->rop, rn); /* texture */
354 greg 1.1 return(amb.rad);
355 greg 1.15 }
356    
357    
358     extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
359     COLOR cr;
360 greg 2.12 register AMBVAL *ap;
361 greg 1.15 FVECT pv, nv;
362     {
363     FVECT v1, v2;
364     register int i;
365 greg 2.12 double d;
366 greg 1.15
367     d = 1.0; /* zeroeth order */
368     /* gradient due to translation */
369     for (i = 0; i < 3; i++)
370     d += ap->gpos[i]*(pv[i]-ap->pos[i]);
371     /* gradient due to rotation */
372     VCOPY(v1, ap->dir);
373     fcross(v2, v1, nv);
374     d += DOT(ap->gdir, v2);
375     if (d <= 0.0) {
376     setcolor(cr, 0.0, 0.0, 0.0);
377     return;
378     }
379     copycolor(cr, ap->val);
380     scalecolor(cr, d);
381 greg 1.1 }
382    
383    
384     static
385 greg 2.9 initambfile(creat) /* initialize ambient file */
386     int creat;
387     {
388     extern char *progname, *octname, VersionID[];
389    
390 greg 2.19 #ifdef F_SETLKW
391     aflock(creat ? F_WRLCK : F_RDLCK);
392     #endif
393 greg 2.12 #ifdef MSDOS
394     setmode(fileno(ambfp), O_BINARY);
395     #endif
396 greg 2.21 setbuf(ambfp, bmalloc(BUFSIZ+8));
397 greg 2.9 if (creat) { /* new file */
398 greg 2.24 newheader("RADIANCE", ambfp);
399 greg 2.9 fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
400     progname, colval(ambval,RED),
401     colval(ambval,GRN), colval(ambval,BLU),
402     ambounce, ambacc);
403     fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
404     ambdiv, ambssamp, ambres,
405     octname==NULL ? "" : octname);
406     fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
407     fputformat(AMBFMT, ambfp);
408     putc('\n', ambfp);
409     putambmagic(ambfp);
410 greg 2.17 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
411     error(USER, "bad ambient file");
412 greg 2.9 }
413    
414    
415     static
416 greg 2.7 avsave(av) /* insert and save an ambient value */
417 greg 2.12 AMBVAL *av;
418 greg 1.1 {
419 greg 2.21 avinsert(avstore(av));
420 greg 1.1 if (ambfp == NULL)
421     return;
422 greg 2.5 if (writambval(av, ambfp) < 0)
423 greg 1.1 goto writerr;
424 greg 2.16 if (++nunflshed >= AMBFLUSH)
425 greg 2.7 if (ambsync() == EOF)
426 greg 1.1 goto writerr;
427     return;
428     writerr:
429 greg 2.17 error(SYSTEM, "error writing ambient file");
430 greg 1.1 }
431    
432    
433 greg 2.20 static AMBVAL *
434     avstore(aval) /* allocate memory and store aval */
435     register AMBVAL *aval;
436     {
437     register AMBVAL *av;
438    
439     if ((av = newambval()) == NULL)
440     error(SYSTEM, "out of memory in avstore");
441     copystruct(av, aval);
442 greg 2.26 av->latick = ambclock;
443     av->next = NULL;
444 greg 2.32 addcolor(avsum, av->val); /* add to sum for averaging */
445 greg 2.26 nambvals++;
446 greg 2.20 return(av);
447     }
448    
449    
450 greg 2.26 #define ATALLOCSZ 512 /* #/8 trees to allocate at once */
451    
452     static AMBTREE *atfreelist = NULL; /* free ambient tree structures */
453    
454    
455 greg 1.1 static
456 greg 2.26 AMBTREE *
457     newambtree() /* allocate 8 ambient tree structs */
458     {
459     register AMBTREE *atp, *upperlim;
460    
461     if (atfreelist == NULL) { /* get more nodes */
462     atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
463     if (atfreelist == NULL)
464     return(NULL);
465     /* link new free list */
466     upperlim = atfreelist + 8*(ATALLOCSZ-1);
467     for (atp = atfreelist; atp < upperlim; atp += 8)
468     atp->kid = atp + 8;
469     atp->kid = NULL;
470     }
471     atp = atfreelist;
472     atfreelist = atp->kid;
473     bzero((char *)atp, 8*sizeof(AMBTREE));
474     return(atp);
475     }
476    
477    
478     static
479     freeambtree(atp) /* free 8 ambient tree structs */
480     AMBTREE *atp;
481     {
482     atp->kid = atfreelist;
483     atfreelist = atp;
484     }
485    
486    
487     static
488 greg 2.21 avinsert(av) /* insert ambient value in our tree */
489 greg 2.20 register AMBVAL *av;
490 greg 1.1 {
491 greg 2.21 register AMBTREE *at;
492 greg 2.23 register AMBVAL *ap;
493     AMBVAL avh;
494 greg 1.1 FVECT ck0;
495 greg 2.21 double s;
496 greg 1.1 int branch;
497     register int i;
498    
499 greg 2.20 if (av->rad <= FTINY)
500     error(CONSISTENCY, "zero ambient radius in avinsert");
501 greg 2.21 at = &atrunk;
502     VCOPY(ck0, thescene.cuorg);
503     s = thescene.cusize;
504 greg 1.1 while (s*(OCTSCALE/2) > av->rad*ambacc) {
505     if (at->kid == NULL)
506     if ((at->kid = newambtree()) == NULL)
507 greg 2.20 error(SYSTEM, "out of memory in avinsert");
508 greg 1.1 s *= 0.5;
509     branch = 0;
510     for (i = 0; i < 3; i++)
511     if (av->pos[i] > ck0[i] + s) {
512     ck0[i] += s;
513     branch |= 1 << i;
514     }
515     at = at->kid + branch;
516     }
517 greg 2.23 avh.next = at->alist; /* order by increasing level */
518     for (ap = &avh; ap->next != NULL; ap = ap->next)
519     if (ap->next->lvl >= av->lvl)
520     break;
521     av->next = ap->next;
522     ap->next = av;
523     at->alist = avh.next;
524 greg 2.7 }
525    
526    
527 greg 2.20 static
528 greg 2.26 unloadatree(at, f) /* unload an ambient value tree */
529 greg 2.20 register AMBTREE *at;
530 greg 2.26 int (*f)();
531 greg 2.20 {
532     register AMBVAL *av;
533     register int i;
534     /* transfer values at this node */
535     for (av = at->alist; av != NULL; av = at->alist) {
536     at->alist = av->next;
537 greg 2.26 (*f)(av);
538 greg 2.20 }
539 greg 2.21 if (at->kid == NULL)
540     return;
541 greg 2.20 for (i = 0; i < 8; i++) /* transfer and free children */
542 greg 2.26 unloadatree(at->kid+i, f);
543 greg 2.20 freeambtree(at->kid);
544 greg 2.26 at->kid = NULL;
545     }
546    
547    
548     static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */
549     static int i_avlist; /* index for lists */
550    
551    
552     static
553     av2list(av)
554     AMBVAL *av;
555     {
556 greg 2.27 #ifdef DEBUG
557 greg 2.26 if (i_avlist >= nambvals)
558     error(CONSISTENCY, "too many ambient values in av2list1");
559 greg 2.27 #endif
560 greg 2.26 avlist1[i_avlist] = avlist2[i_avlist] = av;
561     i_avlist++;
562     }
563    
564    
565     static int
566     alatcmp(avp1, avp2) /* compare ambient values for MRA */
567     AMBVAL **avp1, **avp2;
568     {
569     return((**avp2).latick - (**avp1).latick);
570     }
571    
572    
573     static int
574     aposcmp(avp1, avp2) /* compare ambient value positions */
575     AMBVAL **avp1, **avp2;
576     {
577     return(*avp1 - *avp2);
578     }
579    
580    
581 greg 2.27 #ifdef DEBUG
582     static int
583     avlmemi(avaddr) /* find list position from address */
584     AMBVAL *avaddr;
585     {
586     register AMBVAL **avlpp;
587    
588     avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
589     nambvals, sizeof(AMBVAL *), aposcmp);
590     if (avlpp == NULL)
591     error(CONSISTENCY, "address not found in avlmemi");
592     return(avlpp - avlist2);
593     }
594     #else
595     #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
596     nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
597     #endif
598    
599    
600 greg 2.26 static
601     sortambvals(always) /* resort ambient values */
602     int always;
603     {
604     AMBTREE oldatrunk;
605     AMBVAL tav, *tap, *pnext;
606     register int i, j;
607 greg 2.28 /* see if it's time yet */
608     if (!always && (ambclock < lastsort+sortintvl ||
609     nambvals < SORT_THRESH))
610     return;
611 greg 2.26 /*
612     * The idea here is to minimize memory thrashing
613     * in VM systems by improving reference locality.
614     * We do this by periodically sorting our stored ambient
615     * values in memory in order of most recently to least
616     * recently accessed. This ordering was chosen so that new
617     * ambient values (which tend to be less important) go into
618     * higher memory with the infrequently accessed values.
619     * Since we expect our values to need sorting less
620     * frequently as the process continues, we double our
621     * waiting interval after each call.
622     * This routine is also called by setambacc() with
623     * the "always" parameter set to 1 so that the ambient
624     * tree will be rebuilt with the new accuracy parameter.
625     */
626 greg 2.27 if (tracktime) { /* allocate pointer arrays to sort */
627 greg 2.26 avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
628     avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
629     } else
630     avlist1 = avlist2 = NULL;
631 greg 2.27 if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */
632 greg 2.26 if (avlist1 != NULL)
633     free((char *)avlist1);
634 greg 2.27 if (always) { /* rebuild without sorting */
635     copystruct(&oldatrunk, &atrunk);
636     atrunk.alist = NULL;
637     atrunk.kid = NULL;
638     unloadatree(&oldatrunk, avinsert);
639     }
640 greg 2.26 } else { /* sort memory by last access time */
641     /*
642     * Sorting memory is tricky because it isn't contiguous.
643     * We have to sort an array of pointers by MRA and also
644     * by memory position. We then copy values in "loops"
645     * to minimize memory hits. Nevertheless, we will visit
646 greg 2.27 * everyone at least twice, and this is an expensive process
647 greg 2.26 * when we're thrashing, which is when we need to do it.
648     */
649 greg 2.27 #ifdef DEBUG
650 greg 2.29 sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
651     nambvals, ambclock);
652 greg 2.27 eputs(errmsg);
653     #endif
654     i_avlist = 0;
655     unloadatree(&atrunk, av2list); /* empty current tree */
656     #ifdef DEBUG
657     if (i_avlist < nambvals)
658     error(CONSISTENCY, "missing ambient values in sortambvals");
659     #endif
660 greg 2.26 qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
661     qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
662     for (i = 0; i < nambvals; i++) {
663 greg 2.27 if (avlist1[i] == NULL)
664 greg 2.26 continue;
665     tap = avlist2[i];
666     copystruct(&tav, tap);
667     for (j = i; (pnext = avlist1[j]) != tap;
668 greg 2.27 j = avlmemi(pnext)) {
669 greg 2.26 copystruct(avlist2[j], pnext);
670     avinsert(avlist2[j]);
671     avlist1[j] = NULL;
672     }
673     copystruct(avlist2[j], &tav);
674     avinsert(avlist2[j]);
675     avlist1[j] = NULL;
676     }
677     free((char *)avlist1);
678     free((char *)avlist2);
679 greg 2.28 /* compute new sort interval */
680     sortintvl = ambclock - lastsort;
681 greg 2.32 if (sortintvl >= MAX_SORT_INTVL/2)
682 greg 2.28 sortintvl = MAX_SORT_INTVL;
683     else
684 greg 2.26 sortintvl <<= 1; /* wait twice as long next */
685 greg 2.27 #ifdef DEBUG
686     eputs("done\n");
687     #endif
688 greg 2.26 }
689     if (ambclock >= MAXACLOCK)
690     ambclock = MAXACLOCK/2;
691     lastsort = ambclock;
692 greg 2.20 }
693    
694    
695 greg 2.18 #ifdef F_SETLKW
696 greg 2.10
697 greg 2.19 static
698     aflock(typ) /* lock/unlock ambient file */
699     int typ;
700     {
701     static struct flock fls; /* static so initialized to zeroes */
702    
703     fls.l_type = typ;
704     if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
705     error(SYSTEM, "cannot (un)lock ambient file");
706     }
707    
708    
709 greg 2.16 int
710 greg 2.7 ambsync() /* synchronize ambient file */
711     {
712     static FILE *ambinp = NULL;
713 greg 2.15 static long lastpos = -1;
714     long flen;
715 greg 2.12 AMBVAL avs;
716 greg 2.7 register int n;
717 greg 2.16
718     if (nunflshed == 0)
719     return(0);
720 greg 2.19 if (lastpos < 0) /* initializing (locked in initambfile) */
721     goto syncend;
722 greg 2.7 /* gain exclusive access */
723 greg 2.19 aflock(F_WRLCK);
724 greg 2.7 /* see if file has grown */
725 greg 2.15 if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
726 greg 2.21 goto seekerr;
727 greg 2.15 if (n = flen - lastpos) { /* file has grown */
728 greg 2.17 if (ambinp == NULL) { /* use duplicate filedes */
729     ambinp = fdopen(dup(fileno(ambfp)), "r");
730 greg 2.14 if (ambinp == NULL)
731 greg 2.17 error(SYSTEM, "fdopen failed in ambsync");
732 greg 2.14 }
733 greg 2.15 if (fseek(ambinp, lastpos, 0) < 0)
734 greg 2.21 goto seekerr;
735 greg 2.15 while (n >= AMBVALSIZ) { /* load contributed values */
736 greg 2.7 readambval(&avs, ambinp);
737 greg 2.21 avinsert(avstore(&avs));
738 greg 2.15 n -= AMBVALSIZ;
739     }
740 greg 2.22 /*** seek always as safety measure
741     if (n) ***/ /* alignment */
742 greg 2.21 if (lseek(fileno(ambfp), flen-n, 0) < 0)
743     goto seekerr;
744 greg 2.7 }
745 greg 2.21 #ifdef DEBUG
746     if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
747     sprintf(errmsg, "ambient file buffer at %d rather than %d",
748     ambfp->_ptr - ambfp->_base,
749     nunflshed*AMBVALSIZ);
750     error(CONSISTENCY, errmsg);
751     }
752     #endif
753 greg 2.15 syncend:
754 greg 2.7 n = fflush(ambfp); /* calls write() at last */
755 greg 2.21 if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
756     goto seekerr;
757 greg 2.19 aflock(F_UNLCK); /* release file */
758 greg 2.16 nunflshed = 0;
759 greg 2.7 return(n);
760 greg 2.21 seekerr:
761     error(SYSTEM, "seek failed in ambsync");
762 greg 2.18 }
763    
764     #else
765    
766     int
767     ambsync() /* flush ambient file */
768     {
769     if (nunflshed == 0)
770     return(0);
771     nunflshed = 0;
772     return(fflush(ambfp));
773 greg 1.1 }
774 greg 2.10
775     #endif