Improved support for TimeZoneInfo under windows.
[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
25 extern long long stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
26
27 #include "mono/utils/mono-compiler.h"
28
29 #include "sgen-copy-object.h"
30
31 /*
32  * This is how the copying happens from the nursery to the old generation.
33  * We assume that at this time all the pinned objects have been identified and
34  * marked as such.
35  * We run scan_object() for each pinned object so that each referenced
36  * objects if possible are copied. The new gray objects created can have
37  * scan_object() run on them right away, too.
38  * Then we run copy_object() for the precisely tracked roots. At this point
39  * all the roots are either gray or black. We run scan_object() on the gray
40  * objects until no more gray objects are created.
41  * At the end of the process we walk again the pinned list and we unmark
42  * the pinned flag. As we go we also create the list of free space for use
43  * in the next allocation runs.
44  *
45  * We need to remember objects from the old generation that point to the new one
46  * (or just addresses?).
47  *
48  * copy_object could be made into a macro once debugged (use inline for now).
49  */
50
51 static MONO_ALWAYS_INLINE void
52 SERIAL_COPY_OBJECT (void **obj_slot, SgenGrayQueue *queue) 
53 {
54         char *forwarded;
55         char *copy;
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                 SGEN_UPDATE_REFERENCE (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         copy = copy_object_no_checks (obj, queue);
101         SGEN_UPDATE_REFERENCE (obj_slot, copy);
102 }
103
104 /*
105  * SERIAL_COPY_OBJECT_FROM_OBJ:
106  *
107  *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
108  */
109 static MONO_ALWAYS_INLINE void
110 SERIAL_COPY_OBJECT_FROM_OBJ (void **obj_slot, SgenGrayQueue *queue) 
111 {
112         char *forwarded;
113         char *obj = *obj_slot;
114         void *copy;
115
116         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
117
118         HEAVY_STAT (++stat_copy_object_called_nursery);
119
120         if (!sgen_ptr_in_nursery (obj)) {
121                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
122                 return;
123         }
124
125         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
126
127         /*
128          * Before we can copy the object we must make sure that we are
129          * allowed to, i.e. that the object not pinned, not already
130          * forwarded or belongs to the nursery To Space.
131          */
132
133         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
134                 SGEN_ASSERT (9, (*(MonoVTable**)SGEN_LOAD_VTABLE (obj))->gc_descr,  "forwarded object %p has no gc descriptor", forwarded);
135                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
136                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
137                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
138 #ifndef SGEN_SIMPLE_NURSERY
139                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot)))
140                         sgen_add_to_global_remset (obj_slot, forwarded);
141 #endif
142                 return;
143         }
144         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
145                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "pinned object %p has no gc descriptor", obj);
146                 SGEN_LOG (9, " (pinned, no change)");
147                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
148                 if (!sgen_ptr_in_nursery (obj_slot))
149                         sgen_add_to_global_remset (obj_slot, obj);
150                 return;
151         }
152
153 #ifndef SGEN_SIMPLE_NURSERY
154         if (sgen_nursery_is_to_space (obj)) {
155                 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "to space object %p has no gc descriptor", obj);
156                 SGEN_LOG (9, " (tospace, no change)");
157                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
158
159                 /*
160                  * FIXME:
161                  *
162                  * The card table scanning code sometimes clears cards
163                  * that have just been set for a global remset.  In
164                  * the split nursery the following situation can
165                  * occur:
166                  *
167                  * Let's say object A starts in card C but continues
168                  * into C+1.  Within A, at offset O there's a
169                  * reference to a new nursery object X.  A+O is in
170                  * card C+1.  Now card C is scanned, and as part of
171                  * it, object A.  The reference at A+O is processed by
172                  * copying X into nursery to-space at Y.  Since it's
173                  * still in the nursery, a global remset must be added
174                  * for A+O, so card C+1 is marked.  Now, however, card
175                  * C+1 is scanned, which means that it's cleared
176                  * first.  This wouldn't be terribly bad if reference
177                  * A+O were re-scanned and the global remset re-added,
178                  * but since the reference points to to-space, that
179                  * doesn't happen, and C+1 remains cleared: the remset
180                  * is lost.
181                  *
182                  * There's at least two ways to fix this.  The easy
183                  * one is to re-add the remset on the re-scan.  This
184                  * is that - the following two lines of code.
185                  *
186                  * The proper solution appears to be to first make a
187                  * copy of the cards before scanning a block, then to
188                  * clear all the cards and scan from the copy, so no
189                  * remsets will be overwritten.  Scanning objects at
190                  * most once would be the icing on the cake.
191                  */
192                 if (!sgen_ptr_in_nursery (obj_slot))
193                         sgen_add_to_global_remset (obj_slot, obj);
194
195                 return;
196         }
197 #endif
198
199         HEAVY_STAT (++stat_objects_copied_nursery);
200
201         copy = copy_object_no_checks (obj, queue);
202         SGEN_UPDATE_REFERENCE (obj_slot, copy);
203 #ifndef SGEN_SIMPLE_NURSERY
204         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot)))
205                 sgen_add_to_global_remset (obj_slot, copy);
206 #else
207         /* copy_object_no_checks () can return obj on OOM */
208         if (G_UNLIKELY (obj == copy)) {
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         }
212 #endif
213 }
214
215 #define FILL_MINOR_COLLECTOR_COPY_OBJECT(collector)     do {                    \
216                 (collector)->serial_ops.copy_or_mark_object = SERIAL_COPY_OBJECT;                       \
217         } while (0)