1 /* 2 * Copyright (c) 2017, 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_COMPRESSEDKLASS_INLINE_HPP 26 #define SHARE_OOPS_COMPRESSEDKLASS_INLINE_HPP 27 28 #include "oops/compressedKlass.hpp" 29 30 #include "memory/universe.hpp" 31 #include "oops/oop.hpp" 32 #include "utilities/align.hpp" 33 #include "utilities/globalDefinitions.hpp" 34 35 inline Klass* CompressedKlassPointers::decode_not_null_without_asserts(narrowKlass v, address narrow_base_base, int shift) { 36 return (Klass*)((uintptr_t)narrow_base_base +((uintptr_t)v << shift)); 37 } 38 39 inline narrowKlass CompressedKlassPointers::encode_not_null_without_asserts(Klass* k, address narrow_base, int shift) { 40 return (narrowKlass)(pointer_delta(k, narrow_base, 1) >> shift); 41 } 42 43 inline Klass* CompressedKlassPointers::decode_not_null_without_asserts(narrowKlass v) { 44 return decode_not_null_without_asserts(v, base(), shift()); 45 } 46 47 inline Klass* CompressedKlassPointers::decode_without_asserts(narrowKlass v) { 48 return is_null(v) ? nullptr : decode_not_null_without_asserts(v, base(), shift()); 49 } 50 51 inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v) { 52 assert(!is_null(v), "narrow klass value can never be zero"); 53 DEBUG_ONLY(check_valid_narrow_klass_id(v);) 54 Klass* const k = decode_not_null_without_asserts(v, base(), shift()); 55 DEBUG_ONLY(check_encodable(k)); 56 return k; 57 } 58 59 inline Klass* CompressedKlassPointers::decode(narrowKlass v) { 60 return is_null(v) ? nullptr : decode_not_null(v); 61 } 62 63 inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v) { 64 assert(!is_null(v), "klass value can never be zero"); 65 DEBUG_ONLY(check_encodable(v);) 66 const narrowKlass nk = encode_not_null_without_asserts(v, base(), shift()); 67 assert(decode_not_null_without_asserts(nk, base(), shift()) == v, "reversibility"); 68 DEBUG_ONLY(check_valid_narrow_klass_id(nk);) 69 return nk; 70 } 71 72 inline narrowKlass CompressedKlassPointers::encode(Klass* v) { 73 return is_null(v) ? (narrowKlass)0 : encode_not_null(v); 74 } 75 76 #ifdef ASSERT 77 inline void CompressedKlassPointers::check_encodable(const void* addr) { 78 assert(UseCompressedClassPointers, "Only call for +UseCCP"); 79 assert(addr != nullptr, "Null Klass?"); 80 assert(is_encodable(addr), 81 "Address " PTR_FORMAT " is not encodable (Klass range: " RANGEFMT ", klass alignment: %d)", 82 p2i(addr), RANGE2FMTARGS(_klass_range_start, _klass_range_end), klass_alignment_in_bytes()); 83 } 84 85 inline void CompressedKlassPointers::check_valid_narrow_klass_id(narrowKlass nk) { 86 check_init(_base); 87 assert(UseCompressedClassPointers, "Only call for +UseCCP"); 88 assert(nk > 0, "narrow Klass ID is 0"); 89 const uint64_t nk_mask = ~right_n_bits(narrow_klass_pointer_bits()); 90 assert(((uint64_t)nk & nk_mask) == 0, "narrow klass id bit spillover (%u)", nk); 91 assert(nk >= _lowest_valid_narrow_klass_id && 92 nk <= _highest_valid_narrow_klass_id, "narrowKlass ID out of range (%u)", nk); 93 } 94 #endif // ASSERT 95 96 // Given a narrow Klass ID, returns true if it appears to be valid 97 inline bool CompressedKlassPointers::is_valid_narrow_klass_id(narrowKlass nk) { 98 return nk >= _lowest_valid_narrow_klass_id && nk < _highest_valid_narrow_klass_id; 99 } 100 101 inline address CompressedKlassPointers::encoding_range_end() { 102 const int max_bits = narrow_klass_pointer_bits() + _shift; 103 return (address)((uintptr_t)_base + nth_bit(max_bits)); 104 } 105 106 #endif // SHARE_OOPS_COMPRESSEDKLASS_INLINE_HPP