[handle] Rename MonoHandleStorage->obj to MonoHandleStorage->__private_obj
[mono.git] / mono / metadata / handle.h
1 /*
2  * handle.h: Handle to object in native code
3  *
4  * Authors:
5  *  - Ludovic Henry <ludovic@xamarin.com>
6  *
7  * Copyright 2015 Xamarin, Inc. (www.xamarin.com)
8  */
9
10 #ifndef __MONO_HANDLE_H__
11 #define __MONO_HANDLE_H__
12
13 #include <config.h>
14 #include <glib.h>
15
16 #include <mono/metadata/object.h>
17 #include <mono/metadata/class.h>
18
19 G_BEGIN_DECLS
20
21 /*
22  * DO NOT ACCESS DIRECTLY
23  * USE mono_handle_obj BELOW TO ACCESS OBJ
24  *
25  * The field obj is not private as there is no way to do that
26  * in C, but using a C++ template would simplify that a lot
27  */
28 typedef struct {
29         MonoObject *__private_obj;
30 } MonoHandleStorage;
31
32 typedef MonoHandleStorage* MonoHandle;
33
34 typedef struct _MonoHandleArena MonoHandleArena;
35
36 gsize
37 mono_handle_arena_size (void);
38
39 MonoHandle
40 mono_handle_arena_new (MonoHandleArena *arena, MonoObject *obj);
41
42 MonoHandle
43 mono_handle_arena_elevate (MonoHandleArena *arena, MonoHandle handle);
44
45 void
46 mono_handle_arena_stack_push (MonoHandleArena **arena_stack, MonoHandleArena *arena);
47
48 void
49 mono_handle_arena_stack_pop (MonoHandleArena **arena_stack, MonoHandleArena *arena);
50
51 void
52 mono_handle_arena_initialize (MonoHandleArena **arena_stack);
53
54 void
55 mono_handle_arena_deinitialize (MonoHandleArena **arena_stack);
56
57 MonoHandleArena*
58 mono_handle_arena_current (void);
59
60 MonoHandleArena**
61 mono_handle_arena_current_addr (void);
62
63 #define MONO_HANDLE_ARENA_PUSH()        \
64         do {    \
65                 MonoHandleArena **__arena_stack = mono_handle_arena_current_addr ();    \
66                 MonoHandleArena *__arena = (MonoHandleArena*) g_alloca (mono_handle_arena_size ());     \
67                 mono_handle_arena_stack_push (__arena_stack, __arena)
68
69 #define MONO_HANDLE_ARENA_POP   \
70                 mono_handle_arena_stack_pop (__arena_stack, __arena);   \
71         } while (0)
72
73 #define MONO_HANDLE_ARENA_POP_RETURN_UNSAFE(handle,ret) \
74                 (ret) = (handle)->__private_obj;        \
75                 mono_handle_arena_stack_pop (__arena_stack, __arena);   \
76         } while (0)
77
78 #define MONO_HANDLE_ARENA_POP_RETURN(handle,ret_handle) \
79                 *((MonoHandle**)(&(ret_handle))) = mono_handle_elevate ((MonoHandle*)(handle)); \
80                 mono_handle_arena_stack_pop(__arena_stack, __arena);    \
81         } while (0)
82
83 static inline MonoHandle
84 mono_handle_new (MonoObject *obj)
85 {
86         return mono_handle_arena_new (mono_handle_arena_current (), obj);
87 }
88
89 static inline MonoHandle
90 mono_handle_elevate (MonoHandle handle)
91 {
92         return mono_handle_arena_elevate (mono_handle_arena_current (), handle);
93 }
94
95 #ifndef CHECKED_BUILD
96
97 #define mono_handle_obj(handle) ((handle)->__private_obj)
98
99 #define mono_handle_assign(handle,rawptr) do { (handle)->__private_obj = (rawptr); } while(0)
100
101 #else
102
103 static inline void
104 mono_handle_check_in_critical_section ()
105 {
106         MONO_REQ_GC_UNSAFE_MODE;
107 }
108
109 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (), (handle)->__private_obj)
110
111 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (); (handle)->__private_obj = (rawptr); } while (0)
112
113 #endif
114
115 static inline MonoClass*
116 mono_handle_class (MonoHandle handle)
117 {
118         return mono_object_get_class (handle->__private_obj);
119 }
120
121 static inline MonoDomain*
122 mono_handle_domain (MonoHandle handle)
123 {
124         return mono_object_get_domain (handle->__private_obj);
125 }
126
127 #define mono_handle_obj_is_null(handle) ((handle)->__private_obj == NULL)
128
129 #define MONO_HANDLE_TYPE_DECL(type)      typedef struct { type *__private_obj; } type ## HandleStorage ; \
130         typedef type ## HandleStorage * type ## Handle
131 #define MONO_HANDLE_TYPE(type)           type ## Handle
132 #define MONO_HANDLE_NEW(type,obj)        ((type ## Handle) mono_handle_new ((MonoObject*) (obj)))
133 #define MONO_HANDLE_ELEVATE(type,handle) ((type ## Handle) mono_handle_elevate ((MonoObject*) (handle)->__private_obj))
134
135 #define MONO_HANDLE_ASSIGN(handle,rawptr)       \
136         do {    \
137                 mono_handle_assign ((handle), (rawptr));        \
138         } while (0)
139
140 #define MONO_HANDLE_SETREF(handle,fieldname,value)                      \
141         do {                                                            \
142                 MonoHandle __value = (MonoHandle) (value);              \
143                 MONO_PREPARE_GC_CRITICAL_REGION;                                        \
144                 MONO_OBJECT_SETREF (mono_handle_obj ((handle)), fieldname, mono_handle_obj (__value)); \
145                 MONO_FINISH_GC_CRITICAL_REGION;                                 \
146         } while (0)
147
148 #define MONO_HANDLE_SET(handle,fieldname,value) \
149         do {    \
150                 MONO_PREPARE_GC_CRITICAL_REGION;        \
151                 mono_handle_obj ((handle))->fieldname = (value);        \
152                 MONO_FINISH_GC_CRITICAL_REGION; \
153         } while (0)
154
155 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value)                    \
156         do {                                                            \
157                 MonoHandle __value = (MonoHandle) (value);              \
158                 MONO_PREPARE_GC_CRITICAL_REGION;                                        \
159                 mono_array_setref (mono_handle_obj ((handle)), (index), mono_handle_obj (__value)); \
160                 MONO_FINISH_GC_CRITICAL_REGION;                                 \
161         } while (0)
162
163 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value)  \
164         do {    \
165                 MONO_PREPARE_GC_CRITICAL_REGION;        \
166                 mono_array_set (mono_handle_obj ((handle)), type, (index), (value));    \
167                 MONO_FINISH_GC_CRITICAL_REGION; \
168         } while (0)
169
170
171
172
173 /* Some common handle types */
174
175 MONO_HANDLE_TYPE_DECL (MonoArray);
176 MONO_HANDLE_TYPE_DECL (MonoString);
177
178 G_END_DECLS
179
180 #endif /* __MONO_HANDLE_H__ */