Fix a case missed by 7e222739db7192eb0c5ec17cad18e309482e71b4.
[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         SGEN_ASSERT (9, vt->klass->inited, "vtable %p for class %s:%s was not initialized", vt, vt->klass->name_space, vt->klass->name);
45         SGEN_LOG (9, " (to %p, %s size: %lu)", destination, ((MonoObject*)obj)->vtable->klass->name, (unsigned long)objsize);
46         binary_protocol_copy (obj, destination, vt, objsize);
47
48         if (G_UNLIKELY (MONO_GC_OBJ_MOVED_ENABLED ())) {
49                 int dest_gen = sgen_ptr_in_nursery (destination) ? GENERATION_NURSERY : GENERATION_OLD;
50                 int src_gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
51                 MONO_GC_OBJ_MOVED ((mword)destination, (mword)obj, dest_gen, src_gen, objsize, vt->klass->name_space, vt->klass->name);
52         }
53
54 #ifdef __GNUC__
55         if (objsize <= sizeof (gpointer) * 8) {
56                 mword *dest = (mword*)destination;
57                 goto *copy_labels [objsize / sizeof (gpointer)];
58         LAB_8:
59                 (dest) [7] = ((mword*)obj) [7];
60         LAB_7:
61                 (dest) [6] = ((mword*)obj) [6];
62         LAB_6:
63                 (dest) [5] = ((mword*)obj) [5];
64         LAB_5:
65                 (dest) [4] = ((mword*)obj) [4];
66         LAB_4:
67                 (dest) [3] = ((mword*)obj) [3];
68         LAB_3:
69                 (dest) [2] = ((mword*)obj) [2];
70         LAB_2:
71                 (dest) [1] = ((mword*)obj) [1];
72         LAB_1:
73                 ;
74         LAB_0:
75                 ;
76         } else {
77                 /*can't trust memcpy doing word copies */
78                 mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
79         }
80 #else
81                 mono_gc_memmove (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
82 #endif
83         /* adjust array->bounds */
84         SGEN_ASSERT (9, vt->gc_descr, "vtable %p for class %s:%s has no gc descriptor", vt, vt->klass->name_space, vt->klass->name);
85
86         if (G_UNLIKELY (vt->rank && ((MonoArray*)obj)->bounds)) {
87                 MonoArray *array = (MonoArray*)destination;
88                 array->bounds = (MonoArrayBounds*)((char*)destination + ((char*)((MonoArray*)obj)->bounds - (char*)obj));
89                 SGEN_LOG (9, "Array instance %p: size: %lu, rank: %d, length: %lu", array, (unsigned long)objsize, vt->rank, (unsigned long)mono_array_length (array));
90         }
91         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
92                 sgen_register_moved_object (obj, destination);
93         obj = destination;
94         if (queue) {
95                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_safe_name (obj));
96                 GRAY_OBJECT_ENQUEUE (queue, obj);
97         }
98 }
99
100 /*
101  * This can return OBJ itself on OOM.
102  */
103 #ifdef _MSC_VER
104 static __declspec(noinline) void*
105 #else
106 static G_GNUC_UNUSED void* __attribute__((noinline))
107 #endif
108 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
109 {
110         MonoVTable *vt = ((MonoObject*)obj)->vtable;
111         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
112         mword objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
113         char *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (obj, objsize, has_references);
114
115         if (G_UNLIKELY (!destination)) {
116                 collector_pin_object (obj, queue);
117                 sgen_set_pinned_from_failed_allocation (objsize);
118                 return obj;
119         }
120
121         *(MonoVTable**)destination = vt;
122         par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
123
124         /* set the forwarding pointer */
125         SGEN_FORWARD_OBJECT (obj, destination);
126
127         return destination;
128 }