* class.c (my_mono_class_from_generic_parameter): param->owner can be null.
[mono.git] / mono / metadata / gc.c
index d73bb63ea1ea92cd4a45f8467a55acb079ea9d8d..b8374c6d0643f7bd2e57fb947138bbc7a26b5062 100644 (file)
@@ -11,6 +11,7 @@
 #include <string.h>
 
 #include <mono/metadata/gc-internal.h>
+#include <mono/metadata/mono-gc.h>
 #include <mono/metadata/threads.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/domain-internals.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/utils/mono-logger.h>
-#define GC_I_HIDE_POINTERS
 #include <mono/os/gc_wrapper.h>
-
-#ifndef HIDE_POINTER
-#define HIDE_POINTER(v)         (v)
-#define REVEAL_POINTER(v)       (v)
-#endif
+#include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
 
 typedef struct DomainFinalizationReq {
        MonoDomain *domain;
@@ -38,14 +34,10 @@ extern int __imp_GC_finalize_on_demand;
 #define GC_finalize_on_demand __imp_GC_finalize_on_demand
 #endif
 
-#ifdef HAVE_VALGRIND_MEMCHECK_H
-#include <valgrind/memcheck.h>
-#endif
-
-static int finalize_slot = -1;
-
 static gboolean gc_disabled = FALSE;
 
+#define mono_finalizer_lock() EnterCriticalSection (&finalizer_mutex)
+#define mono_finalizer_unlock() LeaveCriticalSection (&finalizer_mutex)
 static CRITICAL_SECTION finalizer_mutex;
 
 static GSList *domains_to_finalize= NULL;
@@ -72,19 +64,6 @@ run_finalize (void *obj, void *data)
        MonoObject *o, *o2;
        o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
 
-       if (finalize_slot < 0) {
-               int i;
-               MonoClass* obj_class = mono_get_object_class ();
-               for (i = 0; i < obj_class->vtable_size; ++i) {
-                       MonoMethod *cm = obj_class->vtable [i];
-              
-                       if (!strcmp (mono_method_get_name (cm), "Finalize")) {
-                               finalize_slot = i;
-                               break;
-                       }
-               }
-       }
-
        mono_domain_lock (o->vtable->domain);
 
        o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
@@ -109,7 +88,18 @@ run_finalize (void *obj, void *data)
        /* Use _internal here, since this thread can enter a doomed appdomain */
        mono_domain_set_internal (mono_object_domain (o));              
 
-       mono_runtime_invoke (o->vtable->klass->vtable [finalize_slot], o, NULL, &exc);
+       /* delegates that have a native function pointer allocated are
+        * registered for finalization, but they don't have a Finalize
+        * method, because in most cases it's not needed and it's just a waste.
+        */
+       if (o->vtable->klass->delegate) {
+               MonoDelegate* del = (MonoDelegate*)o;
+               if (del->delegate_trampoline)
+                       mono_delegate_free_ftnptr ((MonoDelegate*)o);
+               return;
+       }
+
+       mono_runtime_invoke (mono_class_get_finalizer (o->vtable->klass), o, NULL, &exc);
 
        if (exc) {
                /* fixme: do something useful */
@@ -169,6 +159,14 @@ object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
 #endif
 }
 
+/**
+ * mono_object_register_finalizer:
+ * @obj: object to register
+ *
+ * Records that object @obj has a finalizer, this will call the
+ * Finalize method when the garbage collector disposes the object.
+ * 
+ */
 void
 mono_object_register_finalizer (MonoObject *obj)
 {
@@ -176,11 +174,14 @@ mono_object_register_finalizer (MonoObject *obj)
        object_register_finalizer (obj, run_finalize);
 }
 
-/*
+/**
  * mono_domain_finalize:
+ * @domain: the domain to finalize
+ * @timeout: msects to wait for the finalization to complete
  *
  *  Request finalization of all finalizable objects inside @domain. Wait
  * @timeout msecs for the finalization to complete.
+ *
  * Returns: TRUE if succeeded, FALSE if there was a timeout
  */
 
@@ -191,6 +192,10 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
        guint32 res;
        HANDLE done_event;
 
+       if (mono_thread_current () == gc_thread)
+               /* We are called from inside a finalizer, not much we can do here */
+               return FALSE;
+
        mono_profiler_appdomain_event (domain, MONO_PROFILE_START_UNLOAD);
 
        /* 
@@ -210,11 +215,11 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
        req->domain = domain;
        req->done_event = done_event;
        
-       EnterCriticalSection (&finalizer_mutex);
+       mono_finalizer_lock ();
 
        domains_to_finalize = g_slist_append (domains_to_finalize, req);
 
-       LeaveCriticalSection (&finalizer_mutex);
+       mono_finalizer_unlock ();
 
        /* Tell the finalizer thread to finalize this appdomain */
        finalize_notify ();
@@ -238,11 +243,7 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
 void
 ves_icall_System_GC_InternalCollect (int generation)
 {
-       MONO_ARCH_SAVE_REGS;
-
-#if HAVE_BOEHM_GC
-       GC_gcollect ();
-#endif
+       mono_gc_collect (generation);
 }
 
 gint64
@@ -250,13 +251,9 @@ ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
 {
        MONO_ARCH_SAVE_REGS;
 
-#if HAVE_BOEHM_GC
        if (forceCollection)
-               GC_gcollect ();
-       return GC_get_heap_size () - GC_get_free_bytes ();
-#else
-       return 0;
-#endif
+               mono_gc_collect (mono_gc_max_generation ());
+       return mono_gc_get_used_size ();
 }
 
 void
@@ -306,20 +303,10 @@ ves_icall_System_GC_WaitForPendingFinalizers (void)
 #else
 #endif
 }
-
+#define mono_allocator_lock() EnterCriticalSection (&allocator_section)
+#define mono_allocator_unlock() LeaveCriticalSection (&allocator_section)
 static CRITICAL_SECTION allocator_section;
 static CRITICAL_SECTION handle_section;
-static guint32 next_handle = 0;
-static gpointer *gc_handles = NULL;
-static guint8 *gc_handle_types = NULL;
-static guint32 array_size = 0;
-
-/*
- * The handle type is encoded in the lower two bits of the handle value:
- * 0 -> normal
- * 1 -> pinned
- * 2 -> weak
- */
 
 typedef enum {
        HANDLE_WEAK,
@@ -328,197 +315,363 @@ typedef enum {
        HANDLE_PINNED
 } HandleType;
 
-/*
- * FIXME: make thread safe and reuse the array entries.
- */
+static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
+
 MonoObject *
 ves_icall_System_GCHandle_GetTarget (guint32 handle)
 {
-       MonoObject *obj;
-       gint32 type;
-
-       MONO_ARCH_SAVE_REGS;
-
-       if (gc_handles) {
-               type = handle & 0x3;
-               EnterCriticalSection (&handle_section);
-               g_assert (type == gc_handle_types [handle >> 2]);
-               obj = gc_handles [handle >> 2];
-               LeaveCriticalSection (&handle_section);
-               if (!obj)
-                       return NULL;
-
-               if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK))
-                       return REVEAL_POINTER (obj);
-               else
-                       return obj;
-       }
-       return NULL;
+       return mono_gchandle_get_target (handle);
 }
 
