Path: blob/master/src/hotspot/share/oops/compressedOops.inline.hpp
40951 views
/*1* Copyright (c) 2017, 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_INLINE_HPP25#define SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP2627#include "oops/compressedOops.hpp"2829#include "memory/universe.hpp"30#include "oops/oop.hpp"31#include "utilities/align.hpp"32#include "utilities/globalDefinitions.hpp"3334// Functions for encoding and decoding compressed oops.35// If the oops are compressed, the type passed to these overloaded functions36// is narrowOop. All functions are overloaded so they can be called by37// template functions without conditionals (the compiler instantiates via38// the right type and inlines the appropriate code).3940// Algorithm for encoding and decoding oops from 64 bit pointers to 32 bit41// offset from the heap base. Saving the check for null can save instructions42// in inner GC loops so these are separated.4344inline oop CompressedOops::decode_raw_not_null(narrowOop v) {45assert(!is_null(v), "narrow oop value can never be zero");46return decode_raw(v);47}4849inline oop CompressedOops::decode_raw(narrowOop v) {50return cast_to_oop((uintptr_t)base() + ((uintptr_t)v << shift()));51}5253inline oop CompressedOops::decode_not_null(narrowOop v) {54assert(!is_null(v), "narrow oop value can never be zero");55oop result = decode_raw(v);56assert(is_object_aligned(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result));57assert(Universe::is_in_heap(result), "object not in heap " PTR_FORMAT, p2i((void*) result));58return result;59}6061inline oop CompressedOops::decode(narrowOop v) {62return is_null(v) ? (oop)NULL : decode_not_null(v);63}6465inline narrowOop CompressedOops::encode_not_null(oop v) {66assert(!is_null(v), "oop value can never be zero");67assert(is_object_aligned(v), "address not aligned: " PTR_FORMAT, p2i((void*)v));68assert(is_in(v), "address not in heap range: " PTR_FORMAT, p2i((void*)v));69uint64_t pd = (uint64_t)(pointer_delta((void*)v, (void*)base(), 1));70assert(OopEncodingHeapMax > pd, "change encoding max if new encoding");71narrowOop result = narrow_oop_cast(pd >> shift());72assert(decode_raw(result) == v, "reversibility");73return result;74}7576inline narrowOop CompressedOops::encode(oop v) {77return is_null(v) ? narrowOop::null : encode_not_null(v);78}7980inline oop CompressedOops::decode_not_null(oop v) {81assert(Universe::is_in_heap(v), "object not in heap " PTR_FORMAT, p2i((void*) v));82return v;83}8485inline oop CompressedOops::decode(oop v) {86assert(Universe::is_in_heap_or_null(v), "object not in heap " PTR_FORMAT, p2i((void*) v));87return v;88}8990inline narrowOop CompressedOops::encode_not_null(narrowOop v) {91return v;92}9394inline narrowOop CompressedOops::encode(narrowOop v) {95return v;96}9798inline uint32_t CompressedOops::narrow_oop_value(oop o) {99return narrow_oop_value(encode(o));100}101102inline uint32_t CompressedOops::narrow_oop_value(narrowOop o) {103return static_cast<uint32_t>(o);104}105106template<typename T>107inline narrowOop CompressedOops::narrow_oop_cast(T i) {108static_assert(std::is_integral<T>::value, "precondition");109uint32_t narrow_value = static_cast<uint32_t>(i);110// Ensure no bits lost in conversion to uint32_t.111assert(i == static_cast<T>(narrow_value), "narrowOop overflow");112return static_cast<narrowOop>(narrow_value);113}114115static inline bool check_alignment(Klass* v) {116return (intptr_t)v % KlassAlignmentInBytes == 0;117}118119inline Klass* CompressedKlassPointers::decode_raw(narrowKlass v) {120return decode_raw(v, base());121}122123inline Klass* CompressedKlassPointers::decode_raw(narrowKlass v, address narrow_base) {124return (Klass*)(void*)((uintptr_t)narrow_base +((uintptr_t)v << shift()));125}126127inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v) {128return decode_not_null(v, base());129}130131inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v, address narrow_base) {132assert(!is_null(v), "narrow klass value can never be zero");133Klass* result = decode_raw(v, narrow_base);134assert(check_alignment(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result));135return result;136}137138inline Klass* CompressedKlassPointers::decode(narrowKlass v) {139return is_null(v) ? (Klass*)NULL : decode_not_null(v);140}141142inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v) {143return encode_not_null(v, base());144}145146inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v, address narrow_base) {147assert(!is_null(v), "klass value can never be zero");148assert(check_alignment(v), "Address not aligned");149uint64_t pd = (uint64_t)(pointer_delta((void*)v, narrow_base, 1));150assert(KlassEncodingMetaspaceMax > pd, "change encoding max if new encoding");151uint64_t result = pd >> shift();152assert((result & CONST64(0xffffffff00000000)) == 0, "narrow klass pointer overflow");153assert(decode_not_null(result, narrow_base) == v, "reversibility");154return (narrowKlass)result;155}156157inline narrowKlass CompressedKlassPointers::encode(Klass* v) {158return is_null(v) ? (narrowKlass)0 : encode_not_null(v);159}160161#endif // SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP162163164