1 /*
   2  * Copyright (c) 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_INLINE_HPP
  26 
  27 #include "gc/shared/barrierSetNMethod.hpp"
  28 #include "gc/shenandoah/shenandoahAsserts.hpp"
  29 #include "gc/shenandoah/shenandoahClosures.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "gc/shenandoah/shenandoahNMethod.inline.hpp"
  32 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  33 #include "oops/compressedOops.inline.hpp"
  34 #include "runtime/atomic.hpp"
  35 #include "runtime/thread.hpp"
  36 
  37 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() :
  38   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  39 }
  40 
  41 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) {
  42   if (CompressedOops::is_null(obj)) {
  43     return false;
  44   }
  45   obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  46   shenandoah_assert_not_forwarded_if(NULL, obj,
  47                                      (ShenandoahHeap::heap()->is_concurrent_mark_in_progress() ||
  48                                      ShenandoahHeap::heap()->is_concurrent_traversal_in_progress()));
  49   return _mark_context->is_marked(obj);
  50 }
  51 
  52 ShenandoahIsAliveClosure::ShenandoahIsAliveClosure() :
  53   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  54 }
  55 
  56 bool ShenandoahIsAliveClosure::do_object_b(oop obj) {
  57   if (CompressedOops::is_null(obj)) {
  58     return false;
  59   }
  60   shenandoah_assert_not_forwarded(NULL, obj);
  61   return _mark_context->is_marked(obj);
  62 }
  63 
  64 BoolObjectClosure* ShenandoahIsAliveSelector::is_alive_closure() {
  65   return ShenandoahHeap::heap()->has_forwarded_objects() ?
  66          reinterpret_cast<BoolObjectClosure*>(&_fwd_alive_cl) :
  67          reinterpret_cast<BoolObjectClosure*>(&_alive_cl);
  68 }
  69 
  70 ShenandoahUpdateRefsClosure::ShenandoahUpdateRefsClosure() :
  71   _heap(ShenandoahHeap::heap()) {
  72 }
  73 
  74 template <class T>
  75 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  76   T o = RawAccess<>::oop_load(p);
  77   if (!CompressedOops::is_null(o)) {
  78     oop obj = CompressedOops::decode_not_null(o);
  79     _heap->update_with_forwarded_not_null(p, obj);
  80   }
  81 }
  82 
  83 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  84 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  85 
  86 ShenandoahTraversalUpdateRefsClosure::ShenandoahTraversalUpdateRefsClosure() :
  87   _heap(ShenandoahHeap::heap()),
  88   _traversal_set(ShenandoahHeap::heap()->traversal_gc()->traversal_set()) {
  89   assert(_heap->is_traversal_mode(), "Why we here?");
  90 }
  91 
  92 template <class T>
  93 void ShenandoahTraversalUpdateRefsClosure::do_oop_work(T* p) {
  94   T o = RawAccess<>::oop_load(p);
  95   if (!CompressedOops::is_null(o)) {
  96     oop obj = CompressedOops::decode_not_null(o);
  97     if (_heap->in_collection_set(obj) || _traversal_set->is_in((HeapWord*)obj)) {
  98       obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  99       RawAccess<IS_NOT_NULL>::oop_store(p, obj);
 100     } else {
 101       shenandoah_assert_not_forwarded(p, obj);
 102     }
 103   }
 104 }
 105 
 106 void ShenandoahTraversalUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
 107 void ShenandoahTraversalUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 108 
 109 ShenandoahEvacuateUpdateRootsClosure::ShenandoahEvacuateUpdateRootsClosure() :
 110   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
 111 }
 112 
 113 template <class T>
 114 void ShenandoahEvacuateUpdateRootsClosure::do_oop_work(T* p) {
 115   assert(_heap->is_concurrent_root_in_progress(), "Only do this when evacuation is in progress");
 116 
 117   T o = RawAccess<>::oop_load(p);
 118   if (! CompressedOops::is_null(o)) {
 119     oop obj = CompressedOops::decode_not_null(o);
 120     if (_heap->in_collection_set(obj)) {
 121       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 122       shenandoah_assert_marked(p, obj);
 123       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 124       if (resolved == obj) {
 125         resolved = _heap->evacuate_object(obj, _thread);
 126       }
 127       RawAccess<IS_NOT_NULL>::oop_store(p, resolved);
 128     }
 129   }
 130 }
 131 void ShenandoahEvacuateUpdateRootsClosure::do_oop(oop* p) {
 132   do_oop_work(p);
 133 }
 134 
 135 void ShenandoahEvacuateUpdateRootsClosure::do_oop(narrowOop* p) {
 136   do_oop_work(p);
 137 }
 138 
 139 ShenandoahEvacUpdateOopStorageRootsClosure::ShenandoahEvacUpdateOopStorageRootsClosure() :
 140   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
 141 }
 142 
 143 void ShenandoahEvacUpdateOopStorageRootsClosure::do_oop(oop* p) {
 144   assert(_heap->is_concurrent_root_in_progress(), "Only do this when evacuation is in progress");
 145 
 146   oop obj = RawAccess<>::oop_load(p);
 147   if (! CompressedOops::is_null(obj)) {
 148     if (_heap->in_collection_set(obj)) {
 149       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 150       shenandoah_assert_marked(p, obj);
 151       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 152       if (resolved == obj) {
 153         resolved = _heap->evacuate_object(obj, _thread);
 154       }
 155 
 156       Atomic::cmpxchg(p, obj, resolved);
 157     }
 158   }
 159 }
 160 
 161 void ShenandoahEvacUpdateOopStorageRootsClosure::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