1 /*
   2  * Copyright (c) 2013, 2019, 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 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  29 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "memory/iterator.hpp"
  32 #include "oops/access.hpp"
  33 #include "oops/compressedOops.hpp"
  34 
  35 template <bool HAS_FWD, bool EVAC, bool ENQUEUE>
  36 class ShenandoahUpdateRefsForOopClosure: public BasicOopIterateClosure {
  37 private:
  38   ShenandoahHeap* const _heap;
  39   ShenandoahBarrierSet* const _bs;
  40   const ShenandoahCollectionSet* const _cset;
  41   Thread* const _thread;
  42 
  43   template <class T>
  44   inline void do_oop_work(T* p) {
  45     T o = RawAccess<>::oop_load(p);
  46     if (!CompressedOops::is_null(o)) {
  47       oop obj = CompressedOops::decode_not_null(o);
  48       if (HAS_FWD && _cset->is_in(obj)) {
  49         oop fwd = _bs->resolve_forwarded_not_null(obj);
  50         if (EVAC && obj == fwd) {
  51           fwd = _heap->evacuate_object(obj, _thread);
  52         }
  53         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
  54         ShenandoahHeap::cas_oop(fwd, p, o);
  55         obj = fwd;
  56       }
  57       if (ENQUEUE) {
  58         _bs->enqueue(obj);
  59       }
  60     }
  61   }
  62 public:
  63   ShenandoahUpdateRefsForOopClosure() :
  64           _heap(ShenandoahHeap::heap()),
  65           _bs(ShenandoahBarrierSet::barrier_set()),
  66           _cset(_heap->collection_set()),
  67           _thread(Thread::current()) {
  68   }
  69 
  70   virtual void do_oop(oop* p)       { do_oop_work(p); }
  71   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
  72 };
  73 
  74 void ShenandoahBarrierSet::clone_marking(oop obj) {
  75   assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
  76   assert(ShenandoahStoreValEnqueueBarrier, "only with incremental-update");
  77   if (!_heap->marking_context()->allocated_after_mark_start(obj)) {
  78     ShenandoahUpdateRefsForOopClosure</* has_fwd = */ false, /* evac = */ false, /* enqueue */ true> cl;
  79     obj->oop_iterate(&cl);
  80   }
  81 }
  82 
  83 void ShenandoahBarrierSet::clone_evacuation(oop obj) {
  84   assert(_heap->is_evacuation_in_progress(), "only during evacuation");
  85   if (need_bulk_update(cast_from_oop<HeapWord*>(obj))) {
  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