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.15 by gregl, Mon Dec 29 15:17:45 1997 UTC vs.
Revision 3.59 by greg, Fri Sep 3 23:52:42 2010 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       10
22 > #else
23 > #define CACHESIZE       100     /* 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 MAXFRAG
33 < #define MAXFRAG         131000  /* maximum fragments/file to track (0==inf) */
32 > #ifndef MAXFRAGB
33 > #define MAXFRAGB        64      /* fragment blocks/file to track (0==inf) */
34   #endif
35 + #ifndef FF_DEFAULT
36 +                                /* when to free a beam fragment */
37 + #define FF_DEFAULT      (FF_WRITE|FF_KILL)
38 + #endif
39 + #ifndef MINDIRSEL
40 +                                /* minimum directory seek length */
41 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
42 + #endif
43  
44   #ifndef BSD
45   #define write   writebuf        /* safe i/o routines */
46   #define read    readbuf
47   #endif
48  
49 < #define FRAGBLK         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 */
89 < register HOLO   *hp;
90 < {
91 <        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 125 | 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 136 | 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 +                                (long)rtrunc,
250 +                                100.*rtrunc/(rtrunc+biglob(hp)->nrd));
251 +                error(WARNING, errmsg);
252 +        }
253                                          /* add to holodeck list */
254          for (n = 0; n < HDMAX; n++)
255                  if (hdlist[n] == NULL) {
# Line 161 | Line 260 | HDGRID *hproto;                /* holodeck section grid */
260          return(hp);
261   memerr:
262          error(SYSTEM, "cannot allocate holodeck grid");
263 +        return NULL; /* pro forma return */
264   }
265  
266  
267 < int
268 < hdsync(hp, all)                 /* update beams and directory on disk */
269 < register HOLO   *hp;
270 < int     all;
267 > void
268 > hdmarkdirty(            /* mark holodeck directory position dirty */
269 >        register HOLO   *hp,
270 >        int     i
271 > )
272   {
273 +        static BEAMI    smudge = {0, -1};
274 +        int     mindist, minpos;
275 +        register int    j;
276 +
277 +        if (!hp->dirty++) {                     /* write smudge first time */
278 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI),
279 +                                        SEEK_SET) < 0 ||
280 +                                write(hp->fd, (char *)&smudge,
281 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
282 +                        error(SYSTEM, "seek/write error in hdmarkdirty");
283 +                hp->dirseg[0].s = i;
284 +                hp->dirseg[0].n = 1;
285 +                return;
286 +        }
287 +                                                /* insert into segment list */
288 +        for (j = hp->dirty; j--; ) {
289 +                if (!j || hp->dirseg[j-1].s < i) {
290 +                        hp->dirseg[j].s = i;
291 +                        hp->dirseg[j].n = 1;
292 +                        break;
293 +                }
294 +                *(hp->dirseg+j) = *(hp->dirseg+(j-1));
295 +        }
296 +        do {                            /* check neighbors */
297 +                mindist = nbeams(hp);           /* find closest */
298 +                for (j = hp->dirty; --j; )
299 +                        if (hp->dirseg[j].s -
300 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
301 +                                        < mindist) {
302 +                                minpos = j;
303 +                                mindist = hp->dirseg[j].s -
304 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
305 +                        }
306 +                                                /* good enough? */
307 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
308 +                        break;
309 +                j = minpos - 1;                 /* coalesce neighbors */
310 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
311 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
312 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
313 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
314 +                hp->dirty--;
315 +                while (++j < hp->dirty)         /* close the gap */
316 +                        *(hp->dirseg+j) = *(hp->dirseg+(j+1));
317 +        } while (mindist <= MINDIRSEL);
318 + }
319 +
320 +
321 + extern int
322 + hdsync(                 /* update beams and directory on disk */
323 +        register HOLO   *hp,
324 +        int     all
325 + )
326 + {
327          register int    j, n;
328  
329          if (hp == NULL) {               /* do all holodecks */
# Line 183 | Line 338 | int    all;
338                          hdsyncbeam(hp, j);
339          if (!hp->dirty)                 /* directory clean? */
340                  return(0);
341 <        errno = 0;
342 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
343 <                error(SYSTEM, "cannot seek on holodeck file");
344 <        n = nbeams(hp)*sizeof(BEAMI);
345 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
346 <                error(SYSTEM, "cannot update holodeck section directory");
347 <        hp->dirty = 0;
341 >        errno = 0;                      /* write dirty segments */
342 >        for (j = 0; j < hp->dirty; j++) {
343 >                if (lseek(hp->fd, biglob(hp)->fo +
344 >                            (hp->dirseg[j].s-1)*sizeof(BEAMI), SEEK_SET) < 0)
345 >                        error(SYSTEM, "cannot seek on holodeck file");
346 >                n = hp->dirseg[j].n * sizeof(BEAMI);
347 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
348 >                        error(SYSTEM, "cannot update section directory");
349 >        }
350 >        hp->dirty = 0;                  /* all clean */
351          return(1);
352   }
353  
354  
355 < unsigned
356 < hdmemuse(all)           /* return memory usage (in bytes) */
357 < int     all;                    /* include overhead (painful) */
355 > unsigned int
356 > hdmemuse(               /* return memory usage (in bytes) */
357 >        int     all                     /* include overhead (painful) */
358 > )
359   {
360          long    total = 0;
361          register int    i, j;
# Line 213 | Line 372 | int    all;                    /* include overhead (painful) */
372                  }
373          }
374          if (all)
375 <                for (j = 0; j < nhdfrags; j++) {
376 <                        total += sizeof(struct fragment);
377 <                        if (hdfrag[j].nfrags)
375 >                for (j = 0; j < nhdfragls; j++) {
376 >                        total += sizeof(struct fraglist);
377 >                        if (hdfragl[j].nfrags)
378                                  total += FRAGBLK*sizeof(BEAMI) *
379 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
379 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
380                  }
381          return(total);
382   }
383  
384  
385 < long
386 < hdfilen(fd)             /* return file length for fd */
387 < int     fd;
385 > extern off_t
386 > hdfilen(                /* return file length for fd */
387 >        int     fd
388 > )
389   {
390 <        long    fpos, flen;
390 >        off_t   fpos, flen;
391  
392          if (fd < 0)
393                  return(-1);
394 <        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
395 <                if ((fpos = lseek(fd, 0L, 1)) < 0)
394 >        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
395 >                if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0)
396                          return(-1);
397 <                flen = lseek(fd, 0L, 2);
398 <                lseek(fd, fpos, 0);
397 >                flen = lseek(fd, (off_t)0, SEEK_END);
398 >                lseek(fd, fpos, SEEK_SET);
399                  return(flen);
400          }
401 <        return(hdfrag[fd].flen);
401 >        return(hdfragl[fd].flen);
402   }
403  
404  
405 < long
406 < hdfiluse(fd, all)       /* compute file usage (in bytes) */
407 < int     fd;                     /* open file descriptor to check */
408 < int     all;                    /* include overhead and unflushed data */
405 > extern off_t
406 > hdfiluse(       /* compute file usage (in bytes) */
407 >        int     fd                      /* open file descriptor to check */
408 > )
409   {
410 <        long    total = 0;
411 <        register int    i, j;
410 >        off_t   total = 0;
411 >        register int    j;
412  
413          for (j = 0; hdlist[j] != NULL; j++) {
414                  if (hdlist[j]->fd != fd)
415                          continue;
416                  total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
417 <                if (all) {
418 <                        for (i = nbeams(hdlist[j]); i > 0; i--)
419 <                                if (hdlist[j]->bl[i] != NULL)
420 <                                        total += sizeof(RAYVAL) *
417 >                total += nbeams(hdlist[j])*sizeof(BEAMI) + sizeof(HDGRID);
418 > #if 0
419 >                for (i = nbeams(hdlist[j]); i > 0; i--)
420 >                        if (hdlist[j]->bl[i] != NULL)
421 >                                total += sizeof(RAYVAL) *
422                                                  (hdlist[j]->bl[i]->nrm -
423                                                  hdlist[j]->bi[i].nrd);
424 <                        total += sizeof(HDGRID) +
264 <                                        nbeams(hdlist[j])*sizeof(BEAMI);
265 <                }
424 > #endif
425          }
426 <        return(total);          /* does not include fragments */
426 >        return(total);          /* doesn't include fragments, unflushed rays */
427   }
428  
429  
430 < RAYVAL *
431 < hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */
432 < register HOLO   *hp;
433 < register int    i;
434 < int     nr;                     /* number of new rays desired */
430 > extern RAYVAL *
431 > hdnewrays(      /* allocate space for add'l rays and return pointer */
432 >        register HOLO   *hp,
433 >        register int    i,
434 >        int     nr                      /* number of new rays desired */
435 > )
436   {
437          RAYVAL  *p;
438          int     n;
439  
440 <        if (nr <= 0)
441 <                return(NULL);
442 <        if (i < 1 | i > nbeams(hp))
283 <                error(CONSISTENCY, "bad beam index given to hdnewrays");
440 >        if (nr <= 0) return(NULL);
441 >        CHECK((i < 1) | (i > nbeams(hp)),
442 >                        CONSISTENCY, "bad beam index given to hdnewrays");
443          if (hp->bl[i] != NULL)
444                  hp->bl[i]->tick = hdclock;      /* preempt swap */
445          if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
446                  hdfreecache(PCTFREE, NULL);     /* free some space */
288        errno = 0;
447          if (hp->bl[i] == NULL) {                /* allocate (and load) */
448                  n = hp->bi[i].nrd + nr;
449 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
292 <                        goto memerr;
449 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
450                  blglob(hp)->nrm += n;
451 <                if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
452 <                        if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
451 >                if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) {
452 >                        errno = 0;
453 >                        if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
454                                  error(SYSTEM, "seek error on holodeck file");
455                          n *= sizeof(RAYVAL);
456                          if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
# Line 300 | Line 458 | int    nr;                     /* number of new rays desired */
458                                  "error reading beam from holodeck file");
459                  }
460          } else {                                /* just grow in memory */
461 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
462 <                                hdbsiz(hp->bl[i]->nrm + nr) );
305 <                if (hp->bl[i] == NULL)
306 <                        goto memerr;
461 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
462 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
463                  blglob(hp)->nrm += nr;
464          }
465 +        if (hdfragflags&FF_ALLOC && hp->bi[i].nrd)
466 +                hdfreefrag(hp, i);              /* relinquish old fragment */
467          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
468          hp->bl[i]->nrm += nr;                   /* update in-core structure */
469 <        bzero((char *)p, nr*sizeof(RAYVAL));
470 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
313 <        blglob(hp)->tick = hdclock++;
469 >        memset((void *)p, '\0', nr*sizeof(RAYVAL));
470 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
471          return(p);                              /* point to new rays */
315 memerr:
316        error(SYSTEM, "out of memory in hdnewrays");
472   }
473  
474  
475 < BEAM *
476 < hdgetbeam(hp, i)        /* get beam (from file if necessary) */
477 < register HOLO   *hp;
478 < register int    i;
475 > extern BEAM *
476 > hdgetbeam(      /* get beam (from file if necessary) */
477 >        register HOLO   *hp,
478 >        register int    i
479 > )
480   {
481          register int    n;
482  
483 <        if (i < 1 | i > nbeams(hp))
484 <                error(CONSISTENCY, "bad beam index given to hdgetbeam");
483 >        CHECK((i < 1) | (i > nbeams(hp)),
484 >                        CONSISTENCY, "bad beam index given to hdgetbeam");
485          if (hp->bl[i] == NULL) {                /* load from disk */
486                  if (!(n = hp->bi[i].nrd))
487                          return(NULL);
488                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
489                          hdfreecache(PCTFREE, NULL);     /* get free space */
490 <                errno = 0;
335 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
336 <                        error(SYSTEM, "cannot allocate memory for beam");
490 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
491                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
492 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
492 >                errno = 0;
493 >                if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0)
494                          error(SYSTEM, "seek error on holodeck file");
495                  n *= sizeof(RAYVAL);
496                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
497                          error(SYSTEM, "error reading beam from holodeck file");
498 +                if (hdfragflags&FF_READ)
499 +                        hdfreefrag(hp, i);      /* relinquish old frag. */
500          }
501 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
345 <        blglob(hp)->tick = hdclock++;
501 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
502          return(hp->bl[i]);
503   }
504  
505  
506   int
507 < hdfilord(hb1, hb2)      /* order beams for optimal loading */
508 < register HDBEAMI        *hb1, *hb2;
507 > hdfilord(       /* order beams for quick loading */
508 >        register const void     *hb1,
509 >        register const void     *hb2
510 > )
511   {
512 <        register int    c;
513 <                                /* sort by file descriptor first */
514 <        if ((c = hb1->h->fd - hb2->h->fd))
515 <                return(c);
512 >        register off_t  c;
513 >                                /* residents go first */
514 >        if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL)
515 >                return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL);
516 >        if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL)
517 >                return(-1);
518 >                                /* otherwise sort by file descriptor */
519 >        if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd)
520 >                return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd);
521                                  /* then by position in file */
522 <        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
522 >        c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo
523 >                - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo;
524 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
525   }
526  
527  
528 < hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
529 < register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
530 < int     n;                      /* list length */
531 < int     (*bf)();                /* callback function (optional) */
528 > extern void
529 > hdloadbeams(    /* load a list of beams in optimal order */
530 >        register HDBEAMI        *hb,    /* list gets sorted by hdfilord() */
531 >        int     n,                      /* list length */
532 >        void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */
533 > )
534   {
535          unsigned        origcachesize, memuse;
369        register BEAM   *bp;
536          int     bytesloaded, needbytes, bytes2free;
537 +        register BEAM   *bp;
538          register int    i;
539                                          /* precheck consistency */
540 +        if (n <= 0) return;
541          for (i = n; i--; )
542 <                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
542 >                if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h)))
543                          error(CONSISTENCY, "bad beam in hdloadbeams");
544                                          /* sort list for optimal access */
545 <        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
546 <        bytesloaded = needbytes = 0;    /* figure out memory needs */
545 >        qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord);
546 >        bytesloaded = 0;                /* run through loaded beams */
547 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
548 >                bp->tick = hdclock;     /* preempt swap */
549 >                bytesloaded += bp->nrm;
550 >                if (bf != NULL)
551 >                        (*bf)(bp, hb);
552 >        }
553 >        bytesloaded *= sizeof(RAYVAL);
554          if ((origcachesize = hdcachesize) > 0) {
555 +                needbytes = 0;          /* figure out memory needs */
556                  for (i = n; i--; )
557 <                        if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
382 <                                bp->tick = hdclock;     /* preempt swap */
383 <                                bytesloaded += bp->nrm;
384 <                        } else                          /* prepare to load */
385 <                                needbytes += hb[i].h->bi[hb[i].b].nrd;
386 <                bytesloaded *= sizeof(RAYVAL);
557 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
558                  needbytes *= sizeof(RAYVAL);
559                  do {                            /* free enough memory */
560                          memuse = hdmemuse(0);
561 <                        bytes2free = needbytes - (signed)(hdcachesize-memuse);
562 <                        if (bytes2free > (signed)(memuse - bytesloaded))
561 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
562 >                        if (bytes2free > (int)(memuse - bytesloaded))
563                                  bytes2free = memuse - bytesloaded;
564                  } while (bytes2free > 0 &&
565                                  hdfreecache(100*bytes2free/memuse, NULL) < 0);
566 +                hdcachesize = 0;                /* load beams w/o swap */
567          }
396        hdcachesize = 0;                /* load the ordered beams w/o swap */
568          for (i = 0; i < n; i++)
569                  if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
570 <                        (*bf)(bp, hb[i].h, hb[i].b);
570 >                        (*bf)(bp, hb+i);
571          hdcachesize = origcachesize;    /* resume dynamic swapping */
572   }
573  
574  
575 < int
576 < hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
577 < register HOLO   *hp;
578 < register int    i;
575 > extern int
576 > hdfreefrag(                     /* free a file fragment */
577 >        HOLO    *hp,
578 >        int     i
579 > )
580   {
581 <        unsigned int    nrays;
582 <        long    nfo;
583 <        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);
581 >        register BEAMI  *bi = &hp->bi[i];
582 >        register struct fraglist        *f;
583 >        register int    j, k;
584  
585 <        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
586 <                        hdfrag[hp->fd].flen)            /* EOF special case */
587 <                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
588 <
589 <        else {                                          /* general case */
590 <                register struct fragment        *f = &hdfrag[hp->fd];
591 <                register int    j, k;
592 <                                        /* relinquish old fragment */
434 <                if (hp->bi[i].nrd) {
435 <                        j = f->nfrags++;
436 < #if MAXFRAG
437 <                        if (j >= MAXFRAG-1)
438 <                                f->nfrags--;
439 < #endif
440 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
441 <                                if (f->fi == NULL)
442 <                                        f->fi = (BEAMI *)malloc(
443 <                                                        FRAGBLK*sizeof(BEAMI));
444 <                                else
445 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
446 <                                                (j+FRAGBLK)*sizeof(BEAMI));
447 <                                if (f->fi == NULL)
448 <                                        error(SYSTEM,
449 <                                                "out of memory in hdsyncbeam");
450 <                        }
451 <                        for ( ; ; j--) {        /* insert in descending list */
452 <                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
453 <                                        f->fi[j].fo = hp->bi[i].fo;
454 <                                        f->fi[j].nrd = hp->bi[i].nrd;
455 <                                        break;
456 <                                }
457 <                                copystruct(f->fi+j, f->fi+(j-1));
458 <                        }
459 <                                        /* coalesce adjacent fragments */
460 <                        if (j && f->fi[j-1].fo == f->fi[j].fo +
461 <                                        f->fi[j].nrd*sizeof(RAYVAL)) {
462 <                                f->fi[j].nrd += f->fi[j-1].nrd;
463 <                                f->fi[j-1].nrd = 0;
464 <                        }
465 <                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo +
466 <                                        f->fi[j+1].nrd*sizeof(RAYVAL)) {
467 <                                f->fi[j+1].nrd += f->fi[j].nrd;
468 <                                f->fi[j].nrd = 0;
469 <                        }
470 <                }
471 <                k = -1;                 /* find closest-sized fragment */
472 <                for (j = (nrays ? f->nfrags : 0); j-- > 0; )
473 <                        if (f->fi[j].nrd >= nrays &&
474 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
475 <                                if (f->fi[k=j].nrd == nrays)
476 <                                        break;
477 <                if (k < 0) {            /* no fragment -- extend file */
478 <                        nfo = f->flen;
479 <                        f->flen += nrays*sizeof(RAYVAL);
480 <                } else {                /* else use fragment */
481 <                        nfo = f->fi[k].fo;
482 <                        f->fi[k].fo += nrays*sizeof(RAYVAL);
483 <                        f->fi[k].nrd -= nrays;
484 <                }
485 <                                        /* delete empty remnants */
585 >        if (bi->nrd <= 0)
586 >                return(0);
587 >        DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks,
588 >                        CONSISTENCY, "bad file descriptor in hdfreefrag");
589 >        f = &hdfragl[hp->fd];
590 >        if (!f->writable)
591 >                return(0);
592 >        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
593                  for (j = k = 0; k < f->nfrags; j++, k++) {
594                          while (f->fi[k].nrd == 0)
595                                  if (++k >= f->nfrags)
596                                          goto endloop;
597                          if (k > j)
598 <                                copystruct(f->fi+j, f->fi+k);
598 >                                *(f->fi+j) = *(f->fi+k);
599                  }
600          endloop:
601                  f->nfrags = j;
602          }
603 <        if (nrays) {            /* write the new fragment */
603 >        j = f->nfrags++;                /* allocate a slot in free list */
604 > #if MAXFRAGB
605 >        if (j >= MAXFRAGB*FRAGBLK) {
606 >                f->nfrags = j--;        /* stop list growth */
607 >                if (bi->nrd <= f->fi[j].nrd)
608 >                        return(0);      /* new one no better than discard */
609 >        }
610 > #endif
611 >        if (j % FRAGBLK == 0) {         /* more (or less) free list space */
612 >                register BEAMI  *newp;
613 >                if (f->fi == NULL)
614 >                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
615 >                else
616 >                        newp = (BEAMI *)realloc((void *)f->fi,
617 >                                        (j+FRAGBLK)*sizeof(BEAMI));
618 >                if (newp == NULL) {
619 >                        f->nfrags--;    /* graceful failure */
620 >                        return(0);
621 >                }
622 >                f->fi = newp;
623 >        }
624 >        for ( ; ; j--) {                /* insert in descending list */
625 >                if (!j || bi->fo < f->fi[j-1].fo) {
626 >                        f->fi[j].fo = bi->fo;
627 >                        f->fi[j].nrd = bi->nrd;
628 >                        break;
629 >                }
630 >                *(f->fi+j) = *(f->fi+(j-1));
631 >        }
632 >                                        /* coalesce adjacent fragments */
633 >                                                /* successors never empty */
634 >        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
635 >                f->fi[j].nrd += f->fi[j-1].nrd;
636 >                f->fi[j-1].nrd = 0;
637 >        }
638 >        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
639 >                if (f->fi[k].nrd) {
640 >                        if (f->fi[j].fo == f->fi[k].fo +
641 >                                        f->fi[k].nrd*sizeof(RAYVAL)) {
642 >                                f->fi[k].nrd += f->fi[j].nrd;
643 >                                f->fi[j].nrd = 0;
644 >                        }
645 >                        break;
646 >                }
647 >        biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */
648 >        bi->nrd = 0;
649 >        bi->fo = 0;
650 >        hdmarkdirty(hp, i);                     /* assume we'll reallocate */
651 >        return(1);
652 > }
653 >
654 >
655 > extern int
656 > hdfragOK(       /* get fragment list status for file */
657 >        int     fd,
658 >        int     *listlen,
659 >        register int32  *listsiz
660 > )
661 > {
662 >        register struct fraglist        *f;
663 >        register int    i;
664 >
665 >        if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks)
666 >                return(0);              /* listless */
667 >        if (listlen != NULL)
668 >                *listlen = f->nfrags;
669 >        if (listsiz != NULL)
670 >                for (i = f->nfrags, *listsiz = 0; i--; )
671 >                        *listsiz += f->fi[i].nrd;
672 > #if MAXFRAGB
673 >        return(f->nfrags < MAXFRAGB*FRAGBLK);
674 > #else
675 >        return(1);                      /* list never fills up */
676 > #endif
677 > }
678 >
679 >
680 > off_t
681 > hdallocfrag(            /* allocate a file fragment */
682 >        int     fd,
683 >        uint32  nrays
684 > )
685 > {
686 >        register struct fraglist        *f;
687 >        register int    j;
688 >        off_t   nfo;
689 >
690 >        if (nrays == 0)
691 >                return(-1L);
692 >        DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks,
693 >                        CONSISTENCY, "bad file descriptor in hdallocfrag");
694 >        f = &hdfragl[fd];
695 >        for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */
696 >                if (f->fi[j].nrd >= nrays)
697 >                        break;
698 >        if (j < 0) {                    /* no fragment -- extend file */
699 >                nfo = f->flen;
700 >                f->flen += nrays*sizeof(RAYVAL);
701 >        } else {                        /* else use fragment */
702 >                nfo = f->fi[j].fo;
703 >                f->fi[j].fo += nrays*sizeof(RAYVAL);
704 >                f->fi[j].nrd -= nrays;
705 >        }
706 >        return(nfo);
707 > }
708 >
709 >
710 > int
711 > hdsyncbeam(             /* sync beam in memory with beam on disk */
712 >        register HOLO   *hp,
713 >        register int    i
714 > )
715 > {
716 >        int     fragfreed;
717 >        uint32  nrays;
718 >        unsigned int    n;
719 >        off_t   nfo;
720 >                                        /* check file status */
721 >        if (hdfragl[hp->fd].writable <= 0)
722 >                return(hdfragl[hp->fd].writable);
723 >        DCHECK(i < 1 | i > nbeams(hp),
724 >                        CONSISTENCY, "bad beam index in hdsyncbeam");
725 >                                        /* is current fragment OK? */
726 >        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
727 >                return(0);
728 >                                        /* relinquish old fragment? */
729 >        fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i);
730 >        if (nrays) {                    /* get and write new fragment */
731 >                nfo = hdallocfrag(hp->fd, nrays);
732                  errno = 0;
733 <                if (lseek(hp->fd, nfo, 0) < 0)
733 >                if (lseek(hp->fd, nfo, SEEK_SET) < 0)
734                          error(SYSTEM, "cannot seek on holodeck file");
735                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
736                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
737 <                        hdfrag[hp->fd].writerr++;
738 <                        hdsync(hp, 0);          /* sync directory */
737 >                        hdfragl[hp->fd].writable = -1;
738 >                        hdsync(NULL, 0);        /* sync directories */
739                          error(SYSTEM, "write error in hdsyncbeam");
740                  }
741 <        }
741 >                hp->bi[i].fo = nfo;
742 >        } else
743 >                hp->bi[i].fo = 0;
744          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
745          hp->bi[i].nrd = nrays;
746 <        hp->bi[i].fo = nfo;
747 <        markdirty(hp);          /* section directory now out of date */
746 >        if (!fragfreed)
747 >                hdmarkdirty(hp, i);             /* need to flag dir. ent. */
748          return(1);
749   }
750  
751  
752 < int
753 < hdfreebeam(hp, i)               /* free beam, writing if dirty */
754 < register HOLO   *hp;
755 < register int    i;
752 > extern int
753 > hdfreebeam(             /* free beam, writing if dirty */
754 >        register HOLO   *hp,
755 >        register int    i
756 > )
757   {
758          int     nchanged;
759  
# Line 525 | Line 763 | register int   i;
763                          nchanged += hdfreebeam(hdlist[i], 0);
764                  return(nchanged);
765          }
766 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
766 >        if (hdfragl[hp->fd].writable < 0)       /* check for file error */
767                  return(0);
768          if (i == 0) {                   /* clear entire holodeck */
769 +                if (blglob(hp)->nrm == 0)
770 +                        return(0);              /* already clear */
771                  nchanged = 0;
772                  for (i = nbeams(hp); i > 0; i--)
773                          if (hp->bl[i] != NULL)
774                                  nchanged += hdfreebeam(hp, i);
775 +                DCHECK(blglob(hp)->nrm != 0,
776 +                                CONSISTENCY, "bad beam count in hdfreebeam");
777                  return(nchanged);
778          }
779 < #ifdef DEBUG
780 <        if (i < 1 | i > nbeams(hp))
539 <                error(CONSISTENCY, "bad beam index to hdfreebeam");
540 < #endif
779 >        DCHECK(i < 1 | i > nbeams(hp),
780 >                        CONSISTENCY, "bad beam index to hdfreebeam");
781          if (hp->bl[i] == NULL)
782                  return(0);
783                                          /* check for additions */
# Line 545 | Line 785 | register int   i;
785          if (nchanged)
786                  hdsyncbeam(hp, i);              /* write new fragment */
787          blglob(hp)->nrm -= hp->bl[i]->nrm;
788 <        free((char *)hp->bl[i]);                /* free memory */
788 >        free((void *)hp->bl[i]);                /* free memory */
789          hp->bl[i] = NULL;
790          return(nchanged);
791   }
792  
793  
794 < int
795 < hdkillbeam(hp, i)               /* delete beam from holodeck */
796 < register HOLO   *hp;
797 < register int    i;
794 > extern int
795 > hdkillbeam(             /* delete beam from holodeck */
796 >        register HOLO   *hp,
797 >        register int    i
798 > )
799   {
559        static BEAM     emptybeam;
800          int     nchanged;
801  
802          if (hp == NULL) {               /* clobber all holodecks */
# Line 566 | Line 806 | register int   i;
806                  return(nchanged);
807          }
808          if (i == 0) {                   /* clobber entire holodeck */
809 +                if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0))
810 +                        return(0);              /* already empty */
811                  nchanged = 0;
812 +                nchanged = 0;
813                  for (i = nbeams(hp); i > 0; i--)
814                          if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
815                                  nchanged += hdkillbeam(hp, i);
816 < #ifdef DEBUG
817 <                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
575 <                        error(CONSISTENCY, "bad beam count in hdkillbeam");
576 < #endif
816 >                DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0,
817 >                                CONSISTENCY, "bad beam count in hdkillbeam");
818                  return(nchanged);
819          }
820 < #ifdef DEBUG
821 <        if (i < 1 | i > nbeams(hp))
822 <                error(CONSISTENCY, "bad beam index to hdkillbeam");
823 < #endif
820 >        DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY,
821 >                        "bad beam index to hdkillbeam");
822 >        DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY,
823 >                        "hdkillbeam called on read-only holodeck");
824          if (hp->bl[i] != NULL) {        /* free memory */
825                  blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
826 <                free((char *)hp->bl[i]);
826 >                free((void *)hp->bl[i]);
827 >                hp->bl[i] = NULL;
828          } else
829                  nchanged = hp->bi[i].nrd;
830 <        if (hp->bi[i].nrd) {            /* free file fragment */
831 <                hp->bl[i] = &emptybeam;
832 <                hdsyncbeam(hp, i);
830 >        if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) {
831 >                biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */
832 >                hp->bi[i].nrd = 0;
833 >                hp->bi[i].fo = 0;
834 >                hdmarkdirty(hp, i);
835          }
592        hp->bl[i] = NULL;
836          return(nchanged);
837   }
838  
839  
840   int
841 < hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
842 < register HDBEAMI        *hb;            /* beam list */
843 < int     nents;                          /* current list length */
844 < int     n;                              /* maximum list length */
845 < register HOLO   *hp;                    /* section we're adding from */
841 > hdlrulist(      /* add beams from holodeck to LRU list */
842 >        register HDBEAMI        *hb,            /* beam list */
843 >        int     nents,                          /* current list length */
844 >        int     n,                              /* maximum list length */
845 >        register HOLO   *hp                     /* section we're adding from */
846 > )
847   {
848          register int    i, j;
849                                          /* insert each beam from hp */
850 <        for (i = nbeams(hp); i > 0; i--) {
850 >        for (i = 1; i <= nbeams(hp); i++) {
851                  if (hp->bl[i] == NULL)          /* check if loaded */
852                          continue;
853   #if 0
# Line 619 | Line 863 | register HOLO  *hp;                    /* section we're adding from */
863                                  hb[j].b = i;
864                                  break;
865                          }
866 <                        copystruct(hb+j, hb+(j-1));
866 >                        *(hb+j) = *(hb+(j-1));
867                  }
868          }
869          return(nents);                  /* return new list length */
# Line 627 | Line 871 | register HOLO  *hp;                    /* section we're adding from */
871  
872  
873   int
874 < hdfreecache(pct, honly)         /* free up cache space, writing changes */
875 < int     pct;                            /* maximum percentage to free */
876 < register HOLO   *honly;                 /* NULL means check all */
874 > hdfreecache(            /* free up cache space, writing changes */
875 >        int     pct,                            /* maximum percentage to free */
876 >        register HOLO   *honly                  /* NULL means check all */
877 > )
878   {
879          HDBEAMI hb[FREEBEAMS];
880          int     freetarget;
881          int     n;
882          register int    i;
883 + #ifdef DEBUG
884 +        unsigned        membefore;
885 +
886 +        membefore = hdmemuse(0);
887 + #endif
888                                                  /* compute free target */
889          freetarget = (honly != NULL) ? blglob(honly)->nrm :
890                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 655 | Line 905 | register HOLO  *honly;                 /* NULL means check all */
905                          break;
906          }
907          hdsync(honly, 0);       /* synchronize directories as necessary */
908 + #ifdef DEBUG
909 +        sprintf(errmsg,
910 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
911 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
912 +        wputs(errmsg);
913 + #endif
914          return(-freetarget);    /* return how far past goal we went */
915   }
916  
917  
918 < hddone(hp)              /* clean up holodeck section and free */
919 < register HOLO   *hp;            /* NULL means clean up all */
918 > extern void
919 > hddone(         /* clean up holodeck section and free */
920 >        register HOLO   *hp             /* NULL means clean up all */
921 > )
922   {
923          register int    i;
924  
925          if (hp == NULL) {               /* NULL means clean up everything */
926                  while (hdlist[0] != NULL)
927                          hddone(hdlist[0]);
928 +                free((void *)hdfragl);
929 +                hdfragl = NULL; nhdfragls = 0;
930                  return;
931          }
932                                          /* flush all data and free memory */
# Line 680 | Line 940 | register HOLO  *hp;            /* NULL means clean up all */
940                                  i++;
941                          break;
942                  }
943 <        free((char *)hp->bl);           /* free beam list */
944 <        free((char *)hp);               /* free holodeck struct */
943 >        free((void *)hp->bl);           /* free beam list */
944 >        free((void *)hp);               /* free holodeck struct */
945   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines