| 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     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */ | 
| 52 | < | unsigned long   hdclock;        /* clock value */ | 
| 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 */ | 
| 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   nfrags;         /* number of known fragments */ | 
| 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 >= 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 */ | 
| 87 | < | register HOLO   *hp; | 
| 88 | < | { | 
| 89 | < | 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; | 
| 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 */ | 
| 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 | < | biglob(hp)->nrd = 0;            /* count rays on disk */ | 
| 235 | < | for (n = nbeams(hp); n > 0; n--) | 
| 236 | < | biglob(hp)->nrd += hp->bi[n].nrd; | 
| 234 | > | /* start tracking fragments */ | 
| 235 | > | hdattach(fd, writable); | 
| 236 | > | /* check rays on disk */ | 
| 237 | > | fpos = hdfilen(fd); | 
| 238 | > | biglob(hp)->nrd = rtrunc = 0; | 
| 239 | > | for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) | 
| 240 | > | if (hp->bi[n].nrd) { | 
| 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) { | 
| 255 |  | hdlist[n] = hp; | 
| 256 |  | break; | 
| 257 |  | } | 
| 150 | – | /* start tracking fragments (last) */ | 
| 151 | – | hdattach(fd); | 
| 258 |  | /* all done */ | 
| 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)                      /* update directory on disk if necessary */ | 
| 268 | < | register HOLO   *hp; | 
| 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 */ | 
| 328 | > | if (hp == NULL) {               /* do all holodecks */ | 
| 329 |  | n = 0; | 
| 330 |  | for (j = 0; hdlist[j] != NULL; j++) | 
| 331 | < | n += hdsync(hdlist[j]); | 
| 331 | > | n += hdsync(hdlist[j], all); | 
| 332 |  | return(n); | 
| 333 |  | } | 
| 334 | < | if (!hp->dirty)                 /* check first */ | 
| 334 | > | /* sync the beams */ | 
| 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 | < | long | 
| 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; | 
| 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 | < | hdfiluse(fd, all)       /* compute file usage (in bytes) */ | 
| 386 | < | int     fd;                     /* open file descriptor to check */ | 
| 387 | < | int     all;                    /* include overhead and unflushed data */ | 
| 384 | > | extern off_t | 
| 385 | > | hdfilen(                /* return file length for fd */ | 
| 386 | > | int     fd | 
| 387 | > | ) | 
| 388 |  | { | 
| 389 | < | long    total = 0; | 
| 389 | > | off_t   fpos, flen; | 
| 390 | > |  | 
| 391 | > | if (fd < 0) | 
| 392 | > | return(-1); | 
| 393 | > | if (fd >= nhdfragls || !hdfragl[fd].nlinks) { | 
| 394 | > | if ((fpos = lseek(fd, (off_t)0, SEEK_CUR)) < 0) | 
| 395 | > | return(-1); | 
| 396 | > | flen = lseek(fd, (off_t)0, SEEK_END); | 
| 397 | > | lseek(fd, fpos, SEEK_SET); | 
| 398 | > | return(flen); | 
| 399 | > | } | 
| 400 | > | return(hdfragl[fd].flen); | 
| 401 | > | } | 
| 402 | > |  | 
| 403 | > |  | 
| 404 | > | extern off_t | 
| 405 | > | hdfiluse(       /* compute file usage (in bytes) */ | 
| 406 | > | int     fd                      /* open file descriptor to check */ | 
| 407 | > | ) | 
| 408 | > | { | 
| 409 | > | off_t   total = 0; | 
| 410 |  | register int    i, 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 | > | i = nbeams(hdlist[j]); | 
| 417 | > | total += i*sizeof(BEAMI) + sizeof(HDGRID); | 
| 418 | > | for ( ; 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); | 
| 231 | – | total += sizeof(HDGRID) + | 
| 232 | – | nbeams(hdlist[j])*sizeof(BEAMI); | 
| 233 | – | } | 
| 423 |  | } | 
| 424 |  | return(total);          /* does not include fragments */ | 
| 425 |  | } | 
| 426 |  |  | 
| 427 |  |  | 
| 428 | < | RAYVAL * | 
| 429 | < | hdnewrays(hp, i, nr)    /* allocate space for add'l rays and return pointer */ | 
| 430 | < | register HOLO   *hp; | 
| 431 | < | register int    i; | 
| 432 | < | int     nr;                     /* number of new rays desired */ | 
| 428 | > | extern RAYVAL * | 
| 429 | > | hdnewrays(      /* allocate space for add'l rays and return pointer */ | 
| 430 | > | register HOLO   *hp, | 
| 431 | > | register int    i, | 
| 432 | > | int     nr                      /* number of new rays desired */ | 
| 433 | > | ) | 
| 434 |  | { | 
| 435 |  | RAYVAL  *p; | 
| 436 |  | int     n; | 
| 437 |  |  | 
| 438 | < | if (nr <= 0) | 
| 439 | < | return(NULL); | 
| 440 | < | if (i < 1 | i > nbeams(hp)) | 
| 251 | < | error(CONSISTENCY, "bad beam index given to hdnewrays"); | 
| 438 | > | if (nr <= 0) return(NULL); | 
| 439 | > | CHECK((i < 1) | (i > nbeams(hp)), | 
| 440 | > | CONSISTENCY, "bad beam index given to hdnewrays"); | 
| 441 |  | if (hp->bl[i] != NULL) | 
| 442 |  | hp->bl[i]->tick = hdclock;      /* preempt swap */ | 
| 443 |  | if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) | 
| 444 |  | hdfreecache(PCTFREE, NULL);     /* free some space */ | 
| 256 | – | errno = 0; | 
| 445 |  | if (hp->bl[i] == NULL) {                /* allocate (and load) */ | 
| 446 |  | n = hp->bi[i].nrd + nr; | 
| 447 | < | if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) | 
| 260 | < | goto memerr; | 
| 447 | > | hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); | 
| 448 |  | blglob(hp)->nrm += n; | 
| 449 | < | if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { | 
| 450 | < | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 449 | > | if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { | 
| 450 | > | errno = 0; | 
| 451 | > | if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) | 
| 452 |  | error(SYSTEM, "seek error on holodeck file"); | 
| 453 |  | n *= sizeof(RAYVAL); | 
| 454 |  | if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 456 |  | "error reading beam from holodeck file"); | 
| 457 |  | } | 
| 458 |  | } else {                                /* just grow in memory */ | 
| 459 | < | hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], | 
| 460 | < | hdbsiz(hp->bl[i]->nrm + nr) ); | 
| 273 | < | if (hp->bl[i] == NULL) | 
| 274 | < | goto memerr; | 
| 459 | > | hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], | 
| 460 | > | hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); | 
| 461 |  | blglob(hp)->nrm += nr; | 
| 462 |  | } | 
| 463 | + | if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) | 
| 464 | + | hdfreefrag(hp, i);              /* relinquish old fragment */ | 
| 465 |  | p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; | 
| 466 |  | hp->bl[i]->nrm += nr;                   /* update in-core structure */ | 
| 467 | < | bzero((char *)p, nr*sizeof(RAYVAL)); | 
| 468 | < | hp->bl[i]->tick = ++hdclock;            /* update LRU clock */ | 
| 281 | < | blglob(hp)->tick = hdclock; | 
| 467 | > | memset((void *)p, '\0', nr*sizeof(RAYVAL)); | 
| 468 | > | blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 469 |  | return(p);                              /* point to new rays */ | 
| 283 | – | memerr: | 
| 284 | – | error(SYSTEM, "out of memory in hdnewrays"); | 
| 470 |  | } | 
| 471 |  |  | 
| 472 |  |  | 
| 473 | < | BEAM * | 
| 474 | < | hdgetbeam(hp, i)        /* get beam (from file if necessary) */ | 
| 475 | < | register HOLO   *hp; | 
| 476 | < | register int    i; | 
| 473 | > | extern BEAM * | 
| 474 | > | hdgetbeam(      /* get beam (from file if necessary) */ | 
| 475 | > | register HOLO   *hp, | 
| 476 | > | register int    i | 
| 477 | > | ) | 
| 478 |  | { | 
| 479 |  | register int    n; | 
| 480 |  |  | 
| 481 | < | if (i < 1 | i > nbeams(hp)) | 
| 482 | < | error(CONSISTENCY, "bad beam index given to hdgetbeam"); | 
| 481 | > | CHECK((i < 1) | (i > nbeams(hp)), | 
| 482 | > | CONSISTENCY, "bad beam index given to hdgetbeam"); | 
| 483 |  | if (hp->bl[i] == NULL) {                /* load from disk */ | 
| 484 |  | if (!(n = hp->bi[i].nrd)) | 
| 485 |  | return(NULL); | 
| 486 |  | if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) | 
| 487 |  | hdfreecache(PCTFREE, NULL);     /* get free space */ | 
| 488 | < | errno = 0; | 
| 303 | < | if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) | 
| 304 | < | error(SYSTEM, "cannot allocate memory for beam"); | 
| 488 | > | hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); | 
| 489 |  | blglob(hp)->nrm += hp->bl[i]->nrm = n; | 
| 490 | < | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 490 | > | errno = 0; | 
| 491 | > | if (lseek(hp->fd, hp->bi[i].fo, SEEK_SET) < 0) | 
| 492 |  | error(SYSTEM, "seek error on holodeck file"); | 
| 493 |  | n *= sizeof(RAYVAL); | 
| 494 |  | if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 495 |  | error(SYSTEM, "error reading beam from holodeck file"); | 
| 496 | + | if (hdfragflags&FF_READ) | 
| 497 | + | hdfreefrag(hp, i);      /* relinquish old frag. */ | 
| 498 |  | } | 
| 499 | < | hp->bl[i]->tick = ++hdclock;    /* update LRU clock */ | 
| 313 | < | blglob(hp)->tick = hdclock; | 
| 499 | > | blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 500 |  | return(hp->bl[i]); | 
| 501 |  | } | 
| 502 |  |  | 
| 503 |  |  | 
| 504 |  | int | 
| 505 | < | hdgetbi(hp, i)                  /* allocate a file fragment */ | 
| 506 | < | register HOLO   *hp; | 
| 507 | < | register int    i; | 
| 505 | > | hdfilord(       /* order beams for quick loading */ | 
| 506 | > | register const void     *hb1, | 
| 507 | > | register const void     *hb2 | 
| 508 | > | ) | 
| 509 |  | { | 
| 510 | < | int     nrays = hp->bl[i]->nrm; | 
| 510 | > | register off_t  c; | 
| 511 | > | /* residents go first */ | 
| 512 | > | if (((HDBEAMI*)hb2)->h->bl[((HDBEAMI*)hb2)->b] != NULL) | 
| 513 | > | return(((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] == NULL); | 
| 514 | > | if (((HDBEAMI*)hb1)->h->bl[((HDBEAMI*)hb1)->b] != NULL) | 
| 515 | > | return(-1); | 
| 516 | > | /* otherwise sort by file descriptor */ | 
| 517 | > | if (((HDBEAMI*)hb1)->h->fd != ((HDBEAMI*)hb2)->h->fd) | 
| 518 | > | return(((HDBEAMI*)hb1)->h->fd - ((HDBEAMI*)hb2)->h->fd); | 
| 519 | > | /* then by position in file */ | 
| 520 | > | c = ((HDBEAMI*)hb1)->h->bi[((HDBEAMI*)hb1)->b].fo | 
| 521 | > | - ((HDBEAMI*)hb2)->h->bi[((HDBEAMI*)hb2)->b].fo; | 
| 522 | > | return(c > 0 ? 1 : c < 0 ? -1 : 0); | 
| 523 | > | } | 
| 524 |  |  | 
| 325 | – | if (hp->bi[i].nrd == nrays)     /* current one will do? */ | 
| 326 | – | return(0); | 
| 525 |  |  | 
| 526 | < | if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ | 
| 527 | < | hp->bi[i].fo = lseek(hp->fd, 0L, 2); | 
| 526 | > | extern void | 
| 527 | > | hdloadbeams(    /* load a list of beams in optimal order */ | 
| 528 | > | register HDBEAMI        *hb,    /* list gets sorted by hdfilord() */ | 
| 529 | > | int     n,                      /* list length */ | 
| 530 | > | void    (*bf)(BEAM *bp, HDBEAMI *hb)    /* callback function (optional) */ | 
| 531 | > | ) | 
| 532 | > | { | 
| 533 | > | unsigned        origcachesize, memuse; | 
| 534 | > | int     bytesloaded, needbytes, bytes2free; | 
| 535 | > | register BEAM   *bp; | 
| 536 | > | register int    i; | 
| 537 | > | /* precheck consistency */ | 
| 538 | > | if (n <= 0) return; | 
| 539 | > | for (i = n; i--; ) | 
| 540 | > | if (hb[i].h==NULL || (hb[i].b<1) | (hb[i].b>nbeams(hb[i].h))) | 
| 541 | > | error(CONSISTENCY, "bad beam in hdloadbeams"); | 
| 542 | > | /* sort list for optimal access */ | 
| 543 | > | qsort((void *)hb, n, sizeof(HDBEAMI), hdfilord); | 
| 544 | > | bytesloaded = 0;                /* run through loaded beams */ | 
| 545 | > | for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { | 
| 546 | > | bp->tick = hdclock;     /* preempt swap */ | 
| 547 | > | bytesloaded += bp->nrm; | 
| 548 | > | if (bf != NULL) | 
| 549 | > | (*bf)(bp, hb); | 
| 550 | > | } | 
| 551 | > | bytesloaded *= sizeof(RAYVAL); | 
| 552 | > | if ((origcachesize = hdcachesize) > 0) { | 
| 553 | > | needbytes = 0;          /* figure out memory needs */ | 
| 554 | > | for (i = n; i--; ) | 
| 555 | > | needbytes += hb[i].h->bi[hb[i].b].nrd; | 
| 556 | > | needbytes *= sizeof(RAYVAL); | 
| 557 | > | do {                            /* free enough memory */ | 
| 558 | > | memuse = hdmemuse(0); | 
| 559 | > | bytes2free = needbytes - (int)(hdcachesize-memuse); | 
| 560 | > | if (bytes2free > (int)(memuse - bytesloaded)) | 
| 561 | > | bytes2free = memuse - bytesloaded; | 
| 562 | > | } while (bytes2free > 0 && | 
| 563 | > | hdfreecache(100*bytes2free/memuse, NULL) < 0); | 
| 564 | > | hdcachesize = 0;                /* load beams w/o swap */ | 
| 565 | > | } | 
| 566 | > | for (i = 0; i < n; i++) | 
| 567 | > | if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) | 
| 568 | > | (*bf)(bp, hb+i); | 
| 569 | > | hdcachesize = origcachesize;    /* resume dynamic swapping */ | 
| 570 | > | } | 
| 571 |  |  | 
| 331 | – | else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == | 
| 332 | – | hdfrag[hp->fd].flen)            /* EOF special case */ | 
| 333 | – | hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); | 
| 572 |  |  | 
| 573 | < | else {                                          /* general case */ | 
| 574 | < | register struct fragment        *f = &hdfrag[hp->fd]; | 
| 575 | < | register int    j, k; | 
| 576 | < | /* relinquish old fragment */ | 
| 577 | < | if (hp->bi[i].nrd) { | 
| 578 | < | j = f->nfrags++; | 
| 579 | < | #ifdef MAXFRAG | 
| 580 | < | if (j >= MAXFRAG-1) | 
| 581 | < | f->nfrags--; | 
| 573 | > | extern int | 
| 574 | > | hdfreefrag(                     /* free a file fragment */ | 
| 575 | > | HOLO    *hp, | 
| 576 | > | int     i | 
| 577 | > | ) | 
| 578 | > | { | 
| 579 | > | register BEAMI  *bi = &hp->bi[i]; | 
| 580 | > | register struct fraglist        *f; | 
| 581 | > | register int    j, k; | 
| 582 | > |  | 
| 583 | > | if (bi->nrd <= 0) | 
| 584 | > | return(0); | 
| 585 | > | DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, | 
| 586 | > | CONSISTENCY, "bad file descriptor in hdfreefrag"); | 
| 587 | > | f = &hdfragl[hp->fd]; | 
| 588 | > | if (!f->writable) | 
| 589 | > | return(0); | 
| 590 | > | if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ | 
| 591 | > | for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 592 | > | while (f->fi[k].nrd == 0) | 
| 593 | > | if (++k >= f->nfrags) | 
| 594 | > | goto endloop; | 
| 595 | > | if (k > j) | 
| 596 | > | *(f->fi+j) = *(f->fi+k); | 
| 597 | > | } | 
| 598 | > | endloop: | 
| 599 | > | f->nfrags = j; | 
| 600 | > | } | 
| 601 | > | j = f->nfrags++;                /* allocate a slot in free list */ | 
| 602 | > | #if MAXFRAGB | 
| 603 | > | if (j >= MAXFRAGB*FRAGBLK) { | 
| 604 | > | f->nfrags = j--;        /* stop list growth */ | 
| 605 | > | if (bi->nrd <= f->fi[j].nrd) | 
| 606 | > | return(0);      /* new one no better than discard */ | 
| 607 | > | } | 
| 608 |  | #endif | 
| 609 | < | if (j % FRAGBLK == 0) {         /* more frag. space */ | 
| 610 | < | if (f->fi == NULL) | 
| 611 | < | f->fi = (BEAMI *)malloc( | 
| 612 | < | FRAGBLK*sizeof(BEAMI)); | 
| 613 | < | else | 
| 614 | < | f->fi = (BEAMI *)realloc((char *)f->fi, | 
| 615 | < | (j+FRAGBLK)*sizeof(BEAMI)); | 
| 616 | < | if (f->fi == NULL) | 
| 617 | < | error(SYSTEM, | 
| 618 | < | "out of memory in hdgetbi"); | 
| 619 | < | } | 
| 620 | < | for ( ; ; j--) {        /* insert in descending list */ | 
| 621 | < | if (!j || hp->bi[i].fo < f->fi[j-1].fo) { | 
| 622 | < | f->fi[j].fo = hp->bi[i].fo; | 
| 623 | < | f->fi[j].nrd = hp->bi[i].nrd; | 
| 624 | < | break; | 
| 625 | < | } | 
| 626 | < | copystruct(f->fi+j, f->fi+j-1); | 
| 627 | < | } | 
| 609 | > | if (j % FRAGBLK == 0) {         /* more (or less) free list space */ | 
| 610 | > | register BEAMI  *newp; | 
| 611 | > | if (f->fi == NULL) | 
| 612 | > | newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); | 
| 613 | > | else | 
| 614 | > | newp = (BEAMI *)realloc((void *)f->fi, | 
| 615 | > | (j+FRAGBLK)*sizeof(BEAMI)); | 
| 616 | > | if (newp == NULL) { | 
| 617 | > | f->nfrags--;    /* graceful failure */ | 
| 618 | > | return(0); | 
| 619 | > | } | 
| 620 | > | f->fi = newp; | 
| 621 | > | } | 
| 622 | > | for ( ; ; j--) {                /* insert in descending list */ | 
| 623 | > | if (!j || bi->fo < f->fi[j-1].fo) { | 
| 624 | > | f->fi[j].fo = bi->fo; | 
| 625 | > | f->fi[j].nrd = bi->nrd; | 
| 626 | > | break; | 
| 627 | > | } | 
| 628 | > | *(f->fi+j) = *(f->fi+(j-1)); | 
| 629 | > | } | 
| 630 |  | /* coalesce adjacent fragments */ | 
| 631 | < | for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 632 | < | if (k > j) | 
| 633 | < | copystruct(f->fi+j, f->fi+k); | 
| 634 | < | while (k+1 < f->nfrags && f->fi[k+1].fo + | 
| 635 | < | f->fi[k+1].nrd*sizeof(RAYVAL) | 
| 636 | < | == f->fi[j].fo) { | 
| 637 | < | f->fi[j].fo -= | 
| 638 | < | f->fi[++k].nrd*sizeof(RAYVAL); | 
| 639 | < | f->fi[j].nrd += f->fi[k].nrd; | 
| 640 | < | } | 
| 631 | > | /* successors never empty */ | 
| 632 | > | if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { | 
| 633 | > | f->fi[j].nrd += f->fi[j-1].nrd; | 
| 634 | > | f->fi[j-1].nrd = 0; | 
| 635 | > | } | 
| 636 | > | for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */ | 
| 637 | > | if (f->fi[k].nrd) { | 
| 638 | > | if (f->fi[j].fo == f->fi[k].fo + | 
| 639 | > | f->fi[k].nrd*sizeof(RAYVAL)) { | 
| 640 | > | f->fi[k].nrd += f->fi[j].nrd; | 
| 641 | > | f->fi[j].nrd = 0; | 
| 642 |  | } | 
| 643 | < | f->nfrags = j; | 
| 643 | > | break; | 
| 644 |  | } | 
| 645 | < | k = -1;                 /* find closest-sized fragment */ | 
| 646 | < | for (j = f->nfrags; j-- > 0; ) | 
| 647 | < | if (f->fi[j].nrd >= nrays && | 
| 648 | < | (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 649 | < | if (f->fi[k=j].nrd == nrays) | 
| 650 | < | break; | 
| 651 | < | if (k < 0) {            /* no fragment -- extend file */ | 
| 652 | < | hp->bi[i].fo = f->flen; | 
| 653 | < | f->flen += nrays*sizeof(RAYVAL); | 
| 654 | < | } else {                /* else use fragment */ | 
| 655 | < | hp->bi[i].fo = f->fi[k].fo; | 
| 656 | < | if (f->fi[k].nrd == nrays) {    /* delete fragment */ | 
| 657 | < | f->nfrags--; | 
| 658 | < | for (j = k; j < f->nfrags; j++) | 
| 659 | < | copystruct(f->fi+j, f->fi+j+1); | 
| 660 | < | } else {                        /* else shrink it */ | 
| 661 | < | f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 662 | < | f->fi[k].nrd -= nrays; | 
| 663 | < | } | 
| 664 | < | } | 
| 645 | > | biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */ | 
| 646 | > | bi->nrd = 0; | 
| 647 | > | bi->fo = 0; | 
| 648 | > | hdmarkdirty(hp, i);                     /* assume we'll reallocate */ | 
| 649 | > | return(1); | 
| 650 | > | } | 
| 651 | > |  | 
| 652 | > |  | 
| 653 | > | extern int | 
| 654 | > | hdfragOK(       /* get fragment list status for file */ | 
| 655 | > | int     fd, | 
| 656 | > | int     *listlen, | 
| 657 | > | register int32  *listsiz | 
| 658 | > | ) | 
| 659 | > | { | 
| 660 | > | register struct fraglist        *f; | 
| 661 | > | register int    i; | 
| 662 | > |  | 
| 663 | > | if ((fd < 0) | (fd >= nhdfragls) || !(f = &hdfragl[fd])->nlinks) | 
| 664 | > | return(0);              /* listless */ | 
| 665 | > | if (listlen != NULL) | 
| 666 | > | *listlen = f->nfrags; | 
| 667 | > | if (listsiz != NULL) | 
| 668 | > | for (i = f->nfrags, *listsiz = 0; i--; ) | 
| 669 | > | *listsiz += f->fi[i].nrd; | 
| 670 | > | #if MAXFRAGB | 
| 671 | > | return(f->nfrags < MAXFRAGB*FRAGBLK); | 
| 672 | > | #else | 
| 673 | > | return(1);                      /* list never fills up */ | 
| 674 | > | #endif | 
| 675 | > | } | 
| 676 | > |  | 
| 677 | > |  | 
| 678 | > | off_t | 
| 679 | > | hdallocfrag(            /* allocate a file fragment */ | 
| 680 | > | int     fd, | 
| 681 | > | uint32  nrays | 
| 682 | > | ) | 
| 683 | > | { | 
| 684 | > | register struct fraglist        *f; | 
| 685 | > | register int    j; | 
| 686 | > | off_t   nfo; | 
| 687 | > |  | 
| 688 | > | if (nrays == 0) | 
| 689 | > | return(-1L); | 
| 690 | > | DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, | 
| 691 | > | CONSISTENCY, "bad file descriptor in hdallocfrag"); | 
| 692 | > | f = &hdfragl[fd]; | 
| 693 | > | for (j = f->nfrags; j-- > 0; )  /* first fit algorithm */ | 
| 694 | > | if (f->fi[j].nrd >= nrays) | 
| 695 | > | break; | 
| 696 | > | if (j < 0) {                    /* no fragment -- extend file */ | 
| 697 | > | nfo = f->flen; | 
| 698 | > | f->flen += nrays*sizeof(RAYVAL); | 
| 699 | > | } else {                        /* else use fragment */ | 
| 700 | > | nfo = f->fi[j].fo; | 
| 701 | > | f->fi[j].fo += nrays*sizeof(RAYVAL); | 
| 702 | > | f->fi[j].nrd -= nrays; | 
| 703 |  | } | 
| 704 | + | return(nfo); | 
| 705 | + | } | 
| 706 | + |  | 
| 707 | + |  | 
| 708 | + | int | 
| 709 | + | hdsyncbeam(             /* sync beam in memory with beam on disk */ | 
| 710 | + | register HOLO   *hp, | 
| 711 | + | register int    i | 
| 712 | + | ) | 
| 713 | + | { | 
| 714 | + | int     fragfreed; | 
| 715 | + | uint32  nrays; | 
| 716 | + | unsigned int    n; | 
| 717 | + | off_t   nfo; | 
| 718 | + | /* check file status */ | 
| 719 | + | if (hdfragl[hp->fd].writable <= 0) | 
| 720 | + | return(hdfragl[hp->fd].writable); | 
| 721 | + | DCHECK(i < 1 | i > nbeams(hp), | 
| 722 | + | CONSISTENCY, "bad beam index in hdsyncbeam"); | 
| 723 | + | /* is current fragment OK? */ | 
| 724 | + | if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) | 
| 725 | + | return(0); | 
| 726 | + | /* relinquish old fragment? */ | 
| 727 | + | fragfreed = hdfragflags&FF_WRITE && hp->bi[i].nrd && hdfreefrag(hp,i); | 
| 728 | + | if (nrays) {                    /* get and write new fragment */ | 
| 729 | + | nfo = hdallocfrag(hp->fd, nrays); | 
| 730 | + | errno = 0; | 
| 731 | + | if (lseek(hp->fd, nfo, SEEK_SET) < 0) | 
| 732 | + | error(SYSTEM, "cannot seek on holodeck file"); | 
| 733 | + | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 734 | + | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { | 
| 735 | + | hdfragl[hp->fd].writable = -1; | 
| 736 | + | hdsync(NULL, 0);        /* sync directories */ | 
| 737 | + | error(SYSTEM, "write error in hdsyncbeam"); | 
| 738 | + | } | 
| 739 | + | hp->bi[i].fo = nfo; | 
| 740 | + | } else | 
| 741 | + | hp->bi[i].fo = 0; | 
| 742 |  | biglob(hp)->nrd += nrays - hp->bi[i].nrd; | 
| 743 |  | hp->bi[i].nrd = nrays; | 
| 744 | < | markdirty(hp);          /* section directory now out of date */ | 
| 744 | > | if (!fragfreed) | 
| 745 | > | hdmarkdirty(hp, i);             /* need to flag dir. ent. */ | 
| 746 |  | return(1); | 
| 747 |  | } | 
| 748 |  |  | 
| 749 |  |  | 
| 750 | < | int | 
| 751 | < | hdfreebeam(hp, i)               /* free beam, writing if dirty */ | 
| 752 | < | register HOLO   *hp; | 
| 753 | < | register int    i; | 
| 750 | > | extern int | 
| 751 | > | hdfreebeam(             /* free beam, writing if dirty */ | 
| 752 | > | register HOLO   *hp, | 
| 753 | > | register int    i | 
| 754 | > | ) | 
| 755 |  | { | 
| 756 | < | int     nchanged, n; | 
| 756 | > | int     nchanged; | 
| 757 |  |  | 
| 758 |  | if (hp == NULL) {               /* clear all holodecks */ | 
| 759 |  | nchanged = 0; | 
| 761 |  | nchanged += hdfreebeam(hdlist[i], 0); | 
| 762 |  | return(nchanged); | 
| 763 |  | } | 
| 764 | + | if (hdfragl[hp->fd].writable < 0)       /* check for file error */ | 
| 765 | + | return(0); | 
| 766 |  | if (i == 0) {                   /* clear entire holodeck */ | 
| 767 | + | if (blglob(hp)->nrm == 0) | 
| 768 | + | return(0);              /* already clear */ | 
| 769 |  | nchanged = 0; | 
| 770 | < | for (i = 1; i <= nbeams(hp); i++) | 
| 771 | < | nchanged += hdfreebeam(hp, i); | 
| 770 | > | for (i = nbeams(hp); i > 0; i--) | 
| 771 | > | if (hp->bl[i] != NULL) | 
| 772 | > | nchanged += hdfreebeam(hp, i); | 
| 773 | > | DCHECK(blglob(hp)->nrm != 0, | 
| 774 | > | CONSISTENCY, "bad beam count in hdfreebeam"); | 
| 775 |  | return(nchanged); | 
| 776 |  | } | 
| 777 | < | if (i < 1 | i > nbeams(hp)) | 
| 778 | < | error(CONSISTENCY, "bad beam index to hdfreebeam"); | 
| 777 | > | DCHECK(i < 1 | i > nbeams(hp), | 
| 778 | > | CONSISTENCY, "bad beam index to hdfreebeam"); | 
| 779 |  | if (hp->bl[i] == NULL) | 
| 780 |  | return(0); | 
| 781 |  | /* check for additions */ | 
| 782 |  | nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; | 
| 783 | < | if (nchanged) { | 
| 784 | < | hdgetbi(hp, i);                 /* allocate a file position */ | 
| 433 | < | errno = 0; | 
| 434 | < | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 435 | < | error(SYSTEM, "cannot seek on holodeck file"); | 
| 436 | < | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 437 | < | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 438 | < | error(SYSTEM, "write error in hdfreebeam"); | 
| 439 | < | } | 
| 783 | > | if (nchanged) | 
| 784 | > | hdsyncbeam(hp, i);              /* write new fragment */ | 
| 785 |  | blglob(hp)->nrm -= hp->bl[i]->nrm; | 
| 786 | < | free((char *)hp->bl[i]);                /* free memory */ | 
| 786 | > | free((void *)hp->bl[i]);                /* free memory */ | 
| 787 |  | hp->bl[i] = NULL; | 
| 788 |  | return(nchanged); | 
| 789 |  | } | 
| 790 |  |  | 
| 791 |  |  | 
| 792 | < | hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */ | 
| 793 | < | register HOLO   *ha[];                  /* section list (NULL terminated) */ | 
| 794 | < | register int    ba[];                   /* beam index to go with section */ | 
| 795 | < | int     n;                              /* length of arrays minus 1 */ | 
| 796 | < | register HOLO   *hp;                    /* section we're adding from */ | 
| 792 | > | extern int | 
| 793 | > | hdkillbeam(             /* delete beam from holodeck */ | 
| 794 | > | register HOLO   *hp, | 
| 795 | > | register int    i | 
| 796 | > | ) | 
| 797 |  | { | 
| 798 | + | int     nchanged; | 
| 799 | + |  | 
| 800 | + | if (hp == NULL) {               /* clobber all holodecks */ | 
| 801 | + | nchanged = 0; | 
| 802 | + | for (i = 0; hdlist[i] != NULL; i++) | 
| 803 | + | nchanged += hdkillbeam(hdlist[i], 0); | 
| 804 | + | return(nchanged); | 
| 805 | + | } | 
| 806 | + | if (i == 0) {                   /* clobber entire holodeck */ | 
| 807 | + | if ((biglob(hp)->nrd == 0) & (blglob(hp)->nrm == 0)) | 
| 808 | + | return(0);              /* already empty */ | 
| 809 | + | nchanged = 0; | 
| 810 | + | nchanged = 0; | 
| 811 | + | for (i = nbeams(hp); i > 0; i--) | 
| 812 | + | if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) | 
| 813 | + | nchanged += hdkillbeam(hp, i); | 
| 814 | + | DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, | 
| 815 | + | CONSISTENCY, "bad beam count in hdkillbeam"); | 
| 816 | + | return(nchanged); | 
| 817 | + | } | 
| 818 | + | DCHECK(i < 1 | i > nbeams(hp), CONSISTENCY, | 
| 819 | + | "bad beam index to hdkillbeam"); | 
| 820 | + | DCHECK(!hdfragl[hp->fd].writable, CONSISTENCY, | 
| 821 | + | "hdkillbeam called on read-only holodeck"); | 
| 822 | + | if (hp->bl[i] != NULL) {        /* free memory */ | 
| 823 | + | blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; | 
| 824 | + | free((void *)hp->bl[i]); | 
| 825 | + | hp->bl[i] = NULL; | 
| 826 | + | } else | 
| 827 | + | nchanged = hp->bi[i].nrd; | 
| 828 | + | if (hp->bi[i].nrd && !(hdfragflags&FF_KILL && hdfreefrag(hp,i))) { | 
| 829 | + | biglob(hp)->nrd -= hp->bi[i].nrd;       /* free failed */ | 
| 830 | + | hp->bi[i].nrd = 0; | 
| 831 | + | hp->bi[i].fo = 0; | 
| 832 | + | hdmarkdirty(hp, i); | 
| 833 | + | } | 
| 834 | + | return(nchanged); | 
| 835 | + | } | 
| 836 | + |  | 
| 837 | + |  | 
| 838 | + | int | 
| 839 | + | hdlrulist(      /* add beams from holodeck to LRU list */ | 
| 840 | + | register HDBEAMI        *hb,            /* beam list */ | 
| 841 | + | int     nents,                          /* current list length */ | 
| 842 | + | int     n,                              /* maximum list length */ | 
| 843 | + | register HOLO   *hp                     /* section we're adding from */ | 
| 844 | + | ) | 
| 845 | + | { | 
| 846 |  | register int    i, j; | 
| 454 | – | int     nents; | 
| 455 | – | /* find last entry in LRU list */ | 
| 456 | – | for (j = 0; ha[j] != NULL; j++) | 
| 457 | – | ; | 
| 458 | – | nents = j; | 
| 847 |  | /* insert each beam from hp */ | 
| 848 | < | for (i = nbeams(hp); i > 0; i-- ) { | 
| 848 | > | for (i = 1; i <= nbeams(hp); i++) { | 
| 849 |  | if (hp->bl[i] == NULL)          /* check if loaded */ | 
| 850 |  | continue; | 
| 851 | < | if ((j = ++nents) > n)          /* grow list if we can */ | 
| 851 | > | #if 0 | 
| 852 | > | if (hp->bl[i]->tick == hdclock) /* preempt swap? */ | 
| 853 | > | continue; | 
| 854 | > | #endif | 
| 855 | > | if ((j = ++nents) >= n)         /* grow list if we can */ | 
| 856 |  | nents--; | 
| 857 |  | for ( ; ; ) {                   /* bubble into place */ | 
| 858 |  | if (!--j || hp->bl[i]->tick >= | 
| 859 | < | ha[j-1]->bl[ba[j-1]]->tick) { | 
| 860 | < | ha[j] = hp; | 
| 861 | < | ba[j] = i; | 
| 859 | > | hb[j-1].h->bl[hb[j-1].b]->tick) { | 
| 860 | > | hb[j].h = hp; | 
| 861 | > | hb[j].b = i; | 
| 862 |  | break; | 
| 863 |  | } | 
| 864 | < | ha[j] = ha[j-1]; | 
| 473 | < | ba[j] = ba[j-1]; | 
| 864 | > | *(hb+j) = *(hb+(j-1)); | 
| 865 |  | } | 
| 866 |  | } | 
| 867 | < | ha[nents] = NULL;               /* all done */ | 
| 477 | < | ba[nents] = 0; | 
| 867 | > | return(nents);                  /* return new list length */ | 
| 868 |  | } | 
| 869 |  |  | 
| 870 |  |  | 
| 871 | < | hdfreecache(pct, honly)         /* free up cache space, writing changes */ | 
| 872 | < | int     pct;                            /* maximum percentage to free */ | 
| 873 | < | register HOLO   *honly;                 /* NULL means check all */ | 
| 871 | > | int | 
| 872 | > | hdfreecache(            /* free up cache space, writing changes */ | 
| 873 | > | int     pct,                            /* maximum percentage to free */ | 
| 874 | > | register HOLO   *honly                  /* NULL means check all */ | 
| 875 | > | ) | 
| 876 |  | { | 
| 877 | < | HOLO    *hp[FREEBEAMS+1]; | 
| 486 | < | int     bn[FREEBEAMS+1]; | 
| 877 | > | HDBEAMI hb[FREEBEAMS]; | 
| 878 |  | int     freetarget; | 
| 879 | + | int     n; | 
| 880 |  | register int    i; | 
| 881 | + | #ifdef DEBUG | 
| 882 | + | unsigned        membefore; | 
| 883 | + |  | 
| 884 | + | membefore = hdmemuse(0); | 
| 885 | + | #endif | 
| 886 |  | /* compute free target */ | 
| 887 |  | freetarget = (honly != NULL) ? blglob(honly)->nrm : | 
| 888 |  | hdmemuse(0)/sizeof(RAYVAL) ; | 
| 889 |  | freetarget = freetarget*pct/100; | 
| 890 | + | if (freetarget <= 0) | 
| 891 | + | return(0); | 
| 892 |  | /* find least recently used */ | 
| 893 | < | hp[0] = NULL; | 
| 495 | < | bn[0] = 0; | 
| 893 | > | n = 0; | 
| 894 |  | if (honly != NULL) | 
| 895 | < | hdlrulist(hp, bn, FREEBEAMS, honly); | 
| 895 | > | n = hdlrulist(hb, n, FREEBEAMS, honly); | 
| 896 |  | else | 
| 897 |  | for (i = 0; hdlist[i] != NULL; i++) | 
| 898 | < | hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); | 
| 898 | > | n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); | 
| 899 |  | /* free LRU beams */ | 
| 900 | < | for (i = 0; hp[i] != NULL; i++) { | 
| 901 | < | hdfreebeam(hp[i], bn[i]); | 
| 902 | < | if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) | 
| 900 | > | for (i = 0; i < n; i++) { | 
| 901 | > | hdfreebeam(hb[i].h, hb[i].b); | 
| 902 | > | if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) | 
| 903 |  | break; | 
| 904 |  | } | 
| 905 | < | hdsync(honly);          /* synchronize directories as necessary */ | 
| 905 | > | hdsync(honly, 0);       /* synchronize directories as necessary */ | 
| 906 | > | #ifdef DEBUG | 
| 907 | > | sprintf(errmsg, | 
| 908 | > | "%dK before, %dK after hdfreecache (%dK total), %d rays short\n", | 
| 909 | > | membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); | 
| 910 | > | wputs(errmsg); | 
| 911 | > | #endif | 
| 912 | > | return(-freetarget);    /* return how far past goal we went */ | 
| 913 |  | } | 
| 914 |  |  | 
| 915 |  |  | 
| 916 | < | hddone(hp)              /* clean up holodeck section and free */ | 
| 917 | < | register HOLO   *hp;            /* NULL means clean up all */ | 
| 916 | > | extern void | 
| 917 | > | hddone(         /* clean up holodeck section and free */ | 
| 918 | > | register HOLO   *hp             /* NULL means clean up all */ | 
| 919 | > | ) | 
| 920 |  | { | 
| 921 |  | register int    i; | 
| 922 |  |  | 
| 923 |  | if (hp == NULL) {               /* NULL means clean up everything */ | 
| 924 |  | while (hdlist[0] != NULL) | 
| 925 |  | hddone(hdlist[0]); | 
| 926 | + | free((void *)hdfragl); | 
| 927 | + | hdfragl = NULL; nhdfragls = 0; | 
| 928 |  | return; | 
| 929 |  | } | 
| 930 |  | /* flush all data and free memory */ | 
| 938 |  | i++; | 
| 939 |  | break; | 
| 940 |  | } | 
| 941 | < | free((char *)hp->bl);           /* free beam list */ | 
| 942 | < | free((char *)hp);               /* free holodeck struct */ | 
| 941 | > | free((void *)hp->bl);           /* free beam list */ | 
| 942 | > | free((void *)hp);               /* free holodeck struct */ | 
| 943 |  | } |