Merge pull request #2816 from xmcclure/profile-clean-0
[mono.git] / mono / metadata / sgen-toggleref.c
index 7407e9d70e05a50a826dbc191e42f21fc3b84776..5ca66e738af696bcbd0efcab0000a4c7bd32c9ec 100644 (file)
@@ -7,32 +7,22 @@
  * Copyright 2011 Xamarin, Inc.
  * Copyright (C) 2012 Xamarin Inc
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License 2.0 as published by the Free Software Foundation;
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License 2.0 along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
 
 #ifdef HAVE_SGEN_GC
 
-#include "sgen-gc.h"
+#include "sgen/sgen-gc.h"
 #include "sgen-toggleref.h"
+#include "sgen/sgen-client.h"
 
 
 /*only one of the two can be non null at a given time*/
 typedef struct {
-       void *strong_ref;
-       void *weak_ref;
+       GCObject *strong_ref;
+       GCObject *weak_ref;
 } MonoGCToggleRef;
 
 static MonoToggleRefStatus (*toggleref_callback) (MonoObject *obj);
@@ -90,26 +80,39 @@ sgen_process_togglerefs (void)
                w);
 }
 
-void
-sgen_scan_togglerefs (char *start, char *end, ScanCopyContext ctx)
+void sgen_client_mark_togglerefs (char *start, char *end, ScanCopyContext ctx)
 {
-       CopyOrMarkObjectFunc copy_func = ctx.copy_func;
+       CopyOrMarkObjectFunc copy_func = ctx.ops->copy_or_mark_object;
        SgenGrayQueue *queue = ctx.queue;
        int i;
 
-       SGEN_LOG (4, "Scanning ToggleRefs %d", toggleref_array_size);
+       SGEN_LOG (4, "Marking ToggleRefs %d", toggleref_array_size);
 
        for (i = 0; i < toggleref_array_size; ++i) {
                if (toggleref_array [i].strong_ref) {
-                       char *object = toggleref_array [i].strong_ref;
-                       if (object >= start && object < end) {
+                       GCObject *object = toggleref_array [i].strong_ref;
+                       if ((char*)object >= start && (char*)object < end) {
                                SGEN_LOG (6, "\tcopying strong slot %d", i);
                                copy_func (&toggleref_array [i].strong_ref, queue);
                        }
-               } else if (toggleref_array [i].weak_ref) {
-                       char *object = toggleref_array [i].weak_ref;
+               }
+       }
+       sgen_drain_gray_stack (ctx);
+}
 
-                       if (object >= start && object < end) {
+void sgen_client_clear_togglerefs (char *start, char *end, ScanCopyContext ctx)
+{
+       CopyOrMarkObjectFunc copy_func = ctx.ops->copy_or_mark_object;
+       SgenGrayQueue *queue = ctx.queue;
+       int i;
+
+       SGEN_LOG (4, "Clearing ToggleRefs %d", toggleref_array_size);
+
+       for (i = 0; i < toggleref_array_size; ++i) {
+               if (toggleref_array [i].weak_ref) {
+                       GCObject *object = toggleref_array [i].weak_ref;
+
+                       if ((char*)object >= start && (char*)object < end) {
                                if (sgen_gc_is_object_ready_for_finalization (object)) {
                                        SGEN_LOG (6, "\tcleaning weak slot %d", i);
                                        toggleref_array [i].weak_ref = NULL; /* We defer compaction to only happen on the callback step. */
@@ -120,6 +123,7 @@ sgen_scan_togglerefs (char *start, char *end, ScanCopyContext ctx)
                        }
                }
        }
+       sgen_drain_gray_stack (ctx);
 }
 
 static void
@@ -127,7 +131,7 @@ ensure_toggleref_capacity (int capacity)
 {
        if (!toggleref_array) {
                toggleref_array_capacity = 32;
-               toggleref_array = sgen_alloc_internal_dynamic (
+               toggleref_array = (MonoGCToggleRef *)sgen_alloc_internal_dynamic (
                        toggleref_array_capacity * sizeof (MonoGCToggleRef),
                        INTERNAL_MEM_TOGGLEREF_DATA,
                        TRUE);
@@ -138,7 +142,7 @@ ensure_toggleref_capacity (int capacity)
                while (toggleref_array_capacity < toggleref_array_size + capacity)
                        toggleref_array_capacity *= 2;
 
-               tmp = sgen_alloc_internal_dynamic (
+               tmp = (MonoGCToggleRef *)sgen_alloc_internal_dynamic (
                        toggleref_array_capacity * sizeof (MonoGCToggleRef),
                        INTERNAL_MEM_TOGGLEREF_DATA,
                        TRUE);
@@ -191,4 +195,26 @@ mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (
        toggleref_callback = proccess_toggleref;
 }
 
+static MonoToggleRefStatus
+test_toggleref_callback (MonoObject *obj)
+{
+       static MonoClassField *mono_toggleref_test_field;
+       MonoToggleRefStatus status = MONO_TOGGLE_REF_DROP;
+
+       if (!mono_toggleref_test_field) {
+               mono_toggleref_test_field = mono_class_get_field_from_name (mono_object_get_class (obj), "__test");
+               g_assert (mono_toggleref_test_field);
+       }
+
+       mono_field_get_value (obj, mono_toggleref_test_field, &status);
+       printf ("toggleref-cb obj %d\n", status);
+       return status;
+}
+
+void
+sgen_register_test_toggleref_callback (void)
+{
+       toggleref_callback = test_toggleref_callback;
+}
+
 #endif