ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
(Generate patch)

Comparing ray/src/hd/holofile.c (file contents):
Revision 3.2 by gregl, Fri Oct 31 15:48:32 1997 UTC vs.
Revision 3.11 by gregl, Thu Dec 11 19:57:58 1997 UTC

# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
17   #endif
18   #ifndef FREEBEAMS
19 < #define FREEBEAMS       1024    /* maximum beams to free at a time */
19 > #define FREEBEAMS       512     /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         12      /* maximum fraction to free (%) */
22 > #define PCTFREE         20      /* maximum fraction to free (%) */
23   #endif
24 < #ifndef FFDMAX
25 < #define FFDMAX          64      /* max. file descriptor for tracking */
24 >
25 > /* define MAXFRAG if you want to limit fragment tracking memory */
26 >
27 > #ifndef BSD
28 > #define write   writebuf        /* safe i/o routines */
29 > #define read    readbuf
30   #endif
27 #ifndef MAXFRAG
28 #define MAXFRAG         2048    /* maximum fragments tracked in each file */
29 #endif
31  
32 + #define FRAGBLK         64      /* number of fragments to allocate at a time */
33 +
34   int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
35   unsigned long   hdclock;        /* clock value */
36  
# Line 36 | Line 39 | HOLO   *hdlist[HDMAX+1];       /* holodeck pointers (NULL term
39   static struct fragment {
40          short   nlinks;         /* number of holodeck sections using us */
41          short   nfrags;         /* number of known fragments */
42 <        BEAMI   fi[MAXFRAG];    /* fragments, descending file position */
42 >        BEAMI   *fi;            /* fragments, descending file position */
43          long    flen;           /* last known file length */
44 < } *hdfrag[FFDMAX];      /* fragment lists, indexed by file descriptor */
44 > } *hdfrag;              /* fragment lists, indexed by file descriptor */
45  
46 + static int      nhdfrags;       /* size of hdfrag array */
47  
48 +
49   hdattach(fd)            /* start tracking file fragments for some section */
50   register int    fd;
51   {
52 <        if (fd >= FFDMAX)
53 <                return;                         /* descriptor out of range */
54 <        if (hdfrag[fd] == NULL) {
55 <                if ((hdfrag[fd] = (struct fragment *)
56 <                                malloc(sizeof(struct fragment))) == NULL)
57 <                        return;                 /* should flag error? */
58 <                hdfrag[fd]->nlinks = 0;
59 <                hdfrag[fd]->nfrags = 0;
52 >        if (fd >= nhdfrags) {
53 >                if (nhdfrags)
54 >                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
55 >                                        (fd+1)*sizeof(struct fragment));
56 >                else
57 >                        hdfrag = (struct fragment *)malloc(
58 >                                        (fd+1)*sizeof(struct fragment));
59 >                if (hdfrag == NULL)
60 >                        error(SYSTEM, "out of memory in hdattach");
61 >                bzero((char *)(hdfrag+nhdfrags),
62 >                                (fd+1-nhdfrags)*sizeof(struct fragment));
63 >                nhdfrags = fd+1;
64          }
65 <        hdfrag[fd]->nlinks++;
66 <        hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */
65 >        hdfrag[fd].nlinks++;
66 >        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
67   }
68  
69  
# Line 64 | Line 73 | register int   fd;
73   hdrelease(fd)           /* stop tracking file fragments for some section */
74   register int    fd;
75   {
76 <        if (fd >= FFDMAX || hdfrag[fd] == NULL)
76 >        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
77                  return;
78 <        if (!--hdfrag[fd]->nlinks) {
79 <                free((char *)hdfrag[fd]);
80 <                hdfrag[fd] = NULL;
78 >        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
79 >                free((char *)hdfrag[fd].fi);
80 >                hdfrag[fd].fi = NULL;
81 >                hdfrag[fd].nfrags = 0;
82          }
83   }
84  
85  
86 + markdirty(hp)                   /* mark holodeck section directory dirty */
87 + register HOLO   *hp;
88 + {
89 +        static BEAMI    smudge = {0, -1};
90 +
91 +        if (hp->dirty)          /* already marked? */
92 +                return;
93 +        hp->dirty = 1;
94 +        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
95 +                        || write(hp->fd, (char *)&smudge,
96 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
97 +                error(SYSTEM, "seek/write error in markdirty");
98 + }
99 +
100 +
101   HOLO *
102   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
103   int     fd;                     /* corresponding file descriptor */
# Line 97 | Line 122 | HDGRID *hproto;                /* holodeck section grid */
122                  n = nbeams(hp)*sizeof(BEAMI);
123                  if (read(fd, (char *)(hp->bi+1), n) != n)
124                          error(SYSTEM, "failure loading holodeck directory");
125 +                                                /* check that it's clean */
126 +                if (hp->bi[nbeams(hp)].fo < 0)
127 +                        error(USER, "dirty holodeck section");
128          } else {                        /* assume we're creating it */
129                  if ((hp = hdalloc(hproto)) == NULL)
130                          goto memerr;
# Line 142 | Line 170 | register HOLO  *hp;
170          }
171          if (!hp->dirty)                 /* check first */
172                  return(0);
173 +        errno = 0;
174          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
175                  error(SYSTEM, "cannot seek on holodeck file");
176          n = nbeams(hp)*sizeof(BEAMI);
# Line 171 | Line 200 | int    all;                    /* include overhead (painful) */
200                  }
201          }
202          if (all)
203 <                for (j = 0; j < FFDMAX; j++)
204 <                        if (hdfrag[j] != NULL)
205 <                                total += sizeof(struct fragment);
203 >                for (j = 0; j < nhdfrags; j++) {
204 >                        total += sizeof(struct fragment);
205 >                        if (hdfrag[j].nfrags)
206 >                                total += FRAGBLK*sizeof(BEAMI) *
207 >                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
208 >                }
209          return(total);
210   }
211  
212  
213   long
214 + hdfilen(fd)             /* return file length for fd */
215 + int     fd;
216 + {
217 +        long    fpos, flen;
218 +
219 +        if (fd < 0)
220 +                return(-1);
221 +        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
222 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
223 +                        return(-1);
224 +                flen = lseek(fd, 0L, 2);
225 +                lseek(fd, fpos, 0);
226 +                return(flen);
227 +        }
228 +        return(hdfrag[fd].flen);
229 + }
230 +
231 +
232 + long
233   hdfiluse(fd, all)       /* compute file usage (in bytes) */
234   int     fd;                     /* open file descriptor to check */
235   int     all;                    /* include overhead and unflushed data */
# Line 221 | Line 272 | int    nr;                     /* number of new rays desired */
272                  hp->bl[i]->tick = hdclock;      /* preempt swap */
273          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
274                  hdfreecache(PCTFREE, NULL);     /* free some space */
275 +        errno = 0;
276          if (hp->bl[i] == NULL) {                /* allocate (and load) */
277                  n = hp->bi[i].nrd + nr;
278                  if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
# Line 283 | Line 335 | register int   i;
335  
336  
337   int
338 < hdgetbi(hp, i)                  /* allocate a file fragment */
338 > hdsyncbi(hp, i)                 /* sync beam in memory with beam on disk */
339   register HOLO   *hp;
340   register int    i;
341   {
342 <        int     nrays = hp->bl[i]->nrm;
343 <
344 <        if (hp->bi[i].nrd == nrays)     /* current one will do? */
342 >        unsigned int    nrays = hp->bl[i]->nrm;
343 >        long    nfo;
344 >        unsigned int    n;
345 >                                        /* is current fragment OK? */
346 >        if (nrays == hp->bi[i].nrd)
347                  return(0);
348 +                                        /* check file status */
349 +        if (hp->dirty < 0)
350 +                return(-1);
351  
352 <        if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */
352 >        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
353                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
354  
355          else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
356 <                        hdfrag[hp->fd]->flen)           /* EOF special case */
357 <                hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
356 >                        hdfrag[hp->fd].flen)            /* EOF special case */
357 >                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
358  
359          else {                                          /* general case */
360 <                register struct fragment        *f = hdfrag[hp->fd];
360 >                register struct fragment        *f = &hdfrag[hp->fd];
361                  register int    j, k;
362                                          /* relinquish old fragment */
363                  if (hp->bi[i].nrd) {
364 <                        if ((j = ++f->nfrags) >= MAXFRAG)
364 >                        j = f->nfrags++;
365 > #ifdef MAXFRAG
366 >                        if (j >= MAXFRAG-1)
367                                  f->nfrags--;
368 <                        for ( ; ; ) {   /* stick it in our descending list */
369 <                                if (!--j || hp->bi[i].fo < f->fi[j-1].fo) {
368 > #endif
369 >                        if (j % FRAGBLK == 0) {         /* more frag. space */
370 >                                if (f->fi == NULL)
371 >                                        f->fi = (BEAMI *)malloc(
372 >                                                        FRAGBLK*sizeof(BEAMI));
373 >                                else
374 >                                        f->fi = (BEAMI *)realloc((char *)f->fi,
375 >                                                (j+FRAGBLK)*sizeof(BEAMI));
376 >                                if (f->fi == NULL)
377 >                                        error(SYSTEM,
378 >                                                "out of memory in hdsyncbi");
379 >                        }
380 >                        for ( ; ; j--) {        /* insert in descending list */
381 >                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
382                                          f->fi[j].fo = hp->bi[i].fo;
383                                          f->fi[j].nrd = hp->bi[i].nrd;
384                                          break;
# Line 316 | Line 387 | register int   i;
387                          }
388                                          /* coalesce adjacent fragments */
389                          for (j = k = 0; k < f->nfrags; j++, k++) {
390 <                                if (j != k)
390 >                                if (k > j)
391                                          copystruct(f->fi+j, f->fi+k);
392 <                                while (k+1 < f->nfrags &&
393 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
394 <                                                == f->fi[j].fo)
392 >                                while (k+1 < f->nfrags && f->fi[k+1].fo +
393 >                                                f->fi[k+1].nrd*sizeof(RAYVAL)
394 >                                                        == f->fi[j].fo) {
395                                          f->fi[j].fo -=
396                                                  f->fi[++k].nrd*sizeof(RAYVAL);
397 +                                        f->fi[j].nrd += f->fi[k].nrd;
398 +                                }
399                          }
400                          f->nfrags = j;
401                  }
402                  k = -1;                 /* find closest-sized fragment */
403 <                for (j = f->nfrags; j-- > 0; )
403 >                for (j = nrays ? f->nfrags : 0; j-- > 0; )
404                          if (f->fi[j].nrd >= nrays &&
405 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
406 <                                        && f->fi[k=j].nrd == nrays)
407 <                                break;
405 >                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
406 >                                if (f->fi[k=j].nrd == nrays)
407 >                                        break;
408                  if (k < 0) {            /* no fragment -- extend file */
409 <                        hp->bi[i].fo = f->flen;
409 >                        nfo = f->flen;
410                          f->flen += nrays*sizeof(RAYVAL);
411                  } else {                /* else use fragment */
412 <                        hp->bi[i].fo = f->fi[k].fo;
412 >                        nfo = f->fi[k].fo;
413                          if (f->fi[k].nrd == nrays) {    /* delete fragment */
414                                  f->nfrags--;
415                                  for (j = k; j < f->nfrags; j++)
# Line 347 | Line 420 | register int   i;
420                          }
421                  }
422          }
423 +        if (nrays) {            /* write the new fragment */
424 +                errno = 0;
425 +                if (lseek(hp->fd, nfo, 0) < 0)
426 +                        error(SYSTEM, "cannot seek on holodeck file");
427 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
428 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
429 +                        hp->dirty = -1;         /* avoid recursive error */
430 +                        error(SYSTEM, "write error in hdsyncbi");
431 +                }
432 +        }
433          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
434          hp->bi[i].nrd = nrays;
435 <        hp->dirty++;            /* section directory now out of date */
435 >        hp->bi[i].fo = nfo;
436 >        markdirty(hp);          /* section directory now out of date */
437          return(1);
438   }
439  
# Line 359 | Line 443 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
443   register HOLO   *hp;
444   register int    i;
445   {
446 <        int     nchanged, n;
446 >        int     nchanged;
447  
448          if (hp == NULL) {               /* clear all holodecks */
449                  nchanged = 0;
# Line 369 | Line 453 | register int   i;
453          }
454          if (i == 0) {                   /* clear entire holodeck */
455                  nchanged = 0;
456 <                for (i = 1; i <= nbeams(hp); i++)
457 <                        nchanged += hdfreebeam(hp, i);
456 >                for (i = nbeams(hp); i > 0; i--)
457 >                        if (hp->bl[i] != NULL)
458 >                                nchanged += hdfreebeam(hp, i);
459                  return(nchanged);
460          }
461          if (i < 1 | i > nbeams(hp))
# Line 379 | Line 464 | register int   i;
464                  return(0);
465                                          /* check for additions */
466          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
467 <        if (nchanged) {
468 <                hdgetbi(hp, i);                 /* allocate a file position */
384 <                errno = 0;
385 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
386 <                        error(SYSTEM, "cannot seek on holodeck file");
387 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
388 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
389 <                        error(SYSTEM, "write error in hdfreebeam");
390 <        }
467 >        if (nchanged)
468 >                hdsyncbi(hp, i);                        /* write new fragment */
469          blglob(hp)->nrm -= hp->bl[i]->nrm;
470          free((char *)hp->bl[i]);                /* free memory */
471          hp->bl[i] = NULL;
# Line 395 | Line 473 | register int   i;
473   }
474  
475  
476 + int
477 + hdkillbeam(hp, i)               /* delete beam from holodeck */
478 + register HOLO   *hp;
479 + register int    i;
480 + {
481 +        static BEAM     emptybeam;
482 +        int     nchanged;
483 +
484 +        if (hp == NULL) {               /* clobber all holodecks */
485 +                nchanged = 0;
486 +                for (i = 0; hdlist[i] != NULL; i++)
487 +                        nchanged += hdkillbeam(hdlist[i], 0);
488 +                return(nchanged);
489 +        }
490 +        if (i == 0) {                   /* clobber entire holodeck */
491 +                nchanged = 0;
492 +                for (i = nbeams(hp); i > 0; i--)
493 +                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
494 +                                nchanged += hdkillbeam(hp, i);
495 + #ifdef DEBUG
496 +                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
497 +                        error(CONSISTENCY, "bad beam count in hdkillbeam");
498 + #endif
499 +                return(nchanged);
500 +        }
501 +        if (i < 1 | i > nbeams(hp))
502 +                error(CONSISTENCY, "bad beam index to hdkillbeam");
503 +        if (hp->bl[i] != NULL) {        /* free memory */
504 +                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
505 +                free((char *)hp->bl[i]);
506 +        } else
507 +                nchanged = hp->bi[i].nrd;
508 +        if (hp->bi[i].nrd) {            /* free file fragment */
509 +                hp->bl[i] = &emptybeam;
510 +                hdsyncbi(hp, i);
511 +        }
512 +        hp->bl[i] = NULL;
513 +        return(nchanged);
514 + }
515 +
516 +
517   hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
518   register HOLO   *ha[];                  /* section list (NULL terminated) */
519   register int    ba[];                   /* beam index to go with section */
# Line 408 | Line 527 | register HOLO  *hp;                    /* section we're adding from */
527                  ;
528          nents = j;
529                                          /* insert each beam from hp */
530 <        for (i = nbeams(hp); i > 0; i-- ) {
530 >        for (i = nbeams(hp); i > 0; i--) {
531                  if (hp->bl[i] == NULL)          /* check if loaded */
532                          continue;
533                  if ((j = ++nents) > n)          /* grow list if we can */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines