Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[mono.git] / mono / metadata / gc-internal.h
1 /*
2  * metadata/gc-internal.h: Internal GC interface
3  *
4  * Author: Paolo Molaro <lupus@ximian.com>
5  *
6  * (C) 2002 Ximian, Inc.
7  * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
8  */
9
10 #ifndef __MONO_METADATA_GC_INTERNAL_H__
11 #define __MONO_METADATA_GC_INTERNAL_H__
12
13 #include <glib.h>
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/threads-types.h>
16 #include <mono/utils/gc_wrapper.h>
17
18 #define mono_domain_finalizers_lock(domain) EnterCriticalSection (&(domain)->finalizable_objects_hash_lock);
19 #define mono_domain_finalizers_unlock(domain) LeaveCriticalSection (&(domain)->finalizable_objects_hash_lock);
20
21 /* Register a memory area as a conservatively scanned GC root */
22 #define MONO_GC_REGISTER_ROOT(x) mono_gc_register_root ((char*)&(x), sizeof(x), NULL)
23
24 #define MONO_GC_UNREGISTER_ROOT(x) mono_gc_deregister_root ((char*)&(x))
25
26 /*
27  * Register a memory location as a root pointing to memory allocated using
28  * mono_gc_alloc_fixed (). This includes MonoGHashTable.
29  */
30 #ifdef HAVE_SGEN_GC
31 /* The result of alloc_fixed () is not GC tracked memory */
32 #define MONO_GC_REGISTER_ROOT_FIXED(x)
33 #else
34 #define MONO_GC_REGISTER_ROOT_FIXED(x) MONO_GC_REGISTER_ROOT ((x))
35 #endif
36
37 /*
38  * Return a GC descriptor for an array containing N pointers to memory allocated
39  * by mono_gc_alloc_fixed ().
40  */
41 #ifdef HAVE_SGEN_GC
42 /* The result of alloc_fixed () is not GC tracked memory */
43 #define MONO_GC_ROOT_DESCR_FOR_FIXED(n) mono_gc_make_root_descr_all_refs (0)
44 #else
45 /* The result of alloc_fixed () is GC tracked memory */
46 #define MONO_GC_ROOT_DESCR_FOR_FIXED(n) NULL
47 #endif
48
49 /* Register a memory location holding a single object reference as a GC root */
50 #define MONO_GC_REGISTER_ROOT_SINGLE(x) do { \
51         g_assert (sizeof (x) == sizeof (MonoObject*)); \
52         mono_gc_register_root ((char*)&(x), sizeof(MonoObject*), mono_gc_make_root_descr_all_refs (1)); \
53         } while (0)
54
55 /*
56  * This is used for fields which point to objects which are kept alive by other references
57  * when using Boehm.
58  */
59 #ifdef HAVE_SGEN_GC
60 #define MONO_GC_REGISTER_ROOT_IF_MOVING(x) do { \
61                 MONO_GC_REGISTER_ROOT_SINGLE(x);                \
62 } while (0)
63
64 #define MONO_GC_UNREGISTER_ROOT_IF_MOVING(x) do { \
65         MONO_GC_UNREGISTER_ROOT (x); \
66 } while (0)
67 #else
68 #define MONO_GC_REGISTER_ROOT_IF_MOVING(x)
69 #define MONO_GC_UNREGISTER_ROOT_IF_MOVING(x)
70 #endif
71
72 /* useful until we keep track of gc-references in corlib etc. */
73 #ifdef HAVE_SGEN_GC
74 #define IS_GC_REFERENCE(t) FALSE
75 #else
76 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
77 #endif
78
79 void   mono_object_register_finalizer               (MonoObject  *obj) MONO_INTERNAL;
80 void   ves_icall_System_GC_InternalCollect          (int          generation) MONO_INTERNAL;
81 gint64 ves_icall_System_GC_GetTotalMemory           (MonoBoolean  forceCollection) MONO_INTERNAL;
82 void   ves_icall_System_GC_KeepAlive                (MonoObject  *obj) MONO_INTERNAL;
83 void   ves_icall_System_GC_ReRegisterForFinalize    (MonoObject  *obj) MONO_INTERNAL;
84 void   ves_icall_System_GC_SuppressFinalize         (MonoObject  *obj) MONO_INTERNAL;
85 void   ves_icall_System_GC_WaitForPendingFinalizers (void) MONO_INTERNAL;
86
87 MonoObject *ves_icall_System_GCHandle_GetTarget (guint32 handle) MONO_INTERNAL;
88 guint32     ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type) MONO_INTERNAL;
89 void        ves_icall_System_GCHandle_FreeHandle (guint32 handle) MONO_INTERNAL;
90 gpointer    ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle) MONO_INTERNAL;
91 void        ves_icall_System_GC_register_ephemeron_array (MonoObject *array) MONO_INTERNAL;
92 MonoObject  *ves_icall_System_GC_get_ephemeron_tombstone (void) MONO_INTERNAL;
93
94 extern void mono_gc_init (void) MONO_INTERNAL;
95 extern void mono_gc_base_init (void) MONO_INTERNAL;
96 extern void mono_gc_cleanup (void) MONO_INTERNAL;
97 extern void mono_gc_enable (void) MONO_INTERNAL;
98 extern void mono_gc_disable (void) MONO_INTERNAL;
99
100 /*
101  * Return whenever the current thread is registered with the GC (i.e. started
102  * by the GC pthread wrappers on unix.
103  */
104 extern gboolean mono_gc_is_gc_thread (void) MONO_INTERNAL;
105
106 /*
107  * Try to register a foreign thread with the GC, if we fail or the backend
108  * can't cope with this concept - we return FALSE.
109  */
110 extern gboolean mono_gc_register_thread (void *baseptr) MONO_INTERNAL;
111
112 extern gboolean mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread) MONO_INTERNAL;
113
114 extern void mono_gc_set_stack_end (void *stack_end) MONO_INTERNAL;
115
116 /* only valid after the RECLAIM_START GC event and before RECLAIM_END
117  * Not exported in public headers, but can be linked to (unsupported).
118  */
119 extern gboolean mono_object_is_alive (MonoObject* obj);
120 extern gboolean mono_gc_is_finalizer_thread (MonoThread *thread);
121 extern gpointer mono_gc_out_of_memory (size_t size);
122 extern void     mono_gc_enable_events (void);
123
124 /* disappearing link functionality */
125 void        mono_gc_weak_link_add    (void **link_addr, MonoObject *obj, gboolean track) MONO_INTERNAL;
126 void        mono_gc_weak_link_remove (void **link_addr) MONO_INTERNAL;
127 MonoObject *mono_gc_weak_link_get    (void **link_addr) MONO_INTERNAL;
128
129 #ifndef HAVE_SGEN_GC
130 void    mono_gc_add_weak_track_handle    (MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
131 void    mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
132 void    mono_gc_remove_weak_track_handle (guint32 gchandle) MONO_INTERNAL;
133 GSList* mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj) MONO_INTERNAL;
134 #endif
135
136 /*Ephemeron functionality. Sgen only*/
137 gboolean    mono_gc_ephemeron_array_add (MonoObject *obj) MONO_INTERNAL;
138
139
140 MonoBoolean
141 GCHandle_CheckCurrentDomain (guint32 gchandle) MONO_INTERNAL;
142
143 /* simple interface for data structures needed in the runtime */
144 void* mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits) MONO_INTERNAL;
145
146 /* Return a root descriptor for a root with all refs */
147 void* mono_gc_make_root_descr_all_refs (int numbits) MONO_INTERNAL;
148
149 /* User defined marking function */
150 /* It should work like this:
151  * foreach (ref in GC references in the are structure pointed to by ADDR)
152  *    mark_func (ref)
153  */
154 typedef void (*MonoGCMarkFunc)     (void **addr);
155 typedef void (*MonoGCRootMarkFunc) (void *addr, MonoGCMarkFunc mark_func);
156
157 /* Create a descriptor with a user defined marking function */
158 void *mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker);
159
160 /* Return whenever user defined marking functions are supported */
161 gboolean mono_gc_user_markers_supported (void) MONO_INTERNAL;
162
163 /* desc is the result from mono_gc_make_descr*. A NULL value means
164  * all the words might contain GC pointers.
165  * The memory is non-moving and it will be explicitly deallocated.
166  * size bytes will be available from the returned address (ie, descr
167  * must not be stored in the returned memory)
168  * NOTE: Under Boehm, this returns memory allocated using GC_malloc, so the result should
169  * be stored into a location registered using MONO_GC_REGISTER_ROOT_FIXED ().
170  */
171 void* mono_gc_alloc_fixed            (size_t size, void *descr) MONO_INTERNAL;
172 void  mono_gc_free_fixed             (void* addr) MONO_INTERNAL;
173
174 /* make sure the gchandle was allocated for an object in domain */
175 gboolean mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain) MONO_INTERNAL;
176 void     mono_gchandle_free_domain  (MonoDomain *domain) MONO_INTERNAL;
177
178 typedef void (*FinalizerThreadCallback) (gpointer user_data);
179
180 /* if there are finalizers to run, run them. Returns the number of finalizers run */
181 gboolean mono_gc_pending_finalizers (void) MONO_INTERNAL;
182 void     mono_gc_finalize_notify    (void) MONO_INTERNAL;
183
184 void* mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
185 void* mono_gc_alloc_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
186 void* mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length) MONO_INTERNAL;
187 void* mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size) MONO_INTERNAL;
188 void* mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len) MONO_INTERNAL;
189 void* mono_gc_make_descr_for_string (gsize *bitmap, int numbits) MONO_INTERNAL;
190 void* mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size) MONO_INTERNAL;
191 void* mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size) MONO_INTERNAL;
192
193 void  mono_gc_register_for_finalization (MonoObject *obj, void *user_data) MONO_INTERNAL;
194 void  mono_gc_add_memory_pressure (gint64 value) MONO_INTERNAL;
195 int   mono_gc_register_root (char *start, size_t size, void *descr) MONO_INTERNAL;
196 void  mono_gc_deregister_root (char* addr) MONO_INTERNAL;
197 int   mono_gc_finalizers_for_domain (MonoDomain *domain, MonoObject **out_array, int out_size) MONO_INTERNAL;
198 void  mono_gc_run_finalize (void *obj, void *data) MONO_INTERNAL;
199 void  mono_gc_clear_domain (MonoDomain * domain) MONO_INTERNAL;
200 void* mono_gc_alloc_mature (MonoVTable *vtable) MONO_INTERNAL;
201
202 /* 
203  * Register a root which can only be written using a write barrier.
204  * Writes to the root must be done using a write barrier (MONO_ROOT_SETREF).
205  * If the root uses an user defined mark routine, the writes are not required to be
206  * to the area between START and START+SIZE.
207  * The write barrier allows the GC to avoid scanning this root at each collection, so it
208  * is more efficient.
209  * FIXME: Add an API for clearing remset entries if a root with a user defined
210  * mark routine is deleted.
211  */
212 int mono_gc_register_root_wbarrier (char *start, size_t size, void *descr) MONO_INTERNAL;
213
214 void mono_gc_wbarrier_set_root (gpointer ptr, MonoObject *value) MONO_INTERNAL;
215
216 /* Set a field of a root registered using mono_gc_register_root_wbarrier () */
217 #define MONO_ROOT_SETREF(s,fieldname,value) do {        \
218         mono_gc_wbarrier_set_root (&((s)->fieldname), (MonoObject*)value); \
219 } while (0)
220
221 void  mono_gc_finalize_threadpool_threads (void) MONO_INTERNAL;
222
223 /* fast allocation support */
224
225 /* Accessible using mono_marshal_wrapper_info_from_wrapper () */
226 typedef struct {
227         const char *gc_name;
228         int alloc_type;
229 } AllocatorWrapperInfo;
230
231 MonoMethod* mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box) MONO_INTERNAL;
232 MonoMethod* mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank) MONO_INTERNAL;
233 MonoMethod *mono_gc_get_managed_allocator_by_type (int atype) MONO_INTERNAL;
234
235 guint32 mono_gc_get_managed_allocator_types (void) MONO_INTERNAL;
236
237 /* Return a short string identifying the GC, indented to be saved in AOT images */
238 const char *mono_gc_get_gc_name (void) MONO_INTERNAL;
239
240 /* Fast write barriers */
241 MonoMethod* mono_gc_get_write_barrier (void) MONO_INTERNAL;
242
243 /* Fast valuetype copy */
244 void mono_gc_wbarrier_value_copy_bitmap (gpointer dest, gpointer src, int size, unsigned bitmap) MONO_INTERNAL;
245
246 /* helper for the managed alloc support */
247 MonoString *mono_string_alloc (int length) MONO_INTERNAL;
248
249 /* 
250  * Functions supplied by the runtime and called by the GC. Currently only used
251  * by SGEN.
252  */
253 typedef struct {
254         /* 
255          * Function called during thread startup/attach to allocate thread-local data 
256          * needed by the other functions.
257          */
258         gpointer (*thread_attach_func) (void);
259         /* 
260          * Function called during thread deatch to free the data allocated by
261          * thread_attach_func.
262          */
263         void (*thread_detach_func) (gpointer user_data);
264         /* 
265          * Function called from every thread when suspending for GC. It can save
266          * data needed for marking from thread stacks. user_data is the data returned 
267          * by attach_func. This might called with GC locks held and the word stopped,
268          * so it shouldn't do any synchronization etc.
269          */
270         void (*thread_suspend_func) (gpointer user_data, void *sigcontext);
271         /* 
272          * Function called to mark from thread stacks. user_data is the data returned 
273          * by attach_func. This is called twice, with the word stopped:
274          * - in the first pass, it should mark areas of the stack using
275          *   conservative marking by calling mono_gc_conservatively_scan_area ().
276          * - in the second pass, it should mark the remaining areas of the stack
277          *   using precise marking by calling mono_gc_scan_object ().
278          */
279         void (*thread_mark_func) (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise);
280 } MonoGCCallbacks;
281
282 /* Set the callback functions callable by the GC */
283 void mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks) MONO_INTERNAL;
284 MonoGCCallbacks *mono_gc_get_gc_callbacks (void) MONO_INTERNAL;
285
286 /* Functions callable from the thread mark func */
287
288 /* Scan the memory area between START and END conservatively */
289 void mono_gc_conservatively_scan_area (void *start, void *end) MONO_INTERNAL;
290
291 /* Scan OBJ, returning its new address */
292 void *mono_gc_scan_object (void *obj) MONO_INTERNAL;
293
294 /* Return the bitmap encoded by a descriptor */
295 gsize* mono_gc_get_bitmap_for_descr (void *descr, int *numbits) MONO_INTERNAL;
296
297 /* Return the suspend signal number used by the GC to suspend threads,
298    or -1 if not applicable. */
299 int mono_gc_get_suspend_signal (void) MONO_INTERNAL;
300
301 /*
302  * Return a human readable description of the GC in malloc-ed memory.
303  */
304 char* mono_gc_get_description (void) MONO_INTERNAL;
305
306 /*
307  * Configure the GC to desktop mode
308  */
309 void mono_gc_set_desktop_mode (void) MONO_INTERNAL;
310
311 /*
312  * Return whenever this GC can move objects
313  */
314 gboolean mono_gc_is_moving (void) MONO_INTERNAL;
315
316 typedef void* (*MonoGCLockedCallbackFunc) (void *data);
317
318 void* mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data) MONO_INTERNAL;
319
320 int mono_gc_get_los_limit (void) MONO_INTERNAL;
321
322 guint8* mono_gc_get_card_table (int *shift_bits, gpointer *card_mask) MONO_INTERNAL;
323
324 void* mono_gc_get_nursery (int *shift_bits, size_t *size) MONO_INTERNAL;
325
326 void mono_gc_set_current_thread_appdomain (MonoDomain *domain) MONO_INTERNAL;
327
328 void mono_gc_set_skip_thread (gboolean skip) MONO_INTERNAL;
329
330 /*
331  * Return whenever GC is disabled
332  */
333 gboolean mono_gc_is_disabled (void) MONO_INTERNAL;
334
335 #if defined(__MACH__)
336 void mono_gc_register_mach_exception_thread (pthread_t thread) MONO_INTERNAL;
337 pthread_t mono_gc_get_mach_exception_thread (void) MONO_INTERNAL;
338 #endif
339
340 gboolean mono_gc_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
341
342 gboolean mono_gc_precise_stack_mark_enabled (void) MONO_INTERNAL;
343
344 FILE *mono_gc_get_logfile (void) MONO_INTERNAL;
345
346 typedef void (*mono_reference_queue_callback) (void *user_data);
347
348 typedef struct _MonoReferenceQueue MonoReferenceQueue;
349 typedef struct _RefQueueEntry RefQueueEntry;
350
351 struct _RefQueueEntry {
352 #ifdef HAVE_SGEN_GC
353         void *dis_link;
354 #else
355         guint32 gchandle;
356 #endif
357         void *user_data;
358         RefQueueEntry *next;
359 };
360
361 struct _MonoReferenceQueue {
362         RefQueueEntry *queue;
363         mono_reference_queue_callback callback;
364         MonoReferenceQueue *next;
365         gboolean should_be_deleted;
366 };
367
368 MonoReferenceQueue* mono_gc_reference_queue_new (mono_reference_queue_callback callback) MONO_INTERNAL;
369 void mono_gc_reference_queue_free (MonoReferenceQueue *queue) MONO_INTERNAL;
370 gboolean mono_gc_reference_queue_add (MonoReferenceQueue *queue, MonoObject *obj, void *user_data) MONO_INTERNAL;
371
372 #ifdef HOST_WIN32
373 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved) MONO_INTERNAL;
374 #endif
375
376 void mono_gc_bzero (void *dest, size_t size) MONO_INTERNAL;
377 void mono_gc_memmove (void *dest, const void *src, size_t size) MONO_INTERNAL;
378
379 guint mono_gc_get_vtable_bits (MonoClass *class) MONO_INTERNAL;
380
381 void mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size) MONO_INTERNAL;
382
383 #endif /* __MONO_METADATA_GC_INTERNAL_H__ */
384