1 /*
2 * Copyright (c) 2015, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
27 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
28
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30
31 #include "gc/shared/accessBarrierSupport.inline.hpp"
32 #include "gc/shared/cardTable.hpp"
33 #include "gc/shenandoah/mode/shenandoahMode.hpp"
34 #include "gc/shenandoah/shenandoahAsserts.hpp"
35 #include "gc/shenandoah/shenandoahCardTable.hpp"
36 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
37 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
38 #include "gc/shenandoah/shenandoahGeneration.hpp"
39 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
40 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
41 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
42 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
43 #include "memory/iterator.inline.hpp"
44 #include "oops/oop.inline.hpp"
45
46 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
47 return ShenandoahForwarding::get_forwardee(p);
48 }
49
50 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
51 if (p != nullptr) {
52 return resolve_forwarded_not_null(p);
53 } else {
54 return p;
55 }
56 }
57
58 template <DecoratorSet decorators, class T>
59 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
60 assert(ShenandoahLoadRefBarrier, "Should be enabled");
61
62 constexpr bool on_weak = HasDecorator<decorators, ON_WEAK_OOP_REF>::value;
63 constexpr bool on_phantom = HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value;
64
65 // Handle nulls. Strong loads filtered nulls with cset checks.
66 // Weak/phantom loads need to check for nulls here.
67 if (on_weak || on_phantom) {
68 if (obj == nullptr) {
69 return nullptr;
70 }
71 } else {
72 assert(obj != nullptr, "Should have been filtered before");
73 }
74
75 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
76 if (on_phantom &&
77 _heap->is_concurrent_weak_root_in_progress() &&
78 _heap->is_in_active_generation(obj) &&
79 !_heap->marking_context()->is_marked(obj)) {
80 return nullptr;
81 }
82
83 // Prevent resurrection of unreachable weak references.
84 if (on_weak &&
85 _heap->is_concurrent_weak_root_in_progress() &&
86 _heap->is_in_active_generation(obj) &&
87 !_heap->marking_context()->is_marked_strong(obj)) {
88 return nullptr;
89 }
90
91 // Weak/phantom loads need additional cset check.
92 if (on_phantom || on_weak) {
93 if (!_heap->has_forwarded_objects() || !_heap->in_collection_set(obj)) {
94 return obj;
95 }
96 } else {
97 shenandoah_assert_in_cset(load_addr, obj);
98 }
99
100 oop fwd = ShenandoahForwarding::get_forwardee_mutator(obj);
101 if (obj == fwd) {
102 assert(_heap->is_evacuation_in_progress(), "evac should be in progress");
103 Thread* const t = Thread::current();
104 fwd = _heap->evacuate_object(obj, t);
105 }
106
107 if (load_addr != nullptr && fwd != obj) {
108 // Since we are here and we know the load address, update the reference.
109 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
110 }
111
112 return fwd;
113 }
114
115 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
116 if (!ShenandoahLoadRefBarrier) {
117 return obj;
118 }
119 if (_heap->has_forwarded_objects() && _heap->in_collection_set(obj)) {
120 // Subsumes null-check
121 assert(obj != nullptr, "cset check must have subsumed null-check");
122 oop fwd = resolve_forwarded_not_null(obj);
123 if (obj == fwd && _heap->is_evacuation_in_progress()) {
124 Thread* t = Thread::current();
125 return _heap->evacuate_object(obj, t);
126 }
127 return fwd;
128 }
129 return obj;
130 }
131
132 template <class T>
133 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
134 if (obj == nullptr) {
135 return nullptr;
136 }
137
138 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
139 if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
140 _heap->is_concurrent_weak_root_in_progress() &&
141 _heap->is_in_active_generation(obj) &&
142 !_heap->marking_context()->is_marked(obj)) {
143 return nullptr;
144 }
145
146 // Prevent resurrection of unreachable weak references.
147 if ((decorators & ON_WEAK_OOP_REF) != 0 &&
148 _heap->is_concurrent_weak_root_in_progress() &&
149 _heap->is_in_active_generation(obj) &&
150 !_heap->marking_context()->is_marked_strong(obj)) {
151 return nullptr;
152 }
153
154 // Allow runtime to see unreachable objects that are visited during concurrent class-unloading.
155 if ((decorators & AS_NO_KEEPALIVE) != 0 &&
156 _heap->is_concurrent_weak_root_in_progress() &&
157 !_heap->marking_context()->is_marked(obj)) {
158 return obj;
159 }
160
161 oop fwd = load_reference_barrier(obj);
162 if (load_addr != nullptr && fwd != obj) {
163 // Since we are here and we know the load address, update the reference.
164 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
165 }
166
167 return fwd;
168 }
169
170 inline void ShenandoahBarrierSet::enqueue(oop obj, bool filter) {
171 assert(obj != nullptr, "checked by caller");
172 shenandoah_assert_correct(nullptr, obj);
173 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
174
175 // Filter marked objects before hitting the SATB queues. The same predicate would
176 // be used by SATBMQ::filter to eliminate already marked objects downstream, but
177 // filtering here helps to avoid wasteful SATB queueing work to begin with.
178 if (filter && !_heap->requires_marking(obj)) return;
179
180 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
181 _satb_mark_queue_set.enqueue_known_active(queue, obj);
182 }
183
184 template <DecoratorSet decorators, typename T>
185 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
186 // Uninitialized and no-keepalive stores do not need barrier.
187 if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
188 HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
189 return;
190 }
191
192 // Stores to weak/phantom require no barrier. The original references would
193 // have been enqueued in the SATB buffer by the load barrier if they were needed.
194 if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value ||
195 HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value) {
196 return;
197 }
198
199 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
200 T heap_oop = RawAccess<>::oop_load(field);
201 if (!CompressedOops::is_null(heap_oop)) {
202 enqueue(CompressedOops::decode(heap_oop));
203 }
204 }
205 }
206
207 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
208 if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
209 enqueue(value);
210 }
211 }
212
213 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
214 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
215 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
216 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
217 if (!peek && !on_strong_oop_ref) {
218 satb_enqueue(value);
219 }
220 }
221
222 template <DecoratorSet decorators, typename T>
223 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
224 assert(ShenandoahCardBarrier, "Should have been checked by caller");
225 if (_heap->is_in_young(field)) {
226 // Young field stores do not require card mark.
227 return;
228 }
229 T heap_oop = RawAccess<>::oop_load(field);
230 if (CompressedOops::is_null(heap_oop)) {
231 // Null reference store do not require card mark.
232 return;
233 }
234 oop obj = CompressedOops::decode_not_null(heap_oop);
235 if (!_heap->is_in_young(obj)) {
236 // Not an old->young reference store.
237 return;
238 }
239 volatile CardTable::CardValue* byte = card_table()->byte_for(field);
240 *byte = CardTable::dirty_card_val();
241 }
242
243 template <typename T>
244 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
245 oop value = RawAccess<>::oop_load(addr);
246 value = load_reference_barrier(decorators, value, addr);
247 keep_alive_if_weak(decorators, value);
248 return value;
249 }
250
251 template <typename T>
252 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
253 oop res;
254 oop expected = compare_value;
255 do {
256 compare_value = expected;
257 res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
258 expected = res;
259 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
260
261 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
262 // because it must be the previous value.
263 res = load_reference_barrier(decorators, res, static_cast<T*>(nullptr));
264 satb_enqueue(res);
265 return res;
266 }
267
268 template <typename T>
269 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
270 oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);
271 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
272 // because it must be the previous value.
273 previous = load_reference_barrier<T>(decorators, previous, static_cast<T*>(nullptr));
274 satb_enqueue(previous);
275 return previous;
276 }
277
278 template <DecoratorSet decorators, typename BarrierSetT>
279 template <typename T>
280 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
281 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
282 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
283 return bs->oop_load(decorators, addr);
284 }
285
286 template <DecoratorSet decorators, typename BarrierSetT>
287 template <typename T>
288 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
289 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
290 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
291 return bs->oop_load(decorators, addr);
292 }
293
294 template <DecoratorSet decorators, typename BarrierSetT>
295 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
296 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
297 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
298 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
299 }
300
301 template <DecoratorSet decorators, typename BarrierSetT>
302 template <typename T>
303 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
304 shenandoah_assert_marked_if(nullptr, value,
305 !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
306 && !(ShenandoahHeap::heap()->active_generation()->is_young()
307 && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
308 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
309 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
310 bs->satb_barrier<decorators>(addr);
311 Raw::oop_store(addr, value);
312 }
313
314 template <DecoratorSet decorators, typename BarrierSetT>
315 template <typename T>
316 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
317 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
318 oop_store_common(addr, value);
319 }
320
321 template <DecoratorSet decorators, typename BarrierSetT>
322 template <typename T>
323 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
324 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
325 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
326
327 oop_store_common(addr, value);
328 if (ShenandoahCardBarrier) {
329 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
330 bs->write_ref_field_post<decorators>(addr);
331 }
332 }
333
334 template <DecoratorSet decorators, typename BarrierSetT>
335 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
336 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
337 }
338
339 template <DecoratorSet decorators, typename BarrierSetT>
340 template <typename T>
341 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
342 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
343 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
344 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
345 }
346
347 template <DecoratorSet decorators, typename BarrierSetT>
348 template <typename T>
349 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
350 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
351 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
352 oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
353 if (ShenandoahCardBarrier) {
354 bs->write_ref_field_post<decorators>(addr);
355 }
356 return result;
357 }
358
359 template <DecoratorSet decorators, typename BarrierSetT>
360 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
361 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
362 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
363 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
364 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
365 oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
366 if (ShenandoahCardBarrier) {
367 bs->write_ref_field_post<decorators>(addr);
368 }
369 return result;
370 }
371
372 template <DecoratorSet decorators, typename BarrierSetT>
373 template <typename T>
374 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
375 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
376 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
377 return bs->oop_xchg(decorators, addr, new_value);
378 }
379
380 template <DecoratorSet decorators, typename BarrierSetT>
381 template <typename T>
382 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
383 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
384 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
385 oop result = bs->oop_xchg(decorators, addr, new_value);
386 if (ShenandoahCardBarrier) {
387 bs->write_ref_field_post<decorators>(addr);
388 }
389 return result;
390 }
391
392 template <DecoratorSet decorators, typename BarrierSetT>
393 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
394 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
395 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
396 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
397 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
398 oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
399 if (ShenandoahCardBarrier) {
400 bs->write_ref_field_post<decorators>(addr);
401 }
402 return result;
403 }
404
405 // Clone barrier support
406 template <bool EVAC>
407 class ShenandoahUpdateEvacForCloneOopClosure : public BasicOopIterateClosure {
408 private:
409 ShenandoahHeap* const _heap;
410 const ShenandoahCollectionSet* const _cset;
411 Thread* const _thread;
412
413 template <class T>
414 inline void do_oop_work(T* p) {
415 T o = RawAccess<>::oop_load(p);
416 if (!CompressedOops::is_null(o)) {
417 oop obj = CompressedOops::decode_not_null(o);
418 if (_cset->is_in(obj)) {
419 oop fwd = ShenandoahForwarding::get_forwardee(obj);
420 if (EVAC && obj == fwd) {
421 fwd = _heap->evacuate_object(obj, _thread);
422 }
423 shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
424 ShenandoahHeap::atomic_update_oop(fwd, p, o);
425 obj = fwd;
426 }
427 }
428 }
429
430 public:
431 ShenandoahUpdateEvacForCloneOopClosure() :
432 _heap(ShenandoahHeap::heap()),
433 _cset(_heap->collection_set()),
434 _thread(Thread::current()) {}
435
436 virtual void do_oop(oop* p) { do_oop_work(p); }
437 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
438 };
439
440 void ShenandoahBarrierSet::clone_evacuation(oop obj) {
441 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
442 if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
443 ShenandoahUpdateEvacForCloneOopClosure<true> cl;
444 obj->oop_iterate(&cl);
445 }
446 }
447
448 void ShenandoahBarrierSet::clone_update(oop obj) {
449 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
450 if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
451 ShenandoahUpdateEvacForCloneOopClosure<false> cl;
452 obj->oop_iterate(&cl);
453 }
454 }
455
456 template <DecoratorSet decorators, typename BarrierSetT>
457 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t count) {
458 // Hot code path, called from compiler/runtime. Make sure fast path is fast.
459
460 // Fix up src before doing the copy, if needed.
461 const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
462 if (gc_state != 0 && ShenandoahCloneBarrier) {
463 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
464 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
465 bs->clone_evacuation(src);
466 } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
467 bs->clone_update(src);
468 }
469 }
470
471 Raw::clone(src, dst, count);
472
473 // Current allocator never allocates in old, so clone destination is guaranteed to be in young.
474 // Otherwise we need card barriers.
475 shenandoah_assert_in_young_if(nullptr, dst, ShenandoahCardBarrier);
476 }
477
478 template <DecoratorSet decorators, typename BarrierSetT>
479 template <typename T>
480 OopCopyResult ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
481 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
482 size_t length) {
483 T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
484 T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
485
486 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
487 bs->arraycopy_barrier(src, dst, length);
488 OopCopyResult result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
489 if (ShenandoahCardBarrier) {
490 bs->write_ref_array((HeapWord*) dst, length);
491 }
492 return result;
493 }
494
495 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
496 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
497 // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
498 // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
499 // may have forwarded objects.
500 assert(HAS_FWD == _heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(), "Forwarded object status is sane");
501 // This function cannot be called to handle marking and evacuation at the same time (they operate on
502 // different sides of the copy).
503 static_assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
504
505 Thread* thread = Thread::current();
506 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
507 ShenandoahMarkingContext* ctx = _heap->marking_context();
508 const ShenandoahCollectionSet* const cset = _heap->collection_set();
509 T* end = src + count;
510 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
511 T o = RawAccess<>::oop_load(elem_ptr);
512 if (!CompressedOops::is_null(o)) {
513 oop obj = CompressedOops::decode_not_null(o);
514 if (HAS_FWD && cset->is_in(obj)) {
515 oop fwd = resolve_forwarded_not_null(obj);
516 if (EVAC && obj == fwd) {
517 fwd = _heap->evacuate_object(obj, thread);
518 }
519 shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
520 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
521 }
522 if (ENQUEUE && !ctx->is_marked_strong(obj)) {
523 _satb_mark_queue_set.enqueue_known_active(queue, obj);
524 }
525 }
526 }
527 }
528
529 template <class T>
530 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
531 if (count == 0) {
532 // No elements to copy, no need for barrier
533 return;
534 }
535
536 const char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
537 if ((gc_state & ShenandoahHeap::MARKING) != 0) {
538 // If marking old or young, we must evaluate the SATB barrier. This will be the only
539 // action if we are not marking old. If we are marking old, we must still evaluate the
540 // load reference barrier for a young collection.
541 if (_heap->mode()->is_generational()) {
542 arraycopy_marking<true>(dst, count);
543 } else {
544 arraycopy_marking<false>(dst, count);
545 }
546 }
547
548 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
549 assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during evacuation");
550 arraycopy_evacuation(src, count);
551 } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
552 assert((gc_state & ShenandoahHeap::YOUNG_MARKING) == 0, "Cannot be marking young during update-refs");
553 arraycopy_update(src, count);
554 }
555 }
556
557 template <bool IS_GENERATIONAL, class T>
558 void ShenandoahBarrierSet::arraycopy_marking(T* dst, size_t count) {
559 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
560 if (ShenandoahSATBBarrier) {
561 if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(dst)) ||
562 (IS_GENERATIONAL && _heap->heap_region_containing(dst)->is_old() && _heap->is_concurrent_young_mark_in_progress())) {
563 arraycopy_work<T, false, false, true>(dst, count);
564 }
565 }
566 }
567
568 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
569 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
570 }
571
572 template <class T>
573 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
574 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
575 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
576 arraycopy_work<T, true, true, false>(src, count);
577 }
578 }
579
580 template <class T>
581 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
582 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
583 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
584 arraycopy_work<T, true, false, false>(src, count);
585 }
586 }
587
588 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP