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.39 by gwlarson, Tue Mar 9 08:40:11 1999 UTC vs.
Revision 3.55 by schorsch, Thu Jan 1 11:21:55 2004 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
17 < #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
18 < #else
20 > #ifdef SMLMEM
21   #define CACHESIZE       5
22 + #else
23 + #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
24   #endif
25   #endif
26   #ifndef FREEBEAMS
# Line 52 | Line 56 | 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 85 | 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 < HOLO *
180 < hdinit(fd, hproto)      /* initialize a holodeck section in a file */
181 < int     fd;                     /* corresponding file descriptor */
182 < HDGRID  *hproto;                /* holodeck section grid */
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 <        long    rtrunc;
186 <        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 148 | Line 210 | HDGRID *hproto;                /* holodeck section grid */
210                                  error(WARNING, "dirty holodeck section");
211                                  break;
212                          }
213 <        } else {                        /* assume we're creating it */
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 157 | 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));
# Line 188 | Line 259 | HDGRID *hproto;                /* holodeck section grid */
259          return(hp);
260   memerr:
261          error(SYSTEM, "cannot allocate holodeck grid");
262 +        return NULL; /* pro forma return */
263   }
264  
265  
266 < hdmarkdirty(hp, i)              /* mark holodeck directory position dirty */
267 < register HOLO   *hp;
268 < int     i;
266 > void
267 > hdmarkdirty(            /* mark holodeck directory position dirty */
268 >        register HOLO   *hp,
269 >        int     i
270 > )
271   {
272          static BEAMI    smudge = {0, -1};
273          int     mindist, minpos;
274          register int    j;
275  
276          if (!hp->dirty++) {                     /* write smudge first time */
277 <                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
278 <                                || write(hp->fd, (char *)&smudge,
277 >                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI),
278 >                                        SEEK_SET) < 0 ||
279 >                                write(hp->fd, (char *)&smudge,
280                                          sizeof(BEAMI)) != sizeof(BEAMI))
281                          error(SYSTEM, "seek/write error in hdmarkdirty");
282                  hp->dirseg[0].s = i;
# Line 215 | Line 290 | int    i;
290                          hp->dirseg[j].n = 1;
291                          break;
292                  }
293 <                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
293 >                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
294          }
295          do {                            /* check neighbors */
296                  mindist = nbeams(hp);           /* find closest */
# Line 237 | Line 312 | int    i;
312                                          hp->dirseg[minpos].n - hp->dirseg[j].s;
313                  hp->dirty--;
314                  while (++j < hp->dirty)         /* close the gap */
315 <                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
315 >                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
316          } while (mindist <= MINDIRSEL);
317   }
318  
319  
320 < int
321 < hdsync(hp, all)                 /* update beams and directory on disk */
322 < register HOLO   *hp;
323 < int     all;
320 > extern int
321 > hdsync(                 /* update beams and directory on disk */
322 >        register HOLO   *hp,
323 >        int     all
324 > )
325   {
326          register int    j, n;
327  
# Line 264 | Line 340 | int    all;
340          errno = 0;                      /* write dirty segments */
341          for (j = 0; j < hp->dirty; j++) {
342                  if (lseek(hp->fd, biglob(hp)->fo +
343 <                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
343 >                            (hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0)
344                          error(SYSTEM, "cannot seek on holodeck file");
345                  n = hp->dirseg[j].n * sizeof(BEAMI);
346                  if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
# Line 275 | Line 351 | int    all;
351   }
352  
353  
354 < unsigned
355 < hdmemuse(all)           /* return memory usage (in bytes) */
356 < int     all;                    /* include overhead (painful) */
354 > unsigned int
355 > hdmemuse(               /* return memory usage (in bytes) */
356 >        int     all                     /* include overhead (painful) */
357 > )
358   {
359          long    total = 0;
360          register int    i, j;
# Line 304 | Line 381 | int    all;                    /* include overhead (painful) */
381   }
382  
383  
384 < long
385 < hdfilen(fd)             /* return file length for fd */
386 < int     fd;
384 > extern off_t
385 > hdfilen(                /* return file length for fd */
386 >        int     fd
387 > )
388   {
389 <        long    fpos, flen;
389 >        off_t   fpos, flen;
390  
391          if (fd < 0)
392                  return(-1);
393          if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
394 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
394 >                if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
395                          return(-1);
396 <                flen = lseek(fd, 0L, 2);
397 <                lseek(fd, fpos, 0);
396 >                flen = lseek(fd, (off_t)0, SEEK_END);
397 >                lseek(fd, fpos, SEEK_SET);
398                  return(flen);
399          }
400          return(hdfragl[fd].flen);
401   }
402  
403  
404 < long
405 < hdfiluse(fd, all)       /* compute file usage (in bytes) */
406 < int     fd;                     /* open file descriptor to check */
407 < int     all;                    /* include overhead and unflushed data */
404 > extern off_t
405 > hdfiluse(       /* compute file usage (in bytes) */
406 >        int     fd,                     /* open file descriptor to check */
407 >        int     all                     /* include overhead and unflushed data */
408 > )
409   {
410 <        long    total = 0;
410 >        off_t   total = 0;
411          register int    i, j;
412  
413          for (j = 0; hdlist[j] != NULL; j++) {
# Line 349 | Line 428 | int    all;                    /* include overhead and unflushed data */
428   }
429  
430  
431 < RAYVAL *
432 < hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */
433 < register HOLO   *hp;
434 < register int    i;
435 < int     nr;                     /* number of new rays desired */
431 > extern RAYVAL *
432 > hdnewrays(      /* allocate space for add'l rays and return pointer */
433 >        register HOLO   *hp,
434 >        register int    i,
435 >        int     nr                      /* number of new rays desired */
436 > )
437   {
438          RAYVAL  *p;
439          int     n;
440  
441          if (nr <= 0) return(NULL);
442 <        CHECK(i < 1 | i > nbeams(hp),
442 >        CHECK((i < 1) | (i > nbeams(hp)),
443                          CONSISTENCY, "bad beam index given to hdnewrays");
444          if (hp->bl[i] != NULL)
445                  hp->bl[i]->tick = hdclock;      /* preempt swap */
# Line 371 | Line 451 | int    nr;                     /* number of new rays desired */
451                  blglob(hp)->nrm += n;
452                  if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
453                          errno = 0;
454 <                        if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
454 >                        if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
455                                  error(SYSTEM, "seek error on holodeck file");
456                          n *= sizeof(RAYVAL);
457                          if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 387 | Line 467 | int    nr;                     /* number of new rays desired */
467                  hdfreefrag(hp, i);              /* relinquish old fragment */
468          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
469          hp->bl[i]->nrm += nr;                   /* update in-core structure */
470 <        bzero((char *)p, nr*sizeof(RAYVAL));
470 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
471          blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
472          return(p);                              /* point to new rays */
473   }
474  
475  
476 < BEAM *
477 < hdgetbeam(hp, i)        /* get beam (from file if necessary) */
478 < register HOLO   *hp;
479 < register int    i;
476 > extern BEAM *
477 > hdgetbeam(      /* get beam (from file if necessary) */
478 >        register HOLO   *hp,
479 >        register int    i
480 > )
481   {
482          register int    n;
483  
484 <        CHECK(i < 1 | i > nbeams(hp),
484 >        CHECK((i < 1) | (i > nbeams(hp)),
485                          CONSISTENCY, "bad beam index given to hdgetbeam");
486          if (hp->bl[i] == NULL) {                /* load from disk */
487                  if (!(n = hp->bi[i].nrd))
# Line 410 | Line 491 | register int   i;
491                  hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
492                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
493                  errno = 0;
494 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
494 >                if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
495                          error(SYSTEM, "seek error on holodeck file");
496                  n *= sizeof(RAYVAL);
497                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 424 | Line 505 | register int   i;
505  
506  
507   int
508 < hdfilord(hb1, hb2)      /* order beams for quick loading */
509 < register HDBEAMI        *hb1, *hb2;
508 > hdfilord(       /* order beams for quick loading */
509 >        register const void     *hb1,
510 >        register const void     *hb2
511 > )
512   {
513 <        register long   c;
513 >        register off_t  c;
514                                  /* residents go first */
515 <        if (hb2->h->bl[hb2->b] != NULL)
516 <                return(hb1->h->bl[hb1->b] == NULL);
517 <        if (hb1->h->bl[hb1->b] != NULL)
515 >        if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL)
516 >                return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL);
517 >        if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL)
518                  return(-1);
519                                  /* otherwise sort by file descriptor */
520 <        if ((c = hb1->h->fd - hb2->h->fd))
521 <                return(c);
520 >        if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd)
521 >                return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd);
522                                  /* then by position in file */
523 <        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
523 >        c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo
524 >                - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo;
525          return(c > 0 ? 1 : c < 0 ? -1 : 0);
526   }
527  
528  
529 < hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
530 < register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
531 < int     n;                      /* list length */
532 < int     (*bf)();                /* callback function (optional) */
529 > extern void
530 > hdloadbeams(    /* load a list of beams in optimal order */
531 >        register HDBEAMI        *hb,    /* list gets sorted by hdfilord() */
532 >        int     n,                      /* list length */
533 >        void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */
534 > )
535   {
536          unsigned        origcachesize, memuse;
537          int     bytesloaded, needbytes, bytes2free;
# Line 454 | Line 540 | int    (*bf)();                /* callback function (optional) */
540                                          /* precheck consistency */
541          if (n <= 0) return;
542          for (i = n; i--; )
543 <                if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h))
543 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
544                          error(CONSISTENCY, "bad beam in hdloadbeams");
545                                          /* sort list for optimal access */
546 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
546 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
547          bytesloaded = 0;                /* run through loaded beams */
548          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
549                  bp->tick = hdclock;     /* preempt swap */
# Line 487 | Line 573 | int    (*bf)();                /* callback function (optional) */
573   }
574  
575  
576 < int
577 < hdfreefrag(hp, i)                       /* free a file fragment */
578 < HOLO    *hp;
579 < int     i;
576 > extern int
577 > hdfreefrag(                     /* free a file fragment */
578 >        HOLO    *hp,
579 >        int     i
580 > )
581   {
582          register BEAMI  *bi = &hp->bi[i];
583          register struct fraglist        *f;
# Line 501 | Line 588 | int    i;
588          DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
589                          CONSISTENCY, "bad file descriptor in hdfreefrag");
590          f = &hdfragl[hp->fd];
591 +        if (!f->writable)
592 +                return(0);
593          if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
594                  for (j = k = 0; k < f->nfrags; j++, k++) {
595                          while (f->fi[k].nrd == 0)
596                                  if (++k >= f->nfrags)
597                                          goto endloop;
598                          if (k > j)
599 <                                copystruct(f->fi+j, f->fi+k);
599 >                                *(f->fi+j) = *(f->fi+k);
600                  }
601          endloop:
602                  f->nfrags = j;
# Line 525 | Line 614 | int    i;
614                  if (f->fi == NULL)
615                          newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
616                  else
617 <                        newp = (BEAMI *)realloc((char *)f->fi,
617 >                        newp = (BEAMI *)realloc((void *)f->fi,
618                                          (j+FRAGBLK)*sizeof(BEAMI));
619                  if (newp == NULL) {
620                          f->nfrags--;    /* graceful failure */
# Line 539 | Line 628 | int    i;
628                          f->fi[j].nrd = bi->nrd;
629                          break;
630                  }
631 <                copystruct(f->fi+j, f->fi+(j-1));
631 >                *(f->fi+j) = *(f->fi+(j-1));
632          }
633                                          /* coalesce adjacent fragments */
634                                                  /* successors never empty */
# Line 558 | Line 647 | int    i;
647                  }
648          biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
649          bi->nrd = 0;
650 <        bi->fo = 0L;
650 >        bi->fo = 0;
651          hdmarkdirty(hp, i);                     /* assume we'll reallocate */
652          return(1);
653   }
654  
655  
656 < int
657 < hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */
658 < int     fd;
659 < int     *listlen;
660 < register int4   *listsiz;
656 > extern int
657 > hdfragOK(       /* get fragment list status for file */
658 >        int     fd,
659 >        int     *listlen,
660 >        register int32  *listsiz
661 > )
662   {
663          register struct fraglist        *f;
664          register int    i;
665  
666 <        if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks)
666 >        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
667                  return(0);              /* listless */
668          if (listlen != NULL)
669                  *listlen = f->nfrags;
# Line 588 | Line 678 | register int4  *listsiz;
678   }
679  
680  
681 < long
682 < hdallocfrag(fd, nrays)          /* allocate a file fragment */
683 < int     fd;
684 < unsigned int4   nrays;
681 > off_t
682 > hdallocfrag(            /* allocate a file fragment */
683 >        int     fd,
684 >        uint32  nrays
685 > )
686   {
687          register struct fraglist        *f;
688          register int    j;
689 <        long    nfo;
689 >        off_t   nfo;
690  
691          if (nrays == 0)
692                  return(-1L);
# Line 618 | Line 709 | unsigned int4  nrays;
709  
710  
711   int
712 < hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
713 < register HOLO   *hp;
714 < register int    i;
712 > hdsyncbeam(             /* sync beam in memory with beam on disk */
713 >        register HOLO   *hp,
714 >        register int    i
715 > )
716   {
717          int     fragfreed;
718 <        unsigned int4   nrays;
718 >        uint32  nrays;
719          unsigned int    n;
720 <        long    nfo;
720 >        off_t   nfo;
721                                          /* check file status */
722 <        if (hdfragl[hp->fd].writerr)
723 <                return(-1);
722 >        if (hdfragl[hp->fd].writable <= 0)
723 >                return(hdfragl[hp->fd].writable);
724          DCHECK(i < 1 | i > nbeams(hp),
725                          CONSISTENCY, "bad beam index in hdsyncbeam");
726                                          /* is current fragment OK? */
# Line 639 | Line 731 | register int   i;
731          if (nrays) {                    /* get and write new fragment */
732                  nfo = hdallocfrag(hp->fd, nrays);
733                  errno = 0;
734 <                if (lseek(hp->fd, nfo, 0) < 0)
734 >                if (lseek(hp->fd, nfo, SEEK_SET) < 0)
735                          error(SYSTEM, "cannot seek on holodeck file");
736                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
737                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
738 <                        hdfragl[hp->fd].writerr++;
738 >                        hdfragl[hp->fd].writable = -1;
739                          hdsync(NULL, 0);        /* sync directories */
740                          error(SYSTEM, "write error in hdsyncbeam");
741                  }
742                  hp->bi[i].fo = nfo;
743          } else
744 <                hp->bi[i].fo = 0L;
744 >                hp->bi[i].fo = 0;
745          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
746          hp->bi[i].nrd = nrays;
747          if (!fragfreed)
# Line 658 | Line 750 | register int   i;
750   }
751  
752  
753 < int
754 < hdfreebeam(hp, i)               /* free beam, writing if dirty */
755 < register HOLO   *hp;
756 < register int    i;
753 > extern int
754 > hdfreebeam(             /* free beam, writing if dirty */
755 >        register HOLO   *hp,
756 >        register int    i
757 > )
758   {
759          int     nchanged;
760  
# Line 671 | Line 764 | register int   i;
764                          nchanged += hdfreebeam(hdlist[i], 0);
765                  return(nchanged);
766          }
767 <        if (hdfragl[hp->fd].writerr)    /* check for file error */
767 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
768                  return(0);
769          if (i == 0) {                   /* clear entire holodeck */
770                  if (blglob(hp)->nrm == 0)
# Line 693 | Line 786 | register int   i;
786          if (nchanged)
787                  hdsyncbeam(hp, i);              /* write new fragment */
788          blglob(hp)->nrm -= hp->bl[i]->nrm;
789 <        free((char *)hp->bl[i]);                /* free memory */
789 >        free((void *)hp->bl[i]);                /* free memory */
790          hp->bl[i] = NULL;
791          return(nchanged);
792   }
793  
794  
795 < int
796 < hdkillbeam(hp, i)               /* delete beam from holodeck */
797 < register HOLO   *hp;
798 < register int    i;
795 > extern int
796 > hdkillbeam(             /* delete beam from holodeck */
797 >        register HOLO   *hp,
798 >        register int    i
799 > )
800   {
801          int     nchanged;
802  
# Line 713 | Line 807 | register int   i;
807                  return(nchanged);
808          }
809          if (i == 0) {                   /* clobber entire holodeck */
810 <                if (biglob(hp)->nrd == 0 & blglob(hp)->nrm == 0)
810 >                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
811                          return(0);              /* already empty */
812                  nchanged = 0;
813                  nchanged = 0;
# Line 724 | Line 818 | register int   i;
818                                  CONSISTENCY, "bad beam count in hdkillbeam");
819                  return(nchanged);
820          }
821 <        DCHECK(i < 1 | i > nbeams(hp),
822 <                        CONSISTENCY, "bad beam index to hdkillbeam");
821 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
822 >                        "bad beam index to hdkillbeam");
823 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
824 >                        "hdkillbeam called on read-only holodeck");
825          if (hp->bl[i] != NULL) {        /* free memory */
826                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
827 <                free((char *)hp->bl[i]);
827 >                free((void *)hp->bl[i]);
828                  hp->bl[i] = NULL;
829          } else
830                  nchanged = hp->bi[i].nrd;
831          if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
832                  biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
833                  hp->bi[i].nrd = 0;
834 <                hp->bi[i].fo = 0L;
834 >                hp->bi[i].fo = 0;
835                  hdmarkdirty(hp, i);
836          }
837          return(nchanged);
# Line 743 | Line 839 | register int   i;
839  
840  
841   int
842 < hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
843 < register HDBEAMI        *hb;            /* beam list */
844 < int     nents;                          /* current list length */
845 < int     n;                              /* maximum list length */
846 < register HOLO   *hp;                    /* section we're adding from */
842 > hdlrulist(      /* add beams from holodeck to LRU list */
843 >        register HDBEAMI        *hb,            /* beam list */
844 >        int     nents,                          /* current list length */
845 >        int     n,                              /* maximum list length */
846 >        register HOLO   *hp                     /* section we're adding from */
847 > )
848   {
849          register int    i, j;
850                                          /* insert each beam from hp */
# Line 767 | Line 864 | register HOLO  *hp;                    /* section we're adding from */
864                                  hb[j].b = i;
865                                  break;
866                          }
867 <                        copystruct(hb+j, hb+(j-1));
867 >                        *(hb+j) = *(hb+(j-1));
868                  }
869          }
870          return(nents);                  /* return new list length */
# Line 775 | Line 872 | register HOLO  *hp;                    /* section we're adding from */
872  
873  
874   int
875 < hdfreecache(pct, honly)         /* free up cache space, writing changes */
876 < int     pct;                            /* maximum percentage to free */
877 < register HOLO   *honly;                 /* NULL means check all */
875 > hdfreecache(            /* free up cache space, writing changes */
876 >        int     pct,                            /* maximum percentage to free */
877 >        register HOLO   *honly                  /* NULL means check all */
878 > )
879   {
880          HDBEAMI hb[FREEBEAMS];
881          int     freetarget;
# Line 818 | Line 916 | register HOLO  *honly;                 /* NULL means check all */
916   }
917  
918  
919 < hddone(hp)              /* clean up holodeck section and free */
920 < register HOLO   *hp;            /* NULL means clean up all */
919 > extern void
920 > hddone(         /* clean up holodeck section and free */
921 >        register HOLO   *hp             /* NULL means clean up all */
922 > )
923   {
924          register int    i;
925  
926          if (hp == NULL) {               /* NULL means clean up everything */
927                  while (hdlist[0] != NULL)
928                          hddone(hdlist[0]);
929 <                free((char *)hdfragl);
929 >                free((void *)hdfragl);
930                  hdfragl = NULL; nhdfragls = 0;
931                  return;
932          }
# Line 841 | Line 941 | register HOLO  *hp;            /* NULL means clean up all */
941                                  i++;
942                          break;
943                  }
944 <        free((char *)hp->bl);           /* free beam list */
945 <        free((char *)hp);               /* free holodeck struct */
944 >        free((void *)hp->bl);           /* free beam list */
945 >        free((void *)hp);               /* free holodeck struct */
946   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines