Debian Codegraph @

terps/tads/tads2/mcm.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 mcm.c - memory cache manager
10 Function
11 Manages the high-level memory cache
12 Notes
13 The memory cache manager is a subsystem providing high-level
14 object cache management. Each object in a cache can be swapped
15 out to disk or relocated when it is not locked, allowing the
16 cache manager to keep memory unfragmented and overcommitted.
17 Modified
18 08/03/91 MJRoberts - creation
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24
25 #include "os.h"
26 #include "mcm.h"
27 #include "mch.h"
28 #include "err.h"
29
30 /* get an unused object cache entry, allocating a new page if needed */
31 static mcmodef *mcmoal(mcmcx1def *ctx, mcmon *objnum);
32
33 /* split a (previously free) block into two pieces */
34 static void mcmsplt(mcmcx1def *ctx, mcmon n, ushort siz);
35
36 /* unlink an object from a doubly-linked list */
37 static void mcmunl(mcmcx1def *ctx, mcmon n, mcmon *lst);
38
39 /* initialize a cache, return cache context */
40 /* find free block: find a block from the free pool to satisfy a request */
41 static mcmodef *mcmffb(mcmcx1def *ctx, ushort siz, mcmon *nump);
42
43 /* add page pagenum, initializing entries after firstunu to unused */
44 static void mcmadpg(mcmcx1def *ctx, uint pagenum, mcmon firstunu);
45
46 /* link an object into a doubly-linked list at the head of the list */
47 static void mcmlnkhd(mcmcx1def *ctx, mcmon *lst, mcmon n);
48
49 /* try to allocate a new chunk from the heap */
50 static uchar *mcmhalo(mcmcx1def *ctx);
51
52 /* relocate blocks in a heap */
53 static uchar *mcmreloc(mcmcx1def *ctx, uchar *start, uchar *end);
54
55 /* find next free heap block */
56 static uchar *mcmffh(mcmcx1def *ctx, uchar *p);
57
58 #ifdef NEVER
59 /* update entry to account for a block relocation */
60 static void mcmmove(mcmcx1def *ctx, mcmodef *obj, uchar *newaddr);
61 #else /* NEVER */
62 #define mcmmove(ctx, o, new) ((o)->mcmoptr = (new))
63 #endif /* NEVER */
64
65 /* consolidate two contiguous free blocks into a single block */
66 static void mcmconsol(mcmcx1def *ctx, uchar *p);
67
68 /* collect garbage in all heaps */
69 static void mcmgarb(mcmcx1def *ctx);
70
71 /* make some room by swapping or discarding objects */
72 static int mcmswap(mcmcx1def *ctx, ushort siz);
73
74 /* toss out an object; returns TRUE if successful */
75 static int mcmtoss(mcmcx1def *ctx, mcmon objnum);
76
77 /* next heap block, given a heap block (points to header) */
78 /* uchar *mcmhnxt(mcmcx1def *ctx, uchar *p) */
79
80 #define mcmnxh(ctx, p) \
81 ((p) + osrndsz(sizeof(mcmon)) + mcmgobje(ctx, *(mcmon*)(p))->mcmosiz)
82
83 #ifdef DEBUG
84 # define MCMCLICTX(ctx) assert(*(((ulong *)ctx) - 1) == 0x02020202)
85 # define MCMGLBCTX(ctx) assert(*(((ulong *)ctx) - 1) == 0x01010101)
86 #else /* DEBUG */
87 # define MCMCLICTX(ctx)
88 # define MCMGLBCTX(ctx)
89 #endif /* DEBUG */
90
91 /* initialize a new client context */
92 mcmcxdef *mcmcini(mcmcx1def *globalctx, uint pages,
93 void (*loadfn)(void *, mclhd, uchar *, ushort),
94 void *loadctx,
95 void (*revertfn)(void *, mcmon), void *revertctx)
96 {
97 mcmcxdef *ret;
98 ushort siz;
99
100 siz = sizeof(mcmcxdef) + sizeof(mcmon *) * (pages - 1);
101 IF_DEBUG(siz += sizeof(ulong));
102
103 ret = (mcmcxdef *)mchalo(globalctx->mcmcxerr, siz, "mcm client context");
104 IF_DEBUG((*(ulong *)ret = 0x02020202,
105 ret = (mcmcxdef *)((uchar *)ret + sizeof(ulong))));
106
107 ret->mcmcxmsz = pages;
108 ret->mcmcxgl = globalctx;
109 ret->mcmcxldf = loadfn;
110 ret->mcmcxldc = loadctx;
111 ret->mcmcxrvf = revertfn;
112 ret->mcmcxrvc = revertctx;
113 ret->mcmcxflg = 0;
114 memset(ret->mcmcxmtb, 0, (size_t)(pages * sizeof(mcmon *)));
115 return(ret);
116 }
117
118 /* uninitialize a client context */
119 void mcmcterm(mcmcxdef *ctx)
120 {
121 /* delete the context memory */
122 mchfre(ctx);
123 }
124
125 /* initialize a new global context */
126 mcmcx1def *mcmini(ulong max, uint pages, ulong swapsize,
127 osfildef *swapfp, char *swapfilename, errcxdef *errctx)
128 {
129 mcmcx1def *ctx; /* newly-allocated cache manager context */
130 uchar *noreg chunk;/* 1st chunk of memory managed by this cache mgr */
131 mcmodef *obj; /* pointer to a cache object entry */
132 ushort siz; /* size of current thing being allocated */
133 ushort rem; /* bytes remaining in chunk */
134 int err;
135
136 NOREG((&chunk))
137
138 /* make sure 'max' is big enough - must be at least one chunk */
139 if (max < (ulong)MCMCHUNK) max = (ulong)MCMCHUNK;
140
141 /* allocate space for control structures from low-level heap */
142 rem = MCMCHUNK;
143
144 IF_DEBUG(rem += sizeof(long));
145 chunk = mchalo(errctx, rem, "mcmini");
146 IF_DEBUG((*(ulong *)chunk = 0x01010101, chunk += sizeof(ulong),
147 rem -= sizeof(ulong)));
148
149 ctx = (mcmcx1def *)chunk; /* put context at start of chunk */
150
151 /* initialize swapper; clean up if it fails */
152 ERRBEGIN(errctx)
153 mcsini(&ctx->mcmcxswc, ctx, swapsize, swapfp, swapfilename, errctx);
154 ERRCATCH(errctx, err)
155 mcsclose(&ctx->mcmcxswc);
156 mchfre(chunk);
157 errsig(errctx, err);
158 ERREND(errctx)
159
160 chunk += sizeof(mcmcx1def); /* rest of chunk is after context */
161 rem -= sizeof(mcmcx1def); /* remove from remaining size counter */
162
163 /* allocate the page table (an array of pointers to pages) */
164 ctx->mcmcxtab = (mcmodef **)chunk; /* put at bottom of chunk */
165 siz = pages * sizeof(mcmodef *); /* calcuate size of table */
166
167 memset(ctx->mcmcxtab, 0, (size_t)siz); /* clear entire table */
168 chunk += siz; /* reflect size of table */
169 rem -= siz; /* take it out of the remaining count */
170
171 /* here we begin normal heap marking with object references */
172 ctx->mcmcxhpch = (mcmhdef *)chunk; /* set start of heap chain */
173 chunk += sizeof(mcmhdef);
174 rem -= sizeof(mcmhdef);
175 ctx->mcmcxhpch->mcmhnxt = (mcmhdef *)0; /* no next heap in chain yet */
176
177 /* allocate the first page */
178 *(mcmon *)chunk = 0; /* set object number header in chunk */
179 chunk += osrndsz(sizeof(mcmon));
180 rem -= osrndsz(sizeof(mcmon));
181
182 ctx->mcmcxtab[0] = (mcmodef *)chunk; /* put at bottom of chunk */
183 memset(ctx->mcmcxtab[0], 0, (size_t)MCMPAGESIZE);
184 chunk += MCMPAGESIZE; /* reflect size of page */
185 rem -= MCMPAGESIZE; /* take it out of the remainder */
186
187 /* set up the first page with an entry for itself */
188 obj = mcmgobje(ctx, (mcmon)0); /* point to first page entry */
189 obj->mcmoflg = MCMOFPRES | MCMOFNODISC | MCMOFPAGE | MCMOFNOSWAP;
190 obj->mcmoptr = (uchar *)ctx->mcmcxtab[0];
191 obj->mcmosiz = MCMPAGESIZE;
192
193 /* set up the rest of the context */
194 ctx->mcmcxlru = ctx->mcmcxmru = MCMONINV; /* no mru/lru list yet */
195 ctx->mcmcxmax = max - (ulong)MCMCHUNK;
196 ctx->mcmcxpage = 1; /* next page slot to be allocated will be #1 */
197 ctx->mcmcxpgmx = pages; /* max number of pages we can allocate */
198 ctx->mcmcxerr = errctx;
199 ctx->mcmcxcsw = mcmcswf;
200
201 /* set up the free list with the remainder of the chunk */
202 ctx->mcmcxfre = 1; /* we've allocated object 0; obj 1 is free space */
203 obj = mcmgobje(ctx, ctx->mcmcxfre); /* point to free object entry */
204 obj->mcmonxt = obj->mcmoprv = MCMONINV; /* end of free list */
205 obj->mcmoflg = MCMOFFREE; /* mark the free block as such */
206 *(mcmon *)chunk = ctx->mcmcxfre; /* set free list header */
207
208 chunk += osrndsz(sizeof(mcmon));
209 rem -= osrndsz(sizeof(mcmon));
210 obj->mcmoptr = chunk; /* rest of chunk */
211
212 obj->mcmosiz = rem - osrndsz(sizeof(mcmon)); /* remaining size in chunk */
213
214 /* set flag for end of chunk (invalid object header) */
215 *((mcmon *)(chunk + rem - osrndsz(sizeof(mcmon)))) = MCMONINV;
216
217 /* set up the unused entry list with the remaining headers in the page */
218 mcmadpg(ctx, 0, 2);
219
220 return(ctx);
221 }
222
223 /*
224 * Uninitialize the cache manager. Frees the memory allocated for the
225 * cache, including the context structure itself.
226 */
227 void mcmterm(mcmcx1def *ctx)
228 {
229 mcmhdef *cur, *nxt;
230
231 /*
232 * Free each chunk in the cache block list, *except* the last one. The
233 * last one is special: it's actually the first chunk allocated, since
234 * we build the list in reverse order, and the first chunk pointer
235 * points into the middle of the actual allocation block, since we
236 * sub-allocated the context structure itself and the page table out of
237 * that memory.
238 */
239 for (cur = ctx->mcmcxhpch ; cur != 0 && cur->mcmhnxt != 0 ; cur = nxt)
240 {
241 /* remember the next chunk, and delete this one */
242 nxt = cur->mcmhnxt;
243 mchfre(cur);
244 }
245
246 /*
247 * As described above, the last chunk in the list is the first
248 * allocated, and it points into the middle of the actual allocated
249 * memory block. Luckily, we do have a handy pointer to the start of
250 * the memory block, namely the context pointer - it's the first thing
251 * allocated out of the block, so it's the same as the block pointer.
252 * Freeing the context frees this last/first chunk.
253 */
254 mchfre(ctx);
255 }
256
257 /*
258 * Allocate a new object, returning a pointer to its memory. The new
259 * object is locked upon return. The object number for the new object
260 * is returned at *nump.
261 */
262 static uchar *mcmalo1(mcmcx1def *ctx, ushort siz, mcmon *nump)
263 {
264 mcmon n;
265 mcmodef *o;
266 uchar *chunk;
267
268 MCMGLBCTX(ctx);
269
270 /* round size to appropriate multiple */
271 siz = osrndsz(siz);
272
273 /* if it's bigger than the chunk size, we can't allocate it */
274 if (siz > MCMCHUNK)
275 errsig(ctx->mcmcxerr, ERR_BIGOBJ);
276
277 startover:
278 /* look in the free block chain for a fit to the request */
279 o = mcmffb(ctx, siz, &n);
280 if (n != MCMONINV)
281 {
282 mcmsplt(ctx, n, siz); /* split the block if necessary */
283 mcmgobje(ctx, n)->mcmoflg = MCMOFNODISC | MCMOFLOCK | MCMOFPRES;
284 mcmgobje(ctx, n)->mcmolcnt = 1; /* one locker so far */
285 *nump = n;
286 return(o->mcmoptr);
287 }
288
289 /* nothing found; we must get space out of the heap if possible */
290 chunk = mcmhalo(ctx); /* get space from heap */
291 if (!chunk) goto error; /* can't get any more space from heap */
292 o = mcmoal(ctx, &n); /* set up cache entry for free space */
293 if (n == MCMONINV)
294 {
295 mcmhdef *chunk_hdr = ((mcmhdef *)chunk) - 1;
296 ctx->mcmcxhpch = chunk_hdr->mcmhnxt;
297 mchfre(chunk_hdr);
298 goto error; /* any error means we can't allocate the memory */
299 }
300
301 *(mcmon *)chunk = n; /* set object header */
302 chunk += osrndsz(sizeof(mcmon));
303 o->mcmoptr = chunk;
304 o->mcmosiz = MCMCHUNK - osrndsz(sizeof(mcmon));
305 o->mcmoflg = MCMOFFREE;
306 mcmlnkhd(ctx, &ctx->mcmcxfre, n);
307 goto startover; /* try again, now that we have some memory */
308
309 error:
310 *nump = MCMONINV;
311 return((uchar *)0);
312 }
313
314 static void mcmcliexp(mcmcxdef *cctx, mcmon clinum)
315 {
316 /* add global number to client mapping table at client number */
317 if (cctx->mcmcxmtb[clinum >> 8] == (mcmon *)0)
318 {
319 mcmcx1def *ctx = cctx->mcmcxgl;
320 int i;
321 mcmon *p;
322
323 /* this page is not allocated - allocate it */
324 p = (mcmon *)mchalo(ctx->mcmcxerr, (256 * sizeof(mcmon)),
325 "client mapping page");
326 cctx->mcmcxmtb[clinum >> 8] = p;
327 for (i = 0 ; i < 256 ; ++i) *p++ = MCMONINV;
328 }
329 }
330
331 /* high-level allocate: try, collect garbage, then try again */
332 uchar *mcmalo0(mcmcxdef *cctx, ushort siz, mcmon *nump,
333 mcmon clinum, int noclitrans)
334 {
335 uchar *ret;
336 mcmcx1def *ctx = cctx->mcmcxgl; /* global context */
337 mcmon glb; /* global object number allocated */
338
339 MCMCLICTX(cctx);
340 MCMGLBCTX(ctx);
341
342 /* try once */
343 if ((ret = mcmalo1(ctx, siz, &glb)) != 0)
344 goto done;
345
346 /* collect some garbage */
347 mcmgarb(ctx);
348
349 /* try swapping until we get the memory or have nothing left to swap */
350 for ( ;; )
351 {
352 /* try again */
353 if ((ret = mcmalo1(ctx, siz, &glb)) != 0)
354 goto done;
355
356 /* nothing left to swap? */
357 if (!mcmswap(ctx, siz))
358 break;
359
360 /* try yet again */
361 if ((ret = mcmalo1(ctx, siz, &glb)) != 0)
362 goto done;
363
364 /* collect garbage once again */
365 mcmgarb(ctx);
366 }
367
368 /* try again */
369 if ((ret = mcmalo1(ctx, siz, &glb)) != 0)
370 goto done;
371
372 /* we have no other way of getting more memory, so signal an error */
373 errsig(ctx->mcmcxerr, ERR_NOMEM1);
374 NOTREACHEDV(uchar *);
375
376 done:
377 if (noclitrans)
378 {
379 *nump = glb;
380 return(ret);
381 }
382
383 /* we have an object - generate client number */
384 if (clinum == MCMONINV)
385 {
386 /* find a free number */
387 mcmon **p;
388 uint i;
389 mcmon j;
390 mcmon *q;
391 int found = FALSE;
392 int unused = -1;
393
394 for (i = 0, p = cctx->mcmcxmtb ; i < cctx->mcmcxmsz ; ++i, ++p)
395 {
396 if (*p)
397 {
398 for (j = 0, q = *p ; j < 256 ; ++j, ++q)
399 {
400 if (*q == MCMONINV)
401 {
402 found = TRUE;
403 break;
404 }
405 }
406 }
407 else if (unused == -1)
408 unused = i; /* note an unused page mapping table */
409
410 if (found) break;
411 }
412
413 if (found)
414 clinum = (i << 8) + j;
415 else if (unused != -1)
416 clinum = (unused << 8);
417 else
418 errsig(ctx->mcmcxerr, ERR_CLIFULL);
419 }
420
421 /* expand client mapping table if necessary */
422 mcmcliexp(cctx, clinum);
423
424 /* make sure the entry isn't already in use */
425 if (mcmc2g(cctx, clinum) != MCMONINV)
426 errsig(ctx->mcmcxerr, ERR_CLIUSE);
427
428 cctx->mcmcxmtb[clinum >> 8][clinum & 255] = glb;
429 if (nump) *nump = clinum;
430 return(ret);
431 }
432
433 /* reserve space for an object at a client object number */
434 void mcmrsrv(mcmcxdef *cctx, ushort siz, mcmon clinum, mclhd loadhd)
435 {
436 mcmcx1def *ctx = cctx->mcmcxgl; /* global context */
437 mcmon glb; /* global object number allocated */
438 mcmodef *o;
439
440 MCMCLICTX(cctx);
441 MCMGLBCTX(ctx);
442
443 o = mcmoal(ctx, &glb); /* get a new object header */
444 if (!o) errsig(ctx->mcmcxerr, ERR_NOHDR); /* can't get a new header */
445
446 o->mcmoldh = loadhd;
447 o->mcmoflg = 0;
448 o->mcmosiz = siz;
449
450 mcmcliexp(cctx, clinum);
451 if (mcmc2g(cctx, clinum) != MCMONINV)
452 errsig(ctx->mcmcxerr, ERR_CLIUSE);
453
454 cctx->mcmcxmtb[clinum >> 8][clinum & 255] = glb;
455 }
456
457 /* resize an existing object */
458 uchar *mcmrealo(mcmcxdef *cctx, mcmon cliobj, ushort newsize)
459 {
460 mcmcx1def *ctx = cctx->mcmcxgl; /* global context */
461 mcmon obj = mcmc2g(cctx, cliobj);
462 mcmodef *o = mcmgobje(ctx, obj);
463 mcmon nxt;
464 mcmodef *nxto;
465 uchar *p;
466 int local_lock;
467
468 MCMCLICTX(cctx);
469 MCMGLBCTX(ctx);
470
471 newsize = osrndsz(newsize);
472
473 /* make sure the object is locked, and note if we locked it */
474 if ((local_lock = !(o->mcmoflg & MCMOFLOCK)) != 0)
475 (void)mcmlck(cctx, cliobj);
476
477 ERRBEGIN(ctx->mcmcxerr)
478
479 if (newsize < o->mcmosiz)
480 mcmsplt(ctx, obj, newsize); /* smaller; just split block */
481 else
482 {
483 /* see if there's a free block after this block */
484 p = o->mcmoptr;
485 nxt = *(mcmon *)(p + o->mcmosiz);
486 nxto = (nxt == MCMONINV) ? (mcmodef *)0 : mcmgobje(ctx, nxt);
487
488 if (nxto && ((nxto->mcmoflg & MCMOFFREE)
489 && nxto->mcmosiz >= newsize - o->mcmosiz))
490 {
491 /* sanity check - make sure heap and page table agree */
492 assert(nxto->mcmoptr == p + o->mcmosiz + osrndsz(sizeof(mcmon)));
493 /* annex the free block */
494 o->mcmosiz += nxto->mcmosiz + osrndsz(sizeof(mcmon));
495 /* move the free block to the unused list */
496 mcmunl(ctx, nxt, &ctx->mcmcxfre);
497 nxto->mcmonxt = ctx->mcmcxunu;
498 ctx->mcmcxunu = nxt;
499 nxto->mcmoflg = 0;
500
501 /* split the newly grown block if necessary */
502 mcmsplt(ctx, obj, newsize);
503 }
504 else
505 {
506 /* can't annex; allocate new memory and copy */
507
508 if (o->mcmolcnt != 1) /* if anyone else has a lock... */
509 errsig(ctx->mcmcxerr, ERR_REALCK); /* we can't move it */
510
511 p = mcmalo0(cctx, newsize, &nxt, MCMONINV, TRUE);
512 if (nxt == MCMONINV) errsig(ctx->mcmcxerr, ERR_NOMEM2);
513 memcpy(p, o->mcmoptr, (size_t)o->mcmosiz);
514
515 /* adjust the object entries */
516 nxto = mcmgobje(ctx, nxt); /* get pointer to new entry */
517 newsize = nxto->mcmosiz; /* get actual size of new block */
518 nxto->mcmoptr = o->mcmoptr; /* copy current block info to new */
519 nxto->mcmosiz = o->mcmosiz;
520 o->mcmoptr = p; /* copy new block info to original entry */
521 o->mcmosiz = newsize;
522
523 /* now fix up the heap pointers, and free the temp object */
524 *(mcmon *)(p - osrndsz(sizeof(mcmon))) = obj;
525 *(mcmon *)(nxto->mcmoptr - osrndsz(sizeof(mcmon))) = nxt;
526 mcmgunlck(ctx, nxt);
527 mcmgfre(ctx, nxt);
528 }
529 }
530
531 ERRCLEAN(ctx->mcmcxerr)
532 /* release our lock, if we had to obtain one */
533 if (local_lock) mcmunlck(cctx, cliobj);
534 ERRENDCLN(ctx->mcmcxerr)
535
536 /* return the address of the object */
537 return(o->mcmoptr);
538 }
539
540 /*
541 * Free an object by GLOBAL number: move object to free list.
542 */
543 void mcmgfre(mcmcx1def *ctx, mcmon obj)
544 {
545 mcmodef *o = mcmgobje(ctx, obj);
546
547 MCMGLBCTX(ctx);
548
549 /* signal an error if the object is locked */
550 if (o->mcmolcnt) errsig(ctx->mcmcxerr, ERR_LCKFRE);
551
552 /* take out of LRU chain if it's in the chain */
553 if (o->mcmoflg & MCMOFLRU) mcmunl(ctx, obj, &ctx->mcmcxlru);
554
555 /* put it in the free list */
556 mcmlnkhd(ctx, &ctx->mcmcxfre, obj);
557 o->mcmoflg = MCMOFFREE;
558 }
559
560 /*
561 * load and lock an object that has been swapped out or discarded
562 */
563 uchar *mcmload(mcmcxdef *cctx, mcmon cnum)
564 {
565 mcmcx1def *ctx = cctx->mcmcxgl;
566 mcmodef *o = mcmobje(cctx, cnum);
567 mcmodef *newdef;
568 mcmon newn;
569 mcmon num = mcmc2g(cctx, cnum);
570
571 MCMCLICTX(cctx);
572 MCMGLBCTX(ctx);
573
574 /* we first need to obtain some memory for this object */
575 (void)mcmalo0(cctx, o->mcmosiz, &newn, MCMONINV, TRUE);
576 newdef = mcmgobje(ctx, newn);
577
578 /* use memory block from our new object */
579 o->mcmoptr = newdef->mcmoptr;
580 o->mcmosiz = newdef->mcmosiz;
581
582 /* load or swap the object in */
583 ERRBEGIN(ctx->mcmcxerr)
584 if (o->mcmoflg & (MCMOFNODISC | MCMOFDIRTY))
585 mcsin(&ctx->mcmcxswc, o->mcmoswh, o->mcmoptr, o->mcmosiz);
586 else if (cctx->mcmcxldf)
587 (*cctx->mcmcxldf)(cctx->mcmcxldc, o->mcmoldh, o->mcmoptr,
588 o->mcmosiz);
589 else
590 errsig(ctx->mcmcxerr, ERR_NOLOAD);
591 ERRCLEAN(ctx->mcmcxerr)
592 mcmgunlck(ctx, newn); /* unlock the object */
593 mcmgfre(ctx, newn); /* don't need new memory after all */
594 ERRENDCLN(ctx->mcmcxerr)
595
596 /* unuse the new cache entry we obtained (we just wanted the memory) */
597 /* @@@ */
598 *(mcmon *)(o->mcmoptr - osrndsz(sizeof(mcmon))) = num; /* set obj# */
599 newdef->mcmoflg = 0; /* mark new block as unused */
600 newdef->mcmonxt = ctx->mcmcxunu; /* link to unused chain */
601 ctx->mcmcxunu = newn;
602
603 /* set flags in the newly loaded object and return */
604 o->mcmoflg |= MCMOFPRES | MCMOFLOCK; /* object is now present in memory */
605 o->mcmoflg &= ~MCMOFDIRTY; /* not written since last swapped in */
606 o->mcmoflg |= MCMOFNODISC; /* don't discard once it's been to swap file */
607 o->mcmolcnt = 1; /* one locker so far */
608
609 /* if the object is to be reverted upon loading, revert it now */
610 if (o->mcmoflg & MCMOFREVRT)
611 {
612 (*cctx->mcmcxrvf)(cctx->mcmcxrvc, cnum);
613 o->mcmoflg &= ~MCMOFREVRT;
614 }
615
616 return(o->mcmoptr);
617 }
618
619 /*
620 * Allocate a new object header. This doesn't allocate an object, just
621 * the header for one.
622 */
623 static mcmodef *mcmoal(mcmcx1def *ctx, mcmon *nump)
624 {
625 mcmodef *ret;
626 uint pagenum;
627
628 MCMGLBCTX(ctx);
629
630 /* look first in list of unused headers */
631 startover:
632 if (ctx->mcmcxunu != MCMONINV)
633 {
634 /* we have something in the unused list; return it */
635 *nump = ctx->mcmcxunu;
636 ret = mcmgobje(ctx, *nump);
637 ctx->mcmcxunu = ret->mcmonxt;
638 ret->mcmoswh = MCSSEGINV;
639 return(ret);
640 }
641
642 /*
643 * No unused entries: we must create a new page. To do so, we
644 * simply allocate memory for a new page. Allocate the memory
645 * ourselves, to avoid deadlocking with the allocator (which can
646 * try to get a new entry to satisfy our request for memory).
647 */
648 if (ctx->mcmcxpage == ctx->mcmcxpgmx) goto error; /* no more pages */
649 pagenum = ctx->mcmcxpage++; /* get a new page slot */
650
651 ctx->mcmcxtab[pagenum] =
652 (mcmodef *)mchalo(ctx->mcmcxerr, MCMPAGESIZE, "mcmoal");
653 mcmadpg(ctx, pagenum, MCMONINV);
654 goto startover;
655
656 error:
657 *nump = MCMONINV;
658 return((mcmodef *)0);
659 }
660
661 /* find free block: find a block from the free pool to satisfy allocation */
662 static mcmodef *mcmffb(mcmcx1def *ctx, ushort siz, mcmon *nump)
663 {
664 mcmon n;
665 mcmodef *o;
666 mcmon minn;
667 mcmodef *mino;
668 ushort min;
669
670 MCMGLBCTX(ctx);
671
672 for (minn = MCMONINV, mino = 0, n = ctx->mcmcxfre ; n != MCMONINV ;
673 n = o->mcmonxt)
674 {
675 o = mcmgobje(ctx, n);
676 if (o->mcmosiz == siz)
677 {
678 /* found exact match - use it immediately */
679 minn = n;
680 min = siz;
681 mino = o;
682 break;
683 }
684 else if (o->mcmosiz > siz)
685 {
686 /* found something at least as big; is it smallest yet? */
687 if (minn == MCMONINV || o->mcmosiz < min)
688 {
689 /* yes, best fit so far, use it; but keep looking */
690 minn = n;
691 mino = o;
692 min = o->mcmosiz;
693 }
694 }
695 }
696
697 /* if we found something, remove from the free list */
698 if (minn != MCMONINV)
699 {
700 mcmunl(ctx, minn, &ctx->mcmcxfre);
701 mino->mcmoflg &= ~MCMOFFREE;
702 mino->mcmoswh = MCSSEGINV;
703 }
704
705 *nump = minn;
706 return mino;
707 }
708
709 /*
710 * unlink an object header from one of the doubly-linked lists
711 */
712 static void mcmunl(mcmcx1def *ctx, mcmon n, mcmon *lst)
713 {
714 mcmodef *o = mcmgobje(ctx, n);
715 mcmodef *nxt;
716 mcmodef *prv;
717
718 MCMGLBCTX(ctx);
719
720 /* see if this is LRU chain - must deal with MRU pointer if so */
721 if (lst == &ctx->mcmcxlru)
722 {
723 /* if it's at MRU, set MRU pointer to previous object in list */
724 if (ctx->mcmcxmru == n)
725 {
726 ctx->mcmcxmru = o->mcmoprv; /* set MRU to previous in chain */
727 if (ctx->mcmcxmru != MCMONINV) /* set nxt for new MRU */
728 mcmgobje(ctx, ctx->mcmcxmru)->mcmonxt = MCMONINV;
729 else
730 ctx->mcmcxlru = MCMONINV; /* nothing in list; clear LRU */
731 }
732 o->mcmoflg &= ~MCMOFLRU;
733 }
734
735 nxt = o->mcmonxt == MCMONINV ? (mcmodef *)0 : mcmgobje(ctx, o->mcmonxt);
736 prv = o->mcmoprv == MCMONINV ? (mcmodef *)0 : mcmgobje(ctx, o->mcmoprv);
737
738 /* set back link for next object, if there is a next object */
739 if (nxt) nxt->mcmoprv = o->mcmoprv;
740
741 /* set forward link for previous object, or head if no previous object */
742 if (prv) prv->mcmonxt = o->mcmonxt;
743 else *lst = o->mcmonxt;
744
745 o->mcmonxt = o->mcmoprv = MCMONINV;
746 }
747
748 /* link an item to the head of a doubly-linked list */
749 static void mcmlnkhd(mcmcx1def *ctx, mcmon *lst, mcmon n)
750 {
751 MCMGLBCTX(ctx);
752
753 if (*lst != MCMONINV) mcmgobje(ctx, *lst)->mcmoprv = n;
754 mcmgobje(ctx, n)->mcmonxt = *lst; /* next is previous head of list */
755 *lst = n; /* make object new head of list */
756 mcmgobje(ctx, n)->mcmoprv = MCMONINV; /* there is no previous entry */
757 }
758
759 /* add page pagenum, initializing entries after firstunu to unused */
760 static void mcmadpg(mcmcx1def *ctx, uint pagenum, mcmon firstunu)
761 {
762 mcmon unu;
763 mcmodef *obj;
764 mcmon lastunu;
765
766 MCMGLBCTX(ctx);
767
768 unu = (firstunu == MCMONINV ? pagenum * MCMPAGECNT : firstunu);
769 ctx->mcmcxunu = unu;
770 lastunu = (pagenum * MCMPAGECNT) + MCMPAGECNT - 1;
771 for (obj = mcmgobje(ctx, unu) ; unu < lastunu ; ++obj)
772 obj->mcmonxt = ++unu;
773 obj->mcmonxt = MCMONINV;
774 }
775
776 /*
777 * split a previously-free block into two chunks, adding the remainder
778 * back into the free list, if there's enough left over
779 */
780 static void mcmsplt(mcmcx1def *ctx, mcmon n, ushort siz)
781 {
782 mcmodef *o = mcmgobje(ctx, n);
783 mcmon newn;
784 mcmodef *newp;
785
786 MCMGLBCTX(ctx);
787
788 if (o->mcmosiz < siz + MCMSPLIT) return; /* don't split; we're done */
789
790 newp = mcmoal(ctx, &newn);
791 if (newn == MCMONINV) return; /* ignore error - just skip split */
792
793 /* set up the new entry, and link into free list */
794 *(mcmon *)(o->mcmoptr + siz) = newn;
795 newp->mcmoptr = o->mcmoptr + siz + osrndsz(sizeof(mcmon));
796 newp->mcmosiz = o->mcmosiz - siz - osrndsz(sizeof(mcmon));
797 newp->mcmoflg = MCMOFFREE;
798 mcmlnkhd(ctx, &ctx->mcmcxfre, newn);
799
800 o->mcmosiz = siz; /* size of new object is now exactly as request */
801 }
802
803 /* allocate a new chunk from the heap if possible */
804 static uchar *mcmhalo(mcmcx1def *ctx)
805 {
806 uchar *chunk;
807 int err;
808 # define size (MCMCHUNK + sizeof(mcmhdef) + 2*osrndsz(sizeof(mcmon)))
809
810 VARUSED(err);
811
812 MCMGLBCTX(ctx);
813
814 if (ctx->mcmcxmax < MCMCHUNK) return((uchar *)0);
815
816 ERRBEGIN(ctx->mcmcxerr)
817 chunk = mchalo(ctx->mcmcxerr, size, "mcmhalo");
818 ERRCATCH(ctx->mcmcxerr, err)
819 ctx->mcmcxmax = 0; /* remember we can't allocate anything more */
820 return((uchar *)0); /* return no memory */
821 ERREND(ctx->mcmcxerr)
822
823 ctx->mcmcxmax -= MCMCHUNK;
824
825 /* link into heap chain */
826 ((mcmhdef *)chunk)->mcmhnxt = ctx->mcmcxhpch;
827 ctx->mcmcxhpch = (mcmhdef *)chunk;
828 /*@@@@*/
829 *(mcmon *)(chunk + osrndsz(sizeof(mcmhdef) + MCMCHUNK)) = MCMONINV;
830 return(chunk + sizeof(mcmhdef));
831
832 # undef size
833 }
834
835 /* "use" an object - move to most-recent position in LRU chain */
836 void mcmuse(mcmcx1def *ctx, mcmon obj)
837 {
838 mcmodef *o = mcmgobje(ctx, obj);
839
840 MCMGLBCTX(ctx);
841
842 if (ctx->mcmcxmru == obj) return; /* already MRU; nothing to do */
843
844 /* remove from LRU chain if it's in it */
845 if (o->mcmoflg & MCMOFLRU) mcmunl(ctx, obj, &ctx->mcmcxlru);
846
847 /* set forward pointer of last block, if there is one */
848 if (ctx->mcmcxmru != MCMONINV)
849 mcmgobje(ctx, ctx->mcmcxmru)->mcmonxt = obj;
850
851 o->mcmoprv = ctx->mcmcxmru; /* point back to previous MRU */
852 o->mcmonxt = MCMONINV; /* nothing in list after this one */
853 ctx->mcmcxmru = obj; /* point MRU to new block */
854
855 /* if there's nothing in the chain at all, set LRU to this block, too */
856 if (ctx->mcmcxlru == MCMONINV) ctx->mcmcxlru = obj;
857
858 /* note that object is in LRU chain */
859 o->mcmoflg |= MCMOFLRU;
860 }
861
862 /* find next free block in a heap, starting with pointer */
863 static uchar *mcmffh(mcmcx1def *ctx, uchar *p)
864 {
865 mcmodef *o;
866
867 MCMGLBCTX(ctx);
868
869 while (*(mcmon *)p != MCMONINV)
870 {
871 o = mcmgobje(ctx, *(mcmon *)p);
872 assert(o->mcmoptr == p + osrndsz(sizeof(mcmon)));
873 if (o->mcmoflg & MCMOFFREE) return(p);
874 p += osrndsz(sizeof(mcmon)) + o->mcmosiz; /* move on to next chunk */
875 }
876 return((uchar *)0); /* no more free blocks in heap */
877 }
878
879 #ifdef NEVER
880 static void mcmmove(mcmcx1def *ctx, mcmodef *o, uchar *newpage)
881 {
882 mcmodef **page;
883
884 MCMGLBCTX(ctx);
885
886 /* see if we need to update page table (we do if moving a page) */
887 if (o->mcmoflg & MCMOFPAGE)
888 {
889 for (page = ctx->mcmcxtab ; *page ; ++page)
890 {
891 if (*page == (mcmodef *)(o->mcmoptr))
892 {
893 *page = (mcmodef *)newpag;
894 break;
895 }
896 }
897 if (!*page) printf("\n*** internal error - relocating page\n");
898 }
899 o->mcmoptr = newpage;
900 }
901 #endif /* NEVER */
902
903 /* relocate blocks from p to (but not including) q */
904 static uchar *mcmreloc(mcmcx1def *ctx, uchar *p, uchar *q)
905 {
906 mcmodef *o;
907 ushort dist;
908 mcmon objnum;
909
910 MCMGLBCTX(ctx);
911
912 objnum = *(mcmon *)p; /* get number of free block being bubbled up */
913 o = mcmgobje(ctx, objnum); /* get pointer to free object */
914 assert(o->mcmoptr == p + osrndsz(sizeof(mcmon)));
915 dist = osrndsz(sizeof(mcmon)) + o->mcmosiz; /* compute distance to move */
916 mcmmove(ctx, o, q - dist + osrndsz(sizeof(mcmon))); /* move obj to top */
917
918 memmove(p, p+dist, (size_t)(q - p - o->mcmosiz)); /* move memory */
919
920 /* update cache entries for the blocks we moved */
921 while (p != q - dist)
922 {
923 mcmmove(ctx, mcmgobje(ctx, *(mcmon *)p), p + osrndsz(sizeof(mcmon)));
924 p = mcmnxh(ctx, p);
925 }
926
927 *(mcmon *)(q - dist) = objnum; /* set bubbled num */
928 return(q - dist); /* return new location of bubbled block */
929 }
930
931 /* consolidate the two (free) blocks starting at p into one block */
932 static void mcmconsol(mcmcx1def *ctx, uchar *p)
933 {
934 uchar *q;
935 mcmodef *obj1, *obj2;
936
937 MCMGLBCTX(ctx);
938
939 q = mcmnxh(ctx, p);
940 obj1 = mcmgobje(ctx, *(mcmon *)p);
941 obj2 = mcmgobje(ctx, *(mcmon *)q);
942
943 assert(obj1->mcmoptr == p + osrndsz(sizeof(mcmon)));
944 assert(obj2->mcmoptr == q + osrndsz(sizeof(mcmon)));
945
946 obj1->mcmosiz += osrndsz(sizeof(mcmon)) + obj2->mcmosiz;
947 mcmunl(ctx, *(mcmon *)q, &ctx->mcmcxfre);
948
949 /* add second object entry to unused list */
950 obj2->mcmonxt = ctx->mcmcxunu;
951 ctx->mcmcxunu = *(mcmon *)q;
952 obj2->mcmoflg = 0;
953 }
954
955 /* attempt to compact all heaps by consolidating free space */
956 static void mcmgarb(mcmcx1def *ctx)
957 {
958 mcmhdef *h;
959 uchar *p;
960 uchar *q;
961 uchar *nxt;
962 ushort flags;
963
964 MCMGLBCTX(ctx);
965
966 for (h = ctx->mcmcxhpch ; h ; h = h->mcmhnxt)
967 {
968 p = (uchar *)(h+1); /* get pointer to actual heap */
969 p = mcmffh(ctx, p); /* get first free block in heap */
970 if (!p) continue; /* can't do anything - no free blocks */
971 nxt = mcmnxh(ctx, p); /* remember immediate next block */
972
973 for (q=p ;; )
974 {
975 q = mcmnxh(ctx, q); /* find next chunk in heap */
976 if (*(mcmon *)q == MCMONINV) break; /* reached end of heap */
977 assert(mcmgobje(ctx, *(mcmon *)q)->mcmoptr
978 == q + osrndsz(sizeof(mcmon)));
979 flags = mcmgobje(ctx, *(mcmon *)q)->mcmoflg; /* get flags */
980
981 /* if the block is locked, p can't be relocated */
982 if (flags & MCMOFLOCK)
983 {
984 p = mcmffh(ctx, q); /* find next free block after p */
985 q = p;
986 if (p) continue; /* try again; start with next free block */
987 else break; /* no more free blocks - done with heap */
988 }
989
990 /* if the block is free, we can relocate between p and q */
991 if (flags & MCMOFFREE)
992 {
993 if (q != nxt) p = mcmreloc(ctx, p, q); /* relocate */
994 mcmconsol(ctx, p); /* consolidate two free blocks */
995
996 /* resume looking, starting with consolidated block */
997 nxt = mcmnxh(ctx, p);
998 q = p;
999 continue;
1000 }
1001 }
1002 }
1003 }
1004
1005 /* toss out a particular object */
1006 static int mcmtoss(mcmcx1def *ctx, mcmon n)
1007 {
1008 mcmodef *o = mcmgobje(ctx, n);
1009 mcmodef *newp;
1010 mcmon newn;
1011
1012 MCMGLBCTX(ctx);
1013
1014 /* make a new block for the free space */
1015 newp = mcmoal(ctx, &newn);
1016 if (newn == MCMONINV)
1017 return(FALSE); /* ignore the error, but can't toss it out */
1018
1019 /* write object to swap file if not discardable */
1020 if (o->mcmoflg & (MCMOFNODISC | MCMOFDIRTY))
1021 {
1022 mcsseg old_swap_seg;
1023
1024 /*
1025 * If this object was last loaded out of the load file, rather
1026 * than the swap file, don't attempt to find it in the swap file
1027 * -- so note by setting the old swap segment parameter to null.
1028 */
1029 if (!(o->mcmoflg & MCMOFNODISC))
1030 old_swap_seg = o->mcmoswh;
1031 else
1032 old_swap_seg = MCSSEGINV;
1033
1034 o->mcmoswh = mcsout(&ctx->mcmcxswc, (uint)n, o->mcmoptr, o->mcmosiz,
1035 old_swap_seg, o->mcmoflg & MCMOFDIRTY);
1036 }
1037
1038 /* give the object's space to the newly created block */
1039 newp->mcmoptr = o->mcmoptr;
1040 newp->mcmosiz = o->mcmosiz;
1041 newp->mcmoflg = MCMOFFREE;
1042 /*@@@*/
1043 *(mcmon *)(o->mcmoptr - osrndsz(sizeof(mcmon))) = newn;
1044 mcmlnkhd(ctx, &ctx->mcmcxfre, newn);
1045
1046 o->mcmoflg &= ~MCMOFPRES; /* object is no longer in memory */
1047 mcmunl(ctx, n, &ctx->mcmcxlru); /* remove from LRU list */
1048 return(TRUE); /* successful, so return TRUE */
1049 }
1050
1051 /* swap or discard to make room for siz; return 0 if nothing swapped */
1052 static int mcmswap(mcmcx1def *ctx, ushort siz)
1053 {
1054 mcmon n;
1055 mcmodef *o;
1056 mcmon nxt;
1057 int pass; /* pass 1: swap one piece big enough */
1058 /* pass 2: swap enough pieces to add up to right size */
1059 ushort tot;
1060
1061 MCMGLBCTX(ctx);
1062
1063 for (pass = 1, tot = 0 ; pass < 3 && tot < siz ; ++pass)
1064 {
1065 for (n = ctx->mcmcxlru ; n != MCMONINV && tot < siz ; n = nxt)
1066 {
1067 o = mcmgobje(ctx, n);
1068 nxt = o->mcmonxt; /* get next now, as we may unlink */
1069 if (!(o->mcmoflg & (MCMOFLOCK | MCMOFNOSWAP | MCMOFPAGE))
1070 && (pass == 2 || o->mcmosiz >= siz))
1071 {
1072 /* toss out, and add into size if successful */
1073 if (mcmtoss(ctx, n)) tot += o->mcmosiz;
1074 }
1075 }
1076 }
1077
1078 /* if we managed to remove anything, return TRUE, otherwise FALSE */
1079 return(tot != 0);
1080 }
1081
1082 /* compute size of cache */
1083 ulong mcmcsiz(mcmcxdef *cctx)
1084 {
1085 mcmcx1def *ctx = cctx->mcmcxgl;
1086 mcmhdef *p;
1087 ulong tot;
1088
1089 MCMCLICTX(cctx);
1090 MCMGLBCTX(ctx);
1091
1092 /* count number of heaps, adding in chunk size for each */
1093 for (tot = 0, p = ctx->mcmcxhpch ; p ; p = p->mcmhnxt)
1094 tot += MCMCHUNK;
1095
1096 return(tot);
1097 }
1098
1099 #ifdef MCM_NO_MACRO
1100 /* routines that can be either macros or functions */
1101
1102 uchar *mcmlck(mcmcxdef *ctx, mcmon objnum)
1103 {
1104 mcmodef *o = mcmobje(ctx, objnum);
1105
1106 if ((o->mcmoflg & MCMOFFREE) != 0 || mcmc2g(ctx, objnum) == MCMONINV)
1107 {
1108 errsig(ctx->mcmcxgl->mcmcxerr, ERR_INVOBJ);
1109 return 0;
1110 }
1111 else if (o->mcmoflg & MCMOFPRES)
1112 {
1113 o->mcmoflg |= MCMOFLOCK;
1114 ++(o->mcmolcnt);
1115 return(o->mcmoptr);
1116 }
1117 else
1118 return(mcmload(ctx, objnum));
1119 }
1120
1121 void mcmunlck(mcmcxdef *ctx, mcmon obj)
1122 {
1123 mcmodef *o = mcmobje(ctx, obj);
1124
1125 if (o->mcmoflg & MCMOFLOCK)
1126 {
1127 if (!(--(o->mcmolcnt)))
1128 {
1129 o->mcmoflg &= ~MCMOFLOCK;
1130 mcmuse(ctx->mcmcxgl, mcmc2g(ctx, obj));
1131 }
1132 }
1133 }
1134
1135 void mcmgunlck(mcmcx1def *ctx, mcmon obj)
1136 {
1137 mcmodef *o = mcmgobje(ctx, obj);
1138
1139 if (o->mcmoflg & MCMOFLOCK)
1140 {
1141 if (!(--(o->mcmolcnt)))
1142 {
1143 o->mcmoflg &= ~MCMOFLOCK;
1144 mcmuse(ctx, obj);
1145 }
1146 }
1147 }
1148
1149 #endif /* MCM_NO_MACRO */
1150
1151 /*
1152 * Change an object's swap file handle. This routine will only be
1153 * called for an object that is either present or swapped out (i.e., an
1154 * object with a valid mcsseg number in its swap state).
1155 */
1156 void mcmcswf(mcmcx1def *ctx, mcmon objn, mcsseg swapn, mcsseg oldswapn)
1157 {
1158 mcmodef *o = mcmgobje(ctx, objn);
1159
1160 MCMGLBCTX(ctx);
1161
1162 /*
1163 * Reset the swap number only if the object is swapped out and its
1164 * swap file number matches the old one, or the object is currently
1165 * present (in which case the swap file number is irrelevant and can
1166 * be replaced).
1167 */
1168 if (((o->mcmoflg & (MCMOFDIRTY | MCMOFNODISC)) && o->mcmoswh == oldswapn)
1169 || (o->mcmoflg & MCMOFPRES))
1170 o->mcmoswh = swapn;
1171 }
1172
1173
1174 void mcmfre(mcmcxdef *ctx, mcmon obj)
1175 {
1176 /* free the actual object */
1177 mcmgfre(ctx->mcmcxgl, mcmc2g(ctx, obj));
1178
1179 /* unmap the client object number */
1180 mcmc2g(ctx, obj) = MCMONINV;
1181 }
1182