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