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 ()
50 MONO_REQ_GC_UNSAFE_MODE;
53 #define mono_handle_obj(handle) (mono_handle_check_in_critical_section (), (handle)->obj)
55 #define mono_handle_assign(handle,rawptr) do { mono_handle_check_in_critical_section (); (handle)->obj = (rawptr); } while (0)
59 static inline MonoClass*
60 mono_handle_class (MonoHandle handle)
62 return handle->obj->vtable->klass;
65 static inline MonoDomain*
66 mono_handle_domain (MonoHandle handle)
68 return handle->obj->vtable->domain;
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))
77 #define MONO_HANDLE_ASSIGN(handle,rawptr) \
79 mono_handle_assign ((handle), (rawptr)); \
82 #define MONO_HANDLE_SETREF(handle,fieldname,value) \
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; \
90 #define MONO_HANDLE_SET(handle,fieldname,value) \
92 MONO_PREPARE_GC_CRITICAL_REGION; \
93 mono_handle_obj ((handle))->fieldname = (value); \
94 MONO_FINISH_GC_CRITICAL_REGION; \
97 #define MONO_HANDLE_ARRAY_SETREF(handle,index,value) \
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; \
105 #define MONO_HANDLE_ARRAY_SET(handle,type,index,value) \
107 MONO_PREPARE_GC_CRITICAL_REGION; \
108 mono_array_set (mono_handle_obj ((handle)), (type), (index), (value)); \
109 MONO_FINISH_GC_CRITICAL_REGION; \
112 /* handle arena specific functions */
114 typedef struct _MonoHandleArena MonoHandleArena;
117 mono_handle_arena_size (gsize nb_handles);
120 mono_handle_new (MonoObject *rawptr);
123 mono_handle_elevate (MonoHandle handle);
125 /* Some common handle types */
127 MONO_HANDLE_TYPE_DECL (MonoArray);
128 MONO_HANDLE_TYPE_DECL (MonoString);
132 #endif /* __MONO_HANDLE_H__ */