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