2 * metadata/gc.c: GC icalls.
4 * Author: Paolo Molaro <lupus@ximian.com>
6 * (C) 2002 Ximian, Inc.
13 #include <mono/metadata/gc-internal.h>
14 #include <mono/metadata/threads.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/exception.h>
17 #define GC_I_HIDE_POINTERS
18 #include <mono/os/gc_wrapper.h>
21 #define HIDE_POINTER(v) (v)
22 #define REVEAL_POINTER(v) (v)
25 static int finalize_slot = -1;
27 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
30 * actually, we might want to queue the finalize requests in a separate thread,
31 * but we need to be careful about the execution domain of the thread...
34 run_finalize (void *obj, void *data)
36 MonoObject *exc = NULL;
38 o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
40 if (finalize_slot < 0) {
42 for (i = 0; i < mono_defaults.object_class->vtable_size; ++i) {
43 MonoMethod *cm = mono_defaults.object_class->vtable [i];
45 if (!strcmp (cm->name, "Finalize")) {
52 /* make sure the finalizer is not called again if the object is resurrected */
53 object_register_finalizer (obj, NULL);
54 /* speedup later... and use a timeout */
55 /*g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name);*/
56 mono_domain_set (mono_object_domain (o));
58 mono_runtime_invoke (o->vtable->klass->vtable [finalize_slot], o, NULL, &exc);
61 /* fixme: do something useful */
66 * Some of our objects may point to a different address than the address returned by GC_malloc()
67 * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
68 * This also means that in the callback we need to adjust the pointer to get back the real
70 * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer,
71 * since that, too, can cause the underlying pointer to be offset.
74 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
79 g_assert (GC_base (obj) == (char*)obj - offset);
80 GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
85 mono_object_register_finalizer (MonoObject *obj)
87 /*g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name);*/
88 object_register_finalizer (obj, run_finalize);
92 * to speedup, at class init time, check if a class or struct
93 * have fields that need to be finalized and set a flag.
96 finalize_fields (MonoClass *class, char *data, gboolean instance, GHashTable *todo) {
98 MonoClassField *field;
102 g_print ("Finalize statics on on %s\n", class->name);*/
103 if (instance && class->valuetype)
104 data -= sizeof (MonoObject);
106 for (i = 0; i < class->field.count; ++i) {
107 field = &class->fields [i];
109 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
112 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
115 switch (field->type->type) {
116 case MONO_TYPE_OBJECT:
117 case MONO_TYPE_CLASS:
118 obj = *((MonoObject**)(data + field->offset));
120 if (mono_object_class (obj)->has_finalize) {
121 /* disable the registered finalizer */
122 object_register_finalizer (obj, NULL);
123 run_finalize (obj, NULL);
126 * if the type doesn't have a finalizer, we finalize
127 * the fields ourselves just like we do for structs.
128 * Disabled for now: how do we handle loops?
130 /*finalize_fields (mono_object_class (obj), obj, TRUE, todo);*/
134 case MONO_TYPE_VALUETYPE: {
135 MonoClass *fclass = mono_class_from_mono_type (field->type);
136 if (fclass->enumtype)
138 /*finalize_fields (fclass, data + field->offset, TRUE, todo);*/
141 case MONO_TYPE_ARRAY:
142 case MONO_TYPE_SZARRAY:
143 /* FIXME: foreach item... */
149 class = class->parent;
154 finalize_static_data (MonoClass *class, MonoVTable *vtable, GHashTable *todo) {
156 if (class->enumtype || !vtable->data)
158 finalize_fields (class, vtable->data, FALSE, todo);
162 mono_domain_finalize (MonoDomain *domain) {
164 GHashTable *todo = g_hash_table_new (NULL, NULL);
168 mono_g_hash_table_foreach (domain->class_vtable_hash, (GHFunc)finalize_static_data, todo);
169 /* FIXME: finalize objects in todo... */
170 g_hash_table_destroy (todo);
174 ves_icall_System_GC_InternalCollect (int generation)
184 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
191 return GC_get_heap_size ();
198 ves_icall_System_GC_KeepAlive (MonoObject *obj)
208 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
212 object_register_finalizer (obj, run_finalize);
216 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
220 object_register_finalizer (obj, NULL);
224 ves_icall_System_GC_WaitForPendingFinalizers (void)
229 static CRITICAL_SECTION handle_section;
230 static guint32 next_handle = 0;
231 static gpointer *gc_handles = NULL;
232 static guint8 *gc_handle_types = NULL;
233 static guint32 array_size = 0;
236 * The handle type is encoded in the lower two bits of the handle value:
250 * FIXME: make thread safe and reuse the array entries.
253 ves_icall_System_GCHandle_GetTarget (guint32 handle)
262 EnterCriticalSection (&handle_section);
263 g_assert (type == gc_handle_types [handle >> 2]);
264 obj = gc_handles [handle >> 2];
265 LeaveCriticalSection (&handle_section);
269 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK))
270 return REVEAL_POINTER (obj);
278 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
285 EnterCriticalSection (&handle_section);
287 if (idx >= array_size) {
290 guint8 *new_type_array;
293 new_array = GC_MALLOC (sizeof (gpointer) * (array_size * 2));
294 new_type_array = GC_MALLOC (sizeof (guint8) * (array_size * 2));
297 memcpy (new_array, gc_handles, sizeof (gpointer) * array_size);
298 memcpy (new_type_array, gc_handle_types, sizeof (guint8) * array_size);
299 /* need to re-register links for weak refs. test if GC_realloc needs the same */
300 for (i = 0; i < array_size; ++i) {
301 #if 0 /* This breaks the threaded finalizer, by causing segfaults deep
302 * inside libgc. I assume it will also break without the
303 * threaded finalizer, just that the stress test (bug 31333)
304 * deadlocks too early without it. Reverting to the previous
305 * version here stops the segfault.
307 if ((gc_handle_types[i] == HANDLE_WEAK) || (gc_handle_types[i] == HANDLE_WEAK_TRACK)) { /* all and only disguised pointers have it set */
309 if (((gulong)new_array [i]) & 0x1) {
311 if (gc_handles [i] != (gpointer)-1)
312 GC_unregister_disappearing_link (&(gc_handles [i]));
313 if (new_array [i] != (gpointer)-1)
314 GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(new_array [i]), REVEAL_POINTER (new_array [i]));
319 gc_handles = new_array;
320 gc_handle_types = new_type_array;
322 mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support built-in"));
326 /* resuse the type from the old target */
329 h = (idx << 2) | type;
332 case HANDLE_WEAK_TRACK:
333 val = (gpointer)HIDE_POINTER (val);
334 gc_handles [idx] = val;
335 gc_handle_types [idx] = type;
337 if (gc_handles [idx] != (gpointer)-1)
338 GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
340 mono_raise_exception (mono_get_exception_execution_engine ("No weakref support"));
344 gc_handles [idx] = val;
345 gc_handle_types [idx] = type;
348 LeaveCriticalSection (&handle_section);
353 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
355 int idx = handle >> 2;
356 int type = handle & 0x3;
360 EnterCriticalSection (&handle_section);
363 g_assert (type == gc_handle_types [idx]);
364 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
365 if (gc_handles [idx] != (gpointer)-1)
366 GC_unregister_disappearing_link (&(gc_handles [idx]));
369 mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support"));
372 gc_handles [idx] = (gpointer)-1;
373 gc_handle_types [idx] = (guint8)-1;
374 LeaveCriticalSection (&handle_section);
378 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
381 int type = handle & 0x3;
386 EnterCriticalSection (&handle_section);
387 obj = gc_handles [handle >> 2];
388 g_assert (gc_handle_types [handle >> 2] == type);
389 LeaveCriticalSection (&handle_section);
390 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
391 obj = REVEAL_POINTER (obj);
392 if (obj == (MonoObject *) -1)
402 static HANDLE finalizer_event;
403 static volatile gboolean finished=FALSE;
405 static void finalize_notify (void)
408 g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
411 SetEvent (finalizer_event);
414 static guint32 finalizer_thread (gpointer unused)
418 mono_new_thread_init (NULL, &stack_start, NULL);
421 /* Wait to be notified that there's at least one
424 WaitForSingleObject (finalizer_event, INFINITE);
427 g_message (G_GNUC_PRETTY_FUNCTION ": invoking finalizers");
430 GC_invoke_finalizers ();
437 * Enable or disable the separate finalizer thread.
438 * It's currently disabled because it still requires some
439 * work in the rest of the runtime.
441 #undef ENABLE_FINALIZER_THREAD
443 void mono_gc_init (void)
447 InitializeCriticalSection (&handle_section);
449 #ifdef ENABLE_FINALIZER_THREAD
451 if (getenv ("GC_DONT_GC"))
454 finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
455 if (finalizer_event == NULL) {
456 g_assert_not_reached ();
459 GC_finalize_on_demand = 1;
460 GC_finalizer_notifier = finalize_notify;
462 /* Don't use mono_thread_create here, because we don't want
463 * the runtime to wait for this thread to exit when it's
466 gc_thread = CreateThread (NULL, 0, finalizer_thread, NULL, 0, NULL);
467 if (gc_thread == NULL) {
468 g_assert_not_reached ();
473 void mono_gc_cleanup (void)
476 g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");
479 #ifdef ENABLE_FINALIZER_THREAD
487 /* no Boehm GC support. */
488 void mono_gc_init (void)
490 InitializeCriticalSection (&handle_section);
493 void mono_gc_cleanup (void)