1 /* 2 * Copyright (c) 2017, 2019, 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_INLINE_HPP 26 #define SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP 27 28 #include "gc/shared/collectedHeap.hpp" 29 #include "memory/universe.hpp" 30 #include "oops/compressedOops.hpp" 31 #include "oops/oop.hpp" 32 #include "utilities/align.hpp" 33 34 // Functions for encoding and decoding compressed oops. 35 // If the oops are compressed, the type passed to these overloaded functions 36 // is narrowOop. All functions are overloaded so they can be called by 37 // template functions without conditionals (the compiler instantiates via 38 // the right type and inlines the appropriate code). 39 40 // Algorithm for encoding and decoding oops from 64 bit pointers to 32 bit 41 // offset from the heap base. Saving the check for null can save instructions 42 // in inner GC loops so these are separated. 43 44 inline oop CompressedOops::decode_raw(narrowOop v) { 45 return (oop)(void*)((uintptr_t)base() + ((uintptr_t)v << shift())); 46 } 47 48 inline oop CompressedOops::decode_not_null(narrowOop v) { 49 assert(!is_null(v), "narrow oop value can never be zero"); 50 oop result = decode_raw(v); 51 assert(is_object_aligned(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result)); 52 return result; 53 } 54 55 inline oop CompressedOops::decode(narrowOop v) { 56 return is_null(v) ? (oop)NULL : decode_not_null(v); 57 } 58 59 inline narrowOop CompressedOops::encode_not_null(oop v) { 60 assert(!is_null(v), "oop value can never be zero"); 61 DEBUG_ONLY(Universe::heap()->check_oop_location(v);) 62 uint64_t pd = (uint64_t)(pointer_delta((void*)v, (void*)base(), 1)); 63 assert(OopEncodingHeapMax > pd, "change encoding max if new encoding"); 64 uint64_t result = pd >> shift(); 65 assert((result & CONST64(0xffffffff00000000)) == 0, "narrow oop overflow"); 66 assert(decode(result) == v, "reversibility"); 67 return (narrowOop)result; 68 } 69 70 inline narrowOop CompressedOops::encode(oop v) { 71 return is_null(v) ? (narrowOop)0 : encode_not_null(v); 72 } 73 74 static inline bool check_alignment(Klass* v) { 75 return (intptr_t)v % KlassAlignmentInBytes == 0; 76 } 77 78 inline Klass* CompressedKlassPointers::decode_raw(narrowKlass v) { 79 return (Klass*)(void*)((uintptr_t)base() +((uintptr_t)v << shift())); 80 } 81 82 inline Klass* CompressedKlassPointers::decode_not_null(narrowKlass v) { 83 assert(!is_null(v), "narrow klass value can never be zero"); 84 Klass* result = decode_raw(v); 85 assert(check_alignment(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result)); 86 return result; 87 } 88 89 inline Klass* CompressedKlassPointers::decode(narrowKlass v) { 90 return is_null(v) ? (Klass*)NULL : decode_not_null(v); 91 } 92 93 inline narrowKlass CompressedKlassPointers::encode_not_null(Klass* v) { 94 assert(!is_null(v), "klass value can never be zero"); 95 assert(check_alignment(v), "Address not aligned"); 96 uint64_t pd = (uint64_t)(pointer_delta((void*)v, base(), 1)); 97 assert(KlassEncodingMetaspaceMax > pd, "change encoding max if new encoding"); 98 uint64_t result = pd >> shift(); 99 assert((result & CONST64(0xffffffff00000000)) == 0, "narrow klass pointer overflow"); 100 assert(decode(result) == v, "reversibility"); 101 return (narrowKlass)result; 102 } 103 104 inline narrowKlass CompressedKlassPointers::encode(Klass* v) { 105 return is_null(v) ? (narrowKlass)0 : encode_not_null(v); 106 } 107 108 #endif // SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP