Debian Codegraph @

terps/tads/tads3/vmglob.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/vmglob.h,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmglob.h - T3 VM globals
12 Function
13 The T3 VM requires a number of static variables that are shared
14 by several subsystems. This file defines the globals and a scheme
15 for accessing them.
16
17 Important: note that some VM globals aren't part of the scheme
18 described and defined below. In particular, the try-catch-throw
19 exception handling subsystem doesn't use it; the dynamic compiler
20 doesn't use it; read-only statics don't use it; and some global
21 caches don't use it. The exception subsystem opts out because
22 its globals constitute a critical performance bottleneck, plus
23 they need to be per-thread for multi-threaded builds; so they use
24 native thread-local storage if threads are used, otherwise they're
25 just ordinary globals. The dynamic compiler doesn't use the VM
26 globals scheme because the compiler wasn't originally designed to
27 be used within the VM at all; by the time we adapted it, it was too
28 late and would have required too much work to bring it into the
29 scheme. Read-only statics can be excluded because they can be
30 safely shared among threads and/or instances without any extra
31 work. And caches can safely opt out as long as the information
32 cached is truly global, not private to a VM instance; but note that
33 caches might need to use per-thread storage, or else mutexes, if
34 they can be accessed from multiple threads.
35
36 The globals can be configured in four different ways, depending on
37 how the T3 VM is to be used:
38
39 1. As individual external variables. This is the most efficient
40 way to configure the globals, but this can't be used when
41 multiple independent VM instances are needed in a single process,
42 because each VM would need its own copy of the globals. This
43 is the fastest way to lay out the globals, because it allows
44 the compiler to generate code that accesses internal members of
45 performance-critical global objects directly, without any pointer
46 dereferences.
47
48 2. As members of a static global structure. This is the second
49 most efficient way to configure the globals, but this can't be
50 used when multiple independent VM instances are needed,
51 because each VM would need its own copy of the globals, which
52 isn't possible in this configuration. (Note that accessing
53 members of a global static structure is identical to accessing
54 global variables, since the compiler can compute the relative
55 address of a structure member at compile time and hence does
56 not need to perform any run-time pointer arithmetic or any other
57 computation beyond what it would normally use for global variable
58 access. However, in this configuration, it's not possible to
59 allocate any of the global objects in-line within the master
60 global structure, so each global object's internal members must
61 be accessed through one pointer dereference.)
62
63 3. As members of an allocated structure whose address is contained
64 in a global static variable. This is slightly less efficient
65 than method 1, in that the global pointer must be dereferenced
66 on each global variable access. This method allows multiple VM
67 instances to be used, as long as the host application specifically
68 stores the correct structure pointer in the global static variable
69 before each call to the VM; this can be used when the host system
70 uses the VM in only one thread.
71
72 4. As members of an allocated structure whose address is passed as
73 a parameter to each function that needs global variable access.
74 As with method 2, the host application creates a structure per
75 VM instance it requires, and then passes a pointer to the correct
76 structure on each call to a VM function; the VM internally passes
77 this pointer in its own function calls where needed. This is the
78 most flexible method, in that the VM can be used in multiple
79 thread simultaneously (as long as each thread has a separate VM
80 global structure instance), but is the least efficient, because
81 the structure pointer must be passed around on all calls.
82
83 To select a configuration, define one of the following preprocessor
84 symbols in the makefile:
85
86 VMGLOB_VARS - method 1 - individual global variables
87 VMGLOB_STRUCT - method 2 - global static structure
88 VMGLOB_POINTER - method 3 - global static pointer to allocated structure
89 VMGLOB_PARAM - method 4 - pointer to structure passed as parameter
90
91 We provide all of these different possible configurations because of
92 the trade-offs involved in selecting one. The host application's needs
93 should be determined, and the most efficient configuration that meets
94 those needs should be chosen.
95 Notes
96
97 Modified
98 11/28/98 MJRoberts - Creation
99 */
100
101 #ifndef VMGLOB_H
102 #define VMGLOB_H
103
104 #include <stddef.h>
105
106 #include "t3std.h"
107 #include "vmpoolsl.h"
108
109
110 /* ------------------------------------------------------------------------ */
111 /*
112 * HOST SYSTEM GLOBAL INITIALIZATION
113 *
114 * The host system should declare a local variable as follows:
115 *
116 * vm_globals *vmg__; // two underscores
117 *
118 * The program must call vmglob_alloc() each time it wants to initialize a
119 * global variable context. This routine must always be called at least
120 * once. (In the VMGLOB_STRUCT or VMGLOB_VARS configuration, this routine
121 * has no effect, but should still be called so that the code will work
122 * unchanged in other configurations.) Calling this routine will overwrite
123 * the current static global variable pointer in the VMGLOB_POINTER
124 * configuration, so the caller must take care to keep track of each set of
125 * globals it allocates for later restoration.
126 *
127 * When calling vmglob_alloc(), assign the return value to vmg__.
128 */
129
130 /*
131 * HOST SYSTEM GLOBAL TERMINATION
132 *
133 * The caller should invoke vmglob_delete() for each set of globals it
134 * allocated.
135 */
136
137 /*
138 * HOST SYSTEM VM FUNCTION CALLS
139 *
140 * In each call to a VM function, the host system should include the
141 * macro "vmg_" (one underscore) at the start of each parameter list; do
142 * not put a comma after vmg_. Call functions that take no other
143 * parameters with vmg0_ instead of vmg_.
144 */
145
146 /* ------------------------------------------------------------------------ */
147 /*
148 * INTERNAL FUNCTION DECLARATION AND CALLING
149 *
150 * Every function that requires global access, or which calls a function
151 * that requires global access, must put "VMG_" at the very start of its
152 * formal parameters declaration. Do not put a comma after VMG_, but
153 * simply list the first parameter. If the function has no parameters
154 * at all, use VMG0_ instead of VMG_. For example:
155 *
156 * void CVmClass::func1(VMG_ const textchar_t *str, size_t len) { ... }
157 *. void CVmClass::func2(VMG0_) { ... }
158 *
159 * In each call to a function that uses global variables or calls
160 * functions that use global variables, put "vmg_" at the very start of
161 * the actual parmaeters list in the call; do not put a comma after the
162 * vmg_. If the function takes no parameters at all, use vmg0_ instead
163 * of vmg_.
164 *
165 * func1(vmg_ "test", 4);
166 *. func2(vmg0_);
167 */
168
169
170 /* ------------------------------------------------------------------------ */
171 /*
172 * Functions that themselves can't participate in the prototype
173 * mechanism, because they present an external API to other subsystems,
174 * must keep track of the globals on their own. To do this, they must
175 * stash the global pointer in their own context structure - use
176 * VMGLOB_ADDR to get the address of the global structure. When passing
177 * this stashed pointer back to function calls, use vmg_var(x) (for the
178 * first argument to a function with multiple arguments) or vmg_var0(x)
179 * (for a single-argument call), as appropriate, where x is the local
180 * copy.
181 *
182 * To access globals from this type of routine, put the VMGLOB_PTR(x)
183 * macro in with the local variable decalarations for the function,
184 * passing as the argument the stashed address of the global structure.
185 * This will set things up so that the G_xxx variables are accessible
186 * through a local pointer, when necessary, and will also make the vmg_
187 * and vmg0_ argument macros work properly. See the examples below.
188 */
189 #if 0
190 /* EXAMPLES ONLY - THIS CODE IS FOR DOCUMENTATION ONLY */
191
192 struct my_context_def
193 {
194 /* holder for VM globals structure pointer */
195 vm_globals *vmg;
196 };
197
198 int func_calling_external_interfaces(VMG_ int x, int y, int z)
199 {
200 my_context_def ctx;
201
202 /* stash the address of the VM globals in my private context */
203 ctx.vmg = VMGLOB_ADDR;
204
205 /* call my external API function */
206 external_api_func(&ctx);
207 }
208
209 int func_called_from_external_interface(my_context_def *ctx, int x, int y)
210 {
211 /* set up access to the VM globals through my stashed context */
212 VMGLOB_PTR(ctx->vmg);
213
214 /* access a global variable */
215 G_myvar->do_something();
216
217 /* call a function using a VMG_ prototype */
218 G_myvar->do_something_else(vmg_ x, y);
219 }
220
221 #endif /* 0 */
222
223
224 /* ------------------------------------------------------------------------ */
225 /*
226 * Conditional access to globals.
227 *
228 * Some code that accesses global variables might need to be able to run
229 * during startup or shutdown. On platforms where the globals are
230 * dynamically allocated, such code might need to test to see not only
231 * whether or not particular global variable has been allocated, but also
232 * whether or not the memory containing the global variables themselves
233 * exists. E.g., if you want to access the console object in code that
234 * might be called during early startup or late termination, it's not good
235 * enough to test if G_console constains a non-null object pointer, since
236 * merely accessing G_console itself will dereference the global variable
237 * structure pointer, and this pointer might be null on platforms where the
238 * globals are allocated.
239 *
240 * For such situations, use VMGLOB_IF_AVAIL(varname) to cover the global
241 * variable name. On platforms where the globals are allocated, this will
242 * return null if the globals themselves aren't yet allocated or have
243 * already been freed, otherwise it'll return the variable's value.
244 */
245 #if 0
246
247 /* EXAMPLE ONLY - THIS CODE IS FOR DOCUMENTATION PURPOSES */
248 CVmConsole *con = VMGLOB_IF_AVAIL(G_console);
249
250 #endif
251
252
253 /* ------------------------------------------------------------------------ */
254 /*
255 * Set up to define the global variables. For the POINTER and PARAM
256 * configurations, put pointers to the global structures in a structure.
257 * For the static STRUCT configuration, actually allocate all of the
258 * objects statically.
259 *
260 * Use VM_GLOBAL_OBJDEF() to define the entry for an object (struct or
261 * class) type. The global variable will be a pointer to this type.
262 *
263 * Use VM_GLOBAL_PREOBJDEF to define the entry for an object type that's to
264 * be defined as a pre-allocated static object in the VARS configuration.
265 * This can be used for objects that (1) have very frequent access and thus
266 * should have their fields reachable directly as statics rather than via
267 * static pointers, and (2) need no constructor parameters and thus can be
268 * pre-allocated at compile-time. In the VARS configuration, these
269 * variables can be allocated at compile-time, which allows the compiler to
270 * generate code that accesses the objects' internal members without any
271 * pointer dereferences.
272 *
273 * Any variable defined with VM_GLOBAL_PREOBJDEF MUST have its accessor
274 * defined through VMGLOB_PREACCESS() rather than VMGLOB_ACCESS().
275 *
276 * Use VM_GLOBAL_VARDEF to define a scalar variable.
277 */
278 #if defined(VMGLOB_POINTER) || defined(VMGLOB_PARAM) || defined(VMGLOB_STRUCT)
279 #define VM_GLOBALS_BEGIN struct vm_globals {
280 #define VM_GLOBAL_OBJDEF(typ, var) typ *var;
281 #define VM_GLOBAL_PREOBJDEF(typ, var) typ *var;
282 #define VM_GLOBAL_PRECOBJDEF(typ, var, ctor_args) typ *var;
283 #define VM_GLOBAL_VARDEF(typ, var) typ var;
284 #define VM_GLOBAL_ARRAYDEF(typ, var, eles) typ var[eles];
285 #define VM_GLOBALS_END };
286
287 /*
288 * we do allocate all global objects, including external objects; hence
289 * external globals are non-static
290 */
291 #define VM_IF_ALLOC_PRE_GLOBAL(x) x
292 #define VM_IFELSE_ALLOC_PRE_GLOBAL(x, y) x
293 #define VM_PRE_GLOBALS_ARE_STATIC FALSE
294 #endif
295
296 #if defined(VMGLOB_VARS)
297 #define VM_GLOBALS_BEGIN
298
299 #define VM_GLOBAL_OBJDEF(typ, var) extern typ *G_##var##_X;
300 #define VM_GLOBAL_PREOBJDEF(typ, var) extern typ G_##var##_X;
301 #define VM_GLOBAL_PRECOBJDEF(typ, var, ctor_args) extern typ G_##var##_X;
302 #define VM_GLOBAL_VARDEF(typ, var) extern typ G_##var##_X;
303 #define VM_GLOBAL_ARRAYDEF(typ, var, eles) extern typ G_##var##_X[eles];
304
305 #define VM_GLOBALS_END
306
307 /* we don't actually need a structure for the globals; use a dummy */
308 struct vm_globals { int x; };
309
310 /* external global objects are statically allocated */
311 #define VM_IF_ALLOC_PRE_GLOBAL(x)
312 #define VM_IFELSE_ALLOC_PRE_GLOBAL(x, y) y
313 #define VM_PRE_GLOBALS_ARE_STATIC TRUE
314 #endif
315
316
317 /* define the globals */
318 #include "vmglobv.h"
319
320 /* ------------------------------------------------------------------------ */
321 /*
322 * If we're not including from the global-defining source file, merely
323 * declare the globals.
324 */
325 #ifndef VMGLOB_DECLARE
326 #define VMGLOB_DECLARE extern
327 #endif
328
329
330 /* ------------------------------------------------------------------------ */
331 /*
332 * INDIVIDUAL STATIC GLOBAL VARIABLES configuration. In this
333 * configuration, the globals are defined as individual global variables.
334 * This is the most efficient configuration, but it only allows one VM
335 * instance in a given process.
336 */
337 #ifdef VMGLOB_VARS
338
339 /* initialization - this has no effect in this mode */
340 inline vm_globals *vmglob_alloc() { return 0; }
341
342 /* termination - this has no effect in this mode */
343 inline void vmglob_delete(vm_globals *) { }
344
345 /*
346 * we don't require anything for the parameter declaration or usage, since
347 * we don't use the local parameter mechanism at all
348 */
349 #define VMG_
350 #define VMG0_
351 #define vmg_
352 #define vmg0_
353 #define VMGNULL_
354 #define VMG0NULL_
355 #define Pvmg0_P /* "vmg0_" in parens, for constructor arguments */
356
357 /*
358 * get the address of the globals - this doesn't do anything in this
359 * configuration, as there's not really a global variables structure
360 */
361 #define VMGLOB_ADDR 0
362
363 /* pass a stashed copy of the global pointer, if necessary */
364 #define vmg_var(x)
365 #define vmg_var0(x)
366
367 /* declare a local variable to access the globals */
368 #define VMGLOB_PTR(x)
369
370 /* global variables are statically declared so they're always available */
371 #define VMGLOB_IF_AVAIL(x) x
372
373 /* we access globals directly as individual statics */
374 #define VMGLOB_ACCESS(var) (G_##var##_X)
375 #define VMGLOB_PREACCESS(var) (&G_##var##_X)
376
377 #endif /* VMGLOB_VARS */
378
379 /* ------------------------------------------------------------------------ */
380 /*
381 * STATIC GLOBAL STRUCTURE configuration. In this configuration, the
382 * globals are defined in a single static global structure. This is the
383 * second most efficient configuration, but it only allows one VM instance
384 * per process.
385 */
386 #ifdef VMGLOB_STRUCT
387
388 /* define the global variables structure */
389 VMGLOB_DECLARE vm_globals G_vmglobals;
390
391 /* initialization - this has no effect in this mode */
392 inline vm_globals *vmglob_alloc()
393 {
394 return &G_vmglobals;
395 }
396
397 /* termination - this has no effect in this mode */
398 inline void vmglob_delete(vm_globals *) { }
399
400 /*
401 * we don't require anything for the parameter declaration or usage,
402 * since we don't use the local parameter mechanism at all
403 */
404 #define VMG_
405 #define VMG0_
406 #define vmg_
407 #define vmg0_
408 #define VMGNULL_
409 #define VMG0NULL_
410 #define Pvmg0_P
411
412 /* get the address of the globals */
413 #define VMGLOB_ADDR (&G_vmglobals)
414
415 /* pass a stashed copy of the global pointer, if necessary */
416 #define vmg_var(x)
417 #define vmg_var0(x)
418
419 /* declare a local variable to access the globals */
420 #define VMGLOB_PTR(x)
421
422 /* global variables are statically declared so they're always available */
423 #define VMGLOB_IF_AVAIL(x) x
424
425 /* we access globals directly as individual statics */
426 #define VMGLOB_ACCESS(var) (G_vmglobals.var)
427 #define VMGLOB_PREACCESS(var) (G_vmglobals.var)
428
429 #endif /* VMGLOB_STRUCT */
430
431 /* ------------------------------------------------------------------------ */
432 /*
433 * STATIC GLOBAL POINTER configuration. In this configuration, the
434 * globals are stored in an allocated structure whose address is stored
435 * in a global static pointer variable.
436 */
437 #ifdef VMGLOB_POINTER
438
439 /* define our global static pointer to the global variables */
440 VMGLOB_DECLARE vm_globals *G_vmglobals;
441
442 /* initialization - allocate a new set of globals */
443 inline vm_globals *vmglob_alloc()
444 {
445 G_vmglobals = new vm_globals();
446 return G_vmglobals;
447 }
448
449 /* termination - delete a set of globals */
450 inline void vmglob_delete(vm_globals *glob) { delete glob; }
451
452 /*
453 * we don't require anything for the parameter declaration or usage,
454 * since we don't use the local parameter mechanism at all
455 */
456 #define VMG_
457 #define VMG0_
458 #define vmg_
459 #define vmg0_
460 #define VMGNULL_
461 #define VMG0NULL_
462 #define Pvmg0_P
463
464 /* get the address of the globals */
465 #define VMGLOB_ADDR G_vmglobals
466
467 /* pass a stashed copy of the global pointer, if necessary */
468 #define vmg_var(x)
469 #define vmg_var0(x)
470
471 /* declare a local variable to access the globals */
472 #define VMGLOB_PTR(x)
473
474 /* test to see if a global variable is available */
475 #define VMGLOB_IF_AVAIL(x) (G_vmglobals != 0 ? x : 0)
476
477 /* accessing a global requires dereferencing the global pointer */
478 #define VMGLOB_ACCESS(var) (G_vmglobals->var)
479 #define VMGLOB_PREACCESS(var) (G_vmglobals_var)
480
481 #endif /* VMGLOB_POINTER */
482
483 /* ------------------------------------------------------------------------ */
484 /*
485 * PARAMETER configuration. In this configuration, the globals are
486 * stored in an allocated structure whose address is passed to each VM
487 * function as a parameter.
488 */
489 #ifdef VMGLOB_PARAM
490
491 /* initialization - allocate a new set of globals */
492 inline vm_globals *vmglob_alloc() { return new vm_globals(); }
493
494 /* termination - delete a set of globals */
495 inline void vmglob_delete(vm_globals *glob) { delete glob; }
496
497 /* function declaration for the global pointer parameter */
498 #define VMG_ vm_globals *vmg__,
499 #define VMG0_ vm_globals *vmg__
500
501 /* parameter reference for passing to a function */
502 #define vmg_ vmg__,
503 #define vmg0_ vmg__
504 #define VMGNULL_ ((vm_globals *)NULL),
505 #define VMG0NULL_ ((vm_globals *)NULL)
506 #define Pvmg0_P (vmg__)
507
508 /* get the address of the globals */
509 #define VMGLOB_ADDR vmg__
510
511 /* pass a stashed copy of the global pointer, if necessary */
512 #define vmg_var(x) x,
513 #define vmg_var0(x) x
514
515 /* declare a local variable to access the globals */
516 #define VMGLOB_PTR(x) vm_globals *vmg__ = x
517
518 /* test to see if a global variable is available */
519 #define VMGLOB_IF_AVAIL(x) (vmg__ != 0 ? x : 0)
520
521 /* accessing a global variable requires dereferencing the parameter */
522 #define VMGLOB_ACCESS(var) (vmg__->var)
523 #define VMGLOB_PREACCESS(var) (vmg__->var)
524
525 #endif /* VMGLOB_PARAM */
526
527 /* ------------------------------------------------------------------------ */
528 /*
529 * Global variable accessors. For convenience, we define these cover
530 * macros that access the global variables in the appropriate manner for
531 * our configuration. Code can use these G_xxx symbols syntactically as
532 * though they were normal global variables.
533 */
534 #define G_mem VMGLOB_ACCESS(mem)
535 #define G_undo VMGLOB_ACCESS(undo)
536 #define G_meta_table VMGLOB_ACCESS(meta_table)
537 #define G_bif_table VMGLOB_ACCESS(bif_table)
538 #define G_varheap VMGLOB_ACCESS(varheap)
539 #define G_preinit_mode VMGLOB_ACCESS(preinit_mode)
540 #define G_bif_tads_globals VMGLOB_ACCESS(bif_tads_globals)
541 #define G_host_ifc VMGLOB_ACCESS(host_ifc)
542 #define G_image_loader VMGLOB_ACCESS(image_loader)
543 #define G_disp_cset_name VMGLOB_ACCESS(disp_cset_name)
544 #define G_cmap_from_fname VMGLOB_ACCESS(cmap_from_fname)
545 #define G_cmap_to_fname VMGLOB_ACCESS(cmap_to_fname)
546 #define G_cmap_from_ui VMGLOB_ACCESS(cmap_from_ui)
547 #define G_cmap_to_ui VMGLOB_ACCESS(cmap_to_ui)
548 #define G_cmap_from_file VMGLOB_ACCESS(cmap_from_file)
549 #define G_cmap_to_file VMGLOB_ACCESS(cmap_to_file)
550 #define G_cmap_to_log VMGLOB_ACCESS(cmap_to_log)
551 #define G_console VMGLOB_ACCESS(console)
552 #define G_exc_entry_size VMGLOB_ACCESS(exc_entry_size)
553 #define G_line_entry_size VMGLOB_ACCESS(line_entry_size)
554 #define G_dbg_hdr_size VMGLOB_ACCESS(dbg_hdr_size)
555 #define G_srcf_table VMGLOB_ACCESS(srcf_table)
556 #define G_dbg_lclsym_hdr_size VMGLOB_ACCESS(dbg_lclsym_hdr_size)
557 #define G_dbg_fmt_vsn VMGLOB_ACCESS(dbg_fmt_vsn)
558 #define G_dbg_frame_size VMGLOB_ACCESS(dbg_frame_size)
559 #define G_dyncomp VMGLOB_ACCESS(dyncomp)
560 #define G_net_queue VMGLOB_ACCESS(net_queue)
561 #define G_net_config VMGLOB_ACCESS(net_config)
562 #define G_iter_get_next VMGLOB_ACCESS(iter_get_next)
563 #define G_iter_next_avail VMGLOB_ACCESS(iter_next_avail)
564 #define G_tadsobj_queue VMGLOB_PREACCESS(tadsobj_queue)
565 #define G_predef VMGLOB_PREACCESS(predef)
566 #define G_stk G_interpreter
567 #define G_interpreter VMGLOB_PREACCESS(interpreter)
568 #define G_const_pool VMGLOB_PREACCESS(const_pool)
569 #define G_code_pool VMGLOB_PREACCESS(code_pool)
570 #define G_obj_table VMGLOB_PREACCESS(obj_table)
571 #define G_syslogfile VMGLOB_ACCESS(syslogfile)
572 #define G_file_path VMGLOB_ACCESS(file_path)
573 #define G_sandbox_path VMGLOB_ACCESS(sandbox_path)
574 #define G_tzcache VMGLOB_ACCESS(tzcache)
575 #define G_debugger VMGLOB_ACCESS(debugger)
576
577 #endif /* VMGLOB_H */
578