+/*
+ * if type == -1, change the target of the handle, otherwise allocate a new handle.
+ */
 guint32
 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
 {
-       gpointer val = obj;
-       guint32 h, idx;
-
-       MONO_ARCH_SAVE_REGS;
-
-       EnterCriticalSection (&handle_section);
-       /* Indexes start from 1 since 0 means the handle is not allocated */
-       idx = ++next_handle;
-       if (idx >= array_size) {
-               gpointer *new_array;
-               guint8 *new_type_array;
-               if (!array_size)
-                       array_size = 16;
-#if HAVE_BOEHM_GC
-               new_array = GC_MALLOC (sizeof (gpointer) * (array_size * 2));
-               new_type_array = GC_MALLOC (sizeof (guint8) * (array_size * 2));
-#else
-               new_array = g_malloc0 (sizeof (gpointer) * (array_size * 2));
-               new_type_array = g_malloc0 (sizeof (guint8) * (array_size * 2));
-#endif
-               if (gc_handles) {
-                       int i;
-                       memcpy (new_array, gc_handles, sizeof (gpointer) * array_size);
-                       memcpy (new_type_array, gc_handle_types, sizeof (guint8) * array_size);
-                       /* need to re-register links for weak refs. test if GC_realloc needs the same */
-                       for (i = 0; i < array_size; ++i) {
-#if 0 /* This breaks the threaded finalizer, by causing segfaults deep
-       * inside libgc.  I assume it will also break without the
-       * threaded finalizer, just that the stress test (bug 31333)
-       * deadlocks too early without it.  Reverting to the previous
-       * version here stops the segfault.
-       */
-                               if ((gc_handle_types[i] == HANDLE_WEAK) || (gc_handle_types[i] == HANDLE_WEAK_TRACK)) { /* all and only disguised pointers have it set */
-#else
-                               if (((gulong)new_array [i]) & 0x1) {
-#endif
-#if HAVE_BOEHM_GC
-                                       if (gc_handles [i] != (gpointer)-1)
-                                               GC_unregister_disappearing_link (&(gc_handles [i]));
-                                       if (new_array [i] != (gpointer)-1)
-                                               GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(new_array [i]), REVEAL_POINTER (new_array [i]));
-#endif
-                               }
-                       }
-               }
-               array_size *= 2;
-#ifndef HAVE_BOEHM_GC
-               g_free (gc_handles);
-               g_free (gc_handle_types);
-#endif
-               gc_handles = new_array;
-               gc_handle_types = new_type_array;
+       if (type == -1) {
+               mono_gchandle_set_target (handle, obj);
+               /* the handle doesn't change */
+               return handle;
        }
-
-       /* resuse the type from the old target */
-       if (type == -1)
-               type =  handle & 0x3;
-       h = (idx << 2) | type;
        switch (type) {
        case HANDLE_WEAK:
+               return mono_gchandle_new_weakref (obj, FALSE);
        case HANDLE_WEAK_TRACK:
-               val = (gpointer)HIDE_POINTER (val);
-               gc_handles [idx] = val;
-               gc_handle_types [idx] = type;
-#if HAVE_BOEHM_GC
-               if (gc_handles [idx] != (gpointer)-1)
-                       GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
-#endif
-               break;
+               return mono_gchandle_new_weakref (obj, TRUE);
+       case HANDLE_NORMAL:
+               return mono_gchandle_new (obj, FALSE);
+       case HANDLE_PINNED:
+               return mono_gchandle_new (obj, TRUE);
        default:
-               gc_handles [idx] = val;
-               gc_handle_types [idx] = type;
-               break;
+               g_assert_not_reached ();
        }
-       LeaveCriticalSection (&handle_section);
-       return h;
+       return 0;
 }
 
 void
 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
 {
-       int idx = handle >> 2;
-       int type = handle & 0x3;
-
-       MONO_ARCH_SAVE_REGS;
-
-       EnterCriticalSection (&handle_section);
-
-#ifdef HAVE_BOEHM_GC
-       g_assert (type == gc_handle_types [idx]);
-       if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
-               if (gc_handles [idx] != (gpointer)-1)
-                       GC_unregister_disappearing_link (&(gc_handles [idx]));
-       }
-#endif
-
-       gc_handles [idx] = (gpointer)-1;
-       gc_handle_types [idx] = (guint8)-1;
-       LeaveCriticalSection (&handle_section);
+       mono_gchandle_free (handle);
 }
 
 gpointer
 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
 {
        MonoObject *obj;
-       int type = handle & 0x3;
 
-       MONO_ARCH_SAVE_REGS;
+       obj = mono_gchandle_get_target (handle);
+       if (obj) {
+               MonoClass *klass = mono_object_class (obj);
+               if (klass == mono_defaults.string_class) {
+                       return mono_string_chars ((MonoString*)obj);
+               } else if (klass->rank) {
+                       return mono_array_addr ((MonoArray*)obj, char, 0);
+               } else {
+                       /* the C# code will check and throw the exception */
+                       /* FIXME: missing !klass->blittable test, see bug #61134 */
+                       if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
+                               return (gpointer)-1;
+                       return (char*)obj + sizeof (MonoObject);
+               }
+       }
+       return NULL;
+}
+
+typedef struct {
+       guint32  *bitmap;
+       gpointer *entries;
+       guint32   size;
+       guint8    type;
+       guint     slot_hint : 24; /* starting slot for search */
+       /* 2^16 appdomains should be enough for everyone (though I know I'll regret this in 20 years) */
+       /* we alloc this only for weak refs, since we can get the domain directly in the other cases */
+       guint16  *domain_ids;
+} HandleData;
+
+/* weak and weak-track arrays will be allocated in malloc memory 
+ */
+static HandleData gc_handles [] = {
+       {NULL, NULL, 0, HANDLE_WEAK, 0},
+       {NULL, NULL, 0, HANDLE_WEAK_TRACK, 0},
+       {NULL, NULL, 0, HANDLE_NORMAL, 0},
+       {NULL, NULL, 0, HANDLE_PINNED, 0}
+};
+
+#define lock_handles(handles) EnterCriticalSection (&handle_section)
+#define unlock_handles(handles) LeaveCriticalSection (&handle_section)
+
+static int
+find_first_unset (guint32 bitmap)
+{
+       int i;
+       for (i = 0; i < 32; ++i) {
+               if (!(bitmap & (1 << i)))
+                       return i;
+       }
+       return -1;
+}
 
