aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Marshal.h
blob: 4da60f661521596bffa429a70bf85eac1706af89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#ifndef _MARSHAL_H_
#define _MARSHAL_H_

#include "EmBankMapped.h"		// UnmapPhysicalMemory
#include "EmMemory.h"			// EmMemGet32, EmMemGet16, EmMemGet8, EmMem_memcpy
#include "EmPalmStructs.h"		// EmProxy
#include "EmSubroutine.h"		// EmSubroutine
#include "Platform.h"			// Platform::AllocateMemory


/* ===========================================================================

	Functions and macros for helping with marshaling and unmarshaling
	parameters from and to the emulated process's stack.  First, the stack
	is described with a class called EmSubroutine which lists the function's
	parameters in order.  For example, the definition for NetLibDmReceive is
	as follows:

		EmSubroutine	sub ("Int16", "UInt16 libRefNum, NetSocketRef socket,"
			"void *recordP, UInt32 recordOffset, UInt16 rcvLen, UInt16 flags, "
			"void *fromAddrP, UInt16 *fromLenP, Int32 timeout, Err *errP");

	This class describes the format of the parameters on the stack.  You
	then use one of several macros to fetch and possibly return values from
	and to those parameters.

	Use PARAM_VAL to manage pass-by-value parameter, such as a Long or a
	Word. Example:

		PARAM_VAL(Word, libRefNum);

	This defines a local variable called libRefNum that contains the value
	passed on the stack.  The variable is an instance of a class that can
	produce a Word, and so can be passed to functions that take a Word.

	Use PARAM_REF to manage a pass-by-reference parameter, such as a Long*
	or a Word*.  Example:

		PARAM_REF(Word, fromLenP, kInput);

	This defines a local variable called fromLenP that contains the pointer
	passed on the stack as well as a copy of the value that it points to. 
	The variable is an instance of a class that can produce a Word*, and so
	can be passed to functions that take a Word*.  If the parameter
	reference was NULL, then the class produces NULL as well.  The third
	parameter to the macro describes whether or not the value passed in is
	an input parameter (kInput), output parameter (kOutput) or both
	(kInOut). If kInput or kInOut, the referenced value is fetched and
	converted to host format.  Otherwise, it is assumed that the parameter
	references uninitialized contents.  If kOutput or kInOut, the value is
	copied back into the memory specified by the parameter reference. 
	Values are explicitly copied back to emulated memory by calling
	fromLenP's Put() method.

	Use PARAM_PTR to manage an arbitrary block of data pointed to by a
	function parameter, such as a void* or BytePtr.  Example:

		PARAM_PTR(void, recordP, len, kInOut);

	This defines a local variable called recordP that points to a block of
	data "len" bytes long.  If the fourth parameter is kInput or kInOut, the
	memory pointed to by the function parameter is copied into this block of
	data.  If the fourth parameter is kOutput or kInOut, the contents of the
	buffer are copied back to the emulated memory when the Put() method is
	called.  NULL pointers are handled.

	Use PARAM_STR as a special kind of PARAM_PTR where the length is not
	explicitly known, but the referenced data is NULL terminated.  Example:

		PARAM_STR(Char, nameP);

	This defines a local variable called nameP that contains a pointer to a
	string, the contents of which are copied from emulated memory.  These
	variables are actually like PARAM_STRs with kInput as the third
	parameter, but there's no real need for that restriction.

	The above macros expand to variable definitions.  The name of the
	variable is given by the second macro parameter, and the type of the
	variable is determined by the first parameter.  The type of variable is
	actually created from a class template.  There are four class templates:
	ParamVal, ParamRef, ParamPtr, and ParamStr.

	The constructors for these classes fetch the the appropriate values from
	memory, allocating any necessary buffers.  Their destructors do nothing
	(which really saves in terms of exception handling overhead).

	When the fetched values have been used and it's time to return any
	changed values back to the emulated stack/heap, all the classes (except
	ParamVal) have Put methods.  You must call this method when you are done
	with the values.  The Put method writes back any altered values and
	releases any memory allocated in the constructor.

=========================================================================== */

