2005-07-21 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / metadata / gc.c
index a6b81d5b2cda46fc1fbc19955e2556c0fde8d3de..a8d66b7566a4ed8123a8d9521155c3a55aeb377d 100644 (file)
 #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;
@@ -39,8 +34,6 @@ extern int __imp_GC_finalize_on_demand;
 #define GC_finalize_on_demand __imp_GC_finalize_on_demand
 #endif
 
-static int finalize_slot = -1;
-
 static gboolean gc_disabled = FALSE;
 
 static CRITICAL_SECTION finalizer_mutex;
@@ -69,19 +62,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);
@@ -106,7 +86,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 */
@@ -239,11 +230,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
@@ -345,7 +332,7 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
        default:
                g_assert_not_reached ();
        }
-       return -1;
+       return 0;
 }
 
 void
@@ -383,10 +370,12 @@ 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 
- * (sadly libgc still requires REVEAL/HIDE_POINTER)
  */
 static HandleData gc_handles [] = {
        {NULL, NULL, 0, HANDLE_WEAK, 0},
@@ -417,13 +406,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);
        }
@@ -456,37 +442,33 @@ 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 {
-#ifdef HAVE_BOEHM_GC
                        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) {
-                               GC_unregister_disappearing_link (&(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]), REVEAL_POINTER (entries [i]), entries [i], handles->entries [i]);*/
-                               if (entries [i] && entries [i] != (gpointer)-1) {
-                                       GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(entries [i]), REVEAL_POINTER (entries [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 ();
-#else
-                       handles->entries = g_realloc (handles->entries, sizeof (gpointer) * new_size);
-                       memset (handles->entries + handles->size, 0, sizeof (gpointer) * handles->size);
-#endif
                }
 
                /* set i and slot to the next free position */
@@ -498,16 +480,14 @@ alloc_handle (HandleData *handles, MonoObject *obj)
        handles->bitmap [slot] |= 1 << i;
        slot = slot * 32 + i;
        handles->entries [slot] = obj;
-#ifdef HAVE_BOEHM_GC
        if (handles->type <= HANDLE_WEAK_TRACK) {
-               handles->entries [slot] = (void*)HIDE_POINTER (obj);
-               GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(handles->entries [slot]), obj);
+               if (obj)
+                       mono_gc_weak_link_add (&(handles->entries [slot]), obj);
        }
-#endif
 
        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);
 }
 
 guint32
@@ -526,16 +506,18 @@ mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
 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) {
-               obj = handles->entries [slot];
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
                if (handles->type <= HANDLE_WEAK_TRACK) {
-                       obj = REVEAL_POINTER (obj);
-                       if (obj == (MonoObject *) -1)
-                               obj = NULL;
+                       obj = mono_gc_weak_link_get (&handles->entries [slot]);
+               } else {
+                       obj = handles->entries [slot];
                }
        } else {
                /* print a warning? */
@@ -548,45 +530,73 @@ 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) {
-#ifdef HAVE_BOEHM_GC
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
                if (handles->type <= HANDLE_WEAK_TRACK) {
-                       GC_unregister_disappearing_link (&handles->entries [slot]);
-                       GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(handles->entries [slot]), obj);
-                       handles->entries [slot] = (void*)HIDE_POINTER (obj);
+                       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
-               handles->entries [slot] = obj;
-#endif
        } 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);
 }
 
+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;
+}
+
 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) {
-#ifdef HAVE_BOEHM_GC
+       if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
                if (handles->type <= HANDLE_WEAK_TRACK)
-                       GC_unregister_disappearing_link (&handles->entries [slot]);
-#endif
+                       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, gchandle & 3);*/
+       /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
        unlock_handles (handles);
 }
 
@@ -803,7 +813,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);
                        }
                }