Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mono / metadata / sgen-major-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 /*
32  * This function can be used even if the vtable of obj is not valid
33  * anymore, which is the case in the parallel collector.
34  */
35 static void
36 par_copy_object_no_checks (char *destination, MonoVTable *vt, void *obj, mword objsize, SgenGrayQueue *queue)
37 {
38         static const void *copy_labels [] = { &&LAB_0, &&LAB_1, &&LAB_2, &&LAB_3, &&LAB_4, &&LAB_5, &&LAB_6, &&LAB_7, &&LAB_8 };
39
40         DEBUG (9, g_assert (vt->klass->inited));
41         DEBUG (9, fprintf (gc_debug_file, " (to %p, %s size: %lu)\n", destination, ((MonoObject*)obj)->vtable->klass->name, (unsigned long)objsize));
42         binary_protocol_copy (obj, destination, vt, objsize);
43
44         if (objsize <= sizeof (gpointer) * 8) {
45                 mword *dest = (mword*)destination;
46                 goto *copy_labels [objsize / sizeof (gpointer)];
47         LAB_8:
48                 (dest) [7] = ((mword*)obj) [7];
49         LAB_7:
50                 (dest) [6] = ((mword*)obj) [6];
51         LAB_6:
52                 (dest) [5] = ((mword*)obj) [5];
53         LAB_5:
54                 (dest) [4] = ((mword*)obj) [4];
55         LAB_4:
56                 (dest) [3] = ((mword*)obj) [3];
57         LAB_3:
58                 (dest) [2] = ((mword*)obj) [2];
59         LAB_2:
60                 (dest) [1] = ((mword*)obj) [1];
61         LAB_1:
62                 ;
63         LAB_0:
64                 ;
65         } else {
66                 /*can't trust memcpy doing word copies */
67                 mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
68         }
69         /* adjust array->bounds */
70         DEBUG (9, g_assert (vt->gc_descr));
71         if (G_UNLIKELY (vt->rank && ((MonoArray*)obj)->bounds)) {
72                 MonoArray *array = (MonoArray*)destination;
73                 array->bounds = (MonoArrayBounds*)((char*)destination + ((char*)((MonoArray*)obj)->bounds - (char*)obj));
74                 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)));
75         }
76         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
77                 mono_sgen_register_moved_object (obj, destination);
78         obj = destination;
79         if (queue) {
80                 DEBUG (9, fprintf (gc_debug_file, "Enqueuing gray object %p (%s)\n", obj, mono_sgen_safe_name (obj)));
81                 GRAY_OBJECT_ENQUEUE (queue, obj);
82         }
83 }
84
85 static void*
86 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
87 {
88         MonoVTable *vt = ((MonoObject*)obj)->vtable;
89         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
90         mword objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
91         char *destination = major_alloc_object (objsize, has_references);
92
93         if (G_UNLIKELY (!destination)) {
94                 if (mono_sgen_ptr_in_nursery (obj)) {
95                         mono_sgen_pin_object (obj, queue);
96                 } else {
97                         g_assert (objsize <= SGEN_MAX_SMALL_OBJ_SIZE);
98                         pin_major_object (obj, queue);
99                 }
100                 mono_sgen_set_pinned_from_failed_allocation (objsize);
101                 return obj;
102         }
103
104         *(MonoVTable**)destination = vt;
105         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
106
107         /* set the forwarding pointer */
108         SGEN_FORWARD_OBJECT (obj, destination);
109
110         return destination;
111 }
112
113 /*
114  * This is how the copying happens from the nursery to the old generation.
115  * We assume that at this time all the pinned objects have been identified and
116  * marked as such.
117  * We run scan_object() for each pinned object so that each referenced
118  * objects if possible are copied. The new gray objects created can have
119  * scan_object() run on them right away, too.
120  * Then we run copy_object() for the precisely tracked roots. At this point
121  * all the roots are either gray or black. We run scan_object() on the gray
122  * objects until no more gray objects are created.
123  * At the end of the process we walk again the pinned list and we unmark
124  * the pinned flag. As we go we also create the list of free space for use
125  * in the next allocation runs.
126  *
127  * We need to remember objects from the old generation that point to the new one
128  * (or just addresses?).
129  *
130  * copy_object could be made into a macro once debugged (use inline for now).
131  */
132
133 static void
134 nopar_copy_object (void **obj_slot, SgenGrayQueue *queue)
135 {
136         char *forwarded;
137         char *obj = *obj_slot;
138
139         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
140
141         HEAVY_STAT (++stat_copy_object_called_nursery);
142
143         if (!mono_sgen_ptr_in_nursery (obj)) {
144                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
145                 return;
146         }
147
148         DEBUG (9, fprintf (gc_debug_file, "Precise copy of %p from %p", obj, obj_slot));
149
150         /*
151          * Before we can copy the object we must make sure that we are
152          * allowed to, i.e. that the object not pinned or not already
153          * forwarded.
154          */
155
156         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
157                 DEBUG (9, g_assert ((*(MonoVTable**)SGEN_LOAD_VTABLE(obj))->gc_descr));
158                 DEBUG (9, fprintf (gc_debug_file, " (already forwarded to %p)\n", forwarded));
159                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
160                 *obj_slot = forwarded;
161                 return;
162         }
163         if (SGEN_OBJECT_IS_PINNED (obj)) {
164                 DEBUG (9, g_assert (((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr));
165                 DEBUG (9, fprintf (gc_debug_file, " (pinned, no change)\n"));
166                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
167                 return;
168         }
169
170         HEAVY_STAT (++stat_objects_copied_nursery);
171
172         *obj_slot = copy_object_no_checks (obj, queue);
173 }
174
175 #ifdef SGEN_PARALLEL_MARK
176 static void
177 copy_object (void **obj_slot, SgenGrayQueue *queue)
178 {
179         char *obj = *obj_slot;
180         mword vtable_word, objsize;
181         MonoVTable *vt;
182         void *destination;
183         gboolean has_references;
184
185         DEBUG (9, g_assert (current_collection_generation == GENERATION_NURSERY));
186
187         HEAVY_STAT (++stat_copy_object_called_nursery);
188
189         if (!mono_sgen_ptr_in_nursery (obj)) {
190                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
191                 return;
192         }
193
194         vtable_word = *(mword*)obj;
195         vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
196
197         /*
198          * Before we can copy the object we must make sure that we are
199          * allowed to, i.e. that the object not pinned or not already
200          * forwarded.
201          */
202
203         if (vtable_word & SGEN_FORWARDED_BIT) {
204                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
205                 *obj_slot = vt;
206                 return;
207         }
208         if (vtable_word & SGEN_PINNED_BIT) {
209                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
210                 return;
211         }
212
213         HEAVY_STAT (++stat_objects_copied_nursery);
214
215         objsize = SGEN_ALIGN_UP (mono_sgen_par_object_get_size (vt, (MonoObject*)obj));
216         has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
217
218         destination = alloc_obj_par (objsize, FALSE, has_references);
219
220         if (G_UNLIKELY (!destination)) {
221                 pin_or_update_par (obj_slot, obj, vt, queue);
222                 return;
223         }
224
225         *(MonoVTable**)destination = vt;
226
227         if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
228                 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
229                 obj = destination;
230                 *obj_slot = obj;
231         } else {
232                 /* FIXME: unify with code in major_copy_or_mark_object() */
233
234                 /* FIXME: Give destination back to the allocator. */
235                 *(void**)destination = NULL;
236
237                 vtable_word = *(mword*)obj;
238                 g_assert (vtable_word & SGEN_FORWARDED_BIT);
239
240                 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
241
242                 *obj_slot = obj;
243
244                 ++stat_slots_allocated_in_vain;
245         }
246 }
247 #else
248 static void
249 copy_object (void **obj_slot, SgenGrayQueue *queue)
250 {
251         nopar_copy_object (obj_slot, queue);
252 }
253 #endif
254
255 #define FILL_COLLECTOR_COPY_OBJECT(collector)   do {                    \
256                 (collector)->copy_object = copy_object;                 \
257                 (collector)->nopar_copy_object = nopar_copy_object;     \
258         } while (0)