-       if (gc_handles) {
-               EnterCriticalSection (&handle_section);
-               obj = gc_handles [handle >> 2];
-               g_assert (gc_handle_types [handle >> 2] == type);
-               LeaveCriticalSection (&handle_section);
-               if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
-                       obj = REVEAL_POINTER (obj);
-                       if (obj == (MonoObject *) -1)
-                               return NULL;
+static guint32
+alloc_handle (HandleData *handles, MonoObject *obj)
+{
+       gint slot, i;
+       lock_handles (handles);
+       if (!handles->size) {
+               handles->size = 32;
+               if (handles->type > HANDLE_WEAK_TRACK) {
+                       handles->entries = mono_gc_alloc_fixed (sizeof (gpointer) * handles->size, NULL);
+               } else {
+                       handles->entries = g_malloc0 (sizeof (gpointer) * handles->size);
+                       handles->domain_ids = g_malloc0 (sizeof (guint16) * handles->size);
                }
-               if (obj) {
-                       MonoClass *klass = mono_object_class (obj);
-                       if (klass == mono_defaults.string_class) {
-                               return mono_string_chars ((MonoString*)obj);
-                       } else if (klass->rank) {
-                               return mono_array_addr ((MonoArray*)obj, char, 0);
-                       } else {
-                               /* the C# code will check and throw the exception */
-                               /* FIXME: missing !klass->blittable test, see bug #61134 */
-                               if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
-                                       return (gpointer)-1;
-                               return (char*)obj + sizeof (MonoObject);
+               handles->bitmap = g_malloc0 (handles->size / 8);
+       }
+       i = -1;
+       for (slot = handles->slot_hint; slot < handles->size / 32; ++slot) {
+               if (handles->bitmap [slot] != 0xffffffff) {
+                       i = find_first_unset (handles->bitmap [slot]);
+                       handles->slot_hint = slot;
+                       break;
+               }
+       }
+       if (i == -1 && handles->slot_hint != 0) {
+               for (slot = 0; slot < handles->slot_hint; ++slot) {
+                       if (handles->bitmap [slot] != 0xffffffff) {
+                               i = find_first_unset (handles->bitmap [slot]);
+                               handles->slot_hint = slot;
+                               break;
                        }
                }
        }
-       return NULL;
+       if (i == -1) {
+               guint32 *new_bitmap;
+               guint32 new_size = handles->size * 2; /* always double: we memset to 0 based on this below */
+
+               /* resize and copy the bitmap */
+               new_bitmap = g_malloc0 (new_size / 8);
+               memcpy (new_bitmap, handles->bitmap, handles->size / 8);
+               g_free (handles->bitmap);
+               handles->bitmap = new_bitmap;
+
+               /* resize and copy the entries */
+               if (handles->type > HANDLE_WEAK_TRACK) {
+                       gpointer *entries;
+                       entries = mono_gc_alloc_fixed (sizeof (gpointer) * new_size, NULL);
+                       memcpy (entries, handles->entries, sizeof (gpointer) * handles->size);
+                       handles->entries = entries;
+               } else {
+                       gpointer *entries;
+                       guint16 *domain_ids;
+                       domain_ids = g_malloc0 (sizeof (guint16) * new_size);
+                       entries = g_malloc (sizeof (gpointer) * new_size);
+                       /* we disable GC because we could lose some disappearing link updates */
+                       mono_gc_disable ();
+                       memcpy (entries, handles->entries, sizeof (gpointer) * handles->size);
+                       memset (entries + handles->size, 0, sizeof (gpointer) * handles->size);
+                       memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
+                       for (i = 0; i < handles->size; ++i) {
+                               MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
+                               mono_gc_weak_link_remove (&(handles->entries [i]));
+                               /*g_print ("reg/unreg entry %d of type %d at %p to object %p (%p), was: %p\n", i, handles->type, &(entries [i]), obj, entries [i], handles->entries [i]);*/
+                               if (obj) {
+                                       mono_gc_weak_link_add (&(entries [i]), obj);
+                               }
+                       }
+                       g_free (handles->entries);
+                       g_free (handles->domain_ids);
+                       handles->entries = entries;
+                       handles->domain_ids = domain_ids;
+                       mono_gc_enable ();
+               }
+
+               /* set i and slot to the next free position */
+               i = 0;
+               slot = (handles->size + 1) / 32;
+               handles->slot_hint = handles->size + 1;
+               handles->size = new_size;
+       }
+       handles->bitmap [slot] |= 1 << i;
+       slot = slot * 32 + i;
+       handles->entries [slot] = obj;
+       if (handles->type <= HANDLE_WEAK_TRACK) {
+               if (obj)
+                       mono_gc_weak_link_add (&(handles->entries [slot]), obj);
+       }
+
+       unlock_handles (handles);
+       /*g_print ("allocated entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
+       return (slot << 3) | (handles->type + 1);
 }
 
+/**
+ * mono_gchandle_new:
+ * @obj: managed object to get a handle for
+ * @pinned: whether the object should be pinned
+ *
+ * This returns a handle that wraps the object, this is used to keep a
+ * reference to a managed object from the unmanaged world and preventing the
+ * object from being disposed.
+ * 
+ * If @pinned is false the address of the object can not be obtained, if it is
+ * true the address of the object can be obtained.  This will also pin the
+ * object so it will not be possible by a moving garbage collector to move the
+ * object. 
+ * 
+ * Returns: a handle that can be used to access the object from
+ * unmanaged code.
+ */
 guint32
 mono_gchandle_new (MonoObject *obj, gboolean pinned)
 {
-       return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, pinned? HANDLE_PINNED: HANDLE_NORMAL);
+       return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj);
 }
 
+/**
+ * mono_gchandle_new_weakref:
+ * @obj: managed object to get a handle for
+ * @pinned: whether the object should be pinned
+ *
+ * This returns a weak handle that wraps the object, this is used to
+ * keep a reference to a managed object from the unmanaged world.
+ * Unlike the mono_gchandle_new the object can be reclaimed by the
+ * garbage collector.  In this case the value of the GCHandle will be
+ * set to zero.
+ * 
+ * If @pinned is false the address of the object can not be obtained, if it is
+ * true the address of the object can be obtained.  This will also pin the
+ * object so it will not be possible by a moving garbage collector to move the
+ * object. 
+ * 
+ * Returns: a handle that can be used to access the object from
+ * unmanaged code.
+ */
 guint32
 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
 {
-       return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK);
+       return alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj);
 }
 
