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