Move function from sgen to gc so boehm can use it too.
[mono-project.git] / mono / metadata / gc-internal.h
blob84d1d216d6be8d366a588a4c1756372d643ba15c
1 /*
2 * metadata/gc-internal.h: Internal GC interface
4 * Author: Paolo Molaro <lupus@ximian.com>
6 * (C) 2002 Ximian, Inc.
7 */
9 #ifndef __MONO_METADATA_GC_INTERNAL_H__
10 #define __MONO_METADATA_GC_INTERNAL_H__
12 #include <glib.h>
13 #include <mono/metadata/object-internals.h>
14 #include <mono/metadata/threads-types.h>
15 #include <mono/utils/gc_wrapper.h>
17 #define mono_domain_finalizers_lock(domain) EnterCriticalSection (&(domain)->finalizable_objects_hash_lock);
18 #define mono_domain_finalizers_unlock(domain) LeaveCriticalSection (&(domain)->finalizable_objects_hash_lock);
20 /* Register a memory area as a conservatively scanned GC root */
21 #define MONO_GC_REGISTER_ROOT(x) mono_gc_register_root ((char*)&(x), sizeof(x), NULL)
23 #define MONO_GC_UNREGISTER_ROOT(x) mono_gc_deregister_root ((char*)&(x))
26 * Register a memory location as a root pointing to memory allocated using
27 * mono_gc_alloc_fixed (). This includes MonoGHashTable.
29 #ifdef HAVE_SGEN_GC
30 /* The result of alloc_fixed () is not GC tracked memory */
31 #define MONO_GC_REGISTER_ROOT_FIXED(x)
32 #else
33 #define MONO_GC_REGISTER_ROOT_FIXED(x) MONO_GC_REGISTER_ROOT ((x))
34 #endif
37 * Return a GC descriptor for an array containing N pointers to memory allocated
38 * by mono_gc_alloc_fixed ().
40 #ifdef HAVE_SGEN_GC
41 /* The result of alloc_fixed () is not GC tracked memory */
42 #define MONO_GC_ROOT_DESCR_FOR_FIXED(n) mono_gc_make_root_descr_all_refs (0)
43 #else
44 /* The result of alloc_fixed () is GC tracked memory */
45 #define MONO_GC_ROOT_DESCR_FOR_FIXED(n) NULL
46 #endif
48 /* Register a memory location holding a single object reference as a GC root */
49 #define MONO_GC_REGISTER_ROOT_SINGLE(x) do { \
50 g_assert (sizeof (x) == sizeof (MonoObject*)); \
51 mono_gc_register_root ((char*)&(x), sizeof(MonoObject*), mono_gc_make_root_descr_all_refs (1)); \
52 } while (0)
54 void mono_object_register_finalizer (MonoObject *obj) MONO_INTERNAL;
55 void ves_icall_System_GC_InternalCollect (int generation) MONO_INTERNAL;
56 gint64 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection) MONO_INTERNAL;
57 void ves_icall_System_GC_KeepAlive (MonoObject *obj) MONO_INTERNAL;
58 void ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj) MONO_INTERNAL;
59 void ves_icall_System_GC_SuppressFinalize (MonoObject *obj) MONO_INTERNAL;
60 void ves_icall_System_GC_WaitForPendingFinalizers (void) MONO_INTERNAL;
62 MonoObject *ves_icall_System_GCHandle_GetTarget (guint32 handle) MONO_INTERNAL;
63 guint32 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type) MONO_INTERNAL;
64 void ves_icall_System_GCHandle_FreeHandle (guint32 handle) MONO_INTERNAL;
65 gpointer ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle) MONO_INTERNAL;
66 void ves_icall_System_GC_register_ephemeron_array (MonoObject *array) MONO_INTERNAL;
67 MonoObject *ves_icall_System_GC_get_ephemeron_tombstone (void) MONO_INTERNAL;
69 extern void mono_gc_init (void) MONO_INTERNAL;
70 extern void mono_gc_base_init (void) MONO_INTERNAL;
71 extern void mono_gc_cleanup (void) MONO_INTERNAL;
72 extern void mono_gc_enable (void) MONO_INTERNAL;
73 extern void mono_gc_disable (void) MONO_INTERNAL;
76 * Return whenever the current thread is registered with the GC (i.e. started
77 * by the GC pthread wrappers on unix.
79 extern gboolean mono_gc_is_gc_thread (void) MONO_INTERNAL;
82 * Try to register a foreign thread with the GC, if we fail or the backend
83 * can't cope with this concept - we return FALSE.
85 extern gboolean mono_gc_register_thread (void *baseptr) MONO_INTERNAL;
87 extern gboolean mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread) MONO_INTERNAL;
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).
92 extern gboolean mono_object_is_alive (MonoObject* obj);
93 extern gboolean mono_gc_is_finalizer_thread (MonoThread *thread);
94 extern gpointer mono_gc_out_of_memory (size_t size);
95 extern void mono_gc_enable_events (void);
97 /* disappearing link functionality */
98 void mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track) MONO_INTERNAL;
99 void mono_gc_weak_link_remove (void **link_addr) MONO_INTERNAL;
100 MonoObject *mono_gc_weak_link_get (void **link_addr) MONO_INTERNAL;
102 #ifndef HAVE_SGEN_GC
103 void mono_gc_add_weak_track_handle (MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
104 void mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
105 void mono_gc_remove_weak_track_handle (guint32 gchandle) MONO_INTERNAL;
106 GSList* mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj) MONO_INTERNAL;
107 #endif
109 /*Ephemeron functionality. Sgen only*/
110 gboolean mono_gc_ephemeron_array_add (MonoObject *obj) MONO_INTERNAL;
113 MonoBoolean
114 GCHandle_CheckCurrentDomain (guint32 gchandle) MONO_INTERNAL;
116 /* simple interface for data structures needed in the runtime */
117 void* mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits) MONO_INTERNAL;
119 /* Return a root descriptor for a root with all refs */
120 void* mono_gc_make_root_descr_all_refs (int numbits) MONO_INTERNAL;
122 /* User defined marking function */
123 /* It should work like this:
124 * foreach (ref in GC references in the are structure pointed to by ADDR)
125 * mark_func (ref)
127 typedef void (*MonoGCMarkFunc) (void **addr);
128 typedef void (*MonoGCRootMarkFunc) (void *addr, MonoGCMarkFunc mark_func);
130 /* Create a descriptor with a user defined marking function */
131 void *mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker);
133 /* desc is the result from mono_gc_make_descr*. A NULL value means
134 * all the words might contain GC pointers.
135 * The memory is non-moving and it will be explicitly deallocated.
136 * size bytes will be available from the returned address (ie, descr
137 * must not be stored in the returned memory)
138 * NOTE: Under Boehm, this returns memory allocated using GC_malloc, so the result should
139 * be stored into a location registered using MONO_GC_REGISTER_ROOT_FIXED ().
141 void* mono_gc_alloc_fixed (size_t size, void *descr) MONO_INTERNAL;
142 void mono_gc_free_fixed (void* addr) MONO_INTERNAL;
144 /* make sure the gchandle was allocated for an object in domain */
145 gboolean mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain) MONO_INTERNAL;
146 void mono_gchandle_free_domain (MonoDomain *domain) MONO_INTERNAL;
148 typedef void (*FinalizerThreadCallback) (gpointer user_data);
150 /* if there are finalizers to run, run them. Returns the number of finalizers run */
151 gboolean mono_gc_pending_finalizers (void) MONO_INTERNAL;
152 void mono_gc_finalize_notify (void) MONO_INTERNAL;
154 void* mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
155 void* mono_gc_alloc_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
156 void* mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length) MONO_INTERNAL;
157 void* mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size) MONO_INTERNAL;
158 void* mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len) MONO_INTERNAL;
159 void* mono_gc_make_descr_for_string (gsize *bitmap, int numbits) MONO_INTERNAL;
160 void* mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size) MONO_INTERNAL;
161 void* mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size) MONO_INTERNAL;
163 void mono_gc_register_for_finalization (MonoObject *obj, void *user_data) MONO_INTERNAL;
164 void mono_gc_add_memory_pressure (gint64 value) MONO_INTERNAL;
165 int mono_gc_register_root (char *start, size_t size, void *descr) MONO_INTERNAL;
166 void mono_gc_deregister_root (char* addr) MONO_INTERNAL;
167 int mono_gc_finalizers_for_domain (MonoDomain *domain, MonoObject **out_array, int out_size) MONO_INTERNAL;
168 void mono_gc_run_finalize (void *obj, void *data) MONO_INTERNAL;
169 void mono_gc_clear_domain (MonoDomain * domain) MONO_INTERNAL;
172 * Register a root which can only be written using a write barrier.
173 * Writes to the root must be done using a write barrier (MONO_ROOT_SETREF).
174 * If the root uses an user defined mark routine, the writes are not required to be
175 * to the area between START and START+SIZE.
176 * The write barrier allows the GC to avoid scanning this root at each collection, so it
177 * is more efficient.
178 * FIXME: Add an API for clearing remset entries if a root with a user defined
179 * mark routine is deleted.
181 int mono_gc_register_root_wbarrier (char *start, size_t size, void *descr) MONO_INTERNAL;
183 void mono_gc_wbarrier_set_root (gpointer ptr, MonoObject *value) MONO_INTERNAL;
185 /* Set a field of a root registered using mono_gc_register_root_wbarrier () */
186 #define MONO_ROOT_SETREF(s,fieldname,value) do { \
187 mono_gc_wbarrier_set_root (&((s)->fieldname), (MonoObject*)value); \
188 } while (0)
190 void mono_gc_finalize_threadpool_threads (void) MONO_INTERNAL;
192 /* fast allocation support */
194 /* Accessible using mono_marshal_wrapper_info_from_wrapper () */
195 typedef struct {
196 const char *gc_name;
197 int alloc_type;
198 } AllocatorWrapperInfo;
200 MonoMethod* mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box) MONO_INTERNAL;
201 MonoMethod* mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank) MONO_INTERNAL;
202 MonoMethod *mono_gc_get_managed_allocator_by_type (int atype) MONO_INTERNAL;
204 guint32 mono_gc_get_managed_allocator_types (void) MONO_INTERNAL;
206 /* Return a short string identifying the GC, indented to be saved in AOT images */
207 const char *mono_gc_get_gc_name (void) MONO_INTERNAL;
209 /* Fast write barriers */
210 MonoMethod* mono_gc_get_write_barrier (void) MONO_INTERNAL;
212 /* Fast valuetype copy */
213 void mono_gc_wbarrier_value_copy_bitmap (gpointer dest, gpointer src, int size, unsigned bitmap) MONO_INTERNAL;
215 /* helper for the managed alloc support */
216 MonoString *mono_string_alloc (int length) MONO_INTERNAL;
219 * Functions supplied by the runtime and called by the GC. Currently only used
220 * by SGEN.
222 typedef struct {
224 * Function called during thread startup/attach to allocate thread-local data
225 * needed by the other functions.
227 gpointer (*thread_attach_func) (void);
228 /* FIXME: Add a cleanup function too */
230 * Function called from every thread when suspending for GC. It can save
231 * data needed for marking from thread stacks. user_data is the data returned
232 * by attach_func. This might called with GC locks held and the word stopped,
233 * so it shouldn't do any synchronization etc.
235 void (*thread_suspend_func) (gpointer user_data, void *sigcontext);
237 * Function called to mark from thread stacks. user_data is the data returned
238 * by attach_func. This is called twice, with the word stopped:
239 * - in the first pass, it should mark areas of the stack using
240 * conservative marking by calling mono_gc_conservatively_scan_area ().
241 * - in the second pass, it should mark the remaining areas of the stack
242 * using precise marking by calling mono_gc_scan_object ().
244 void (*thread_mark_func) (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise);
245 } MonoGCCallbacks;
247 /* Set the callback functions callable by the GC */
248 void mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks) MONO_INTERNAL;
249 MonoGCCallbacks *mono_gc_get_gc_callbacks (void) MONO_INTERNAL;
251 /* Functions callable from the thread mark func */
253 /* Scan the memory area between START and END conservatively */
254 void mono_gc_conservatively_scan_area (void *start, void *end) MONO_INTERNAL;
256 /* Scan OBJ, returning its new address */
257 void *mono_gc_scan_object (void *obj) MONO_INTERNAL;
259 /* Return the bitmap encoded by a descriptor */
260 gsize* mono_gc_get_bitmap_for_descr (void *descr, int *numbits) MONO_INTERNAL;
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) MONO_INTERNAL;
267 * Return a human readable description of the GC in malloc-ed memory.
269 char* mono_gc_get_description (void) MONO_INTERNAL;
272 * Configure the GC to desktop mode
274 void mono_gc_set_desktop_mode (void) MONO_INTERNAL;
277 * Return whenever this GC can move objects
279 gboolean mono_gc_is_moving (void) MONO_INTERNAL;
281 typedef void* (*MonoGCLockedCallbackFunc) (void *data);
283 void* mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data) MONO_INTERNAL;
285 #ifdef HAVE_SGEN_GC
286 int mono_gc_get_los_limit (void) MONO_INTERNAL;
288 guint8* mono_gc_get_card_table (int *shift_bits, gpointer *card_mask) MONO_INTERNAL;
290 void* mono_gc_get_nursery (int *shift_bits, size_t *size) MONO_INTERNAL;
291 #endif
294 * Return whenever GC is disabled
296 gboolean mono_gc_is_disabled (void) MONO_INTERNAL;
298 #if defined(__MACH__)
299 void mono_gc_register_mach_exception_thread (pthread_t thread) MONO_INTERNAL;
300 pthread_t mono_gc_get_mach_exception_thread (void) MONO_INTERNAL;
301 #endif
303 gboolean mono_gc_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
305 #endif /* __MONO_METADATA_GC_INTERNAL_H__ */