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_COMPRESSEDKLASS_HPP 26 #define SHARE_OOPS_COMPRESSEDKLASS_HPP 27 28 #include "memory/allStatic.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 31 class outputStream; 32 class Klass; 33 34 // If compressed klass pointers then use narrowKlass. 35 typedef juint narrowKlass; 36 37 const int LogKlassAlignmentInBytes = 3; 38 const int KlassAlignmentInBytes = 1 << LogKlassAlignmentInBytes; 39 40 // Maximal size of compressed class space. Above this limit compression is not possible. 41 // Also upper bound for placement of zero based class space. (Class space is further limited 42 // to be < 3G, see arguments.cpp.) 43 const uint64_t KlassEncodingMetaspaceMax = (uint64_t(max_juint) + 1) << LogKlassAlignmentInBytes; 44 45 // For UseCompressedClassPointers. 46 class CompressedKlassPointers : public AllStatic { 47 friend class VMStructs; 48 friend class ArchiveBuilder; 49 50 static address _base; 51 static int _shift; 52 53 // Together with base, this defines the address range within which Klass 54 // structures will be located: [base, base+range). While the maximal 55 // possible encoding range is 4|32G for shift 0|3, if we know beforehand 56 // the expected range of Klass* pointers will be smaller, a platform 57 // could use this info to optimize encoding. 58 static size_t _range; 59 60 // Helper function for common cases. 61 static char* reserve_address_space_X(uintptr_t from, uintptr_t to, size_t size, size_t alignment, bool aslr); 62 static char* reserve_address_space_for_unscaled_encoding(size_t size, bool aslr); 63 static char* reserve_address_space_for_zerobased_encoding(size_t size, bool aslr); 64 static char* reserve_address_space_for_16bit_move(size_t size, bool aslr); 65 66 DEBUG_ONLY(static void assert_is_valid_encoding(address addr, size_t len, address base, int shift);) 67 68 static inline Klass* decode_not_null_without_asserts(narrowKlass v, address base, int shift); 69 static inline Klass* decode_not_null(narrowKlass v, address base, int shift); 70 71 static inline narrowKlass encode_not_null(Klass* v, address base, int shift); 72 73 public: 74 75 // Reserve a range of memory that is to contain Klass strucutures which are referenced by narrow Klass IDs. 76 // If optimize_for_zero_base is true, the implementation will attempt to reserve optimized for zero-based encoding. 77 static char* reserve_address_space_for_compressed_classes(size_t size, bool aslr, bool optimize_for_zero_base); 78 79 // Given a klass range [addr, addr+len) and a given encoding scheme, assert that this scheme covers the range, then 80 // set this encoding scheme. Used by CDS at runtime to re-instate the scheme used to pre-compute klass ids for 81 // archived heap objects. In this case, we don't have the freedom to choose base and shift; they are handed to 82 // us from CDS. 83 static void initialize_for_given_encoding(address addr, size_t len, address requested_base, int requested_shift); 84 85 // Given an address range [addr, addr+len) which the encoding is supposed to 86 // cover, choose base, shift and range. 87 // The address range is the expected range of uncompressed Klass pointers we 88 // will encounter (and the implicit promise that there will be no Klass 89 // structures outside this range). 90 static void initialize(address addr, size_t len); 91 92 static void print_mode(outputStream* st); 93 94 static address base() { return _base; } 95 static size_t range() { return _range; } 96 static int shift() { return _shift; } 97 98 static bool is_null(Klass* v) { return v == nullptr; } 99 static bool is_null(narrowKlass v) { return v == 0; } 100 101 // Versions without asserts 102 static inline Klass* decode_not_null_without_asserts(narrowKlass v); 103 static inline Klass* decode_without_asserts(narrowKlass v); 104 105 static inline Klass* decode_not_null(narrowKlass v); 106 static inline Klass* decode(narrowKlass v); 107 108 static inline narrowKlass encode_not_null(Klass* v); 109 static inline narrowKlass encode(Klass* v); 110 }; 111 112 #endif // SHARE_OOPS_COMPRESSEDKLASS_HPP