3 * Handle to object in native code
6 * - Ludovic Henry <ludovic@xamarin.com>
7 * - Aleksey Klieger <aleksey.klieger@xamarin.com>
8 * - Rodrigo Kumpera <kumpera@xamarin.com>
10 * Copyright 2016 Dot net foundation.
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #ifndef __MONO_HANDLE_DECL_H__
15 #define __MONO_HANDLE_DECL_H__
19 #include <mono/metadata/object-forward.h>
20 #include <mono/utils/mono-compiler.h>
22 // Type-safe handles are a struct with a pointer to pointer.
23 // The only operations allowed on them are the functions/macros in this file, and assignment
24 // from same handle type to same handle type.
26 // Raw handles are void* but still same underlying representation, really void**.
28 // marshal-ilgen.c does not know how to marshal type safe handles.
29 // It passes/accepts raw handles and generated wrappers in C convert.
32 Handle macros/functions
35 #define TYPED_HANDLE_NAME(TYPE) TYPE ## Handle
36 #define TYPED_OUT_HANDLE_NAME(TYPE) TYPE ## HandleOut
37 #define TYPED_IN_OUT_HANDLE_NAME(TYPE) TYPE ## HandleInOut
40 #define MONO_HANDLE_CAST_FOR(type) mono_handle_cast_##type
41 #define MONO_HANDLE_TYPECHECK_FOR(type) mono_handle_typecheck_##type
44 * TYPED_HANDLE_DECL(SomeType):
45 * Expands to a decl for handles to SomeType.
47 * For example, TYPED_HANDLE_DECL(MonoObject) (see below) expands to:
52 * MonoObjectHandleOut,
53 * MonoObjectHandleInOut;
55 * Out is intended for out parameters, InOut is intended for ref parameters.
56 * This is not enforced.
57 * Internal helper functions are also generated.
60 #define MONO_IF_CPLUSPLUS(x) x
62 #define MONO_IF_CPLUSPLUS(x) /* nothing */
65 #define TYPED_HANDLE_DECL(TYPE) \
69 TYPE * GetRaw () const { return __raw ? *__raw : NULL; } \
71 TYPE * volatile *__raw; \
72 } TYPED_HANDLE_NAME (TYPE), \
73 TYPED_OUT_HANDLE_NAME (TYPE), \
74 TYPED_IN_OUT_HANDLE_NAME (TYPE); \
75 /* Do not call these functions directly. Use MONO_HANDLE_NEW and MONO_HANDLE_CAST. */ \
76 /* Another way to do this involved casting mono_handle_new function to a different type. */ \
77 static inline MONO_ALWAYS_INLINE TYPED_HANDLE_NAME (TYPE) \
78 MONO_HANDLE_CAST_FOR (TYPE) (MonoRawHandle a) \
80 TYPED_HANDLE_NAME (TYPE) b = { (TYPE**)a }; \
83 static inline MONO_ALWAYS_INLINE MonoObject* \
84 MONO_HANDLE_TYPECHECK_FOR (TYPE) (TYPE *a) \
86 return (MonoObject*)a; \
88 /* Out/InOut synonyms for icall-def.h HANDLES () */ \
89 static inline MONO_ALWAYS_INLINE TYPED_HANDLE_NAME (TYPE) \
90 MONO_HANDLE_CAST_FOR (TYPE##Out) (gpointer a) \
92 return MONO_HANDLE_CAST_FOR (TYPE) (a); \
94 static inline MONO_ALWAYS_INLINE MonoObject* \
95 MONO_HANDLE_TYPECHECK_FOR (TYPE##Out) (TYPE *a) \
97 return MONO_HANDLE_TYPECHECK_FOR (TYPE) (a); \
99 static inline MONO_ALWAYS_INLINE TYPED_HANDLE_NAME (TYPE) \
100 MONO_HANDLE_CAST_FOR (TYPE##InOut) (gpointer a) \
102 return MONO_HANDLE_CAST_FOR (TYPE) (a); \
104 static inline MONO_ALWAYS_INLINE MonoObject* \
105 MONO_HANDLE_TYPECHECK_FOR (TYPE##InOut) (TYPE *a) \
107 return MONO_HANDLE_TYPECHECK_FOR (TYPE) (a); \
110 #endif /* __MONO_HANDLE_DECL_H__ */