3 * Copy functions for nursery collections.
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright (C) 2012 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #undef SERIAL_COPY_OBJECT
13 #undef SERIAL_COPY_OBJECT_FROM_OBJ
15 #if defined(SGEN_SIMPLE_NURSERY)
17 #ifdef SGEN_SIMPLE_PAR_NURSERY
18 /* Not supported with concurrent major yet */
19 #define SERIAL_COPY_OBJECT simple_par_nursery_copy_object
20 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_copy_object_from_obj
22 #ifdef SGEN_CONCURRENT_MAJOR
23 #define SERIAL_COPY_OBJECT simple_nursery_serial_with_concurrent_major_copy_object
24 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_with_concurrent_major_copy_object_from_obj
26 #define SERIAL_COPY_OBJECT simple_nursery_serial_copy_object
27 #define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_copy_object_from_obj
31 #elif defined (SGEN_SPLIT_NURSERY)
33 #ifdef SGEN_CONCURRENT_MAJOR
34 #define SERIAL_COPY_OBJECT split_nursery_serial_with_concurrent_major_copy_object
35 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_with_concurrent_major_copy_object_from_obj
37 #define SERIAL_COPY_OBJECT split_nursery_serial_copy_object
38 #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_copy_object_from_obj
42 #error "No nursery configuration specified"
46 extern guint64 stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
49 * This is how the copying happens from the nursery to the old generation.
50 * We assume that at this time all the pinned objects have been identified and
52 * We run scan_object() for each pinned object so that each referenced
53 * objects if possible are copied. The new gray objects created can have
54 * scan_object() run on them right away, too.
55 * Then we run copy_object() for the precisely tracked roots. At this point
56 * all the roots are either gray or black. We run scan_object() on the gray
57 * objects until no more gray objects are created.
58 * At the end of the process we walk again the pinned list and we unmark
59 * the pinned flag. As we go we also create the list of free space for use
60 * in the next allocation runs.
62 * We need to remember objects from the old generation that point to the new one
63 * (or just addresses?).
65 * copy_object could be made into a macro once debugged (use inline for now).
68 static MONO_ALWAYS_INLINE void
69 SERIAL_COPY_OBJECT (GCObject **obj_slot, SgenGrayQueue *queue)
73 GCObject *obj = *obj_slot;
75 SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
77 HEAVY_STAT (++stat_copy_object_called_nursery);
79 if (!sgen_ptr_in_nursery (obj)) {
80 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
84 SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
87 * Before we can copy the object we must make sure that we are
88 * allowed to, i.e. that the object not pinned, not already
89 * forwarded or belongs to the nursery To Space.
92 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
93 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded), "forwarded object %p has no gc descriptor", forwarded);
94 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
95 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
96 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
99 if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
100 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
101 SGEN_LOG (9, " (pinned, no change)");
102 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
106 #ifndef SGEN_SIMPLE_NURSERY
107 if (sgen_nursery_is_to_space (obj)) {
108 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
109 SGEN_LOG (9, " (tospace, no change)");
110 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);
115 HEAVY_STAT (++stat_objects_copied_nursery);
117 #ifdef SGEN_SIMPLE_PAR_NURSERY
118 copy = copy_object_no_checks_par (obj, queue);
120 copy = copy_object_no_checks (obj, queue);
122 SGEN_UPDATE_REFERENCE (obj_slot, copy);
126 * SERIAL_COPY_OBJECT_FROM_OBJ:
128 * Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
130 static MONO_ALWAYS_INLINE void
131 SERIAL_COPY_OBJECT_FROM_OBJ (GCObject **obj_slot, SgenGrayQueue *queue)
134 GCObject *obj = *obj_slot;
137 SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
139 HEAVY_STAT (++stat_copy_object_called_nursery);
141 if (!sgen_ptr_in_nursery (obj)) {
142 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
146 SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
149 * Before we can copy the object we must make sure that we are
150 * allowed to, i.e. that the object not pinned, not already
151 * forwarded or belongs to the nursery To Space.
154 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
155 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded), "forwarded object %p has no gc descriptor", forwarded);
156 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
157 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
158 #ifdef SGEN_CONCURRENT_MAJOR
159 /* See comment on STORE_STORE_FENCE below. */
162 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
163 #ifndef SGEN_SIMPLE_NURSERY
164 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
165 sgen_add_to_global_remset (obj_slot, forwarded);
169 if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
170 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
171 SGEN_LOG (9, " (pinned, no change)");
172 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
173 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
174 sgen_add_to_global_remset (obj_slot, obj);
178 #ifndef SGEN_SIMPLE_NURSERY
179 if (sgen_nursery_is_to_space (obj)) {
180 /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
181 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
182 SGEN_LOG (9, " (tospace, no change)");
183 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);
188 * The card table scanning code sometimes clears cards
189 * that have just been set for a global remset. In
190 * the split nursery the following situation can
193 * Let's say object A starts in card C but continues
194 * into C+1. Within A, at offset O there's a
195 * reference to a new nursery object X. A+O is in
196 * card C+1. Now card C is scanned, and as part of
197 * it, object A. The reference at A+O is processed by
198 * copying X into nursery to-space at Y. Since it's
199 * still in the nursery, a global remset must be added
200 * for A+O, so card C+1 is marked. Now, however, card
201 * C+1 is scanned, which means that it's cleared
202 * first. This wouldn't be terribly bad if reference
203 * A+O were re-scanned and the global remset re-added,
204 * but since the reference points to to-space, that
205 * doesn't happen, and C+1 remains cleared: the remset
208 * There's at least two ways to fix this. The easy
209 * one is to re-add the remset on the re-scan. This
210 * is that - the following two lines of code.
212 * The proper solution appears to be to first make a
213 * copy of the cards before scanning a block, then to
214 * clear all the cards and scan from the copy, so no
215 * remsets will be overwritten. Scanning objects at
216 * most once would be the icing on the cake.
218 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
219 sgen_add_to_global_remset (obj_slot, obj);
225 HEAVY_STAT (++stat_objects_copied_nursery);
227 #ifdef SGEN_SIMPLE_PAR_NURSERY
228 copy = copy_object_no_checks_par (obj, queue);
230 copy = copy_object_no_checks (obj, queue);
232 #ifdef SGEN_CONCURRENT_MAJOR
234 * If an object is evacuated to the major heap and a reference to it, from the major
235 * heap, updated, the concurrent major collector might follow that reference and
236 * scan the new major object. To make sure the object contents are seen by the
237 * major collector we need this write barrier, so that the reference is seen after
242 SGEN_UPDATE_REFERENCE (obj_slot, copy);
243 #ifndef SGEN_SIMPLE_NURSERY
244 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
245 sgen_add_to_global_remset (obj_slot, copy);
247 /* copy_object_no_checks () can return obj on OOM */
248 if (G_UNLIKELY (obj == copy)) {
249 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
250 sgen_add_to_global_remset (obj_slot, copy);