2010-01-08 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / marshal.h
1
2 /*
3  * marshal.h: Routines for marshaling complex types in P/Invoke methods.
4  * 
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.  http://www.ximian.com
9  *
10  */
11
12 #ifndef __MONO_MARSHAL_H__
13 #define __MONO_MARSHAL_H__
14
15 #include <mono/metadata/class.h>
16 #include <mono/metadata/object-internals.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/opcodes.h>
19 #include <mono/metadata/reflection.h>
20 #include <mono/metadata/method-builder.h>
21
22 #define mono_marshal_find_bitfield_offset(type, elem, byte_offset, bitmask) \
23         do { \
24                 type tmp; \
25                 memset (&tmp, 0, sizeof (tmp)); \
26                 tmp.elem = 1; \
27                 mono_marshal_find_nonzero_bit_offset ((guint8*)&tmp, sizeof (tmp), (byte_offset), (bitmask)); \
28         } while (0)
29
30 /*
31  * This structure holds the state kept by the emit_ marshalling functions.
32  * This is exported so it can be used by cominterop.c.
33  */
34 typedef struct {
35         MonoMethodBuilder *mb;
36         MonoMethodSignature *sig;
37         MonoMethodPInvoke *piinfo;
38         int *orig_conv_args; /* Locals containing the original values of byref args */
39         int retobj_var;
40         MonoClass *retobj_class;
41         MonoMethodSignature *csig; /* Might need to be changed due to MarshalAs directives */
42         MonoImage *image; /* The image to use for looking up custom marshallers */
43 } EmitMarshalContext;
44
45 typedef enum {
46         /*
47          * This is invoked to convert arguments from the current types to
48          * the underlying types expected by the platform routine.  If required,
49          * the methods create a temporary variable with the proper type, and return
50          * the location for it (either the passed argument, or the newly allocated
51          * local slot).
52          */
53         MARSHAL_ACTION_CONV_IN,
54
55         /*
56          * This operation is called to push the actual value that was optionally
57          * converted on the first stage
58          */
59         MARSHAL_ACTION_PUSH,
60
61         /*
62          * Convert byref arguments back or free resources allocated during the
63          * CONV_IN stage
64          */
65         MARSHAL_ACTION_CONV_OUT,
66
67         /*
68          * The result from the unmanaged call is at the top of the stack when
69          * this action is invoked.    The result should be stored in the
70          * third local variable slot. 
71          */
72         MARSHAL_ACTION_CONV_RESULT,
73
74         MARSHAL_ACTION_MANAGED_CONV_IN,
75         MARSHAL_ACTION_MANAGED_CONV_OUT,
76         MARSHAL_ACTION_MANAGED_CONV_RESULT
77 } MarshalAction;
78
79 G_BEGIN_DECLS
80
81 /*type of the function pointer of methods returned by mono_marshal_get_runtime_invoke*/
82 typedef MonoObject *(*RuntimeInvokeFunction) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
83
84 typedef void (*RuntimeInvokeDynamicFunction) (void *args, MonoObject **exc, void* compiled_method);
85
86 /* marshaling helper functions */
87
88 void
89 mono_marshal_init (void) MONO_INTERNAL;
90
91 void
92 mono_marshal_cleanup (void) MONO_INTERNAL;
93
94 gint32
95 mono_class_native_size (MonoClass *klass, guint32 *align) MONO_INTERNAL;
96
97 MonoMarshalType *
98 mono_marshal_load_type_info (MonoClass* klass) MONO_INTERNAL;
99
100 gint32
101 mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, guint32 *align,
102                         gboolean as_field, gboolean unicode) MONO_INTERNAL;
103
104 int            
105 mono_type_native_stack_size (MonoType *type, guint32 *alignment) MONO_INTERNAL;
106
107 gpointer
108 mono_array_to_savearray (MonoArray *array) MONO_INTERNAL;
109
110 gpointer
111 mono_array_to_lparray (MonoArray *array) MONO_INTERNAL;
112
113 void
114 mono_free_lparray (MonoArray *array, gpointer* nativeArray) MONO_INTERNAL;
115
116 void
117 mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text) MONO_INTERNAL;
118
119 void
120 mono_string_utf16_to_builder (MonoStringBuilder *sb, gunichar2 *text) MONO_INTERNAL;
121
122 gpointer
123 mono_string_builder_to_utf8 (MonoStringBuilder *sb) MONO_INTERNAL;
124
125 gpointer
126 mono_string_builder_to_utf16 (MonoStringBuilder *sb) MONO_INTERNAL;
127
128 gpointer
129 mono_string_to_ansibstr (MonoString *string_obj) MONO_INTERNAL;
130
131 gpointer
132 mono_string_to_bstr (MonoString *string_obj) MONO_INTERNAL;
133
134 void
135 mono_string_to_byvalstr (gpointer dst, MonoString *src, int size) MONO_INTERNAL;
136
137 void
138 mono_string_to_byvalwstr (gpointer dst, MonoString *src, int size) MONO_INTERNAL;
139
140 gpointer
141 mono_delegate_to_ftnptr (MonoDelegate *delegate) MONO_INTERNAL;
142
143 MonoDelegate*
144 mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn) MONO_INTERNAL;
145
146 void mono_delegate_free_ftnptr (MonoDelegate *delegate) MONO_INTERNAL;
147
148 void
149 mono_marshal_set_last_error (void) MONO_INTERNAL;
150
151 gpointer
152 mono_marshal_asany (MonoObject *obj, MonoMarshalNative string_encoding, int param_attrs) MONO_INTERNAL;
153
154 void
155 mono_marshal_free_asany (MonoObject *o, gpointer ptr, MonoMarshalNative string_encoding, int param_attrs) MONO_INTERNAL;
156
157 guint
158 mono_type_to_ldind (MonoType *type) MONO_INTERNAL;
159
160 guint
161 mono_type_to_stind (MonoType *type) MONO_INTERNAL;
162
163 /* functions to create various architecture independent helper functions */
164
165 MonoMethod *
166 mono_marshal_method_from_wrapper (MonoMethod *wrapper) MONO_INTERNAL;
167
168 MonoMethod *
169 mono_marshal_get_remoting_invoke (MonoMethod *method) MONO_INTERNAL;
170
171 MonoMethod *
172 mono_marshal_get_xappdomain_invoke (MonoMethod *method) MONO_INTERNAL;
173
174 MonoMethod *
175 mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTarget target_type) MONO_INTERNAL;
176
177 MonoMethod *
178 mono_marshal_get_remoting_invoke_with_check (MonoMethod *method) MONO_INTERNAL;
179
180 MonoMethod *
181 mono_marshal_get_delegate_begin_invoke (MonoMethod *method) MONO_INTERNAL;
182
183 MonoMethod *
184 mono_marshal_get_delegate_end_invoke (MonoMethod *method) MONO_INTERNAL;
185
186 MonoMethod *
187 mono_marshal_get_delegate_invoke (MonoMethod *method, MonoDelegate *del) MONO_INTERNAL;
188
189 MonoMethod *
190 mono_marshal_get_runtime_invoke (MonoMethod *method, gboolean virtual) MONO_INTERNAL;
191
192 MonoMethod*
193 mono_marshal_get_runtime_invoke_dynamic (void) MONO_INTERNAL;
194
195 MonoMethodSignature*
196 mono_marshal_get_string_ctor_signature (MonoMethod *method) MONO_INTERNAL;
197
198 MonoMethod *
199 mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject **this_loc) MONO_INTERNAL;
200
201 gpointer
202 mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type) MONO_INTERNAL;
203
204 MonoMethod *
205 mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gconstpointer func, gboolean check_exceptions) MONO_INTERNAL;
206
207 MonoMethod *
208 mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, gboolean aot) MONO_INTERNAL;
209
210 MonoMethod *
211 mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func) MONO_INTERNAL;
212
213 MonoMethod *
214 mono_marshal_get_struct_to_ptr (MonoClass *klass) MONO_INTERNAL;
215
216 MonoMethod *
217 mono_marshal_get_ptr_to_struct (MonoClass *klass) MONO_INTERNAL;
218
219 MonoMethod *
220 mono_marshal_get_stfld_wrapper (MonoType *type) MONO_INTERNAL;
221
222 MonoMethod *
223 mono_marshal_get_ldfld_wrapper (MonoType *type) MONO_INTERNAL;
224
225 MonoMethod *
226 mono_marshal_get_ldflda_wrapper (MonoType *type) MONO_INTERNAL;
227
228 MonoMethod *
229 mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass) MONO_INTERNAL;
230
231 MonoMethod *
232 mono_marshal_get_stfld_remote_wrapper (MonoClass *klass) MONO_INTERNAL;
233
234 MonoMethod *
235 mono_marshal_get_synchronized_wrapper (MonoMethod *method) MONO_INTERNAL;
236
237 MonoMethod *
238 mono_marshal_get_unbox_wrapper (MonoMethod *method) MONO_INTERNAL;
239
240 MonoMethod *
241 mono_marshal_get_isinst (MonoClass *klass) MONO_INTERNAL;
242
243 MonoMethod *
244 mono_marshal_get_castclass (MonoClass *klass) MONO_INTERNAL;
245
246 MonoMethod *
247 mono_marshal_get_proxy_cancast (MonoClass *klass) MONO_INTERNAL;
248
249 MonoMethod *
250 mono_marshal_get_stelemref (void) MONO_INTERNAL;
251
252 MonoMethod*
253 mono_marshal_get_array_address (int rank, int elem_size) MONO_INTERNAL;
254
255 MonoMethod *
256 mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface,
257                                        gchar *name, MonoMethod *method) MONO_INTERNAL;
258
259 MonoMethod *
260 mono_marshal_get_thunk_invoke_wrapper (MonoMethod *method) MONO_INTERNAL;
261
262 void
263 mono_marshal_free_dynamic_wrappers (MonoMethod *method) MONO_INTERNAL;
264
265 void
266 mono_marshal_free_inflated_wrappers (MonoMethod *method) MONO_INTERNAL;
267
268 /* marshaling internal calls */
269
270 void * 
271 mono_marshal_alloc (gulong size) MONO_INTERNAL;
272
273 void 
274 mono_marshal_free (gpointer ptr) MONO_INTERNAL;
275
276 void
277 mono_marshal_free_array (gpointer *ptr, int size) MONO_INTERNAL;
278
279 gboolean 
280 mono_marshal_free_ccw (MonoObject* obj) MONO_INTERNAL;
281
282 MonoObject *
283 mono_marshal_xdomain_copy_value (MonoObject *val) MONO_INTERNAL;
284
285 void
286 cominterop_release_all_rcws (void) MONO_INTERNAL; 
287
288 void
289 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *src, gint32 start_index,
290                                                                     gpointer dest, gint32 length) MONO_INTERNAL;
291
292 void
293 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src, gint32 start_index,
294                                                                       MonoArray *dest, gint32 length) MONO_INTERNAL;
295
296 gpointer
297 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr, gint32 offset) MONO_INTERNAL;
298
299 unsigned char
300 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr, gint32 offset) MONO_INTERNAL;
301
302 gint16
303 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr, gint32 offset) MONO_INTERNAL;
304
305 gint32
306 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr, gint32 offset) MONO_INTERNAL;
307
308 gint64
309 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr, gint32 offset) MONO_INTERNAL;
310
311 void
312 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr, gint32 offset, unsigned char val) MONO_INTERNAL;
313
314 void
315 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr, gint32 offset, gpointer val) MONO_INTERNAL;
316
317 void
318 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr, gint32 offset, gint16 val) MONO_INTERNAL;
319
320 void
321 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr, gint32 offset, gint32 val) MONO_INTERNAL;
322
323 void
324 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr, gint32 offset, gint64 val) MONO_INTERNAL;
325
326 MonoString *
327 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr) MONO_INTERNAL;
328
329 MonoString *
330 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len (char *ptr, gint32 len) MONO_INTERNAL;
331
332 MonoString *
333 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni (guint16 *ptr) MONO_INTERNAL;
334
335 MonoString *
336 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16 *ptr, gint32 len) MONO_INTERNAL;
337
338 MonoString *
339 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr) MONO_INTERNAL;
340
341 guint32
342 ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m) MONO_INTERNAL;
343
344 guint32 
345 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void) MONO_INTERNAL;
346
347 guint32 
348 ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype) MONO_INTERNAL;
349
350 void
351 ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject *obj, gpointer dst, MonoBoolean delete_old) MONO_INTERNAL;
352
353 void
354 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src, MonoObject *dst) MONO_INTERNAL;
355
356 MonoObject *
357 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src, MonoReflectionType *type) MONO_INTERNAL;
358
359 int
360 ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType *type, MonoString *field_name) MONO_INTERNAL;
361
362 gpointer
363 ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString *string) MONO_INTERNAL;
364
365 gpointer
366 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString *string) MONO_INTERNAL;
367
368 gpointer
369 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString *string) MONO_INTERNAL;
370
371 void
372 ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure (gpointer src, MonoReflectionType *type) MONO_INTERNAL;
373
374 void*
375 ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size) MONO_INTERNAL;
376
377 void
378 ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem (void *ptr) MONO_INTERNAL;
379
380 gpointer 
381 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr, int size) MONO_INTERNAL;
382
383 void*
384 ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (int size) MONO_INTERNAL;
385
386 gpointer 
387 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocHGlobal (gpointer ptr, int size) MONO_INTERNAL;
388
389 void
390 ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal (void *ptr) MONO_INTERNAL;
391
392 void
393 ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (void *ptr) MONO_INTERNAL;
394
395 void*
396 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray *arrayobj, int index) MONO_INTERNAL;
397
398 MonoDelegate*
399 ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal (void *ftn, MonoReflectionType *type) MONO_INTERNAL;
400
401 int
402 ves_icall_System_Runtime_InteropServices_Marshal_AddRefInternal (gpointer pUnk) MONO_INTERNAL;
403
404 int
405 ves_icall_System_Runtime_InteropServices_Marshal_QueryInterfaceInternal (gpointer pUnk, gpointer riid, gpointer* ppv) MONO_INTERNAL;
406
407 int
408 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseInternal (gpointer pUnk) MONO_INTERNAL;
409
410 void*
411 ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal (MonoObject* object) MONO_INTERNAL;
412
413 MonoObject*
414 ves_icall_System_Runtime_InteropServices_Marshal_GetObjectForCCW (void* pUnk) MONO_INTERNAL;
415
416 void*
417 ves_icall_System_Runtime_InteropServices_Marshal_GetIDispatchForObjectInternal (MonoObject* object) MONO_INTERNAL;
418
419 void*
420 ves_icall_System_Runtime_InteropServices_Marshal_GetCCW (MonoObject* object, MonoReflectionType* type) MONO_INTERNAL;
421
422 MonoBoolean
423 ves_icall_System_Runtime_InteropServices_Marshal_IsComObject (MonoObject* object) MONO_INTERNAL;
424
425 gint32
426 ves_icall_System_Runtime_InteropServices_Marshal_ReleaseComObjectInternal (MonoObject* object) MONO_INTERNAL;
427
428 MonoObject *
429 ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type) MONO_INTERNAL;
430
431 void
432 ves_icall_System_ComObject_ReleaseInterfaces(MonoComObject* obj) MONO_INTERNAL;
433
434 gpointer
435 ves_icall_System_ComObject_GetInterfaceInternal (MonoComObject* obj, MonoReflectionType* type, MonoBoolean throw_exception) MONO_INTERNAL;
436
437 void
438 ves_icall_Mono_Interop_ComInteropProxy_AddProxy (gpointer pUnk, MonoComInteropProxy* proxy) MONO_INTERNAL;
439
440 MonoComInteropProxy*
441 ves_icall_Mono_Interop_ComInteropProxy_FindProxy (gpointer pUnk) MONO_INTERNAL;
442
443 void
444 mono_win32_compat_CopyMemory (gpointer dest, gconstpointer source, gsize length);
445
446 void
447 mono_win32_compat_FillMemory (gpointer dest, gsize length, guchar fill);
448
449 void
450 mono_win32_compat_MoveMemory (gpointer dest, gconstpointer source, gsize length);
451
452 void
453 mono_win32_compat_ZeroMemory (gpointer dest, gsize length);
454
455 void
456 mono_marshal_find_nonzero_bit_offset (guint8 *buf, int len, int *byte_offset, guint8 *bitmask) MONO_INTERNAL;
457
458 MonoMethodSignature*
459 mono_signature_no_pinvoke (MonoMethod *method) MONO_INTERNAL;
460
461 /* Called from cominterop.c */
462
463 void
464 mono_marshal_emit_native_wrapper (MonoImage *image, MonoMethodBuilder *mb, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func, gboolean aot, gboolean check_exceptions) MONO_INTERNAL;
465
466 void
467 mono_marshal_emit_managed_wrapper (MonoMethodBuilder *mb, MonoMethodSignature *invoke_sig, MonoMarshalSpec **mspecs, EmitMarshalContext* m, MonoMethod *method, MonoObject** this_loc) MONO_INTERNAL;
468
469 GHashTable*
470 mono_marshal_get_cache (GHashTable **var, GHashFunc hash_func, GCompareFunc equal_func) MONO_INTERNAL;
471
472 MonoMethod*
473 mono_marshal_find_in_cache (GHashTable *cache, gpointer key) MONO_INTERNAL;
474
475 MonoMethod*
476 mono_mb_create_and_cache (GHashTable *cache, gpointer key,
477                                                   MonoMethodBuilder *mb, MonoMethodSignature *sig,
478                                                   int max_stack) MONO_INTERNAL;
479 void
480 mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder *mb) MONO_INTERNAL;
481
482 G_END_DECLS
483
484 #endif /* __MONO_MARSHAL_H__ */
485
486