New tests.
[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  */
8
9 #ifndef __MONO_METADATA_GC_INTERNAL_H__
10 #define __MONO_METADATA_GC_INTERNAL_H__
11
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>
16
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);
19
20 #define MONO_GC_REGISTER_ROOT(x) mono_gc_register_root ((char*)&(x), sizeof(x), NULL)
21
22 #define MONO_GC_UNREGISTER_ROOT(x) mono_gc_deregister_root ((char*)&(x))
23
24 void   mono_object_register_finalizer               (MonoObject  *obj) MONO_INTERNAL;
25 void   ves_icall_System_GC_InternalCollect          (int          generation) MONO_INTERNAL;
26 gint64 ves_icall_System_GC_GetTotalMemory           (MonoBoolean  forceCollection) MONO_INTERNAL;
27 void   ves_icall_System_GC_KeepAlive                (MonoObject  *obj) MONO_INTERNAL;
28 void   ves_icall_System_GC_ReRegisterForFinalize    (MonoObject  *obj) MONO_INTERNAL;
29 void   ves_icall_System_GC_SuppressFinalize         (MonoObject  *obj) MONO_INTERNAL;
30 void   ves_icall_System_GC_WaitForPendingFinalizers (void) MONO_INTERNAL;
31
32 MonoObject *ves_icall_System_GCHandle_GetTarget (guint32 handle) MONO_INTERNAL;
33 guint32     ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type) MONO_INTERNAL;
34 void        ves_icall_System_GCHandle_FreeHandle (guint32 handle) MONO_INTERNAL;
35 gpointer    ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle) MONO_INTERNAL;
36
37 extern void mono_gc_init (void) MONO_INTERNAL;
38 extern void mono_gc_base_init (void) MONO_INTERNAL;
39 extern void mono_gc_cleanup (void) MONO_INTERNAL;
40 extern void mono_gc_enable (void) MONO_INTERNAL;
41 extern void mono_gc_disable (void) MONO_INTERNAL;
42
43 /*
44  * Return whenever the current thread is registered with the GC (i.e. started
45  * by the GC pthread wrappers on unix.
46  */
47 extern gboolean mono_gc_is_gc_thread (void) MONO_INTERNAL;
48
49 /*
50  * Try to register a foreign thread with the GC, if we fail or the backend
51  * can't cope with this concept - we return FALSE.
52  */
53 extern gboolean mono_gc_register_thread (void *baseptr) MONO_INTERNAL;
54
55 extern gboolean mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread) MONO_INTERNAL;
56
57 /* only valid after the RECLAIM_START GC event and before RECLAIM_END
58  * Not exported in public headers, but can be linked to (unsupported).
59  */
60 extern gboolean mono_object_is_alive (MonoObject* obj);
61 extern gboolean mono_gc_is_finalizer_thread (MonoThread *thread);
62 extern gpointer mono_gc_out_of_memory (size_t size);
63 extern void     mono_gc_enable_events (void);
64
65 /* disappearing link functionality */
66 void        mono_gc_weak_link_add    (void **link_addr, MonoObject *obj, gboolean track) MONO_INTERNAL;
67 void        mono_gc_weak_link_remove (void **link_addr) MONO_INTERNAL;
68 MonoObject *mono_gc_weak_link_get    (void **link_addr) MONO_INTERNAL;
69
70 #ifndef HAVE_SGEN_GC
71 void    mono_gc_add_weak_track_handle    (MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
72 void    mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle) MONO_INTERNAL;
73 void    mono_gc_remove_weak_track_handle (guint32 gchandle) MONO_INTERNAL;
74 GSList* mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj) MONO_INTERNAL;
75 #endif
76
77 MonoBoolean
78 GCHandle_CheckCurrentDomain (guint32 gchandle) MONO_INTERNAL;
79
80 /* simple interface for data structures needed in the runtime */
81 void* mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits) MONO_INTERNAL;
82
83 /* User defined marking function */
84 /* It should work like this:
85  * foreach (ref in GC references in the are structure pointed to by ADDR)
86  *    mark_func (ref)
87  */
88 typedef void (*MonoGCMarkFunc)     (void **addr);
89 typedef void (*MonoGCRootMarkFunc) (void *addr, MonoGCMarkFunc mark_func);
90
91 /* Create a descriptor with a user defined marking function */
92 void *mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker);
93
94 /* desc is the result from mono_gc_make_descr*. A NULL value means
95  * all the words might contain GC pointers.
96  * The memory is non-moving and it will be explicitly deallocated.
97  * size bytes will be available from the returned address (ie, descr
98  * must not be stored in the returned memory)
99  */
100 void* mono_gc_alloc_fixed            (size_t size, void *descr) MONO_INTERNAL;
101 void  mono_gc_free_fixed             (void* addr) MONO_INTERNAL;
102
103 /* make sure the gchandle was allocated for an object in domain */
104 gboolean mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain) MONO_INTERNAL;
105 void     mono_gchandle_free_domain  (MonoDomain *domain) MONO_INTERNAL;
106
107 typedef void (*FinalizerThreadCallback) (gpointer user_data);
108
109 /* if there are finalizers to run, run them. Returns the number of finalizers run */
110 gboolean mono_gc_pending_finalizers (void) MONO_INTERNAL;
111 void     mono_gc_finalize_notify    (void) MONO_INTERNAL;
112
113 void* mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
114 void* mono_gc_alloc_obj (MonoVTable *vtable, size_t size) MONO_INTERNAL;
115 void* mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length) MONO_INTERNAL;
116 void* mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size) MONO_INTERNAL;
117 void* mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len) MONO_INTERNAL;
118 void* mono_gc_make_descr_for_string (gsize *bitmap, int numbits) MONO_INTERNAL;
119 void* mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size) MONO_INTERNAL;
120 void* mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size) MONO_INTERNAL;
121
122 void  mono_gc_register_for_finalization (MonoObject *obj, void *user_data) MONO_INTERNAL;
123 void  mono_gc_add_memory_pressure (gint64 value) MONO_INTERNAL;
124 int   mono_gc_register_root (char *start, size_t size, void *descr) MONO_INTERNAL;
125 void  mono_gc_deregister_root (char* addr) MONO_INTERNAL;
126 int   mono_gc_finalizers_for_domain (MonoDomain *domain, MonoObject **out_array, int out_size) MONO_INTERNAL;
127 void  mono_gc_run_finalize (void *obj, void *data) MONO_INTERNAL;
128 void  mono_gc_clear_domain (MonoDomain * domain) MONO_INTERNAL;
129
130 /* 
131  * Register a root which can only be written using a write barrier.
132  * Writes to the root must be done using a write barrier (MONO_ROOT_SETREF).
133  * If the root uses an user defined mark routine, the writes are not required to be
134  * to the area between START and START+SIZE.
135  * The write barrier allows the GC to avoid scanning this root at each collection, so it
136  * is more efficient.
137  * FIXME: Add an API for clearing remset entries if a root with a user defined
138  * mark routine is deleted.
139  */
140 int mono_gc_register_root_wbarrier (char *start, size_t size, void *descr) MONO_INTERNAL;
141
142 void mono_gc_wbarrier_set_root (gpointer ptr, MonoObject *value) MONO_INTERNAL;
143
144 /* Set a field of a root registered using mono_gc_register_root_wbarrier () */
145 #define MONO_ROOT_SETREF(s,fieldname,value) do {        \
146         mono_gc_wbarrier_set_root (&((s)->fieldname), (MonoObject*)value); \
147 } while (0)
148
149 void  mono_gc_finalize_threadpool_threads (void) MONO_INTERNAL;
150
151 /* fast allocation support */
152
153 /* Accessible using mono_marshal_wrapper_info_from_wrapper () */
154 typedef struct {
155         int alloc_type;
156 } AllocatorWrapperInfo;
157
158 MonoMethod* mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box) MONO_INTERNAL;
159 MonoMethod* mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank) MONO_INTERNAL;
160 MonoMethod *mono_gc_get_managed_allocator_by_type (int atype) MONO_INTERNAL;
161
162 guint32 mono_gc_get_managed_allocator_types (void) MONO_INTERNAL;
163
164 /* Fast write barriers */
165 MonoMethod* mono_gc_get_write_barrier (void) MONO_INTERNAL;
166
167 /* helper for the managed alloc support */
168 MonoString *mono_string_alloc (int length) MONO_INTERNAL;
169
170 /* 
171  * Functions supplied by the runtime and called by the GC. Currently only used
172  * by SGEN.
173  */
174 typedef struct {
175         /* 
176          * Function called during thread startup/attach to allocate thread-local data 
177          * needed by the other functions.
178          */
179         gpointer (*thread_attach_func) (void);
180         /* FIXME: Add a cleanup function too */
181         /* 
182          * Function called from every thread when suspending for GC. It can save
183          * data needed for marking from thread stacks. user_data is the data returned 
184          * by attach_func. This might called with GC locks held and the word stopped,
185          * so it shouldn't do any synchronization etc.
186          */
187         void (*thread_suspend_func) (gpointer user_data, void *sigcontext);
188         /* 
189          * Function called to mark from thread stacks. user_data is the data returned 
190          * by attach_func. This is called twice, with the word stopped:
191          * - in the first pass, it should mark areas of the stack using
192          *   conservative marking by calling mono_gc_conservatively_scan_area ().
193          * - in the second pass, it should mark the remaining areas of the stack
194          *   using precise marking by calling mono_gc_scan_object ().
195          */
196         void (*thread_mark_func) (gpointer user_data, guint8 *stack_start, guint8 *stack_end, gboolean precise);
197 } MonoGCCallbacks;
198
199 /* Set the callback functions callable by the GC */
200 void mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks) MONO_INTERNAL;
201
202 /* Functions callable from the thread mark func */
203
204 /* Scan the memory area between START and END conservatively */
205 void mono_gc_conservatively_scan_area (void *start, void *end) MONO_INTERNAL;
206
207 /* Scan OBJ, returning its new address */
208 void *mono_gc_scan_object (void *obj) MONO_INTERNAL;
209
210 /* Return the bitmap encoded by a descriptor */
211 gsize* mono_gc_get_bitmap_for_descr (void *descr, int *numbits) MONO_INTERNAL;
212
213 /* Return the suspend signal number used by the GC to suspend threads,
214    or -1 if not applicable. */
215 int mono_gc_get_suspend_signal (void) MONO_INTERNAL;
216
217 typedef void* (*MonoGCLockedCallbackFunc) (void *data);
218
219 void* mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data) MONO_INTERNAL;
220
221 #ifdef HAVE_SGEN_GC
222 int mono_gc_get_los_limit (void) MONO_INTERNAL;
223 #endif
224
225 #endif /* __MONO_METADATA_GC_INTERNAL_H__ */
226