少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * \file
 * Copy functions for nursery collections.
 *
 * Copyright 2001-2003 Ximian, Inc
 * Copyright 2003-2010 Novell, Inc.
 * Copyright (C) 2012 Xamarin Inc
 *
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
 
#undef SERIAL_COPY_OBJECT
#undef SERIAL_COPY_OBJECT_FROM_OBJ
 
#if defined(SGEN_SIMPLE_NURSERY)
 
#ifdef SGEN_SIMPLE_PAR_NURSERY
 
#ifdef SGEN_CONCURRENT_MAJOR
#define SERIAL_COPY_OBJECT simple_par_nursery_with_concurrent_major_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_with_concurrent_major_copy_object_from_obj
#else
#define SERIAL_COPY_OBJECT simple_par_nursery_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ simple_par_nursery_copy_object_from_obj
#endif
 
#else
 
#ifdef SGEN_CONCURRENT_MAJOR
#define SERIAL_COPY_OBJECT simple_nursery_serial_with_concurrent_major_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_with_concurrent_major_copy_object_from_obj
#else
#define SERIAL_COPY_OBJECT simple_nursery_serial_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ simple_nursery_serial_copy_object_from_obj
#endif
 
#endif
 
#elif defined (SGEN_SPLIT_NURSERY)
 
#ifdef SGEN_CONCURRENT_MAJOR
#define SERIAL_COPY_OBJECT split_nursery_serial_with_concurrent_major_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_with_concurrent_major_copy_object_from_obj
#else
#define SERIAL_COPY_OBJECT split_nursery_serial_copy_object
#define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_copy_object_from_obj
#endif
 
#else
#error "No nursery configuration specified"
#endif
 
 
extern guint64 stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
 
/*
 * This is how the copying happens from the nursery to the old generation.
 * We assume that at this time all the pinned objects have been identified and
 * marked as such.
 * We run scan_object() for each pinned object so that each referenced
 * objects if possible are copied. The new gray objects created can have
 * scan_object() run on them right away, too.
 * Then we run copy_object() for the precisely tracked roots. At this point
 * all the roots are either gray or black. We run scan_object() on the gray
 * objects until no more gray objects are created.
 * At the end of the process we walk again the pinned list and we unmark
 * the pinned flag. As we go we also create the list of free space for use
 * in the next allocation runs.
 *
 * We need to remember objects from the old generation that point to the new one
 * (or just addresses?).
 *
 * copy_object could be made into a macro once debugged (use inline for now).
 */
 
static MONO_ALWAYS_INLINE void
SERIAL_COPY_OBJECT (GCObject **obj_slot, SgenGrayQueue *queue) 
{
    GCObject *forwarded;
    GCObject *copy;
    GCObject *obj = *obj_slot;
 
    SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
 
    HEAVY_STAT (++stat_copy_object_called_nursery);
 
    if (!sgen_ptr_in_nursery (obj)) {
        HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
        return;
    }
 
    SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
 
    /*
     * Before we can copy the object we must make sure that we are
     * allowed to, i.e. that the object not pinned, not already
     * forwarded or belongs to the nursery To Space.
     */
 
    if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
        SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
        SGEN_LOG (9, " (already forwarded to %p)", forwarded);
        HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
        SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
        return;
    }
    if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
        SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
        SGEN_LOG (9, " (pinned, no change)");
        HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
        return;
    }
 
#ifndef SGEN_SIMPLE_NURSERY
    if (sgen_nursery_is_to_space (obj)) {
        SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
        SGEN_LOG (9, " (tospace, no change)");
        HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);        
        return;
    }
#endif
 
    HEAVY_STAT (++stat_objects_copied_nursery);
 
#ifdef SGEN_SIMPLE_PAR_NURSERY
    copy = copy_object_no_checks_par (obj, queue);
#else
    copy = copy_object_no_checks (obj, queue);
#endif
    SGEN_UPDATE_REFERENCE (obj_slot, copy);
}
 
