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