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 "oops/compressedOops.inline.hpp"
  32 #include "runtime/thread.hpp"
  33 
  34 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() :
  35   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  36 }
  37 
  38 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) {
  39   if (CompressedOops::is_null(obj)) {
  40     return false;
  41   }
  42   obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  43   shenandoah_assert_not_forwarded_if(NULL, obj,
  44                                      (ShenandoahHeap::heap()->is_concurrent_mark_in_progress() ||
  45                                      ShenandoahHeap::heap()->is_concurrent_traversal_in_progress()));
  46   return _mark_context->is_marked(obj);
  47 }
  48 
  49 ShenandoahIsAliveClosure::ShenandoahIsAliveClosure() :
  50   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  51 }
  52 
  53 bool ShenandoahIsAliveClosure::do_object_b(oop obj) {
  54   if (CompressedOops::is_null(obj)) {
  55     return false;
  56   }
  57   shenandoah_assert_not_forwarded(NULL, obj);
  58   return _mark_context->is_marked(obj);
  59 }
  60 
  61 BoolObjectClosure* ShenandoahIsAliveSelector::is_alive_closure() {
  62   return ShenandoahHeap::heap()->has_forwarded_objects() ?
  63          reinterpret_cast<BoolObjectClosure*>(&_fwd_alive_cl) :
  64          reinterpret_cast<BoolObjectClosure*>(&_alive_cl);
  65 }
  66 
  67 ShenandoahUpdateRefsClosure::ShenandoahUpdateRefsClosure() :
  68   _heap(ShenandoahHeap::heap()) {
  69 }
  70 
  71 template <class T>
  72 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  73   T o = RawAccess<>::oop_load(p);
  74   if (!CompressedOops::is_null(o)) {
  75     oop obj = CompressedOops::decode_not_null(o);
  76     _heap->update_with_forwarded_not_null(p, obj);
  77   }
  78 }
  79 
  80 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  81 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  82 
  83 ShenandoahEvacuateUpdateRootsClosure::ShenandoahEvacuateUpdateRootsClosure() :
  84   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
  85 }
  86 
  87 template <class T>
  88 void ShenandoahEvacuateUpdateRootsClosure::do_oop_work(T* p) {
  89   assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
  90 
  91   T o = RawAccess<>::oop_load(p);
  92   if (! CompressedOops::is_null(o)) {
  93     oop obj = CompressedOops::decode_not_null(o);
  94     if (_heap->in_collection_set(obj)) {
  95       shenandoah_assert_marked(p, obj);
  96       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  97       if (oopDesc::equals_raw(resolved, obj)) {
  98         resolved = _heap->evacuate_object(obj, _thread);
  99       }
 100       RawAccess<IS_NOT_NULL>::oop_store(p, resolved);
 101     }
 102   }
 103 }
 104 void ShenandoahEvacuateUpdateRootsClosure::do_oop(oop* p) {
 105   do_oop_work(p);
 106 }
 107 
 108 void ShenandoahEvacuateUpdateRootsClosure::do_oop(narrowOop* p) {
 109   do_oop_work(p);
 110 }
 111 
 112 ShenandoahEvacUpdateOopStorageRootsClosure::ShenandoahEvacUpdateOopStorageRootsClosure() :
 113   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
 114 }
 115 
 116 void ShenandoahEvacUpdateOopStorageRootsClosure::do_oop(oop* p) {
 117   assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 118 
 119   oop obj = RawAccess<>::oop_load(p);
 120   if (! CompressedOops::is_null(obj)) {
 121     if (_heap->in_collection_set(obj)) {
 122       shenandoah_assert_marked(p, obj);
 123       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 124       if (oopDesc::equals_raw(resolved, obj)) {
 125         resolved = _heap->evacuate_object(obj, _thread);
 126       }
 127 
 128       Atomic::cmpxchg(resolved, p, obj);
 129     }
 130   }
 131 }
 132 
 133 void ShenandoahEvacUpdateOopStorageRootsClosure::do_oop(narrowOop* p) {
 134   ShouldNotReachHere();
 135 }
 136 
 137 ShenandoahCodeBlobAndDisarmClosure::ShenandoahCodeBlobAndDisarmClosure(OopClosure* cl) :
 138   CodeBlobToOopClosure(cl, true /* fix_relocations */),
 139    _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
 140 }
 141 
 142 void ShenandoahCodeBlobAndDisarmClosure::do_code_blob(CodeBlob* cb) {
 143   nmethod* const nm = cb->as_nmethod_or_null();
 144   if (nm != NULL && !nm->test_set_oops_do_mark()) {
 145     assert(!ShenandoahNMethod::gc_data(nm)->is_unregistered(), "Should not be here");
 146     CodeBlobToOopClosure::do_code_blob(cb);
 147     _bs->disarm(nm);
 148   }
 149 }
 150 
 151 #ifdef ASSERT
 152 template <class T>
 153 void ShenandoahAssertNotForwardedClosure::do_oop_work(T* p) {
 154   T o = RawAccess<>::oop_load(p);
 155   if (!CompressedOops::is_null(o)) {
 156     oop obj = CompressedOops::decode_not_null(o);
 157     shenandoah_assert_not_forwarded(p, obj);
 158   }
 159 }
 160 
 161 void ShenandoahAssertNotForwardedClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 162 void ShenandoahAssertNotForwardedClosure::do_oop(oop* p)       { do_oop_work(p); }
 163 #endif
 164 
 165 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP