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/shenandoah/shenandoahAsserts.hpp"
  27 #include "gc/shenandoah/shenandoahClosures.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  29 #include "oops/compressedOops.inline.hpp"
  30 #include "runtime/thread.hpp"
  31 
  32 ShenandoahForwardedIsAliveClosure::ShenandoahForwardedIsAliveClosure() :
  33   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  34 }
  35 
  36 bool ShenandoahForwardedIsAliveClosure::do_object_b(oop obj) {
  37   if (CompressedOops::is_null(obj)) {
  38     return false;
  39   }
  40   obj = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  41   shenandoah_assert_not_forwarded_if(NULL, obj,
  42                                      (ShenandoahHeap::heap()->is_concurrent_mark_in_progress() ||
  43                                      ShenandoahHeap::heap()->is_concurrent_traversal_in_progress()));
  44   return _mark_context->is_marked(obj);
  45 }
  46 
  47 ShenandoahIsAliveClosure::ShenandoahIsAliveClosure() :
  48   _mark_context(ShenandoahHeap::heap()->marking_context()) {
  49 }
  50 
  51 bool ShenandoahIsAliveClosure::do_object_b(oop obj) {
  52   if (CompressedOops::is_null(obj)) {
  53     return false;
  54   }
  55   shenandoah_assert_not_forwarded(NULL, obj);
  56   return _mark_context->is_marked(obj);
  57 }
  58 
  59 BoolObjectClosure* ShenandoahIsAliveSelector::is_alive_closure() {
  60   return ShenandoahHeap::heap()->has_forwarded_objects() ?
  61          reinterpret_cast<BoolObjectClosure*>(&_fwd_alive_cl) :
  62          reinterpret_cast<BoolObjectClosure*>(&_alive_cl);
  63 }
  64 
  65 ShenandoahUpdateRefsClosure::ShenandoahUpdateRefsClosure() :
  66   _heap(ShenandoahHeap::heap()) {
  67 }
  68 
  69 template <class T>
  70 void ShenandoahUpdateRefsClosure::do_oop_work(T* p) {
  71   T o = RawAccess<>::oop_load(p);
  72   if (!CompressedOops::is_null(o)) {
  73     oop obj = CompressedOops::decode_not_null(o);
  74     _heap->update_with_forwarded_not_null(p, obj);
  75   }
  76 }
  77 
  78 void ShenandoahUpdateRefsClosure::do_oop(oop* p)       { do_oop_work(p); }
  79 void ShenandoahUpdateRefsClosure::do_oop(narrowOop* p) { do_oop_work(p); }
  80 
  81 ShenandoahEvacuateUpdateRootsClosure::ShenandoahEvacuateUpdateRootsClosure() :
  82   _heap(ShenandoahHeap::heap()), _thread(Thread::current()) {
  83 }
  84 
  85 template <class T>
  86 void ShenandoahEvacuateUpdateRootsClosure::do_oop_work(T* p) {
  87   assert(_heap->is_evacuation_in_progress(), "Only do this when evacuation is in progress");
  88 
  89   T o = RawAccess<>::oop_load(p);
  90   if (! CompressedOops::is_null(o)) {
  91     oop obj = CompressedOops::decode_not_null(o);
  92     if (_heap->in_collection_set(obj)) {
  93       shenandoah_assert_marked(p, obj);
  94       oop resolved = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  95       if (resolved == obj) {
  96         resolved = _heap->evacuate_object(obj, _thread);
  97       }
  98       RawAccess<IS_NOT_NULL>::oop_store(p, resolved);
  99     }
 100   }
 101 }
 102 
 103 void ShenandoahEvacuateUpdateRootsClosure::do_oop(oop* p) {
 104   do_oop_work(p);
 105 }
 106 
 107 void ShenandoahEvacuateUpdateRootsClosure::do_oop(narrowOop* p) {
 108   do_oop_work(p);
 109 }
 110 
 111 #ifdef ASSERT
 112 template <class T>
 113 void ShenandoahAssertNotForwardedClosure::do_oop_work(T* p) {
 114   T o = RawAccess<>::oop_load(p);
 115   if (!CompressedOops::is_null(o)) {
 116     oop obj = CompressedOops::decode_not_null(o);
 117     shenandoah_assert_not_forwarded(p, obj);
 118   }
 119 }
 120 
 121 void ShenandoahAssertNotForwardedClosure::do_oop(narrowOop* p) { do_oop_work(p); }
 122 void ShenandoahAssertNotForwardedClosure::do_oop(oop* p)       { do_oop_work(p); }
 123 #endif
 124 
 125 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP