* class.c (my_mono_class_from_generic_parameter): param->owner can be null.
[mono.git] / mono / metadata / gc.c
index 8109509aaedbcd8bb83501b3339b8ac92adaf5fa..b8374c6d0643f7bd2e57fb947138bbc7a26b5062 100644 (file)
@@ -20,6 +20,7 @@
 #include <mono/metadata/class-internals.h>
 #include <mono/utils/mono-logger.h>
 #include <mono/os/gc_wrapper.h>
+#include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
 
 typedef struct DomainFinalizationReq {
        MonoDomain *domain;
@@ -35,6 +36,8 @@ extern int __imp_GC_finalize_on_demand;
 
 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;
@@ -85,6 +88,17 @@ 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));              
 
+       /* 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) {
@@ -145,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)
 {
@@ -152,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
  */
 
@@ -190,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 ();
@@ -218,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
@@ -282,7 +303,8 @@ 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;
 
@@ -324,7 +346,7 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
        default:
                g_assert_not_reached ();
        }
-       return -1;
+       return 0;
 }
 
 void
@@ -362,6 +384,9 @@ typedef struct {
        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 
@@ -395,13 +420,10 @@ alloc_handle (HandleData *handles, MonoObject *obj)
        if (!handles->size) {
                handles->size = 32;
                if (handles->type > HANDLE_WEAK_TRACK) {
-#ifdef HAVE_BOEHM_GC
-                       handles->entries = GC_MALLOC (sizeof (gpointer) * handles->size);
-#else
-                       handles->entries = g_malloc0 (sizeof (gpointer) * handles->size);
-#endif
+                       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);
                }
                handles->bitmap = g_malloc0 (handles->size / 8);
        }
@@ -434,22 +456,20 @@ alloc_handle (HandleData *handles, MonoObject *obj)
 
                /* resize and copy the entries */
                if (handles->type > HANDLE_WEAK_TRACK) {
-#ifdef HAVE_BOEHM_GC
                        gpointer *entries;
-                       entries = GC_MALLOC (sizeof (gpointer) * new_size);
+                       entries = mono_gc_alloc_fixed (sizeof (gpointer) * new_size, NULL);
                        memcpy (entries, handles->entries, sizeof (gpointer) * handles->size);
                        handles->entries = entries;
-#else
-                       handles->entries = g_realloc (handles->entries, sizeof (gpointer) * new_size);
-                       memset (handles->entries + handles->size, 0, sizeof (gpointer) * handles->size);
-#endif
                } 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]));
@@ -459,7 +479,9 @@ alloc_handle (HandleData *handles, MonoObject *obj)
                                }
                        }
                        g_free (handles->entries);
+                       g_free (handles->domain_ids);
                        handles->entries = entries;
+                       handles->domain_ids = domain_ids;
                        mono_gc_enable ();
                }
 
@@ -473,35 +495,84 @@ alloc_handle (HandleData *handles, MonoObject *obj)
        slot = slot * 32 + i;
        handles->entries [slot] = obj;
        if (handles->type <= HANDLE_WEAK_TRACK) {
-               mono_gc_weak_link_add (&(handles->entries [slot]), obj);
+               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 << 2) | handles->type;
+       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 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 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)
 {
-       guint slot = gchandle >> 2;
-       HandleData *handles = &gc_handles [gchandle & 3];
+       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) {
+       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 {
@@ -518,30 +589,80 @@ mono_gchandle_get_target (guint32 gchandle)
 static void
 mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
 {
-       guint slot = gchandle >> 2;
-       HandleData *handles = &gc_handles [gchandle & 3];
+       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) {
+       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]);
-                       mono_gc_weak_link_add (&handles->entries [slot], obj);
+                       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, gchandle & 3, obj, handles->entries [slot]);*/
+       /*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)
 {
-       guint slot = gchandle >> 2;
-       HandleData *handles = &gc_handles [gchandle & 3];
+       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) {
+       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;
@@ -549,7 +670,7 @@ mono_gchandle_free (guint32 gchandle)
        } else {
                /* print a warning? */
        }
-       /*g_print ("freed entry %d of type %d\n", slot, gchandle & 3);*/
+       /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
        unlock_handles (handles);
 }
 
@@ -629,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
@@ -679,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 = {
@@ -766,7 +887,7 @@ void mono_gc_cleanup (void)
                        finalize_notify ();
                        /* Finishing the finalizer thread, so wait a little bit... */
                        /* MS seems to wait for about 2 seconds */
-                       if (WaitForSingleObjectEx (shutdown_event, 2000000, FALSE) == WAIT_TIMEOUT) {
+                       if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
                                mono_thread_stop (gc_thread);
                        }
                }
@@ -791,6 +912,16 @@ void mono_gc_cleanup (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)
 {