1 /*
  2  * Copyright (c) 2005, 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 #ifndef SHARE_OOPS_KLASS_INLINE_HPP
 26 #define SHARE_OOPS_KLASS_INLINE_HPP
 27 
 28 #include "oops/klass.hpp"
 29 
 30 #include "classfile/classLoaderData.inline.hpp"
 31 #include "oops/klassVtable.hpp"
 32 #include "oops/markWord.hpp"
 33 #include "utilities/rotate_bits.hpp"
 34 
 35 // This loads and keeps the klass's loader alive.
 36 inline oop Klass::klass_holder() const {
 37   return class_loader_data()->holder();
 38 }
 39 
 40 inline bool Klass::is_non_strong_hidden() const {
 41   return is_hidden() && class_loader_data()->has_class_mirror_holder();
 42 }
 43 
 44 // Iff the class loader (or mirror for non-strong hidden classes) is alive the
 45 // Klass is considered alive. This is safe to call before the CLD is marked as
 46 // unloading, and hence during concurrent class unloading.
 47 // This returns false if the Klass is unloaded, or about to be unloaded because the holder of
 48 // the CLD is no longer strongly reachable.
 49 // The return value of this function may change from true to false after a safepoint. So the caller
 50 // of this function must ensure that a safepoint doesn't happen while interpreting the return value.
 51 inline bool Klass::is_loader_alive() const {
 52   return class_loader_data()->is_alive();
 53 }
 54 
 55 inline void Klass::set_prototype_header(markWord header) {
 56   assert(!is_inline_klass() || header.is_inline_type(), "Unexpected prototype");
 57   assert(_prototype_header.value() == 0 || _prototype_header == markWord::prototype(),
 58          "Prototype already set");
 59 #ifdef _LP64
 60     assert(header == markWord::prototype() ||
 61            header.is_inline_type() ||
 62            header.is_flat_array() ||
 63            header.is_null_free_array(),
 64            "unknown prototype header");
 65 #else
 66     assert(header == markWord::prototype() ||
 67            header.is_inline_type(),
 68            "unknown prototype header");
 69 #endif
 70   _prototype_header = header;
 71 }
 72 
 73 inline oop Klass::java_mirror() const {
 74   return _java_mirror.resolve();
 75 }
 76 
 77 inline oop Klass::java_mirror_no_keepalive() const {
 78   return _java_mirror.peek();
 79 }
 80 
 81 inline klassVtable Klass::vtable() const {
 82   return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size());
 83 }
 84 
 85 inline oop Klass::class_loader() const {
 86   return class_loader_data()->class_loader();
 87 }
 88 
 89 inline vtableEntry* Klass::start_of_vtable() const {
 90   return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset()));
 91 }
 92 
 93 inline ByteSize Klass::vtable_start_offset() {
 94   return in_ByteSize(InstanceKlass::header_size() * wordSize);
 95 }
 96 
 97 // subtype check: true if is_subclass_of, or if k is interface and receiver implements it
 98 inline bool Klass::is_subtype_of(Klass* k) const {
 99   assert(secondary_supers() != nullptr, "must be");
100   const juint off = k->super_check_offset();
101   const juint secondary_offset = in_bytes(secondary_super_cache_offset());
102   if (off == secondary_offset) {
103     return search_secondary_supers(k);
104   } else {
105     Klass* sup = *(Klass**)( (address)this + off );
106     return (sup == k);
107   }
108 }
109 
110 // Hashed search for secondary super k.
111 inline bool Klass::lookup_secondary_supers_table(Klass* k) const {
112   uintx bitmap = _secondary_supers_bitmap;
113 
114   constexpr int highest_bit_number = SECONDARY_SUPERS_TABLE_SIZE - 1;
115   uint8_t slot = k->_hash_slot;
116   uintx shifted_bitmap = bitmap << (highest_bit_number - slot);
117 
118   precond((int)population_count(bitmap) <= secondary_supers()->length());
119 
120   // First check the bitmap to see if super_klass might be present. If
121   // the bit is zero, we are certain that super_klass is not one of
122   // the secondary supers.
123   if (((shifted_bitmap >> highest_bit_number) & 1) == 0) {
124     return false;
125   }
126 
127   // Calculate the initial hash probe
128   int index = population_count(shifted_bitmap) - 1;
129   if (secondary_supers()->at(index) == k) {
130     // Yes! It worked the first time.
131     return true;
132   }
133 
134   // Is there another entry to check? Consult the bitmap. If Bit 1,
135   // the next bit to test, is zero, we are certain that super_klass is
136   // not one of the secondary supers.
137   bitmap = rotate_right(bitmap, slot);
138   if ((bitmap & 2) == 0) {
139     return false;
140   }
141 
142   // Continue probing the hash table
143   return fallback_search_secondary_supers(k, index, bitmap);
144 }
145 
146 inline bool Klass::search_secondary_supers(Klass *k) const {
147   // This is necessary because I am never in my own secondary_super list.
148   if (this == k)
149     return true;
150 
151   bool result = lookup_secondary_supers_table(k);
152 
153 #ifndef PRODUCT
154   if (VerifySecondarySupers) {
155     bool linear_result = linear_search_secondary_supers(k);
156     if (linear_result != result) {
157       on_secondary_supers_verification_failure((Klass*)this, k, linear_result, result, "mismatch");
158     }
159   }
160 #endif // PRODUCT
161 
162   return result;
163 }
164 
165 #endif // SHARE_OOPS_KLASS_INLINE_HPP