[utils] Use MONO_ALWAYS_INLINE + Add MONO_NEVER_INLINE
[mono.git] / mono / metadata / 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  * 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 #define collector_pin_object(obj, queue) sgen_pin_object (obj, queue);
23 #define COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION alloc_for_promotion
24 #define COLLECTOR_PARALLEL_ALLOC_FOR_PROMOTION par_alloc_for_promotion
25
26 extern long long stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
27
28 #include "mono/utils/mono-compiler.h"
29
30 #include "sgen-copy-object.h"
31
32 /*
33  * This is how the copying happens from the nursery to the old generation.
34  * We assume that at this time all the pinned objects have been identified and
35  * marked as such.
36  * We run scan_object() for each pinned object so that each referenced
37  * objects if possible are copied. The new gray objects created can have
38  * scan_object() run on them right away, too.
39  * Then we run copy_object() for the precisely tracked roots. At this point
40  * all the roots are either gray or black. We run scan_object() on the gray
41  * objects until no more gray objects are created.
42  * At the end of the process we walk again the pinned list and we unmark
43  * the pinned flag. As we go we also create the list of free space for use
44  * in the next allocation runs.
45  *
46  * We need to remember objects from the old generation that point to the new one
47  * (or just addresses?).
48  *
49  * copy_object could be made into a macro once debugged (use inline for now).
50  */
51
52 static MONO_ALWAYS_INLINE void
53 SERIAL_COPY_OBJECT (void **obj_slot, SgenGrayQueue *queue) 
54 {
55         char *forwarded;
56         char *obj = *obj_slot;
57
58         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
59
60         HEAVY_STAT (++stat_copy_object_called_nursery);
61
62         if (!sgen_ptr_in_nursery (obj)) {
63                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
64                 return;
65         }
66
67         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
68
69         /*
70          * Before we can copy the object we must make sure that we are
71          * allowed to, i.e. that the object not pinned, not already
72          * forwarded or belongs to the nursery To Space.
73          */
74
75         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
76                 SGEN_ASSERT (9, (*(MonoVTable**)SGEN_LOAD_VTABLE (obj))->gc_descr,  "forwarded object %p has no gc descriptor", forwarded);
77                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
78                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
79                 *obj_slot = forwarded;
80                 return;
81         }
82         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
83                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "pinned object %p has no gc descriptor", obj);
84                 SGEN_LOG (9, " (pinned, no change)");
85                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
86                 return;
87         }
88
89 #ifndef SGEN_SIMPLE_NURSERY
90         if (sgen_nursery_is_to_space (obj)) {
91                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "to space object %p has no gc descriptor", obj);
92                 SGEN_LOG (9, " (tospace, no change)");
93                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
94                 return;
95         }
96 #endif
97
98         HEAVY_STAT (++stat_objects_copied_nursery);
99
100         *obj_slot = copy_object_no_checks (obj, queue);
101 }
102
103 /*
104  * SERIAL_COPY_OBJECT_FROM_OBJ:
105  *
106  *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
107  */
108 static MONO_ALWAYS_INLINE void
109 SERIAL_COPY_OBJECT_FROM_OBJ (void **obj_slot, SgenGrayQueue *queue) 
110 {
111         char *forwarded;
112         char *obj = *obj_slot;
113         void *copy;
114
115         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
116
117         HEAVY_STAT (++stat_copy_object_called_nursery);
118
119         if (!sgen_ptr_in_nursery (obj)) {
120                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
121                 return;
122         }
123
124         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
125
126         /*
127          * Before we can copy the object we must make sure that we are
128          * allowed to, i.e. that the object not pinned, not already
129          * forwarded or belongs to the nursery To Space.
130          */
131
132         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
133                 SGEN_ASSERT (9, (*(MonoVTable**)SGEN_LOAD_VTABLE (obj))->gc_descr,  "forwarded object %p has no gc descriptor", forwarded);
134                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
135                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
136                 *obj_slot = forwarded;
137 #ifndef SGEN_SIMPLE_NURSERY
138                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot)))
139                         sgen_add_to_global_remset (obj_slot, forwarded);
140 #endif
141                 return;
142         }
143         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
144                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "pinned object %p has no gc descriptor", obj);
145                 SGEN_LOG (9, " (pinned, no change)");
146                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
147                 if (!sgen_ptr_in_nursery (obj_slot))
148                         sgen_add_to_global_remset (obj_slot, obj);
149                 return;
150         }
151
152 #ifndef SGEN_SIMPLE_NURSERY
153         if (sgen_nursery_is_to_space (obj)) {
154                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "to space object %p has no gc descriptor", obj);
155                 SGEN_LOG (9, " (tospace, no change)");
156                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
157
158                 /*
159                  * FIXME:
160                  *
161                  * The card table scanning code sometimes clears cards
162                  * that have just been set for a global remset.  In
163                  * the split nursery the following situation can
164                  * occur:
165                  *
166                  * Let's say object A starts in card C but continues
167                  * into C+1.  Within A, at offset O there's a
168                  * reference to a new nursery object X.  A+O is in
169                  * card C+1.  Now card C is scanned, and as part of
170                  * it, object A.  The reference at A+O is processed by
171                  * copying X into nursery to-space at Y.  Since it's
172                  * still in the nursery, a global remset must be added
173                  * for A+O, so card C+1 is marked.  Now, however, card
174                  * C+1 is scanned, which means that it's cleared
175                  * first.  This wouldn't be terribly bad if reference
176                  * A+O were re-scanned and the global remset re-added,
177                  * but since the reference points to to-space, that
178                  * doesn't happen, and C+1 remains cleared: the remset
179                  * is lost.
180                  *
181                  * There's at least two ways to fix this.  The easy
182                  * one is to re-add the remset on the re-scan.  This
183                  * is that - the following two lines of code.
184                  *
185                  * The proper solution appears to be to first make a
186                  * copy of the cards before scanning a block, then to
187                  * clear all the cards and scan from the copy, so no
188                  * remsets will be overwritten.  Scanning objects at
189                  * most once would be the icing on the cake.
190                  */
191                 if (!sgen_ptr_in_nursery (obj_slot))
192                         sgen_add_to_global_remset (obj_slot, obj);
193
194                 return;
195         }
196 #endif
197
198         HEAVY_STAT (++stat_objects_copied_nursery);
199
200         copy = copy_object_no_checks (obj, queue);
201         *obj_slot = copy;
202 #ifndef SGEN_SIMPLE_NURSERY
203         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot)))
204                 sgen_add_to_global_remset (obj_slot, copy);
205 #else
206         /* copy_object_no_checks () can return obj on OOM */
207         if (G_UNLIKELY (obj == copy)) {
208                 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot)))
209                         sgen_add_to_global_remset (obj_slot, copy);
210         }
211 #endif
212 }
213
214 static void
215 PARALLEL_COPY_OBJECT (void **obj_slot, SgenGrayQueue *queue)
216 {
217         char *obj = *obj_slot;
218         mword vtable_word, objsize;
219         MonoVTable *vt;
220         void *destination;
221         gboolean has_references;
222
223         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-par-copy from a %d generation collection", current_collection_generation);
224
225         HEAVY_STAT (++stat_copy_object_called_nursery);
226
227         if (!sgen_ptr_in_nursery (obj)) {
228                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
229                 return;
230         }
231
232         vtable_word = *(mword*)obj;
233         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
234
235         /*
236          * Before we can copy the object we must make sure that we are
237          * allowed to, i.e. that the object not pinned, not already
238          * forwarded and not in the nursery To Space.
239          */
240
241         if (vtable_word & SGEN_FORWARDED_BIT) {
242                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
243                 *obj_slot = vt;
244                 return;
245         }
246         if (vtable_word & SGEN_PINNED_BIT) {
247                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
248                 return;
249         }
250
251         if (sgen_nursery_is_to_space (obj)) {
252                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
253                 return;
254         }
255
256         HEAVY_STAT (++stat_objects_copied_nursery);
257
258         objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
259         has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
260
261         destination = COLLECTOR_PARALLEL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
262
263         if (G_UNLIKELY (!destination)) {
264                 sgen_parallel_pin_or_update (obj_slot, obj, vt, queue);
265                 return;
266         }
267
268         *(MonoVTable**)destination = vt;
269
270         if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
271                 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
272                 obj = destination;
273                 *obj_slot = obj;
274         } else {
275                 /* FIXME: unify with code in major_copy_or_mark_object() */
276
277                 /* FIXME: Give destination back to the allocator. */
278                 /*The major collector only needs the first word zeroed and nursery requires all bits to be. */
279                 if (!sgen_ptr_in_nursery (destination))
280                         *(void**)destination = NULL;
281                 else
282                         memset (destination, 0, objsize);
283
284                 vtable_word = *(mword*)obj;
285                 g_assert (vtable_word & SGEN_FORWARDED_BIT);
286
287                 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
288
289                 *obj_slot = obj;
290
291                 HEAVY_STAT (++stat_slots_allocated_in_vain);
292         }
293 }
294
295 #define FILL_MINOR_COLLECTOR_COPY_OBJECT(collector)     do {                    \
296                 (collector)->serial_ops.copy_or_mark_object = SERIAL_COPY_OBJECT;                       \
297                 (collector)->parallel_ops.copy_or_mark_object = PARALLEL_COPY_OBJECT;   \
298         } while (0)