0cb4633b36164ceffd5d7a1fc7d6a4e86b00e43e
[mono.git] / mono / metadata / sgen-copy-object.h
1 /*
2  * sgen-copy-object.h: This is where objects are copied.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 extern guint64 stat_copy_object_called_nursery;
23 extern guint64 stat_objects_copied_nursery;
24
25 extern guint64 stat_nursery_copy_object_failed_from_space;
26 extern guint64 stat_nursery_copy_object_failed_forwarded;
27 extern guint64 stat_nursery_copy_object_failed_pinned;
28
29 extern guint64 stat_slots_allocated_in_vain;
30
31 /*
32  * Copies an object and enqueues it if a queue is given.
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 MONO_ALWAYS_INLINE void
38 par_copy_object_no_checks (char *destination, GCVTable *vt, void *obj, mword objsize, SgenGrayQueue *queue)
39 {
40         sgen_client_pre_copy_checks (destination, vt, obj, objsize);
41         binary_protocol_copy (obj, destination, vt, objsize);
42
43 #ifdef ENABLE_DTRACE
44         if (G_UNLIKELY (MONO_GC_OBJ_MOVED_ENABLED ())) {
45                 int dest_gen = sgen_ptr_in_nursery (destination) ? GENERATION_NURSERY : GENERATION_OLD;
46                 int src_gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
47                 MONO_GC_OBJ_MOVED ((mword)destination, (mword)obj, dest_gen, src_gen, objsize, sgen_client_vtable_get_namespace (vt), sgen_client_vtable_get_name (vt));
48         }
49 #endif
50
51         /* FIXME: assumes object layout */
52         memcpy (destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
53
54         /* adjust array->bounds */
55         SGEN_ASSERT (9, sgen_vtable_get_descriptor (vt), "vtable %p has no gc descriptor", vt);
56
57         sgen_client_update_copied_object (destination, vt, obj, objsize);
58         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
59                 sgen_register_moved_object (obj, destination);
60         obj = destination;
61         if (queue) {
62                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_client_object_safe_name (obj));
63                 GRAY_OBJECT_ENQUEUE (queue, obj, sgen_vtable_get_descriptor (vt));
64         }
65 }
66
67 /*
68  * This can return OBJ itself on OOM.
69  */
70 static MONO_NEVER_INLINE void*
71 copy_object_no_checks (void *obj, SgenGrayQueue *queue)
72 {
73         GCVTable *vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
74         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
75         mword objsize = SGEN_ALIGN_UP (sgen_client_par_object_get_size (vt, obj));
76         /* FIXME: Does this not mark the newly allocated object? */
77         char *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
78
79         if (G_UNLIKELY (!destination)) {
80                 /* FIXME: Is this path ever tested? */
81                 collector_pin_object (obj, queue);
82                 sgen_set_pinned_from_failed_allocation (objsize);
83                 return obj;
84         }
85
86         if (!has_references)
87                 queue = NULL;
88
89         par_copy_object_no_checks (destination, vt, obj, objsize, queue);
90         /* FIXME: mark mod union cards if necessary */
91
92         /* set the forwarding pointer */
93         SGEN_FORWARD_OBJECT (obj, destination);
94
95         return destination;
96 }