1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. 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 
 25 #include "precompiled.hpp"
 26 #include "oops/markWord.hpp"
 27 #include "runtime/basicLock.inline.hpp"
 28 #include "runtime/javaThread.hpp"
 29 #include "runtime/objectMonitor.inline.hpp"
 30 #include "utilities/ostream.hpp"
 31 
 32 #ifdef _LP64
 33 STATIC_ASSERT((markWord::klass_shadow_mask_inplace & markWord::klass_mask_in_place) == 0);
 34 STATIC_ASSERT((markWord::klass_load_shift + markWord::klass_shadow_bits) == markWord::klass_shift);
 35 STATIC_ASSERT(markWord::klass_shift + markWord::klass_bits == 64);
 36 // The hash (preceding nKlass) shall be a direct neighbor but not interleave
 37 STATIC_ASSERT(markWord::klass_shift == markWord::hash_bits_compact + markWord::hash_shift_compact);
 38 #endif
 39 
 40 markWord markWord::displaced_mark_helper() const {
 41   assert(has_displaced_mark_helper(), "check");
 42   if (has_monitor()) {
 43     // Has an inflated monitor. Must be checked before has_locker().
 44     ObjectMonitor* monitor = this->monitor();
 45     return monitor->header();
 46   }
 47   if (has_locker()) {  // has a stack lock
 48     BasicLock* locker = this->locker();
 49     return locker->displaced_header();
 50   }
 51   // This should never happen:
 52   fatal("bad header=" INTPTR_FORMAT, value());
 53   return markWord(value());
 54 }
 55 
 56 void markWord::set_displaced_mark_helper(markWord m) const {
 57   assert(has_displaced_mark_helper(), "check");
 58   if (has_monitor()) {
 59     // Has an inflated monitor. Must be checked before has_locker().
 60     ObjectMonitor* monitor = this->monitor();
 61     monitor->set_header(m);
 62     return;
 63   }
 64   if (has_locker()) {  // has a stack lock
 65     BasicLock* locker = this->locker();
 66     locker->set_displaced_header(m);
 67     return;
 68   }
 69   // This should never happen:
 70   fatal("bad header=" INTPTR_FORMAT, value());
 71 }
 72 
 73 void markWord::print_on(outputStream* st, bool print_monitor_info) const {
 74   if (is_marked()) {  // last bits = 11
 75     st->print(" marked(" INTPTR_FORMAT ")", value());
 76   } else if (has_monitor()) {  // last bits = 10
 77     // have to check has_monitor() before is_locked()
 78     st->print(" monitor(" INTPTR_FORMAT ")=", value());
 79     if (print_monitor_info && !UseObjectMonitorTable) {
 80       ObjectMonitor* mon = monitor();
 81       if (mon == nullptr) {
 82         st->print("null (this should never be seen!)");
 83       } else {
 84         mon->print_on(st);
 85       }
 86     }
 87   } else if (is_locked()) {  // last bits != 01 => 00
 88     // thin locked
 89     st->print(" locked(" INTPTR_FORMAT ")", value());
 90   } else {
 91     st->print(" mark(");
 92     if (is_unlocked()) {   // last bits = 01
 93       st->print("is_unlocked");
 94       if (has_no_hash()) {
 95         st->print(" no_hash");
 96       } else {
 97         st->print(" hash=" INTPTR_FORMAT, hash());
 98       }
 99     } else {
100       st->print("??");
101     }
102     st->print(" age=%d)", age());
103   }
104 }