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