1 /*
   2  * Copyright (c) 2015, 2018, 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 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP
  26 
  27 #include "gc_implementation/shenandoah/shenandoahAsserts.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahForwarding.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahHeapRegion.hpp"
  31 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
  32 #include "runtime/atomic.hpp"
  33 #include "runtime/thread.hpp"
  34 
  35 inline HeapWord* ShenandoahForwarding::get_forwardee_raw(oop obj) {
  36   shenandoah_assert_in_heap(NULL, obj);
  37   return get_forwardee_raw_unchecked(obj);
  38 }
  39 
  40 inline HeapWord* ShenandoahForwarding::get_forwardee_raw_unchecked(oop obj) {
  41   // JVMTI use mark words for marking objects for their needs.
  42   // On this path, we can encounter the "marked" object, but with NULL
  43   // fwdptr. That object is still not forwarded, and we need to return
  44   // the object itself.
  45   markOop mark = obj->mark();
  46   if (mark->is_marked()) {
  47     HeapWord* fwdptr = (HeapWord*) mark->clear_lock_bits();
  48     if (fwdptr != NULL) {
  49       return fwdptr;
  50     }
  51   }
  52   return (HeapWord*) obj;
  53 }
  54 
  55 inline oop ShenandoahForwarding::get_forwardee_mutator(oop obj) {
  56   // Same as above, but mutator thread cannot ever see NULL forwardee.
  57   shenandoah_assert_correct(NULL, obj);
  58   assert(Thread::current()->is_Java_thread(), "Must be a mutator thread");
  59 
  60   markOop mark = obj->mark();
  61   if (mark->is_marked()) {
  62     HeapWord* fwdptr = (HeapWord*) mark->clear_lock_bits();
  63     assert(fwdptr != NULL, "Forwarding pointer is never null here");
  64     return oop(fwdptr);
  65   } else {
  66     return obj;
  67   }
  68 }
  69 
  70 inline oop ShenandoahForwarding::get_forwardee(oop obj) {
  71   shenandoah_assert_correct(NULL, obj);
  72   return oop(get_forwardee_raw_unchecked(obj));
  73 }
  74 
  75 inline bool ShenandoahForwarding::is_forwarded(oop obj) {
  76   return obj->mark()->is_marked();
  77 }
  78 
  79 inline oop ShenandoahForwarding::try_update_forwardee(oop obj, oop update) {
  80   markOop old_mark = obj->mark();
  81   if (old_mark->is_marked()) {
  82     return (oop) old_mark->clear_lock_bits();
  83   }
  84 
  85   markOop new_mark = markOopDesc::encode_pointer_as_mark(update);
  86   markOop prev_mark = obj->cas_set_mark(new_mark, old_mark);
  87   if (prev_mark == old_mark) {
  88     return update;
  89   } else {
  90     return (oop) prev_mark->clear_lock_bits();
  91   }
  92 }
  93 
  94 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP