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