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/shenandoahAsserts.hpp"
 34 #include "gc/shenandoah/shenandoahCardTable.hpp"
 35 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 36 #include "gc/shenandoah/shenandoahEvacOOMHandler.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 "gc/shenandoah/mode/shenandoahMode.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   // Prevent resurrection of unreachable objects that are visited during
125   // concurrent class-unloading.
126   if ((decorators & AS_NO_KEEPALIVE) != 0 &&
127       _heap->is_evacuation_in_progress() &&
128       !_heap->marking_context()->is_marked(obj)) {
129     return obj;
130   }
131 
132   oop fwd = load_reference_barrier(obj);
133   if (load_addr != nullptr && fwd != obj) {
134     // Since we are here and we know the load address, update the reference.
135     ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
136   }
137 
138   return fwd;
139 }
140 
141 inline void ShenandoahBarrierSet::enqueue(oop obj) {
142   assert(obj != nullptr, "checked by caller");
143   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
144 
145   // Filter marked objects before hitting the SATB queues. The same predicate would
146   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
147   // filtering here helps to avoid wasteful SATB queueing work to begin with.
148   if (!_heap->requires_marking(obj)) return;
149 
150   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
151   _satb_mark_queue_set.enqueue_known_active(queue, obj);
152 }
153 
154 template <DecoratorSet decorators, typename T>
155 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
156   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
157       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
158     return;
159   }
160   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
161     T heap_oop = RawAccess<>::oop_load(field);
162     if (!CompressedOops::is_null(heap_oop)) {
163       enqueue(CompressedOops::decode(heap_oop));
164     }
165   }
166 }
167 
168 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
169   if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
170     enqueue(value);
171   }
172 }
173 
174 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
175   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
176   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
177   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
178   if (!peek && !on_strong_oop_ref) {
179     satb_enqueue(value);
180   }
181 }
182 
183 template <DecoratorSet decorators, typename T>
184 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
185   assert(ShenandoahCardBarrier, "Should have been checked by caller");
186   volatile CardTable::CardValue* byte = card_table()->byte_for(field);
187   *byte = CardTable::dirty_card_val();
188 }
189 
190 template <typename T>
191 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
192   oop value = RawAccess<>::oop_load(addr);
193   value = load_reference_barrier(decorators, value, addr);
194   keep_alive_if_weak(decorators, value);
195   return value;
196 }
197 
198 template <typename T>
199 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
200   oop res;
201   oop expected = compare_value;
202   do {
203     compare_value = expected;
204     res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
205     expected = res;
206   } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
207 
208   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
209   // because it must be the previous value.
210   res = load_reference_barrier(decorators, res, static_cast<T*>(nullptr));
211   satb_enqueue(res);
212   return res;
213 }
214 
215 template <typename T>
216 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
217   oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);
218   // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
219   // because it must be the previous value.
220   previous = load_reference_barrier<T>(decorators, previous, static_cast<T*>(nullptr));
221   satb_enqueue(previous);
222   return previous;
223 }
224 
225 template <DecoratorSet decorators, typename BarrierSetT>
226 template <typename T>
227 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
228   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
229   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
230   return bs->oop_load(decorators, addr);
231 }
232 
233 template <DecoratorSet decorators, typename BarrierSetT>
234 template <typename T>
235 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
236   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
237   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
238   return bs->oop_load(decorators, addr);
239 }
240 
241 template <DecoratorSet decorators, typename BarrierSetT>
242 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
243   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
244   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
245   return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
246 }
247 
248 template <DecoratorSet decorators, typename BarrierSetT>
249 template <typename T>
250 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
251   shenandoah_assert_marked_if(nullptr, value,
252                               !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
253                               && !(ShenandoahHeap::heap()->active_generation()->is_young()
254                                    && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
255   shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
256   ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
257   bs->satb_barrier<decorators>(addr);
258   Raw::oop_store(addr, value);
259 }
260 
261 template <DecoratorSet decorators, typename BarrierSetT>
262 template <typename T>
263 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
264   oop_store_common(addr, value);
265 }
266 
267 template <DecoratorSet decorators, typename BarrierSetT>
268 template <typename T>
269 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
270   shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
271   shenandoah_assert_not_forwarded_except  (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
272 
273   oop_store_common(addr, value);
274   if (ShenandoahCardBarrier) {
275     ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
276     bs->write_ref_field_post<decorators>(addr);
277   }
278 }
279 
280 template <DecoratorSet decorators, typename BarrierSetT>
281 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
282   oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
283 }
284 
285 template <DecoratorSet decorators, typename BarrierSetT>
286 template <typename T>
287 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
288   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
289   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
290   return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
291 }
292 
293 template <DecoratorSet decorators, typename BarrierSetT>
294 template <typename T>
295 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
296   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
297   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
298   oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
299   if (ShenandoahCardBarrier) {
300     bs->write_ref_field_post<decorators>(addr);
301   }
302   return result;
303 }
304 
305 template <DecoratorSet decorators, typename BarrierSetT>
306 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
307   assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
308   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
309   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
310   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
311   oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
312   if (ShenandoahCardBarrier) {
313     bs->write_ref_field_post<decorators>(addr);
314   }
315   return result;
316 }
317 
318 template <DecoratorSet decorators, typename BarrierSetT>
319 template <typename T>
320 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
321   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
322   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
323   return bs->oop_xchg(decorators, addr, new_value);
324 }
325 
326 template <DecoratorSet decorators, typename BarrierSetT>
327 template <typename T>
328 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
329   assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
330   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
331   oop result = bs->oop_xchg(decorators, addr, new_value);
332   if (ShenandoahCardBarrier) {
333     bs->write_ref_field_post<decorators>(addr);
334   }
335   return result;
336 }
337 
338 template <DecoratorSet decorators, typename BarrierSetT>
339 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
340   assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
341   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
342   DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
343   auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
344   oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
345   if (ShenandoahCardBarrier) {
346     bs->write_ref_field_post<decorators>(addr);
347   }
348   return result;
349 }
350 
351 // Clone barrier support
352 template <DecoratorSet decorators, typename BarrierSetT>
353 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
354   if (ShenandoahCloneBarrier) {
355     ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
356   }
357   Raw::clone(src, dst, size);
358 }
359 
360 template <DecoratorSet decorators, typename BarrierSetT>
361 template <typename T>
362 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
363                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
364                                                                                          size_t length) {
365   T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
366   T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
367 
368   ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
369   bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
370                         arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
371                         length);
372   Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
373   if (ShenandoahCardBarrier) {
374     bs->write_ref_array((HeapWord*) dst, length);
375   }
376 }
377 
378 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
379 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
380   // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
381   // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
382   // may have forwarded objects. In this case, the `arraycopy_work` is first called with HAS_FWD=true and
383   // ENQUEUE=false.
384   assert(HAS_FWD == _heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(),
385          "Forwarded object status is sane");
386   // This function cannot be called to handle marking and evacuation at the same time (they operate on
387   // different sides of the copy).
388   assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
389 
390   Thread* thread = Thread::current();
391   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
392   ShenandoahMarkingContext* ctx = _heap->marking_context();
393   const ShenandoahCollectionSet* const cset = _heap->collection_set();
394   T* end = src + count;
395   for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
396     T o = RawAccess<>::oop_load(elem_ptr);
397     if (!CompressedOops::is_null(o)) {
398       oop obj = CompressedOops::decode_not_null(o);
399       if (HAS_FWD && cset->is_in(obj)) {
400         oop fwd = resolve_forwarded_not_null(obj);
401         if (EVAC && obj == fwd) {
402           fwd = _heap->evacuate_object(obj, thread);
403         }
404         shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
405         ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
406       }
407       if (ENQUEUE && !ctx->is_marked_strong_or_old(obj)) {
408         _satb_mark_queue_set.enqueue_known_active(queue, obj);
409       }
410     }
411   }
412 }
413 
414 template <class T>
415 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
416   if (count == 0) {
417     // No elements to copy, no need for barrier
418     return;
419   }
420 
421   char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
422   if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
423     arraycopy_evacuation(src, count);
424   } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
425     arraycopy_update(src, count);
426   }
427 
428   if (_heap->mode()->is_generational()) {
429     assert(ShenandoahSATBBarrier, "Generational mode assumes SATB mode");
430     if ((gc_state & ShenandoahHeap::YOUNG_MARKING) != 0) {
431       arraycopy_marking(src, dst, count, false);
432     }
433     if ((gc_state & ShenandoahHeap::OLD_MARKING) != 0) {
434       arraycopy_marking(src, dst, count, true);
435     }
436   } else if ((gc_state & ShenandoahHeap::MARKING) != 0) {
437     arraycopy_marking(src, dst, count, false);
438   }
439 }
440 
441 template <class T>
442 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count, bool is_old_marking) {
443   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
444   /*
445    * Note that an old-gen object is considered live if it is live at the start of OLD marking or if it is promoted
446    * following the start of OLD marking.
447    *
448    * 1. Every object promoted following the start of OLD marking will be above TAMS within its old-gen region
449    * 2. Every object live at the start of OLD marking will be referenced from a "root" or it will be referenced from
450    *    another live OLD-gen object.  With regards to old-gen, roots include stack locations and all of live young-gen.
451    *    All root references to old-gen are identified during a bootstrap young collection.  All references from other
452    *    old-gen objects will be marked during the traversal of all old objects, or will be marked by the SATB barrier.
453    *
454    * During old-gen marking (which is interleaved with young-gen collections), call arraycopy_work() if:
455    *
456    * 1. The overwritten array resides in old-gen and it is below TAMS within its old-gen region
457    * 2. Do not call arraycopy_work for any array residing in young-gen because young-gen collection is idle at this time
458    *
459    * During young-gen marking, call arraycopy_work() if:
460    *
461    * 1. The overwritten array resides in young-gen and is below TAMS within its young-gen region
462    * 2. Additionally, if array resides in old-gen, regardless of its relationship to TAMS because this old-gen array
463    *    may hold references to young-gen
464    */
465   if (ShenandoahSATBBarrier) {
466     T* array = dst;
467     HeapWord* array_addr = reinterpret_cast<HeapWord*>(array);
468     ShenandoahHeapRegion* r = _heap->heap_region_containing(array_addr);
469     if (is_old_marking) {
470       // Generational, old marking
471       assert(_heap->mode()->is_generational(), "Invariant");
472       if (r->is_old() && (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
473         arraycopy_work<T, false, false, true>(array, count);
474       }
475     } else if (_heap->mode()->is_generational()) {
476       // Generational, young marking
477       if (r->is_old() || (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
478         arraycopy_work<T, false, false, true>(array, count);
479       }
480     } else if (array_addr < _heap->marking_context()->top_at_mark_start(r)) {
481       // Non-generational, marking
482       arraycopy_work<T, false, false, true>(array, count);
483     }
484   }
485 }
486 
487 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
488   return ary < _heap->heap_region_containing(ary)->get_update_watermark();
489 }
490 
491 template <class T>
492 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
493   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
494   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
495     ShenandoahEvacOOMScope oom_evac;
496     arraycopy_work<T, true, true, false>(src, count);
497   }
498 }
499 
500 template <class T>
501 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
502   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
503   if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
504     arraycopy_work<T, true, false, false>(src, count);
505   }
506 }
507 
508 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP