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_shift + markWord::klass_bits == 32);
 34 // The hash (preceding klass bits) shall be a direct neighbor but not interleave
 35 STATIC_ASSERT(markWord::klass_shift == markWord::hashctrl_bits + markWord::hashctrl_shift);
 36 #endif
 37 
 38 markWord markWord::displaced_mark_helper() const {
 39   assert(has_displaced_mark_helper(), "check");
 40   if (has_monitor()) {
 41     // Has an inflated monitor. Must be checked before has_locker().
 42     ObjectMonitor* monitor = this->monitor();
 43     return monitor->header();
 44   }
 45   if (has_locker()) {  // has a stack lock
 46     BasicLock* locker = this->locker();
 47     return locker->displaced_header();
 48   }
 49   // This should never happen:
 50   fatal("bad header=" INTPTR_FORMAT, value());
 51   return markWord(value());
 52 }
 53 
 54 void markWord::set_displaced_mark_helper(markWord m) const {
 55   assert(has_displaced_mark_helper(), "check");
 56   if (has_monitor()) {
 57     // Has an inflated monitor. Must be checked before has_locker().
 58     ObjectMonitor* monitor = this->monitor();
 59     monitor->set_header(m);
 60     return;
 61   }
 62   if (has_locker()) {  // has a stack lock
 63     BasicLock* locker = this->locker();
 64     locker->set_displaced_header(m);
 65     return;
 66   }
 67   // This should never happen:
 68   fatal("bad header=" INTPTR_FORMAT, value());
 69 }
 70 
 71 void markWord::print_on(outputStream* st, bool print_monitor_info) const {
 72   if (is_marked()) {  // last bits = 11
 73     st->print(" marked(" INTPTR_FORMAT ")", value());
 74   } else if (has_monitor()) {  // last bits = 10
 75     // have to check has_monitor() before is_locked()
 76     st->print(" monitor(" INTPTR_FORMAT ")=", value());
 77     if (print_monitor_info && !UseObjectMonitorTable) {
 78       ObjectMonitor* mon = monitor();
 79       if (mon == nullptr) {
 80         st->print("null (this should never be seen!)");
 81       } else {
 82         mon->print_on(st);
 83       }
 84     }
 85   } else if (is_locked()) {  // last bits != 01 => 00
 86     // thin locked
 87     st->print(" locked(" INTPTR_FORMAT ")", value());
 88   } else {
 89     st->print(" mark(");
 90     if (is_unlocked()) {   // last bits = 01
 91       st->print("is_unlocked");
 92       if (has_no_hash()) {
 93         st->print(" no_hash");
 94       } else if (UseCompactObjectHeaders) {
 95         st->print(" hash is-hashed=%s is-copied=%s", BOOL_TO_STR(is_hashed_not_expanded()), BOOL_TO_STR(
 96                 is_hashed_expanded()));
 97       } else {
 98         st->print(" hash=" INTPTR_FORMAT, hash());
 99       }
100     } else {
101       st->print("??");
102     }
103     st->print(" age=%d)", age());
104   }
105 }