Merge pull request #2363 from eriklarko/master
[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 "object.h"
17 #include "class.h"
18 #include "class-internals.h"
19 #include "threads-types.h"
20
21 #include "mono/utils/mono-threads-coop.h"
22
23 G_BEGIN_DECLS
24
25 typedef struct _MonoHandleStorage MonoHandleStorage;
26 typedef MonoHandleStorage* MonoHandle;
27
28 /*
29  * DO NOT ACCESS DIRECTLY
30  * USE mono_handle_obj BELOW TO ACCESS OBJ
31  *
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
34  */
35 struct _MonoHandleStorage {
36         MonoObject *obj;
37 };
38
39 #ifndef CHECKED_BUILD
40
41 #define mono_handle_obj(handle) ((handle)->obj)
42
43 #define mono_handle_assign(handle,rawptr) do { (handle)->obj = (rawptr); } while(0)
44
45 #else
46
47 static inline void
48 mono_handle_check_in_critical_section ()
49 {
50         MONO_REQ_GC_UNSAFE_MODE;
51 }
52
53 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (), (handle)->obj)
54
55 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (); (handle)->obj = (rawptr); } while (0)
56
57 #endif
58
59 static inline MonoClass*
60 mono_handle_class (MonoHandle handle)
61 {
62         return handle->obj->vtable->klass;
63 }
64
65 static inline MonoDomain*
66 mono_handle_domain (MonoHandle handle)
67 {
68         return handle->obj->vtable->domain;
69 }
70
71 #define MONO_HANDLE_TYPE_DECL(type)      typedef struct { type *obj; } type ## HandleStorage ; \
72         typedef type ## HandleStorage * type ## Handle
73 #define MONO_HANDLE_TYPE(type)           type ## Handle
74 #define MONO_HANDLE_NEW(type,obj)        ((type ## Handle) mono_handle_new ((MonoObject*) (obj)))
75 #define MONO_HANDLE_ELEVATE(type,handle) ((type ## Handle) mono_handle_elevate ((MonoObject*) (handle)->obj))
76
77 #define MONO_HANDLE_ASSIGN(handle,rawptr)       \
78         do {    \
79                 mono_handle_assign ((handle), (rawptr));        \
80         } while (0)
81
82 #define MONO_HANDLE_SETREF(handle,fieldname,value)                      \
83         do {                                                            \
84                 MonoHandle __value = (MonoHandle) (value);              \
85                 MONO_PREPARE_GC_CRITICAL_REGION;                                        \
86                 MONO_OBJECT_SETREF (mono_handle_obj ((handle)), fieldname, mono_handle_obj (__value)); \
87                 MONO_FINISH_GC_CRITICAL_REGION;                                 \
88         } while (0)
89
90 #define MONO_HANDLE_SET(handle,fieldname,value) \
91         do {    \
92                 MONO_PREPARE_GC_CRITICAL_REGION;        \
93                 mono_handle_obj ((handle))->fieldname = (value);        \
94                 MONO_FINISH_GC_CRITICAL_REGION; \
95         } while (0)
96
97 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value)                    \
98         do {                                                            \
99                 MonoHandle __value = (MonoHandle) (value);              \
100                 MONO_PREPARE_GC_CRITICAL_REGION;                                        \
101                 mono_array_setref (mono_handle_obj ((handle)), (index), mono_handle_obj (__value)); \
102                 MONO_FINISH_GC_CRITICAL_REGION;                                 \
103         } while (0)
104
105 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value)  \
106         do {    \
107                 MONO_PREPARE_GC_CRITICAL_REGION;        \
108                 mono_array_set (mono_handle_obj ((handle)), (type), (index), (value));  \
109                 MONO_FINISH_GC_CRITICAL_REGION; \
110         } while (0)
111
112 /* handle arena specific functions */
113
114 typedef struct _MonoHandleArena MonoHandleArena;
115
116 gsize
117 mono_handle_arena_size (gsize nb_handles);
118
119 MonoHandle
120 mono_handle_new (MonoObject *rawptr);
121
122 MonoHandle
123 mono_handle_elevate (MonoHandle handle);
124
125 /* Some common handle types */
126
127 MONO_HANDLE_TYPE_DECL (MonoArray);
128 MONO_HANDLE_TYPE_DECL (MonoString);
129
130 G_END_DECLS
131
132 #endif /* __MONO_HANDLE_H__ */