-/* This will return NULL for a collected object if using a weakref handle */
+/**
+ * mono_gchandle_get_target:
+ * @gchandle: a GCHandle's handle.
+ *
+ * The handle was previously created by calling mono_gchandle_new or
+ * mono_gchandle_new_weakref. 
+ *
+ * Returns a pointer to the MonoObject represented by the handle or
+ * NULL for a collected object if using a weakref handle.
+ */
 MonoObject*
 mono_gchandle_get_target (guint32 gchandle)
 {
-       return ves_icall_System_GCHandle_GetTarget (gchandle);
+       guint slot = gchandle >> 3;
+       guint type = (gchandle & 7) - 1;
+       HandleData *handles = &gc_handles [type];
+       MonoObject *obj = NULL;
+       if (type > 3)
+               return NULL;
+       lock_handles (handles);
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
+               if (handles->type <= HANDLE_WEAK_TRACK) {
+                       obj = mono_gc_weak_link_get (&handles->entries [slot]);
+               } else {
+                       obj = handles->entries [slot];
+               }
+       } else {
+               /* print a warning? */
+       }
+       unlock_handles (handles);
+       /*g_print ("get target of entry %d of type %d: %p\n", slot, handles->type, obj);*/
+       return obj;
+}
+
+static void
+mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
+{
+       guint slot = gchandle >> 3;
+       guint type = (gchandle & 7) - 1;
+       HandleData *handles = &gc_handles [type];
+       if (type > 3)
+               return;
+       lock_handles (handles);
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
+               if (handles->type <= HANDLE_WEAK_TRACK) {
+                       mono_gc_weak_link_remove (&handles->entries [slot]);
+                       if (obj)
+                               mono_gc_weak_link_add (&handles->entries [slot], obj);
+               } else {
+                       handles->entries [slot] = obj;
+               }
+       } else {
+               /* print a warning? */
+       }
+       /*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
+       unlock_handles (handles);
+}
+
+/**
+ * mono_gchandle_is_in_domain:
+ * @gchandle: a GCHandle's handle.
+ * @domain: An application domain.
+ *
+ * Returns: true if the object wrapped by the @gchandle belongs to the specific @domain.
+ */
+gboolean
+mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
+{
+       guint slot = gchandle >> 3;
+       guint type = (gchandle & 7) - 1;
+       HandleData *handles = &gc_handles [type];
+       gboolean result = FALSE;
+       if (type > 3)
+               return FALSE;
+       lock_handles (handles);
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
+               if (handles->type <= HANDLE_WEAK_TRACK) {
+                       result = domain->domain_id == handles->domain_ids [slot];
+               } else {
+                       MonoObject *obj;
+                       obj = handles->entries [slot];
+                       if (obj == NULL)
+                               result = TRUE;
+                       else
+                               result = domain == mono_object_domain (obj);
+               }
+       } else {
+               /* print a warning? */
+       }
+       unlock_handles (handles);
+       return result;
 }
 
