1 /*
   2  * Copyright (c) 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
  24 #define SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
  25 
  26 #include "gc/shared/barrierSetNMethod.hpp"
  27 #include "gc/shenandoah/shenandoahAsserts.hpp"
  28 #include "gc/shenandoah/shenandoahClosures.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
  31 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  32 #include "oops/compressedOops.inline.hpp"
  33 #include "runtime/atomic.hpp"
  34 #include "runtime/thread.hpp"
  35 
  36 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() :
  37   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  38 }
  39 
  40 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) {
  41   if (CompressedOops::is_null(obj)) {
  42     return false;
  43   }
  44   obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  45   shenandoah_assert_not_forwarded_if(NULL, obj,
  46                                      (ShenandoahHeap::heap()->is_concurrent_mark_in_progress() ||
  47                                      ShenandoahHeap::heap()->is_concurrent_traversal_in_progress()));
  48   return _mark_context->is_marked(obj);
  49 }
  50 
  51 ShenandoahIsAliveClosure::ShenandoahIsAliveClosure() :
  52   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  53 }
  54 
  55 bool ShenandoahIsAliveClosure::do_object_b(oop obj) {
  56   if (CompressedOops::is_null(obj)) {
  57     return false;
  58   }
  59   shenandoah_assert_not_forwarded(NULL, obj);
  60   return _mark_context->is_marked(obj);
  61 }
  62 
  63 BoolObjectClosure* ShenandoahIsAliveSelector::is_alive_closure() {
  64   return ShenandoahHeap::heap()->has_forwarded_objects() ?
  65          reinterpret_cast<BoolObjectClosure*>(&_fwd_alive_cl) :
  66          reinterpret_cast<BoolObjectClosure*>(&_alive_cl);
  67 }
  68 
  69 ShenandoahUpdateRefsClosure::ShenandoahUpdateRefsClosure() :
  70   _heap(ShenandoahHeap::heap()) {
  71 }
  72 
  73 template <class T>
  74 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  75   T o = RawAccess<>::oop_load(p);
  76   if (!CompressedOops::is_null(o)) {
  77     oop obj = CompressedOops::decode_not_null(o);
  78     _heap->update_with_forwarded_not_null(p, obj);
  79   }
  80 }
  81 
  82 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  83 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  84 
  85 ShenandoahTraversalUpdateRefsClosure::ShenandoahTraversalUpdateRefsClosure() :
  86   _heap(ShenandoahHeap::heap()),
  87   _traversal_set(ShenandoahHeap::heap()->traversal_gc()->traversal_set()) {
  88   assert(_heap->is_traversal_mode(), "Why we here?");
  89 }
  90 
  91 template <class T>
  92 void ShenandoahTraversalUpdateRefsClosure::do_oop_work(T* p) {
  93   T o = RawAccess<>::oop_load(p);
  94   if (!CompressedOops::is_null(o)) {
  95     oop obj = CompressedOops::decode_not_null(o);
  96     if (_heap->in_collection_set(obj) || _traversal_set->is_in((HeapWord*)obj)) {
  97       obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  98       RawAccess<IS_NOT_NULL>::oop_store(p, obj);
  99     } else {
 100       shenandoah_assert_not_forwarded(p, obj);
 101     }
 102   }
 103 }
 104 
 105 void ShenandoahTraversalUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
 106 void ShenandoahTraversalUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 107 
 108 ShenandoahEvacuateUpdateRootsClosure::ShenandoahEvacuateUpdateRootsClosure() :
 109   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
 110 }
 111 
 112 template <class T>
 113 void ShenandoahEvacuateUpdateRootsClosure::do_oop_work(T* p) {
 114   assert(_heap->is_concurrent_root_in_progress(), "Only do this when evacuation is in progress");
 115 
 116   T o = RawAccess<>::oop_load(p);
 117   if (! CompressedOops::is_null(o)) {
 118     oop obj = CompressedOops::decode_not_null(o);
 119     if (_heap->in_collection_set(obj)) {
 120       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 121       shenandoah_assert_marked(p, obj);
 122       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 123       if (resolved == obj) {
 124         resolved = _heap->evacuate_object(obj, _thread);
 125       }
 126       RawAccess<IS_NOT_NULL>::oop_store(p, resolved);
 127     }
 128   }
 129 }
 130 void ShenandoahEvacuateUpdateRootsClosure::do_oop(oop* p) {
 131   do_oop_work(p);
 132 }
 133 
 134 void ShenandoahEvacuateUpdateRootsClosure::do_oop(narrowOop* p) {
 135   do_oop_work(p);
 136 }
 137 
 138 ShenandoahEvacUpdateCleanupRootsClosure::ShenandoahEvacUpdateCleanupRootsClosure() :
 139   _heap(ShenandoahHeap::heap()), _thread(Thread::current()), _context(_heap->complete_marking_context()) {
 140 }
 141 
 142 void ShenandoahEvacUpdateCleanupRootsClosure::do_oop(oop* p) {
 143   assert(_heap->is_concurrent_root_in_progress(), "Only do this when evacuation is in progress");
 144 
 145   oop obj = RawAccess<>::oop_load(p);
 146   if (! CompressedOops::is_null(obj)) {
 147     if (!_context->is_marked(obj)) {
 148       Atomic::cmpxchg(p, obj, (oop)NULL);
 149     } else if (_heap->in_collection_set(obj)) {
 150       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 151       shenandoah_assert_marked(p, obj);
 152       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 153       if (resolved == obj) {
 154         resolved = _heap->evacuate_object(obj, _thread);
 155       }
 156       Atomic::cmpxchg(p, obj, resolved);
 157     }
 158   }
 159 }
 160 
 161 void ShenandoahEvacUpdateCleanupRootsClosure::do_oop(narrowOop* p) {
 162   ShouldNotReachHere();
 163 }
 164 
 165 ShenandoahCodeBlobAndDisarmClosure::ShenandoahCodeBlobAndDisarmClosure(OopClosure* cl) :
 166   CodeBlobToOopClosure(cl, true /* fix_relocations */),
 167    _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
 168 }
 169 
 170 void ShenandoahCodeBlobAndDisarmClosure::do_code_blob(CodeBlob* cb) {
 171   nmethod* const nm = cb->as_nmethod_or_null();
 172   if (nm != NULL && nm->oops_do_try_claim()) {
 173     assert(!ShenandoahNMethod::gc_data(nm)->is_unregistered(), "Should not be here");
 174     CodeBlobToOopClosure::do_code_blob(cb);
 175     _bs->disarm(nm);
 176   }
 177 }
 178 
 179 #ifdef ASSERT
 180 template <class T>
 181 void ShenandoahAssertNotForwardedClosure::do_oop_work(T* p) {
 182   T o = RawAccess<>::oop_load(p);
 183   if (!CompressedOops::is_null(o)) {
 184     oop obj = CompressedOops::decode_not_null(o);
 185     shenandoah_assert_not_forwarded(p, obj);
 186   }
 187 }
 188 
 189 void ShenandoahAssertNotForwardedClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 190 void ShenandoahAssertNotForwardedClosure::do_oop(oop* p)       { do_oop_work(p); }
 191 #endif
 192 
 193 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP