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     assert_object_is_in_heap(p, obj);
 121 
 122     if (_heap->in_collection_set(obj)) {
 123       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 124       shenandoah_assert_marked(p, obj);
 125       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 126       if (resolved == obj) {
 127         resolved = _heap->evacuate_object(obj, _thread);
 128       }
 129       RawAccess<IS_NOT_NULL>::oop_store(p, resolved);
 130     }
 131   }
 132 }
 133 void ShenandoahEvacuateUpdateRootsClosure::do_oop(oop* p) {
 134   do_oop_work(p);
 135 }
 136 
 137 void ShenandoahEvacuateUpdateRootsClosure::do_oop(narrowOop* p) {
 138   do_oop_work(p);
 139 }
 140 
 141 ShenandoahEvacUpdateOopStorageRootsClosure::ShenandoahEvacUpdateOopStorageRootsClosure() :
 142   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
 143 }
 144 
 145 void ShenandoahEvacUpdateOopStorageRootsClosure::do_oop(oop* p) {
 146   assert(_heap->is_concurrent_root_in_progress(), "Only do this when evacuation is in progress");
 147 
 148   oop obj = RawAccess<>::oop_load(p);
 149   if (! CompressedOops::is_null(obj)) {
 150     if (_heap->in_collection_set(obj)) {
 151       assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
 152       shenandoah_assert_marked(p, obj);
 153       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
 154       if (resolved == obj) {
 155         resolved = _heap->evacuate_object(obj, _thread);
 156       }
 157 
 158       Atomic::cmpxchg(p, obj, resolved);
 159     }
 160   }
 161 }
 162 
 163 void ShenandoahEvacUpdateOopStorageRootsClosure::do_oop(narrowOop* p) {
 164   ShouldNotReachHere();
 165 }
 166 
 167 ShenandoahCodeBlobAndDisarmClosure::ShenandoahCodeBlobAndDisarmClosure(OopClosure* cl) :
 168   CodeBlobToOopClosure(cl, true /* fix_relocations */),
 169    _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {
 170 }
 171 
 172 void ShenandoahCodeBlobAndDisarmClosure::do_code_blob(CodeBlob* cb) {
 173   nmethod* const nm = cb->as_nmethod_or_null();
 174   if (nm != NULL && nm->oops_do_try_claim()) {
 175     assert(!ShenandoahNMethod::gc_data(nm)->is_unregistered(), "Should not be here");
 176     CodeBlobToOopClosure::do_code_blob(cb);
 177     _bs->disarm(nm);
 178   }
 179 }
 180 
 181 #ifdef ASSERT
 182 template <class T>
 183 void ShenandoahAssertNotForwardedClosure::do_oop_work(T* p) {
 184   T o = RawAccess<>::oop_load(p);
 185   if (!CompressedOops::is_null(o)) {
 186     oop obj = CompressedOops::decode_not_null(o);
 187     shenandoah_assert_not_forwarded(p, obj);
 188   }
 189 }
 190 
 191 void ShenandoahAssertNotForwardedClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 192 void ShenandoahAssertNotForwardedClosure::do_oop(oop* p)       { do_oop_work(p); }
 193 #endif
 194 
 195 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP