2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
26 * This file defines copy functions for nursery collections.
29 #define collector_pin_object(obj, queue) sgen_pin_object (obj, queue);
30 #define COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION alloc_for_promotion
31 #define COLLECTOR_PARALLEL_ALLOC_FOR_PROMOTION par_alloc_for_promotion
33 extern long long stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
35 #include "sgen-copy-object.h"
38 * This is how the copying happens from the nursery to the old generation.
39 * We assume that at this time all the pinned objects have been identified and
41 * We run scan_object() for each pinned object so that each referenced
42 * objects if possible are copied. The new gray objects created can have
43 * scan_object() run on them right away, too.
44 * Then we run copy_object() for the precisely tracked roots. At this point
45 * all the roots are either gray or black. We run scan_object() on the gray
46 * objects until no more gray objects are created.
47 * At the end of the process we walk again the pinned list and we unmark
48 * the pinned flag. As we go we also create the list of free space for use
49 * in the next allocation runs.
51 * We need to remember objects from the old generation that point to the new one
52 * (or just addresses?).
54 * copy_object could be made into a macro once debugged (use inline for now).
58 static __forceinline void
60 static inline void __attribute__((always_inline))
62 SERIAL_COPY_OBJECT (void **obj_slot, SgenGrayQueue *queue)
65 char *obj = *obj_slot;
67 SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
69 HEAVY_STAT (++stat_copy_object_called_nursery);
71 if (!sgen_ptr_in_nursery (obj)) {
72 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
76 SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
79 * Before we can copy the object we must make sure that we are
80 * allowed to, i.e. that the object not pinned, not already
81 * forwarded or belongs to the nursery To Space.
84 if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
85 SGEN_ASSERT (9, (*(MonoVTable**)SGEN_LOAD_VTABLE (obj))->gc_descr, "forwarded object %p has no gc descriptor", forwarded);
86 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
87 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
88 *obj_slot = forwarded;
91 if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
92 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "pinned object %p has no gc descriptor", obj);
93 SGEN_LOG (9, " (pinned, no change)");
94 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
98 #ifndef SGEN_SIMPLE_NURSERY
99 if (sgen_nursery_is_to_space (obj)) {
100 SGEN_ASSERT (9, ((MonoVTable*)SGEN_LOAD_VTABLE(obj))->gc_descr, "to space object %p has no gc descriptor", obj);
101 SGEN_LOG (9, " (tospace, no change)");
102 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);
107 HEAVY_STAT (++stat_objects_copied_nursery);
109 *obj_slot = copy_object_no_checks (obj, queue);
113 PARALLEL_COPY_OBJECT (void **obj_slot, SgenGrayQueue *queue)
115 char *obj = *obj_slot;
116 mword vtable_word, objsize;
119 gboolean has_references;
121 SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-par-copy from a %d generation collection", current_collection_generation);
123 HEAVY_STAT (++stat_copy_object_called_nursery);
125 if (!sgen_ptr_in_nursery (obj)) {
126 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
130 vtable_word = *(mword*)obj;
131 vt = (MonoVTable*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
134 * Before we can copy the object we must make sure that we are
135 * allowed to, i.e. that the object not pinned, not already
136 * forwarded and not in the nursery To Space.
139 if (vtable_word & SGEN_FORWARDED_BIT) {
140 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
144 if (vtable_word & SGEN_PINNED_BIT) {
145 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
149 if (sgen_nursery_is_to_space (obj)) {
150 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);
154 HEAVY_STAT (++stat_objects_copied_nursery);
156 objsize = SGEN_ALIGN_UP (sgen_par_object_get_size (vt, (MonoObject*)obj));
157 has_references = SGEN_VTABLE_HAS_REFERENCES (vt);
159 destination = COLLECTOR_PARALLEL_ALLOC_FOR_PROMOTION (obj, objsize, has_references);
161 if (G_UNLIKELY (!destination)) {
162 sgen_parallel_pin_or_update (obj_slot, obj, vt, queue);
166 *(MonoVTable**)destination = vt;
168 if (SGEN_CAS_PTR ((void*)obj, (void*)((mword)destination | SGEN_FORWARDED_BIT), vt) == vt) {
169 par_copy_object_no_checks (destination, vt, obj, objsize, has_references ? queue : NULL);
173 /* FIXME: unify with code in major_copy_or_mark_object() */
175 /* FIXME: Give destination back to the allocator. */
176 /*The major collector only needs the first word zeroed and nursery requires all bits to be. */
177 if (!sgen_ptr_in_nursery (destination))
178 *(void**)destination = NULL;
180 memset (destination, 0, objsize);
182 vtable_word = *(mword*)obj;
183 g_assert (vtable_word & SGEN_FORWARDED_BIT);
185 obj = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
189 HEAVY_STAT (++stat_slots_allocated_in_vain);
193 #define FILL_MINOR_COLLECTOR_COPY_OBJECT(collector) do { \
194 (collector)->serial_ops.copy_or_mark_object = SERIAL_COPY_OBJECT; \
195 (collector)->parallel_ops.copy_or_mark_object = PARALLEL_COPY_OBJECT; \