Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / sgen-old-bridge.c
index 63b39f4946ab644eb84cc1b6ea2cae9cfb4f0485..12f82b07f725afa8c0bfad012269a0a480a36814 100644 (file)
@@ -1,40 +1,12 @@
-/*
- * sgen-bridge.c: Simple generational GC.
+/**
+ * \file
+ * Simple generational GC.
  *
  * Copyright 2011 Novell, Inc (http://www.novell.com)
  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
- *
- * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
- * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
- *
- * Permission is hereby granted to use or copy this program
- * for any purpose,  provided the above notices are retained on all copies.
- * Permission to modify the code and to distribute modified code is granted,
- * provided the above notices are retained, and a notice that the code was
- * modified is included with the above copyright notice.
- *
- *
  * Copyright 2001-2003 Ximian, Inc
  * Copyright 2003-2010 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
@@ -70,11 +42,12 @@ typedef struct {
        DynArray array;
 } DynSCCArray;
 
-
 /*
+ * Bridge data for a single managed object
+ *
  * FIXME: Optimizations:
  *
- * Don't allocate a scrs array for just one source.  Most objects have
+ * Don't allocate a srcs array for just one source.  Most objects have
  * just one source, so use the srcs pointer itself.
  */
 typedef struct _HashEntry {
@@ -85,8 +58,10 @@ typedef struct _HashEntry {
 
        int finishing_time;
 
+       // "Source" managed objects pointing at this destination
        DynPtrArray srcs;
 
+       // Index in sccs array of SCC this object was folded into
        int scc_index;
 } HashEntry;
 
@@ -95,13 +70,19 @@ typedef struct {
        double weight;
 } HashEntryWithAccounting;
 
+// The graph of managed objects/HashEntries is reduced to a graph of strongly connected components
 typedef struct _SCC {
        int index;
        int api_index;
+
+       // How many bridged objects does this SCC hold references to?
        int num_bridge_entries;
+
+       // Index in global sccs array of SCCs holding pointers to this SCC
        DynIntArray xrefs;              /* these are incoming, not outgoing */
 } SCC;
 
+// Maps managed objects to corresponding HashEntry stricts
 static SgenHashTable hash_table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE, INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntry), mono_aligned_addr_hash, NULL);
 
 static int current_time;
@@ -147,7 +128,7 @@ dyn_array_ensure_capacity (DynArray *da, int capacity, int elem_size)
        while (capacity > da->capacity)
                da->capacity *= 2;
 
-       new_data = sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+       new_data = (char *)sgen_alloc_internal_dynamic (elem_size * da->capacity, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        memcpy (new_data, da->data, elem_size * da->size);
        sgen_free_internal_dynamic (da->data, elem_size * old_capacity, INTERNAL_MEM_BRIDGE_DATA);
        da->data = new_data;
@@ -193,7 +174,7 @@ dyn_array_int_set_size (DynIntArray *da, int size)
 static void
 dyn_array_int_add (DynIntArray *da, int x)
 {
-       int *p = dyn_array_add (&da->array, sizeof (int));
+       int *p = (int *)dyn_array_add (&da->array, sizeof (int));
        *p = x;
 }
 
@@ -258,7 +239,7 @@ dyn_array_ptr_get (DynPtrArray *da, int x)
 static void
 dyn_array_ptr_add (DynPtrArray *da, void *ptr)
 {
-       void **p = dyn_array_add (&da->array, sizeof (void*));
+       void **p = (void **)dyn_array_add (&da->array, sizeof (void*));
        *p = ptr;
 }
 
@@ -298,7 +279,7 @@ dyn_array_scc_size (DynSCCArray *da)
 static SCC*
 dyn_array_scc_add (DynSCCArray *da)
 {
-       return dyn_array_add (&da->array, sizeof (SCC));
+       return (SCC *)dyn_array_add (&da->array, sizeof (SCC));
 }
 
 static SCC*
@@ -392,11 +373,13 @@ dyn_array_int_merge_one (DynIntArray *array, int value)
 
 
 static void
-enable_accounting (void)
+set_config (const SgenBridgeProcessorConfig *config)
 {
-       SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
-       bridge_accounting_enabled = TRUE;
-       hash_table = table;
+       if (config->accounting) {
+               SgenHashTable table = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_BRIDGE_HASH_TABLE, INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY, sizeof (HashEntryWithAccounting), mono_aligned_addr_hash, NULL);
+               bridge_accounting_enabled = TRUE;
+               hash_table = table;
+       }
 }
 
 static MonoGCBridgeObjectKind
