1 /* 2 * Copyright (c) 2005, 2025, 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 void Klass::keep_alive() const { 41 // Resolving the holder (a WeakHandle) will keep the klass alive until the next safepoint. 42 // Making the klass's CLD handle oops (e.g. the java_mirror), safe to store in the object 43 // graph and its roots (e.g. Handles). 44 static_cast<void>(klass_holder()); 45 } 46 47 inline bool Klass::is_non_strong_hidden() const { 48 return is_hidden() && class_loader_data()->has_class_mirror_holder(); 49 } 50 51 // Iff the class loader (or mirror for non-strong hidden classes) is alive the 52 // Klass is considered alive. This is safe to call before the CLD is marked as 53 // unloading, and hence during concurrent class unloading. 54 // This returns false if the Klass is unloaded, or about to be unloaded because the holder of 55 // the CLD is no longer strongly reachable. 56 // The return value of this function may change from true to false after a safepoint. So the caller 57 // of this function must ensure that a safepoint doesn't happen while interpreting the return value. 58 inline bool Klass::is_loader_alive() const { 59 return class_loader_data()->is_alive(); 60 } 61 62 inline markWord Klass::make_prototype_header(const Klass* kls, markWord prototype) { 63 if (UseCompactObjectHeaders) { 64 // With compact object headers, the narrow Klass ID is part of the mark word. 65 // We therefore seed the mark word with the narrow Klass ID. 66 // Note that only those Klass that can be instantiated have a narrow Klass ID. 67 // For those who don't, we leave the klass bits empty and assert if someone 68 // tries to use those. 69 const narrowKlass nk = CompressedKlassPointers::is_encodable(kls) ? 70 CompressedKlassPointers::encode(const_cast<Klass*>(kls)) : 0; 71 prototype = prototype.set_narrow_klass(nk); 72 } 73 return prototype; 74 } 75 76 inline void Klass::set_prototype_header(markWord header) { 77 _prototype_header = header; 78 } 79 80 inline bool Klass::is_loader_present_and_alive() const { 81 ClassLoaderData* cld = class_loader_data(); 82 return (cld != nullptr) ? cld->is_alive() : false; 83 } 84 85 inline markWord Klass::prototype_header() const { 86 // You only need prototypes for allocating objects. If the class is not instantiable, it won't live in 87 // class space and have no narrow Klass ID. But in that case we should not need the prototype. 88 assert(!UseCompactObjectHeaders || _prototype_header.narrow_klass() > 0, "Klass " PTR_FORMAT ": invalid prototype (" PTR_FORMAT ")", 89 p2i(this), _prototype_header.value()); 90 return _prototype_header; 91 } 92 93 // May no longer be required (was used to avoid a bootstrapping problem... 94 inline markWord Klass::default_prototype_header(Klass* k) { 95 return (k == nullptr) ? markWord::prototype() : k->prototype_header(); 96 } 97 98 inline void Klass::set_prototype_header_klass(narrowKlass klass) { 99 // Merge narrowKlass in existing prototype header. 100 _prototype_header = _prototype_header.set_narrow_klass(klass); 101 } 102 103 // Loading the java_mirror does not keep its holder alive. See Klass::keep_alive(). 104 inline oop Klass::java_mirror() const { 105 return _java_mirror.resolve(); 106 } 107 108 inline oop Klass::java_mirror_no_keepalive() const { 109 return _java_mirror.peek(); 110 } 111 112 inline klassVtable Klass::vtable() const { 113 return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size()); 114 } 115 116 inline oop Klass::class_loader() const { 117 return class_loader_data()->class_loader(); 118 } 119 120 inline vtableEntry* Klass::start_of_vtable() const { 121 return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset())); 122 } 123 124 inline ByteSize Klass::vtable_start_offset() { 125 return in_ByteSize(InstanceKlass::header_size() * wordSize); 126 } 127 128 // subtype check: true if is_subclass_of, or if k is interface and receiver implements it 129 inline bool Klass::is_subtype_of(Klass* k) const { 130 assert(secondary_supers() != nullptr, "must be"); 131 const juint off = k->super_check_offset(); 132 const juint secondary_offset = in_bytes(secondary_super_cache_offset()); 133 if (off == secondary_offset) { 134 return search_secondary_supers(k); 135 } else { 136 Klass* sup = *(Klass**)( (address)this + off ); 137 return (sup == k); 138 } 139 } 140 141 // Hashed search for secondary super k. 142 inline bool Klass::lookup_secondary_supers_table(Klass* k) const { 143 uintx bitmap = _secondary_supers_bitmap; 144 145 constexpr int highest_bit_number = SECONDARY_SUPERS_TABLE_SIZE - 1; 146 uint8_t slot = k->_hash_slot; 147 uintx shifted_bitmap = bitmap << (highest_bit_number - slot); 148 149 precond((int)population_count(bitmap) <= secondary_supers()->length()); 150 151 // First check the bitmap to see if super_klass might be present. If 152 // the bit is zero, we are certain that super_klass is not one of 153 // the secondary supers. 154 if (((shifted_bitmap >> highest_bit_number) & 1) == 0) { 155 return false; 156 } 157 158 // Calculate the initial hash probe 159 int index = population_count(shifted_bitmap) - 1; 160 if (secondary_supers()->at(index) == k) { 161 // Yes! It worked the first time. 162 return true; 163 } 164 165 // Is there another entry to check? Consult the bitmap. If Bit 1, 166 // the next bit to test, is zero, we are certain that super_klass is 167 // not one of the secondary supers. 168 bitmap = rotate_right(bitmap, slot); 169 if ((bitmap & 2) == 0) { 170 return false; 171 } 172 173 // Continue probing the hash table 174 return fallback_search_secondary_supers(k, index, bitmap); 175 } 176 177 inline bool Klass::search_secondary_supers(Klass *k) const { 178 // This is necessary because I am never in my own secondary_super list. 179 if (this == k) 180 return true; 181 182 bool result = lookup_secondary_supers_table(k); 183 184 #ifndef PRODUCT 185 if (VerifySecondarySupers) { 186 bool linear_result = linear_search_secondary_supers(k); 187 if (linear_result != result) { 188 on_secondary_supers_verification_failure((Klass*)this, k, linear_result, result, "mismatch"); 189 } 190 } 191 #endif // PRODUCT 192 193 return result; 194 } 195 196 // Returns true if this Klass needs to be addressable via narrow Klass ID. 197 inline bool Klass::needs_narrow_id() const { 198 // Classes that are never instantiated need no narrow Klass Id, since the 199 // only point of having a narrow id is to put it into an object header. Keeping 200 // never instantiated classes out of class space lessens the class space pressure. 201 // For more details, see JDK-8338526. 202 // Note: don't call this function before access flags are initialized. 203 return !is_abstract() && !is_interface(); 204 } 205 #endif // SHARE_OOPS_KLASS_INLINE_HPP