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