Merge pull request #271 from pruiz/xamarin-bug-4108
[mono.git] / mono / metadata / sgen-copy-object.h
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 extern long long stat_copy_object_called_nursery;
25 extern long long stat_objects_copied_nursery;
26
27 extern long long stat_nursery_copy_object_failed_from_space;
28 extern long long stat_nursery_copy_object_failed_forwarded;
29 extern long long stat_nursery_copy_object_failed_pinned;
30
31 extern long long stat_slots_allocated_in_vain;
32
33 /*
34  * This function can be used even if the vtable of obj is not valid
35  * anymore, which is the case in the parallel collector.
36  */
37 static void
38 par_copy_object_no_checks (char *destination, MonoVTable *vt, void *obj, mword objsize, SgenGrayQueue *queue)
39 {
40         static const void *copy_labels [] = { &&LAB_0, &&LAB_1, &&LAB_2, &&LAB_3, &&LAB_4, &&LAB_5, &&LAB_6, &&LAB_7, &&LAB_8 };
41
42         DEBUG (9, g_assert (vt->klass->inited));
43         DEBUG (9, fprintf (gc_debug_file, " (to %p, %s size: %lu)\n", destination, ((MonoObject*)obj)->vtable->klass->name, (unsigned long)objsize));
44         binary_protocol_copy (obj, destination, vt, objsize);
45
46         if (objsize <= sizeof (gpointer) * 8) {
47                 mword *dest = (mword*)destination;
48                 goto *copy_labels [objsize / sizeof (gpointer)];
49         LAB_8:
50                 (dest) [7] = ((mword*)obj) [7];
51         LAB_7:
52                 (dest) [6] = ((mword*)obj) [6];
53         LAB_6:
54                 (dest) [5] = ((mword*)obj) [5];
55         LAB_5:
56                 (dest) [4] = ((mword*)obj) [4];
57         LAB_4:
58                 (dest) [3] = ((mword*)obj) [3];
59         LAB_3:
60                 (dest) [2] = ((mword*)obj) [2];
61         LAB_2:
62                 (dest) [1] = ((mword*)obj) [1];
63         LAB_1:
64                 ;
65         LAB_0:
66                 ;
67         } else {
68                 /*can't trust memcpy doing word copies */
69                 mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
70         }
71         /* adjust array->bounds */
72         DEBUG (9, g_assert (vt->gc_descr));
73         if (G_UNLIKELY (vt->rank && ((MonoArray*)obj)->bounds)) {
74                 MonoArray *array = (MonoArray*)destination;
75                 array->bounds = (MonoArrayBounds*)((char*)destination + ((char*)((MonoArray*)obj)->bounds - (char*)obj));
76                 DEBUG (9, fprintf (gc_debug_file, "Array instance %p: size: %lu, rank: %d, length: %lu\n", array, (unsigned long)objsize, vt->rank, (unsigned long)mono_array_length (array)));
77         }
78         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
79                 sgen_register_moved_object (obj, destination);
80         obj = destination;
81         if (queue) {
82                 DEBUG (9, fprintf (gc_debug_file, "Enqueuing gray object %p (%s)\n", obj, sgen_safe_name (obj)));
83                 GRAY_OBJECT_ENQUEUE (queue, obj);
84         }
85 }
86
87 static G_GNUC_UNUSED void*
88 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
89 {
90         MonoVTable *vt = ((MonoObject*)obj)->vtable;
91         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
92         mword objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
93         char *destination = collector_serial_alloc_for_promotion (obj, objsize, has_references);
94
95         if (G_UNLIKELY (!destination)) {
96                 collector_pin_object (obj, queue);
97                 sgen_set_pinned_from_failed_allocation (objsize);
98                 return obj;
99         }
100
101         *(MonoVTable**)destination = vt;
102         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
103
104         /* set the forwarding pointer */
105         SGEN_FORWARD_OBJECT (obj, destination);
106
107         return destination;
108 }
109
110 #ifdef GENERATE_COPY_FUNCTIONS
111
112 /*
113  * This is how the copying happens from the nursery to the old generation.
114  * We assume that at this time all the pinned objects have been identified and
115  * marked as such.
116  * We run scan_object() for each pinned object so that each referenced
117  * objects if possible are copied. The new gray objects created can have
118  * scan_object() run on them right away, too.
119  * Then we run copy_object() for the precisely tracked roots. At this point
120  * all the roots are either gray or black. We run scan_object() on the gray
121  * objects until no more gray objects are created.
122  * At the end of the process we walk again the pinned list and we unmark
123  * the pinned flag. As we go we also create the list of free space for use
124  * in the next allocation runs.
125  *
126  * We need to remember objects from the old generation that point to the new one
127  * (or just addresses?).
128  *
129  * copy_object could be made into a macro once debugged (use inline for now).
130  */
131
132 static void
133 serial_copy_object (void **obj_slot, SgenGrayQueue *queue)
134 {
135         char *forwarded;
136         char *obj = *obj_slot;
137
138         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
139
140         HEAVY_STAT (++stat_copy_object_called_nursery);
141
142         if (!sgen_ptr_in_nursery (obj)) {
143                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
144                 return;
145         }
146
147         DEBUG (9, fprintf (gc_debug_file, "Precise copy of %p from %p", obj, obj_slot));
148
149         /*
150          * Before we can copy the object we must make sure that we are
151          * allowed to, i.e. that the object not pinned, not already
152          * forwarded or belongs to the nursery To Space.
153          */
154
155         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
156                 DEBUG (9, g_assert ((*(MonoVTable**)SGEN_LOAD_VTABLE(obj))->gc_descr));
157                 DEBUG (9, fprintf (gc_debug_file, " (already forwarded to %p)\n", forwarded));
158                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
159                 *obj_slot = forwarded;
160                 return;
161         }
162         if (SGEN_OBJECT_IS_PINNED (obj)) {
163                 DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
164                 DEBUG (9, fprintf (gc_debug_file, " (pinned, no change)\n"));
165                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
166                 return;
167         }
168
169         if (sgen_nursery_is_to_space (obj)) {
170                 DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
171                 DEBUG (9, fprintf (gc_debug_file, " (tospace, no change)\n"));
172                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
173                 return;
174         }
175
176         HEAVY_STAT (++stat_objects_copied_nursery);
177
178         *obj_slot = copy_object_no_checks (obj, queue);
179 }
180
181 static void
182 parallel_copy_object (void **obj_slot, SgenGrayQueue *queue)
183 {
184         char *obj = *obj_slot;
185         mword vtable_word, objsize;
186         MonoVTable *vt;
187         void *destination;
188         gboolean has_references;
189
190         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
191
192         HEAVY_STAT (++stat_copy_object_called_nursery);
193
194         if (!sgen_ptr_in_nursery (obj)) {
195                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
196                 return;
197         }
198
199         vtable_word = *(mword*)obj;
200         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
201
202         /*
203          * Before we can copy the object we must make sure that we are
204          * allowed to, i.e. that the object not pinned, not already
205          * forwarded and not in the nursery To Space.
206          */
207
208         if (vtable_word & SGEN_FORWARDED_BIT) {
209                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
210                 *obj_slot = vt;
211                 return;
212         }
213         if (vtable_word & SGEN_PINNED_BIT) {
214                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
215                 return;
216         }
217
218         if (sgen_nursery_is_to_space (obj)) {
219                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
220                 return;
221         }
222
223         HEAVY_STAT (++stat_objects_copied_nursery);
224
225         objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
226         has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
227
228         destination = collector_parallel_alloc_for_promotion (obj, objsize, has_references);
229
230         if (G_UNLIKELY (!destination)) {
231                 sgen_parallel_pin_or_update (obj_slot, obj, vt, queue);
232                 return;
233         }
234
235         *(MonoVTable**)destination = vt;
236
237         if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
238                 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
239                 obj = destination;
240                 *obj_slot = obj;
241         } else {
242                 /* FIXME: unify with code in major_copy_or_mark_object() */
243
244                 /* FIXME: Give destination back to the allocator. */
245                 /*The major collector only needs the first word zeroed and nursery requires all bits to be. */
246                 if (!sgen_ptr_in_nursery (destination))
247                         *(void**)destination = NULL;
248                 else
249                         memset (destination, 0, objsize);
250
251                 vtable_word = *(mword*)obj;
252                 g_assert (vtable_word & SGEN_FORWARDED_BIT);
253
254                 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
255
256                 *obj_slot = obj;
257
258                 HEAVY_STAT (++stat_slots_allocated_in_vain);
259         }
260 }
261
262 #endif