@@ -408,7 +391,7 @@ class_kind (MonoClass *klass)
 static HashEntry*
 get_hash_entry (GCObject *obj, gboolean *existing)
 {
-       HashEntry *entry = sgen_hash_table_lookup (&hash_table, obj);
+       HashEntry *entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
        HashEntry new_entry;
 
        if (entry) {
@@ -428,7 +411,7 @@ get_hash_entry (GCObject *obj, gboolean *existing)
 
        sgen_hash_table_replace (&hash_table, obj, &new_entry, NULL);
 
-       return sgen_hash_table_lookup (&hash_table, obj);
+       return (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
 }
 
 static void
@@ -445,7 +428,7 @@ free_data (void)
        int total_srcs = 0;
        int max_srcs = 0;
 
-       SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
+       SGEN_HASH_TABLE_FOREACH (&hash_table, GCObject *, obj, HashEntry *, entry) {
                int entry_size = dyn_array_ptr_size (&entry->srcs);
                total_srcs += entry_size;
                if (entry_size > max_srcs)
@@ -516,11 +499,11 @@ dfs1 (HashEntry *obj_entry)
                GCObject *obj;
                ++dfs1_passes;
 
-               obj_entry = dyn_array_ptr_pop (&dfs_stack);
+               obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
                if (obj_entry) {
                        char *start;
                        mword desc;
-                       src = dyn_array_ptr_pop (&dfs_stack);
+                       src = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
 
                        obj = obj_entry->obj;
                        desc = sgen_obj_get_descriptor_safe (obj);
@@ -544,7 +527,7 @@ dfs1 (HashEntry *obj_entry)
                        start = (char*)obj;
 #include "sgen/sgen-scan-object.h"
                } else {
-                       obj_entry = dyn_array_ptr_pop (&dfs_stack);
+                       obj_entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
 
                        //g_print ("finish %s\n", sgen_safe_name (obj_entry->obj));
                        register_finishing_time (obj_entry, current_time++);
@@ -592,7 +575,7 @@ dfs2 (HashEntry *entry)
        dyn_array_ptr_push (&dfs_stack, entry);
 
        do {
-               entry = dyn_array_ptr_pop (&dfs_stack);
+               entry = (HashEntry *)dyn_array_ptr_pop (&dfs_stack);
                ++dfs2_passes;
 
                if (entry->scc_index >= 0) {
@@ -616,7 +599,7 @@ compare_hash_entries (const HashEntry *e1, const HashEntry *e2)
 
 DEF_QSORT_INLINE(hash_entries, HashEntry*, compare_hash_entries)
 
-static unsigned long step_1, step_2, step_3, step_4, step_5, step_6;
+static gint64 step_1, step_2, step_3, step_4, step_5, step_6;
 static int fist_pass_links, second_pass_links, sccs_links;
 static int max_sccs_links = 0;
 
@@ -663,10 +646,10 @@ processing_stw_step (void)
        */
        bridge_count = dyn_array_ptr_size (&registered_bridges);
        for (i = 0; i < bridge_count ; ++i)
-               register_bridge_object (dyn_array_ptr_get (&registered_bridges, i));
+               register_bridge_object ((GCObject *)dyn_array_ptr_get (&registered_bridges, i));
 
        for (i = 0; i < bridge_count; ++i)
-               dfs1 (get_hash_entry (dyn_array_ptr_get (&registered_bridges, i), NULL));
+               dfs1 (get_hash_entry ((GCObject *)dyn_array_ptr_get (&registered_bridges, i), NULL));
 
        SGEN_TV_GETTIME (atv);
        step_2 = SGEN_TV_ELAPSED (btv, atv);
@@ -700,10 +683,10 @@ processing_build_callback_data (int generation)
 
        /* alloc and fill array of all entries */
 
-       all_entries = sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+       all_entries = (HashEntry **)sgen_alloc_internal_dynamic (sizeof (HashEntry*) * hash_table.num_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
 
        j = 0;
-       SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
+       SGEN_HASH_TABLE_FOREACH (&hash_table, GCObject *, obj, HashEntry *, entry) {
                g_assert (entry->finishing_time >= 0);
                all_entries [j++] = entry;
                fist_pass_links += dyn_array_ptr_size (&entry->srcs);
@@ -795,7 +778,7 @@ processing_build_callback_data (int generation)
                max_sccs_links = MAX (max_sccs_links, dyn_array_int_size (&scc->xrefs));
        }
 
-       api_sccs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+       api_sccs = (MonoGCBridgeSCC **)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC*) * num_sccs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        num_xrefs = 0;
        j = 0;
        for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
@@ -803,7 +786,7 @@ processing_build_callback_data (int generation)
                if (!scc->num_bridge_entries)
                        continue;
 
-               api_sccs [j] = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+               api_sccs [j] = (MonoGCBridgeSCC *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeSCC) + sizeof (MonoObject*) * scc->num_bridge_entries, INTERNAL_MEM_BRIDGE_DATA, TRUE);
                api_sccs [j]->is_alive = FALSE;
                api_sccs [j]->num_objs = scc->num_bridge_entries;
                scc->num_bridge_entries = 0;
@@ -812,14 +795,14 @@ processing_build_callback_data (int generation)
                num_xrefs += dyn_array_int_size (&scc->xrefs);
        }
 
-       SGEN_HASH_TABLE_FOREACH (&hash_table, obj, entry) {
+       SGEN_HASH_TABLE_FOREACH (&hash_table, GCObject *, obj, HashEntry *, entry) {
                if (entry->is_bridge) {
                        SCC *scc = dyn_array_scc_get_ptr (&sccs, entry->scc_index);
                        api_sccs [scc->api_index]->objs [scc->num_bridge_entries++] = (MonoObject*)entry->obj;
                }
        } SGEN_HASH_TABLE_FOREACH_END;
 
-       api_xrefs = sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
+       api_xrefs = (MonoGCBridgeXRef *)sgen_alloc_internal_dynamic (sizeof (MonoGCBridgeXRef) * num_xrefs, INTERNAL_MEM_BRIDGE_DATA, TRUE);
        j = 0;
        for (i = 0; i < dyn_array_scc_size (&sccs); ++i) {
                int k;
@@ -923,7 +906,7 @@ describe_pointer (GCObject *obj)
                }
        }
 
-       entry = sgen_hash_table_lookup (&hash_table, obj);
+       entry = (HashEntry *)sgen_hash_table_lookup (&hash_table, obj);
        if (!entry)
                return;
 
@@ -942,7 +925,7 @@ sgen_old_bridge_init (SgenBridgeProcessor *collector)
        collector->class_kind = class_kind;
        collector->register_finalized_object = register_finalized_object;
        collector->describe_pointer = describe_pointer;
-       collector->enable_accounting = enable_accounting;
+       collector->set_config = set_config;
 
        bridge_processor = collector;
 }