struct HostGremlinInfoType;
struct HostStatType;
struct HostTmType;
struct HostUTimeType;

typedef struct ExgSocketType*	ExgSocketPtr;
typedef UInt8*					UInt8Ptr;


#define CALLED_SETUP(return_decl, parameter_decl)			\
	static EmSubroutine	sub;								\
															\
	static Bool initialized;								\
	if (!initialized)										\
	{														\
		initialized = true;									\
		sub.DescribeDecl (return_decl, parameter_decl);		\
	}														\
															\
	sub.PrepareStack (kForBeingCalled, false)


#define CALLED_SETUP_HC(return_decl, parameter_decl)		\
	static EmSubroutine	sub;								\
															\
	static Bool initialized;								\
	if (!initialized)										\
	{														\
		initialized = true;									\
		sub.DescribeDecl (return_decl, "HostControlSelectorType _selector, " parameter_decl);		\
	}														\
															\
	sub.PrepareStack (kForBeingCalled, false)


#define CALLED_SETUP_STDARG(return_decl, parameter_decl)	\
	EmSubroutine	sub;									\
	sub.DescribeDecl (return_decl, parameter_decl);			\
	sub.PrepareStack (kForBeingCalled, true)


#define CALLED_SETUP_STDARG_HC(return_decl, parameter_decl)	\
	EmSubroutine	sub;									\
	sub.DescribeDecl (return_decl, "HostControlSelectorType selector, " parameter_decl);			\
	sub.PrepareStack (kForBeingCalled, true)


#define CALLER_SETUP(return_decl, parameter_decl)			\
	static EmSubroutine	sub;								\
															\
	static Bool initialized;								\
	if (!initialized)										\
	{														\
		initialized = true;									\
		sub.DescribeDecl (return_decl, parameter_decl);		\
	}														\
															\
	sub.PrepareStack (kForCalling, false)


#define GET_RESULT_VAL(type)								\
	type result;											\
	Marshal::GetReturnVal (sub, result);


#define GET_RESULT_PTR()									\
	emuptr result;											\
	Marshal::GetReturnVal (sub, result);


#define PUT_RESULT_VAL(type, val)							\
	Marshal::PutReturnVal (sub, (type) val);


#define PUT_RESULT_PTR(type, val)							\
	Marshal::PutReturnVal (sub, (type) val);


#define RETURN_RESULT_VAL(type)								\
	GET_RESULT_VAL (type);									\
	return result


#define RETURN_RESULT_PTR(type)								\
	GET_RESULT_PTR ();										\
	return (type) result



// Macros used when being called by emulated code
// (e.g., from trap patches).

