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