ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
(Generate patch)

Comparing ray/src/hd/holofile.c (file contents):
Revision 3.14 by gregl, Thu Dec 18 15:12:23 1997 UTC vs.
Revision 3.57 by greg, Thu Sep 9 01:41:21 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for managing holodeck files
6   *
7   *      9/30/97 GWLarson
8   */
9  
10 + #include "copyright.h"
11 +
12 + #include <string.h>
13 +
14 + #include "platform.h"
15 + #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       5
22 > #else
23 > #define CACHESIZE       17      /* default cache size (Mbytes, 0==inf) */
24   #endif
25 + #endif
26   #ifndef FREEBEAMS
27 < #define FREEBEAMS       512     /* maximum beams to free at a time */
27 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
28   #endif
29   #ifndef PCTFREE
30 < #define PCTFREE         20      /* maximum fraction to free (%) */
30 > #define PCTFREE         15      /* maximum fraction to free (%) */
31   #endif
32 + #ifndef MAXFRAGB
33 + #define MAXFRAGB        16      /* 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  
25 /* define MAXFRAG if you want to limit fragment tracking memory */
26
44   #ifndef BSD
45   #define write   writebuf        /* safe i/o routines */
46   #define read    readbuf
47   #endif
48  
49 < #define FRAGBLK         64      /* 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 fragment {
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 */
63 < } *hdfrag;              /* fragment lists, indexed by file descriptor */
62 >        off_t   flen;           /* last known file length */
63 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
64  
65 < static int      nhdfrags;       /* size of hdfrag array */
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 < hdattach(fd)            /* start tracking file fragments for some section */
80 < register int    fd;
79 >
80 >
81 > HOLO *
82 > hdalloc(                /* allocate and set holodeck section based on grid */
83 >        HDGRID  *hproto
84 > )
85   {
86 <        if (fd >= nhdfrags) {
87 <                if (nhdfrags)
88 <                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
89 <                                        (fd+1)*sizeof(struct fragment));
90 <                else
91 <                        hdfrag = (struct fragment *)malloc(
92 <                                        (fd+1)*sizeof(struct fragment));
93 <                if (hdfrag == NULL)
94 <                        error(SYSTEM, "out of memory in hdattach");
95 <                bzero((char *)(hdfrag+nhdfrags),
96 <                                (fd+1-nhdfrags)*sizeof(struct fragment));
97 <                nhdfrags = fd+1;
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 <        hdfrag[fd].nlinks++;
106 <        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
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 < /* Do we need a routine to locate file fragments given known occupants? */
116 > char *
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((void *)ptr, siz);
127 >                                        /* check success */
128 >        if (newp == NULL && rout != NULL) {
129 >                hdfreecache(25, NULL);  /* free some memory */
130 >                errno = 0;              /* retry */
131 >                newp = hdrealloc(ptr, siz, NULL);
132 >                if (newp == NULL) {     /* give up and report error */
133 >                        sprintf(errmsg, "out of memory in %s", rout);
134 >                        error(SYSTEM, errmsg);
135 >                }
136 >        }
137 >        return(newp);
138 > }
139  
140  
141 < hdrelease(fd)           /* stop 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 < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
148 <                return;
149 <        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
150 <                free((char *)hdfrag[fd].fi);
151 <                hdfrag[fd].fi = NULL;
152 <                hdfrag[fd].nfrags = 0;
147 >        if (fd >= nhdfragls) {
148 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
149 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
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].writable = wr;              /* set writable flag */
156 +                                                /* get file length */
157 +        hdfragl[fd].flen = lseek(fd, (off_t)0, SEEK_END);
158   }
159  
160  
161 < markdirty(hp)                   /* mark holodeck section directory dirty */
88 < register HOLO   *hp;
89 < {
90 <        static BEAMI    smudge = {0, -1};
161 > /* Do we need a routine to locate file fragments given known occupants? */
162  
163 <        if (hp->dirty)          /* already marked? */
163 >
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)
170                  return;
171 <        hp->dirty = 1;
172 <        if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0
173 <                        || write(hp->fd, (char *)&smudge,
174 <                                        sizeof(BEAMI)) != sizeof(BEAMI))
175 <                error(SYSTEM, "seek/write error in markdirty");
171 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
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    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 124 | 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(USER, "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 135 | 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 = 0;
238 >        biglob(hp)->nrd = rtrunc = 0;
239          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
240 <                if (hp->bi[n].nrd)
241 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
242 <                                hp->bi[n].nrd = 0;      /* off end */
243 <                        else
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));
250 +                error(WARNING, errmsg);
251 +        }
252                                          /* add to holodeck list */
253          for (n = 0; n < HDMAX; n++)
254                  if (hdlist[n] == NULL) {
# Line 160 | 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 < int
267 < hdsync(hp, all)                 /* update beams and directory on disk */
268 < register HOLO   *hp;
269 < int     all;
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),
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;
283 +                hp->dirseg[0].n = 1;
284 +                return;
285 +        }
286 +                                                /* insert into segment list */
287 +        for (j = hp->dirty; j--; ) {
288 +                if (!j || hp->dirseg[j-1].s < i) {
289 +                        hp->dirseg[j].s = i;
290 +                        hp->dirseg[j].n = 1;
291 +                        break;
292 +                }
293 +                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
294 +        }
295 +        do {                            /* check neighbors */
296 +                mindist = nbeams(hp);           /* find closest */
297 +                for (j = hp->dirty; --j; )
298 +                        if (hp->dirseg[j].s -
299 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
300 +                                        < mindist) {
301 +                                minpos = j;
302 +                                mindist = hp->dirseg[j].s -
303 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
304 +                        }
305 +                                                /* good enough? */
306 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
307 +                        break;
308 +                j = minpos - 1;                 /* coalesce neighbors */
309 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
310 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
311 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
312 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
313 +                hp->dirty--;
314 +                while (++j < hp->dirty)         /* close the gap */
315 +                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
316 +        } while (mindist <= MINDIRSEL);
317 + }
318 +
319 +
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  
328          if (hp == NULL) {               /* do all holodecks */
# Line 177 | Line 332 | int    all;
332                  return(n);
333          }
334                                          /* sync the beams */
335 <        for (j = all ? nbeams(hp) : 0; j > 0; j--)
335 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
336                  if (hp->bl[j] != NULL)
337                          hdsyncbeam(hp, j);
338          if (!hp->dirty)                 /* directory clean? */
339                  return(0);
340 <        errno = 0;
341 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
342 <                error(SYSTEM, "cannot seek on holodeck file");
343 <        n = nbeams(hp)*sizeof(BEAMI);
344 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
345 <                error(SYSTEM, "cannot update holodeck section directory");
346 <        hp->dirty = 0;
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), 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)
347 >                        error(SYSTEM, "cannot update section directory");
348 >        }
349 >        hp->dirty = 0;                  /* all clean */
350          return(1);
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 212 | Line 371 | int    all;                    /* include overhead (painful) */
371                  }
372          }
373          if (all)
374 <                for (j = 0; j < nhdfrags; j++) {
375 <                        total += sizeof(struct fragment);
376 <                        if (hdfrag[j].nfrags)
374 >                for (j = 0; j < nhdfragls; j++) {
375 >                        total += sizeof(struct fraglist);
376 >                        if (hdfragl[j].nfrags)
377                                  total += FRAGBLK*sizeof(BEAMI) *
378 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
378 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
379                  }
380          return(total);
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 >= nhdfrags || !hdfrag[fd].nlinks) {
394 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
393 >        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
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(hdfrag[fd].flen);
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 > )
408   {
409 <        long    total = 0;
410 <        register int    i, j;
409 >        off_t   total = 0;
410 >        register int    j;
411  
412          for (j = 0; hdlist[j] != NULL; j++) {
413                  if (hdlist[j]->fd != fd)
414                          continue;
415                  total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
416 <                if (all) {
417 <                        for (i = nbeams(hdlist[j]); i > 0; i--)
418 <                                if (hdlist[j]->bl[i] != NULL)
419 <                                        total += sizeof(RAYVAL) *
416 >                total += nbeams(hdlist[j])*sizeof(BEAMI) + sizeof(HDGRID);
417 > #if 0
418 >                for (i = nbeams(hdlist[j]); i > 0; i--)
419 >                        if (hdlist[j]->bl[i] != NULL)
420 >                                total += sizeof(RAYVAL) *
421                                                  (hdlist[j]->bl[i]->nrm -
422                                                  hdlist[j]->bi[i].nrd);
423 <                        total += sizeof(HDGRID) +
263 <                                        nbeams(hdlist[j])*sizeof(BEAMI);
264 <                }
423 > #endif
424          }
425 <        return(total);          /* does not include fragments */
425 >        return(total);          /* doesn't include fragments, unflushed rays */
426   }
427  
428  
429 < RAYVAL *
430 < hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */
431 < register HOLO   *hp;
432 < register int    i;
433 < int     nr;                     /* number of new rays desired */
429 > extern RAYVAL *
430 > hdnewrays(      /* allocate space for add'l rays and return pointer */
431 >        register HOLO   *hp,
432 >        register int    i,
433 >        int     nr                      /* number of new rays desired */
434 > )
435   {
436          RAYVAL  *p;
437          int     n;
438  
439 <        if (nr <= 0)
440 <                return(NULL);
441 <        if (i < 1 | i > nbeams(hp))
282 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
439 >        if (nr <= 0) return(NULL);
440 >        CHECK((i < 1) | (i > nbeams(hp)),
441 >                        CONSISTENCY, "bad beam index given to hdnewrays");
442          if (hp->bl[i] != NULL)
443                  hp->bl[i]->tick = hdclock;      /* preempt swap */
444          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
445                  hdfreecache(PCTFREE, NULL);     /* free some space */
287        errno = 0;
446          if (hp->bl[i] == NULL) {                /* allocate (and load) */
447                  n = hp->bi[i].nrd + nr;
448 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
291 <                        goto memerr;
448 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
449                  blglob(hp)->nrm += n;
450 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
451 <                        if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
450 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
451 >                        errno = 0;
452 >                        if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
453                                  error(SYSTEM, "seek error on holodeck file");
454                          n *= sizeof(RAYVAL);
455                          if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 299 | Line 457 | int    nr;                     /* number of new rays desired */
457                                  "error reading beam from holodeck file");
458                  }
459          } else {                                /* just grow in memory */
460 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
461 <                                hdbsiz(hp->bl[i]->nrm + nr) );
304 <                if (hp->bl[i] == NULL)
305 <                        goto memerr;
460 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
461 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
462                  blglob(hp)->nrm += nr;
463          }
464 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
465 +                hdfreefrag(hp, i);              /* relinquish old fragment */
466          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
467          hp->bl[i]->nrm += nr;                   /* update in-core structure */
468 <        bzero((char *)p, nr*sizeof(RAYVAL));
469 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
312 <        blglob(hp)->tick = hdclock++;
468 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
469 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
470          return(p);                              /* point to new rays */
314 memerr:
315        error(SYSTEM, "out of memory in hdnewrays");
471   }
472  
473  
474 < BEAM *
475 < hdgetbeam(hp, i)        /* get beam (from file if necessary) */
476 < register HOLO   *hp;
477 < register int    i;
474 > extern BEAM *
475 > hdgetbeam(      /* get beam (from file if necessary) */
476 >        register HOLO   *hp,
477 >        register int    i
478 > )
479   {
480          register int    n;
481  
482 <        if (i < 1 | i > nbeams(hp))
483 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
482 >        CHECK((i < 1) | (i > nbeams(hp)),
483 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
484          if (hp->bl[i] == NULL) {                /* load from disk */
485                  if (!(n = hp->bi[i].nrd))
486                          return(NULL);
487                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
488                          hdfreecache(PCTFREE, NULL);     /* get free space */
489 <                errno = 0;
334 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
335 <                        error(SYSTEM, "cannot allocate memory for beam");
489 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
490                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
491 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
491 >                errno = 0;
492 >                if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
493                          error(SYSTEM, "seek error on holodeck file");
494                  n *= sizeof(RAYVAL);
495                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
496                          error(SYSTEM, "error reading beam from holodeck file");
497 +                if (hdfragflags&FF_READ)
498 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
499          }
500 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
344 <        blglob(hp)->tick = hdclock++;
500 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
501          return(hp->bl[i]);
502   }
503  
504  
505   int
506 < hdfilord(hb1, hb2)      /* order beams for optimal loading */
507 < register HDBEAMI        *hb1, *hb2;
506 > hdfilord(       /* order beams for quick loading */
507 >        register const void     *hb1,
508 >        register const void     *hb2
509 > )
510   {
511 <        register int    c;
512 <                                /* sort by file descriptor first */
513 <        if ((c = hb1->h->fd - hb2->h->fd))
514 <                return(c);
511 >        register off_t  c;
512 >                                /* residents go first */
513 >        if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL)
514 >                return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL);
515 >        if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL)
516 >                return(-1);
517 >                                /* otherwise sort by file descriptor */
518 >        if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd)
519 >                return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd);
520                                  /* then by position in file */
521 <        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
521 >        c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo
522 >                - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo;
523 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
524   }
525  
526  
527 < hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
528 < register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
529 < int     n;                      /* list length */
530 < int     (*bf)();                /* callback function (optional) */
527 > extern void
528 > hdloadbeams(    /* load a list of beams in optimal order */
529 >        register HDBEAMI        *hb,    /* list gets sorted by hdfilord() */
530 >        int     n,                      /* list length */
531 >        void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */
532 > )
533   {
534          unsigned        origcachesize, memuse;
368        register BEAM   *bp;
535          int     bytesloaded, needbytes, bytes2free;
536 +        register BEAM   *bp;
537          register int    i;
538                                          /* precheck consistency */
539 +        if (n <= 0) return;
540          for (i = n; i--; )
541 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
541 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
542                          error(CONSISTENCY, "bad beam in hdloadbeams");
543                                          /* sort list for optimal access */
544 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
545 <        if ((origcachesize = hdcachesize) == 0)
546 <                goto loadbeams;
547 <        bytesloaded = needbytes = 0;    /* figure out memory needs */
548 <        for (i = 0; i < n; i++)
549 <                if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
550 <                        bp->tick = hdclock;             /* preempt swap */
551 <                        bytesloaded += bp->nrm;
384 <                } else                                  /* prepare to load */
385 <                        needbytes += hb[i].h->bi[hb[i].b].nrd;
544 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
545 >        bytesloaded = 0;                /* run through loaded beams */
546 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
547 >                bp->tick = hdclock;     /* preempt swap */
548 >                bytesloaded += bp->nrm;
549 >                if (bf != NULL)
550 >                        (*bf)(bp, hb);
551 >        }
552          bytesloaded *= sizeof(RAYVAL);
553 <        needbytes *= sizeof(RAYVAL);
554 <        do {                            /* free enough memory */
555 <                memuse = hdmemuse(0);
556 <                bytes2free = needbytes - (signed)(hdcachesize-memuse);
557 <                if (bytes2free > (signed)(memuse - bytesloaded))
558 <                        bytes2free = memuse - bytesloaded;
559 <        } while (bytes2free > 0 &&
560 <                        hdfreecache(100*bytes2free/memuse, NULL) < 0);
561 < loadbeams:
562 <        hdcachesize = 0;                /* load the ordered beams w/o swap */
553 >        if ((origcachesize = hdcachesize) > 0) {
554 >                needbytes = 0;          /* figure out memory needs */
555 >                for (i = n; i--; )
556 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
557 >                needbytes *= sizeof(RAYVAL);
558 >                do {                            /* free enough memory */
559 >                        memuse = hdmemuse(0);
560 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
561 >                        if (bytes2free > (int)(memuse - bytesloaded))
562 >                                bytes2free = memuse - bytesloaded;
563 >                } while (bytes2free > 0 &&
564 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
565 >                hdcachesize = 0;                /* load beams w/o swap */
566 >        }
567          for (i = 0; i < n; i++)
568                  if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
569 <                        (*bf)(bp, hb[i].h, hb[i].b);
569 >                        (*bf)(bp, hb+i);
570          hdcachesize = origcachesize;    /* resume dynamic swapping */
571   }
572  
573  
574 < int
575 < hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
576 < register HOLO   *hp;
577 < register int    i;
574 > extern int
575 > hdfreefrag(                     /* free a file fragment */
576 >        HOLO    *hp,
577 >        int     i
578 > )
579   {
580 <        unsigned int    nrays;
581 <        long    nfo;
582 <        unsigned int    n;
412 <                                        /* check file status */
413 <        if (hdfrag[hp->fd].writerr)
414 <                return(-1);
415 < #ifdef DEBUG
416 <        if (i < 1 | i > nbeams(hp))
417 <                error(CONSISTENCY, "bad beam index in hdsyncbeam");
418 < #endif
419 <                                        /* is current fragment OK? */
420 <        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
421 <                return(0);
422 <                                        /* locate fragment */
423 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
424 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
580 >        register BEAMI  *bi = &hp->bi[i];
581 >        register struct fraglist        *f;
582 >        register int    j, k;
583  
584 <        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
585 <                        hdfrag[hp->fd].flen)            /* EOF special case */
586 <                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
587 <
588 <        else {                                          /* general case */
589 <                register struct fragment        *f = &hdfrag[hp->fd];
590 <                register int    j, k;
591 <                                        /* relinquish old fragment */
592 <                if (hp->bi[i].nrd) {
593 <                        j = f->nfrags++;
594 < #ifdef MAXFRAG
595 <                        if (j >= MAXFRAG-1)
596 <                                f->nfrags--;
584 >        if (bi->nrd <= 0)
585 >                return(0);
586 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
587 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
588 >        f = &hdfragl[hp->fd];
589 >        if (!f->writable)
590 >                return(0);
591 >        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
592 >                for (j = k = 0; k < f->nfrags; j++, k++) {
593 >                        while (f->fi[k].nrd == 0)
594 >                                if (++k >= f->nfrags)
595 >                                        goto endloop;
596 >                        if (k > j)
597 >                                *(f->fi+j) = *(f->fi+k);
598 >                }
599 >        endloop:
600 >                f->nfrags = j;
601 >        }
602 >        j = f->nfrags++;                /* allocate a slot in free list */
603 > #if MAXFRAGB
604 >        if (j >= MAXFRAGB*FRAGBLK) {
605 >                f->nfrags = j--;        /* stop list growth */
606 >                if (bi->nrd <= f->fi[j].nrd)
607 >                        return(0);      /* new one no better than discard */
608 >        }
609   #endif
610 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
611 <                                if (f->fi == NULL)
612 <                                        f->fi = (BEAMI *)malloc(
613 <                                                        FRAGBLK*sizeof(BEAMI));
614 <                                else
615 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
616 <                                                (j+FRAGBLK)*sizeof(BEAMI));
617 <                                if (f->fi == NULL)
618 <                                        error(SYSTEM,
619 <                                                "out of memory in hdsyncbeam");
620 <                        }
621 <                        for ( ; ; j--) {        /* insert in descending list */
622 <                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
623 <                                        f->fi[j].fo = hp->bi[i].fo;
624 <                                        f->fi[j].nrd = hp->bi[i].nrd;
625 <                                        break;
626 <                                }
627 <                                copystruct(f->fi+j, f->fi+(j-1));
628 <                        }
610 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
611 >                register BEAMI  *newp;
612 >                if (f->fi == NULL)
613 >                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
614 >                else
615 >                        newp = (BEAMI *)realloc((void *)f->fi,
616 >                                        (j+FRAGBLK)*sizeof(BEAMI));
617 >                if (newp == NULL) {
618 >                        f->nfrags--;    /* graceful failure */
619 >                        return(0);
620 >                }
621 >                f->fi = newp;
622 >        }
623 >        for ( ; ; j--) {                /* insert in descending list */
624 >                if (!j || bi->fo < f->fi[j-1].fo) {
625 >                        f->fi[j].fo = bi->fo;
626 >                        f->fi[j].nrd = bi->nrd;
627 >                        break;
628 >                }
629 >                *(f->fi+j) = *(f->fi+(j-1));
630 >        }
631                                          /* coalesce adjacent fragments */
632 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
633 <                                if (k > j)
634 <                                        copystruct(f->fi+j, f->fi+k);
635 <                                while (k+1 < f->nfrags && f->fi[k+1].fo +
636 <                                                f->fi[k+1].nrd*sizeof(RAYVAL)
637 <                                                        == f->fi[j].fo) {
638 <                                        f->fi[j].fo -=
639 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
640 <                                        f->fi[j].nrd += f->fi[k].nrd;
641 <                                }
632 >                                                /* successors never empty */
633 >        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
634 >                f->fi[j].nrd += f->fi[j-1].nrd;
635 >                f->fi[j-1].nrd = 0;
636 >        }
637 >        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
638 >                if (f->fi[k].nrd) {
639 >                        if (f->fi[j].fo == f->fi[k].fo +
640 >                                        f->fi[k].nrd*sizeof(RAYVAL)) {
641 >                                f->fi[k].nrd += f->fi[j].nrd;
642 >                                f->fi[j].nrd = 0;
643                          }
644 <                        f->nfrags = j;
644 >                        break;
645                  }
646 <                k = -1;                 /* find closest-sized fragment */
647 <                for (j = nrays ? f->nfrags : 0; j-- > 0; )
648 <                        if (f->fi[j].nrd >= nrays &&
649 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
650 <                                if (f->fi[k=j].nrd == nrays)
651 <                                        break;
652 <                if (k < 0) {            /* no fragment -- extend file */
653 <                        nfo = f->flen;
654 <                        f->flen += nrays*sizeof(RAYVAL);
655 <                } else {                /* else use fragment */
656 <                        nfo = f->fi[k].fo;
657 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
658 <                                f->nfrags--;
659 <                                for (j = k; j < f->nfrags; j++)
660 <                                        copystruct(f->fi+j, f->fi+(j+1));
661 <                        } else {                        /* else shrink it */
662 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
663 <                                f->fi[k].nrd -= nrays;
664 <                        }
665 <                }
646 >        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
647 >        bi->nrd = 0;
648 >        bi->fo = 0;
649 >        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
650 >        return(1);
651 > }
652 >
653 >
654 > extern int
655 > hdfragOK(       /* get fragment list status for file */
656 >        int     fd,
657 >        int     *listlen,
658 >        register int32  *listsiz
659 > )
660 > {
661 >        register struct fraglist        *f;
662 >        register int    i;
663 >
664 >        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
665 >                return(0);              /* listless */
666 >        if (listlen != NULL)
667 >                *listlen = f->nfrags;
668 >        if (listsiz != NULL)
669 >                for (i = f->nfrags, *listsiz = 0; i--; )
670 >                        *listsiz += f->fi[i].nrd;
671 > #if MAXFRAGB
672 >        return(f->nfrags < MAXFRAGB*FRAGBLK);
673 > #else
674 >        return(1);                      /* list never fills up */
675 > #endif
676 > }
677 >
678 >
679 > off_t
680 > hdallocfrag(            /* allocate a file fragment */
681 >        int     fd,
682 >        uint32  nrays
683 > )
684 > {
685 >        register struct fraglist        *f;
686 >        register int    j;
687 >        off_t   nfo;
688 >
689 >        if (nrays == 0)
690 >                return(-1L);
691 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
692 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
693 >        f = &hdfragl[fd];
694 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
695 >                if (f->fi[j].nrd >= nrays)
696 >                        break;
697 >        if (j < 0) {                    /* no fragment -- extend file */
698 >                nfo = f->flen;
699 >                f->flen += nrays*sizeof(RAYVAL);
700 >        } else {                        /* else use fragment */
701 >                nfo = f->fi[j].fo;
702 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
703 >                f->fi[j].nrd -= nrays;
704          }
705 <        if (nrays) {            /* write the new fragment */
705 >        return(nfo);
706 > }
707 >
708 >
709 > int
710 > hdsyncbeam(             /* sync beam in memory with beam on disk */
711 >        register HOLO   *hp,
712 >        register int    i
713 > )
714 > {
715 >        int     fragfreed;
716 >        uint32  nrays;
717 >        unsigned int    n;
718 >        off_t   nfo;
719 >                                        /* check file status */
720 >        if (hdfragl[hp->fd].writable <= 0)
721 >                return(hdfragl[hp->fd].writable);
722 >        DCHECK(i < 1 | i > nbeams(hp),
723 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
724 >                                        /* is current fragment OK? */
725 >        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
726 >                return(0);
727 >                                        /* relinquish old fragment? */
728 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
729 >        if (nrays) {                    /* get and write new fragment */
730 >                nfo = hdallocfrag(hp->fd, nrays);
731                  errno = 0;
732 <                if (lseek(hp->fd, nfo, 0) < 0)
732 >                if (lseek(hp->fd, nfo, SEEK_SET) < 0)
733                          error(SYSTEM, "cannot seek on holodeck file");
734                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
735                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
736 <                        hdfrag[hp->fd].writerr++;
737 <                        hdsync(hp, 0);          /* sync directory */
736 >                        hdfragl[hp->fd].writable = -1;
737 >                        hdsync(NULL, 0);        /* sync directories */
738                          error(SYSTEM, "write error in hdsyncbeam");
739                  }
740 <        }
740 >                hp->bi[i].fo = nfo;
741 >        } else
742 >                hp->bi[i].fo = 0;
743          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
744          hp->bi[i].nrd = nrays;
745 <        hp->bi[i].fo = nfo;
746 <        markdirty(hp);          /* section directory now out of date */
745 >        if (!fragfreed)
746 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
747          return(1);
748   }
749  
750  
751 < int
752 < hdfreebeam(hp, i)               /* free beam, writing if dirty */
753 < register HOLO   *hp;
754 < register int    i;
751 > extern int
752 > hdfreebeam(             /* free beam, writing if dirty */
753 >        register HOLO   *hp,
754 >        register int    i
755 > )
756   {
757          int     nchanged;
758  
# Line 523 | Line 762 | register int   i;
762                          nchanged += hdfreebeam(hdlist[i], 0);
763                  return(nchanged);
764          }
765 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
765 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
766                  return(0);
767          if (i == 0) {                   /* clear entire holodeck */
768 +                if (blglob(hp)->nrm == 0)
769 +                        return(0);              /* already clear */
770                  nchanged = 0;
771                  for (i = nbeams(hp); i > 0; i--)
772                          if (hp->bl[i] != NULL)
773                                  nchanged += hdfreebeam(hp, i);
774 +                DCHECK(blglob(hp)->nrm != 0,
775 +                                CONSISTENCY, "bad beam count in hdfreebeam");
776                  return(nchanged);
777          }
778 < #ifdef DEBUG
779 <        if (i < 1 | i > nbeams(hp))
537 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
538 < #endif
778 >        DCHECK(i < 1 | i > nbeams(hp),
779 >                        CONSISTENCY, "bad beam index to hdfreebeam");
780          if (hp->bl[i] == NULL)
781                  return(0);
782                                          /* check for additions */
# Line 543 | Line 784 | register int   i;
784          if (nchanged)
785                  hdsyncbeam(hp, i);              /* write new fragment */
786          blglob(hp)->nrm -= hp->bl[i]->nrm;
787 <        free((char *)hp->bl[i]);                /* free memory */
787 >        free((void *)hp->bl[i]);                /* free memory */
788          hp->bl[i] = NULL;
789          return(nchanged);
790   }
791  
792  
793 < int
794 < hdkillbeam(hp, i)               /* delete beam from holodeck */
795 < register HOLO   *hp;
796 < register int    i;
793 > extern int
794 > hdkillbeam(             /* delete beam from holodeck */
795 >        register HOLO   *hp,
796 >        register int    i
797 > )
798   {
557        static BEAM     emptybeam;
799          int     nchanged;
800  
801          if (hp == NULL) {               /* clobber all holodecks */
# Line 564 | Line 805 | register int   i;
805                  return(nchanged);
806          }
807          if (i == 0) {                   /* clobber entire holodeck */
808 +                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
809 +                        return(0);              /* already empty */
810                  nchanged = 0;
811 +                nchanged = 0;
812                  for (i = nbeams(hp); i > 0; i--)
813                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
814                                  nchanged += hdkillbeam(hp, i);
815 < #ifdef DEBUG
816 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
573 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
574 < #endif
815 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
816 >                                CONSISTENCY, "bad beam count in hdkillbeam");
817                  return(nchanged);
818          }
819 < #ifdef DEBUG
820 <        if (i < 1 | i > nbeams(hp))
821 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
822 < #endif
819 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
820 >                        "bad beam index to hdkillbeam");
821 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
822 >                        "hdkillbeam called on read-only holodeck");
823          if (hp->bl[i] != NULL) {        /* free memory */
824                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
825 <                free((char *)hp->bl[i]);
825 >                free((void *)hp->bl[i]);
826 >                hp->bl[i] = NULL;
827          } else
828                  nchanged = hp->bi[i].nrd;
829 <        if (hp->bi[i].nrd) {            /* free file fragment */
830 <                hp->bl[i] = &emptybeam;
831 <                hdsyncbeam(hp, i);
829 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
830 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
831 >                hp->bi[i].nrd = 0;
832 >                hp->bi[i].fo = 0;
833 >                hdmarkdirty(hp, i);
834          }
590        hp->bl[i] = NULL;
835          return(nchanged);
836   }
837  
838  
839   int
840 < hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
841 < register HDBEAMI        *hb;            /* beam list */
842 < int     nents;                          /* current list length */
843 < int     n;                              /* maximum list length */
844 < register HOLO   *hp;                    /* section we're adding from */
840 > hdlrulist(      /* add beams from holodeck to LRU list */
841 >        register HDBEAMI        *hb,            /* beam list */
842 >        int     nents,                          /* current list length */
843 >        int     n,                              /* maximum list length */
844 >        register HOLO   *hp                     /* section we're adding from */
845 > )
846   {
847          register int    i, j;
848                                          /* insert each beam from hp */
849 <        for (i = nbeams(hp); i > 0; i--) {
849 >        for (i = 1; i <= nbeams(hp); i++) {
850                  if (hp->bl[i] == NULL)          /* check if loaded */
851                          continue;
852   #if 0
# Line 617 | Line 862 | register HOLO  *hp;                    /* section we're adding from */
862                                  hb[j].b = i;
863                                  break;
864                          }
865 <                        copystruct(hb+j, hb+(j-1));
865 >                        *(hb+j) = *(hb+(j-1));
866                  }
867          }
868          return(nents);                  /* return new list length */
# Line 625 | Line 870 | register HOLO  *hp;                    /* section we're adding from */
870  
871  
872   int
873 < hdfreecache(pct, honly)         /* free up cache space, writing changes */
874 < int     pct;                            /* maximum percentage to free */
875 < register HOLO   *honly;                 /* NULL means check all */
873 > hdfreecache(            /* free up cache space, writing changes */
874 >        int     pct,                            /* maximum percentage to free */
875 >        register HOLO   *honly                  /* NULL means check all */
876 > )
877   {
878          HDBEAMI hb[FREEBEAMS];
879          int     freetarget;
880          int     n;
881          register int    i;
882 + #ifdef DEBUG
883 +        unsigned        membefore;
884 +
885 +        membefore = hdmemuse(0);
886 + #endif
887                                                  /* compute free target */
888          freetarget = (honly != NULL) ? blglob(honly)->nrm :
889                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 653 | Line 904 | register HOLO  *honly;                 /* NULL means check all */
904                          break;
905          }
906          hdsync(honly, 0);       /* synchronize directories as necessary */
907 + #ifdef DEBUG
908 +        sprintf(errmsg,
909 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
910 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
911 +        wputs(errmsg);
912 + #endif
913          return(-freetarget);    /* return how far past goal we went */
914   }
915  
916  
917 < hddone(hp)              /* clean up holodeck section and free */
918 < register HOLO   *hp;            /* NULL means clean up all */
917 > extern void
918 > hddone(         /* clean up holodeck section and free */
919 >        register HOLO   *hp             /* NULL means clean up all */
920 > )
921   {
922          register int    i;
923  
924          if (hp == NULL) {               /* NULL means clean up everything */
925                  while (hdlist[0] != NULL)
926                          hddone(hdlist[0]);
927 +                free((void *)hdfragl);
928 +                hdfragl = NULL; nhdfragls = 0;
929                  return;
930          }
931                                          /* flush all data and free memory */
# Line 678 | Line 939 | register HOLO  *hp;            /* NULL means clean up all */
939                                  i++;
940                          break;
941                  }
942 <        free((char *)hp->bl);           /* free beam list */
943 <        free((char *)hp);               /* free holodeck struct */
942 >        free((void *)hp->bl);           /* free beam list */
943 >        free((void *)hp);               /* free holodeck struct */
944   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines