First set of licensing changes
[mono.git] / mono / sgen / 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  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 extern guint64 stat_copy_object_called_nursery;
12 extern guint64 stat_objects_copied_nursery;
13
14 extern guint64 stat_nursery_copy_object_failed_from_space;
15 extern guint64 stat_nursery_copy_object_failed_forwarded;
16 extern guint64 stat_nursery_copy_object_failed_pinned;
17
18 extern guint64 stat_slots_allocated_in_vain;
19
20 /*
21  * Copies an object and enqueues it if a queue is given.
22  *
23  * This function can be used even if the vtable of obj is not valid
24  * anymore, which is the case in the parallel collector.
25  */
26 static MONO_ALWAYS_INLINE void
27 par_copy_object_no_checks (char *destination, GCVTable vt, void *obj, mword objsize, SgenGrayQueue *queue)
28 {
29         sgen_client_pre_copy_checks (destination, vt, obj, objsize);
30         binary_protocol_copy (obj, destination, vt, objsize);
31
32         /* FIXME: assumes object layout */
33         memcpy ((char*)destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
34
35         /* adjust array->bounds */
36         SGEN_ASSERT (9, sgen_vtable_get_descriptor (vt), "vtable %p has no gc descriptor", vt);
37
38         sgen_client_update_copied_object (destination, vt, obj, objsize);
39         obj = destination;
40         if (queue) {
41                 SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_client_vtable_get_name (vt));
42                 GRAY_OBJECT_ENQUEUE (queue, (GCObject *)obj, sgen_vtable_get_descriptor (vt));
43         }
44 }
45
46 /*
47  * This can return OBJ itself on OOM.
48  */
49 static MONO_NEVER_INLINE GCObject *
50 copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
51 {
52         GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
53         gboolean has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
54         mword objsize = SGEN_ALIGN_UP (sgen_client_par_object_get_size (vt, obj));
55         /* FIXME: Does this not mark the newly allocated object? */
56         void *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
57
58         if (G_UNLIKELY (!destination)) {
59                 /* FIXME: Is this path ever tested? */
60                 collector_pin_object (obj, queue);
61                 sgen_set_pinned_from_failed_allocation (objsize);
62                 return obj;
63         }
64
65         if (!has_references)
66                 queue = NULL;
67
68         par_copy_object_no_checks ((char *)destination, vt, obj, objsize, queue);
69         /* FIXME: mark mod union cards if necessary */
70
71         /* set the forwarding pointer */
72         SGEN_FORWARD_OBJECT (obj, destination);
73
74         return (GCObject *)destination;
75 }