Hmm, this breaks the build :-(
[mono.git] / mono / metadata / gc.c
index 4695c8c1a2a6b6ca544a093481bf9eda34432595..065a427987019739f086e8a0ab48eacb91a2c881 100644 (file)
@@ -16,6 +16,7 @@
 #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>
 
@@ -36,6 +37,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;
@@ -110,6 +115,19 @@ run_finalize (void *obj, void *data)
        }
 }
 
+gpointer
+mono_gc_out_of_memory (size_t size)
+{
+       /* 
+        * we could allocate at program startup some memory that we could release 
+        * back to the system at this point if we're really low on memory (ie, size is
+        * lower than the memory we set apart)
+        */
+       mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
+
+       return NULL;
+}
+
 /*
  * Some of our objects may point to a different address than the address returned by GC_malloc()
  * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
@@ -347,13 +365,17 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
        /* Indexes start from 1 since 0 means the handle is not allocated */
        idx = ++next_handle;
        if (idx >= array_size) {
-#if HAVE_BOEHM_GC
                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);
@@ -370,20 +392,22 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
 #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;
-#else
-               LeaveCriticalSection (&handle_section);
-               mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support built-in"));
-#endif
        }
 
        /* resuse the type from the old target */
@@ -399,9 +423,6 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
 #if HAVE_BOEHM_GC
                if (gc_handles [idx] != (gpointer)-1)
                        GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
-#else
-               LeaveCriticalSection (&handle_section);
-               mono_raise_exception (mono_get_exception_execution_engine ("No weakref support"));
 #endif
                break;
        default:
@@ -429,9 +450,6 @@ ves_icall_System_GCHandle_FreeHandle (guint32 handle)
                if (gc_handles [idx] != (gpointer)-1)
                        GC_unregister_disappearing_link (&(gc_handles [idx]));
        }
-#else
-       LeaveCriticalSection (&handle_section);
-       mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support"));
 #endif
 
        gc_handles [idx] = (gpointer)-1;
@@ -457,11 +475,49 @@ ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
                        if (obj == (MonoObject *) -1)
                                return NULL;
                }
-               return obj;
+               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;
 }
 
+guint32
+mono_gchandle_new (MonoObject *obj, gboolean pinned)
+{
+       return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, pinned? HANDLE_PINNED: HANDLE_NORMAL);
+}
+
+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);
+}
+
+/* This will return NULL for a collected object if using a weakref handle */
+MonoObject*
+mono_gchandle_get_target (guint32 gchandle)
+{
+       return ves_icall_System_GCHandle_GetTarget (gchandle);
+}
+
+void
+mono_gchandle_free (guint32 gchandle)
+{
+       ves_icall_System_GCHandle_FreeHandle (gchandle);
+}
+
 #if HAVE_BOEHM_GC
 
 static HANDLE finalizer_event;
@@ -515,6 +571,9 @@ finalize_domain_objects (DomainFinalizationReq *req)
                g_ptr_array_free (objs, TRUE);
        }
 
+       /* Process finalizers which are already in the queue */
+       GC_invoke_finalizers ();
+
        /* printf ("DONE.\n"); */
        SetEvent (req->done_event);
 
@@ -606,6 +665,26 @@ static GCThreadFunctions mono_gc_thread_vtable = {
 };
 #endif /* WITH_INCLUDED_LIBGC */
 
+static void
+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);
@@ -616,6 +695,14 @@ void mono_gc_init (void)
 #ifdef WITH_INCLUDED_LIBGC
        gc_thread_vtable = &mono_gc_thread_vtable;
 #endif
+       
+       MONO_GC_REGISTER_ROOT (gc_handles);
+       MONO_GC_REGISTER_ROOT (gc_handle_types);
+       GC_no_dls = TRUE;
+
+       GC_oom_fn = mono_gc_out_of_memory;
+
+       GC_set_warn_proc (mono_gc_warning);
 
 #ifdef ENABLE_FINALIZER_THREAD
 
@@ -624,6 +711,20 @@ 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.");
+               
+               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);