Debian Codegraph @

terps/tads/tads2/mcm.h

raw symbols SCIP
1 /*
2 $Header: d:/cvsroot/tads/TADS2/MCM.H,v 1.2 1999/05/17 02:52:12 MJRoberts Exp $
3 */
4
5 /*
6 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
7 *
8 * Please see the accompanying license file, LICENSE.TXT, for information
9 * on using and copying this software.
10 */
11 /*
12 Name
13 mcm.h - memory cache manager
14 Function
15 Definitions for memory cache manager
16 Notes
17 None
18 Modified
19 08/03/91 MJRoberts - creation
20 */
21
22 #ifndef MCM_INCLUDED
23 #define MCM_INCLUDED
24
25 #ifndef OS_INCLUDED
26 # include "os.h"
27 #endif
28 #ifndef STD_INCLUDED
29 # include "std.h"
30 #endif
31 #ifndef MCS_INCLUDED
32 # include "mcs.h"
33 #endif
34 #ifndef MCL_INCLUDED
35 # include "mcl.h"
36 #endif
37
38 #include <stdio.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /* if the os layer doesn't want long mcm macros, we don't either */
46 #ifdef OS_MCM_NO_MACRO
47 # define MCM_NO_MACRO
48 #endif
49
50 /*
51 * mcmon - cache object number. Each object in the cache is referenced
52 * by an object number, which is a number of this type.
53 */
54 typedef ushort mcmon;
55
56 /* invalid object number - used to indicate N/A or no object */
57 #define MCMONINV ((mcmon)~0)
58
59 /* invalid page number */
60 #define MCMPINV ((uint)~0)
61
62 /*
63 * mcmodef - cache object definition. Each cache object is managed by
64 * this structure. Pointers for a doubly-linked list are provided;
65 * these are used to maintain a recent-use list for objects in memory,
66 * and to maintain a free list for cache object entries not in use. A
67 * set of flag bits is provided, as is the size of the object and its
68 * location (which is a memory pointer for objects in memory, a swap
69 * file handle for swapped-out objects, or a load-file handle for
70 * objects that are unloaded).
71 */
72 typedef struct mcmodef mcmodef;
73 struct mcmodef
74 {
75 uchar *mcmoptr; /* memory pointer (if object is present) */
76 union
77 {
78 mcsseg mcmolocs; /* swap segment handle */
79 mclhd mcmolocl; /* load file object handle */
80 } mcmoloc; /* object's external location */
81 # define mcmoswh mcmoloc.mcmolocs
82 # define mcmoldh mcmoloc.mcmolocl
83 mcmon mcmonxt; /* next object in this list */
84 mcmon mcmoprv; /* previous object in this list */
85 ushort mcmoflg; /* flags for this cache object */
86 # define MCMOFDIRTY 0x01 /* object has been written */
87 # define MCMOFNODISC 0x02 /* not in load file (can't be discarded) */
88 # define MCMOFLOCK 0x04 /* object is locked */
89 # define MCMOFPRES 0x08 /* object is present in memory */
90 # define MCMOFLRU 0x10 /* object is in LRU chain */
91 # define MCMOFPAGE 0x20 /* object is a cache manager page */
92 # define MCMOFNOSWAP 0x40 /* object cannot be swapped out */
93 # define MCMOFFREE 0x80 /* entry refers to a free block of memory */
94 # define MCMOFREVRT 0x100 /* call revert callback upon loading */
95 uchar mcmolcnt; /* lock count */
96 ushort mcmosiz; /* size of the object */
97 };
98
99 /* heap header - allocate one of these in each heap */
100 typedef struct mcmhdef mcmhdef;
101 struct mcmhdef
102 {
103 mcmhdef *mcmhnxt; /* next heap in this chain */
104 };
105
106 /* GLOBAL cache manager context: tracks cache manager state */
107 typedef struct mcmcx1def mcmcx1def;
108 struct mcmcx1def
109 {
110 mcmodef **mcmcxtab; /* page table for the cache */
111 errcxdef *mcmcxerr; /* error handling context */
112 mcmhdef *mcmcxhpch; /* heap chain pointer */
113 mcscxdef mcmcxswc; /* swap manager context */
114 mclcxdef mcmcxldc; /* loader context */
115 ulong mcmcxmax; /* maximum amount of actual heap we can ever alloc */
116 mcmon mcmcxlru; /* least recently used object still in memory */
117 mcmon mcmcxmru; /* most recently used object */
118 mcmon mcmcxfre; /* head of free list */
119 mcmon mcmcxunu; /* head of unused list */
120 ushort mcmcxpage; /* last page table slot used */
121 ushort mcmcxpgmx; /* maximum number of pages we can allocate */
122 void (*mcmcxcsw)(mcmcx1def *, mcmon, mcsseg, mcsseg);
123 /* change swap handle in object to new swap handle */
124 };
125
126 /* CLIENT cache manager context: used by client to request mcm services */
127 typedef struct mcmcxdef mcmcxdef;
128 struct mcmcxdef
129 {
130 mcmcx1def *mcmcxgl; /* global cache manager context */
131 uint mcmcxflg; /* flags */
132 uint mcmcxmsz; /* maximum size of mapping table */
133 void (*mcmcxldf)(void *ctx, mclhd handle, uchar *ptr,
134 ushort siz); /* callback to load objects */
135 void *mcmcxldc; /* context for load callback */
136 void (*mcmcxrvf)(void *ctx, mcmon objn); /* revert object */
137 void *mcmcxrvc; /* context for revert callback */
138 mcmon *mcmcxmtb[1]; /* mapping table */
139 };
140
141 /* context flags */
142 #define MCMCXF_NO_PRP_DEL 0x0001 /* PRPFDEL is invalid in this game file */
143
144 /* convert from a client object number to a global object number */
145 /* mcmon mcmc2g(mcmcxdef *ctx, mcmon objn); */
146 #define mcmc2g(ctx, objn) ((ctx)->mcmcxmtb[(objn)>>8][(objn)&255])
147
148 /*
149 * FREE LIST: this is a list, headed by context->mcmcxfre and chained
150 * forward and back by mcmonxt and mcmoprv, consisting of free memory
151 * blocks. These refer to blocks in the heap that are not used by any
152 * client objects.
153 */
154 /*
155 * UNUSED LIST: this is a list, headed by context->mcmcxunu and chained
156 * forward by mcmonxt (not back, because it's never necessary to take
157 * anything out of the list except at the head, nor to search the list
158 * backwards), of unused cache object entries. These entries are not
159 * associated with any client object or with any heap memory. This list
160 * is used to get a new cache object header, and deleted cache objects
161 * are placed onto this list.
162 */
163 /*
164 * LRU LIST: this is a list of in-memory blocks in ascending order of
165 * recency of use by the client. Each time a client unlocks a block,
166 * the block is moved to the most recent position in the list (the end
167 * of the list). To make it fast to add a new object, we keep a pointer
168 * to the end of the list as well as to the beginning. The start of the
169 * list is at context->mcmcxlru, and is the least recently unlocked
170 * block still in memory. The end of the list is at context->mcmcxmru,
171 * and is the most recently unlocked block.
172 */
173
174 /*
175 * initialize the cache manager, returning a context for cache manager
176 * operations; a null pointer is returned if insufficient heap memory is
177 * available for initialization. The 'max' argument specifies the
178 * maximum amount of actual low-level heap memory that the cache manager
179 * can ever allocate on behalf of this context (of course, it can
180 * overcommit the heap through swapping). If 'max' is less than the
181 * size of a single heap allocation, it is adjusted upwards to that
182 * minimum.
183 */
184 mcmcx1def *mcmini(ulong max, uint pages, ulong swapsize,
185 osfildef *swapfp, char *swapfilename, errcxdef *errctx);
186
187 /* terminate the cache manager - frees the structure and all cache memory */
188 void mcmterm(mcmcx1def *ctx);
189
190 /* allocate a client context */
191 mcmcxdef *mcmcini(mcmcx1def *globalctx, uint pages,
192 void (*loadfn)(void *, mclhd, uchar *, ushort),
193 void *loadctx,
194 void (*revertfn)(void *, mcmon), void *revertctx);
195
196 /* terminate a client context - frees the structure memory */
197 void mcmcterm(mcmcxdef *ctx);
198
199
200 /*
201 * Lock a cache object, bringing it into memory if necessary. Returns
202 * a pointer to the memory containing the object. A null pointer is
203 * returned in case of error. The object remains fixed in memory at the
204 * returned location until unlocked. Locks are stacked; that is, if
205 * an object is locked twice in succession, it needs to be unlocked
206 * twice in succession before it is actually unlocked.
207 */
208 #ifdef MCM_NO_MACRO
209 uchar *mcmlck(mcmcxdef *ctx, mcmon objnum);
210 #else /* MCM_NO_MACRO */
211
212 /* uchar *mcmlck(mcmcxdef *ctx, mcmon objnum); */
213 #define mcmlck(ctx,num) \
214 ((mcmobje(ctx,num)->mcmoflg & MCMOFPRES ) ? \
215 ((mcmobje(ctx,num)->mcmoflg|=MCMOFLOCK), \
216 ++(mcmobje(ctx,num)->mcmolcnt), mcmobje(ctx,num)->mcmoptr) \
217 : mcmload(ctx,num))
218
219 #endif /* MCM_NO_MACRO */
220
221 /*
222 * Unlock a cache object, allowing it to be moved and swapped.
223 * Unlocking an object moves it to the end (i.e., most recently used)
224 * position on the LRU chain, making it the least favorable to swap out
225 * or discard. This happens at unlock time (rather than lock time)
226 * because presumably the client has been using the object the entire
227 * time it was locked. For this reason, and to keep memory unfragmented
228 * as much as possible, objects should not be kept locked except when
229 * actually in use. Note that locks nest; if an object is locked three
230 * times without an intervening unlock, it must be unlocked three times
231 * in a row. An object can be unlocked even if it's not locked; doing
232 * so has no effect.
233 */
234 #ifdef MCM_NO_MACRO
235 void mcmunlck(mcmcxdef *ctx, mcmon objnum);
236 #else /* MCM_NO_MACRO */
237
238 /* void mcmunlck(mcmcxdef *ctx, mcmon objnum); */
239 #define mcmunlck(ctx,obj) \
240 ((mcmobje(ctx,obj)->mcmoflg & MCMOFLOCK) ? \
241 (--(mcmobje(ctx,obj)->mcmolcnt) ? (void)0 : \
242 ((mcmobje(ctx,obj)->mcmoflg&=(~MCMOFLOCK)), \
243 mcmuse((ctx)->mcmcxgl,mcmc2g(ctx,obj)))) : (void)0)
244
245 #endif /* MCM_NO_MACRO */
246
247 /*
248 * Allocate a new cache object. The new object is locked upon return.
249 * A pointer to the memory for the new object is returned, and
250 * the object number is returned at *nump. A null pointer is returned
251 * if the object cannot be allocated.
252 */
253 /* uchar *mcmalo(mcmcxdef *ctx, ushort siz, mcmon *nump); */
254 #define mcmalo(ctx, siz, nump) mcmalo0(ctx, siz, nump, MCMONINV, FALSE)
255
256 /*
257 * Reserve space for an object, giving it a particular client object
258 * number. This doesn't actually allocate any space for the object, but
259 * just sets it up so that it can be loaded by the client when it's
260 * needed.
261 */
262 void mcmrsrv(mcmcxdef *ctx, ushort siz, mcmon clinum, mclhd loadhd);
263
264 /*
265 * Allocate a new cache object, and associate it with a particular
266 * client object number. An error is signalled if the client object
267 * number is already in use.
268 */
269 /* uchar *mcmalonum(mcmcxdef *ctx, ushort siz, mcmon num); */
270 #define mcmalonum(ctx, siz, num) mcmalo0(ctx, siz, (mcmon *)0, num, FALSE)
271
272 /*
273 * Reallocate an existing object. The object's size is increased
274 * or reduced according to newsize. The object is locked if it is
275 * not already, and the address of the object's memory is returned.
276 * Note that the object can move when reallocated, even if it was
277 * locked before the call.
278 */
279 uchar *mcmrealo(mcmcxdef *ctx, mcmon objnum, ushort newsize);
280
281 /*
282 * Touch a cache object (rendering it dirty). When an object is
283 * written, the client must touch it to ensure that the version in
284 * memory is not discarded. The cache manager attempts to optimize
285 * activity by not writing objects that can be reconstructed from the
286 * load or swap file. Touching the object informs the cache manager
287 * that the object is different from any version it has in a swap or
288 * load file.
289 */
290 /* void mcmtch(mcmcxdef *ctx, mcmon objnum); */
291 #define mcmtch(ctx,obj) \
292 (mcmobje(ctx,obj)->mcmoflg |= MCMOFDIRTY)
293 /* was: (mcmobje(ctx,obj)->mcmoflg |= (MCMOFDIRTY | MCMOFNODISC)) */
294
295 /* get size of a cache manager object - object need not be locked */
296 /* ushort mcmobjsiz(mcmcxdef *ctx, mcmon objn); */
297 #define mcmobjsiz(ctx, objn) (mcmobje(ctx, objn)->mcmosiz)
298
299 /* determine if object has ever been touched */
300 /* int mcmobjdirty(mcmcxdef *ctx, mcmon objn); */
301 #define mcmobjdirty(ctx, objn) \
302 (mcmobje(ctx, objn)->mcmoflg & (MCMOFDIRTY | MCMOFNODISC))
303
304 /* get object's memory pointer - object must be locked for valid result */
305 /* uchar *mcmobjptr(mcmcxdef *ctx, mcmon objn); */
306 #define mcmobjptr(ctx, objn) (mcmobje(ctx, objn)->mcmoptr)
307
308 /*
309 * Free an object. The memory occupied by the object is discarded, and
310 * the object may no longer be referenced.
311 */
312 void mcmfre(mcmcxdef *ctx, mcmon obj);
313
314 /*
315 * "Revert" an object - convert it back to original state. This
316 * routine just invokes a client callback to do the actual reversion
317 * work. The callback is called immediately if the object is already
318 * present in memory, but is deferred until the object is loaded/swapped
319 * in if the object is not in memory.
320 */
321 /* void mcmrevert(mcmcxdef *ctx, mcmon objn); */
322 #define mcmrevert(ctx, objn) \
323 ((mcmobje(ctx, objn)->mcmoflg & MCMOFPRES) ? \
324 ((*(ctx)->mcmcxrvf)((ctx)->mcmcxrvc, objn), DISCARD 0) \
325 : DISCARD (mcmobje(ctx, objn)->mcmoflg |= MCMOFREVRT))
326
327 /* get current size of object cache */
328 ulong mcmcsiz(mcmcxdef *ctx);
329
330 /* change an object's swap handle (used by swapper) */
331 /* void mcmcsw(mcmcx1def *ctx, ushort objn, mcsseg swapn, mcsseg oldswn); */
332 #define mcmcsw(ctx, objn, swapn, oldswapn) \
333 ((*(ctx)->mcmcxcsw)(ctx, objn, swapn, oldswapn))
334
335 /* ------------------------------- PRIVATE ------------------------------- */
336
337 /* Unlock an object by its global handle */
338 #ifdef MCM_NO_MACRO
339 void mcmgunlck(mcmcx1def *ctx, mcmon objnum);
340 #else /* MCM_NO_MACRO */
341
342 /* void mcmgunlck(mcmcx1def *ctx, mcmon objnum); */
343 #define mcmgunlck(ctx,obj) \
344 ((mcmgobje(ctx,obj)->mcmoflg & MCMOFLOCK) ? \
345 (--(mcmgobje(ctx,obj)->mcmolcnt) ? (void)0 : \
346 ((mcmgobje(ctx,obj)->mcmoflg&=(~MCMOFLOCK)), mcmuse(ctx,obj))) : \
347 (void)0)
348
349 #endif /* MCM_NO_MACRO */
350
351 /* real memory allocator; clients use cover macros */
352 uchar *mcmalo0(mcmcxdef *ctx, ushort siz, mcmon *nump, mcmon clinum,
353 int noclitrans);
354
355 /* free an object by global object number */
356 void mcmgfre(mcmcx1def *ctx, mcmon obj);
357
358 /* "use" an object (move to most-recent position in LRU chain) */
359 void mcmuse(mcmcx1def *ctx, mcmon n);
360
361 /*
362 * Load or swap in a cache object which is currently unloaded, locking
363 * it before returning. Returns a pointer to the memory containing
364 * the object, or a null pointer in case of error. The object
365 * remains fixed in memory at the returned location until unlocked.
366 */
367 uchar *mcmload(mcmcxdef *ctx, mcmon objnum);
368
369 /*
370 * Size of each chunk of memory we'll request from the heap manager.
371 * To cut down on wasted memory from the heap manager, we'll always make
372 * our requests in this large size, then sub-allocate out of these
373 * chunks.
374 */
375 #define MCMCHUNK 32768
376
377 /*
378 * number of cache entries in a page - make this a power of 2 to keep
379 * the arithmetic to find a cache object entry simple
380 */
381 #define MCMPAGECNT 256
382
383 /*
384 * size of a page, in bytes
385 */
386 #define MCMPAGESIZE (MCMPAGECNT * sizeof(mcmodef))
387
388 /*
389 * When allocating memory, and we find a free block satisfying the
390 * request, we will split the free block if doing so would result in
391 * enough space in the second block. MCMSPLIT specifies the minimum
392 * size left over that will allow a split to occur.
393 */
394 #define MCMSPLIT 64
395
396 /* get an object cache entyr given a GLOBAL object number */
397 #define mcmgobje(ctx,num) (&((ctx)->mcmcxtab[(num)>>8][(num)&255]))
398
399 /* get an object cache entry given a CLIENT object number */
400 /* mcmodef *mcmobje(mcmcxdef *ctx, mcmon objnum) */
401 #define mcmobje(ctx,num) mcmgobje((ctx)->mcmcxgl,mcmc2g(ctx,num))
402
403 /* allocate a block that will be locked for its entire lifetime */
404 void *mcmptralo(mcmcxdef *ctx, ushort siz);
405
406 /* free a block allocated with mcmptralo */
407 void mcmptrfre(mcmcxdef *ctx, void *block);
408
409 /* change an object's swap handle */
410 void mcmcswf(mcmcx1def *ctx, mcmon objn, mcsseg swapn, mcsseg oldswapn);
411
412 #ifdef __cplusplus
413 }
414 #endif
415
416 #endif /* MCM_INCLUDED */