Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / sgen / sgen-minor-copy-object.h
1 /**
2  * \file
3  * Copy functions for nursery collections.
4  *
5  * Copyright 2001-2003 Ximian, Inc
6  * Copyright 2003-2010 Novell, Inc.
7  * Copyright (C) 2012 Xamarin Inc
8  *
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11
12 #undef SERIAL_COPY_OBJECT
13 #undef SERIAL_COPY_OBJECT_FROM_OBJ
14
15 #if defined(SGEN_SIMPLE_NURSERY)
16
17 #ifdef SGEN_SIMPLE_PAR_NURSERY
18
19 #ifdef SGEN_CONCURRENT_MAJOR
20 #define SERIAL_COPY_OBJECT simple_par_nursery_with_concurrent_major_copy_object
21 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_with_concurrent_major_copy_object_from_obj
22 #else
23 #define SERIAL_COPY_OBJECT simple_par_nursery_copy_object
24 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_copy_object_from_obj
25 #endif
26
27 #else
28
29 #ifdef SGEN_CONCURRENT_MAJOR
30 #define SERIAL_COPY_OBJECT simple_nursery_serial_with_concurrent_major_copy_object
31 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_with_concurrent_major_copy_object_from_obj
32 #else
33 #define SERIAL_COPY_OBJECT simple_nursery_serial_copy_object
34 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_copy_object_from_obj
35 #endif
36
37 #endif
38
39 #elif defined (SGEN_SPLIT_NURSERY)
40
41 #ifdef SGEN_CONCURRENT_MAJOR
42 #define SERIAL_COPY_OBJECT split_nursery_serial_with_concurrent_major_copy_object
43 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_with_concurrent_major_copy_object_from_obj
44 #else
45 #define SERIAL_COPY_OBJECT split_nursery_serial_copy_object
46 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_copy_object_from_obj
47 #endif
48
49 #else
50 #error "No nursery configuration specified"
51 #endif
52
53
54 extern guint64 stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
55
56 /*
57  * This is how the copying happens from the nursery to the old generation.
58  * We assume that at this time all the pinned objects have been identified and
59  * marked as such.
60  * We run scan_object() for each pinned object so that each referenced
61  * objects if possible are copied. The new gray objects created can have
62  * scan_object() run on them right away, too.
63  * Then we run copy_object() for the precisely tracked roots. At this point
64  * all the roots are either gray or black. We run scan_object() on the gray
65  * objects until no more gray objects are created.
66  * At the end of the process we walk again the pinned list and we unmark
67  * the pinned flag. As we go we also create the list of free space for use
68  * in the next allocation runs.
69  *
70  * We need to remember objects from the old generation that point to the new one
71  * (or just addresses?).
72  *
73  * copy_object could be made into a macro once debugged (use inline for now).
74  */
75
76 static MONO_ALWAYS_INLINE void
77 SERIAL_COPY_OBJECT (GCObject **obj_slot, SgenGrayQueue *queue) 
78 {
79         GCObject *forwarded;
80         GCObject *copy;
81         GCObject *obj = *obj_slot;
82
83         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
84
85         HEAVY_STAT (++stat_copy_object_called_nursery);
86
87         if (!sgen_ptr_in_nursery (obj)) {
88                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
89                 return;
90         }
91
92         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
93
94         /*
95          * Before we can copy the object we must make sure that we are
96          * allowed to, i.e. that the object not pinned, not already
97          * forwarded or belongs to the nursery To Space.
98          */
99
100         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
101                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
102                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
103                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
104                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
105                 return;
106         }
107         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
108                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
109                 SGEN_LOG (9, " (pinned, no change)");
110                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
111                 return;
112         }
113
114 #ifndef SGEN_SIMPLE_NURSERY
115         if (sgen_nursery_is_to_space (obj)) {
116                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
117                 SGEN_LOG (9, " (tospace, no change)");
118                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
119                 return;
120         }
121 #endif
122
123         HEAVY_STAT (++stat_objects_copied_nursery);
124
125 #ifdef SGEN_SIMPLE_PAR_NURSERY
126         copy = copy_object_no_checks_par (obj, queue);
127 #else
128         copy = copy_object_no_checks (obj, queue);
129 #endif
130         SGEN_UPDATE_REFERENCE (obj_slot, copy);
131 }
132
133 /*
134  * SERIAL_COPY_OBJECT_FROM_OBJ:
135  *
136  *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
137  */
138 static MONO_ALWAYS_INLINE void
139 SERIAL_COPY_OBJECT_FROM_OBJ (GCObject **obj_slot, SgenGrayQueue *queue)
140 {
141         GCObject *forwarded;
142         GCObject *obj = *obj_slot;
143         GCObject *copy;
144
145         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
146
147         HEAVY_STAT (++stat_copy_object_called_nursery);
148
149         if (!sgen_ptr_in_nursery (obj)) {
150                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
151                 return;
152         }
153
154         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
155
156         /*
157          * Before we can copy the object we must make sure that we are
158          * allowed to, i.e. that the object not pinned, not already
159          * forwarded or belongs to the nursery To Space.
160          */
161
162         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
163                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
164                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
165                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
166 #ifdef SGEN_CONCURRENT_MAJOR
167                 /* See comment on STORE_STORE_FENCE below. */
168                 STORE_STORE_FENCE;
169 #endif
170                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
171 #ifndef SGEN_SIMPLE_NURSERY
172                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
173                         sgen_add_to_global_remset (obj_slot, forwarded);
174 #endif
175                 return;
176         }
177         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
178                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
179                 SGEN_LOG (9, " (pinned, no change)");
180                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
181                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
182                         sgen_add_to_global_remset (obj_slot, obj);
183                 return;
184         }
185
186 #ifndef SGEN_SIMPLE_NURSERY
187         if (sgen_nursery_is_to_space (obj)) {
188                 /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
189                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
190                 SGEN_LOG (9, " (tospace, no change)");
191                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
192
193                 /*
194                  * FIXME:
195                  *
196                  * The card table scanning code sometimes clears cards
197                  * that have just been set for a global remset.  In
198                  * the split nursery the following situation can
199                  * occur:
200                  *
201                  * Let's say object A starts in card C but continues
202                  * into C+1.  Within A, at offset O there's a
203                  * reference to a new nursery object X.  A+O is in
204                  * card C+1.  Now card C is scanned, and as part of
205                  * it, object A.  The reference at A+O is processed by
206                  * copying X into nursery to-space at Y.  Since it's
207                  * still in the nursery, a global remset must be added
208                  * for A+O, so card C+1 is marked.  Now, however, card
209                  * C+1 is scanned, which means that it's cleared
210                  * first.  This wouldn't be terribly bad if reference
211                  * A+O were re-scanned and the global remset re-added,
212                  * but since the reference points to to-space, that
213                  * doesn't happen, and C+1 remains cleared: the remset
214                  * is lost.
215                  *
216                  * There's at least two ways to fix this.  The easy
217                  * one is to re-add the remset on the re-scan.  This
218                  * is that - the following two lines of code.
219                  *
220                  * The proper solution appears to be to first make a
221                  * copy of the cards before scanning a block, then to
222                  * clear all the cards and scan from the copy, so no
223                  * remsets will be overwritten.  Scanning objects at
224                  * most once would be the icing on the cake.
225                  */
226                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
227                         sgen_add_to_global_remset (obj_slot, obj);
228
229                 return;
230         }
231 #endif
232
233         HEAVY_STAT (++stat_objects_copied_nursery);
234
235 #ifdef SGEN_SIMPLE_PAR_NURSERY
236         copy = copy_object_no_checks_par (obj, queue);
237 #else
238         copy = copy_object_no_checks (obj, queue);
239 #endif
240 #ifdef SGEN_CONCURRENT_MAJOR
241         /*
242          * If an object is evacuated to the major heap and a reference to it, from the major
243          * heap, updated, the concurrent major collector might follow that reference and
244          * scan the new major object.  To make sure the object contents are seen by the
245          * major collector we need this write barrier, so that the reference is seen after
246          * the object.
247          */
248         STORE_STORE_FENCE;
249 #endif
250         SGEN_UPDATE_REFERENCE (obj_slot, copy);
251 #ifndef SGEN_SIMPLE_NURSERY
252         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
253                 sgen_add_to_global_remset (obj_slot, copy);
254 #else
255         /* copy_object_no_checks () can return obj on OOM */
256         if (G_UNLIKELY (obj == copy)) {
257                 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
258                         sgen_add_to_global_remset (obj_slot, copy);
259         }
260 #endif
261 }