#define CALLED_GET_PARAM_VAL(type, name)					\
	ParamVal<type>		name (sub, #name)

#define CALLED_GET_PARAM_REF(type, name, io)				\
	ParamRef<type, io>	name (sub, #name)

#define CALLED_GET_PARAM_PTR(type, name, len, io)			\
	ParamPtr<type, io>	name (sub, #name, len)

#define CALLED_GET_PARAM_STR(type, name)					\
	ParamStr<type>		name (sub, #name)

#define CALLED_PUT_PARAM_REF(name)							\
	name.Put ()


// Macros used when calling emulated code
// (e.g., the stuff in ROMStubs.cpp)

#define CALLER_PUT_PARAM_VAL(type, name)					\
	PushParamVal<type>		_##name (sub, #name, (type) name);		\
	/* *Use* the variable this way.  GCC will complain that the */	\
	/* PushParamVal variable is unused otherwise. */				\
	((void) _##name)

#define CALLER_PUT_PARAM_REF(type, name, io)				\
	PushParamRef<type, io>	_##name (sub, #name, (type*) name)

#define CALLER_PUT_PARAM_PTR(type, name, len, io)			\
	PushParamPtr<type, io>	_##name (sub, #name, name, len)

#define CALLER_PUT_PARAM_STR(type, name)					\
	PushParamStr<type>		_##name (sub, #name, name)

#define CALLER_GET_PARAM_REF(name)							\
	_##name.Get ()


class Marshal
{
	public:
			// VC++ is a bit medieval here...
//		static const int	kInput	= 0x01;
//		static const int	kOutput	= 0x02;
//		static const int	kInOut	= kInput | kOutput;
		enum
		{
			kInput	= 0x01,
			kOutput	= 0x02,
			kInOut	= kInput | kOutput
		};

		#define INPUT(io)	(((io) & Marshal::kInput) != 0)
		#define OUTPUT(io)	(((io) & Marshal::kOutput) != 0)

		static void*			GetBuffer (emuptr p, long len);
#if (__GNUC__ == 2)
		static void				PutBuffer (emuptr p, unsigned char* const buf, long len)
								{
									if (p)
									{
										EmMem_memcpy (p, (void*) buf, len);
										void* b = buf;
										Platform::DisposeMemory (b);
									}
								}
#else
		template <class T>
		static void				PutBuffer (emuptr p, T* const buf, long len)
								{
									if (p)
									{
										EmMem_memcpy (p, (void*) buf, len);
										void* b = buf;
										Platform::DisposeMemory (b);
									}
								}
#endif


	/* ===========================================================================
		The ParamFoo classes are templatized on the type of values they are to
		fetch.  In order to perform the actual fetching, they call GetParamVal
		(and put the value back with PutParamVal).  In order for those calls to
		succeed, we have to have overloaded versions of that function for each
		type we ever fetch.  Those overloaded functions appear below.
	=========================================================================== */

#define DECLARE_POINTER_MARSHALLER(type)									\
		inline static void GetParamVal (EmSubroutine& sub, EmParamNameArg name, type*& v)		\
			{ sub.GetParamVal (name, (emuptr&) v); }						\
																			\
		inline static void PutParamVal (EmSubroutine& sub, EmParamNameArg name, const type* v)	\
			{ sub.SetParamVal (name, (emuptr) v); }							\
																			\
		inline static void PutReturnVal (EmSubroutine& sub, const type* v)	\
			{ sub.SetReturnVal ((emuptr) v); }


		DECLARE_POINTER_MARSHALLER (void)
		DECLARE_POINTER_MARSHALLER (FormType)
		DECLARE_POINTER_MARSHALLER (ExgSocketType)
		DECLARE_POINTER_MARSHALLER (_opaque)


#define DECLARE_SCALAR_MARSHALLER(type, asType, num_bits)				\
		inline static void GetParamVal (EmSubroutine& sub, EmParamNameArg name, type& v)	\
		{																\
			asType temp;												\
			sub.GetParamVal (name, temp);								\
			v = (type) temp;											\
		}																\
																		\
		inline static void PutParamVal (EmSubroutine& sub, EmParamNameArg name, type v)		\
			{ sub.SetParamVal (name, (asType) v); }						\
																		\
		inline static void GetParamRef (emuptr p, type& v)				\
			{ v = (type) EmMemGet##num_bits (p); }						\
																		\
		inline static void PutParamRef (emuptr p, const type& v)		\
			{ EmMemPut##num_bits (p, (asType) v); }						\
																		\
		inline static void GetReturnVal (EmSubroutine& sub, type& v)	\
		{																\
			asType temp;												\
			sub.GetReturnVal (temp);									\
			v = (type) temp;											\
		}																\
																		\
		inline static void PutReturnVal (EmSubroutine& sub, type v)		\
			{ sub.SetReturnVal ((asType&) v); }							\
																		\
		inline static long GetBufSize (const type&)						\
			{ return sizeof(asType); }


		DECLARE_SCALAR_MARSHALLER (int8, int8, 8)
		DECLARE_SCALAR_MARSHALLER (uint8, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (int16, int16, 16)
		DECLARE_SCALAR_MARSHALLER (uint16, uint16, 16)
		DECLARE_SCALAR_MARSHALLER (int32, int32, 32)
		DECLARE_SCALAR_MARSHALLER (uint32, uint32, 32)

		DECLARE_SCALAR_MARSHALLER (ClipboardFormatType, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (DlkSyncStateType, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (FormObjectKind, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (LocalIDKind, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (NetSocketAddrEnum, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (NetSocketTypeEnum, uint8, 8)
		DECLARE_SCALAR_MARSHALLER (SystemPreferencesChoice, uint8, 8)

		DECLARE_SCALAR_MARSHALLER (SysAppInfoPtr, uint32, 32)
		DECLARE_SCALAR_MARSHALLER (UInt8Ptr, uint32, 32)


#define DECLARE_STRUCT_MARSHALLER(type)								\
		static void			Get##type (emuptr p, type&);			\
		static void			Put##type (emuptr p, const type&);		\
																	\
		static long			GetBufSize (const type&)				\
			{ return EmProxy##type::GetSize (); }					\
																	\
		inline static void	GetParamRef (emuptr p, type& v)			\
			{ Get##type (p, v); }									\
																	\
		inline static void	PutParamRef (emuptr p, const type& v)	\
			{ Put##type (p, v); }


		DECLARE_STRUCT_MARSHALLER (DlkServerSessionType)
		DECLARE_STRUCT_MARSHALLER (DmSearchStateType)
		DECLARE_STRUCT_MARSHALLER (EventType)
		DECLARE_STRUCT_MARSHALLER (FieldAttrType)
		DECLARE_STRUCT_MARSHALLER (HostGremlinInfoType)
		DECLARE_STRUCT_MARSHALLER (HostStatType)
		DECLARE_STRUCT_MARSHALLER (HostTmType)
		DECLARE_STRUCT_MARSHALLER (HostUTimeType)
		DECLARE_STRUCT_MARSHALLER (HwrBatCmdReadType)
		DECLARE_STRUCT_MARSHALLER (NetSocketAddrType)
		DECLARE_STRUCT_MARSHALLER (NetIOParamType)
		DECLARE_STRUCT_MARSHALLER (NetHostInfoBufType)
		DECLARE_STRUCT_MARSHALLER (NetServInfoBufType)
		DECLARE_STRUCT_MARSHALLER (PointType)
		DECLARE_STRUCT_MARSHALLER (RectangleType)
		DECLARE_STRUCT_MARSHALLER (SndCommandType)
		DECLARE_STRUCT_MARSHALLER (SysAppInfoType)
		DECLARE_STRUCT_MARSHALLER (SysKernelInfoType)
		DECLARE_STRUCT_MARSHALLER (SysNVParamsType)
};


/* ===========================================================================
	Class that manages an immediate value from the emulated stack. This
	class can fetch the value from the stack and produce that value via a
	type operator.  The value cannot be changed, you can't take the address
	of it, nor can the value be written back to the stack.
=========================================================================== */

template <typename T>
class ParamVal
{
	public:
						ParamVal (EmSubroutine& sub, EmParamNameArg name)
						{
							Marshal::GetParamVal (sub, name, fVal);
						}

						operator T(void) { return fVal; }

	private:
		T				fVal;
};


/* ===========================================================================
	Class that manages a value passed by reference on the emulated stack.
	This class can fetch the pointer to the value and make a copy of the
	value pointed to if the pointer was NULL.  The class can produce a
	pointer to the local copy of the value, or NULL if the parameter was
	NULL.  The class has a Put method which copies the local copy back to
	the emulated memory the original came from.  Whether or not the
	referenced value is fetched or returned is determined by the inOut
	template parameter.  If the kInput bit is set, the value is copied from
	emulated memory to a local copy.  If the kOutput bit is set, the local
	copy is copied back to emulated memory when the Put method is called.
=========================================================================== */

template <typename T, long inOut>
class ParamRef
{
	public:
						ParamRef (EmSubroutine& sub, EmParamNameArg name) :
							fSub (&sub),
							fName (name),
							fPtr (EmMemNULL)
						{
							// Get and cache the pointer to the data.

							fSub->GetParamVal (fName.c_str (), fPtr);

							// If there's a pointer and this is an input
							// variable, get the data.

							if (fPtr && INPUT(inOut))
							{
								Marshal::GetParamRef (fPtr, fVal);
							}
						}

		void			Put (void)
						{
							// If there's a pointer and this is an output
							// variable, store the data.

							if (fPtr && OUTPUT(inOut))
							{
								Marshal::PutParamRef (fPtr, fVal);
							}
						}

						operator T*(void) { return fPtr ? &fVal : NULL; }
		const T&		operator *(void) const { return fVal; }
		T&				operator *(void) { return fVal; }

						operator emuptr (void) { return fPtr; }

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		emuptr			fPtr;
		T				fVal;
};


/* ===========================================================================
	Class that manages a block of memory pointed to by a pointer on the
	emulated stack.  If the pointer is non-NULL, a local block of memory is
	allocated.  If the kInput bit of the inOut template parameter is set,
	the range of emulated memory is copied into the local buffer. This class
	can produce a pointer to the local block of memory on demand (if the
	stack parameter was NULL, this class produces NULL). If the kOutput bit
	of the inOut template paraemter is set, the local block's contents are
	copied back into emulated memory when the Put method is called. 
	Regardless of inOut values, the Put method must be called in order to
	release the lock block of memory.
=========================================================================== */

template <typename T, long inOut>
class ParamPtr
{
	public:
						ParamPtr (EmSubroutine& sub, EmParamNameArg name, long len) :
							fSub (&sub),
							fName (name),
							fPtr (EmMemNULL),
							fLen (len),
							fVal (NULL)
						{
							// Get and cache the pointer to the data.

							fSub->GetParamVal (fName.c_str (), fPtr);

							if (fPtr)
							{
								fVal = (T*) Platform::AllocateMemory (fLen);
								if (fVal && INPUT(inOut))
								{
									EmMem_memcpy ((void*) fVal, fPtr, fLen);
								}
							}
						}

						~ParamPtr ()	// !!! Update comments about d'tors and disposing memory
						{
							Platform::DisposeMemory (fVal);
						}

		void			Put (void)
						{
							if (fPtr && fVal && OUTPUT(inOut))
							{
								EmMem_memcpy (fPtr, (const void*) fVal, fLen);
							}
						}

						operator T*(void) { return fVal; }

						operator emuptr (void) { return fPtr; }

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		emuptr			fPtr;
		long			fLen;
		T*				fVal;
};


/* ===========================================================================
	Class that manages a NULL-terminated range of memory pointed to by a
	pointer on the emulated stack.  The input pointer can be NULL. The
	elements of the string can be anything, but will normally be Char
	(actually, they may *have* to be Char, since I get the length of the
	string by calling EmMem_strlen).  The string is modifiable in-place, but
	any changes are not copied back to emulated memory (this behavior can be
	changed if needed).
=========================================================================== */

template <typename T>
class ParamStr
{
	public:
						ParamStr (EmSubroutine& sub, EmParamNameArg name) :
							fSub (&sub),
							fName (name),
							fPtr (EmMemNULL),
							fVal (NULL)
						{
							fSub->GetParamVal (fName.c_str (), fPtr);

							if (fPtr)
							{
								fVal = (T*) Platform::AllocateMemory (EmMem_strlen (fPtr) + 1);
								if (fVal)
								{
									EmMem_strcpy (fVal, fPtr);
								}
							}
						}

						~ParamStr ()	// !!! Update comments about d'tors and disposing memory
						{
							Platform::DisposeMemory (fVal);
						}

		void			Put (void)
						{
						}

						operator T*(void) { return fVal; }

						operator emuptr (void) { return fPtr; }

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		emuptr			fPtr;
		T*				fVal;
};


template <typename T>
class PushParamVal
{
	public:
						PushParamVal (EmSubroutine& sub, EmParamNameArg name, T val)
						{
							Marshal::PutParamVal (sub, name, val);
						}
};

template <typename T, long inOut>
class PushParamRef
{
	public:
						PushParamRef (EmSubroutine& sub, EmParamNameArg name, T* ptr) :
							fSub (&sub),
							fName (name),
							fHostPtr (ptr),
							fMappedData (NULL),
							fMappedPtr (EmMemNULL)
						{
							if (fHostPtr)
							{
								// Allocate a buffer big enough for the mapped/translated data.

								long	size = Marshal::GetBufSize (*fHostPtr);
								fMappedData = Platform::AllocateMemory (size);

								// Map the buffer

								EmBankMapped::MapPhysicalMemory (fMappedData, size);
								fMappedPtr = EmBankMapped::GetEmulatedAddress (fMappedData);

								// Copy/translate the data into it.

								if (INPUT(inOut))
								{
									Marshal::PutParamRef (fMappedPtr, *fHostPtr);
								}

								// Pass the pointer to the data.

								fSub->SetParamVal (fName.c_str (), fMappedPtr);
							}
							else
							{
								fMappedPtr = EmMemNULL;
							}

							// Pass the pointer to the data.

							fSub->SetParamVal (fName.c_str (), fMappedPtr);
						}

						~PushParamRef (void)
						{
							if (fMappedData)
							{
								EmBankMapped::UnmapPhysicalMemory (fMappedData);
								Platform::FreeMemory (fMappedData);
							}
						}

		void			Get (void)
						{
							if (fHostPtr && OUTPUT(inOut))
							{
								Marshal::GetParamRef (fMappedPtr, *fHostPtr);
							}
						}

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		T*				fHostPtr;
		void*			fMappedData;
		emuptr			fMappedPtr;
};

template <typename T, long inOut>
class PushParamPtr
{
	public:
						PushParamPtr (EmSubroutine& sub, EmParamNameArg name, const T* ptr, long size) :
							fSub (&sub),
							fName (name),
							fHostPtr (ptr),
							fMappedPtr (EmMemNULL)
						{
							if (fHostPtr)
							{
								// Map the buffer

								EmBankMapped::MapPhysicalMemory (fHostPtr, size);
								fMappedPtr = EmBankMapped::GetEmulatedAddress (fHostPtr);
							}
							else
							{
								fMappedPtr = EmMemNULL;
							}

							// Pass the pointer to the data.

							fSub->SetParamVal (fName.c_str (), fMappedPtr);
						}

						~PushParamPtr (void)
						{
							if (fHostPtr)
							{
								EmBankMapped::UnmapPhysicalMemory (fHostPtr);
							}
						}

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		const T*		fHostPtr;
		emuptr			fMappedPtr;
};

template <typename T>
class PushParamStr
{
	public:
						PushParamStr (EmSubroutine& sub, EmParamNameArg name, const T * const ptr) :
							fSub (&sub),
							fName (name),
							fHostPtr (ptr),
							fMappedPtr (EmMemNULL)
						{
							if (fHostPtr)
							{
								// Map the buffer

								long	size = strlen (ptr) + 1;
								EmBankMapped::MapPhysicalMemory (fHostPtr, size);
								fMappedPtr = EmBankMapped::GetEmulatedAddress (fHostPtr);
							}
							else
							{
								fMappedPtr = EmMemNULL;
							}

							// Pass the pointer to the data.

							fSub->SetParamVal (fName.c_str (), fMappedPtr);
						}

						~PushParamStr (void)
						{
							if (fHostPtr)
							{
								EmBankMapped::UnmapPhysicalMemory (fHostPtr);
							}
						}

	private:
		EmSubroutine*	fSub;
		EmParamName		fName;
		const T* const	fHostPtr;
		emuptr			fMappedPtr;
};

#endif	// _MARSHAL_H_