Merge pull request #3806 from BrzVlad/feature-parallel-gc-final
[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  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 /*
12  * Defines
13  *
14  *     GCObject* copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
15  *
16  * which allocates new space for `obj`, copies it there, forwards `obj` to its new location,
17  * and enqueues the copy into `queue`.
18  *
19  * To be defined by the includer:
20  *
21  *     COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION(vt, obj, objsize, has_refs)
22  *
23  * Allocates space for promoting object `obj`, with size `objsize`, and initizializes the
24  * vtable with `vt`.  `has_refs` indicates whether the object contains references.
25  *
26  *     collector_pin_object(obj, queue)
27  *
28  * Called when no space for `obj` could be allocated.  It must pin `obj` and enqueue it into
29  * `queue` for scanning.
30  */
31
32 extern guint64 stat_copy_object_called_nursery;
33 extern guint64 stat_objects_copied_nursery;
34
35 extern guint64 stat_nursery_copy_object_failed_from_space;
36 extern guint64 stat_nursery_copy_object_failed_forwarded;
37 extern guint64 stat_nursery_copy_object_failed_pinned;
38
39 extern guint64 stat_slots_allocated_in_vain;
40
41 /*
42  * This function can be used even if the vtable of obj is not valid
43  * anymore, which is the case in the parallel collector.
44  */
45 static MONO_ALWAYS_INLINE void
46 par_copy_object_no_checks (char *destination, GCVTable vt, void *obj, mword objsize)
47 {
48         sgen_client_pre_copy_checks (destination, vt, obj, objsize);
49         binary_protocol_copy (obj, destination, vt, objsize);
50
51         /* FIXME: assumes object layout */
52         memcpy ((char*)destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
53
54         /* adjust array->bounds */
55         SGEN_ASSERT (9, sgen_vtable_get_descriptor (vt), "vtable %p has no gc descriptor", vt);
56
57         sgen_client_update_copied_object (destination, vt, obj, objsize);
58 }
59
60 /*
61  * Copies an object and enqueues it if a queue is given.
62  * This can return OBJ itself on OOM.
63  */
64 static MONO_NEVER_INLINE GCObject *
65 copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
66 {
67         GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
68         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
69         mword objsize = SGEN_ALIGN_UP (sgen_client_par_object_get_size (vt, obj));
70         void *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
71
72         if (G_UNLIKELY (!destination)) {
73                 /* FIXME: Is this path ever tested? */
74                 collector_pin_object (obj, queue);
75                 sgen_set_pinned_from_failed_allocation (objsize);
76                 return obj;
77         }
78
79         par_copy_object_no_checks ((char *)destination, vt, obj, objsize);
80
81         /* set the forwarding pointer */
82         SGEN_FORWARD_OBJECT (obj, destination);
83
84         if (has_references) {
85                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", destination, sgen_client_vtable_get_name (vt));
86                 GRAY_OBJECT_ENQUEUE_SERIAL (queue, (GCObject *)destination, sgen_vtable_get_descriptor (vt));
87         }
88
89         return (GCObject *)destination;
90 }
91
92 #if defined(COPY_OR_MARK_PARALLEL)
93 static MONO_NEVER_INLINE GCObject *
94 copy_object_no_checks_par (GCObject *obj, SgenGrayQueue *queue)
95 {
96         mword vtable_word = *(mword*)obj;
97         GCObject *destination;
98
99         destination = (GCObject*) SGEN_VTABLE_IS_FORWARDED (vtable_word);
100
101         if (!destination) {
102                 GCVTable vt = (GCVTable) vtable_word;
103                 GCObject *final_destination;
104                 /*
105                  * At this point we know vt is not tagged and we shouldn't access the vtable through obj
106                  * since it could get copied at any time by another thread.
107                  */
108                 gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
109                 mword objsize = SGEN_ALIGN_UP (sgen_client_par_object_get_size (vt, obj));
110                 destination = major_collector.alloc_object_par (vt, objsize, has_references);
111
112                 par_copy_object_no_checks ((char*)destination, vt, obj, objsize);
113
114                 /* FIXME we might need a membar here so other threads see the vtable before we forward */
115
116                 /* set the forwarding pointer */
117                 SGEN_FORWARD_OBJECT_PAR (obj, destination, final_destination);
118
119                 if (destination == final_destination) {
120                         /* In a racing case, only the worker that allocated the object enqueues it */
121                         if (has_references) {
122                                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", destination, sgen_client_vtable_get_name (vt));
123                                 GRAY_OBJECT_ENQUEUE_PARALLEL (queue, (GCObject *)destination, sgen_vtable_get_descriptor (vt));
124                         }
125                 } else {
126                         destination = final_destination;
127                 }
128         }
129
130         return destination;
131 }
132 #endif
133
134 #undef COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION
135 #undef collector_pin_object
136 #undef COPY_OR_MARK_PARALLEL