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