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/shenandoahEvacOOMHandler.hpp"
 33 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 34 #include "memory/iterator.inline.hpp"
 35 #include "oops/access.hpp"
 36 #include "oops/compressedOops.hpp"
 37 
 38 template <bool HAS_FWD, bool EVAC, bool ENQUEUE>
 39 class ShenandoahUpdateRefsForOopClosure: public BasicOopIterateClosure {
 40 private:
 41   ShenandoahHeap* const _heap;
 42   ShenandoahBarrierSet* const _bs;
 43   const ShenandoahCollectionSet* const _cset;
 44   Thread* const _thread;
 45 
 46   template <class T>
 47   inline void do_oop_work(T* p) {
 48     T o = RawAccess<>::oop_load(p);
 49     if (!CompressedOops::is_null(o)) {
 50       oop obj = CompressedOops::decode_not_null(o);
 51       if (HAS_FWD && _cset->is_in(obj)) {
 52         oop fwd = _bs->resolve_forwarded_not_null(obj);
 53         if (EVAC && obj == fwd) {
 54           fwd = _heap->evacuate_object(obj, _thread);
 55         }
 56         shenandoah_assert_forwarded_except(p, obj, _heap->cancelled_gc());
 57         ShenandoahHeap::atomic_update_oop(fwd, p, o);
 58         obj = fwd;
 59       }
 60       if (ENQUEUE) {
 61         _bs->enqueue(obj);
 62       }
 63     }
 64   }
 65 public:
 66   ShenandoahUpdateRefsForOopClosure() :
 67           _heap(ShenandoahHeap::heap()),
 68           _bs(ShenandoahBarrierSet::barrier_set()),
 69           _cset(_heap->collection_set()),
 70           _thread(Thread::current()) {
 71   }
 72 
 73   virtual void do_oop(oop* p)       { do_oop_work(p); }
 74   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 75 };
 76 
 77 void ShenandoahBarrierSet::clone_marking(oop obj) {
 78   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
 79   assert(ShenandoahIUBarrier, "only with incremental-update");
 80   if (!_heap->marking_context()->allocated_after_mark_start(obj)) {
 81     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ false, /* evac = */ false, /* enqueue */ true> cl;
 82     obj->oop_iterate(&cl);
 83   }
 84 }
 85 
 86 void ShenandoahBarrierSet::clone_evacuation(oop obj) {
 87   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
 88   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
 89     ShenandoahEvacOOMScope oom_evac_scope;
 90     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ true, /* evac = */ true, /* enqueue */ false> cl;
 91     obj->oop_iterate(&cl);
 92   }
 93 }
 94 
 95 void ShenandoahBarrierSet::clone_update(oop obj) {
 96   assert(_heap->is_update_refs_in_progress(), "only during update-refs");
 97   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
 98     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ true, /* evac = */ false, /* enqueue */ false> cl;
 99     obj->oop_iterate(&cl);
100   }
101 }
102 
103 void ShenandoahBarrierSet::clone_barrier(oop obj) {
104   assert(ShenandoahCloneBarrier, "only get here with clone barriers enabled");
105   shenandoah_assert_correct(nullptr, obj);
106 
107   // We only need to handle YOUNG_MARKING here because the clone barrier
108   // is only invoked during marking if Shenandoah is in incremental update
109   // mode. OLD_MARKING should only happen when Shenandoah is in generational
110   // mode, which uses the SATB write barrier.
111   int gc_state = _heap->gc_state();
112   if ((gc_state & ShenandoahHeap::YOUNG_MARKING) != 0) {
113     clone_marking(obj);
114   } else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
115     clone_evacuation(obj);
116   } else {
117     clone_update(obj);
118   }
119 }
120 
121 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSETCLONE_INLINE_HPP
--- EOF ---