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.27 by gwlarson, Wed Nov 4 16:44:16 1998 UTC vs.
Revision 3.59 by greg, Fri Sep 3 23:52:42 2010 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 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 + #include "rtprocess.h"
16 +
17   #include "holo.h"
18  
19   #ifndef CACHESIZE
20 < #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
20 > #ifdef SMLMEM
21 > #define CACHESIZE       10
22 > #else
23 > #define CACHESIZE       100     /* default cache size (Mbytes, 0==inf) */
24   #endif
25 + #endif
26   #ifndef FREEBEAMS
27   #define FREEBEAMS       1500    /* maximum beams to free at a time */
28   #endif
29   #ifndef PCTFREE
30   #define PCTFREE         15      /* maximum fraction to free (%) */
31   #endif
32 < #ifndef MAXFRAG
33 < #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
32 > #ifndef MAXFRAGB
33 > #define MAXFRAGB        64      /* fragment blocks/file to track (0==inf) */
34   #endif
35 + #ifndef FF_DEFAULT
36 +                                /* when to free a beam fragment */
37 + #define FF_DEFAULT      (FF_WRITE|FF_KILL)
38 + #endif
39 + #ifndef MINDIRSEL
40 +                                /* minimum directory seek length */
41 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
42 + #endif
43  
44   #ifndef BSD
45   #define write   writebuf        /* safe i/o routines */
46   #define read    readbuf
47   #endif
48  
49 < #define FRAGBLK         256     /* number of fragments to allocate at a time */
49 > #define FRAGBLK         512     /* number of fragments to allocate at a time */
50  
51 + int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */
52   unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
53 < unsigned long   hdclock;        /* clock value */
53 > unsigned long   hdclock;                /* clock value */
54  
55   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
56  
57   static struct fraglist {
58          short   nlinks;         /* number of holodeck sections using us */
59 <        short   writerr;        /* write error encountered */
59 >        short   writable;       /* 0 read-only, <0 write error encountered */
60          int     nfrags;         /* number of known fragments */
61          BEAMI   *fi;            /* fragments, descending file position */
62 <        long    flen;           /* last known file length */
62 >        off_t   flen;           /* last known file length */
63   } *hdfragl;             /* fragment lists, indexed by file descriptor */
64  
65   static int      nhdfragls;      /* size of hdfragl array */
66  
67 + static HOLO *hdalloc(HDGRID *hproto);
68 + static char *hdrealloc(char *ptr, unsigned siz, char *rout);
69 + static void hdattach(int fd, int wr);
70 + static void hdrelease(int fd);
71 + static void hdmarkdirty(HOLO *hp, int i);
72 + static unsigned int hdmemuse(int all);
73 + static int hdfilord(const void *hb1, const void *hb2);
74 + static off_t hdallocfrag(int fd, uint32 nrays);
75 + static int hdsyncbeam(HOLO *hp, int i);
76 + static int hdlrulist(HDBEAMI *hb, int nents, int n, HOLO *hp);
77 + static int hdfreecache(int pct, HOLO *honly);
78  
79 +
80 +
81 + HOLO *
82 + hdalloc(                /* allocate and set holodeck section based on grid */
83 +        HDGRID  *hproto
84 + )
85 + {
86 +        HOLO    hdhead;
87 +        register HOLO   *hp;
88 +        int     n;
89 +                                /* copy grid to temporary header */
90 +        memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID));
91 +                                /* compute grid vectors and sizes */
92 +        hdcompgrid(&hdhead);
93 +                                /* allocate header with directory */
94 +        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
95 +        if ((hp = (HOLO *)malloc(n)) == NULL)
96 +                return(NULL);
97 +                                /* copy header information */
98 +        *hp = hdhead;
99 +                                /* allocate and clear beam list */
100 +        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
101 +        if (hp->bl == NULL) {
102 +                free((void *)hp);
103 +                return(NULL);
104 +        }
105 +        memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
106 +        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
107 +        hp->fd = -1;
108 +        hp->dirty = 0;
109 +        hp->priv = NULL;
110 +                                /* clear beam directory */
111 +        memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI));
112 +        return(hp);             /* all is well */
113 + }
114 +
115 +
116   char *
117 < hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
118 < char    *ptr;
119 < unsigned        siz;
120 < char    *rout;
117 > hdrealloc(      /* (re)allocate memory, retry then error */
118 >        char    *ptr,
119 >        unsigned        siz,
120 >        char    *rout
121 > )
122   {
123          register char   *newp;
124                                          /* call malloc/realloc */
125          if (ptr == NULL) newp = (char *)malloc(siz);
126 <        else newp = (char *)realloc(ptr, siz);
126 >        else newp = (char *)realloc((void *)ptr, siz);
127                                          /* check success */
128          if (newp == NULL && rout != NULL) {
129                  hdfreecache(25, NULL);  /* free some memory */
# Line 72 | Line 138 | char   *rout;
138   }
139  
140  
141 < hdattach(fd)            /* start tracking file fragments for some section */
142 < register int    fd;
141 > void
142 > hdattach(       /* start tracking file fragments for some section */
143 >        register int    fd,
144 >        int     wr
145 > )
146   {
147          if (fd >= nhdfragls) {
148                  hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
149                                  (fd+1)*sizeof(struct fraglist), "hdattach");
150 <                bzero((char *)(hdfragl+nhdfragls),
151 <                                (fd+1-nhdfragls)*sizeof(struct fraglist));
150 >                memset((void *)(hdfragl+nhdfragls),
151 >                                '\0', (fd+1-nhdfragls)*sizeof(struct fraglist));
152                  nhdfragls = fd+1;
153          }
154          hdfragl[fd].nlinks++;
155 <        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
155 >        hdfragl[fd].writable = wr;              /* set writable flag */
156 >                                                /* get file length */
157 >        hdfragl[fd].flen = lseek(fd, (off_t)0, SEEK_END);
158   }
159  
160  
161   /* Do we need a routine to locate file fragments given known occupants? */
162  
163  
164 < hdrelease(fd)           /* stop tracking file fragments for some section */
165 < register int    fd;
164 > void
165 > hdrelease(              /* stop tracking file fragments for some section */
166 >        register int    fd
167 > )
168   {
169 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
169 >        if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks)
170                  return;
171          if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
172 <                free((char *)hdfragl[fd].fi);
172 >                free((void *)hdfragl[fd].fi);
173                  hdfragl[fd].fi = NULL;
174                  hdfragl[fd].nfrags = 0;
175          }
176   }
177  
178  
179 < markdirty(hp)                   /* mark holodeck section directory dirty */
180 < register HOLO   *hp;
179 > extern HOLO *
180 > hdinit( /* initialize a holodeck section in a file */
181 >        int     fd,                     /* corresponding file descriptor */
182 >        HDGRID  *hproto         /* holodeck section grid */
183 > )
184   {
185 <        static BEAMI    smudge = {0, -1};
186 <
187 <        if (hp->dirty)          /* already marked? */
112 <                return;
113 <        hp->dirty = 1;
114 <        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
115 <                        || write(hp->fd, (char *)&smudge,
116 <                                        sizeof(BEAMI)) != sizeof(BEAMI))
117 <                error(SYSTEM, "seek/write error in markdirty");
118 < }
119 <
120 <
121 < HOLO *
122 < hdinit(fd, hproto)      /* initialize a holodeck section in a file */
123 < int     fd;                     /* corresponding file descriptor */
124 < HDGRID  *hproto;                /* holodeck section grid */
125 < {
126 <        long    rtrunc;
127 <        long    fpos;
185 >        off_t   rtrunc;
186 >        off_t   fpos;
187 >        int     writable;
188          register HOLO   *hp;
189          register int    n;
190                                          /* prepare for system errors */
191          errno = 0;
192 <        if ((fpos = lseek(fd, 0L, 1)) < 0)
192 >        if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
193                  error(SYSTEM, "cannot determine holodeck file position");
194          if (hproto == NULL) {           /* assume we're loading it */
195                  HDGRID  hpr;
# Line 144 | Line 204 | HDGRID *hproto;                /* holodeck section grid */
204                  if (read(fd, (char *)(hp->bi+1), n) != n)
205                          error(SYSTEM, "failure loading holodeck directory");
206                                                  /* check that it's clean */
207 <                if (hp->bi[nbeams(hp)].fo < 0)
208 <                        error(WARNING, "dirty holodeck section");
209 <        } else {                        /* assume we're creating it */
207 >                for (n = nbeams(hp); n > 0; n--)
208 >                        if (hp->bi[n].fo < 0) {
209 >                                hp->bi[n].fo = 0;
210 >                                error(WARNING, "dirty holodeck section");
211 >                                break;
212 >                        }
213 >                                                /* check writability */
214 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
215 >                        writable = hdfragl[fd].writable;
216 >                else
217 >                        writable = lseek(fd, fpos, SEEK_SET) == fpos &&
218 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
219 >                                                        sizeof(HDGRID);
220 >        } else {                        /* else assume we're creating it */
221                  if ((hp = hdalloc(hproto)) == NULL)
222                          goto memerr;
223                                                  /* write header and skeleton */
# Line 155 | Line 226 | HDGRID *hproto;                /* holodeck section grid */
226                                          sizeof(HDGRID) ||
227                                  write(fd, (char *)(hp->bi+1), n) != n)
228                          error(SYSTEM, "cannot write header to holodeck file");
229 +                writable = 1;
230          }
231          hp->fd = fd;    
232          hp->dirty = 0;
233          biglob(hp)->fo = fpos + sizeof(HDGRID);
234                                          /* start tracking fragments */
235 <        hdattach(fd);
235 >        hdattach(fd, writable);
236                                          /* check rays on disk */
237          fpos = hdfilen(fd);
238          biglob(hp)->nrd = rtrunc = 0;
239          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
240 <                if (hp->bi[n].nrd)
240 >                if (hp->bi[n].nrd) {
241                          if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
242                                  rtrunc += hp->bi[n].nrd;
243                                  hp->bi[n].nrd = 0;
244                          } else
245                                  biglob(hp)->nrd += hp->bi[n].nrd;
246 +                }
247          if (rtrunc) {
248                  sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
249 <                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
249 >                                (long)rtrunc,
250 >                                100.*rtrunc/(rtrunc+biglob(hp)->nrd));
251                  error(WARNING, errmsg);
252          }
253                                          /* add to holodeck list */
# Line 186 | Line 260 | HDGRID *hproto;                /* holodeck section grid */
260          return(hp);
261   memerr:
262          error(SYSTEM, "cannot allocate holodeck grid");
263 +        return NULL; /* pro forma return */
264   }
265  
266  
267 < int
268 < hdsync(hp, all)                 /* update beams and directory on disk */
269 < register HOLO   *hp;
270 < int     all;
267 > void
268 > hdmarkdirty(            /* mark holodeck directory position dirty */
269 >        register HOLO   *hp,
270 >        int     i
271 > )
272   {
273 +        static BEAMI    smudge = {0, -1};
274 +        int     mindist, minpos;
275 +        register int    j;
276 +
277 +        if (!hp->dirty++) {                     /* write smudge first time */
278 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI),
279 +                                        SEEK_SET) < 0 ||
280 +                                write(hp->fd, (char *)&smudge,
281 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
282 +                        error(SYSTEM, "seek/write error in hdmarkdirty");
283 +                hp->dirseg[0].s = i;
284 +                hp->dirseg[0].n = 1;
285 +                return;
286 +        }
287 +                                                /* insert into segment list */
288 +        for (j = hp->dirty; j--; ) {
289 +                if (!j || hp->dirseg[j-1].s < i) {
290 +                        hp->dirseg[j].s = i;
291 +                        hp->dirseg[j].n = 1;
292 +                        break;
293 +                }
294 +                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
295 +        }
296 +        do {                            /* check neighbors */
297 +                mindist = nbeams(hp);           /* find closest */
298 +                for (j = hp->dirty; --j; )
299 +                        if (hp->dirseg[j].s -
300 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
301 +                                        < mindist) {
302 +                                minpos = j;
303 +                                mindist = hp->dirseg[j].s -
304 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
305 +                        }
306 +                                                /* good enough? */
307 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
308 +                        break;
309 +                j = minpos - 1;                 /* coalesce neighbors */
310 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
311 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
312 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
313 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
314 +                hp->dirty--;
315 +                while (++j < hp->dirty)         /* close the gap */
316 +                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
317 +        } while (mindist <= MINDIRSEL);
318 + }
319 +
320 +
321 + extern int
322 + hdsync(                 /* update beams and directory on disk */
323 +        register HOLO   *hp,
324 +        int     all
325 + )
326 + {
327          register int    j, n;
328  
329          if (hp == NULL) {               /* do all holodecks */
# Line 208 | Line 338 | int    all;
338                          hdsyncbeam(hp, j);
339          if (!hp->dirty)                 /* directory clean? */
340                  return(0);
341 <        errno = 0;
342 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
343 <                error(SYSTEM, "cannot seek on holodeck file");
344 <        n = nbeams(hp)*sizeof(BEAMI);
345 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
346 <                error(SYSTEM, "cannot update holodeck section directory");
347 <        hp->dirty = 0;
341 >        errno = 0;                      /* write dirty segments */
342 >        for (j = 0; j < hp->dirty; j++) {
343 >                if (lseek(hp->fd, biglob(hp)->fo +
344 >                            (hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0)
345 >                        error(SYSTEM, "cannot seek on holodeck file");
346 >                n = hp->dirseg[j].n * sizeof(BEAMI);
347 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
348 >                        error(SYSTEM, "cannot update section directory");
349 >        }
350 >        hp->dirty = 0;                  /* all clean */
351          return(1);
352   }
353  
354  
355 < unsigned
356 < hdmemuse(all)           /* return memory usage (in bytes) */
357 < int     all;                    /* include overhead (painful) */
355 > unsigned int
356 > hdmemuse(               /* return memory usage (in bytes) */
357 >        int     all                     /* include overhead (painful) */
358 > )
359   {
360          long    total = 0;
361          register int    i, j;
# Line 248 | Line 382 | int    all;                    /* include overhead (painful) */
382   }
383  
384  
385 < long
386 < hdfilen(fd)             /* return file length for fd */
387 < int     fd;
385 > extern off_t
386 > hdfilen(                /* return file length for fd */
387 >        int     fd
388 > )
389   {
390 <        long    fpos, flen;
390 >        off_t   fpos, flen;
391  
392          if (fd < 0)
393                  return(-1);
394          if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
395 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
395 >                if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
396                          return(-1);
397 <                flen = lseek(fd, 0L, 2);
398 <                lseek(fd, fpos, 0);
397 >                flen = lseek(fd, (off_t)0, SEEK_END);
398 >                lseek(fd, fpos, SEEK_SET);
399                  return(flen);
400          }
401          return(hdfragl[fd].flen);
402   }
403  
404  
405 < long
406 < hdfiluse(fd, all)       /* compute file usage (in bytes) */
407 < int     fd;                     /* open file descriptor to check */
408 < int     all;                    /* include overhead and unflushed data */
405 > extern off_t
406 > hdfiluse(       /* compute file usage (in bytes) */
407 >        int     fd                      /* open file descriptor to check */
408 > )
409   {
410 <        long    total = 0;
411 <        register int    i, j;
410 >        off_t   total = 0;
411 >        register int    j;
412  
413          for (j = 0; hdlist[j] != NULL; j++) {
414                  if (hdlist[j]->fd != fd)
415                          continue;
416                  total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
417 <                if (all) {
418 <                        for (i = nbeams(hdlist[j]); i > 0; i--)
419 <                                if (hdlist[j]->bl[i] != NULL)
420 <                                        total += sizeof(RAYVAL) *
417 >                total += nbeams(hdlist[j])*sizeof(BEAMI) + sizeof(HDGRID);
418 > #if 0
419 >                for (i = nbeams(hdlist[j]); i > 0; i--)
420 >                        if (hdlist[j]->bl[i] != NULL)
421 >                                total += sizeof(RAYVAL) *
422                                                  (hdlist[j]->bl[i]->nrm -
423                                                  hdlist[j]->bi[i].nrd);
424 <                        total += sizeof(HDGRID) +
289 <                                        nbeams(hdlist[j])*sizeof(BEAMI);
290 <                }
424 > #endif
425          }
426 <        return(total);          /* does not include fragments */
426 >        return(total);          /* doesn't include fragments, unflushed rays */
427   }
428  
429  
430 < RAYVAL *
431 < hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */
432 < register HOLO   *hp;
433 < register int    i;
434 < int     nr;                     /* number of new rays desired */
430 > extern RAYVAL *
431 > hdnewrays(      /* allocate space for add'l rays and return pointer */
432 >        register HOLO   *hp,
433 >        register int    i,
434 >        int     nr                      /* number of new rays desired */
435 > )
436   {
437          RAYVAL  *p;
438          int     n;
439  
440 <        if (nr <= 0)
441 <                return(NULL);
442 <        if (i < 1 | i > nbeams(hp))
308 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
440 >        if (nr <= 0) return(NULL);
441 >        CHECK((i < 1) | (i > nbeams(hp)),
442 >                        CONSISTENCY, "bad beam index given to hdnewrays");
443          if (hp->bl[i] != NULL)
444                  hp->bl[i]->tick = hdclock;      /* preempt swap */
445          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
# Line 314 | Line 448 | int    nr;                     /* number of new rays desired */
448                  n = hp->bi[i].nrd + nr;
449                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
450                  blglob(hp)->nrm += n;
451 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
451 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
452                          errno = 0;
453 <                        if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
453 >                        if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
454                                  error(SYSTEM, "seek error on holodeck file");
455                          n *= sizeof(RAYVAL);
456                          if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 328 | Line 462 | int    nr;                     /* number of new rays desired */
462                                  hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
463                  blglob(hp)->nrm += nr;
464          }
465 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
466 +                hdfreefrag(hp, i);              /* relinquish old fragment */
467          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
468          hp->bl[i]->nrm += nr;                   /* update in-core structure */
469 <        bzero((char *)p, nr*sizeof(RAYVAL));
469 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
470          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
471          return(p);                              /* point to new rays */
472   }
473  
474  
475 < BEAM *
476 < hdgetbeam(hp, i)        /* get beam (from file if necessary) */
477 < register HOLO   *hp;
478 < register int    i;
475 > extern BEAM *
476 > hdgetbeam(      /* get beam (from file if necessary) */
477 >        register HOLO   *hp,
478 >        register int    i
479 > )
480   {
481          register int    n;
482  
483 <        if (i < 1 | i > nbeams(hp))
484 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
483 >        CHECK((i < 1) | (i > nbeams(hp)),
484 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
485          if (hp->bl[i] == NULL) {                /* load from disk */
486                  if (!(n = hp->bi[i].nrd))
487                          return(NULL);
# Line 353 | Line 490 | register int   i;
490                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
491                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
492                  errno = 0;
493 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
493 >                if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
494                          error(SYSTEM, "seek error on holodeck file");
495                  n *= sizeof(RAYVAL);
496                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
497                          error(SYSTEM, "error reading beam from holodeck file");
498 +                if (hdfragflags&FF_READ)
499 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
500          }
501          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
502          return(hp->bl[i]);
# Line 365 | Line 504 | register int   i;
504  
505  
506   int
507 < hdfilord(hb1, hb2)      /* order beams for quick loading */
508 < register HDBEAMI        *hb1, *hb2;
507 > hdfilord(       /* order beams for quick loading */
508 >        register const void     *hb1,
509 >        register const void     *hb2
510 > )
511   {
512 <        register long   c;
512 >        register off_t  c;
513                                  /* residents go first */
514 <        if (hb2->h->bl[hb2->b] != NULL)
515 <                return(hb1->h->bl[hb1->b] == NULL);
516 <        if (hb1->h->bl[hb1->b] != NULL)
514 >        if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL)
515 >                return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL);
516 >        if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL)
517                  return(-1);
518                                  /* otherwise sort by file descriptor */
519 <        if ((c = hb1->h->fd - hb2->h->fd))
520 <                return(c);
519 >        if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd)
520 >                return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd);
521                                  /* then by position in file */
522 <        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
522 >        c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo
523 >                - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo;
524          return(c > 0 ? 1 : c < 0 ? -1 : 0);
525   }
526  
527  
528 < hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
529 < register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
530 < int     n;                      /* list length */
531 < int     (*bf)();                /* callback function (optional) */
528 > extern void
529 > hdloadbeams(    /* load a list of beams in optimal order */
530 >        register HDBEAMI        *hb,    /* list gets sorted by hdfilord() */
531 >        int     n,                      /* list length */
532 >        void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */
533 > )
534   {
535          unsigned        origcachesize, memuse;
536          int     bytesloaded, needbytes, bytes2free;
# Line 395 | Line 539 | int    (*bf)();                /* callback function (optional) */
539                                          /* precheck consistency */
540          if (n <= 0) return;
541          for (i = n; i--; )
542 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
542 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
543                          error(CONSISTENCY, "bad beam in hdloadbeams");
544                                          /* sort list for optimal access */
545 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
545 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
546          bytesloaded = 0;                /* run through loaded beams */
547          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
548                  bp->tick = hdclock;     /* preempt swap */
# Line 428 | Line 572 | int    (*bf)();                /* callback function (optional) */
572   }
573  
574  
575 < hdfreefrag(fd, bi)                      /* free a file fragment */
576 < int     fd;
577 < register BEAMI  *bi;
575 > extern int
576 > hdfreefrag(                     /* free a file fragment */
577 >        HOLO    *hp,
578 >        int     i
579 > )
580   {
581 +        register BEAMI  *bi = &hp->bi[i];
582          register struct fraglist        *f;
583          register int    j, k;
584  
585 <        if (bi->nrd == 0)
586 <                return;
587 < #ifdef DEBUG
588 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
589 <                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
590 < #endif
591 <        f = &hdfragl[fd];
585 >        if (bi->nrd <= 0)
586 >                return(0);
587 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
588 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
589 >        f = &hdfragl[hp->fd];
590 >        if (!f->writable)
591 >                return(0);
592          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
593                  for (j = k = 0; k < f->nfrags; j++, k++) {
594                          while (f->fi[k].nrd == 0)
595                                  if (++k >= f->nfrags)
596                                          goto endloop;
597                          if (k > j)
598 <                                copystruct(f->fi+j, f->fi+k);
598 >                                *(f->fi+j) = *(f->fi+k);
599                  }
600          endloop:
601                  f->nfrags = j;
602          }
603          j = f->nfrags++;                /* allocate a slot in free list */
604 < #if MAXFRAG
605 <        if (j >= MAXFRAG-1)
606 <                f->nfrags--;
604 > #if MAXFRAGB
605 >        if (j >= MAXFRAGB*FRAGBLK) {
606 >                f->nfrags = j--;        /* stop list growth */
607 >                if (bi->nrd <= f->fi[j].nrd)
608 >                        return(0);      /* new one no better than discard */
609 >        }
610   #endif
611 <        if (j % FRAGBLK == 0) {         /* more free list space */
611 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
612                  register BEAMI  *newp;
613                  if (f->fi == NULL)
614                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
615                  else
616 <                        newp = (BEAMI *)realloc((char *)f->fi,
616 >                        newp = (BEAMI *)realloc((void *)f->fi,
617                                          (j+FRAGBLK)*sizeof(BEAMI));
618                  if (newp == NULL) {
619                          f->nfrags--;    /* graceful failure */
620 <                        return;
620 >                        return(0);
621                  }
622                  f->fi = newp;
623          }
# Line 477 | Line 627 | register BEAMI *bi;
627                          f->fi[j].nrd = bi->nrd;
628                          break;
629                  }
630 <                copystruct(f->fi+j, f->fi+(j-1));
630 >                *(f->fi+j) = *(f->fi+(j-1));
631          }
632                                          /* coalesce adjacent fragments */
633                                                  /* successors never empty */
# Line 494 | Line 644 | register BEAMI *bi;
644                          }
645                          break;
646                  }
647 +        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
648 +        bi->nrd = 0;
649 +        bi->fo = 0;
650 +        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
651 +        return(1);
652   }
653  
654  
655 < long
656 < hdallocfrag(fd, nrays)          /* allocate a file fragment */
657 < int     fd;
658 < unsigned int4   nrays;
655 > extern int
656 > hdfragOK(       /* get fragment list status for file */
657 >        int     fd,
658 >        int     *listlen,
659 >        register int32  *listsiz
660 > )
661   {
662          register struct fraglist        *f;
663 <        register int    j, k;
507 <        long    nfo;
663 >        register int    i;
664  
665 +        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
666 +                return(0);              /* listless */
667 +        if (listlen != NULL)
668 +                *listlen = f->nfrags;
669 +        if (listsiz != NULL)
670 +                for (i = f->nfrags, *listsiz = 0; i--; )
671 +                        *listsiz += f->fi[i].nrd;
672 + #if MAXFRAGB
673 +        return(f->nfrags < MAXFRAGB*FRAGBLK);
674 + #else
675 +        return(1);                      /* list never fills up */
676 + #endif
677 + }
678 +
679 +
680 + off_t
681 + hdallocfrag(            /* allocate a file fragment */
682 +        int     fd,
683 +        uint32  nrays
684 + )
685 + {
686 +        register struct fraglist        *f;
687 +        register int    j;
688 +        off_t   nfo;
689 +
690          if (nrays == 0)
691                  return(-1L);
692 < #ifdef DEBUG
693 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
513 <                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
514 < #endif
692 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
693 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
694          f = &hdfragl[fd];
695 <        k = -1;                         /* find closest-sized fragment */
696 <        for (j = f->nfrags; j-- > 0; )
697 <                if (f->fi[j].nrd >= nrays &&
698 <                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
520 <                        if (f->fi[k=j].nrd == nrays)
521 <                                break;
522 <        if (k < 0) {                    /* no fragment -- extend file */
695 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
696 >                if (f->fi[j].nrd >= nrays)
697 >                        break;
698 >        if (j < 0) {                    /* no fragment -- extend file */
699                  nfo = f->flen;
700                  f->flen += nrays*sizeof(RAYVAL);
701          } else {                        /* else use fragment */
702 <                nfo = f->fi[k].fo;
703 <                f->fi[k].fo += nrays*sizeof(RAYVAL);
704 <                f->fi[k].nrd -= nrays;
702 >                nfo = f->fi[j].fo;
703 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
704 >                f->fi[j].nrd -= nrays;
705          }
706          return(nfo);
707   }
708  
709  
710   int
711 < hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
712 < register HOLO   *hp;
713 < register int    i;
711 > hdsyncbeam(             /* sync beam in memory with beam on disk */
712 >        register HOLO   *hp,
713 >        register int    i
714 > )
715   {
716 <        unsigned int4   nrays;
716 >        int     fragfreed;
717 >        uint32  nrays;
718          unsigned int    n;
719 <        long    nfo;
719 >        off_t   nfo;
720                                          /* check file status */
721 <        if (hdfragl[hp->fd].writerr)
722 <                return(-1);
723 < #ifdef DEBUG
724 <        if (i < 1 | i > nbeams(hp))
547 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
548 < #endif
721 >        if (hdfragl[hp->fd].writable <= 0)
722 >                return(hdfragl[hp->fd].writable);
723 >        DCHECK(i < 1 | i > nbeams(hp),
724 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
725                                          /* is current fragment OK? */
726          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
727                  return(0);
728 <        if (hp->bi[i].nrd)              /* relinquish old fragment */
729 <                hdfreefrag(hp->fd, &hp->bi[i]);
728 >                                        /* relinquish old fragment? */
729 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
730          if (nrays) {                    /* get and write new fragment */
731                  nfo = hdallocfrag(hp->fd, nrays);
732                  errno = 0;
733 <                if (lseek(hp->fd, nfo, 0) < 0)
733 >                if (lseek(hp->fd, nfo, SEEK_SET) < 0)
734                          error(SYSTEM, "cannot seek on holodeck file");
735                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
736                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
737 <                        hdfragl[hp->fd].writerr++;
737 >                        hdfragl[hp->fd].writable = -1;
738                          hdsync(NULL, 0);        /* sync directories */
739                          error(SYSTEM, "write error in hdsyncbeam");
740                  }
741                  hp->bi[i].fo = nfo;
742          } else
743 <                hp->bi[i].fo = 0L;
743 >                hp->bi[i].fo = 0;
744          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
745          hp->bi[i].nrd = nrays;
746 <        markdirty(hp);                  /* section directory now out of date */
746 >        if (!fragfreed)
747 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
748          return(1);
749   }
750  
751  
752 < int
753 < hdfreebeam(hp, i)               /* free beam, writing if dirty */
754 < register HOLO   *hp;
755 < register int    i;
752 > extern int
753 > hdfreebeam(             /* free beam, writing if dirty */
754 >        register HOLO   *hp,
755 >        register int    i
756 > )
757   {
758          int     nchanged;
759  
# Line 585 | Line 763 | register int   i;
763                          nchanged += hdfreebeam(hdlist[i], 0);
764                  return(nchanged);
765          }
766 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
766 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
767                  return(0);
768          if (i == 0) {                   /* clear entire holodeck */
769 +                if (blglob(hp)->nrm == 0)
770 +                        return(0);              /* already clear */
771                  nchanged = 0;
772                  for (i = nbeams(hp); i > 0; i--)
773                          if (hp->bl[i] != NULL)
774                                  nchanged += hdfreebeam(hp, i);
775 +                DCHECK(blglob(hp)->nrm != 0,
776 +                                CONSISTENCY, "bad beam count in hdfreebeam");
777                  return(nchanged);
778          }
779 < #ifdef DEBUG
780 <        if (i < 1 | i > nbeams(hp))
599 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
600 < #endif
779 >        DCHECK(i < 1 | i > nbeams(hp),
780 >                        CONSISTENCY, "bad beam index to hdfreebeam");
781          if (hp->bl[i] == NULL)
782                  return(0);
783                                          /* check for additions */
# Line 605 | Line 785 | register int   i;
785          if (nchanged)
786                  hdsyncbeam(hp, i);              /* write new fragment */
787          blglob(hp)->nrm -= hp->bl[i]->nrm;
788 <        free((char *)hp->bl[i]);                /* free memory */
788 >        free((void *)hp->bl[i]);                /* free memory */
789          hp->bl[i] = NULL;
790          return(nchanged);
791   }
792  
793  
794 < int
795 < hdkillbeam(hp, i)               /* delete beam from holodeck */
796 < register HOLO   *hp;
797 < register int    i;
794 > extern int
795 > hdkillbeam(             /* delete beam from holodeck */
796 >        register HOLO   *hp,
797 >        register int    i
798 > )
799   {
619        static BEAM     emptybeam;
800          int     nchanged;
801  
802          if (hp == NULL) {               /* clobber all holodecks */
# Line 626 | Line 806 | register int   i;
806                  return(nchanged);
807          }
808          if (i == 0) {                   /* clobber entire holodeck */
809 +                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
810 +                        return(0);              /* already empty */
811                  nchanged = 0;
812 +                nchanged = 0;
813                  for (i = nbeams(hp); i > 0; i--)
814                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
815                                  nchanged += hdkillbeam(hp, i);
816 < #ifdef DEBUG
817 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
635 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
636 < #endif
816 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
817 >                                CONSISTENCY, "bad beam count in hdkillbeam");
818                  return(nchanged);
819          }
820 < #ifdef DEBUG
821 <        if (i < 1 | i > nbeams(hp))
822 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
823 < #endif
820 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
821 >                        "bad beam index to hdkillbeam");
822 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
823 >                        "hdkillbeam called on read-only holodeck");
824          if (hp->bl[i] != NULL) {        /* free memory */
825                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
826 <                free((char *)hp->bl[i]);
826 >                free((void *)hp->bl[i]);
827 >                hp->bl[i] = NULL;
828          } else
829                  nchanged = hp->bi[i].nrd;
830 <        if (hp->bi[i].nrd) {            /* free file fragment */
831 <                hp->bl[i] = &emptybeam;
832 <                hdsyncbeam(hp, i);
830 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
831 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
832 >                hp->bi[i].nrd = 0;
833 >                hp->bi[i].fo = 0;
834 >                hdmarkdirty(hp, i);
835          }
652        hp->bl[i] = NULL;
836          return(nchanged);
837   }
838  
839  
840   int
841 < hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
842 < register HDBEAMI        *hb;            /* beam list */
843 < int     nents;                          /* current list length */
844 < int     n;                              /* maximum list length */
845 < register HOLO   *hp;                    /* section we're adding from */
841 > hdlrulist(      /* add beams from holodeck to LRU list */
842 >        register HDBEAMI        *hb,            /* beam list */
843 >        int     nents,                          /* current list length */
844 >        int     n,                              /* maximum list length */
845 >        register HOLO   *hp                     /* section we're adding from */
846 > )
847   {
848          register int    i, j;
849                                          /* insert each beam from hp */
# Line 679 | Line 863 | register HOLO  *hp;                    /* section we're adding from */
863                                  hb[j].b = i;
864                                  break;
865                          }
866 <                        copystruct(hb+j, hb+(j-1));
866 >                        *(hb+j) = *(hb+(j-1));
867                  }
868          }
869          return(nents);                  /* return new list length */
# Line 687 | Line 871 | register HOLO  *hp;                    /* section we're adding from */
871  
872  
873   int
874 < hdfreecache(pct, honly)         /* free up cache space, writing changes */
875 < int     pct;                            /* maximum percentage to free */
876 < register HOLO   *honly;                 /* NULL means check all */
874 > hdfreecache(            /* free up cache space, writing changes */
875 >        int     pct,                            /* maximum percentage to free */
876 >        register HOLO   *honly                  /* NULL means check all */
877 > )
878   {
879          HDBEAMI hb[FREEBEAMS];
880          int     freetarget;
# Line 730 | Line 915 | register HOLO  *honly;                 /* NULL means check all */
915   }
916  
917  
918 < hddone(hp)              /* clean up holodeck section and free */
919 < register HOLO   *hp;            /* NULL means clean up all */
918 > extern void
919 > hddone(         /* clean up holodeck section and free */
920 >        register HOLO   *hp             /* NULL means clean up all */
921 > )
922   {
923          register int    i;
924  
925          if (hp == NULL) {               /* NULL means clean up everything */
926                  while (hdlist[0] != NULL)
927                          hddone(hdlist[0]);
928 <                free((char *)hdfragl);
928 >                free((void *)hdfragl);
929                  hdfragl = NULL; nhdfragls = 0;
930                  return;
931          }
932                                          /* flush all data and free memory */
933 <        hdfreebeam(hp, 0);
747 <        hdsync(hp, 0);
933 >        hdflush(hp);
934                                          /* release fragment resources */
935          hdrelease(hp->fd);
936                                          /* remove hp from active list */
# Line 754 | Line 940 | register HOLO  *hp;            /* NULL means clean up all */
940                                  i++;
941                          break;
942                  }
943 <        free((char *)hp->bl);           /* free beam list */
944 <        free((char *)hp);               /* free holodeck struct */
943 >        free((void *)hp->bl);           /* free beam list */
944 >        free((void *)hp);               /* free holodeck struct */
945   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines