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 static inline bool check_alignment(Klass* v) {
36   return (intptr_t)v % KlassAlignmentInBytes == 0;
37 }
38 
39 inline Klass* CompressedKlassPointers::decode_not_null_without_asserts(narrowKlass v, address narrow_base, int shift) {
40   return (Klass*)((uintptr_t)narrow_base +((uintptr_t)v << shift));
41 }
42 
43 inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v, address narrow_base, int shift) {
44   assert(!is_null(v), "narrow klass value can never be zero");
45   Klass* result = decode_not_null_without_asserts(v, narrow_base, shift);
46   assert(check_alignment(result), "address not aligned: " PTR_FORMAT, p2i(result));
47   return result;
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   assert(check_alignment(v), "Address not aligned");
53   uint64_t pd = (uint64_t)(pointer_delta(v, narrow_base, 1));
54   assert(KlassEncodingMetaspaceMax > pd, "change encoding max if new encoding");
55   uint64_t result = pd >> shift;
56   assert((result & CONST64(0xffffffff00000000)) == 0, "narrow klass pointer overflow");
57   assert(decode_not_null((narrowKlass)result, narrow_base, shift) == v, "reversibility");
58   return (narrowKlass)result;
59 }
60 
61 inline Klass* CompressedKlassPointers::decode_not_null_without_asserts(narrowKlass v) {
62   return decode_not_null_without_asserts(v, base(), shift());
63 }
64 
65 inline Klass* CompressedKlassPointers::decode_without_asserts(narrowKlass v) {
66   return is_null(v) ? nullptr : decode_not_null_without_asserts(v);
67 }
68 
69 inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v) {
70   return decode_not_null(v, base(), shift());
71 }
72 
73 inline Klass* CompressedKlassPointers::decode(narrowKlass v) {
74   return is_null(v) ? nullptr : decode_not_null(v);
75 }
76 
77 inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v) {
78   return encode_not_null(v, base(), shift());
79 }
80 
81 inline narrowKlass CompressedKlassPointers::encode(Klass* v) {
82   return is_null(v) ? (narrowKlass)0 : encode_not_null(v);
83 }
84 
85 #endif // SHARE_OOPS_COMPRESSEDKLASS_INLINE_HPP