[sgen] Remove weak ref hashes.
[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/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_mutex_lock (&(domain)->finalizable_objects_hash_lock);
21 #define mono_domain_finalizers_unlock(domain) mono_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);
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 MonoBoolean ves_icall_Mono_Runtime_SetGCAllowSynchronousMajor (MonoBoolean flag);
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_mutex_cleanup (void);
89 extern void mono_gc_base_cleanup (void);
90
91 /*
92  * Return whenever the current thread is registered with the GC (i.e. started
93  * by the GC pthread wrappers on unix.
94  */
95 extern gboolean mono_gc_is_gc_thread (void);
96
97 extern gboolean mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread);
98
99 extern void mono_gc_set_stack_end (void *stack_end);
100
101 /* only valid after the RECLAIM_START GC event and before RECLAIM_END
102  * Not exported in public headers, but can be linked to (unsupported).
103  */
104 gboolean mono_object_is_alive (MonoObject* obj);
105 gboolean mono_gc_is_finalizer_thread (MonoThread *thread);
106 gpointer mono_gc_out_of_memory (size_t size);
107 void     mono_gc_enable_events (void);
108 void     mono_gc_enable_alloc_events (void);
109
110 /* disappearing link functionality */
111 void        mono_gc_weak_link_add    (void **link_addr, MonoObject *obj, gboolean track);
112 void        mono_gc_weak_link_remove (void **link_addr, gboolean track);
113 MonoObject *mono_gc_weak_link_get    (void **link_addr);
114
115 /*Ephemeron functionality. Sgen only*/
116 gboolean    mono_gc_ephemeron_array_add (MonoObject *obj);
117
118 /* To disable synchronous, evacuating collections - concurrent SGen only */
119 gboolean    mono_gc_set_allow_synchronous_major (gboolean flag);
120
121 MonoBoolean
122 mono_gc_GCHandle_CheckCurrentDomain (guint32 gchandle);
123
124 /* User defined marking function */
125 /* It should work like this:
126  * foreach (ref in GC references in the are structure pointed to by ADDR)
127  *    mark_func (ref)
128  */
129 typedef void (*MonoGCMarkFunc)     (MonoObject **addr, void *gc_data);
130 typedef void (*MonoGCRootMarkFunc) (void *addr, MonoGCMarkFunc mark_func, void *gc_data);
131
132 /* Create a descriptor with a user defined marking function */
133 MonoGCDescriptor mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker);
134
135 /* Return whenever user defined marking functions are supported */
136 gboolean mono_gc_user_markers_supported (void);
137
138 /* desc is the result from mono_gc_make_descr*. A NULL value means
139  * all the words might contain GC pointers.
140  * The memory is non-moving and it will be explicitly deallocated.
141  * size bytes will be available from the returned address (ie, descr
142  * must not be stored in the returned memory)
143  * NOTE: Under Boehm, this returns memory allocated using GC_malloc, so the result should
144  * be stored into a location registered using MONO_GC_REGISTER_ROOT_FIXED ().
145  */
146 void* mono_gc_alloc_fixed            (size_t size, MonoGCDescriptor descr, MonoGCRootSource source, const char *msg);
147 void  mono_gc_free_fixed             (void* addr);
148
149 /* make sure the gchandle was allocated for an object in domain */
150 gboolean mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain);
151 void     mono_gchandle_free_domain  (MonoDomain *domain);
152
153 typedef void (*FinalizerThreadCallback) (gpointer user_data);
154
155 /* if there are finalizers to run, run them. Returns the number of finalizers run */
156 gboolean mono_gc_pending_finalizers (void);
157 void     mono_gc_finalize_notify    (void);
158
159 void* mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size);
160 void* mono_gc_alloc_obj (MonoVTable *vtable, size_t size);
161 void* mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length);
162 void* mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size);
163 void* mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len);
164 MonoGCDescriptor mono_gc_make_descr_for_string (gsize *bitmap, int numbits);
165
166 void  mono_gc_register_for_finalization (MonoObject *obj, void *user_data);
167 void  mono_gc_add_memory_pressure (gint64 value);
168 MONO_API int   mono_gc_register_root (char *start, size_t size, MonoGCDescriptor descr, MonoGCRootSource source, const char *msg);
169 void  mono_gc_deregister_root (char* addr);
170 int   mono_gc_finalizers_for_domain (MonoDomain *domain, MonoObject **out_array, int out_size);
171 void  mono_gc_run_finalize (void *obj, void *data);
172 void  mono_gc_clear_domain (MonoDomain * domain);
173 void* mono_gc_alloc_mature (MonoVTable *vtable);
174
175 /* 
176  * Register a root which can only be written using a write barrier.
177  * Writes to the root must be done using a write barrier (MONO_ROOT_SETREF).
178  * If the root uses an user defined mark routine, the writes are not required to be
179  * to the area between START and START+SIZE.
180  * The write barrier allows the GC to avoid scanning this root at each collection, so it
181  * is more efficient.
182  * FIXME: Add an API for clearing remset entries if a root with a user defined
183  * mark routine is deleted.
184  */
185 int mono_gc_register_root_wbarrier (char *start, size_t size, MonoGCDescriptor descr, MonoGCRootSource source, const char *msg);
186
187 void mono_gc_wbarrier_set_root (gpointer ptr, MonoObject *value);
188
189 /* Set a field of a root registered using mono_gc_register_root_wbarrier () */
190 #define MONO_ROOT_SETREF(s,fieldname,value) do {        \
191         mono_gc_wbarrier_set_root (&((s)->fieldname), (MonoObject*)value); \
192 } while (0)
193
194 void  mono_gc_finalize_threadpool_threads (void);
195
196 /* fast allocation support */
197
198 /* Accessible using mono_marshal_wrapper_info_from_wrapper () */
199 typedef struct {
200         const char *gc_name;
201         int alloc_type;
202 } AllocatorWrapperInfo;
203
204 int mono_gc_get_aligned_size_for_allocator (int size);
205 MonoMethod* mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box, gboolean known_instance_size);
206 MonoMethod* mono_gc_get_managed_array_allocator (MonoClass *klass);
207 MonoMethod *mono_gc_get_managed_allocator_by_type (int atype, gboolean slowpath);
208
209 guint32 mono_gc_get_managed_allocator_types (void);
210
211 /* Return a short string identifying the GC, indented to be saved in AOT images */
212 const char *mono_gc_get_gc_name (void);
213
214 /* Fast write barriers */
215 MonoMethod* mono_gc_get_specific_write_barrier (gboolean is_concurrent);
216 MonoMethod* mono_gc_get_write_barrier (void);
217
218 /* Fast valuetype copy */
219 void mono_gc_wbarrier_value_copy_bitmap (gpointer dest, gpointer src, int size, unsigned bitmap);
220
221 /* helper for the managed alloc support */
222 MonoString *mono_string_alloc (int length);
223
224 /* 
225  * Functions supplied by the runtime and called by the GC. Currently only used
226  * by SGEN.
227  */
228 typedef struct {
229         /* 
230          * Function called during thread startup/attach to allocate thread-local data 
231          * needed by the other functions.
232          */
233         gpointer (*thread_attach_func) (void);
234         /* 
235          * Function called during thread deatch to free the data allocated by
236          * thread_attach_func.
237          */
238         void (*thread_detach_func) (gpointer user_data);
239         /* 
240          * Function called from every thread when suspending for GC. It can save
241          * data needed for marking from thread stacks. user_data is the data returned 
242          * by attach_func. This might called with GC locks held and the word stopped,
243          * so it shouldn't do any synchronization etc.
244          */
245         void (*thread_suspend_func) (gpointer user_data, void *sigcontext, MonoContext *ctx);
246         /* 
247          * Function called to mark from thread stacks. user_data is the data returned 
248          * by attach_func. This is called twice, with the word stopped:
249          * - in the first pass, it should mark areas of the stack using
250          *   conservative marking by calling mono_gc_conservatively_scan_area ().
251          * - in the second pass, it should mark the remaining areas of the stack
252          *   using precise marking by calling mono_gc_scan_object ().
253          */
254         void (*thread_mark_func) (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise, void *gc_data);
255         /*
256          * Function called for debugging to get the current managed method for
257          * tracking the provenances of objects.
258          */
259         gpointer (*get_provenance_func) (void);
260 } MonoGCCallbacks;
261
262 /* Set the callback functions callable by the GC */
263 void mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks);
264 MonoGCCallbacks *mono_gc_get_gc_callbacks (void);
265
266 /* Functions callable from the thread mark func */
267
268 /* Scan the memory area between START and END conservatively */
269 void mono_gc_conservatively_scan_area (void *start, void *end);
270
271 /* Scan OBJ, returning its new address */
272 void *mono_gc_scan_object (void *obj, void *gc_data);
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_suspend_signal (void);
277
278 /* Return the suspend signal number used by the GC to suspend threads,
279    or -1 if not applicable. */
280 int mono_gc_get_restart_signal (void);
281
282 /*
283  * Return a human readable description of the GC in malloc-ed memory.
284  */
285 char* mono_gc_get_description (void);
286
287 /*
288  * Configure the GC to desktop mode
289  */
290 void mono_gc_set_desktop_mode (void);
291
292 /*
293  * Return whenever this GC can move objects
294  */
295 gboolean mono_gc_is_moving (void);
296
297 typedef void* (*MonoGCLockedCallbackFunc) (void *data);
298
299 void* mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data);
300
301 int mono_gc_get_los_limit (void);
302
303 guint8* mono_gc_get_card_table (int *shift_bits, gpointer *card_mask);
304 gboolean mono_gc_card_table_nursery_check (void);
305
306 void* mono_gc_get_nursery (int *shift_bits, size_t *size);
307
308 void mono_gc_set_current_thread_appdomain (MonoDomain *domain);
309
310 void mono_gc_set_skip_thread (gboolean skip);
311
312 #ifndef HOST_WIN32
313 int mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
314 #endif
315
316 /*
317  * Return whenever GC is disabled
318  */
319 gboolean mono_gc_is_disabled (void);
320
321 /*
322  * Return whenever this is the null GC
323  */
324 gboolean mono_gc_is_null (void);
325
326 void mono_gc_set_string_length (MonoString *str, gint32 new_length);
327
328 #if defined(__MACH__)
329 void mono_gc_register_mach_exception_thread (pthread_t thread);
330 pthread_t mono_gc_get_mach_exception_thread (void);
331 #endif
332
333 gboolean mono_gc_precise_stack_mark_enabled (void);
334
335 typedef struct _RefQueueEntry RefQueueEntry;
336
337 struct _RefQueueEntry {
338         void *dis_link;
339         guint32 gchandle;
340         MonoDomain *domain;
341         void *user_data;
342         RefQueueEntry *next;
343 };
344
345 struct _MonoReferenceQueue {
346         RefQueueEntry *queue;
347         mono_reference_queue_callback callback;
348         MonoReferenceQueue *next;
349         gboolean should_be_deleted;
350 };
351
352 enum {
353         MONO_GC_FINALIZER_EXTENSION_VERSION = 1,
354 };
355
356 typedef struct {
357         int version;
358         gboolean (*is_class_finalization_aware) (MonoClass *klass);
359         void (*object_queued_for_finalization) (MonoObject *object);
360 } MonoGCFinalizerCallbacks;
361
362 MONO_API void mono_gc_register_finalizer_callbacks (MonoGCFinalizerCallbacks *callbacks);
363
364
365 #ifdef HOST_WIN32
366 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved);
367 #endif
368
369 guint mono_gc_get_vtable_bits (MonoClass *klass);
370
371 void mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size);
372
373 /* If set, print debugging messages around finalizers. */
374 extern gboolean log_finalizers;
375
376 /* If set, do not run finalizers. */
377 extern gboolean do_not_finalize;
378
379 gboolean mono_gc_object_older_than (MonoObject *object, int generation);
380
381 #endif /* __MONO_METADATA_GC_INTERNAL_H__ */
382