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.37 by gwlarson, Wed Feb 10 17:09:20 1999 UTC vs.
Revision 3.61 by greg, Fri Oct 5 19:19:16 2018 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1999 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 < #ifdef BIGMEM
21 < #define CACHESIZE       32      /* default cache size (Mbytes, 0==inf) */
20 > #ifdef SMLMEM
21 > #define CACHESIZE       10
22   #else
23 < #define CACHESIZE       12
23 > #define CACHESIZE       100     /* default cache size (Mbytes, 0==inf) */
24   #endif
25   #endif
26   #ifndef FREEBEAMS
# Line 26 | Line 30 | static char SCCSid[] = "$SunId$ SGI";
30   #define PCTFREE         15      /* maximum fraction to free (%) */
31   #endif
32   #ifndef MAXFRAGB
33 < #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */
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_ALLOC|FF_WRITE|FF_KILL)
37 > #define FF_DEFAULT      (FF_WRITE|FF_KILL)
38   #endif
39   #ifndef MINDIRSEL
40                                  /* minimum directory seek length */
# Line 38 | Line 42 | static char SCCSid[] = "$SunId$ SGI";
42   #endif
43  
44   #ifndef BSD
45 + #ifdef write /* platform.h renames those for Windows */
46 + #undef write
47 + #endif
48   #define write   writebuf        /* safe i/o routines */
49 + #ifdef read
50 + #undef read
51 + #endif
52   #define read    readbuf
53   #endif
54  
# Line 52 | Line 62 | HOLO   *hdlist[HDMAX+1];       /* holodeck pointers (NULL term
62  
63   static struct fraglist {
64          short   nlinks;         /* number of holodeck sections using us */
65 <        short   writerr;        /* write error encountered */
65 >        short   writable;       /* 0 read-only, <0 write error encountered */
66          int     nfrags;         /* number of known fragments */
67          BEAMI   *fi;            /* fragments, descending file position */
68 <        long    flen;           /* last known file length */
68 >        off_t   flen;           /* last known file length */
69   } *hdfragl;             /* fragment lists, indexed by file descriptor */
70  
71   static int      nhdfragls;      /* size of hdfragl array */
72  
73 + static HOLO *hdalloc(HDGRID *hproto);
74 + static char *hdrealloc(char *ptr, unsigned siz, char *rout);
75 + static void hdattach(int fd, int wr);
76 + static void hdrelease(int fd);
77 + static void hdmarkdirty(HOLO *hp, int i);
78 + static unsigned int hdmemuse(int all);
79 + static int hdfilord(const void *hb1, const void *hb2);
80 + static off_t hdallocfrag(int fd, uint32 nrays);
81 + static int hdsyncbeam(HOLO *hp, int i);
82 + static int hdlrulist(HDBEAMI *hb, int nents, int n, HOLO *hp);
83 + static int hdfreecache(int pct, HOLO *honly);
84  
85 +
86 +
87 + HOLO *
88 + hdalloc(                /* allocate and set holodeck section based on grid */
89 +        HDGRID  *hproto
90 + )
91 + {
92 +        HOLO    hdhead;
93 +        HOLO    *hp;
94 +        int     n;
95 +                                /* copy grid to temporary header */
96 +        memcpy((void *)&hdhead, (void *)hproto, sizeof(HDGRID));
97 +                                /* compute grid vectors and sizes */
98 +        hdcompgrid(&hdhead);
99 +                                /* allocate header with directory */
100 +        n = sizeof(HOLO)+nbeams(&hdhead)*sizeof(BEAMI);
101 +        if ((hp = (HOLO *)malloc(n)) == NULL)
102 +                return(NULL);
103 +                                /* copy header information */
104 +        *hp = hdhead;
105 +                                /* allocate and clear beam list */
106 +        hp->bl = (BEAM **)malloc((nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
107 +        if (hp->bl == NULL) {
108 +                free((void *)hp);
109 +                return(NULL);
110 +        }
111 +        memset((void *)hp->bl, '\0', (nbeams(hp)+1)*sizeof(BEAM *)+sizeof(BEAM));
112 +        hp->bl[0] = (BEAM *)(hp->bl+nbeams(hp)+1);      /* set blglob(hp) */
113 +        hp->fd = -1;
114 +        hp->dirty = 0;
115 +        hp->priv = NULL;
116 +                                /* clear beam directory */
117 +        memset((void *)hp->bi, '\0', (nbeams(hp)+1)*sizeof(BEAMI));
118 +        return(hp);             /* all is well */
119 + }
120 +
121 +
122   char *
123 < hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
124 < char    *ptr;
125 < unsigned        siz;
126 < char    *rout;
123 > hdrealloc(      /* (re)allocate memory, retry then error */
124 >        char    *ptr,
125 >        unsigned        siz,
126 >        char    *rout
127 > )
128   {
129 <        register char   *newp;
129 >        char    *newp;
130                                          /* call malloc/realloc */
131          if (ptr == NULL) newp = (char *)malloc(siz);
132 <        else newp = (char *)realloc(ptr, siz);
132 >        else newp = (char *)realloc((void *)ptr, siz);
133                                          /* check success */
134          if (newp == NULL && rout != NULL) {
135                  hdfreecache(25, NULL);  /* free some memory */
# Line 85 | Line 144 | char   *rout;
144   }
145  
146  
147 < hdattach(fd)            /* start tracking file fragments for some section */
148 < register int    fd;
147 > void
148 > hdattach(       /* start tracking file fragments for some section */
149 >        int     fd,
150 >        int     wr
151 > )
152   {
153          if (fd >= nhdfragls) {
154                  hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
155                                  (fd+1)*sizeof(struct fraglist), "hdattach");
156 <                bzero((char *)(hdfragl+nhdfragls),
157 <                                (fd+1-nhdfragls)*sizeof(struct fraglist));
156 >                memset((void *)(hdfragl+nhdfragls),
157 >                                '\0', (fd+1-nhdfragls)*sizeof(struct fraglist));
158                  nhdfragls = fd+1;
159          }
160          hdfragl[fd].nlinks++;
161 <        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
161 >        hdfragl[fd].writable = wr;              /* set writable flag */
162 >                                                /* get file length */
163 >        hdfragl[fd].flen = lseek(fd, (off_t)0, SEEK_END);
164   }
165  
166  
167   /* Do we need a routine to locate file fragments given known occupants? */
168  
169  
170 < hdrelease(fd)           /* stop tracking file fragments for some section */
171 < register int    fd;
170 > void
171 > hdrelease(              /* stop tracking file fragments for some section */
172 >        int     fd
173 > )
174   {
175 <        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
175 >        if ((fd < 0) | (fd >= nhdfragls) || !hdfragl[fd].nlinks)
176                  return;
177          if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
178 <                free((char *)hdfragl[fd].fi);
178 >                free((void *)hdfragl[fd].fi);
179                  hdfragl[fd].fi = NULL;
180                  hdfragl[fd].nfrags = 0;
181          }
# Line 117 | Line 183 | register int   fd;
183  
184  
185   HOLO *
186 < hdinit(fd, hproto)      /* initialize a holodeck section in a file */
187 < int     fd;                     /* corresponding file descriptor */
188 < HDGRID  *hproto;                /* holodeck section grid */
186 > hdinit( /* initialize a holodeck section in a file */
187 >        int     fd,                     /* corresponding file descriptor */
188 >        HDGRID  *hproto         /* holodeck section grid */
189 > )
190   {
191 <        long    rtrunc;
192 <        long    fpos;
193 <        register HOLO   *hp;
194 <        register int    n;
191 >        off_t   rtrunc;
192 >        off_t   fpos;
193 >        int     writable;
194 >        HOLO    *hp;
195 >        int     n;
196                                          /* prepare for system errors */
197          errno = 0;
198 <        if ((fpos = lseek(fd, 0L, 1)) < 0)
198 >        if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
199                  error(SYSTEM, "cannot determine holodeck file position");
200          if (hproto == NULL) {           /* assume we're loading it */
201                  HDGRID  hpr;
# Line 148 | Line 216 | HDGRID *hproto;                /* holodeck section grid */
216                                  error(WARNING, "dirty holodeck section");
217                                  break;
218                          }
219 <        } else {                        /* assume we're creating it */
219 >                                                /* check writability */
220 >                if (fd < nhdfragls && hdfragl[fd].nlinks)
221 >                        writable = hdfragl[fd].writable;
222 >                else
223 >                        writable = lseek(fd, fpos, SEEK_SET) == fpos &&
224 >                                write(fd, (char *)hp, sizeof(HDGRID)) ==
225 >                                                        sizeof(HDGRID);
226 >        } else {                        /* else assume we're creating it */
227                  if ((hp = hdalloc(hproto)) == NULL)
228                          goto memerr;
229                                                  /* write header and skeleton */
# Line 157 | Line 232 | HDGRID *hproto;                /* holodeck section grid */
232                                          sizeof(HDGRID) ||
233                                  write(fd, (char *)(hp->bi+1), n) != n)
234                          error(SYSTEM, "cannot write header to holodeck file");
235 +                writable = 1;
236          }
237          hp->fd = fd;    
238          hp->dirty = 0;
239          biglob(hp)->fo = fpos + sizeof(HDGRID);
240                                          /* start tracking fragments */
241 <        hdattach(fd);
241 >        hdattach(fd, writable);
242                                          /* check rays on disk */
243          fpos = hdfilen(fd);
244          biglob(hp)->nrd = rtrunc = 0;
245          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
246 <                if (hp->bi[n].nrd)
246 >                if (hp->bi[n].nrd) {
247                          if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
248                                  rtrunc += hp->bi[n].nrd;
249                                  hp->bi[n].nrd = 0;
250                          } else
251                                  biglob(hp)->nrd += hp->bi[n].nrd;
252 +                }
253          if (rtrunc) {
254                  sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
255 <                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
255 >                                (long)rtrunc,
256 >                                100.*rtrunc/(rtrunc+biglob(hp)->nrd));
257                  error(WARNING, errmsg);
258          }
259                                          /* add to holodeck list */
# Line 188 | Line 266 | HDGRID *hproto;                /* holodeck section grid */
266          return(hp);
267   memerr:
268          error(SYSTEM, "cannot allocate holodeck grid");
269 +        return NULL; /* pro forma return */
270   }
271  
272  
273 < hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
274 < register HOLO   *hp;
275 < int     i;
273 > void
274 > hdmarkdirty(            /* mark holodeck directory position dirty */
275 >        HOLO    *hp,
276 >        int     i
277 > )
278   {
279          static BEAMI    smudge = {0, -1};
280          int     mindist, minpos;
281 <        register int    j;
281 >        int     j;
282  
283          if (!hp->dirty++) {                     /* write smudge first time */
284 <                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
285 <                                || write(hp->fd, (char *)&smudge,
284 >                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI),
285 >                                        SEEK_SET) < 0 ||
286 >                                write(hp->fd, (char *)&smudge,
287                                          sizeof(BEAMI)) != sizeof(BEAMI))
288                          error(SYSTEM, "seek/write error in hdmarkdirty");
289                  hp->dirseg[0].s = i;
# Line 215 | Line 297 | int    i;
297                          hp->dirseg[j].n = 1;
298                          break;
299                  }
300 <                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
300 >                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
301          }
302          do {                            /* check neighbors */
303                  mindist = nbeams(hp);           /* find closest */
# Line 237 | Line 319 | int    i;
319                                          hp->dirseg[minpos].n - hp->dirseg[j].s;
320                  hp->dirty--;
321                  while (++j < hp->dirty)         /* close the gap */
322 <                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
322 >                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
323          } while (mindist <= MINDIRSEL);
324   }
325  
326  
327   int
328 < hdsync(hp, all)                 /* update beams and directory on disk */
329 < register HOLO   *hp;
330 < int     all;
328 > hdsync(                 /* update beams and directory on disk */
329 >        HOLO    *hp,
330 >        int     all
331 > )
332   {
333 <        register int    j, n;
333 >        int     j, n;
334  
335          if (hp == NULL) {               /* do all holodecks */
336                  n = 0;
# Line 264 | Line 347 | int    all;
347          errno = 0;                      /* write dirty segments */
348          for (j = 0; j < hp->dirty; j++) {
349                  if (lseek(hp->fd, biglob(hp)->fo +
350 <                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
350 >                            (hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0)
351                          error(SYSTEM, "cannot seek on holodeck file");
352                  n = hp->dirseg[j].n * sizeof(BEAMI);
353                  if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
# Line 275 | Line 358 | int    all;
358   }
359  
360  
361 < unsigned
362 < hdmemuse(all)           /* return memory usage (in bytes) */
363 < int     all;                    /* include overhead (painful) */
361 > unsigned int
362 > hdmemuse(               /* return memory usage (in bytes) */
363 >        int     all                     /* include overhead (painful) */
364 > )
365   {
366          long    total = 0;
367 <        register int    i, j;
367 >        int     i, j;
368  
369          for (j = 0; hdlist[j] != NULL; j++) {
370                  total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
# Line 304 | Line 388 | int    all;                    /* include overhead (painful) */
388   }
389  
390  
391 < long
392 < hdfilen(fd)             /* return file length for fd */
393 < int     fd;
391 > off_t
392 > hdfilen(                /* return file length for fd */
393 >        int     fd
394 > )
395   {
396 <        long    fpos, flen;
396 >        off_t   fpos, flen;
397  
398          if (fd < 0)
399                  return(-1);
400          if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
401 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
401 >                if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
402                          return(-1);
403 <                flen = lseek(fd, 0L, 2);
404 <                lseek(fd, fpos, 0);
403 >                flen = lseek(fd, (off_t)0, SEEK_END);
404 >                lseek(fd, fpos, SEEK_SET);
405                  return(flen);
406          }
407          return(hdfragl[fd].flen);
408   }
409  
410  
411 < long
412 < hdfiluse(fd, all)       /* compute file usage (in bytes) */
413 < int     fd;                     /* open file descriptor to check */
414 < int     all;                    /* include overhead and unflushed data */
411 > off_t
412 > hdfiluse(       /* compute file usage (in bytes) */
413 >        int     fd                      /* open file descriptor to check */
414 > )
415   {
416 <        long    total = 0;
417 <        register int    i, j;
416 >        off_t   total = 0;
417 >        int     j;
418  
419          for (j = 0; hdlist[j] != NULL; j++) {
420                  if (hdlist[j]->fd != fd)
421                          continue;
422                  total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
423 <                if (all) {
424 <                        for (i = nbeams(hdlist[j]); i > 0; i--)
425 <                                if (hdlist[j]->bl[i] != NULL)
426 <                                        total += sizeof(RAYVAL) *
423 >                total += nbeams(hdlist[j])*sizeof(BEAMI) + sizeof(HDGRID);
424 > #if 0
425 >                for (i = nbeams(hdlist[j]); i > 0; i--)
426 >                        if (hdlist[j]->bl[i] != NULL)
427 >                                total += sizeof(RAYVAL) *
428                                                  (hdlist[j]->bl[i]->nrm -
429                                                  hdlist[j]->bi[i].nrd);
430 <                        total += sizeof(HDGRID) +
345 <                                        nbeams(hdlist[j])*sizeof(BEAMI);
346 <                }
430 > #endif
431          }
432 <        return(total);          /* does not include fragments */
432 >        return(total);          /* doesn't include fragments, unflushed rays */
433   }
434  
435  
436   RAYVAL *
437 < hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */
438 < register HOLO   *hp;
439 < register int    i;
440 < int     nr;                     /* number of new rays desired */
437 > hdnewrays(      /* allocate space for add'l rays and return pointer */
438 >        HOLO    *hp,
439 >        int     i,
440 >        int     nr                      /* number of new rays desired */
441 > )
442   {
443          RAYVAL  *p;
444          int     n;
445  
446          if (nr <= 0) return(NULL);
447 <        CHECK(i < 1 | i > nbeams(hp),
447 >        CHECK((i < 1) | (i > nbeams(hp)),
448                          CONSISTENCY, "bad beam index given to hdnewrays");
449          if (hp->bl[i] != NULL)
450                  hp->bl[i]->tick = hdclock;      /* preempt swap */
# Line 371 | Line 456 | int    nr;                     /* number of new rays desired */
456                  blglob(hp)->nrm += n;
457                  if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
458                          errno = 0;
459 <                        if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
459 >                        if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
460                                  error(SYSTEM, "seek error on holodeck file");
461                          n *= sizeof(RAYVAL);
462                          if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 387 | Line 472 | int    nr;                     /* number of new rays desired */
472                  hdfreefrag(hp, i);              /* relinquish old fragment */
473          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
474          hp->bl[i]->nrm += nr;                   /* update in-core structure */
475 <        bzero((char *)p, nr*sizeof(RAYVAL));
475 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
476          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
477          return(p);                              /* point to new rays */
478   }
479  
480  
481   BEAM *
482 < hdgetbeam(hp, i)        /* get beam (from file if necessary) */
483 < register HOLO   *hp;
484 < register int    i;
482 > hdgetbeam(      /* get beam (from file if necessary) */
483 >        HOLO    *hp,
484 >        int     i
485 > )
486   {
487 <        register int    n;
487 >        int     n;
488  
489 <        CHECK(i < 1 | i > nbeams(hp),
489 >        CHECK((i < 1) | (i > nbeams(hp)),
490                          CONSISTENCY, "bad beam index given to hdgetbeam");
491          if (hp->bl[i] == NULL) {                /* load from disk */
492                  if (!(n = hp->bi[i].nrd))
# Line 410 | Line 496 | register int   i;
496                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
497                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
498                  errno = 0;
499 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
499 >                if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
500                          error(SYSTEM, "seek error on holodeck file");
501                  n *= sizeof(RAYVAL);
502                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 424 | Line 510 | register int   i;
510  
511  
512   int
513 < hdfilord(hb1, hb2)      /* order beams for quick loading */
514 < register HDBEAMI        *hb1, *hb2;
513 > hdfilord(       /* order beams for quick loading */
514 >        const void      *hb1,
515 >        const void      *hb2
516 > )
517   {
518 <        register long   c;
518 >        off_t   c;
519                                  /* residents go first */
520 <        if (hb2->h->bl[hb2->b] != NULL)
521 <                return(hb1->h->bl[hb1->b] == NULL);
522 <        if (hb1->h->bl[hb1->b] != NULL)
520 >        if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL)
521 >                return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL);
522 >        if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL)
523                  return(-1);
524                                  /* otherwise sort by file descriptor */
525 <        if ((c = hb1->h->fd - hb2->h->fd))
526 <                return(c);
525 >        if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd)
526 >                return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd);
527                                  /* then by position in file */
528 <        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
528 >        c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo
529 >                - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo;
530          return(c > 0 ? 1 : c < 0 ? -1 : 0);
531   }
532  
533  
534 < hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
535 < register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
536 < int     n;                      /* list length */
537 < int     (*bf)();                /* callback function (optional) */
534 > void
535 > hdloadbeams(    /* load a list of beams in optimal order */
536 >        HDBEAMI *hb,    /* list gets sorted by hdfilord() */
537 >        int     n,                      /* list length */
538 >        void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */
539 > )
540   {
541          unsigned        origcachesize, memuse;
542          int     bytesloaded, needbytes, bytes2free;
543 <        register BEAM   *bp;
544 <        register int    i;
543 >        BEAM    *bp;
544 >        int     i;
545                                          /* precheck consistency */
546          if (n <= 0) return;
547          for (i = n; i--; )
548 <                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
548 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
549                          error(CONSISTENCY, "bad beam in hdloadbeams");
550                                          /* sort list for optimal access */
551 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
551 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
552          bytesloaded = 0;                /* run through loaded beams */
553          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
554                  bp->tick = hdclock;     /* preempt swap */
# Line 488 | Line 579 | int    (*bf)();                /* callback function (optional) */
579  
580  
581   int
582 < hdfreefrag(hp, i)                       /* free a file fragment */
583 < HOLO    *hp;
584 < int     i;
582 > hdfreefrag(                     /* free a file fragment */
583 >        HOLO    *hp,
584 >        int     i
585 > )
586   {
587 <        register BEAMI  *bi = &hp->bi[i];
588 <        register struct fraglist        *f;
589 <        register int    j, k;
587 >        BEAMI   *bi = &hp->bi[i];
588 >        struct fraglist *f;
589 >        int     j, k;
590  
591          if (bi->nrd <= 0)
592                  return(0);
593          DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
594                          CONSISTENCY, "bad file descriptor in hdfreefrag");
595          f = &hdfragl[hp->fd];
596 +        if (!f->writable)
597 +                return(0);
598          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
599                  for (j = k = 0; k < f->nfrags; j++, k++) {
600                          while (f->fi[k].nrd == 0)
601                                  if (++k >= f->nfrags)
602                                          goto endloop;
603                          if (k > j)
604 <                                copystruct(f->fi+j, f->fi+k);
604 >                                *(f->fi+j) = *(f->fi+k);
605                  }
606          endloop:
607                  f->nfrags = j;
# Line 521 | Line 615 | int    i;
615          }
616   #endif
617          if (j % FRAGBLK == 0) {         /* more (or less) free list space */
618 <                register BEAMI  *newp;
618 >                BEAMI   *newp;
619                  if (f->fi == NULL)
620                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
621                  else
622 <                        newp = (BEAMI *)realloc((char *)f->fi,
622 >                        newp = (BEAMI *)realloc((void *)f->fi,
623                                          (j+FRAGBLK)*sizeof(BEAMI));
624                  if (newp == NULL) {
625                          f->nfrags--;    /* graceful failure */
# Line 539 | Line 633 | int    i;
633                          f->fi[j].nrd = bi->nrd;
634                          break;
635                  }
636 <                copystruct(f->fi+j, f->fi+(j-1));
636 >                *(f->fi+j) = *(f->fi+(j-1));
637          }
638                                          /* coalesce adjacent fragments */
639                                                  /* successors never empty */
# Line 558 | Line 652 | int    i;
652                  }
653          biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
654          bi->nrd = 0;
655 <        bi->fo = 0L;
655 >        bi->fo = 0;
656 >        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
657          return(1);
658   }
659  
660  
661   int
662 < hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
663 < int     fd;
664 < int     *listlen;
665 < register int4   *listsiz;
662 > hdfragOK(       /* get fragment list status for file */
663 >        int     fd,
664 >        int     *listlen,
665 >        int32   *listsiz
666 > )
667   {
668 <        register struct fraglist        *f;
669 <        register int    i;
668 >        struct fraglist *f;
669 >        int     i;
670  
671 <        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
671 >        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
672                  return(0);              /* listless */
673          if (listlen != NULL)
674                  *listlen = f->nfrags;
# Line 587 | Line 683 | register int4  *listsiz;
683   }
684  
685  
686 < long
687 < hdallocfrag(fd, nrays)          /* allocate a file fragment */
688 < int     fd;
689 < unsigned int4   nrays;
686 > off_t
687 > hdallocfrag(            /* allocate a file fragment */
688 >        int     fd,
689 >        uint32  nrays
690 > )
691   {
692 <        register struct fraglist        *f;
693 <        register int    j;
694 <        long    nfo;
692 >        struct fraglist *f;
693 >        int     j;
694 >        off_t   nfo;
695  
696          if (nrays == 0)
697                  return(-1L);
# Line 617 | Line 714 | unsigned int4  nrays;
714  
715  
716   int
717 < hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
718 < register HOLO   *hp;
719 < register int    i;
717 > hdsyncbeam(             /* sync beam in memory with beam on disk */
718 >        HOLO    *hp,
719 >        int     i
720 > )
721   {
722 <        unsigned int4   nrays;
722 >        int     fragfreed;
723 >        uint32  nrays;
724          unsigned int    n;
725 <        long    nfo;
725 >        off_t   nfo;
726                                          /* check file status */
727 <        if (hdfragl[hp->fd].writerr)
728 <                return(-1);
727 >        if (hdfragl[hp->fd].writable <= 0)
728 >                return(hdfragl[hp->fd].writable);
729          DCHECK(i < 1 | i > nbeams(hp),
730                          CONSISTENCY, "bad beam index in hdsyncbeam");
731                                          /* is current fragment OK? */
732          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
733                  return(0);
734 <        if (hdfragflags&FF_WRITE && hp->bi[i].nrd)
735 <                hdfreefrag(hp, i);      /* relinquish old fragment */
734 >                                        /* relinquish old fragment? */
735 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
736          if (nrays) {                    /* get and write new fragment */
737                  nfo = hdallocfrag(hp->fd, nrays);
738                  errno = 0;
739 <                if (lseek(hp->fd, nfo, 0) < 0)
739 >                if (lseek(hp->fd, nfo, SEEK_SET) < 0)
740                          error(SYSTEM, "cannot seek on holodeck file");
741                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
742                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
743 <                        hdfragl[hp->fd].writerr++;
743 >                        hdfragl[hp->fd].writable = -1;
744                          hdsync(NULL, 0);        /* sync directories */
745                          error(SYSTEM, "write error in hdsyncbeam");
746                  }
747                  hp->bi[i].fo = nfo;
748          } else
749 <                hp->bi[i].fo = 0L;
749 >                hp->bi[i].fo = 0;
750          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
751          hp->bi[i].nrd = nrays;
752 <        hdmarkdirty(hp, i);             /* section directory now out of date */
752 >        if (!fragfreed)
753 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
754          return(1);
755   }
756  
757  
758   int
759 < hdfreebeam(hp, i)               /* free beam, writing if dirty */
760 < register HOLO   *hp;
761 < register int    i;
759 > hdfreebeam(             /* free beam, writing if dirty */
760 >        HOLO    *hp,
761 >        int     i
762 > )
763   {
764          int     nchanged;
765  
# Line 668 | Line 769 | register int   i;
769                          nchanged += hdfreebeam(hdlist[i], 0);
770                  return(nchanged);
771          }
772 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
772 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
773                  return(0);
774          if (i == 0) {                   /* clear entire holodeck */
775                  if (blglob(hp)->nrm == 0)
# Line 690 | Line 791 | register int   i;
791          if (nchanged)
792                  hdsyncbeam(hp, i);              /* write new fragment */
793          blglob(hp)->nrm -= hp->bl[i]->nrm;
794 <        free((char *)hp->bl[i]);                /* free memory */
794 >        free((void *)hp->bl[i]);                /* free memory */
795          hp->bl[i] = NULL;
796          return(nchanged);
797   }
798  
799  
800   int
801 < hdkillbeam(hp, i)               /* delete beam from holodeck */
802 < register HOLO   *hp;
803 < register int    i;
801 > hdkillbeam(             /* delete beam from holodeck */
802 >        HOLO    *hp,
803 >        int     i
804 > )
805   {
806          int     nchanged;
807  
# Line 710 | Line 812 | register int   i;
812                  return(nchanged);
813          }
814          if (i == 0) {                   /* clobber entire holodeck */
815 <                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
815 >                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
816                          return(0);              /* already empty */
817                  nchanged = 0;
818                  nchanged = 0;
# Line 721 | Line 823 | register int   i;
823                                  CONSISTENCY, "bad beam count in hdkillbeam");
824                  return(nchanged);
825          }
826 <        DCHECK(i < 1 | i > nbeams(hp),
827 <                        CONSISTENCY, "bad beam index to hdkillbeam");
826 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
827 >                        "bad beam index to hdkillbeam");
828 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
829 >                        "hdkillbeam called on read-only holodeck");
830          if (hp->bl[i] != NULL) {        /* free memory */
831                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
832 <                free((char *)hp->bl[i]);
832 >                free((void *)hp->bl[i]);
833 >                hp->bl[i] = NULL;
834          } else
835                  nchanged = hp->bi[i].nrd;
836 <        if (hp->bi[i].nrd) {
837 <                if (hdfragflags&FF_KILL)
838 <                        hdfreefrag(hp, i);
839 <                biglob(hp)->nrd -= hp->bi[i].nrd;
840 <                hp->bi[i].nrd = 0;      /* make sure it's gone */
736 <                hp->bi[i].fo = 0L;
836 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
837 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
838 >                hp->bi[i].nrd = 0;
839 >                hp->bi[i].fo = 0;
840 >                hdmarkdirty(hp, i);
841          }
738        hp->bl[i] = NULL;
842          return(nchanged);
843   }
844  
845  
846   int
847 < hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
848 < register HDBEAMI        *hb;            /* beam list */
849 < int     nents;                          /* current list length */
850 < int     n;                              /* maximum list length */
851 < register HOLO   *hp;                    /* section we're adding from */
847 > hdlrulist(      /* add beams from holodeck to LRU list */
848 >        HDBEAMI *hb,            /* beam list */
849 >        int     nents,                          /* current list length */
850 >        int     n,                              /* maximum list length */
851 >        HOLO    *hp                     /* section we're adding from */
852 > )
853   {
854 <        register int    i, j;
854 >        int     i, j;
855                                          /* insert each beam from hp */
856          for (i = 1; i <= nbeams(hp); i++) {
857                  if (hp->bl[i] == NULL)          /* check if loaded */
# Line 765 | Line 869 | register HOLO  *hp;                    /* section we're adding from */
869                                  hb[j].b = i;
870                                  break;
871                          }
872 <                        copystruct(hb+j, hb+(j-1));
872 >                        *(hb+j) = *(hb+(j-1));
873                  }
874          }
875          return(nents);                  /* return new list length */
# Line 773 | Line 877 | register HOLO  *hp;                    /* section we're adding from */
877  
878  
879   int
880 < hdfreecache(pct, honly)         /* free up cache space, writing changes */
881 < int     pct;                            /* maximum percentage to free */
882 < register HOLO   *honly;                 /* NULL means check all */
880 > hdfreecache(            /* free up cache space, writing changes */
881 >        int     pct,                            /* maximum percentage to free */
882 >        HOLO    *honly                  /* NULL means check all */
883 > )
884   {
885          HDBEAMI hb[FREEBEAMS];
886          int     freetarget;
887          int     n;
888 <        register int    i;
888 >        int     i;
889   #ifdef DEBUG
890          unsigned        membefore;
891  
# Line 816 | Line 921 | register HOLO  *honly;                 /* NULL means check all */
921   }
922  
923  
924 < hddone(hp)              /* clean up holodeck section and free */
925 < register HOLO   *hp;            /* NULL means clean up all */
924 > void
925 > hddone(         /* clean up holodeck section and free */
926 >        HOLO    *hp             /* NULL means clean up all */
927 > )
928   {
929 <        register int    i;
929 >        int     i;
930  
931          if (hp == NULL) {               /* NULL means clean up everything */
932                  while (hdlist[0] != NULL)
933                          hddone(hdlist[0]);
934 <                free((char *)hdfragl);
934 >                free((void *)hdfragl);
935                  hdfragl = NULL; nhdfragls = 0;
936                  return;
937          }
# Line 839 | Line 946 | register HOLO  *hp;            /* NULL means clean up all */
946                                  i++;
947                          break;
948                  }
949 <        free((char *)hp->bl);           /* free beam list */
950 <        free((char *)hp);               /* free holodeck struct */
949 >        free((void *)hp->bl);           /* free beam list */
950 >        free((void *)hp);               /* free holodeck struct */
951   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines