Merge pull request #438 from mattleibow/master
[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 inline 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 (G_UNLIKELY (MONO_GC_OBJ_MOVED_ENABLED ())) {
47                 int dest_gen = sgen_ptr_in_nursery (destination) ? GENERATION_NURSERY : GENERATION_OLD;
48                 int src_gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
49                 MONO_GC_OBJ_MOVED ((mword)destination, (mword)obj, dest_gen, src_gen, objsize, vt->klass->name_space, vt->klass->name);
50         }
51
52         if (objsize <= sizeof (gpointer) * 8) {
53                 mword *dest = (mword*)destination;
54                 goto *copy_labels [objsize / sizeof (gpointer)];
55         LAB_8:
56                 (dest) [7] = ((mword*)obj) [7];
57         LAB_7:
58                 (dest) [6] = ((mword*)obj) [6];
59         LAB_6:
60                 (dest) [5] = ((mword*)obj) [5];
61         LAB_5:
62                 (dest) [4] = ((mword*)obj) [4];
63         LAB_4:
64                 (dest) [3] = ((mword*)obj) [3];
65         LAB_3:
66                 (dest) [2] = ((mword*)obj) [2];
67         LAB_2:
68                 (dest) [1] = ((mword*)obj) [1];
69         LAB_1:
70                 ;
71         LAB_0:
72                 ;
73         } else {
74                 /*can't trust memcpy doing word copies */
75                 mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
76         }
77         /* adjust array->bounds */
78         DEBUG (9, g_assert (vt->gc_descr));
79         if (G_UNLIKELY (vt->rank && ((MonoArray*)obj)->bounds)) {
80                 MonoArray *array = (MonoArray*)destination;
81                 array->bounds = (MonoArrayBounds*)((char*)destination + ((char*)((MonoArray*)obj)->bounds - (char*)obj));
82                 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)));
83         }
84         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
85                 sgen_register_moved_object (obj, destination);
86         obj = destination;
87         if (queue) {
88                 DEBUG (9, fprintf (gc_debug_file, "Enqueuing gray object %p (%s)\n", obj, sgen_safe_name (obj)));
89                 GRAY_OBJECT_ENQUEUE (queue, obj);
90         }
91 }
92
93 static G_GNUC_UNUSED void* __attribute__((noinline))
94 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
95 {
96         MonoVTable *vt = ((MonoObject*)obj)->vtable;
97         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
98         mword objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
99         char *destination = collector_serial_alloc_for_promotion (obj, objsize, has_references);
100
101         if (G_UNLIKELY (!destination)) {
102                 collector_pin_object (obj, queue);
103                 sgen_set_pinned_from_failed_allocation (objsize);
104                 return obj;
105         }
106
107         *(MonoVTable**)destination = vt;
108         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
109
110         /* set the forwarding pointer */
111         SGEN_FORWARD_OBJECT (obj, destination);
112
113         return destination;
114 }
115
116 #ifdef GENERATE_COPY_FUNCTIONS
117
118 extern long long stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
119
120 #if defined(SGEN_SIMPLE_NURSERY)
121 #define serial_copy_object simple_nursery_serial_copy_object
122 #define parallel_copy_object simple_nursery_parallel_copy_object
123
124 #elif defined (SGEN_SPLIT_NURSERY)
125
126 #define serial_copy_object split_nursery_serial_copy_object
127 #define parallel_copy_object split_nursery_parallel_copy_object
128
129 #else
130 #error "Please define GC_CONF_NAME"
131 #endif
132
133 /*
134  * This is how the copying happens from the nursery to the old generation.
135  * We assume that at this time all the pinned objects have been identified and
136  * marked as such.
137  * We run scan_object() for each pinned object so that each referenced
138  * objects if possible are copied. The new gray objects created can have
139  * scan_object() run on them right away, too.
140  * Then we run copy_object() for the precisely tracked roots. At this point
141  * all the roots are either gray or black. We run scan_object() on the gray
142  * objects until no more gray objects are created.
143  * At the end of the process we walk again the pinned list and we unmark
144  * the pinned flag. As we go we also create the list of free space for use
145  * in the next allocation runs.
146  *
147  * We need to remember objects from the old generation that point to the new one
148  * (or just addresses?).
149  *
150  * copy_object could be made into a macro once debugged (use inline for now).
151  */
152
153 static inline void __attribute__((always_inline))
154 serial_copy_object (void **obj_slot, SgenGrayQueue *queue) 
155 {
156         char *forwarded;
157         char *obj = *obj_slot;
158
159         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
160
161         HEAVY_STAT (++stat_copy_object_called_nursery);
162
163         if (!sgen_ptr_in_nursery (obj)) {
164                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
165                 return;
166         }
167
168         DEBUG (9, fprintf (gc_debug_file, "Precise copy of %p from %p", obj, obj_slot));
169
170         /*
171          * Before we can copy the object we must make sure that we are
172          * allowed to, i.e. that the object not pinned, not already
173          * forwarded or belongs to the nursery To Space.
174          */
175
176         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
177                 DEBUG (9, g_assert ((*(MonoVTable**)SGEN_LOAD_VTABLE(obj))->gc_descr));
178                 DEBUG (9, fprintf (gc_debug_file, " (already forwarded to %p)\n", forwarded));
179                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
180                 *obj_slot = forwarded;
181                 return;
182         }
183         if (SGEN_OBJECT_IS_PINNED (obj)) {
184                 DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
185                 DEBUG (9, fprintf (gc_debug_file, " (pinned, no change)\n"));
186                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
187                 return;
188         }
189
190         if (sgen_nursery_is_to_space (obj)) {
191                 DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
192                 DEBUG (9, fprintf (gc_debug_file, " (tospace, no change)\n"));
193                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
194                 return;
195         }
196
197         HEAVY_STAT (++stat_objects_copied_nursery);
198
199         *obj_slot = copy_object_no_checks (obj, queue);
200 }
201
202 static void
203 parallel_copy_object (void **obj_slot, SgenGrayQueue *queue)
204 {
205         char *obj = *obj_slot;
206         mword vtable_word, objsize;
207         MonoVTable *vt;
208         void *destination;
209         gboolean has_references;
210
211         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
212
213         HEAVY_STAT (++stat_copy_object_called_nursery);
214
215         if (!sgen_ptr_in_nursery (obj)) {
216                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
217                 return;
218         }
219
220         vtable_word = *(mword*)obj;
221         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
222
223         /*
224          * Before we can copy the object we must make sure that we are
225          * allowed to, i.e. that the object not pinned, not already
226          * forwarded and not in the nursery To Space.
227          */
228
229         if (vtable_word & SGEN_FORWARDED_BIT) {
230                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
231                 *obj_slot = vt;
232                 return;
233         }
234         if (vtable_word & SGEN_PINNED_BIT) {
235                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
236                 return;
237         }
238
239         if (sgen_nursery_is_to_space (obj)) {
240                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
241                 return;
242         }
243
244         HEAVY_STAT (++stat_objects_copied_nursery);
245
246         objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
247         has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
248
249         destination = collector_parallel_alloc_for_promotion (obj, objsize, has_references);
250
251         if (G_UNLIKELY (!destination)) {
252                 sgen_parallel_pin_or_update (obj_slot, obj, vt, queue);
253                 return;
254         }
255
256         *(MonoVTable**)destination = vt;
257
258         if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
259                 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
260                 obj = destination;
261                 *obj_slot = obj;
262         } else {
263                 /* FIXME: unify with code in major_copy_or_mark_object() */
264
265                 /* FIXME: Give destination back to the allocator. */
266                 /*The major collector only needs the first word zeroed and nursery requires all bits to be. */
267                 if (!sgen_ptr_in_nursery (destination))
268                         *(void**)destination = NULL;
269                 else
270                         memset (destination, 0, objsize);
271
272                 vtable_word = *(mword*)obj;
273                 g_assert (vtable_word & SGEN_FORWARDED_BIT);
274
275                 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
276
277                 *obj_slot = obj;
278
279                 HEAVY_STAT (++stat_slots_allocated_in_vain);
280         }
281 }
282
283 #endif