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