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