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