2 * handle.h: Handle to object in native code
5 * - Ludovic Henry <ludovic@xamarin.com>
7 * Copyright 2015 Xamarin, Inc. (www.xamarin.com)
10 #ifndef __MONO_HANDLE_H__
11 #define __MONO_HANDLE_H__
18 #include "class-internals.h"
19 #include "threads-types.h"
21 #include "mono/utils/mono-threads-coop.h"
25 typedef struct _MonoHandleStorage MonoHandleStorage
;
26 typedef MonoHandleStorage
* MonoHandle
;
29 * DO NOT ACCESS DIRECTLY
30 * USE mono_handle_obj BELOW TO ACCESS OBJ
32 * The field obj is not private as there is no way to do that
33 * in C, but using a C++ template would simplify that a lot
35 struct _MonoHandleStorage
{
41 #define mono_handle_obj(handle) ((handle)->obj)
43 #define mono_handle_assign(handle,rawptr) do { (handle)->obj = (rawptr); } while(0)
48 mono_handle_check_in_critical_section (const gchar
* file
, const gint lineno
)
50 MonoThreadInfo
*info
= (MonoThreadInfo
*) mono_thread_info_current_unchecked ();
51 if (info
&& !info
->inside_critical_region
)
52 g_error ("Assertion at %s:%d: mono_handle_check_in_critical_section failed", file
, lineno
);
55 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (__FILE__, __LINE__), (handle)->obj)
57 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (__FILE__, __LINE__); (handle)->obj = (rawptr); } while (0)
61 static inline MonoClass
*
62 mono_handle_class (MonoHandle handle
)
64 return handle
->obj
->vtable
->klass
;
67 static inline MonoDomain
*
68 mono_handle_domain (MonoHandle handle
)
70 return handle
->obj
->vtable
->domain
;
73 #define MONO_HANDLE_TYPE_DECL(type) typedef struct { type *obj; } type ## HandleStorage ; \
74 typedef type ## HandleStorage * type ## Handle
75 #define MONO_HANDLE_TYPE(type) type ## Handle
76 #define MONO_HANDLE_NEW(type,obj) ((type ## Handle) mono_handle_new ((MonoObject*) (obj)))
77 #define MONO_HANDLE_ELEVATE(type,handle) ((type ## Handle) mono_handle_elevate ((MonoObject*) (handle)->obj))
79 #define MONO_HANDLE_ASSIGN(handle,rawptr) \
81 mono_handle_assign ((handle), (rawptr)); \
84 #define MONO_HANDLE_SETREF(handle,fieldname,value) \
86 MonoHandle __value = (MonoHandle) (value); \
87 MONO_PREPARE_CRITICAL; \
88 MONO_OBJECT_SETREF (mono_handle_obj ((handle)), fieldname, mono_handle_obj (__value)); \
89 MONO_FINISH_CRITICAL; \
92 #define MONO_HANDLE_SET(handle,fieldname,value) \
94 MONO_PREPARE_CRITICAL; \
95 mono_handle_obj ((handle))->fieldname = (value); \
96 MONO_FINISH_CRITICAL; \
99 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value) \
101 MonoHandle __value = (MonoHandle) (value); \
102 MONO_PREPARE_CRITICAL; \
103 mono_array_setref (mono_handle_obj ((handle)), (index), mono_handle_obj (__value)); \
104 MONO_FINISH_CRITICAL; \
107 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value) \
109 MONO_PREPARE_CRITICAL; \
110 mono_array_set (mono_handle_obj ((handle)), (type), (index), (value)); \
111 MONO_FINISH_CRITICAL; \
114 /* handle arena specific functions */
116 typedef struct _MonoHandleArena MonoHandleArena
;
119 mono_handle_arena_size (gsize nb_handles
);
122 mono_handle_new (MonoObject
*rawptr
);
125 mono_handle_elevate (MonoHandle handle
);
127 /* Some common handle types */
129 MONO_HANDLE_TYPE_DECL (MonoArray
);
130 MONO_HANDLE_TYPE_DECL (MonoString
);
134 #endif /* __MONO_HANDLE_H__ */