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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines