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