/*
 * SERIAL_COPY_OBJECT_FROM_OBJ:
 *
 *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
 */
static MONO_ALWAYS_INLINE void
SERIAL_COPY_OBJECT_FROM_OBJ (GCObject **obj_slot, SgenGrayQueue *queue)
{
    GCObject *forwarded;
    GCObject *obj = *obj_slot;
    GCObject *copy;
 
    SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
 
    HEAVY_STAT (++stat_copy_object_called_nursery);
 
    if (!sgen_ptr_in_nursery (obj)) {
        HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
        return;
    }
 
    SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
 
    /*
     * Before we can copy the object we must make sure that we are
     * allowed to, i.e. that the object not pinned, not already
     * forwarded or belongs to the nursery To Space.
     */
 
    if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
        SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
        SGEN_LOG (9, " (already forwarded to %p)", forwarded);
        HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
#ifdef SGEN_CONCURRENT_MAJOR
        /* See comment on STORE_STORE_FENCE below. */
        STORE_STORE_FENCE;
#endif
        SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
#ifndef SGEN_SIMPLE_NURSERY
        if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
            sgen_add_to_global_remset (obj_slot, forwarded);
#endif
        return;
    }
    if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
        SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
        SGEN_LOG (9, " (pinned, no change)");
        HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
        if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
            sgen_add_to_global_remset (obj_slot, obj);
        return;
    }
 
#ifndef SGEN_SIMPLE_NURSERY
    if (sgen_nursery_is_to_space (obj)) {
        /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
        SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
        SGEN_LOG (9, " (tospace, no change)");
        HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);        
 
        /*
         * FIXME:
         *
         * The card table scanning code sometimes clears cards
         * that have just been set for a global remset.  In
         * the split nursery the following situation can
         * occur:
         *
         * Let's say object A starts in card C but continues
         * into C+1.  Within A, at offset O there's a
         * reference to a new nursery object X.  A+O is in
         * card C+1.  Now card C is scanned, and as part of
         * it, object A.  The reference at A+O is processed by
         * copying X into nursery to-space at Y.  Since it's
         * still in the nursery, a global remset must be added
         * for A+O, so card C+1 is marked.  Now, however, card
         * C+1 is scanned, which means that it's cleared
         * first.  This wouldn't be terribly bad if reference
         * A+O were re-scanned and the global remset re-added,
         * but since the reference points to to-space, that
         * doesn't happen, and C+1 remains cleared: the remset
         * is lost.
         *
         * There's at least two ways to fix this.  The easy
         * one is to re-add the remset on the re-scan.  This
         * is that - the following two lines of code.
         *
         * The proper solution appears to be to first make a
         * copy of the cards before scanning a block, then to
         * clear all the cards and scan from the copy, so no
         * remsets will be overwritten.  Scanning objects at
         * most once would be the icing on the cake.
         */
        if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
            sgen_add_to_global_remset (obj_slot, obj);
 
        return;
    }
#endif
 
    HEAVY_STAT (++stat_objects_copied_nursery);
 
#ifdef SGEN_SIMPLE_PAR_NURSERY
    copy = copy_object_no_checks_par (obj, queue);
#else
    copy = copy_object_no_checks (obj, queue);
#endif
#ifdef SGEN_CONCURRENT_MAJOR
    /*
     * If an object is evacuated to the major heap and a reference to it, from the major
     * heap, updated, the concurrent major collector might follow that reference and
     * scan the new major object.  To make sure the object contents are seen by the
     * major collector we need this write barrier, so that the reference is seen after
     * the object.
     */
    STORE_STORE_FENCE;
#endif
    SGEN_UPDATE_REFERENCE (obj_slot, copy);
#ifndef SGEN_SIMPLE_NURSERY
    if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
        sgen_add_to_global_remset (obj_slot, copy);
#else
    /* copy_object_no_checks () can return obj on OOM */
    if (G_UNLIKELY (obj == copy)) {
        if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
            sgen_add_to_global_remset (obj_slot, copy);
    }
#endif
}