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.14 by gregl, Thu Dec 18 15:12:23 1997 UTC vs.
Revision 3.52 by schorsch, Sun Jul 27 22:12:02 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for managing holodeck files
6   *
7   *      9/30/97 GWLarson
8   */
9  
10 + #include "copyright.h"
11 +
12 + #include <string.h>
13 +
14   #include "holo.h"
15  
16   #ifndef CACHESIZE
17 < #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
17 > #ifdef SMLMEM
18 > #define CACHESIZE       5
19 > #else
20 > #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
21   #endif
22 + #endif
23   #ifndef FREEBEAMS
24 < #define FREEBEAMS       512     /* maximum beams to free at a time */
24 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
25   #endif
26   #ifndef PCTFREE
27 < #define PCTFREE         20      /* maximum fraction to free (%) */
27 > #define PCTFREE         15      /* maximum fraction to free (%) */
28   #endif
29 + #ifndef MAXFRAGB
30 + #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
31 + #endif
32 + #ifndef FF_DEFAULT
33 +                                /* when to free a beam fragment */
34 + #define FF_DEFAULT      (FF_WRITE|FF_KILL)
35 + #endif
36 + #ifndef MINDIRSEL
37 +                                /* minimum directory seek length */
38 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
39 + #endif
40  
25 /* define MAXFRAG if you want to limit fragment tracking memory */
26
41   #ifndef BSD
42   #define write   writebuf        /* safe i/o routines */
43   #define read    readbuf
44   #endif
45  
46 < #define FRAGBLK         64      /* number of fragments to allocate at a time */
46 > #define FRAGBLK         512     /* number of fragments to allocate at a time */
47  
48 + int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */
49   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
50 < unsigned long   hdclock;        /* clock value */
50 > unsigned long   hdclock;                /* clock value */
51  
52   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
53  
54 < static struct fragment {
54 > static struct fraglist {
55          short   nlinks;         /* number of holodeck sections using us */
56 <        short   writerr;        /* write error encountered */
56 >        short   writable;       /* 0 read-only, <0 write error encountered */
57          int     nfrags;         /* number of known fragments */
58          BEAMI   *fi;            /* fragments, descending file position */
59 <        long    flen;           /* last known file length */
60 < } *hdfrag;              /* fragment lists, indexed by file descriptor */
59 >        off_t   flen;           /* last known file length */
60 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
61  
62 < static int      nhdfrags;       /* size of hdfrag array */
62 > static int      nhdfragls;      /* size of hdfragl array */
63  
64  
65 < hdattach(fd)            /* start tracking file fragments for some section */
66 < register int    fd;
65 > HOLO *
66 > hdalloc(hproto)         /* allocate and set holodeck section based on grid */
67 > HDGRID  *hproto;
68   {
69 <        if (fd >= nhdfrags) {
70 <                if (nhdfrags)
71 <                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
72 <                                        (fd+1)*sizeof(struct fragment));
73 <                else
74 <                        hdfrag = (struct fragment *)malloc(
75 <                                        (fd+1)*sizeof(struct fragment));
76 <                if (hdfrag == NULL)
77 <                        error(SYSTEM, "out of memory in hdattach");
78 <                bzero((char *)(hdfrag+nhdfrags),
79 <                                (fd+1-nhdfrags)*sizeof(struct fragment));
80 <                nhdfrags = fd+1;
69 >        HOLO    hdhead;
70 >        register HOLO   *hp;
71 >        int     n;
72 >                                /* copy grid to temporary header */
73 >        memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID));
74 >                                /* compute grid vectors and sizes */
75 >        hdcompgrid(&hdhead);
76 >                                /* allocate header with directory */
77 >        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
78 >        if ((hp = (HOLO *)malloc(n)) == NULL)
79 >                return(NULL);
80 >                                /* copy header information */
81 >        *hp = hdhead;
82 >                                /* allocate and clear beam list */
83 >        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
84 >        if (hp->bl == NULL) {
85 >                free((void *)hp);
86 >                return(NULL);
87          }
88 <        hdfrag[fd].nlinks++;
89 <        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
88 >        memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
89 >        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
90 >        hp->fd = -1;
91 >        hp->dirty = 0;
92 >        hp->priv = NULL;
93 >                                /* clear beam directory */
94 >        memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI));
95 >        return(hp);             /* all is well */
96   }
97  
98  
99 < /* Do we need a routine to locate file fragments given known occupants? */
99 > char *
100 > hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
101 > char    *ptr;
102 > unsigned        siz;
103 > char    *rout;
104 > {
105 >        register char   *newp;
106 >                                        /* call malloc/realloc */
107 >        if (ptr == NULL) newp = (char *)malloc(siz);
108 >        else newp = (char *)realloc((void *)ptr, siz);
109 >                                        /* check success */
110 >        if (newp == NULL && rout != NULL) {
111 >                hdfreecache(25, NULL);  /* free some memory */
112 >                errno = 0;              /* retry */
113 >                newp = hdrealloc(ptr, siz, NULL);
114 >                if (newp == NULL) {     /* give up and report error */
115 >                        sprintf(errmsg, "out of memory in %s", rout);
116 >                        error(SYSTEM, errmsg);
117 >                }
118 >        }
119 >        return(newp);
120 > }
121  
122  
123 < hdrelease(fd)           /* stop tracking file fragments for some section */
123 > hdattach(fd, wr)        /* start tracking file fragments for some section */
124   register int    fd;
125 + int     wr;
126   {
127 <        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
128 <                return;
129 <        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
130 <                free((char *)hdfrag[fd].fi);
131 <                hdfrag[fd].fi = NULL;
132 <                hdfrag[fd].nfrags = 0;
127 >        if (fd >= nhdfragls) {
128 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
129 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
130 >                memset((void *)(hdfragl+nhdfragls),
131 >                                '\0', (fd+1-nhdfragls)*sizeof(struct fraglist));
132 >                nhdfragls = fd+1;
133          }
134 +        hdfragl[fd].nlinks++;
135 +        hdfragl[fd].writable = wr;              /* set writable flag */
136 +        hdfragl[fd].flen = lseek(fd, (off_t)0, 2);      /* get file length */
137   }
138  
139  
140 < markdirty(hp)                   /* mark holodeck section directory dirty */
88 < register HOLO   *hp;
89 < {
90 <        static BEAMI    smudge = {0, -1};
140 > /* Do we need a routine to locate file fragments given known occupants? */
141  
142 <        if (hp->dirty)          /* already marked? */
142 >
143 > hdrelease(fd)           /* stop tracking file fragments for some section */
144 > register int    fd;
145 > {
146 >        if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks)
147                  return;
148 <        hp->dirty = 1;
149 <        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
150 <                        || write(hp->fd, (char *)&smudge,
151 <                                        sizeof(BEAMI)) != sizeof(BEAMI))
152 <                error(SYSTEM, "seek/write error in markdirty");
148 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
149 >                free((void *)hdfragl[fd].fi);
150 >                hdfragl[fd].fi = NULL;
151 >                hdfragl[fd].nfrags = 0;
152 >        }
153   }
154  
155  
# Line 104 | Line 158 | hdinit(fd, hproto)     /* initialize a holodeck section in
158   int     fd;                     /* corresponding file descriptor */
159   HDGRID  *hproto;                /* holodeck section grid */
160   {
161 <        long    fpos;
161 >        off_t   rtrunc;
162 >        off_t   fpos;
163 >        int     writable;
164          register HOLO   *hp;
165          register int    n;
166                                          /* prepare for system errors */
167          errno = 0;
168 <        if ((fpos = lseek(fd, 0L, 1)) < 0)
168 >        if ((fpos = lseek(fd, (off_t)0, 1)) < 0)
169                  error(SYSTEM, "cannot determine holodeck file position");
170          if (hproto == NULL) {           /* assume we're loading it */
171                  HDGRID  hpr;
# Line 124 | Line 180 | HDGRID *hproto;                /* holodeck section grid */
180                  if (read(fd, (char *)(hp->bi+1), n) != n)
181                          error(SYSTEM, "failure loading holodeck directory");
182                                                  /* check that it's clean */
183 <                if (hp->bi[nbeams(hp)].fo < 0)
184 <                        error(USER, "dirty holodeck section");
185 <        } else {                        /* assume we're creating it */
183 >                for (n = nbeams(hp); n > 0; n--)
184 >                        if (hp->bi[n].fo < 0) {
185 >                                hp->bi[n].fo = 0;
186 >                                error(WARNING, "dirty holodeck section");
187 >                                break;
188 >                        }
189 >                                                /* check writability */
190 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
191 >                        writable = hdfragl[fd].writable;
192 >                else
193 >                        writable = lseek(fd, fpos, 0) == fpos &&
194 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
195 >                                                        sizeof(HDGRID);
196 >        } else {                        /* else assume we're creating it */
197                  if ((hp = hdalloc(hproto)) == NULL)
198                          goto memerr;
199                                                  /* write header and skeleton */
# Line 135 | Line 202 | HDGRID *hproto;                /* holodeck section grid */
202                                          sizeof(HDGRID) ||
203                                  write(fd, (char *)(hp->bi+1), n) != n)
204                          error(SYSTEM, "cannot write header to holodeck file");
205 +                writable = 1;
206          }
207          hp->fd = fd;    
208          hp->dirty = 0;
209          biglob(hp)->fo = fpos + sizeof(HDGRID);
210                                          /* start tracking fragments */
211 <        hdattach(fd);
211 >        hdattach(fd, writable);
212                                          /* check rays on disk */
213          fpos = hdfilen(fd);
214 <        biglob(hp)->nrd = 0;
214 >        biglob(hp)->nrd = rtrunc = 0;
215          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
216 <                if (hp->bi[n].nrd)
217 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
218 <                                hp->bi[n].nrd = 0;      /* off end */
219 <                        else
216 >                if (hp->bi[n].nrd) {
217 >                        if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
218 >                                rtrunc += hp->bi[n].nrd;
219 >                                hp->bi[n].nrd = 0;
220 >                        } else
221                                  biglob(hp)->nrd += hp->bi[n].nrd;
222 +                }
223 +        if (rtrunc) {
224 +                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
225 +                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
226 +                error(WARNING, errmsg);
227 +        }
228                                          /* add to holodeck list */
229          for (n = 0; n < HDMAX; n++)
230                  if (hdlist[n] == NULL) {
# Line 163 | Line 238 | memerr:
238   }
239  
240  
241 + hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
242 + register HOLO   *hp;
243 + int     i;
244 + {
245 +        static BEAMI    smudge = {0, -1};
246 +        int     mindist, minpos;
247 +        register int    j;
248 +
249 +        if (!hp->dirty++) {                     /* write smudge first time */
250 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
251 +                                || write(hp->fd, (char *)&smudge,
252 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
253 +                        error(SYSTEM, "seek/write error in hdmarkdirty");
254 +                hp->dirseg[0].s = i;
255 +                hp->dirseg[0].n = 1;
256 +                return;
257 +        }
258 +                                                /* insert into segment list */
259 +        for (j = hp->dirty; j--; ) {
260 +                if (!j || hp->dirseg[j-1].s < i) {
261 +                        hp->dirseg[j].s = i;
262 +                        hp->dirseg[j].n = 1;
263 +                        break;
264 +                }
265 +                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
266 +        }
267 +        do {                            /* check neighbors */
268 +                mindist = nbeams(hp);           /* find closest */
269 +                for (j = hp->dirty; --j; )
270 +                        if (hp->dirseg[j].s -
271 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
272 +                                        < mindist) {
273 +                                minpos = j;
274 +                                mindist = hp->dirseg[j].s -
275 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
276 +                        }
277 +                                                /* good enough? */
278 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
279 +                        break;
280 +                j = minpos - 1;                 /* coalesce neighbors */
281 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
282 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
283 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
284 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
285 +                hp->dirty--;
286 +                while (++j < hp->dirty)         /* close the gap */
287 +                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
288 +        } while (mindist <= MINDIRSEL);
289 + }
290 +
291 +
292   int
293   hdsync(hp, all)                 /* update beams and directory on disk */
294   register HOLO   *hp;
# Line 177 | Line 303 | int    all;
303                  return(n);
304          }
305                                          /* sync the beams */
306 <        for (j = all ? nbeams(hp) : 0; j > 0; j--)
306 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
307                  if (hp->bl[j] != NULL)
308                          hdsyncbeam(hp, j);
309          if (!hp->dirty)                 /* directory clean? */
310                  return(0);
311 <        errno = 0;
312 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
313 <                error(SYSTEM, "cannot seek on holodeck file");
314 <        n = nbeams(hp)*sizeof(BEAMI);
315 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
316 <                error(SYSTEM, "cannot update holodeck section directory");
317 <        hp->dirty = 0;
311 >        errno = 0;                      /* write dirty segments */
312 >        for (j = 0; j < hp->dirty; j++) {
313 >                if (lseek(hp->fd, biglob(hp)->fo +
314 >                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
315 >                        error(SYSTEM, "cannot seek on holodeck file");
316 >                n = hp->dirseg[j].n * sizeof(BEAMI);
317 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
318 >                        error(SYSTEM, "cannot update section directory");
319 >        }
320 >        hp->dirty = 0;                  /* all clean */
321          return(1);
322   }
323  
# Line 212 | Line 341 | int    all;                    /* include overhead (painful) */
341                  }
342          }
343          if (all)
344 <                for (j = 0; j < nhdfrags; j++) {
345 <                        total += sizeof(struct fragment);
346 <                        if (hdfrag[j].nfrags)
344 >                for (j = 0; j < nhdfragls; j++) {
345 >                        total += sizeof(struct fraglist);
346 >                        if (hdfragl[j].nfrags)
347                                  total += FRAGBLK*sizeof(BEAMI) *
348 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
348 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
349                  }
350          return(total);
351   }
352  
353  
354 < long
354 > off_t
355   hdfilen(fd)             /* return file length for fd */
356   int     fd;
357   {
358 <        long    fpos, flen;
358 >        off_t   fpos, flen;
359  
360          if (fd < 0)
361                  return(-1);
362 <        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
363 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
362 >        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
363 >                if ((fpos = lseek(fd, (off_t)0, 1)) < 0)
364                          return(-1);
365 <                flen = lseek(fd, 0L, 2);
365 >                flen = lseek(fd, (off_t)0, 2);
366                  lseek(fd, fpos, 0);
367                  return(flen);
368          }
369 <        return(hdfrag[fd].flen);
369 >        return(hdfragl[fd].flen);
370   }
371  
372  
373 < long
373 > off_t
374   hdfiluse(fd, all)       /* compute file usage (in bytes) */
375   int     fd;                     /* open file descriptor to check */
376   int     all;                    /* include overhead and unflushed data */
377   {
378 <        long    total = 0;
378 >        off_t   total = 0;
379          register int    i, j;
380  
381          for (j = 0; hdlist[j] != NULL; j++) {
# Line 276 | Line 405 | int    nr;                     /* number of new rays desired */
405          RAYVAL  *p;
406          int     n;
407  
408 <        if (nr <= 0)
409 <                return(NULL);
410 <        if (i < 1 | i > nbeams(hp))
282 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
408 >        if (nr <= 0) return(NULL);
409 >        CHECK((i < 1) | (i > nbeams(hp)),
410 >                        CONSISTENCY, "bad beam index given to hdnewrays");
411          if (hp->bl[i] != NULL)
412                  hp->bl[i]->tick = hdclock;      /* preempt swap */
413          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
414                  hdfreecache(PCTFREE, NULL);     /* free some space */
287        errno = 0;
415          if (hp->bl[i] == NULL) {                /* allocate (and load) */
416                  n = hp->bi[i].nrd + nr;
417 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
291 <                        goto memerr;
417 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
418                  blglob(hp)->nrm += n;
419 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
419 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
420 >                        errno = 0;
421                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
422                                  error(SYSTEM, "seek error on holodeck file");
423                          n *= sizeof(RAYVAL);
# Line 299 | Line 426 | int    nr;                     /* number of new rays desired */
426                                  "error reading beam from holodeck file");
427                  }
428          } else {                                /* just grow in memory */
429 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
430 <                                hdbsiz(hp->bl[i]->nrm + nr) );
304 <                if (hp->bl[i] == NULL)
305 <                        goto memerr;
429 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
430 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
431                  blglob(hp)->nrm += nr;
432          }
433 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
434 +                hdfreefrag(hp, i);              /* relinquish old fragment */
435          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
436          hp->bl[i]->nrm += nr;                   /* update in-core structure */
437 <        bzero((char *)p, nr*sizeof(RAYVAL));
438 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
312 <        blglob(hp)->tick = hdclock++;
437 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
438 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
439          return(p);                              /* point to new rays */
314 memerr:
315        error(SYSTEM, "out of memory in hdnewrays");
440   }
441  
442  
# Line 323 | Line 447 | register int   i;
447   {
448          register int    n;
449  
450 <        if (i < 1 | i > nbeams(hp))
451 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
450 >        CHECK((i < 1) | (i > nbeams(hp)),
451 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
452          if (hp->bl[i] == NULL) {                /* load from disk */
453                  if (!(n = hp->bi[i].nrd))
454                          return(NULL);
455                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
456                          hdfreecache(PCTFREE, NULL);     /* get free space */
457 <                errno = 0;
334 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
335 <                        error(SYSTEM, "cannot allocate memory for beam");
457 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
458                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
459 +                errno = 0;
460                  if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
461                          error(SYSTEM, "seek error on holodeck file");
462                  n *= sizeof(RAYVAL);
463                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
464                          error(SYSTEM, "error reading beam from holodeck file");
465 +                if (hdfragflags&FF_READ)
466 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
467          }
468 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
344 <        blglob(hp)->tick = hdclock++;
468 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
469          return(hp->bl[i]);
470   }
471  
472  
473   int
474 < hdfilord(hb1, hb2)      /* order beams for optimal loading */
474 > hdfilord(hb1, hb2)      /* order beams for quick loading */
475   register HDBEAMI        *hb1, *hb2;
476   {
477 <        register int    c;
478 <                                /* sort by file descriptor first */
479 <        if ((c = hb1->h->fd - hb2->h->fd))
480 <                return(c);
477 >        register off_t  c;
478 >                                /* residents go first */
479 >        if (hb2->h->bl[hb2->b] != NULL)
480 >                return(hb1->h->bl[hb1->b] == NULL);
481 >        if (hb1->h->bl[hb1->b] != NULL)
482 >                return(-1);
483 >                                /* otherwise sort by file descriptor */
484 >        if (hb1->h->fd != hb2->h->fd)
485 >                return(hb1->h->fd - hb2->h->fd);
486                                  /* then by position in file */
487 <        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
487 >        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
488 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
489   }
490  
491  
492   hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
493   register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
494   int     n;                      /* list length */
495 < int     (*bf)();                /* callback function (optional) */
495 > void    (*bf)();                /* callback function (optional) */
496   {
497          unsigned        origcachesize, memuse;
368        register BEAM   *bp;
498          int     bytesloaded, needbytes, bytes2free;
499 +        register BEAM   *bp;
500          register int    i;
501                                          /* precheck consistency */
502 +        if (n <= 0) return;
503          for (i = n; i--; )
504 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
504 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
505                          error(CONSISTENCY, "bad beam in hdloadbeams");
506                                          /* sort list for optimal access */
507 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
508 <        if ((origcachesize = hdcachesize) == 0)
509 <                goto loadbeams;
510 <        bytesloaded = needbytes = 0;    /* figure out memory needs */
511 <        for (i = 0; i < n; i++)
512 <                if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
513 <                        bp->tick = hdclock;             /* preempt swap */
514 <                        bytesloaded += bp->nrm;
384 <                } else                                  /* prepare to load */
385 <                        needbytes += hb[i].h->bi[hb[i].b].nrd;
507 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
508 >        bytesloaded = 0;                /* run through loaded beams */
509 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
510 >                bp->tick = hdclock;     /* preempt swap */
511 >                bytesloaded += bp->nrm;
512 >                if (bf != NULL)
513 >                        (*bf)(bp, hb);
514 >        }
515          bytesloaded *= sizeof(RAYVAL);
516 <        needbytes *= sizeof(RAYVAL);
517 <        do {                            /* free enough memory */
518 <                memuse = hdmemuse(0);
519 <                bytes2free = needbytes - (signed)(hdcachesize-memuse);
520 <                if (bytes2free > (signed)(memuse - bytesloaded))
521 <                        bytes2free = memuse - bytesloaded;
522 <        } while (bytes2free > 0 &&
523 <                        hdfreecache(100*bytes2free/memuse, NULL) < 0);
524 < loadbeams:
525 <        hdcachesize = 0;                /* load the ordered beams w/o swap */
516 >        if ((origcachesize = hdcachesize) > 0) {
517 >                needbytes = 0;          /* figure out memory needs */
518 >                for (i = n; i--; )
519 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
520 >                needbytes *= sizeof(RAYVAL);
521 >                do {                            /* free enough memory */
522 >                        memuse = hdmemuse(0);
523 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
524 >                        if (bytes2free > (int)(memuse - bytesloaded))
525 >                                bytes2free = memuse - bytesloaded;
526 >                } while (bytes2free > 0 &&
527 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
528 >                hdcachesize = 0;                /* load beams w/o swap */
529 >        }
530          for (i = 0; i < n; i++)
531                  if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
532 <                        (*bf)(bp, hb[i].h, hb[i].b);
532 >                        (*bf)(bp, hb+i);
533          hdcachesize = origcachesize;    /* resume dynamic swapping */
534   }
535  
536  
537   int
538 + hdfreefrag(hp, i)                       /* free a file fragment */
539 + HOLO    *hp;
540 + int     i;
541 + {
542 +        register BEAMI  *bi = &hp->bi[i];
543 +        register struct fraglist        *f;
544 +        register int    j, k;
545 +
546 +        if (bi->nrd <= 0)
547 +                return(0);
548 +        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
549 +                        CONSISTENCY, "bad file descriptor in hdfreefrag");
550 +        f = &hdfragl[hp->fd];
551 +        if (!f->writable)
552 +                return(0);
553 +        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
554 +                for (j = k = 0; k < f->nfrags; j++, k++) {
555 +                        while (f->fi[k].nrd == 0)
556 +                                if (++k >= f->nfrags)
557 +                                        goto endloop;
558 +                        if (k > j)
559 +                                *(f->fi+j) = *(f->fi+k);
560 +                }
561 +        endloop:
562 +                f->nfrags = j;
563 +        }
564 +        j = f->nfrags++;                /* allocate a slot in free list */
565 + #if MAXFRAGB
566 +        if (j >= MAXFRAGB*FRAGBLK) {
567 +                f->nfrags = j--;        /* stop list growth */
568 +                if (bi->nrd <= f->fi[j].nrd)
569 +                        return(0);      /* new one no better than discard */
570 +        }
571 + #endif
572 +        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
573 +                register BEAMI  *newp;
574 +                if (f->fi == NULL)
575 +                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
576 +                else
577 +                        newp = (BEAMI *)realloc((void *)f->fi,
578 +                                        (j+FRAGBLK)*sizeof(BEAMI));
579 +                if (newp == NULL) {
580 +                        f->nfrags--;    /* graceful failure */
581 +                        return(0);
582 +                }
583 +                f->fi = newp;
584 +        }
585 +        for ( ; ; j--) {                /* insert in descending list */
586 +                if (!j || bi->fo < f->fi[j-1].fo) {
587 +                        f->fi[j].fo = bi->fo;
588 +                        f->fi[j].nrd = bi->nrd;
589 +                        break;
590 +                }
591 +                *(f->fi+j) = *(f->fi+(j-1));
592 +        }
593 +                                        /* coalesce adjacent fragments */
594 +                                                /* successors never empty */
595 +        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
596 +                f->fi[j].nrd += f->fi[j-1].nrd;
597 +                f->fi[j-1].nrd = 0;
598 +        }
599 +        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
600 +                if (f->fi[k].nrd) {
601 +                        if (f->fi[j].fo == f->fi[k].fo +
602 +                                        f->fi[k].nrd*sizeof(RAYVAL)) {
603 +                                f->fi[k].nrd += f->fi[j].nrd;
604 +                                f->fi[j].nrd = 0;
605 +                        }
606 +                        break;
607 +                }
608 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
609 +        bi->nrd = 0;
610 +        bi->fo = 0;
611 +        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
612 +        return(1);
613 + }
614 +
615 +
616 + int
617 + hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
618 + int     fd;
619 + int     *listlen;
620 + register int32  *listsiz;
621 + {
622 +        register struct fraglist        *f;
623 +        register int    i;
624 +
625 +        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
626 +                return(0);              /* listless */
627 +        if (listlen != NULL)
628 +                *listlen = f->nfrags;
629 +        if (listsiz != NULL)
630 +                for (i = f->nfrags, *listsiz = 0; i--; )
631 +                        *listsiz += f->fi[i].nrd;
632 + #if MAXFRAGB
633 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
634 + #else
635 +        return(1);                      /* list never fills up */
636 + #endif
637 + }
638 +
639 +
640 + off_t
641 + hdallocfrag(fd, nrays)          /* allocate a file fragment */
642 + int     fd;
643 + uint32  nrays;
644 + {
645 +        register struct fraglist        *f;
646 +        register int    j;
647 +        off_t   nfo;
648 +
649 +        if (nrays == 0)
650 +                return(-1L);
651 +        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
652 +                        CONSISTENCY, "bad file descriptor in hdallocfrag");
653 +        f = &hdfragl[fd];
654 +        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
655 +                if (f->fi[j].nrd >= nrays)
656 +                        break;
657 +        if (j < 0) {                    /* no fragment -- extend file */
658 +                nfo = f->flen;
659 +                f->flen += nrays*sizeof(RAYVAL);
660 +        } else {                        /* else use fragment */
661 +                nfo = f->fi[j].fo;
662 +                f->fi[j].fo += nrays*sizeof(RAYVAL);
663 +                f->fi[j].nrd -= nrays;
664 +        }
665 +        return(nfo);
666 + }
667 +
668 +
669 + int
670   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
671   register HOLO   *hp;
672   register int    i;
673   {
674 <        unsigned int    nrays;
675 <        long    nfo;
674 >        int     fragfreed;
675 >        uint32  nrays;
676          unsigned int    n;
677 +        off_t   nfo;
678                                          /* check file status */
679 <        if (hdfrag[hp->fd].writerr)
680 <                return(-1);
681 < #ifdef DEBUG
682 <        if (i < 1 | i > nbeams(hp))
417 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
418 < #endif
679 >        if (hdfragl[hp->fd].writable <= 0)
680 >                return(hdfragl[hp->fd].writable);
681 >        DCHECK(i < 1 | i > nbeams(hp),
682 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
683                                          /* is current fragment OK? */
684          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
685                  return(0);
686 <                                        /* locate fragment */
687 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
688 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
689 <
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 < #ifdef 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 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
461 <                                if (k > j)
462 <                                        copystruct(f->fi+j, f->fi+k);
463 <                                while (k+1 < f->nfrags && f->fi[k+1].fo +
464 <                                                f->fi[k+1].nrd*sizeof(RAYVAL)
465 <                                                        == f->fi[j].fo) {
466 <                                        f->fi[j].fo -=
467 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
468 <                                        f->fi[j].nrd += f->fi[k].nrd;
469 <                                }
470 <                        }
471 <                        f->nfrags = j;
472 <                }
473 <                k = -1;                 /* find closest-sized fragment */
474 <                for (j = nrays ? f->nfrags : 0; j-- > 0; )
475 <                        if (f->fi[j].nrd >= nrays &&
476 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
477 <                                if (f->fi[k=j].nrd == nrays)
478 <                                        break;
479 <                if (k < 0) {            /* no fragment -- extend file */
480 <                        nfo = f->flen;
481 <                        f->flen += nrays*sizeof(RAYVAL);
482 <                } else {                /* else use fragment */
483 <                        nfo = f->fi[k].fo;
484 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
485 <                                f->nfrags--;
486 <                                for (j = k; j < f->nfrags; j++)
487 <                                        copystruct(f->fi+j, f->fi+(j+1));
488 <                        } else {                        /* else shrink it */
489 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
490 <                                f->fi[k].nrd -= nrays;
491 <                        }
492 <                }
493 <        }
494 <        if (nrays) {            /* write the new fragment */
686 >                                        /* relinquish old fragment? */
687 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
688 >        if (nrays) {                    /* get and write new fragment */
689 >                nfo = hdallocfrag(hp->fd, nrays);
690                  errno = 0;
691                  if (lseek(hp->fd, nfo, 0) < 0)
692                          error(SYSTEM, "cannot seek on holodeck file");
693                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
694                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
695 <                        hdfrag[hp->fd].writerr++;
696 <                        hdsync(hp, 0);          /* sync directory */
695 >                        hdfragl[hp->fd].writable = -1;
696 >                        hdsync(NULL, 0);        /* sync directories */
697                          error(SYSTEM, "write error in hdsyncbeam");
698                  }
699 <        }
699 >                hp->bi[i].fo = nfo;
700 >        } else
701 >                hp->bi[i].fo = 0;
702          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
703          hp->bi[i].nrd = nrays;
704 <        hp->bi[i].fo = nfo;
705 <        markdirty(hp);          /* section directory now out of date */
704 >        if (!fragfreed)
705 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
706          return(1);
707   }
708  
# Line 523 | Line 720 | register int   i;
720                          nchanged += hdfreebeam(hdlist[i], 0);
721                  return(nchanged);
722          }
723 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
723 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
724                  return(0);
725          if (i == 0) {                   /* clear entire holodeck */
726 +                if (blglob(hp)->nrm == 0)
727 +                        return(0);              /* already clear */
728                  nchanged = 0;
729                  for (i = nbeams(hp); i > 0; i--)
730                          if (hp->bl[i] != NULL)
731                                  nchanged += hdfreebeam(hp, i);
732 +                DCHECK(blglob(hp)->nrm != 0,
733 +                                CONSISTENCY, "bad beam count in hdfreebeam");
734                  return(nchanged);
735          }
736 < #ifdef DEBUG
737 <        if (i < 1 | i > nbeams(hp))
537 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
538 < #endif
736 >        DCHECK(i < 1 | i > nbeams(hp),
737 >                        CONSISTENCY, "bad beam index to hdfreebeam");
738          if (hp->bl[i] == NULL)
739                  return(0);
740                                          /* check for additions */
# Line 543 | Line 742 | register int   i;
742          if (nchanged)
743                  hdsyncbeam(hp, i);              /* write new fragment */
744          blglob(hp)->nrm -= hp->bl[i]->nrm;
745 <        free((char *)hp->bl[i]);                /* free memory */
745 >        free((void *)hp->bl[i]);                /* free memory */
746          hp->bl[i] = NULL;
747          return(nchanged);
748   }
# Line 554 | Line 753 | hdkillbeam(hp, i)              /* delete beam from holodeck */
753   register HOLO   *hp;
754   register int    i;
755   {
557        static BEAM     emptybeam;
756          int     nchanged;
757  
758          if (hp == NULL) {               /* clobber all holodecks */
# Line 564 | Line 762 | register int   i;
762                  return(nchanged);
763          }
764          if (i == 0) {                   /* clobber entire holodeck */
765 +                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
766 +                        return(0);              /* already empty */
767                  nchanged = 0;
768 +                nchanged = 0;
769                  for (i = nbeams(hp); i > 0; i--)
770                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
771                                  nchanged += hdkillbeam(hp, i);
772 < #ifdef DEBUG
773 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
573 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
574 < #endif
772 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
773 >                                CONSISTENCY, "bad beam count in hdkillbeam");
774                  return(nchanged);
775          }
776 < #ifdef DEBUG
777 <        if (i < 1 | i > nbeams(hp))
778 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
779 < #endif
776 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
777 >                        "bad beam index to hdkillbeam");
778 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
779 >                        "hdkillbeam called on read-only holodeck");
780          if (hp->bl[i] != NULL) {        /* free memory */
781                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
782 <                free((char *)hp->bl[i]);
782 >                free((void *)hp->bl[i]);
783 >                hp->bl[i] = NULL;
784          } else
785                  nchanged = hp->bi[i].nrd;
786 <        if (hp->bi[i].nrd) {            /* free file fragment */
787 <                hp->bl[i] = &emptybeam;
788 <                hdsyncbeam(hp, i);
786 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
787 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
788 >                hp->bi[i].nrd = 0;
789 >                hp->bi[i].fo = 0;
790 >                hdmarkdirty(hp, i);
791          }
590        hp->bl[i] = NULL;
792          return(nchanged);
793   }
794  
# Line 601 | Line 802 | register HOLO  *hp;                    /* section we're adding from */
802   {
803          register int    i, j;
804                                          /* insert each beam from hp */
805 <        for (i = nbeams(hp); i > 0; i--) {
805 >        for (i = 1; i <= nbeams(hp); i++) {
806                  if (hp->bl[i] == NULL)          /* check if loaded */
807                          continue;
808   #if 0
# Line 617 | Line 818 | register HOLO  *hp;                    /* section we're adding from */
818                                  hb[j].b = i;
819                                  break;
820                          }
821 <                        copystruct(hb+j, hb+(j-1));
821 >                        *(hb+j) = *(hb+(j-1));
822                  }
823          }
824          return(nents);                  /* return new list length */
# Line 633 | Line 834 | register HOLO  *honly;                 /* NULL means check all */
834          int     freetarget;
835          int     n;
836          register int    i;
837 + #ifdef DEBUG
838 +        unsigned        membefore;
839 +
840 +        membefore = hdmemuse(0);
841 + #endif
842                                                  /* compute free target */
843          freetarget = (honly != NULL) ? blglob(honly)->nrm :
844                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 653 | Line 859 | register HOLO  *honly;                 /* NULL means check all */
859                          break;
860          }
861          hdsync(honly, 0);       /* synchronize directories as necessary */
862 + #ifdef DEBUG
863 +        sprintf(errmsg,
864 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
865 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
866 +        wputs(errmsg);
867 + #endif
868          return(-freetarget);    /* return how far past goal we went */
869   }
870  
# Line 665 | Line 877 | register HOLO  *hp;            /* NULL means clean up all */
877          if (hp == NULL) {               /* NULL means clean up everything */
878                  while (hdlist[0] != NULL)
879                          hddone(hdlist[0]);
880 +                free((void *)hdfragl);
881 +                hdfragl = NULL; nhdfragls = 0;
882                  return;
883          }
884                                          /* flush all data and free memory */
# Line 678 | Line 892 | register HOLO  *hp;            /* NULL means clean up all */
892                                  i++;
893                          break;
894                  }
895 <        free((char *)hp->bl);           /* free beam list */
896 <        free((char *)hp);               /* free holodeck struct */
895 >        free((void *)hp->bl);           /* free beam list */
896 >        free((void *)hp);               /* free holodeck struct */
897   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines