2b7bc60670a3751b20566b5c35f100d1b0d8ecf8
[mono.git] / mono / sgen / sgen-copy-object.h
1 /*
2  * sgen-copy-object.h: This is where objects are copied.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 extern guint64 stat_copy_object_called_nursery;
23 extern guint64 stat_objects_copied_nursery;
24
25 extern guint64 stat_nursery_copy_object_failed_from_space;
26 extern guint64 stat_nursery_copy_object_failed_forwarded;
27 extern guint64 stat_nursery_copy_object_failed_pinned;
28
29 extern guint64 stat_slots_allocated_in_vain;
30
31 /*
32  * Copies an object and enqueues it if a queue is given.
33  *
34  * This function can be used even if the vtable of obj is not valid
35  * anymore, which is the case in the parallel collector.
36  */
37 static MONO_ALWAYS_INLINE void
38 par_copy_object_no_checks (char *destination, GCVTable vt, void *obj, mword objsize, SgenGrayQueue *queue)
39 {
40         sgen_client_pre_copy_checks (destination, vt, obj, objsize);
41         binary_protocol_copy (obj, destination, vt, objsize);
42
43         /* FIXME: assumes object layout */
44         memcpy ((char*)destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
45
46         /* adjust array->bounds */
47         SGEN_ASSERT (9, sgen_vtable_get_descriptor (vt), "vtable %p has no gc descriptor", vt);
48
49         sgen_client_update_copied_object (destination, vt, obj, objsize);
50         obj = destination;
51         if (queue) {
52                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_client_vtable_get_name (vt));
53                 GRAY_OBJECT_ENQUEUE (queue, (GCObject *)obj, sgen_vtable_get_descriptor (vt));
54         }
55 }
56
57 /*
58  * This can return OBJ itself on OOM.
59  */
60 static MONO_NEVER_INLINE GCObject *
61 copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
62 {
63         GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
64         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
65         mword objsize = SGEN_ALIGN_UP (sgen_client_par_object_get_size (vt, obj));
66         /* FIXME: Does this not mark the newly allocated object? */
67         void *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
68
69         if (G_UNLIKELY (!destination)) {
70                 /* FIXME: Is this path ever tested? */
71                 collector_pin_object (obj, queue);
72                 sgen_set_pinned_from_failed_allocation (objsize);
73                 return obj;
74         }
75
76         if (!has_references)
77                 queue = NULL;
78
79         par_copy_object_no_checks ((char *)destination, vt, obj, objsize, queue);
80         /* FIXME: mark mod union cards if necessary */
81
82         /* set the forwarding pointer */
83         SGEN_FORWARD_OBJECT (obj, destination);
84
85         return (GCObject *)destination;
86 }