1 /*
  2  * Copyright (c) 2017, 2023, 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 Klass* CompressedKlassPointers::decode_not_null(narrowKlass v, address narrow_base, int shift) {
 40   assert(!is_null(v), "narrow klass value can never be zero");
 41   Klass* result = decode_not_null_without_asserts(v, narrow_base, shift);
 42   DEBUG_ONLY(check_valid_klass(result, narrow_base, shift));
 43   return result;
 44 }
 45 
 46 inline narrowKlass CompressedKlassPointers::encode_not_null_without_asserts(Klass* k, address narrow_base, int shift) {
 47   return (narrowKlass)(pointer_delta(k, narrow_base, 1) >> shift);
 48 }
 49 
 50 inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v, address narrow_base, int shift) {
 51   assert(!is_null(v), "klass value can never be zero");
 52   DEBUG_ONLY(check_valid_klass(v);)
 53   narrowKlass result = encode_not_null_without_asserts(v, narrow_base, shift);
 54   assert(decode_not_null((narrowKlass)result, narrow_base, shift) == v, "reversibility");
 55   return result;
 56 }
 57 
 58 inline Klass* CompressedKlassPointers::decode_not_null_without_asserts(narrowKlass v) {
 59   return decode_not_null_without_asserts(v, base(), shift());
 60 }
 61 
 62 inline Klass* CompressedKlassPointers::decode_without_asserts(narrowKlass v) {
 63   return is_null(v) ? nullptr : decode_not_null_without_asserts(v);
 64 }
 65 
 66 inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v) {
 67   DEBUG_ONLY(check_valid_narrow_klass_id(v);)
 68   return decode_not_null(v, base(), shift());
 69 }
 70 
 71 inline Klass* CompressedKlassPointers::decode(narrowKlass v) {
 72   return is_null(v) ? nullptr : decode_not_null(v);
 73 }
 74 
 75 inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v) {
 76   narrowKlass nk = encode_not_null(v, base(), shift());
 77   DEBUG_ONLY(check_valid_narrow_klass_id(nk);)
 78   return nk;
 79 }
 80 
 81 inline narrowKlass CompressedKlassPointers::encode(Klass* v) {
 82   return is_null(v) ? (narrowKlass)0 : encode_not_null(v);
 83 }
 84 
 85 #ifdef ASSERT
 86 inline void CompressedKlassPointers::check_valid_klass(const Klass* k, address base, int shift) {
 87   const int log_alignment = MAX2(3, shift); // always at least 64-bit aligned
 88   assert(is_aligned(k, nth_bit(log_alignment)), "Klass (" PTR_FORMAT ") not properly aligned to %zu",
 89          p2i(k), nth_bit(shift));
 90   const address encoding_end = base + nth_bit(narrow_klass_pointer_bits() + shift);
 91   assert((address)k >= base && (address)k < encoding_end,
 92          "Klass (" PTR_FORMAT ") falls outside of the valid encoding range [" PTR_FORMAT "-" PTR_FORMAT ")",
 93          p2i(k), p2i(base), p2i(encoding_end));
 94 }
 95 
 96 inline void CompressedKlassPointers::check_valid_klass(const Klass* k) {
 97   assert(UseCompressedClassPointers, "Only call for +UseCCP");
 98   check_valid_klass(k, base(), shift());
 99   // Also assert that k falls into what we know is the valid Klass range. This is usually smaller
100   // than the encoding range (e.g. encoding range covers 4G, but we only have 1G class space and a
101   // tiny bit of CDS => 1.1G)
102   const address klassrange_end = base() + range();
103   assert((address)k < klassrange_end,
104       "Klass (" PTR_FORMAT ") falls outside of the valid klass range [" PTR_FORMAT "-" PTR_FORMAT ")",
105       p2i(k), p2i(base()), p2i(klassrange_end));
106 }
107 inline void CompressedKlassPointers::check_valid_narrow_klass_id(narrowKlass nk) {
108   assert(UseCompressedClassPointers, "Only call for +UseCCP");
109   const uint64_t nk_mask = ~right_n_bits(narrow_klass_pointer_bits());
110   assert(((uint64_t)nk & nk_mask) == 0, "narrow klass id bit spillover (%u)", nk);
111   assert(nk >= _lowest_valid_narrow_klass_id &&
112          nk <= _highest_valid_narrow_klass_id, "narrowKlass ID out of range (%u)", nk);
113 }
114 #endif // ASSERT
115 
116 #endif // SHARE_OOPS_COMPRESSEDKLASS_INLINE_HPP