2 * sgen-copy-object.h: This is where objects are copied.
4 * Copyright 2001-2003 Ximian, Inc
5 * Copyright 2003-2010 Novell, Inc.
6 * Copyright (C) 2012 Xamarin Inc
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 * GCObject* copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
16 * which allocates new space for `obj`, copies it there, forwards `obj` to its new location,
17 * and enqueues the copy into `queue`.
19 * To be defined by the includer:
21 * COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION(vt, obj, objsize, has_refs)
23 * Allocates space for promoting object `obj`, with size `objsize`, and initizializes the
24 * vtable with `vt`. `has_refs` indicates whether the object contains references.
26 * collector_pin_object(obj, queue)
28 * Called when no space for `obj` could be allocated. It must pin `obj` and enqueue it into
29 * `queue` for scanning.
32 extern guint64 stat_copy_object_called_nursery;
33 extern guint64 stat_objects_copied_nursery;
35 extern guint64 stat_nursery_copy_object_failed_from_space;
36 extern guint64 stat_nursery_copy_object_failed_forwarded;
37 extern guint64 stat_nursery_copy_object_failed_pinned;
39 extern guint64 stat_slots_allocated_in_vain;
42 * Copies an object and enqueues it if a queue is given.
44 * This function can be used even if the vtable of obj is not valid
45 * anymore, which is the case in the parallel collector.
47 static MONO_ALWAYS_INLINE void
48 par_copy_object_no_checks (char *destination, GCVTable vt, void *obj, mword objsize, SgenGrayQueue *queue)
50 sgen_client_pre_copy_checks (destination, vt, obj, objsize);
51 binary_protocol_copy (obj, destination, vt, objsize);
53 /* FIXME: assumes object layout */
54 memcpy ((char*)destination + sizeof (mword), (char*)obj + sizeof (mword), objsize - sizeof (mword));
56 /* adjust array->bounds */
57 SGEN_ASSERT (9, sgen_vtable_get_descriptor (vt), "vtable %p has no gc descriptor", vt);
59 sgen_client_update_copied_object (destination, vt, obj, objsize);
62 SGEN_LOG (9, "Enqueuing gray object %p (%s)", obj, sgen_client_vtable_get_name (vt));
63 GRAY_OBJECT_ENQUEUE (queue, (GCObject *)obj, sgen_vtable_get_descriptor (vt));
68 * This can return OBJ itself on OOM.
70 static MONO_NEVER_INLINE GCObject *
71 copy_object_no_checks (GCObject *obj, SgenGrayQueue *queue)
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 void *destination = COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION (vt, obj, objsize, has_references);
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);
89 par_copy_object_no_checks ((char *)destination, vt, obj, objsize, queue);
90 /* FIXME: mark mod union cards if necessary */
92 /* set the forwarding pointer */
93 SGEN_FORWARD_OBJECT (obj, destination);
95 return (GCObject *)destination;
98 #undef COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION
99 #undef collector_pin_object