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