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/shenandoah/shenandoahBarrierSet.inline.hpp"
  28 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
  29 #include "gc/shenandoah/shenandoahEvacOOMHandler.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 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 (_cset->is_in((HeapWord *)obj)) {
  49         oop fwd = _bs->resolve_forwarded_not_null(obj);
  50         if (EVAC && obj == fwd) {
  51           fwd = _heap->evacuate_object(obj, _thread);
  52         }
  53         if (ENQUEUE) {
  54           _bs->enqueue(fwd);
  55         }
  56         assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
  57         ShenandoahHeap::cas_oop(fwd, p, o);
  58       }
  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_barrier(oop obj) {
  75   assert(ShenandoahCloneBarrier, "only get here with clone barriers enabled");
  76   if (!_heap->has_forwarded_objects()) return;
  77 
  78   // This is called for cloning an object (see jvm.cpp) after the clone
  79   // has been made. We are not interested in any 'previous value' because
  80   // it would be NULL in any case. But we *are* interested in any oop*
  81   // that potentially need to be updated.
  82 
  83   shenandoah_assert_correct(NULL, obj);
  84   if (_heap->is_evacuation_in_progress()) {
  85     ShenandoahEvacOOMScope evac_scope;
  86     ShenandoahUpdateRefsForOopClosure</* evac = */ true, /* enqueue */ false> cl;
  87     obj->oop_iterate(&cl);
  88   } else if (_heap->is_concurrent_traversal_in_progress()) {
  89     ShenandoahEvacOOMScope evac_scope;
  90     ShenandoahUpdateRefsForOopClosure</* evac = */ true, /* enqueue */ true> cl;
  91     obj->oop_iterate(&cl);
  92   } else {
  93     ShenandoahUpdateRefsForOopClosure</* evac = */ false, /* enqueue */ false> cl;
  94     obj->oop_iterate(&cl);
  95   }
  96 }
  97 
  98 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSETCLONE_INLINE_HPP