Path: blob/master/src/hotspot/share/oops/compressedOops.hpp
40951 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_OOPS_COMPRESSEDOOPS_HPP25#define SHARE_OOPS_COMPRESSEDOOPS_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "oops/oopsHierarchy.hpp"30#include "utilities/globalDefinitions.hpp"31#include <type_traits>3233class outputStream;34class ReservedHeapSpace;3536struct NarrowPtrStruct {37// Base address for oop-within-java-object materialization.38// NULL if using wide oops or zero based narrow oops.39address _base;40// Number of shift bits for encoding/decoding narrow ptrs.41// 0 if using wide ptrs or zero based unscaled narrow ptrs,42// LogMinObjAlignmentInBytes/LogKlassAlignmentInBytes otherwise.43int _shift;44// Generate code with implicit null checks for narrow ptrs.45bool _use_implicit_null_checks;46};4748class CompressedOops : public AllStatic {49friend class VMStructs;5051// For UseCompressedOops.52static NarrowPtrStruct _narrow_oop;5354// The address range of the heap55static MemRegion _heap_address_range;5657public:58// For UseCompressedOops59// Narrow Oop encoding mode:60// 0 - Use 32-bits oops without encoding when61// NarrowOopHeapBaseMin + heap_size < 4Gb62// 1 - Use zero based compressed oops with encoding when63// NarrowOopHeapBaseMin + heap_size < 32Gb64// 2 - Use compressed oops with disjoint heap base if65// base is 32G-aligned and base > 0. This allows certain66// optimizations in encoding/decoding.67// Disjoint: Bits used in base are disjoint from bits used68// for oops ==> oop = (cOop << 3) | base. One can disjoint69// the bits of an oop into base and compressed oop.70// 3 - Use compressed oops with heap base + encoding.71enum Mode {72UnscaledNarrowOop = 0,73ZeroBasedNarrowOop = 1,74DisjointBaseNarrowOop = 2,75HeapBasedNarrowOop = 3,76AnyNarrowOopMode = 477};7879// The representation type for narrowOop is assumed to be uint32_t.80static_assert(std::is_same<uint32_t, std::underlying_type_t<narrowOop>>::value,81"narrowOop has unexpected representation type");8283static void initialize(const ReservedHeapSpace& heap_space);8485static void set_base(address base);86static void set_shift(int shift);87static void set_use_implicit_null_checks(bool use);8889static address base() { return _narrow_oop._base; }90static address begin() { return (address)_heap_address_range.start(); }91static address end() { return (address)_heap_address_range.end(); }92static bool is_base(void* addr) { return (base() == (address)addr); }93static int shift() { return _narrow_oop._shift; }94static bool use_implicit_null_checks() { return _narrow_oop._use_implicit_null_checks; }9596static address* ptrs_base_addr() { return &_narrow_oop._base; }97static address ptrs_base() { return _narrow_oop._base; }9899static bool is_in(void* addr);100static bool is_in(MemRegion mr);101102static Mode mode();103static const char* mode_to_string(Mode mode);104105// Test whether bits of addr and possible offsets into the heap overlap.106static bool is_disjoint_heap_base_address(address addr);107108// Check for disjoint base compressed oops.109static bool base_disjoint();110111// Check for real heapbased compressed oops.112// We must subtract the base as the bits overlap.113// If we negate above function, we also get unscaled and zerobased.114static bool base_overlaps();115116static void print_mode(outputStream* st);117118static bool is_null(oop v) { return v == NULL; }119static bool is_null(narrowOop v) { return v == narrowOop::null; }120121static inline oop decode_raw_not_null(narrowOop v);122static inline oop decode_raw(narrowOop v);123static inline oop decode_not_null(narrowOop v);124static inline oop decode(narrowOop v);125static inline narrowOop encode_not_null(oop v);126static inline narrowOop encode(oop v);127128// No conversions needed for these overloads129static inline oop decode_not_null(oop v);130static inline oop decode(oop v);131static inline narrowOop encode_not_null(narrowOop v);132static inline narrowOop encode(narrowOop v);133134static inline uint32_t narrow_oop_value(oop o);135static inline uint32_t narrow_oop_value(narrowOop o);136137template<typename T>138static inline narrowOop narrow_oop_cast(T i);139};140141// For UseCompressedClassPointers.142class CompressedKlassPointers : public AllStatic {143friend class VMStructs;144145static NarrowPtrStruct _narrow_klass;146147// Together with base, this defines the address range within which Klass148// structures will be located: [base, base+range). While the maximal149// possible encoding range is 4|32G for shift 0|3, if we know beforehand150// the expected range of Klass* pointers will be smaller, a platform151// could use this info to optimize encoding.152static size_t _range;153154static void set_base(address base);155static void set_range(size_t range);156157public:158159static void set_shift(int shift);160161162// Given an address p, return true if p can be used as an encoding base.163// (Some platforms have restrictions of what constitutes a valid base164// address).165static bool is_valid_base(address p);166167// Given an address range [addr, addr+len) which the encoding is supposed to168// cover, choose base, shift and range.169// The address range is the expected range of uncompressed Klass pointers we170// will encounter (and the implicit promise that there will be no Klass171// structures outside this range).172static void initialize(address addr, size_t len);173174static void print_mode(outputStream* st);175176static address base() { return _narrow_klass._base; }177static size_t range() { return _range; }178static int shift() { return _narrow_klass._shift; }179180static bool is_null(Klass* v) { return v == NULL; }181static bool is_null(narrowKlass v) { return v == 0; }182183static inline Klass* decode_raw(narrowKlass v, address base);184static inline Klass* decode_raw(narrowKlass v);185static inline Klass* decode_not_null(narrowKlass v);186static inline Klass* decode_not_null(narrowKlass v, address base);187static inline Klass* decode(narrowKlass v);188static inline narrowKlass encode_not_null(Klass* v);189static inline narrowKlass encode_not_null(Klass* v, address base);190static inline narrowKlass encode(Klass* v);191192};193194#endif // SHARE_OOPS_COMPRESSEDOOPS_HPP195196197