+/**
+ * mono_gchandle_free:
+ * @gchandle: a GCHandle's handle.
+ *
+ * Frees the @gchandle handle.  If there are no outstanding
+ * references, the garbage collector can reclaim the memory of the
+ * object wrapped. 
+ */
 void
 mono_gchandle_free (guint32 gchandle)
 {
-       ves_icall_System_GCHandle_FreeHandle (gchandle);
+       guint slot = gchandle >> 3;
+       guint type = (gchandle & 7) - 1;
+       HandleData *handles = &gc_handles [type];
+       if (type > 3)
+               return;
+       lock_handles (handles);
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
+               if (handles->type <= HANDLE_WEAK_TRACK)
+                       mono_gc_weak_link_remove (&handles->entries [slot]);
+               handles->entries [slot] = NULL;
+               handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
+       } else {
+               /* print a warning? */
+       }
+       /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
+       unlock_handles (handles);
 }
 
 #if HAVE_BOEHM_GC
@@ -597,16 +750,16 @@ static guint32 finalizer_thread (gpointer unused)
                WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
 
                if (domains_to_finalize) {
-                       EnterCriticalSection (&finalizer_mutex);
+                       mono_finalizer_lock ();
                        if (domains_to_finalize) {
                                DomainFinalizationReq *req = domains_to_finalize->data;
                                domains_to_finalize = g_slist_remove (domains_to_finalize, req);
-                               LeaveCriticalSection (&finalizer_mutex);
+                               mono_finalizer_unlock ();
 
                                finalize_domain_objects (req);
                        }
                        else
-                               LeaveCriticalSection (&finalizer_mutex);
+                               mono_finalizer_unlock ();
                }                               
 
 #ifdef DEBUG
