Debian Codegraph @

terps/tads/tads2/mcs.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/MCS.C,v 1.2 1999/05/17 02:52:12 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 mcs.c - memory cache swapping manager
15 Function
16 Implements swapping functions for cache manager
17 Notes
18 None
19 Modified
20 08/04/91 MJRoberts - creation
21 */
22
23 #include <assert.h>
24 #include <stdio.h>
25 #include "os.h"
26 #include "std.h"
27 #include "err.h"
28 #include "mcs.h"
29 #include "mch.h"
30 #include "mcm.h"
31
32 /* initialize swapper: allocate memory for swap page table */
33 void mcsini(mcscxdef *ctx, mcmcx1def *gmemctx, ulong maxsiz,
34 osfildef *fp, char *swapfilename, errcxdef *errctx)
35 {
36 uchar *p;
37
38 ctx->mcscxtab = (mcsdsdef **)0; /* anticipate failure */
39
40 /* allocate space from the low-level heap for page table and one page */
41 p = mchalo(errctx, ((MCSPAGETAB * sizeof(mcsdsdef *))
42 + (MCSPAGECNT * sizeof(mcsdsdef))), "mcsini");
43
44 /* set up the context with pointers to this chunk */
45 ctx->mcscxtab = (mcsdsdef **)p;
46 memset(p, 0, (size_t)(MCSPAGETAB * sizeof(mcsdsdef *)));
47 p += MCSPAGETAB * sizeof(mcsdsdef *);
48 ctx->mcscxtab[0] = (mcsdsdef *)p;
49
50 /* set up the rest of the context */
51 ctx->mcscxtop = (ulong)0;
52 ctx->mcscxmax = maxsiz;
53 ctx->mcscxmsg = 0;
54 ctx->mcscxfp = fp;
55 ctx->mcscxerr = errctx;
56 ctx->mcscxmem = gmemctx;
57
58 /*
59 * store the swap filename - make a copy so that the caller doesn't
60 * have to retain the original copy (in case it's on the stack)
61 */
62 if (swapfilename != 0)
63 {
64 ctx->mcscxfname = (char *)mchalo(errctx,
65 (strlen(swapfilename)+1),
66 "mcsini");
67 strcpy(ctx->mcscxfname, swapfilename);
68 }
69 else
70 ctx->mcscxfname = 0;
71 }
72
73 /* close the swapper */
74 void mcsclose(mcscxdef *ctx)
75 {
76 if (ctx->mcscxtab) mchfre(ctx->mcscxtab);
77 }
78
79 /*
80 * Attempt to compact the swap file when it grows too big. The segment
81 * descriptors are always allocated in increasing seek location within
82 * the swap file. To compress the file, make each descriptor's
83 * allocated size equal its used size for each in-use segment, and leave
84 * free segments at their allocated sizes.
85 */
86 static void mcscompact(mcscxdef *ctx)
87 {
88 char buf[512];
89 ulong max;
90 mcsseg cur_in;
91 mcsseg cur_out;
92 mcsdsdef *desc_in;
93 mcsdsdef *desc_out;
94 uint siz;
95 uint rdsiz;
96 ulong ptr_in;
97 ulong ptr_out;
98
99 max = 0; /* start at offset zero within file */
100 for (cur_in = cur_out = 0 ; cur_in < ctx->mcscxmsg ; ++cur_in)
101 {
102 desc_in = mcsdsc(ctx, cur_in);
103
104 /*
105 * If the present descriptor's address is wrong, and the swap
106 * segment is in use, move the swap segment. If it's not in
107 * use, we don't need to move it, because we're going to throw
108 * away the segment entirely.
109 */
110 if (desc_in->mcsdsptr != max
111 && (desc_in->mcsdsflg & MCSDSFINUSE))
112 {
113 /* ptr_in is the old location, ptr_out is the new location */
114 ptr_in = desc_in->mcsdsptr;
115 ptr_out = max;
116
117 /* copy through our buffer */
118 for (siz = desc_in->mcsdsosz ; siz ; siz -= rdsiz)
119 {
120 /* size is whole buffer, or last piece if smaller */
121 rdsiz = (siz > sizeof(buf) ? sizeof(buf) : siz);
122
123 /* seek to old location and get the piece */
124 osfseek(ctx->mcscxfp, ptr_in, OSFSK_SET);
125 (void)osfrb(ctx->mcscxfp, buf, (size_t)rdsiz);
126
127 /* seek to new location and write the piece */
128 osfseek(ctx->mcscxfp, ptr_out, OSFSK_SET);
129 (void)osfwb(ctx->mcscxfp, buf, (size_t)rdsiz);
130
131 /* adjust the pointers by the size copied */
132 ptr_in += rdsiz;
133 ptr_out += rdsiz;
134 }
135 }
136
137 /* adjust object descriptor to reflect new location */
138 desc_in->mcsdsptr = max;
139
140 /*
141 * Make current object's size exact if it's in use. If it's
142 * not in use, delete the segment altogether.
143 */
144 if (desc_in->mcsdsflg & MCSDSFINUSE)
145 {
146 desc_in->mcsdssiz = desc_in->mcsdsosz;
147 max += desc_in->mcsdssiz;
148
149 /* copy descriptor to correct position to close any holes */
150 if (cur_out != cur_in)
151 {
152 desc_out = mcsdsc(ctx, cur_out);
153 OSCPYSTRUCT(*desc_out, *desc_in);
154
155 /* we need to renumber the corresponding object as well */
156 mcmcsw(ctx->mcscxmem, (mcmon)desc_in->mcsdsobj,
157 cur_out, cur_in);
158 }
159
160 /* we actually wrote this one, so move output pointer */
161 ++cur_out;
162 }
163 else
164 {
165 /*
166 * We need to renumber the corresponding object so that it
167 * knows there is no swap segment for it any more.
168 */
169 mcmcsw(ctx->mcscxmem, (mcmon)desc_in->mcsdsobj,
170 MCSSEGINV, cur_in);
171 }
172 }
173
174 /*
175 * Adjust the top of the file for our new size, and add the savings
176 * into the available space counter. Also, adjust the total handle
177 * count to reflect any descriptors that we've deleted.
178 */
179 ctx->mcscxmax += (ctx->mcscxtop - max);
180 ctx->mcscxtop = max;
181 ctx->mcscxmsg = cur_out;
182 }
183
184 /* swap an object out to the swap file */
185 mcsseg mcsout(mcscxdef *ctx, uint objid, uchar *ptr, ushort siz,
186 mcsseg oldseg, int dirty)
187 {
188 mcsdsdef *desc;
189 mcsdsdef **pagep;
190 uint i;
191 uint j;
192 mcsseg min;
193 mcsseg cur;
194 ushort minsiz;
195
196 IF_DEBUG(printf("<< mcsout: objid=%d, ptr=%lx, siz=%u, oldseg=%u >>\n",
197 objid, (unsigned long)ptr, siz, oldseg));
198
199 /* see if old segment can be reused */
200 if (oldseg != MCSSEGINV)
201 {
202 desc = mcsdsc(ctx, oldseg);
203 if (!(desc->mcsdsflg & MCSDSFINUSE) /* if old seg is not in use */
204 && desc->mcsdsobj == objid /* and it has same object */
205 && desc->mcsdssiz >= siz /* and it's still big enough */
206 && !dirty) /* and the object in memory hasn't been changed */
207 {
208 /* we can reuse the old segment without rewriting it */
209 desc->mcsdsflg |= MCSDSFINUSE; /* mark segment as in use */
210 return(oldseg);
211 }
212 }
213
214 /* look for the smallest unused segment big enough for this object */
215 for (cur = 0, min = MCSSEGINV, i = 0, pagep = ctx->mcscxtab
216 ; cur < ctx->mcscxmsg && i < MCSPAGETAB && *pagep ; ++pagep, ++i)
217 {
218 for (j = 0, desc = *pagep ; cur < ctx->mcscxmsg && j < MCSPAGECNT
219 ; ++desc, ++j, ++cur)
220 {
221 if (!(desc->mcsdsflg & MCSDSFINUSE)
222 && desc->mcsdssiz >= siz
223 && (min == MCSSEGINV || desc->mcsdssiz < minsiz))
224 {
225 min = cur;
226 minsiz = desc->mcsdssiz;
227 if (minsiz == siz) break; /* exact match - we're done */
228 }
229 }
230 /* quit if we found an exact match */
231 if (min != MCSSEGINV && minsiz == siz) break;
232 }
233
234 /* if we found nothing, allocate a new segment if possible */
235 if (min == MCSSEGINV)
236 {
237 if (siz > ctx->mcscxmax)
238 {
239 /* swap file is too big; compact it and try again */
240 mcscompact(ctx);
241 if (siz > ctx->mcscxmax)
242 errsig(ctx->mcscxerr, ERR_SWAPBIG);
243 }
244
245 min = ctx->mcscxmsg;
246 if ((min >> 8) >= MCSPAGETAB) /* exceeded pages in page table? */
247 errsig(ctx->mcscxerr, ERR_SWAPPG);
248
249 if (!ctx->mcscxtab[min >> 8]) /* haven't allocate page yet? */
250 {
251 ctx->mcscxtab[min >> 8] =
252 (mcsdsdef *)mchalo(ctx->mcscxerr,
253 (MCSPAGECNT * sizeof(mcsdsdef)),
254 "mcsout");
255 }
256
257 /* set up new descriptor */
258 desc = mcsdsc(ctx, min);
259 desc->mcsdsptr = ctx->mcscxtop;
260 desc->mcsdssiz = siz;
261 desc->mcsdsobj = objid;
262
263 /* write out the segment */
264 mcswrt(ctx, desc, ptr, siz);
265 desc->mcsdsflg = MCSDSFINUSE;
266
267 /* update context information to account for new segment */
268 ctx->mcscxtop += siz; /* add to top seek offset in file */
269 ctx->mcscxmax -= siz; /* take size out of quota */
270 ctx->mcscxmsg++; /* increment last segment allocated */
271
272 return(min);
273 }
274 else
275 {
276 desc = mcsdsc(ctx, min);
277 desc->mcsdsobj = objid;
278 mcswrt(ctx, desc, ptr, siz);
279 desc->mcsdsflg |= MCSDSFINUSE;
280
281 return(min);
282 }
283 }
284
285 void mcsin(mcscxdef *ctx, mcsseg seg, uchar *ptr, ushort siz)
286 {
287 mcsdsdef *desc = mcsdsc(ctx, seg);
288
289 IF_DEBUG(printf("<< mcsin: seg=%u, ptr=%lx, siz=%d, objid=%u >>\n",
290 seg, (unsigned long)ptr, siz, desc->mcsdsobj));
291
292 assert(seg < ctx->mcscxmsg);
293
294 /* can only swap in as much as we wrote */
295 if (desc->mcsdsosz < siz) siz = desc->mcsdsosz;
296
297 /* seek to and read the segment */
298 if (osfseek(ctx->mcscxfp, desc->mcsdsptr, OSFSK_SET))
299 errsig(ctx->mcscxerr, ERR_FSEEK);
300 if (osfrb(ctx->mcscxfp, ptr, (size_t)siz))
301 errsig(ctx->mcscxerr, ERR_FREAD);
302
303 desc->mcsdsflg &= ~MCSDSFINUSE; /* segment no longer in use */
304 }
305
306 void mcswrt(mcscxdef *ctx, mcsdsdef *desc, uchar *buf, ushort bufl)
307 {
308 int tries;
309
310 desc->mcsdsosz = bufl;
311
312 for (tries = 0 ; tries < 2 ; ++tries)
313 {
314 /* attempt to write the object to the swap file */
315 if (osfseek(ctx->mcscxfp, desc->mcsdsptr, OSFSK_SET))
316 errsig(ctx->mcscxerr, ERR_FSEEK);
317 if (!osfwb(ctx->mcscxfp, buf, (size_t)bufl))
318 return;
319
320 /* couldn't write it; compact the swap file */
321 mcscompact(ctx);
322 }
323
324 /* couldn't write to swap file, even after compacting it */
325 errsig(ctx->mcscxerr, ERR_FWRITE);
326 }
327