First set of licensing changes
[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                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
125 #ifndef SGEN_SIMPLE_NURSERY
126                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
127                         sgen_add_to_global_remset (obj_slot, forwarded);
128 #endif
129                 return;
130         }
131         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
132                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
133                 SGEN_LOG (9, " (pinned, no change)");
134                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
135                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
136                         sgen_add_to_global_remset (obj_slot, obj);
137                 return;
138         }
139
140 #ifndef SGEN_SIMPLE_NURSERY
141         if (sgen_nursery_is_to_space (obj)) {
142                 /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
143                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
144                 SGEN_LOG (9, " (tospace, no change)");
145                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
146
147                 /*
148                  * FIXME:
149                  *
150                  * The card table scanning code sometimes clears cards
151                  * that have just been set for a global remset.  In
152                  * the split nursery the following situation can
153                  * occur:
154                  *
155                  * Let's say object A starts in card C but continues
156                  * into C+1.  Within A, at offset O there's a
157                  * reference to a new nursery object X.  A+O is in
158                  * card C+1.  Now card C is scanned, and as part of
159                  * it, object A.  The reference at A+O is processed by
160                  * copying X into nursery to-space at Y.  Since it's
161                  * still in the nursery, a global remset must be added
162                  * for A+O, so card C+1 is marked.  Now, however, card
163                  * C+1 is scanned, which means that it's cleared
164                  * first.  This wouldn't be terribly bad if reference
165                  * A+O were re-scanned and the global remset re-added,
166                  * but since the reference points to to-space, that
167                  * doesn't happen, and C+1 remains cleared: the remset
168                  * is lost.
169                  *
170                  * There's at least two ways to fix this.  The easy
171                  * one is to re-add the remset on the re-scan.  This
172                  * is that - the following two lines of code.
173                  *
174                  * The proper solution appears to be to first make a
175                  * copy of the cards before scanning a block, then to
176                  * clear all the cards and scan from the copy, so no
177                  * remsets will be overwritten.  Scanning objects at
178                  * most once would be the icing on the cake.
179                  */
180                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
181                         sgen_add_to_global_remset (obj_slot, obj);
182
183                 return;
184         }
185 #endif
186
187         HEAVY_STAT (++stat_objects_copied_nursery);
188
189         copy = copy_object_no_checks (obj, queue);
190         SGEN_UPDATE_REFERENCE (obj_slot, copy);
191 #ifndef SGEN_SIMPLE_NURSERY
192         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
193                 sgen_add_to_global_remset (obj_slot, copy);
194 #else
195         /* copy_object_no_checks () can return obj on OOM */
196         if (G_UNLIKELY (obj == copy)) {
197                 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
198                         sgen_add_to_global_remset (obj_slot, copy);
199         }
200 #endif
201 }
202
203 #define FILL_MINOR_COLLECTOR_COPY_OBJECT(collector)     do {                    \
204                 (collector)->serial_ops.copy_or_mark_object = SERIAL_COPY_OBJECT;                       \
205         } while (0)