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.15 by gregl, Mon Dec 29 15:17:45 1997 UTC vs.
Revision 3.31 by gwlarson, Wed Dec 30 11:30:04 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# 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       512     /* maximum beams to free at a time */
19 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         20      /* maximum fraction to free (%) */
22 > #define PCTFREE         15      /* maximum fraction to free (%) */
23   #endif
24 < #ifndef MAXFRAG
25 < #define MAXFRAG         131000  /* maximum fragments/file to track (0==inf) */
24 > #ifndef MAXFRAGB
25 > #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
26   #endif
27 + #ifndef MINDIRSEL
28 +                                /* minimum directory seek length */
29 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
30 + #endif
31  
32   #ifndef BSD
33   #define write   writebuf        /* safe i/o routines */
34   #define read    readbuf
35   #endif
36  
37 < #define FRAGBLK         64      /* number of fragments to allocate at a time */
37 > #define FRAGBLK         512     /* number of fragments to allocate at a time */
38  
39   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
40   unsigned long   hdclock;        /* clock value */
41  
42   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
43  
44 < static struct fragment {
44 > static struct fraglist {
45          short   nlinks;         /* number of holodeck sections using us */
46          short   writerr;        /* write error encountered */
47          int     nfrags;         /* number of known fragments */
48          BEAMI   *fi;            /* fragments, descending file position */
49          long    flen;           /* last known file length */
50 < } *hdfrag;              /* fragment lists, indexed by file descriptor */
50 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
51  
52 < static int      nhdfrags;       /* size of hdfrag array */
52 > static int      nhdfragls;      /* size of hdfragl array */
53  
54  
55 + char *
56 + hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
57 + char    *ptr;
58 + unsigned        siz;
59 + char    *rout;
60 + {
61 +        register char   *newp;
62 +                                        /* call malloc/realloc */
63 +        if (ptr == NULL) newp = (char *)malloc(siz);
64 +        else newp = (char *)realloc(ptr, siz);
65 +                                        /* check success */
66 +        if (newp == NULL && rout != NULL) {
67 +                hdfreecache(25, NULL);  /* free some memory */
68 +                errno = 0;              /* retry */
69 +                newp = hdrealloc(ptr, siz, NULL);
70 +                if (newp == NULL) {     /* give up and report error */
71 +                        sprintf(errmsg, "out of memory in %s", rout);
72 +                        error(SYSTEM, errmsg);
73 +                }
74 +        }
75 +        return(newp);
76 + }
77 +
78 +
79   hdattach(fd)            /* start tracking file fragments for some section */
80   register int    fd;
81   {
82 <        if (fd >= nhdfrags) {
83 <                if (nhdfrags)
84 <                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
85 <                                        (fd+1)*sizeof(struct fragment));
86 <                else
87 <                        hdfrag = (struct fragment *)malloc(
60 <                                        (fd+1)*sizeof(struct fragment));
61 <                if (hdfrag == NULL)
62 <                        error(SYSTEM, "out of memory in hdattach");
63 <                bzero((char *)(hdfrag+nhdfrags),
64 <                                (fd+1-nhdfrags)*sizeof(struct fragment));
65 <                nhdfrags = fd+1;
82 >        if (fd >= nhdfragls) {
83 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
84 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
85 >                bzero((char *)(hdfragl+nhdfragls),
86 >                                (fd+1-nhdfragls)*sizeof(struct fraglist));
87 >                nhdfragls = fd+1;
88          }
89 <        hdfrag[fd].nlinks++;
90 <        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
89 >        hdfragl[fd].nlinks++;
90 >        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
91   }
92  
93  
# Line 75 | Line 97 | register int   fd;
97   hdrelease(fd)           /* stop tracking file fragments for some section */
98   register int    fd;
99   {
100 <        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
100 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
101                  return;
102 <        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
103 <                free((char *)hdfrag[fd].fi);
104 <                hdfrag[fd].fi = NULL;
105 <                hdfrag[fd].nfrags = 0;
102 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
103 >                free((char *)hdfragl[fd].fi);
104 >                hdfragl[fd].fi = NULL;
105 >                hdfragl[fd].nfrags = 0;
106          }
107   }
108  
109  
88 markdirty(hp)                   /* mark holodeck section directory dirty */
89 register HOLO   *hp;
90 {
91        static BEAMI    smudge = {0, -1};
92
93        if (hp->dirty)          /* already marked? */
94                return;
95        hp->dirty = 1;
96        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
97                        || write(hp->fd, (char *)&smudge,
98                                        sizeof(BEAMI)) != sizeof(BEAMI))
99                error(SYSTEM, "seek/write error in markdirty");
100 }
101
102
110   HOLO *
111   hdinit(fd, hproto)      /* initialize a holodeck section in a file */
112   int     fd;                     /* corresponding file descriptor */
113   HDGRID  *hproto;                /* holodeck section grid */
114   {
115 +        long    rtrunc;
116          long    fpos;
117          register HOLO   *hp;
118          register int    n;
# Line 125 | Line 133 | HDGRID *hproto;                /* holodeck section grid */
133                  if (read(fd, (char *)(hp->bi+1), n) != n)
134                          error(SYSTEM, "failure loading holodeck directory");
135                                                  /* check that it's clean */
136 <                if (hp->bi[nbeams(hp)].fo < 0)
137 <                        error(USER, "dirty holodeck section");
136 >                for (n = nbeams(hp); n > 0; n--)
137 >                        if (hp->bi[n].fo < 0) {
138 >                                hp->bi[n].fo = 0;
139 >                                error(WARNING, "dirty holodeck section");
140 >                                break;
141 >                        }
142          } else {                        /* assume we're creating it */
143                  if ((hp = hdalloc(hproto)) == NULL)
144                          goto memerr;
# Line 144 | Line 156 | HDGRID *hproto;                /* holodeck section grid */
156          hdattach(fd);
157                                          /* check rays on disk */
158          fpos = hdfilen(fd);
159 <        biglob(hp)->nrd = 0;
159 >        biglob(hp)->nrd = rtrunc = 0;
160          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
161                  if (hp->bi[n].nrd)
162 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
163 <                                hp->bi[n].nrd = 0;      /* off end */
164 <                        else
162 >                        if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
163 >                                rtrunc += hp->bi[n].nrd;
164 >                                hp->bi[n].nrd = 0;
165 >                        } else
166                                  biglob(hp)->nrd += hp->bi[n].nrd;
167 +        if (rtrunc) {
168 +                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
169 +                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
170 +                error(WARNING, errmsg);
171 +        }
172                                          /* add to holodeck list */
173          for (n = 0; n < HDMAX; n++)
174                  if (hdlist[n] == NULL) {
# Line 164 | Line 182 | memerr:
182   }
183  
184  
185 + markdirty(hp, i)                /* mark holodeck directory position dirty */
186 + register HOLO   *hp;
187 + int     i;
188 + {
189 +        static BEAMI    smudge = {0, -1};
190 +        int     mindist, minpos;
191 +        register int    j;
192 +
193 +        if (!hp->dirty++) {                     /* write smudge first time */
194 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
195 +                                || write(hp->fd, (char *)&smudge,
196 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
197 +                        error(SYSTEM, "seek/write error in markdirty");
198 +                hp->dirseg[0].s = i;
199 +                hp->dirseg[0].n = 1;
200 +                return;
201 +        }
202 +                                                /* insert into segment list */
203 +        for (j = hp->dirty; j--; ) {
204 +                if (!j || hp->dirseg[j-1].s < i) {
205 +                        hp->dirseg[j].s = i;
206 +                        hp->dirseg[j].n = 1;
207 +                        break;
208 +                }
209 +                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
210 +        }
211 +        do {                            /* check neighbors */
212 +                mindist = nbeams(hp);           /* find closest */
213 +                for (j = hp->dirty; --j; )
214 +                        if (hp->dirseg[j].s -
215 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
216 +                                        < mindist) {
217 +                                minpos = j;
218 +                                mindist = hp->dirseg[j].s -
219 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
220 +                        }
221 +                                                /* good enough? */
222 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
223 +                        break;
224 +                j = minpos - 1;                 /* coalesce neighbors */
225 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
226 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
227 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
228 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
229 +                hp->dirty--;
230 +                while (++j < hp->dirty)         /* close the gap */
231 +                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
232 +        } while (mindist <= MINDIRSEL);
233 + }
234 +
235 +
236   int
237   hdsync(hp, all)                 /* update beams and directory on disk */
238   register HOLO   *hp;
# Line 183 | Line 252 | int    all;
252                          hdsyncbeam(hp, j);
253          if (!hp->dirty)                 /* directory clean? */
254                  return(0);
255 <        errno = 0;
256 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
257 <                error(SYSTEM, "cannot seek on holodeck file");
258 <        n = nbeams(hp)*sizeof(BEAMI);
259 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
260 <                error(SYSTEM, "cannot update holodeck section directory");
261 <        hp->dirty = 0;
255 >        errno = 0;                      /* write dirty segments */
256 >        for (j = 0; j < hp->dirty; j++) {
257 >                if (lseek(hp->fd, biglob(hp)->fo +
258 >                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
259 >                        error(SYSTEM, "cannot seek on holodeck file");
260 >                n = hp->dirseg[j].n * sizeof(BEAMI);
261 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
262 >                        error(SYSTEM, "cannot update section directory");
263 >        }
264 >        hp->dirty = 0;                  /* all clean */
265          return(1);
266   }
267  
# Line 213 | Line 285 | int    all;                    /* include overhead (painful) */
285                  }
286          }
287          if (all)
288 <                for (j = 0; j < nhdfrags; j++) {
289 <                        total += sizeof(struct fragment);
290 <                        if (hdfrag[j].nfrags)
288 >                for (j = 0; j < nhdfragls; j++) {
289 >                        total += sizeof(struct fraglist);
290 >                        if (hdfragl[j].nfrags)
291                                  total += FRAGBLK*sizeof(BEAMI) *
292 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
292 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
293                  }
294          return(total);
295   }
# Line 231 | Line 303 | int    fd;
303  
304          if (fd < 0)
305                  return(-1);
306 <        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
306 >        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
307                  if ((fpos = lseek(fd, 0L, 1)) < 0)
308                          return(-1);
309                  flen = lseek(fd, 0L, 2);
310                  lseek(fd, fpos, 0);
311                  return(flen);
312          }
313 <        return(hdfrag[fd].flen);
313 >        return(hdfragl[fd].flen);
314   }
315  
316  
# Line 285 | Line 357 | int    nr;                     /* number of new rays desired */
357                  hp->bl[i]->tick = hdclock;      /* preempt swap */
358          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
359                  hdfreecache(PCTFREE, NULL);     /* free some space */
288        errno = 0;
360          if (hp->bl[i] == NULL) {                /* allocate (and load) */
361                  n = hp->bi[i].nrd + nr;
362 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
292 <                        goto memerr;
362 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
363                  blglob(hp)->nrm += n;
364                  if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
365 +                        errno = 0;
366                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
367                                  error(SYSTEM, "seek error on holodeck file");
368                          n *= sizeof(RAYVAL);
# Line 300 | Line 371 | int    nr;                     /* number of new rays desired */
371                                  "error reading beam from holodeck file");
372                  }
373          } else {                                /* just grow in memory */
374 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
375 <                                hdbsiz(hp->bl[i]->nrm + nr) );
305 <                if (hp->bl[i] == NULL)
306 <                        goto memerr;
374 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
375 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
376                  blglob(hp)->nrm += nr;
377          }
378          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
379          hp->bl[i]->nrm += nr;                   /* update in-core structure */
380          bzero((char *)p, nr*sizeof(RAYVAL));
381 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
313 <        blglob(hp)->tick = hdclock++;
381 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
382          return(p);                              /* point to new rays */
315 memerr:
316        error(SYSTEM, "out of memory in hdnewrays");
383   }
384  
385  
# Line 331 | Line 397 | register int   i;
397                          return(NULL);
398                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
399                          hdfreecache(PCTFREE, NULL);     /* get free space */
400 <                errno = 0;
335 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
336 <                        error(SYSTEM, "cannot allocate memory for beam");
400 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
401                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
402 +                errno = 0;
403                  if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
404                          error(SYSTEM, "seek error on holodeck file");
405                  n *= sizeof(RAYVAL);
406                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
407                          error(SYSTEM, "error reading beam from holodeck file");
408          }
409 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
345 <        blglob(hp)->tick = hdclock++;
409 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
410          return(hp->bl[i]);
411   }
412  
413  
414   int
415 < hdfilord(hb1, hb2)      /* order beams for optimal loading */
415 > hdfilord(hb1, hb2)      /* order beams for quick loading */
416   register HDBEAMI        *hb1, *hb2;
417   {
418 <        register int    c;
419 <                                /* sort by file descriptor first */
418 >        register long   c;
419 >                                /* residents go first */
420 >        if (hb2->h->bl[hb2->b] != NULL)
421 >                return(hb1->h->bl[hb1->b] == NULL);
422 >        if (hb1->h->bl[hb1->b] != NULL)
423 >                return(-1);
424 >                                /* otherwise sort by file descriptor */
425          if ((c = hb1->h->fd - hb2->h->fd))
426                  return(c);
427                                  /* then by position in file */
428 <        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
428 >        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
429 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
430   }
431  
432  
# Line 366 | Line 436 | int    n;                      /* list length */
436   int     (*bf)();                /* callback function (optional) */
437   {
438          unsigned        origcachesize, memuse;
369        register BEAM   *bp;
439          int     bytesloaded, needbytes, bytes2free;
440 +        register BEAM   *bp;
441          register int    i;
442                                          /* precheck consistency */
443 +        if (n <= 0) return;
444          for (i = n; i--; )
445                  if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
446                          error(CONSISTENCY, "bad beam in hdloadbeams");
447                                          /* sort list for optimal access */
448          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
449 <        bytesloaded = needbytes = 0;    /* figure out memory needs */
449 >        bytesloaded = 0;                /* run through loaded beams */
450 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
451 >                bp->tick = hdclock;     /* preempt swap */
452 >                bytesloaded += bp->nrm;
453 >                if (bf != NULL)
454 >                        (*bf)(bp, hb);
455 >        }
456 >        bytesloaded *= sizeof(RAYVAL);
457          if ((origcachesize = hdcachesize) > 0) {
458 +                needbytes = 0;          /* figure out memory needs */
459                  for (i = n; i--; )
460 <                        if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
382 <                                bp->tick = hdclock;     /* preempt swap */
383 <                                bytesloaded += bp->nrm;
384 <                        } else                          /* prepare to load */
385 <                                needbytes += hb[i].h->bi[hb[i].b].nrd;
386 <                bytesloaded *= sizeof(RAYVAL);
460 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
461                  needbytes *= sizeof(RAYVAL);
462                  do {                            /* free enough memory */
463                          memuse = hdmemuse(0);
464 <                        bytes2free = needbytes - (signed)(hdcachesize-memuse);
465 <                        if (bytes2free > (signed)(memuse - bytesloaded))
464 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
465 >                        if (bytes2free > (int)(memuse - bytesloaded))
466                                  bytes2free = memuse - bytesloaded;
467                  } while (bytes2free > 0 &&
468                                  hdfreecache(100*bytes2free/memuse, NULL) < 0);
469 +                hdcachesize = 0;                /* load beams w/o swap */
470          }
396        hdcachesize = 0;                /* load the ordered beams w/o swap */
471          for (i = 0; i < n; i++)
472                  if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
473 <                        (*bf)(bp, hb[i].h, hb[i].b);
473 >                        (*bf)(bp, hb+i);
474          hdcachesize = origcachesize;    /* resume dynamic swapping */
475   }
476  
477  
478 + hdfreefrag(fd, bi)                      /* free a file fragment */
479 + int     fd;
480 + register BEAMI  *bi;
481 + {
482 +        register struct fraglist        *f;
483 +        register int    j, k;
484 +
485 +        if (bi->nrd == 0)
486 +                return;
487 + #ifdef DEBUG
488 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
489 +                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
490 + #endif
491 +        f = &hdfragl[fd];
492 +        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
493 +                for (j = k = 0; k < f->nfrags; j++, k++) {
494 +                        while (f->fi[k].nrd == 0)
495 +                                if (++k >= f->nfrags)
496 +                                        goto endloop;
497 +                        if (k > j)
498 +                                copystruct(f->fi+j, f->fi+k);
499 +                }
500 +        endloop:
501 +                f->nfrags = j;
502 +        }
503 +        j = f->nfrags++;                /* allocate a slot in free list */
504 + #if MAXFRAGB
505 +        if (j >= MAXFRAGB*FRAGBLK) {
506 +                f->nfrags = j--;        /* stop list growth */
507 +                if (bi->nrd <= f->fi[j].nrd)
508 +                        return;         /* new one no better than discard */
509 +        }
510 + #endif
511 +        if (j % FRAGBLK == 0) {         /* more free list space */
512 +                register BEAMI  *newp;
513 +                if (f->fi == NULL)
514 +                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
515 +                else
516 +                        newp = (BEAMI *)realloc((char *)f->fi,
517 +                                        (j+FRAGBLK)*sizeof(BEAMI));
518 +                if (newp == NULL) {
519 +                        f->nfrags--;    /* graceful failure */
520 +                        return;
521 +                }
522 +                f->fi = newp;
523 +        }
524 +        for ( ; ; j--) {                /* insert in descending list */
525 +                if (!j || bi->fo < f->fi[j-1].fo) {
526 +                        f->fi[j].fo = bi->fo;
527 +                        f->fi[j].nrd = bi->nrd;
528 +                        break;
529 +                }
530 +                copystruct(f->fi+j, f->fi+(j-1));
531 +        }
532 +                                        /* coalesce adjacent fragments */
533 +                                                /* successors never empty */
534 +        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
535 +                f->fi[j].nrd += f->fi[j-1].nrd;
536 +                f->fi[j-1].nrd = 0;
537 +        }
538 +        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
539 +                if (f->fi[k].nrd) {
540 +                        if (f->fi[j].fo == f->fi[k].fo +
541 +                                        f->fi[k].nrd*sizeof(RAYVAL)) {
542 +                                f->fi[k].nrd += f->fi[j].nrd;
543 +                                f->fi[j].nrd = 0;
544 +                        }
545 +                        break;
546 +                }
547 + }
548 +
549 +
550 + long
551 + hdallocfrag(fd, nrays)          /* allocate a file fragment */
552 + int     fd;
553 + unsigned int4   nrays;
554 + {
555 +        register struct fraglist        *f;
556 +        register int    j, k;
557 +        long    nfo;
558 +
559 +        if (nrays == 0)
560 +                return(-1L);
561 + #ifdef DEBUG
562 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
563 +                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
564 + #endif
565 +        f = &hdfragl[fd];
566 +        k = -1;                         /* find closest-sized fragment */
567 +        for (j = f->nfrags; j-- > 0; )
568 +                if (f->fi[j].nrd >= nrays &&
569 +                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
570 +                        if (f->fi[k=j].nrd == nrays)
571 +                                break;
572 +        if (k < 0) {                    /* no fragment -- extend file */
573 +                nfo = f->flen;
574 +                f->flen += nrays*sizeof(RAYVAL);
575 +        } else {                        /* else use fragment */
576 +                nfo = f->fi[k].fo;
577 +                f->fi[k].fo += nrays*sizeof(RAYVAL);
578 +                f->fi[k].nrd -= nrays;
579 +        }
580 +        return(nfo);
581 + }
582 +
583 +
584   int
585   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
586   register HOLO   *hp;
587   register int    i;
588   {
589 <        unsigned int    nrays;
410 <        long    nfo;
589 >        unsigned int4   nrays;
590          unsigned int    n;
591 +        long    nfo;
592                                          /* check file status */
593 <        if (hdfrag[hp->fd].writerr)
593 >        if (hdfragl[hp->fd].writerr)
594                  return(-1);
595   #ifdef DEBUG
596          if (i < 1 | i > nbeams(hp))
# Line 419 | Line 599 | register int   i;
599                                          /* is current fragment OK? */
600          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
601                  return(0);
602 <                                        /* locate fragment */
603 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
604 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
605 <
426 <        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
427 <                        hdfrag[hp->fd].flen)            /* EOF special case */
428 <                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
429 <
430 <        else {                                          /* general case */
431 <                register struct fragment        *f = &hdfrag[hp->fd];
432 <                register int    j, k;
433 <                                        /* relinquish old fragment */
434 <                if (hp->bi[i].nrd) {
435 <                        j = f->nfrags++;
436 < #if MAXFRAG
437 <                        if (j >= MAXFRAG-1)
438 <                                f->nfrags--;
439 < #endif
440 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
441 <                                if (f->fi == NULL)
442 <                                        f->fi = (BEAMI *)malloc(
443 <                                                        FRAGBLK*sizeof(BEAMI));
444 <                                else
445 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
446 <                                                (j+FRAGBLK)*sizeof(BEAMI));
447 <                                if (f->fi == NULL)
448 <                                        error(SYSTEM,
449 <                                                "out of memory in hdsyncbeam");
450 <                        }
451 <                        for ( ; ; j--) {        /* insert in descending list */
452 <                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
453 <                                        f->fi[j].fo = hp->bi[i].fo;
454 <                                        f->fi[j].nrd = hp->bi[i].nrd;
455 <                                        break;
456 <                                }
457 <                                copystruct(f->fi+j, f->fi+(j-1));
458 <                        }
459 <                                        /* coalesce adjacent fragments */
460 <                        if (j && f->fi[j-1].fo == f->fi[j].fo +
461 <                                        f->fi[j].nrd*sizeof(RAYVAL)) {
462 <                                f->fi[j].nrd += f->fi[j-1].nrd;
463 <                                f->fi[j-1].nrd = 0;
464 <                        }
465 <                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo +
466 <                                        f->fi[j+1].nrd*sizeof(RAYVAL)) {
467 <                                f->fi[j+1].nrd += f->fi[j].nrd;
468 <                                f->fi[j].nrd = 0;
469 <                        }
470 <                }
471 <                k = -1;                 /* find closest-sized fragment */
472 <                for (j = (nrays ? f->nfrags : 0); j-- > 0; )
473 <                        if (f->fi[j].nrd >= nrays &&
474 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
475 <                                if (f->fi[k=j].nrd == nrays)
476 <                                        break;
477 <                if (k < 0) {            /* no fragment -- extend file */
478 <                        nfo = f->flen;
479 <                        f->flen += nrays*sizeof(RAYVAL);
480 <                } else {                /* else use fragment */
481 <                        nfo = f->fi[k].fo;
482 <                        f->fi[k].fo += nrays*sizeof(RAYVAL);
483 <                        f->fi[k].nrd -= nrays;
484 <                }
485 <                                        /* delete empty remnants */
486 <                for (j = k = 0; k < f->nfrags; j++, k++) {
487 <                        while (f->fi[k].nrd == 0)
488 <                                if (++k >= f->nfrags)
489 <                                        goto endloop;
490 <                        if (k > j)
491 <                                copystruct(f->fi+j, f->fi+k);
492 <                }
493 <        endloop:
494 <                f->nfrags = j;
495 <        }
496 <        if (nrays) {            /* write the new fragment */
602 >        if (hp->bi[i].nrd)              /* relinquish old fragment */
603 >                hdfreefrag(hp->fd, &hp->bi[i]);
604 >        if (nrays) {                    /* get and write new fragment */
605 >                nfo = hdallocfrag(hp->fd, nrays);
606                  errno = 0;
607                  if (lseek(hp->fd, nfo, 0) < 0)
608                          error(SYSTEM, "cannot seek on holodeck file");
609                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
610                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
611 <                        hdfrag[hp->fd].writerr++;
612 <                        hdsync(hp, 0);          /* sync directory */
611 >                        hdfragl[hp->fd].writerr++;
612 >                        hdsync(NULL, 0);        /* sync directories */
613                          error(SYSTEM, "write error in hdsyncbeam");
614                  }
615 <        }
615 >                hp->bi[i].fo = nfo;
616 >        } else
617 >                hp->bi[i].fo = 0L;
618          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
619          hp->bi[i].nrd = nrays;
620 <        hp->bi[i].fo = nfo;
510 <        markdirty(hp);          /* section directory now out of date */
620 >        markdirty(hp, i);               /* section directory now out of date */
621          return(1);
622   }
623  
# Line 525 | Line 635 | register int   i;
635                          nchanged += hdfreebeam(hdlist[i], 0);
636                  return(nchanged);
637          }
638 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
638 >        if (hdfragl[hp->fd].writerr)    /* check for file error */
639                  return(0);
640          if (i == 0) {                   /* clear entire holodeck */
641                  nchanged = 0;
# Line 603 | Line 713 | register HOLO  *hp;                    /* section we're adding from */
713   {
714          register int    i, j;
715                                          /* insert each beam from hp */
716 <        for (i = nbeams(hp); i > 0; i--) {
716 >        for (i = 1; i <= nbeams(hp); i++) {
717                  if (hp->bl[i] == NULL)          /* check if loaded */
718                          continue;
719   #if 0
# Line 635 | Line 745 | register HOLO  *honly;                 /* NULL means check all */
745          int     freetarget;
746          int     n;
747          register int    i;
748 + #ifdef DEBUG
749 +        unsigned        membefore;
750 +
751 +        membefore = hdmemuse(0);
752 + #endif
753                                                  /* compute free target */
754          freetarget = (honly != NULL) ? blglob(honly)->nrm :
755                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 655 | Line 770 | register HOLO  *honly;                 /* NULL means check all */
770                          break;
771          }
772          hdsync(honly, 0);       /* synchronize directories as necessary */
773 + #ifdef DEBUG
774 +        sprintf(errmsg,
775 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
776 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
777 +        wputs(errmsg);
778 + #endif
779          return(-freetarget);    /* return how far past goal we went */
780   }
781  
# Line 667 | Line 788 | register HOLO  *hp;            /* NULL means clean up all */
788          if (hp == NULL) {               /* NULL means clean up everything */
789                  while (hdlist[0] != NULL)
790                          hddone(hdlist[0]);
791 +                free((char *)hdfragl);
792 +                hdfragl = NULL; nhdfragls = 0;
793                  return;
794          }
795                                          /* flush all data and free memory */
796 <        hdflush(hp);
796 >        hdfreebeam(hp, 0);
797 >        hdsync(hp, 0);
798                                          /* release fragment resources */
799          hdrelease(hp->fd);
800                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines