2 * metadata/gc.c: GC icalls.
4 * Author: Paolo Molaro <lupus@ximian.com>
6 * (C) 2002 Ximian, Inc.
13 #include <mono/metadata/gc.h>
14 #include <mono/metadata/threads.h>
15 #include <mono/metadata/tabledefs.h>
16 #define GC_I_HIDE_POINTERS
17 #include <mono/os/gc_wrapper.h>
20 #define HIDE_POINTER(v) (v)
21 #define REVEAL_POINTER(v) (v)
24 static int finalize_slot = -1;
27 * actually, we might want to queue the finalize requests in a separate thread,
28 * but we need to be careful about the execution domain of the thread...
31 run_finalize (void *obj, void *data)
33 MonoObject *exc = NULL;
35 o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
37 if (finalize_slot < 0) {
39 for (i = 0; i < mono_defaults.object_class->vtable_size; ++i) {
40 MonoMethod *cm = mono_defaults.object_class->vtable [i];
42 if (!strcmp (cm->name, "Finalize")) {
48 /* speedup later... and use a timeout */
49 /*g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name);*/
50 mono_domain_set (mono_object_domain (o));
51 mono_runtime_invoke (o->vtable->klass->vtable [finalize_slot], o, NULL, &exc);
54 /* fixme: do something useful */
59 * Some of our objects may point to a different address than the address returned by GC_malloc()
60 * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
61 * This also means that in the callback we need to adjust the pointer to get back the real
63 * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer,
64 * since that, too, can cause the underlying pointer to be offset.
67 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
72 g_assert (GC_base (obj) == (char*)obj - offset);
73 GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
78 mono_object_register_finalizer (MonoObject *obj)
80 /*g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name);*/
81 object_register_finalizer (obj, run_finalize);
85 * to speedup, at class init time, check if a class or struct
86 * have fields that need to be finalized and set a flag.
89 finalize_fields (MonoClass *class, char *data, gboolean instance, GHashTable *todo) {
91 MonoClassField *field;
95 g_print ("Finalize statics on on %s\n", class->name);*/
96 if (instance && class->valuetype)
97 data -= sizeof (MonoObject);
99 for (i = 0; i < class->field.count; ++i) {
100 field = &class->fields [i];
102 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
105 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
108 switch (field->type->type) {
109 case MONO_TYPE_OBJECT:
110 case MONO_TYPE_CLASS:
111 obj = *((MonoObject**)(data + field->offset));
113 if (mono_object_class (obj)->has_finalize) {
114 /* disable the registered finalizer */
115 object_register_finalizer (obj, NULL);
116 run_finalize (obj, NULL);
119 * if the type doesn't have a finalizer, we finalize
120 * the fields ourselves just like we do for structs.
121 * Disabled for now: how do we handle loops?
123 /*finalize_fields (mono_object_class (obj), obj, TRUE, todo);*/
127 case MONO_TYPE_VALUETYPE: {
128 MonoClass *fclass = mono_class_from_mono_type (field->type);
129 if (fclass->enumtype)
131 /*finalize_fields (fclass, data + field->offset, TRUE, todo);*/
134 case MONO_TYPE_ARRAY:
135 case MONO_TYPE_SZARRAY:
136 /* FIXME: foreach item... */
142 class = class->parent;
147 finalize_static_data (MonoClass *class, MonoVTable *vtable, GHashTable *todo) {
149 if (class->enumtype || !vtable->data)
151 finalize_fields (class, vtable->data, FALSE, todo);
155 mono_domain_finalize (MonoDomain *domain) {
157 GHashTable *todo = g_hash_table_new (NULL, NULL);
161 mono_g_hash_table_foreach (domain->class_vtable_hash, (GHFunc)finalize_static_data, todo);
162 /* FIXME: finalize objects in todo... */
163 g_hash_table_destroy (todo);
167 ves_icall_System_GC_InternalCollect (int generation)
175 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
180 return GC_get_heap_size ();
187 ves_icall_System_GC_KeepAlive (MonoObject *obj)
195 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
197 object_register_finalizer (obj, run_finalize);
201 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
203 object_register_finalizer (obj, NULL);
207 ves_icall_System_GC_WaitForPendingFinalizers (void)
211 /*static CRITICAL_SECTION handle_section;*/
212 static guint32 next_handle = 0;
213 static gpointer *gc_handles = NULL;
214 static guint8 *gc_handle_types = NULL;
215 static guint32 array_size = 0;
218 * The handle type is encoded in the lower two bits of the handle value:
232 * FIXME: make thread safe and reuse the array entries.
235 ves_icall_System_GCHandle_GetTarget (guint32 handle)
242 g_assert (type == gc_handle_types [handle >> 2]);
243 obj = gc_handles [handle >> 2];
247 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK))
248 return REVEAL_POINTER (obj);
256 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
259 guint32 h, idx = next_handle++;
261 if (idx >= array_size) {
264 guint8 *new_type_array;
267 new_array = GC_MALLOC (sizeof (gpointer) * (array_size * 2));
268 new_type_array = GC_MALLOC (sizeof (guint8) * (array_size * 2));
271 memcpy (new_array, gc_handles, sizeof (gpointer) * array_size);
272 memcpy (new_type_array, gc_handle_types, sizeof (guint8) * array_size);
273 /* need to re-register links for weak refs. test if GC_realloc needs the same */
274 for (i = 0; i < array_size; ++i) {
275 #if 0 /* This breaks the threaded finalizer, by causing segfaults deep
276 * inside libgc. I assume it will also break without the
277 * threaded finalizer, just that the stress test (bug 31333)
278 * deadlocks too early without it. Reverting to the previous
279 * version here stops the segfault.
281 if ((gc_handle_types[i] == HANDLE_WEAK) || (gc_handle_types[i] == HANDLE_WEAK_TRACK)) { /* all and only disguised pointers have it set */
283 if (((gulong)new_array [i]) & 0x1) {
285 if (gc_handles [i] != (gpointer)-1)
286 GC_unregister_disappearing_link (&(gc_handles [i]));
287 if (new_array [i] != (gpointer)-1)
288 GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(new_array [i]), REVEAL_POINTER (new_array [i]));
293 gc_handles = new_array;
294 gc_handle_types = new_type_array;
296 g_error ("No GCHandle support built-in");
300 /* resuse the type from the old target */
303 h = (idx << 2) | type;
306 case HANDLE_WEAK_TRACK:
307 val = (gpointer)HIDE_POINTER (val);
308 gc_handles [idx] = val;
309 gc_handle_types [idx] = type;
311 if (gc_handles [idx] != (gpointer)-1)
312 GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
314 g_error ("No weakref support");
318 gc_handles [idx] = val;
319 gc_handle_types [idx] = type;
326 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
328 int idx = handle >> 2;
329 int type = handle & 0x3;
332 g_assert (type == gc_handle_types [idx]);
333 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
334 if (gc_handles [idx] != (gpointer)-1)
335 GC_unregister_disappearing_link (&(gc_handles [idx]));
338 g_error ("No GCHandle support");
341 gc_handles [idx] = (gpointer)-1;
342 gc_handle_types [idx] = (guint8)-1;
346 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
349 int type = handle & 0x3;
352 obj = gc_handles [handle >> 2];
353 g_assert (gc_handle_types [handle >> 2] == type);
354 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
355 obj = REVEAL_POINTER (obj);
356 if (obj == (MonoObject *) -1)
364 static HANDLE finalizer_event;
365 static volatile gboolean finished=FALSE;
367 static void finalize_notify (void)
370 g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
373 SetEvent (finalizer_event);
376 static guint32 finalizer_thread (gpointer unused)
380 mono_new_thread_init (NULL, &stack_start);
383 /* Wait to be notified that there's at least one
386 WaitForSingleObject (finalizer_event, INFINITE);
389 g_message (G_GNUC_PRETTY_FUNCTION ": invoking finalizers");
392 GC_invoke_finalizers ();
398 void mono_gc_init (void)
402 finalizer_event=CreateEvent (NULL, FALSE, FALSE, NULL);
403 if(finalizer_event==NULL) {
404 g_assert_not_reached ();
407 GC_finalize_on_demand=1;
408 GC_finalizer_notifier=finalize_notify;
410 /* Don't use mono_thread_create here, because we don't want
411 * the runtime to wait for this thread to exit when it's
414 gc_thread=CreateThread (NULL, 0, finalizer_thread, NULL, 0, NULL);
415 if(gc_thread==NULL) {
416 g_assert_not_reached ();
420 void mono_gc_cleanup (void)
423 g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");