| 1 | < | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | > | /* Copyright (c) 1999 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 16 |  | #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */ | 
| 17 |  | #endif | 
| 18 |  | #ifndef FREEBEAMS | 
| 19 | < | #define FREEBEAMS       1024    /* maximum beams to free at a time */ | 
| 19 | > | #define FREEBEAMS       1500    /* maximum beams to free at a time */ | 
| 20 |  | #endif | 
| 21 |  | #ifndef PCTFREE | 
| 22 | < | #define PCTFREE         12      /* maximum fraction to free (%) */ | 
| 22 | > | #define PCTFREE         15      /* maximum fraction to free (%) */ | 
| 23 |  | #endif | 
| 24 | < | #ifndef FFDMAX | 
| 25 | < | #define FFDMAX          64      /* max. file descriptor for tracking */ | 
| 24 | > | #ifndef MAXFRAGB | 
| 25 | > | #define MAXFRAGB        16      /* fragment blocks/file to track (0==inf) */ | 
| 26 |  | #endif | 
| 27 | < | #ifndef MAXFRAG | 
| 28 | < | #define MAXFRAG         2048    /* maximum fragments tracked in each file */ | 
| 27 | > | #ifndef FF_DEFAULT | 
| 28 | > | /* when to free a beam fragment */ | 
| 29 | > | #define FF_DEFAULT      (FF_ALLOC|FF_WRITE|FF_KILL) | 
| 30 |  | #endif | 
| 31 | + | #ifndef MINDIRSEL | 
| 32 | + | /* minimum directory seek length */ | 
| 33 | + | #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI)) | 
| 34 | + | #endif | 
| 35 |  |  | 
| 36 | < | int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */ | 
| 37 | < | unsigned long   hdclock;        /* clock value */ | 
| 36 | > | #ifndef BSD | 
| 37 | > | #define write   writebuf        /* safe i/o routines */ | 
| 38 | > | #define read    readbuf | 
| 39 | > | #endif | 
| 40 |  |  | 
| 41 | + | #define FRAGBLK         512     /* number of fragments to allocate at a time */ | 
| 42 | + |  | 
| 43 | + | int     hdfragflags = FF_DEFAULT;       /* tells when to free fragments */ | 
| 44 | + | unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */ | 
| 45 | + | unsigned long   hdclock;                /* clock value */ | 
| 46 | + |  | 
| 47 |  | HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */ | 
| 48 |  |  | 
| 49 | < | static struct fragment { | 
| 49 | > | static struct fraglist { | 
| 50 |  | short   nlinks;         /* number of holodeck sections using us */ | 
| 51 | < | short   nfrags;         /* number of known fragments */ | 
| 52 | < | BEAMI   fi[MAXFRAG];    /* fragments, descending file position */ | 
| 51 | > | short   writerr;        /* write error encountered */ | 
| 52 | > | int     nfrags;         /* number of known fragments */ | 
| 53 | > | BEAMI   *fi;            /* fragments, descending file position */ | 
| 54 |  | long    flen;           /* last known file length */ | 
| 55 | < | } *hdfrag[FFDMAX];      /* fragment lists, indexed by file descriptor */ | 
| 55 | > | } *hdfragl;             /* fragment lists, indexed by file descriptor */ | 
| 56 |  |  | 
| 57 | + | static int      nhdfragls;      /* size of hdfragl array */ | 
| 58 |  |  | 
| 59 | + |  | 
| 60 | + | char * | 
| 61 | + | hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */ | 
| 62 | + | char    *ptr; | 
| 63 | + | unsigned        siz; | 
| 64 | + | char    *rout; | 
| 65 | + | { | 
| 66 | + | register char   *newp; | 
| 67 | + | /* call malloc/realloc */ | 
| 68 | + | if (ptr == NULL) newp = (char *)malloc(siz); | 
| 69 | + | else newp = (char *)realloc(ptr, siz); | 
| 70 | + | /* check success */ | 
| 71 | + | if (newp == NULL && rout != NULL) { | 
| 72 | + | hdfreecache(25, NULL);  /* free some memory */ | 
| 73 | + | errno = 0;              /* retry */ | 
| 74 | + | newp = hdrealloc(ptr, siz, NULL); | 
| 75 | + | if (newp == NULL) {     /* give up and report error */ | 
| 76 | + | sprintf(errmsg, "out of memory in %s", rout); | 
| 77 | + | error(SYSTEM, errmsg); | 
| 78 | + | } | 
| 79 | + | } | 
| 80 | + | return(newp); | 
| 81 | + | } | 
| 82 | + |  | 
| 83 | + |  | 
| 84 |  | hdattach(fd)            /* start tracking file fragments for some section */ | 
| 85 |  | register int    fd; | 
| 86 |  | { | 
| 87 | < | if (fd >= FFDMAX) | 
| 88 | < | return;                         /* descriptor out of range */ | 
| 89 | < | if (hdfrag[fd] == NULL) { | 
| 90 | < | if ((hdfrag[fd] = (struct fragment *) | 
| 91 | < | malloc(sizeof(struct fragment))) == NULL) | 
| 92 | < | return;                 /* should flag error? */ | 
| 53 | < | hdfrag[fd]->nlinks = 0; | 
| 54 | < | hdfrag[fd]->nfrags = 0; | 
| 87 | > | if (fd >= nhdfragls) { | 
| 88 | > | hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, | 
| 89 | > | (fd+1)*sizeof(struct fraglist), "hdattach"); | 
| 90 | > | bzero((char *)(hdfragl+nhdfragls), | 
| 91 | > | (fd+1-nhdfragls)*sizeof(struct fraglist)); | 
| 92 | > | nhdfragls = fd+1; | 
| 93 |  | } | 
| 94 | < | hdfrag[fd]->nlinks++; | 
| 95 | < | hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */ | 
| 94 | > | hdfragl[fd].nlinks++; | 
| 95 | > | hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */ | 
| 96 |  | } | 
| 97 |  |  | 
| 98 |  |  | 
| 102 |  | hdrelease(fd)           /* stop tracking file fragments for some section */ | 
| 103 |  | register int    fd; | 
| 104 |  | { | 
| 105 | < | if (fd >= FFDMAX || hdfrag[fd] == NULL) | 
| 105 | > | if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) | 
| 106 |  | return; | 
| 107 | < | if (!--hdfrag[fd]->nlinks) { | 
| 108 | < | free((char *)hdfrag[fd]); | 
| 109 | < | hdfrag[fd] = NULL; | 
| 107 | > | if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { | 
| 108 | > | free((char *)hdfragl[fd].fi); | 
| 109 | > | hdfragl[fd].fi = NULL; | 
| 110 | > | hdfragl[fd].nfrags = 0; | 
| 111 |  | } | 
| 112 |  | } | 
| 113 |  |  | 
| 117 |  | int     fd;                     /* corresponding file descriptor */ | 
| 118 |  | HDGRID  *hproto;                /* holodeck section grid */ | 
| 119 |  | { | 
| 120 | + | long    rtrunc; | 
| 121 |  | long    fpos; | 
| 122 |  | register HOLO   *hp; | 
| 123 |  | register int    n; | 
| 137 |  | n = nbeams(hp)*sizeof(BEAMI); | 
| 138 |  | if (read(fd, (char *)(hp->bi+1), n) != n) | 
| 139 |  | error(SYSTEM, "failure loading holodeck directory"); | 
| 140 | + | /* check that it's clean */ | 
| 141 | + | for (n = nbeams(hp); n > 0; n--) | 
| 142 | + | if (hp->bi[n].fo < 0) { | 
| 143 | + | hp->bi[n].fo = 0; | 
| 144 | + | error(WARNING, "dirty holodeck section"); | 
| 145 | + | break; | 
| 146 | + | } | 
| 147 |  | } else {                        /* assume we're creating it */ | 
| 148 |  | if ((hp = hdalloc(hproto)) == NULL) | 
| 149 |  | goto memerr; | 
| 157 |  | hp->fd = fd; | 
| 158 |  | hp->dirty = 0; | 
| 159 |  | biglob(hp)->fo = fpos + sizeof(HDGRID); | 
| 160 | < | biglob(hp)->nrd = 0;            /* count rays on disk */ | 
| 161 | < | for (n = nbeams(hp); n > 0; n--) | 
| 162 | < | biglob(hp)->nrd += hp->bi[n].nrd; | 
| 160 | > | /* start tracking fragments */ | 
| 161 | > | hdattach(fd); | 
| 162 | > | /* check rays on disk */ | 
| 163 | > | fpos = hdfilen(fd); | 
| 164 | > | biglob(hp)->nrd = rtrunc = 0; | 
| 165 | > | for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) | 
| 166 | > | if (hp->bi[n].nrd) | 
| 167 | > | if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) { | 
| 168 | > | rtrunc += hp->bi[n].nrd; | 
| 169 | > | hp->bi[n].nrd = 0; | 
| 170 | > | } else | 
| 171 | > | biglob(hp)->nrd += hp->bi[n].nrd; | 
| 172 | > | if (rtrunc) { | 
| 173 | > | sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", | 
| 174 | > | rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); | 
| 175 | > | error(WARNING, errmsg); | 
| 176 | > | } | 
| 177 |  | /* add to holodeck list */ | 
| 178 |  | for (n = 0; n < HDMAX; n++) | 
| 179 |  | if (hdlist[n] == NULL) { | 
| 180 |  | hdlist[n] = hp; | 
| 181 |  | break; | 
| 182 |  | } | 
| 122 | – | /* start tracking fragments (last) */ | 
| 123 | – | hdattach(fd); | 
| 183 |  | /* all done */ | 
| 184 |  | return(hp); | 
| 185 |  | memerr: | 
| 187 |  | } | 
| 188 |  |  | 
| 189 |  |  | 
| 190 | + | markdirty(hp, i)                /* mark holodeck directory position dirty */ | 
| 191 | + | register HOLO   *hp; | 
| 192 | + | int     i; | 
| 193 | + | { | 
| 194 | + | static BEAMI    smudge = {0, -1}; | 
| 195 | + | int     mindist, minpos; | 
| 196 | + | register int    j; | 
| 197 | + |  | 
| 198 | + | if (!hp->dirty++) {                     /* write smudge first time */ | 
| 199 | + | if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0 | 
| 200 | + | || write(hp->fd, (char *)&smudge, | 
| 201 | + | sizeof(BEAMI)) != sizeof(BEAMI)) | 
| 202 | + | error(SYSTEM, "seek/write error in markdirty"); | 
| 203 | + | hp->dirseg[0].s = i; | 
| 204 | + | hp->dirseg[0].n = 1; | 
| 205 | + | return; | 
| 206 | + | } | 
| 207 | + | /* insert into segment list */ | 
| 208 | + | for (j = hp->dirty; j--; ) { | 
| 209 | + | if (!j || hp->dirseg[j-1].s < i) { | 
| 210 | + | hp->dirseg[j].s = i; | 
| 211 | + | hp->dirseg[j].n = 1; | 
| 212 | + | break; | 
| 213 | + | } | 
| 214 | + | copystruct(hp->dirseg+j, hp->dirseg+(j-1)); | 
| 215 | + | } | 
| 216 | + | do {                            /* check neighbors */ | 
| 217 | + | mindist = nbeams(hp);           /* find closest */ | 
| 218 | + | for (j = hp->dirty; --j; ) | 
| 219 | + | if (hp->dirseg[j].s - | 
| 220 | + | (hp->dirseg[j-1].s + hp->dirseg[j-1].n) | 
| 221 | + | < mindist) { | 
| 222 | + | minpos = j; | 
| 223 | + | mindist = hp->dirseg[j].s - | 
| 224 | + | (hp->dirseg[j-1].s + hp->dirseg[j-1].n); | 
| 225 | + | } | 
| 226 | + | /* good enough? */ | 
| 227 | + | if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL) | 
| 228 | + | break; | 
| 229 | + | j = minpos - 1;                 /* coalesce neighbors */ | 
| 230 | + | if (hp->dirseg[j].s + hp->dirseg[j].n < | 
| 231 | + | hp->dirseg[minpos].s + hp->dirseg[minpos].n) | 
| 232 | + | hp->dirseg[j].n = hp->dirseg[minpos].s + | 
| 233 | + | hp->dirseg[minpos].n - hp->dirseg[j].s; | 
| 234 | + | hp->dirty--; | 
| 235 | + | while (++j < hp->dirty)         /* close the gap */ | 
| 236 | + | copystruct(hp->dirseg+j, hp->dirseg+(j+1)); | 
| 237 | + | } while (mindist <= MINDIRSEL); | 
| 238 | + | } | 
| 239 | + |  | 
| 240 | + |  | 
| 241 |  | int | 
| 242 | < | hdsync(hp)                      /* update directory on disk if necessary */ | 
| 242 | > | hdsync(hp, all)                 /* update beams and directory on disk */ | 
| 243 |  | register HOLO   *hp; | 
| 244 | + | int     all; | 
| 245 |  | { | 
| 246 |  | register int    j, n; | 
| 247 |  |  | 
| 248 | < | if (hp == NULL) {               /* do all */ | 
| 248 | > | if (hp == NULL) {               /* do all holodecks */ | 
| 249 |  | n = 0; | 
| 250 |  | for (j = 0; hdlist[j] != NULL; j++) | 
| 251 | < | n += hdsync(hdlist[j]); | 
| 251 | > | n += hdsync(hdlist[j], all); | 
| 252 |  | return(n); | 
| 253 |  | } | 
| 254 | < | if (!hp->dirty)                 /* check first */ | 
| 254 | > | /* sync the beams */ | 
| 255 | > | for (j = (all ? nbeams(hp) : 0); j > 0; j--) | 
| 256 | > | if (hp->bl[j] != NULL) | 
| 257 | > | hdsyncbeam(hp, j); | 
| 258 | > | if (!hp->dirty)                 /* directory clean? */ | 
| 259 |  | return(0); | 
| 260 | < | errno = 0; | 
| 261 | < | if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) | 
| 262 | < | error(SYSTEM, "cannot seek on holodeck file"); | 
| 263 | < | n = nbeams(hp)*sizeof(BEAMI); | 
| 264 | < | if (write(hp->fd, (char *)(hp->bi+1), n) != n) | 
| 265 | < | error(SYSTEM, "cannot update holodeck section directory"); | 
| 266 | < | hp->dirty = 0; | 
| 260 | > | errno = 0;                      /* write dirty segments */ | 
| 261 | > | for (j = 0; j < hp->dirty; j++) { | 
| 262 | > | if (lseek(hp->fd, biglob(hp)->fo + | 
| 263 | > | (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0) | 
| 264 | > | error(SYSTEM, "cannot seek on holodeck file"); | 
| 265 | > | n = hp->dirseg[j].n * sizeof(BEAMI); | 
| 266 | > | if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n) | 
| 267 | > | error(SYSTEM, "cannot update section directory"); | 
| 268 | > | } | 
| 269 | > | hp->dirty = 0;                  /* all clean */ | 
| 270 |  | return(1); | 
| 271 |  | } | 
| 272 |  |  | 
| 273 |  |  | 
| 274 | < | long | 
| 274 | > | unsigned | 
| 275 |  | hdmemuse(all)           /* return memory usage (in bytes) */ | 
| 276 |  | int     all;                    /* include overhead (painful) */ | 
| 277 |  | { | 
| 290 |  | } | 
| 291 |  | } | 
| 292 |  | if (all) | 
| 293 | < | for (j = 0; j < FFDMAX; j++) | 
| 294 | < | if (hdfrag[j] != NULL) | 
| 295 | < | total += sizeof(struct fragment); | 
| 293 | > | for (j = 0; j < nhdfragls; j++) { | 
| 294 | > | total += sizeof(struct fraglist); | 
| 295 | > | if (hdfragl[j].nfrags) | 
| 296 | > | total += FRAGBLK*sizeof(BEAMI) * | 
| 297 | > | ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; | 
| 298 | > | } | 
| 299 |  | return(total); | 
| 300 |  | } | 
| 301 |  |  | 
| 302 |  |  | 
| 303 |  | long | 
| 304 | + | hdfilen(fd)             /* return file length for fd */ | 
| 305 | + | int     fd; | 
| 306 | + | { | 
| 307 | + | long    fpos, flen; | 
| 308 | + |  | 
| 309 | + | if (fd < 0) | 
| 310 | + | return(-1); | 
| 311 | + | if (fd >= nhdfragls || !hdfragl[fd].nlinks) { | 
| 312 | + | if ((fpos = lseek(fd, 0L, 1)) < 0) | 
| 313 | + | return(-1); | 
| 314 | + | flen = lseek(fd, 0L, 2); | 
| 315 | + | lseek(fd, fpos, 0); | 
| 316 | + | return(flen); | 
| 317 | + | } | 
| 318 | + | return(hdfragl[fd].flen); | 
| 319 | + | } | 
| 320 | + |  | 
| 321 | + |  | 
| 322 | + | long | 
| 323 |  | hdfiluse(fd, all)       /* compute file usage (in bytes) */ | 
| 324 |  | int     fd;                     /* open file descriptor to check */ | 
| 325 |  | int     all;                    /* include overhead and unflushed data */ | 
| 354 |  | RAYVAL  *p; | 
| 355 |  | int     n; | 
| 356 |  |  | 
| 357 | < | if (nr <= 0) | 
| 358 | < | return(NULL); | 
| 359 | < | if (i < 1 | i > nbeams(hp)) | 
| 220 | < | error(CONSISTENCY, "bad beam index given to hdnewrays"); | 
| 357 | > | if (nr <= 0) return(NULL); | 
| 358 | > | CHECK(i < 1 | i > nbeams(hp), | 
| 359 | > | CONSISTENCY, "bad beam index given to hdnewrays"); | 
| 360 |  | if (hp->bl[i] != NULL) | 
| 361 |  | hp->bl[i]->tick = hdclock;      /* preempt swap */ | 
| 362 |  | if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) | 
| 363 |  | hdfreecache(PCTFREE, NULL);     /* free some space */ | 
| 225 | – | errno = 0; | 
| 364 |  | if (hp->bl[i] == NULL) {                /* allocate (and load) */ | 
| 365 |  | n = hp->bi[i].nrd + nr; | 
| 366 | < | if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) | 
| 229 | < | goto memerr; | 
| 366 | > | hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); | 
| 367 |  | blglob(hp)->nrm += n; | 
| 368 | < | if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { | 
| 368 | > | if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { | 
| 369 | > | errno = 0; | 
| 370 |  | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 371 |  | error(SYSTEM, "seek error on holodeck file"); | 
| 372 |  | n *= sizeof(RAYVAL); | 
| 375 |  | "error reading beam from holodeck file"); | 
| 376 |  | } | 
| 377 |  | } else {                                /* just grow in memory */ | 
| 378 | < | hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], | 
| 379 | < | hdbsiz(hp->bl[i]->nrm + nr) ); | 
| 242 | < | if (hp->bl[i] == NULL) | 
| 243 | < | goto memerr; | 
| 378 | > | hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], | 
| 379 | > | hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); | 
| 380 |  | blglob(hp)->nrm += nr; | 
| 381 |  | } | 
| 382 | + | if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) | 
| 383 | + | hdfreefrag(hp, i);              /* relinquish old fragment */ | 
| 384 |  | p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; | 
| 385 |  | hp->bl[i]->nrm += nr;                   /* update in-core structure */ | 
| 386 |  | bzero((char *)p, nr*sizeof(RAYVAL)); | 
| 387 | < | hp->bl[i]->tick = ++hdclock;            /* update LRU clock */ | 
| 250 | < | blglob(hp)->tick = hdclock; | 
| 387 | > | blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 388 |  | return(p);                              /* point to new rays */ | 
| 252 | – | memerr: | 
| 253 | – | error(SYSTEM, "out of memory in hdnewrays"); | 
| 389 |  | } | 
| 390 |  |  | 
| 391 |  |  | 
| 396 |  | { | 
| 397 |  | register int    n; | 
| 398 |  |  | 
| 399 | < | if (i < 1 | i > nbeams(hp)) | 
| 400 | < | error(CONSISTENCY, "bad beam index given to hdgetbeam"); | 
| 399 | > | CHECK(i < 1 | i > nbeams(hp), | 
| 400 | > | CONSISTENCY, "bad beam index given to hdgetbeam"); | 
| 401 |  | if (hp->bl[i] == NULL) {                /* load from disk */ | 
| 402 |  | if (!(n = hp->bi[i].nrd)) | 
| 403 |  | return(NULL); | 
| 404 |  | if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) | 
| 405 |  | hdfreecache(PCTFREE, NULL);     /* get free space */ | 
| 406 | < | errno = 0; | 
| 272 | < | if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) | 
| 273 | < | error(SYSTEM, "cannot allocate memory for beam"); | 
| 406 | > | hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); | 
| 407 |  | blglob(hp)->nrm += hp->bl[i]->nrm = n; | 
| 408 | + | errno = 0; | 
| 409 |  | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 410 |  | error(SYSTEM, "seek error on holodeck file"); | 
| 411 |  | n *= sizeof(RAYVAL); | 
| 412 |  | if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 413 |  | error(SYSTEM, "error reading beam from holodeck file"); | 
| 414 | + | if (hdfragflags&FF_READ) | 
| 415 | + | hdfreefrag(hp, i);      /* relinquish old frag. */ | 
| 416 |  | } | 
| 417 | < | hp->bl[i]->tick = ++hdclock;    /* update LRU clock */ | 
| 282 | < | blglob(hp)->tick = hdclock; | 
| 417 | > | blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ | 
| 418 |  | return(hp->bl[i]); | 
| 419 |  | } | 
| 420 |  |  | 
| 421 |  |  | 
| 422 |  | int | 
| 423 | < | hdgetbi(hp, i)                  /* allocate a file fragment */ | 
| 424 | < | register HOLO   *hp; | 
| 290 | < | register int    i; | 
| 423 | > | hdfilord(hb1, hb2)      /* order beams for quick loading */ | 
| 424 | > | register HDBEAMI        *hb1, *hb2; | 
| 425 |  | { | 
| 426 | < | int     nrays = hp->bl[i]->nrm; | 
| 426 | > | register long   c; | 
| 427 | > | /* residents go first */ | 
| 428 | > | if (hb2->h->bl[hb2->b] != NULL) | 
| 429 | > | return(hb1->h->bl[hb1->b] == NULL); | 
| 430 | > | if (hb1->h->bl[hb1->b] != NULL) | 
| 431 | > | return(-1); | 
| 432 | > | /* otherwise sort by file descriptor */ | 
| 433 | > | if ((c = hb1->h->fd - hb2->h->fd)) | 
| 434 | > | return(c); | 
| 435 | > | /* then by position in file */ | 
| 436 | > | c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; | 
| 437 | > | return(c > 0 ? 1 : c < 0 ? -1 : 0); | 
| 438 | > | } | 
| 439 |  |  | 
| 294 | – | if (hp->bi[i].nrd == nrays)     /* current one will do? */ | 
| 295 | – | return(0); | 
| 440 |  |  | 
| 441 | < | if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ | 
| 442 | < | hp->bi[i].fo = lseek(hp->fd, 0L, 2); | 
| 441 | > | hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */ | 
| 442 | > | register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */ | 
| 443 | > | int     n;                      /* list length */ | 
| 444 | > | int     (*bf)();                /* callback function (optional) */ | 
| 445 | > | { | 
| 446 | > | unsigned        origcachesize, memuse; | 
| 447 | > | int     bytesloaded, needbytes, bytes2free; | 
| 448 | > | register BEAM   *bp; | 
| 449 | > | register int    i; | 
| 450 | > | /* precheck consistency */ | 
| 451 | > | if (n <= 0) return; | 
| 452 | > | for (i = n; i--; ) | 
| 453 | > | if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) | 
| 454 | > | error(CONSISTENCY, "bad beam in hdloadbeams"); | 
| 455 | > | /* sort list for optimal access */ | 
| 456 | > | qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); | 
| 457 | > | bytesloaded = 0;                /* run through loaded beams */ | 
| 458 | > | for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { | 
| 459 | > | bp->tick = hdclock;     /* preempt swap */ | 
| 460 | > | bytesloaded += bp->nrm; | 
| 461 | > | if (bf != NULL) | 
| 462 | > | (*bf)(bp, hb); | 
| 463 | > | } | 
| 464 | > | bytesloaded *= sizeof(RAYVAL); | 
| 465 | > | if ((origcachesize = hdcachesize) > 0) { | 
| 466 | > | needbytes = 0;          /* figure out memory needs */ | 
| 467 | > | for (i = n; i--; ) | 
| 468 | > | needbytes += hb[i].h->bi[hb[i].b].nrd; | 
| 469 | > | needbytes *= sizeof(RAYVAL); | 
| 470 | > | do {                            /* free enough memory */ | 
| 471 | > | memuse = hdmemuse(0); | 
| 472 | > | bytes2free = needbytes - (int)(hdcachesize-memuse); | 
| 473 | > | if (bytes2free > (int)(memuse - bytesloaded)) | 
| 474 | > | bytes2free = memuse - bytesloaded; | 
| 475 | > | } while (bytes2free > 0 && | 
| 476 | > | hdfreecache(100*bytes2free/memuse, NULL) < 0); | 
| 477 | > | hdcachesize = 0;                /* load beams w/o swap */ | 
| 478 | > | } | 
| 479 | > | for (i = 0; i < n; i++) | 
| 480 | > | if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) | 
| 481 | > | (*bf)(bp, hb+i); | 
| 482 | > | hdcachesize = origcachesize;    /* resume dynamic swapping */ | 
| 483 | > | } | 
| 484 |  |  | 
| 300 | – | else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == | 
| 301 | – | hdfrag[hp->fd]->flen)           /* EOF special case */ | 
| 302 | – | hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); | 
| 485 |  |  | 
| 486 | < | else {                                          /* general case */ | 
| 487 | < | register struct fragment        *f = hdfrag[hp->fd]; | 
| 488 | < | register int    j, k; | 
| 489 | < | /* relinquish old fragment */ | 
| 490 | < | if (hp->bi[i].nrd) { | 
| 491 | < | if ((j = ++f->nfrags) >= MAXFRAG) | 
| 492 | < | f->nfrags--; | 
| 493 | < | for ( ; ; ) {   /* stick it in our descending list */ | 
| 494 | < | if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { | 
| 495 | < | f->fi[j].fo = hp->bi[i].fo; | 
| 496 | < | f->fi[j].nrd = hp->bi[i].nrd; | 
| 497 | < | break; | 
| 498 | < | } | 
| 499 | < | copystruct(f->fi+j, f->fi+j-1); | 
| 500 | < | } | 
| 486 | > | hdfreefrag(hp, i)                       /* free a file fragment */ | 
| 487 | > | HOLO    *hp; | 
| 488 | > | int     i; | 
| 489 | > | { | 
| 490 | > | register BEAMI  *bi = &hp->bi[i]; | 
| 491 | > | register struct fraglist        *f; | 
| 492 | > | register int    j, k; | 
| 493 | > |  | 
| 494 | > | if (bi->nrd <= 0) | 
| 495 | > | return; | 
| 496 | > | DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, | 
| 497 | > | CONSISTENCY, "bad file descriptor in hdfreefrag"); | 
| 498 | > | f = &hdfragl[hp->fd]; | 
| 499 | > | if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ | 
| 500 | > | for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 501 | > | while (f->fi[k].nrd == 0) | 
| 502 | > | if (++k >= f->nfrags) | 
| 503 | > | goto endloop; | 
| 504 | > | if (k > j) | 
| 505 | > | copystruct(f->fi+j, f->fi+k); | 
| 506 | > | } | 
| 507 | > | endloop: | 
| 508 | > | f->nfrags = j; | 
| 509 | > | } | 
| 510 | > | j = f->nfrags++;                /* allocate a slot in free list */ | 
| 511 | > | #if MAXFRAGB | 
| 512 | > | if (j >= MAXFRAGB*FRAGBLK) { | 
| 513 | > | f->nfrags = j--;        /* stop list growth */ | 
| 514 | > | if (bi->nrd <= f->fi[j].nrd) | 
| 515 | > | return;         /* new one no better than discard */ | 
| 516 | > | } | 
| 517 | > | #endif | 
| 518 | > | if (j % FRAGBLK == 0) {         /* more (or less) free list space */ | 
| 519 | > | register BEAMI  *newp; | 
| 520 | > | if (f->fi == NULL) | 
| 521 | > | newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); | 
| 522 | > | else | 
| 523 | > | newp = (BEAMI *)realloc((char *)f->fi, | 
| 524 | > | (j+FRAGBLK)*sizeof(BEAMI)); | 
| 525 | > | if (newp == NULL) { | 
| 526 | > | f->nfrags--;    /* graceful failure */ | 
| 527 | > | return; | 
| 528 | > | } | 
| 529 | > | f->fi = newp; | 
| 530 | > | } | 
| 531 | > | for ( ; ; j--) {                /* insert in descending list */ | 
| 532 | > | if (!j || bi->fo < f->fi[j-1].fo) { | 
| 533 | > | f->fi[j].fo = bi->fo; | 
| 534 | > | f->fi[j].nrd = bi->nrd; | 
| 535 | > | break; | 
| 536 | > | } | 
| 537 | > | copystruct(f->fi+j, f->fi+(j-1)); | 
| 538 | > | } | 
| 539 |  | /* coalesce adjacent fragments */ | 
| 540 | < | for (j = k = 0; k < f->nfrags; j++, k++) { | 
| 541 | < | if (k > j) | 
| 542 | < | copystruct(f->fi+j, f->fi+k); | 
| 543 | < | while (k+1 < f->nfrags && f->fi[k+1].fo + | 
| 544 | < | f->fi[k+1].nrd*sizeof(RAYVAL) | 
| 545 | < | == f->fi[j].fo) { | 
| 546 | < | f->fi[j].fo -= | 
| 547 | < | f->fi[++k].nrd*sizeof(RAYVAL); | 
| 548 | < | f->fi[j].nrd += f->fi[k].nrd; | 
| 549 | < | } | 
| 540 | > | /* successors never empty */ | 
| 541 | > | if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { | 
| 542 | > | f->fi[j].nrd += f->fi[j-1].nrd; | 
| 543 | > | f->fi[j-1].nrd = 0; | 
| 544 | > | } | 
| 545 | > | for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */ | 
| 546 | > | if (f->fi[k].nrd) { | 
| 547 | > | if (f->fi[j].fo == f->fi[k].fo + | 
| 548 | > | f->fi[k].nrd*sizeof(RAYVAL)) { | 
| 549 | > | f->fi[k].nrd += f->fi[j].nrd; | 
| 550 | > | f->fi[j].nrd = 0; | 
| 551 |  | } | 
| 552 | < | f->nfrags = j; | 
| 552 | > | break; | 
| 553 |  | } | 
| 554 | < | k = -1;                 /* find closest-sized fragment */ | 
| 555 | < | for (j = f->nfrags; j-- > 0; ) | 
| 556 | < | if (f->fi[j].nrd >= nrays && | 
| 557 | < | (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 558 | < | if (f->fi[k=j].nrd == nrays) | 
| 559 | < | break; | 
| 560 | < | if (k < 0) {            /* no fragment -- extend file */ | 
| 561 | < | hp->bi[i].fo = f->flen; | 
| 562 | < | f->flen += nrays*sizeof(RAYVAL); | 
| 563 | < | } else {                /* else use fragment */ | 
| 564 | < | hp->bi[i].fo = f->fi[k].fo; | 
| 565 | < | if (f->fi[k].nrd == nrays) {    /* delete fragment */ | 
| 566 | < | f->nfrags--; | 
| 567 | < | for (j = k; j < f->nfrags; j++) | 
| 568 | < | copystruct(f->fi+j, f->fi+j+1); | 
| 569 | < | } else {                        /* else shrink it */ | 
| 570 | < | f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 571 | < | f->fi[k].nrd -= nrays; | 
| 572 | < | } | 
| 573 | < | } | 
| 554 | > | biglob(hp)->nrd -= bi->nrd;             /* tell fragment it's free */ | 
| 555 | > | bi->nrd = 0; | 
| 556 | > | bi->fo = 0; | 
| 557 | > | } | 
| 558 | > |  | 
| 559 | > |  | 
| 560 | > | int | 
| 561 | > | hdfragOK(fd, listlen, listsiz)  /* get fragment list status for file */ | 
| 562 | > | int     fd; | 
| 563 | > | int     *listlen; | 
| 564 | > | register int4   *listsiz; | 
| 565 | > | { | 
| 566 | > | register struct fraglist        *f; | 
| 567 | > | register int    i; | 
| 568 | > |  | 
| 569 | > | if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) | 
| 570 | > | return(0);              /* listless */ | 
| 571 | > | if (listlen != NULL) | 
| 572 | > | *listlen = f->nfrags; | 
| 573 | > | if (listsiz != NULL) | 
| 574 | > | for (i = f->nfrags, *listsiz = 0; i--; ) | 
| 575 | > | *listsiz += f->fi[i].nrd; | 
| 576 | > | #if MAXFRAGB | 
| 577 | > | return(f->nfrags < MAXFRAGB*FRAGBLK); | 
| 578 | > | #else | 
| 579 | > | return(1);                      /* list never fills up */ | 
| 580 | > | #endif | 
| 581 | > | } | 
| 582 | > |  | 
| 583 | > |  | 
| 584 | > | long | 
| 585 | > | hdallocfrag(fd, nrays)          /* allocate a file fragment */ | 
| 586 | > | int     fd; | 
| 587 | > | unsigned int4   nrays; | 
| 588 | > | { | 
| 589 | > | register struct fraglist        *f; | 
| 590 | > | register int    j, k; | 
| 591 | > | long    nfo; | 
| 592 | > |  | 
| 593 | > | if (nrays == 0) | 
| 594 | > | return(-1L); | 
| 595 | > | DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, | 
| 596 | > | CONSISTENCY, "bad file descriptor in hdallocfrag"); | 
| 597 | > | f = &hdfragl[fd]; | 
| 598 | > | k = -1;                         /* find closest-sized fragment */ | 
| 599 | > | for (j = f->nfrags; j-- > 0; ) | 
| 600 | > | if (f->fi[j].nrd >= nrays && | 
| 601 | > | (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) | 
| 602 | > | if (f->fi[k=j].nrd == nrays) | 
| 603 | > | break; | 
| 604 | > | if (k < 0) {                    /* no fragment -- extend file */ | 
| 605 | > | nfo = f->flen; | 
| 606 | > | f->flen += nrays*sizeof(RAYVAL); | 
| 607 | > | } else {                        /* else use fragment */ | 
| 608 | > | nfo = f->fi[k].fo; | 
| 609 | > | f->fi[k].fo += nrays*sizeof(RAYVAL); | 
| 610 | > | f->fi[k].nrd -= nrays; | 
| 611 |  | } | 
| 612 | < | biglob(hp)->nrd += nrays - hp->bi[i].nrd; | 
| 613 | < | hp->bi[i].nrd = nrays; | 
| 614 | < | hp->dirty++;            /* section directory now out of date */ | 
| 612 | > | return(nfo); | 
| 613 | > | } | 
| 614 | > |  | 
| 615 | > |  | 
| 616 | > | int | 
| 617 | > | hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */ | 
| 618 | > | register HOLO   *hp; | 
| 619 | > | register int    i; | 
| 620 | > | { | 
| 621 | > | unsigned int4   nrays; | 
| 622 | > | unsigned int    n; | 
| 623 | > | long    nfo; | 
| 624 | > | /* check file status */ | 
| 625 | > | if (hdfragl[hp->fd].writerr) | 
| 626 | > | return(-1); | 
| 627 | > | DCHECK(i < 1 | i > nbeams(hp), | 
| 628 | > | CONSISTENCY, "bad beam index in hdsyncbeam"); | 
| 629 | > | /* is current fragment OK? */ | 
| 630 | > | if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) | 
| 631 | > | return(0); | 
| 632 | > | if (hdfragflags&FF_WRITE && hp->bi[i].nrd) | 
| 633 | > | hdfreefrag(hp, i);      /* relinquish old fragment */ | 
| 634 | > | if (nrays) {                    /* get and write new fragment */ | 
| 635 | > | nfo = hdallocfrag(hp->fd, nrays); | 
| 636 | > | errno = 0; | 
| 637 | > | if (lseek(hp->fd, nfo, 0) < 0) | 
| 638 | > | error(SYSTEM, "cannot seek on holodeck file"); | 
| 639 | > | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 640 | > | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { | 
| 641 | > | hdfragl[hp->fd].writerr++; | 
| 642 | > | hdsync(NULL, 0);        /* sync directories */ | 
| 643 | > | error(SYSTEM, "write error in hdsyncbeam"); | 
| 644 | > | } | 
| 645 | > | hp->bi[i].fo = nfo; | 
| 646 | > | } else | 
| 647 | > | hp->bi[i].fo = 0L; | 
| 648 | > | biglob(hp)->nrd += hp->bi[i].nrd = nrays; | 
| 649 | > | markdirty(hp, i);               /* section directory now out of date */ | 
| 650 |  | return(1); | 
| 651 |  | } | 
| 652 |  |  | 
| 656 |  | register HOLO   *hp; | 
| 657 |  | register int    i; | 
| 658 |  | { | 
| 659 | < | int     nchanged, n; | 
| 659 | > | int     nchanged; | 
| 660 |  |  | 
| 661 |  | if (hp == NULL) {               /* clear all holodecks */ | 
| 662 |  | nchanged = 0; | 
| 664 |  | nchanged += hdfreebeam(hdlist[i], 0); | 
| 665 |  | return(nchanged); | 
| 666 |  | } | 
| 667 | + | if (hdfragl[hp->fd].writerr)    /* check for file error */ | 
| 668 | + | return(0); | 
| 669 |  | if (i == 0) {                   /* clear entire holodeck */ | 
| 670 |  | nchanged = 0; | 
| 671 | < | for (i = 1; i <= nbeams(hp); i++) | 
| 672 | < | nchanged += hdfreebeam(hp, i); | 
| 671 | > | for (i = nbeams(hp); i > 0; i--) | 
| 672 | > | if (hp->bl[i] != NULL) | 
| 673 | > | nchanged += hdfreebeam(hp, i); | 
| 674 |  | return(nchanged); | 
| 675 |  | } | 
| 676 | < | if (i < 1 | i > nbeams(hp)) | 
| 677 | < | error(CONSISTENCY, "bad beam index to hdfreebeam"); | 
| 676 | > | DCHECK(i < 1 | i > nbeams(hp), | 
| 677 | > | CONSISTENCY, "bad beam index to hdfreebeam"); | 
| 678 |  | if (hp->bl[i] == NULL) | 
| 679 |  | return(0); | 
| 680 |  | /* check for additions */ | 
| 681 |  | nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; | 
| 682 | < | if (nchanged) { | 
| 683 | < | hdgetbi(hp, i);                 /* allocate a file position */ | 
| 388 | < | errno = 0; | 
| 389 | < | if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) | 
| 390 | < | error(SYSTEM, "cannot seek on holodeck file"); | 
| 391 | < | n = hp->bl[i]->nrm * sizeof(RAYVAL); | 
| 392 | < | if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) | 
| 393 | < | error(SYSTEM, "write error in hdfreebeam"); | 
| 394 | < | } | 
| 682 | > | if (nchanged) | 
| 683 | > | hdsyncbeam(hp, i);              /* write new fragment */ | 
| 684 |  | blglob(hp)->nrm -= hp->bl[i]->nrm; | 
| 685 |  | free((char *)hp->bl[i]);                /* free memory */ | 
| 686 |  | hp->bl[i] = NULL; | 
| 688 |  | } | 
| 689 |  |  | 
| 690 |  |  | 
| 691 | < | hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */ | 
| 692 | < | register HOLO   *ha[];                  /* section list (NULL terminated) */ | 
| 693 | < | register int    ba[];                   /* beam index to go with section */ | 
| 694 | < | int     n;                              /* length of arrays minus 1 */ | 
| 691 | > | int | 
| 692 | > | hdkillbeam(hp, i)               /* delete beam from holodeck */ | 
| 693 | > | register HOLO   *hp; | 
| 694 | > | register int    i; | 
| 695 | > | { | 
| 696 | > | int     nchanged; | 
| 697 | > |  | 
| 698 | > | if (hp == NULL) {               /* clobber all holodecks */ | 
| 699 | > | nchanged = 0; | 
| 700 | > | for (i = 0; hdlist[i] != NULL; i++) | 
| 701 | > | nchanged += hdkillbeam(hdlist[i], 0); | 
| 702 | > | return(nchanged); | 
| 703 | > | } | 
| 704 | > | if (i == 0) {                   /* clobber entire holodeck */ | 
| 705 | > | nchanged = 0; | 
| 706 | > | for (i = nbeams(hp); i > 0; i--) | 
| 707 | > | if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) | 
| 708 | > | nchanged += hdkillbeam(hp, i); | 
| 709 | > | DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, | 
| 710 | > | CONSISTENCY, "bad beam count in hdkillbeam"); | 
| 711 | > | return(nchanged); | 
| 712 | > | } | 
| 713 | > | DCHECK(i < 1 | i > nbeams(hp), | 
| 714 | > | CONSISTENCY, "bad beam index to hdkillbeam"); | 
| 715 | > | if (hp->bl[i] != NULL) {        /* free memory */ | 
| 716 | > | blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; | 
| 717 | > | free((char *)hp->bl[i]); | 
| 718 | > | } else | 
| 719 | > | nchanged = hp->bi[i].nrd; | 
| 720 | > | if (hp->bi[i].nrd) { | 
| 721 | > | if (hdfragflags&FF_KILL) | 
| 722 | > | hdfreefrag(hp, i); | 
| 723 | > | hp->bi[i].nrd = 0;      /* make sure it's gone */ | 
| 724 | > | hp->bi[i].fo = 0L; | 
| 725 | > | } | 
| 726 | > | hp->bl[i] = NULL; | 
| 727 | > | return(nchanged); | 
| 728 | > | } | 
| 729 | > |  | 
| 730 | > |  | 
| 731 | > | int | 
| 732 | > | hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */ | 
| 733 | > | register HDBEAMI        *hb;            /* beam list */ | 
| 734 | > | int     nents;                          /* current list length */ | 
| 735 | > | int     n;                              /* maximum list length */ | 
| 736 |  | register HOLO   *hp;                    /* section we're adding from */ | 
| 737 |  | { | 
| 738 |  | register int    i, j; | 
| 409 | – | int     nents; | 
| 410 | – | /* find last entry in LRU list */ | 
| 411 | – | for (j = 0; ha[j] != NULL; j++) | 
| 412 | – | ; | 
| 413 | – | nents = j; | 
| 739 |  | /* insert each beam from hp */ | 
| 740 | < | for (i = nbeams(hp); i > 0; i-- ) { | 
| 740 | > | for (i = 1; i <= nbeams(hp); i++) { | 
| 741 |  | if (hp->bl[i] == NULL)          /* check if loaded */ | 
| 742 |  | continue; | 
| 743 | < | if ((j = ++nents) > n)          /* grow list if we can */ | 
| 743 | > | #if 0 | 
| 744 | > | if (hp->bl[i]->tick == hdclock) /* preempt swap? */ | 
| 745 | > | continue; | 
| 746 | > | #endif | 
| 747 | > | if ((j = ++nents) >= n)         /* grow list if we can */ | 
| 748 |  | nents--; | 
| 749 |  | for ( ; ; ) {                   /* bubble into place */ | 
| 750 |  | if (!--j || hp->bl[i]->tick >= | 
| 751 | < | ha[j-1]->bl[ba[j-1]]->tick) { | 
| 752 | < | ha[j] = hp; | 
| 753 | < | ba[j] = i; | 
| 751 | > | hb[j-1].h->bl[hb[j-1].b]->tick) { | 
| 752 | > | hb[j].h = hp; | 
| 753 | > | hb[j].b = i; | 
| 754 |  | break; | 
| 755 |  | } | 
| 756 | < | ha[j] = ha[j-1]; | 
| 428 | < | ba[j] = ba[j-1]; | 
| 756 | > | copystruct(hb+j, hb+(j-1)); | 
| 757 |  | } | 
| 758 |  | } | 
| 759 | < | ha[nents] = NULL;               /* all done */ | 
| 432 | < | ba[nents] = 0; | 
| 759 | > | return(nents);                  /* return new list length */ | 
| 760 |  | } | 
| 761 |  |  | 
| 762 |  |  | 
| 763 | + | int | 
| 764 |  | hdfreecache(pct, honly)         /* free up cache space, writing changes */ | 
| 765 |  | int     pct;                            /* maximum percentage to free */ | 
| 766 |  | register HOLO   *honly;                 /* NULL means check all */ | 
| 767 |  | { | 
| 768 | < | HOLO    *hp[FREEBEAMS+1]; | 
| 441 | < | int     bn[FREEBEAMS+1]; | 
| 768 | > | HDBEAMI hb[FREEBEAMS]; | 
| 769 |  | int     freetarget; | 
| 770 | + | int     n; | 
| 771 |  | register int    i; | 
| 772 | + | #ifdef DEBUG | 
| 773 | + | unsigned        membefore; | 
| 774 | + |  | 
| 775 | + | membefore = hdmemuse(0); | 
| 776 | + | #endif | 
| 777 |  | /* compute free target */ | 
| 778 |  | freetarget = (honly != NULL) ? blglob(honly)->nrm : | 
| 779 |  | hdmemuse(0)/sizeof(RAYVAL) ; | 
| 780 |  | freetarget = freetarget*pct/100; | 
| 781 | + | if (freetarget <= 0) | 
| 782 | + | return(0); | 
| 783 |  | /* find least recently used */ | 
| 784 | < | hp[0] = NULL; | 
| 450 | < | bn[0] = 0; | 
| 784 | > | n = 0; | 
| 785 |  | if (honly != NULL) | 
| 786 | < | hdlrulist(hp, bn, FREEBEAMS, honly); | 
| 786 | > | n = hdlrulist(hb, n, FREEBEAMS, honly); | 
| 787 |  | else | 
| 788 |  | for (i = 0; hdlist[i] != NULL; i++) | 
| 789 | < | hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); | 
| 789 | > | n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); | 
| 790 |  | /* free LRU beams */ | 
| 791 | < | for (i = 0; hp[i] != NULL; i++) { | 
| 792 | < | hdfreebeam(hp[i], bn[i]); | 
| 793 | < | if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) | 
| 791 | > | for (i = 0; i < n; i++) { | 
| 792 | > | hdfreebeam(hb[i].h, hb[i].b); | 
| 793 | > | if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) | 
| 794 |  | break; | 
| 795 |  | } | 
| 796 | < | hdsync(honly);          /* synchronize directories as necessary */ | 
| 796 | > | hdsync(honly, 0);       /* synchronize directories as necessary */ | 
| 797 | > | #ifdef DEBUG | 
| 798 | > | sprintf(errmsg, | 
| 799 | > | "%dK before, %dK after hdfreecache (%dK total), %d rays short\n", | 
| 800 | > | membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); | 
| 801 | > | wputs(errmsg); | 
| 802 | > | #endif | 
| 803 | > | return(-freetarget);    /* return how far past goal we went */ | 
| 804 |  | } | 
| 805 |  |  | 
| 806 |  |  | 
| 812 |  | if (hp == NULL) {               /* NULL means clean up everything */ | 
| 813 |  | while (hdlist[0] != NULL) | 
| 814 |  | hddone(hdlist[0]); | 
| 815 | + | free((char *)hdfragl); | 
| 816 | + | hdfragl = NULL; nhdfragls = 0; | 
| 817 |  | return; | 
| 818 |  | } | 
| 819 |  | /* flush all data and free memory */ | 
| 820 | < | hdflush(hp); | 
| 820 | > | hdfreebeam(hp, 0); | 
| 821 | > | hdsync(hp, 0); | 
| 822 |  | /* release fragment resources */ | 
| 823 |  | hdrelease(hp->fd); | 
| 824 |  | /* remove hp from active list */ |