@@ -647,12 +800,12 @@ extern void mono_gc_push_all_stacks (void);
 
 static void mono_gc_lock (void)
 {
-       EnterCriticalSection (&allocator_section);
+       mono_allocator_lock ();
 }
 
 static void mono_gc_unlock (void)
 {
-       LeaveCriticalSection (&allocator_section);
+       mono_allocator_unlock ();
 }
 
 static GCThreadFunctions mono_gc_thread_vtable = {
@@ -674,20 +827,6 @@ mono_gc_warning (char *msg, GC_word arg)
        mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
 }
 
-static gboolean
-mono_running_on_valgrind (void)
-{
-#ifdef HAVE_VALGRIND_MEMCHECK_H
-               if (RUNNING_ON_VALGRIND)
-                       return TRUE;
-               else
-                       return FALSE;
-#else
-               return FALSE;
-#endif
-}
-
-
 void mono_gc_init (void)
 {
        InitializeCriticalSection (&handle_section);
@@ -699,8 +838,8 @@ void mono_gc_init (void)
        gc_thread_vtable = &mono_gc_thread_vtable;
 #endif
        
-       MONO_GC_REGISTER_ROOT (gc_handles);
-       MONO_GC_REGISTER_ROOT (gc_handle_types);
+       MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_NORMAL].entries);
+       MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_PINNED].entries);
        GC_no_dls = TRUE;
 
        GC_oom_fn = mono_gc_out_of_memory;
@@ -714,20 +853,6 @@ void mono_gc_init (void)
                return;
        }
        
-       /* valgrind does not play nicely with the GC,
-        * so, turn it off when we are under vg.
-        */
-       if (mono_running_on_valgrind ()) {
-               /* valgrind doesnt like g_warning for some reason... */
-               printf ("You are running under valgrind. Currently, valgrind does "
-                          "not support the GC. This program will run with the GC "
-                          "turned off. Your program may take up a fair amount of "
-                          "memory. Also, finalizers will not be run.\n");
-               
-               gc_disabled = TRUE;
-               return;
-       }
-       
        finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
        pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
        shutdown_event = CreateEvent (NULL, TRUE, FALSE, NULL);
@@ -758,37 +883,21 @@ void mono_gc_cleanup (void)
        if (!gc_disabled) {
                ResetEvent (shutdown_event);
                finished = TRUE;
-               finalize_notify ();
-               /* Finishing the finalizer thread, so wait a little bit... */
-               /* MS seems to wait for about 2 seconds */
-               if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
-                       mono_thread_stop (gc_thread);
+               if (mono_thread_current () != gc_thread) {
+                       finalize_notify ();
+                       /* Finishing the finalizer thread, so wait a little bit... */
+                       /* MS seems to wait for about 2 seconds */
+                       if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
+                               mono_thread_stop (gc_thread);
+                       }
                }
+               gc_thread = NULL;
+               GC_finalizer_notifier = NULL;
        }
 
 #endif
 }
 
-void
-mono_gc_disable (void)
-{
-#ifdef HAVE_GC_ENABLE
-       GC_disable ();
-#else
-       g_assert_not_reached ();
-#endif
-}
-
-void
-mono_gc_enable (void)
-{
-#ifdef HAVE_GC_ENABLE
-       GC_enable ();
-#else
-       g_assert_not_reached ();
-#endif
-}
-
 #else
 
 /* no Boehm GC support. */
@@ -801,18 +910,18 @@ void mono_gc_cleanup (void)
 {
 }
 
-void
-mono_gc_disable (void)
-{
-}
-
-void
-mono_gc_enable (void)
-{
-}
-
 #endif
 
+/**
+ * mono_gc_is_finalizer_thread:
+ * @thread: the thread to test.
+ *
+ * In Mono objects are finalized asynchronously on a separate thread.
+ * This routine tests whether the @thread argument represents the
+ * finalization thread.
+ * 
+ * Returns true if @thread is the finalization thread.
+ */
 gboolean
 mono_gc_is_finalizer_thread (MonoThread *thread)
 {