1 /* 2 * Copyright (c) 2019, 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_COMPRESSEDOOPS_HPP 26 #define SHARE_OOPS_COMPRESSEDOOPS_HPP 27 28 #include "memory/allStatic.hpp" 29 #include "memory/memRegion.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 #include <type_traits> 33 34 class outputStream; 35 class ReservedHeapSpace; 36 37 struct NarrowPtrStruct { 38 // Base address for oop-within-java-object materialization. 39 // null if using wide oops or zero based narrow oops. 40 address _base; 41 // Number of shift bits for encoding/decoding narrow ptrs. 42 // 0 if using wide ptrs or zero based unscaled narrow ptrs, 43 // LogMinObjAlignmentInBytes/LogKlassAlignmentInBytes otherwise. 44 int _shift; 45 // Generate code with implicit null checks for narrow ptrs. 46 bool _use_implicit_null_checks; 47 }; 48 49 class CompressedOops : public AllStatic { 50 friend class VMStructs; 51 52 // For UseCompressedOops. 53 static NarrowPtrStruct _narrow_oop; 54 55 // The address range of the heap 56 static MemRegion _heap_address_range; 57 58 public: 59 // For UseCompressedOops 60 // Narrow Oop encoding mode: 61 // 0 - Use 32-bits oops without encoding when 62 // NarrowOopHeapBaseMin + heap_size < 4Gb 63 // 1 - Use zero based compressed oops with encoding when 64 // NarrowOopHeapBaseMin + heap_size < 32Gb 65 // 2 - Use compressed oops with disjoint heap base if 66 // base is 32G-aligned and base > 0. This allows certain 67 // optimizations in encoding/decoding. 68 // Disjoint: Bits used in base are disjoint from bits used 69 // for oops ==> oop = (cOop << 3) | base. One can disjoint 70 // the bits of an oop into base and compressed oop. 71 // 3 - Use compressed oops with heap base + encoding. 72 enum Mode { 73 UnscaledNarrowOop = 0, 74 ZeroBasedNarrowOop = 1, 75 DisjointBaseNarrowOop = 2, 76 HeapBasedNarrowOop = 3, 77 AnyNarrowOopMode = 4 78 }; 79 80 // The representation type for narrowOop is assumed to be uint32_t. 81 static_assert(std::is_same<uint32_t, std::underlying_type_t<narrowOop>>::value, 82 "narrowOop has unexpected representation type"); 83 84 static void initialize(const ReservedHeapSpace& heap_space); 85 86 static void set_base(address base); 87 static void set_shift(int shift); 88 static void set_use_implicit_null_checks(bool use); 89 90 static address base() { return _narrow_oop._base; } 91 static address begin() { return (address)_heap_address_range.start(); } 92 static address end() { return (address)_heap_address_range.end(); } 93 static bool is_base(void* addr) { return (base() == (address)addr); } 94 static int shift() { return _narrow_oop._shift; } 95 static bool use_implicit_null_checks() { return _narrow_oop._use_implicit_null_checks; } 96 97 static address ptrs_base_addr() { return (address)&_narrow_oop._base; } 98 static address ptrs_base() { return _narrow_oop._base; } 99 100 static bool is_in(void* addr); 101 static bool is_in(MemRegion mr); 102 103 static Mode mode(); 104 static const char* mode_to_string(Mode mode); 105 106 // Test whether bits of addr and possible offsets into the heap overlap. 107 static bool is_disjoint_heap_base_address(address addr); 108 109 // Check for disjoint base compressed oops. 110 static bool base_disjoint(); 111 112 // Check for real heapbased compressed oops. 113 // We must subtract the base as the bits overlap. 114 // If we negate above function, we also get unscaled and zerobased. 115 static bool base_overlaps(); 116 117 static void print_mode(outputStream* st); 118 119 static bool is_null(oop v) { return v == nullptr; } 120 static bool is_null(narrowOop v) { return v == narrowOop::null; } 121 122 static inline oop decode_raw_not_null(narrowOop v); 123 static inline oop decode_raw(narrowOop v); 124 static inline oop decode_not_null(narrowOop v); 125 static inline oop decode(narrowOop v); 126 static inline narrowOop encode_not_null(oop v); 127 static inline narrowOop encode(oop v); 128 129 // No conversions needed for these overloads 130 static inline oop decode_raw_not_null(oop v); 131 static inline oop decode_not_null(oop v); 132 static inline oop decode(oop v); 133 static inline narrowOop encode_not_null(narrowOop v); 134 static inline narrowOop encode(narrowOop v); 135 136 static inline uint32_t narrow_oop_value(oop o); 137 static inline uint32_t narrow_oop_value(narrowOop o); 138 139 template<typename T> 140 static inline narrowOop narrow_oop_cast(T i); 141 }; 142 143 #endif // SHARE_OOPS_COMPRESSEDOOPS_HPP