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