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.12 by gregl, Fri Dec 12 19:52:31 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 129 | Line 157 | memerr:
157  
158  
159   int
160 < hdsync(hp)                      /* update directory on disk if necessary */
160 > hdsync(hp, all)                 /* update beams and directory on disk */
161   register HOLO   *hp;
162 + int     all;
163   {
164          register int    j, n;
165  
166 <        if (hp == NULL) {               /* do all */
166 >        if (hp == NULL) {               /* do all holodecks */
167                  n = 0;
168                  for (j = 0; hdlist[j] != NULL; j++)
169 <                        n += hdsync(hdlist[j]);
169 >                        n += hdsync(hdlist[j], all);
170                  return(n);
171          }
172 <        if (!hp->dirty)                 /* check first */
172 >                                        /* sync the beams */
173 >        for (j = all ? nbeams(hp) : 0; j > 0; j--)
174 >                if (hp->bl[j] != NULL)
175 >                        hdsyncbeam(hp, j);
176 >        if (!hp->dirty)                 /* directory dirty? */
177                  return(0);
178 +        errno = 0;
179          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
180                  error(SYSTEM, "cannot seek on holodeck file");
181          n = nbeams(hp)*sizeof(BEAMI);
# Line 171 | Line 205 | int    all;                    /* include overhead (painful) */
205                  }
206          }
207          if (all)
208 <                for (j = 0; j < FFDMAX; j++)
209 <                        if (hdfrag[j] != NULL)
210 <                                total += sizeof(struct fragment);
208 >                for (j = 0; j < nhdfrags; j++) {
209 >                        total += sizeof(struct fragment);
210 >                        if (hdfrag[j].nfrags)
211 >                                total += FRAGBLK*sizeof(BEAMI) *
212 >                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
213 >                }
214          return(total);
215   }
216  
217  
218   long
219 + hdfilen(fd)             /* return file length for fd */
220 + int     fd;
221 + {
222 +        long    fpos, flen;
223 +
224 +        if (fd < 0)
225 +                return(-1);
226 +        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
227 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
228 +                        return(-1);
229 +                flen = lseek(fd, 0L, 2);
230 +                lseek(fd, fpos, 0);
231 +                return(flen);
232 +        }
233 +        return(hdfrag[fd].flen);
234 + }
235 +
236 +
237 + long
238   hdfiluse(fd, all)       /* compute file usage (in bytes) */
239   int     fd;                     /* open file descriptor to check */
240   int     all;                    /* include overhead and unflushed data */
# Line 221 | Line 277 | int    nr;                     /* number of new rays desired */
277                  hp->bl[i]->tick = hdclock;      /* preempt swap */
278          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
279                  hdfreecache(PCTFREE, NULL);     /* free some space */
280 +        errno = 0;
281          if (hp->bl[i] == NULL) {                /* allocate (and load) */
282                  n = hp->bi[i].nrd + nr;
283                  if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
# Line 283 | Line 340 | register int   i;
340  
341  
342   int
343 < hdgetbi(hp, i)                  /* allocate a file fragment */
343 > hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
344   register HOLO   *hp;
345   register int    i;
346   {
347 <        int     nrays = hp->bl[i]->nrm;
348 <
349 <        if (hp->bi[i].nrd == nrays)     /* current one will do? */
347 >        unsigned int    nrays;
348 >        long    nfo;
349 >        unsigned int    n;
350 > #ifdef DEBUG
351 >        if (i < 1 | i > nbeams(hp))
352 >                error(CONSISTENCY, "bad beam index in hdsyncbeam");
353 > #endif
354 >                                        /* is current fragment OK? */
355 >        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
356                  return(0);
357 +                                        /* check file status */
358 +        if (hp->dirty < 0)
359 +                return(-1);
360  
361 <        if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */
361 >        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
362                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
363  
364          else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
365 <                        hdfrag[hp->fd]->flen)           /* EOF special case */
366 <                hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
365 >                        hdfrag[hp->fd].flen)            /* EOF special case */
366 >                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
367  
368          else {                                          /* general case */
369 <                register struct fragment        *f = hdfrag[hp->fd];
369 >                register struct fragment        *f = &hdfrag[hp->fd];
370                  register int    j, k;
371                                          /* relinquish old fragment */
372                  if (hp->bi[i].nrd) {
373 <                        if ((j = ++f->nfrags) >= MAXFRAG)
373 >                        j = f->nfrags++;
374 > #ifdef MAXFRAG
375 >                        if (j >= MAXFRAG-1)
376                                  f->nfrags--;
377 <                        for ( ; ; ) {   /* stick it in our descending list */
378 <                                if (!--j || hp->bi[i].fo < f->fi[j-1].fo) {
377 > #endif
378 >                        if (j % FRAGBLK == 0) {         /* more frag. space */
379 >                                if (f->fi == NULL)
380 >                                        f->fi = (BEAMI *)malloc(
381 >                                                        FRAGBLK*sizeof(BEAMI));
382 >                                else
383 >                                        f->fi = (BEAMI *)realloc((char *)f->fi,
384 >                                                (j+FRAGBLK)*sizeof(BEAMI));
385 >                                if (f->fi == NULL)
386 >                                        error(SYSTEM,
387 >                                                "out of memory in hdsyncbeam");
388 >                        }
389 >                        for ( ; ; j--) {        /* insert in descending list */
390 >                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
391                                          f->fi[j].fo = hp->bi[i].fo;
392                                          f->fi[j].nrd = hp->bi[i].nrd;
393                                          break;
# Line 316 | Line 396 | register int   i;
396                          }
397                                          /* coalesce adjacent fragments */
398                          for (j = k = 0; k < f->nfrags; j++, k++) {
399 <                                if (j != k)
399 >                                if (k > j)
400                                          copystruct(f->fi+j, f->fi+k);
401 <                                while (k+1 < f->nfrags &&
402 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
403 <                                                == f->fi[j].fo)
401 >                                while (k+1 < f->nfrags && f->fi[k+1].fo +
402 >                                                f->fi[k+1].nrd*sizeof(RAYVAL)
403 >                                                        == f->fi[j].fo) {
404                                          f->fi[j].fo -=
405                                                  f->fi[++k].nrd*sizeof(RAYVAL);
406 +                                        f->fi[j].nrd += f->fi[k].nrd;
407 +                                }
408                          }
409                          f->nfrags = j;
410                  }
411                  k = -1;                 /* find closest-sized fragment */
412 <                for (j = f->nfrags; j-- > 0; )
412 >                for (j = nrays ? f->nfrags : 0; j-- > 0; )
413                          if (f->fi[j].nrd >= nrays &&
414 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
415 <                                        && f->fi[k=j].nrd == nrays)
416 <                                break;
414 >                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
415 >                                if (f->fi[k=j].nrd == nrays)
416 >                                        break;
417                  if (k < 0) {            /* no fragment -- extend file */
418 <                        hp->bi[i].fo = f->flen;
418 >                        nfo = f->flen;
419                          f->flen += nrays*sizeof(RAYVAL);
420                  } else {                /* else use fragment */
421 <                        hp->bi[i].fo = f->fi[k].fo;
421 >                        nfo = f->fi[k].fo;
422                          if (f->fi[k].nrd == nrays) {    /* delete fragment */
423                                  f->nfrags--;
424                                  for (j = k; j < f->nfrags; j++)
# Line 347 | Line 429 | register int   i;
429                          }
430                  }
431          }
432 +        if (nrays) {            /* write the new fragment */
433 +                errno = 0;
434 +                if (lseek(hp->fd, nfo, 0) < 0)
435 +                        error(SYSTEM, "cannot seek on holodeck file");
436 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
437 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
438 +                        hp->dirty = -1;         /* avoid recursive error */
439 +                        error(SYSTEM, "write error in hdsyncbeam");
440 +                }
441 +        }
442          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
443          hp->bi[i].nrd = nrays;
444 <        hp->dirty++;            /* section directory now out of date */
444 >        hp->bi[i].fo = nfo;
445 >        markdirty(hp);          /* section directory now out of date */
446          return(1);
447   }
448  
# Line 359 | Line 452 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
452   register HOLO   *hp;
453   register int    i;
454   {
455 <        int     nchanged, n;
455 >        int     nchanged;
456  
457          if (hp == NULL) {               /* clear all holodecks */
458                  nchanged = 0;
# Line 369 | Line 462 | register int   i;
462          }
463          if (i == 0) {                   /* clear entire holodeck */
464                  nchanged = 0;
465 <                for (i = 1; i <= nbeams(hp); i++)
466 <                        nchanged += hdfreebeam(hp, i);
465 >                for (i = nbeams(hp); i > 0; i--)
466 >                        if (hp->bl[i] != NULL)
467 >                                nchanged += hdfreebeam(hp, i);
468                  return(nchanged);
469          }
470 + #ifdef DEBUG
471          if (i < 1 | i > nbeams(hp))
472                  error(CONSISTENCY, "bad beam index to hdfreebeam");
473 + #endif
474          if (hp->bl[i] == NULL)
475                  return(0);
476                                          /* check for additions */
477          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
478 <        if (nchanged) {
479 <                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 <        }
478 >        if (nchanged)
479 >                hdsyncbeam(hp, i);              /* write new fragment */
480          blglob(hp)->nrm -= hp->bl[i]->nrm;
481          free((char *)hp->bl[i]);                /* free memory */
482          hp->bl[i] = NULL;
# Line 395 | Line 484 | register int   i;
484   }
485  
486  
487 + int
488 + hdkillbeam(hp, i)               /* delete beam from holodeck */
489 + register HOLO   *hp;
490 + register int    i;
491 + {
492 +        static BEAM     emptybeam;
493 +        int     nchanged;
494 +
495 +        if (hp == NULL) {               /* clobber all holodecks */
496 +                nchanged = 0;
497 +                for (i = 0; hdlist[i] != NULL; i++)
498 +                        nchanged += hdkillbeam(hdlist[i], 0);
499 +                return(nchanged);
500 +        }
501 +        if (i == 0) {                   /* clobber entire holodeck */
502 +                nchanged = 0;
503 +                for (i = nbeams(hp); i > 0; i--)
504 +                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
505 +                                nchanged += hdkillbeam(hp, i);
506 + #ifdef DEBUG
507 +                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
508 +                        error(CONSISTENCY, "bad beam count in hdkillbeam");
509 + #endif
510 +                return(nchanged);
511 +        }
512 + #ifdef DEBUG
513 +        if (i < 1 | i > nbeams(hp))
514 +                error(CONSISTENCY, "bad beam index to hdkillbeam");
515 + #endif
516 +        if (hp->bl[i] != NULL) {        /* free memory */
517 +                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
518 +                free((char *)hp->bl[i]);
519 +        } else
520 +                nchanged = hp->bi[i].nrd;
521 +        if (hp->bi[i].nrd) {            /* free file fragment */
522 +                hp->bl[i] = &emptybeam;
523 +                hdsyncbeam(hp, i);
524 +        }
525 +        hp->bl[i] = NULL;
526 +        return(nchanged);
527 + }
528 +
529 +
530   hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
531   register HOLO   *ha[];                  /* section list (NULL terminated) */
532   register int    ba[];                   /* beam index to go with section */
# Line 408 | Line 540 | register HOLO  *hp;                    /* section we're adding from */
540                  ;
541          nents = j;
542                                          /* insert each beam from hp */
543 <        for (i = nbeams(hp); i > 0; i-- ) {
543 >        for (i = nbeams(hp); i > 0; i--) {
544                  if (hp->bl[i] == NULL)          /* check if loaded */
545                          continue;
546                  if ((j = ++nents) > n)          /* grow list if we can */
# Line 455 | Line 587 | register HOLO  *honly;                 /* NULL means check all */
587                  if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
588                          break;
589          }
590 <        hdsync(honly);          /* synchronize directories as necessary */
590 >        hdsync(honly, 0);       /* synchronize directories as necessary */
591   }
592  
593  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines