1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   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 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHTRAVERSALGC_INLINE_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHTRAVERSALGC_INLINE_HPP
  26 
  27 #include "gc/shared/markBitMap.inline.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  31 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
  32 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  33 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  34 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  35 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
  36 #include "memory/iterator.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 
  39 template <class T, bool STRING_DEDUP, bool DEGEN, bool UPDATE_MATRIX>
  40 void ShenandoahTraversalGC::process_oop(T* p, Thread* thread, ShenandoahObjToScanQueue* queue, oop base_obj) {
  41   T o = RawAccess<>::oop_load(p);
  42   if (!CompressedOops::is_null(o)) {
  43     oop obj = CompressedOops::decode_not_null(o);
  44     bool update_matrix = true;
  45     if (DEGEN) {
  46       oop forw = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  47       if (!oopDesc::unsafe_equals(obj, forw)) {
  48         // Update reference.
  49         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
  50       }
  51       obj = forw;
  52     } else if (_heap->in_collection_set(obj)) {
  53       oop forw = ShenandoahBarrierSet::resolve_forwarded_not_null(obj);
  54       if (oopDesc::unsafe_equals(obj, forw)) {
  55         forw = _heap->evacuate_object(obj, thread);
  56       }
  57       // tty->print_cr("NORMAL visit: "PTR_FORMAT", obj: "PTR_FORMAT" to "PTR_FORMAT, p2i(p), p2i(obj), p2i(forw));
  58       assert(! oopDesc::unsafe_equals(obj, forw) || _heap->cancelled_gc(), "must be evacuated");
  59       // Update reference.
  60       oop previous = _heap->atomic_compare_exchange_oop(forw, p, obj);
  61       if (UPDATE_MATRIX && !oopDesc::unsafe_equals(previous, obj)) {
  62         update_matrix = false;
  63       }
  64       obj = forw;
  65     }
  66 
  67     if (UPDATE_MATRIX && update_matrix) {
  68       shenandoah_assert_not_forwarded_except(p, obj, _heap->cancelled_gc());
  69       const void* src;
  70       if (!_heap->is_in_reserved(p)) {
  71         src = (const void*)(HeapWord*) obj;
  72       } else {
  73         src = p;
  74       }
  75       if (src != NULL) {
  76         _matrix->set_connected(src, obj);
  77       }
  78     }
  79 
  80     if (_heap->next_marking_context()->mark(obj)) {
  81       bool succeeded = queue->push(ShenandoahMarkTask(obj));
  82       assert(succeeded, "must succeed to push to task queue");
  83 
  84       if (STRING_DEDUP && ShenandoahStringDedup::is_candidate(obj) && !_heap->cancelled_gc()) {
  85         assert(ShenandoahStringDedup::is_enabled(), "Must be enabled");
  86         // Only dealing with to-space string, so that we can avoid evac-oom protocol, which is costly here.
  87         assert(!_heap->in_collection_set(obj), "Must be in to-space");
  88         ShenandoahStringDedup::enqueue_candidate(obj);
  89       }
  90     }
  91   }
  92 }
  93 
  94 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHTRAVERSALGC_INLINE_HPP