1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSETCLONE_INLINE_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSETCLONE_INLINE_HPP
 27 
 28 // No shenandoahBarrierSetClone.hpp
 29 
 30 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 31 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
 32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 33 #include "memory/iterator.inline.hpp"
 34 #include "oops/access.hpp"
 35 #include "oops/compressedOops.hpp"
 36 
 37 template <bool HAS_FWD, bool EVAC, bool ENQUEUE>
 38 class ShenandoahUpdateRefsForOopClosure: public BasicOopIterateClosure {
 39 private:
 40   ShenandoahHeap* const _heap;
 41   ShenandoahBarrierSet* const _bs;
 42   const ShenandoahCollectionSet* const _cset;
 43   Thread* const _thread;
 44 
 45   template <class T>
 46   inline void do_oop_work(T* p) {
 47     T o = RawAccess<>::oop_load(p);
 48     if (!CompressedOops::is_null(o)) {
 49       oop obj = CompressedOops::decode_not_null(o);
 50       if (HAS_FWD && _cset->is_in(obj)) {
 51         oop fwd = _bs->resolve_forwarded_not_null(obj);
 52         if (EVAC && obj == fwd) {
 53           fwd = _heap->evacuate_object(obj, _thread);
 54         }
 55         shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
 56         ShenandoahHeap::atomic_update_oop(fwd, p, o);
 57         obj = fwd;
 58       }
 59       if (ENQUEUE) {
 60         _bs->enqueue(obj);
 61       }
 62     }
 63   }
 64 public:
 65   ShenandoahUpdateRefsForOopClosure() :
 66           _heap(ShenandoahHeap::heap()),
 67           _bs(ShenandoahBarrierSet::barrier_set()),
 68           _cset(_heap->collection_set()),
 69           _thread(Thread::current()) {
 70   }
 71 
 72   virtual void do_oop(oop* p)       { do_oop_work(p); }
 73   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 74 };
 75 
 76 void ShenandoahBarrierSet::clone_evacuation(oop obj) {
 77   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
 78   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
 79     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ true, /* evac = */ true, /* enqueue */ false> cl;
 80     obj->oop_iterate(&cl);
 81   }
 82 }
 83 
 84 void ShenandoahBarrierSet::clone_update(oop obj) {
 85   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
 86   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
 87     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ true, /* evac = */ false, /* enqueue */ false> cl;
 88     obj->oop_iterate(&cl);
 89   }
 90 }
 91 
 92 void ShenandoahBarrierSet::clone_barrier(oop obj) {
 93   assert(ShenandoahCloneBarrier, "only get here with clone barriers enabled");
 94   shenandoah_assert_correct(nullptr, obj);
 95 
 96   if (_heap->is_evacuation_in_progress()) {
 97     clone_evacuation(obj);
 98   } else {
 99     clone_update(obj);
100   }
101 }
102 
103 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSETCLONE_INLINE_HPP