Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mono / metadata / sgen-copy-object.h
index 7526a3f836309b9d0e1389d6418141892bf1052b..76a00e281ac167469aa0d29a5e5376d791058913 100644 (file)
@@ -1,25 +1,22 @@
 /*
+ * sgen-copy-object.h: This is where objects are copied.
+ *
  * 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.
+ * 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.
  */
 extern long long stat_copy_object_called_nursery;
 extern long long stat_objects_copied_nursery;
@@ -37,12 +34,21 @@ extern long long stat_slots_allocated_in_vain;
 static inline void
 par_copy_object_no_checks (char *destination, MonoVTable *vt, void *obj, mword objsize, SgenGrayQueue *queue)
 {
+#ifdef __GNUC__
        static const void *copy_labels [] = { &&LAB_0, &&LAB_1, &&LAB_2, &&LAB_3, &&LAB_4, &&LAB_5, &&LAB_6, &&LAB_7, &&LAB_8 };
+#endif
 
-       DEBUG (9, g_assert (vt->klass->inited));
-       DEBUG (9, fprintf (gc_debug_file, " (to %p, %s size: %lu)\n", destination, ((MonoObject*)obj)->vtable->klass->name, (unsigned long)objsize));
+       SGEN_ASSERT (9, vt->klass->inited, "vtable %p for class %s:%s was not initialized", vt, vt->klass->name_space, vt->klass->name);
+       SGEN_LOG (9, " (to %p, %s size: %lu)", destination, ((MonoObject*)obj)->vtable->klass->name, (unsigned long)objsize);
        binary_protocol_copy (obj, destination, vt, objsize);
 
+       if (G_UNLIKELY (MONO_GC_OBJ_MOVED_ENABLED ())) {
+               int dest_gen = sgen_ptr_in_nursery (destination) ? GENERATION_NURSERY : GENERATION_OLD;
+               int src_gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
+               MONO_GC_OBJ_MOVED ((mword)destination, (mword)obj, dest_gen, src_gen, objsize, vt->klass->name_space, vt->klass->name);
+       }
+
+#ifdef __GNUC__
        if (objsize <= sizeof (gpointer) * 8) {
                mword *dest = (mword*)destination;
                goto *copy_labels [objsize / sizeof (gpointer)];
@@ -68,29 +74,40 @@ par_copy_object_no_checks (char *destination, MonoVTable *vt, void *obj, mword o
                /*can't trust memcpy doing word copies */
                mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
        }
+#else
+               mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
+#endif
        /* adjust array->bounds */
-       DEBUG (9, g_assert (vt->gc_descr));
+       SGEN_ASSERT (9, vt->gc_descr, "vtable %p for class %s:%s has no gc descriptor", vt, vt->klass->name_space, vt->klass->name);
+
        if (G_UNLIKELY (vt->rank && ((MonoArray*)obj)->bounds)) {
                MonoArray *array = (MonoArray*)destination;
                array->bounds = (MonoArrayBounds*)((char*)destination + ((char*)((MonoArray*)obj)->bounds - (char*)obj));
-               DEBUG (9, fprintf (gc_debug_file, "Array instance %p: size: %lu, rank: %d, length: %lu\n", array, (unsigned long)objsize, vt->rank, (unsigned long)mono_array_length (array)));
+               SGEN_LOG (9, "Array instance %p: size: %lu, rank: %d, length: %lu", array, (unsigned long)objsize, vt->rank, (unsigned long)mono_array_length (array));
        }
        if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
                sgen_register_moved_object (obj, destination);
        obj = destination;
        if (queue) {
-               DEBUG (9, fprintf (gc_debug_file, "Enqueuing gray object %p (%s)\n", obj, sgen_safe_name (obj)));
+               SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_safe_name (obj));
                GRAY_OBJECT_ENQUEUE (queue, obj);
        }
 }
 
+/*
+ * This can return OBJ itself on OOM.
+ */
+#ifdef _MSC_VER
+static __declspec(noinline) void*
+#else
 static G_GNUC_UNUSED void* __attribute__((noinline))
+#endif
 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
 {
        MonoVTable *vt = ((MonoObject*)obj)->vtable;
        gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
        mword objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
-       char *destination = collector_serial_alloc_for_promotion (obj, objsize, has_references);
+       char *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (obj, objsize, has_references);
 
        if (G_UNLIKELY (!destination)) {
                collector_pin_object (obj, queue);
@@ -106,172 +123,3 @@ copy_object_no_checks (void *obj, SgenGrayQueue *queue)
 
        return destination;
 }
-
-#ifdef GENERATE_COPY_FUNCTIONS
-
-extern long long stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
-
-#if defined(SGEN_SIMPLE_NURSERY)
-#define serial_copy_object simple_nursery_serial_copy_object
-#define parallel_copy_object simple_nursery_parallel_copy_object
-
-#elif defined (SGEN_SPLIT_NURSERY)
-
-#define serial_copy_object split_nursery_serial_copy_object
-#define parallel_copy_object split_nursery_parallel_copy_object
-
-#else
-#error "Please define GC_CONF_NAME"
-#endif
-
-/*
- * This is how the copying happens from the nursery to the old generation.
- * We assume that at this time all the pinned objects have been identified and
- * marked as such.
- * We run scan_object() for each pinned object so that each referenced
- * objects if possible are copied. The new gray objects created can have
- * scan_object() run on them right away, too.
- * Then we run copy_object() for the precisely tracked roots. At this point
- * all the roots are either gray or black. We run scan_object() on the gray
- * objects until no more gray objects are created.
- * At the end of the process we walk again the pinned list and we unmark
- * the pinned flag. As we go we also create the list of free space for use
- * in the next allocation runs.
- *
- * We need to remember objects from the old generation that point to the new one
- * (or just addresses?).
- *
- * copy_object could be made into a macro once debugged (use inline for now).
- */
-
-static inline void __attribute__((always_inline))
-serial_copy_object (void **obj_slot, SgenGrayQueue *queue) 
-{
-       char *forwarded;
-       char *obj = *obj_slot;
-
-       DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
-
-       HEAVY_STAT (++stat_copy_object_called_nursery);
-
-       if (!sgen_ptr_in_nursery (obj)) {
-               HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
-               return;
-       }
-
-       DEBUG (9, fprintf (gc_debug_file, "Precise copy of %p from %p", obj, obj_slot));
-
-       /*
-        * Before we can copy the object we must make sure that we are
-        * allowed to, i.e. that the object not pinned, not already
-        * forwarded or belongs to the nursery To Space.
-        */
-
-       if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
-               DEBUG (9, g_assert ((*(MonoVTable**)SGEN_LOAD_VTABLE(obj))->gc_descr));
-               DEBUG (9, fprintf (gc_debug_file, " (already forwarded to %p)\n", forwarded));
-               HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
-               *obj_slot = forwarded;
-               return;
-       }
-       if (SGEN_OBJECT_IS_PINNED (obj)) {
-               DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
-               DEBUG (9, fprintf (gc_debug_file, " (pinned, no change)\n"));
-               HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
-               return;
-       }
-
-       if (sgen_nursery_is_to_space (obj)) {
-               DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
-               DEBUG (9, fprintf (gc_debug_file, " (tospace, no change)\n"));
-               HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
-               return;
-       }
-
-       HEAVY_STAT (++stat_objects_copied_nursery);
-
-       *obj_slot = copy_object_no_checks (obj, queue);
-}
-
-static void
-parallel_copy_object (void **obj_slot, SgenGrayQueue *queue)
-{
-       char *obj = *obj_slot;
-       mword vtable_word, objsize;
-       MonoVTable *vt;
-       void *destination;
-       gboolean has_references;
-
-       DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
-
-       HEAVY_STAT (++stat_copy_object_called_nursery);
-
-       if (!sgen_ptr_in_nursery (obj)) {
-               HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
-               return;
-       }
-
-       vtable_word = *(mword*)obj;
-       vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
-
-       /*
-        * Before we can copy the object we must make sure that we are
-        * allowed to, i.e. that the object not pinned, not already
-        * forwarded and not in the nursery To Space.
-        */
-
-       if (vtable_word & SGEN_FORWARDED_BIT) {
-               HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
-               *obj_slot = vt;
-               return;
-       }
-       if (vtable_word & SGEN_PINNED_BIT) {
-               HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
-               return;
-       }
-
-       if (sgen_nursery_is_to_space (obj)) {
-               HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
-               return;
-       }
-
-       HEAVY_STAT (++stat_objects_copied_nursery);
-
-       objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
-       has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
-
-       destination = collector_parallel_alloc_for_promotion (obj, objsize, has_references);
-
-       if (G_UNLIKELY (!destination)) {
-               sgen_parallel_pin_or_update (obj_slot, obj, vt, queue);
-               return;
-       }
-
-       *(MonoVTable**)destination = vt;
-
-       if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
-               par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
-               obj = destination;
-               *obj_slot = obj;
-       } else {
-               /* FIXME: unify with code in major_copy_or_mark_object() */
-
-               /* FIXME: Give destination back to the allocator. */
-               /*The major collector only needs the first word zeroed and nursery requires all bits to be. */
-               if (!sgen_ptr_in_nursery (destination))
-                       *(void**)destination = NULL;
-               else
-                       memset (destination, 0, objsize);
-
-               vtable_word = *(mword*)obj;
-               g_assert (vtable_word & SGEN_FORWARDED_BIT);
-
-               obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
-
-               *obj_slot = obj;
-
-               HEAVY_STAT (++stat_slots_allocated_in_vain);
-       }
-}
-
-#endif