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