1 /* 2 * Copyright (c) 1997, 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_OOPSHIERARCHY_HPP 26 #define SHARE_OOPS_OOPSHIERARCHY_HPP 27 28 #include "metaprogramming/primitiveConversions.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 31 #include <type_traits> 32 33 // OBJECT hierarchy 34 // This hierarchy is a representation hierarchy, i.e. if A is a superclass 35 // of B, A's representation is a prefix of B's representation. 36 37 // Global offset instead of address for an oop within a java object. 38 enum class narrowOop : uint32_t { null = 0 }; 39 40 typedef void* OopOrNarrowOopStar; 41 42 #ifndef CHECK_UNHANDLED_OOPS 43 44 typedef class oopDesc* oop; 45 typedef class instanceOopDesc* instanceOop; 46 typedef class stackChunkOopDesc* stackChunkOop; 47 typedef class arrayOopDesc* arrayOop; 48 typedef class objArrayOopDesc* objArrayOop; 49 typedef class typeArrayOopDesc* typeArrayOop; 50 typedef class flatArrayOopDesc* flatArrayOop; 51 52 #else 53 54 // When CHECK_UNHANDLED_OOPS is defined, an "oop" is a class with a 55 // carefully chosen set of constructors and conversion operators to go 56 // to and from the underlying oopDesc pointer type. 57 // 58 // Because oop and its subclasses <type>Oop are class types, arbitrary 59 // conversions are not accepted by the compiler. Applying a cast to 60 // an oop will cause the best matched conversion operator to be 61 // invoked returning the underlying oopDesc* type if appropriate. 62 // No copy constructors, explicit user conversions or operators of 63 // numerical type should be defined within the oop class. Most C++ 64 // compilers will issue a compile time error concerning the overloading 65 // ambiguity between operators of numerical and pointer types. If 66 // a conversion to or from an oop to a numerical type is needed, 67 // use the inline template methods, cast_*_oop, defined below. 68 // 69 // Converting null to oop to Handle implicit is no longer accepted by the 70 // compiler because there are too many steps in the conversion. Use Handle() 71 // instead, which generates less code anyway. 72 73 class Thread; 74 class oopDesc; 75 76 extern "C" bool CheckUnhandledOops; 77 78 // Extra verification when creating and using oops. 79 // Used to catch broken oops as soon as possible. 80 using CheckOopFunctionPointer = void(*)(oopDesc*); 81 extern CheckOopFunctionPointer check_oop_function; 82 83 class oop { 84 oopDesc* _o; 85 86 void register_oop(); 87 void unregister_oop(); 88 89 // Extra verification of the oop 90 void check_oop() const { if (check_oop_function != nullptr && _o != nullptr) check_oop_function(_o); } 91 92 void on_usage() const { check_oop(); } 93 void on_construction() { check_oop(); if (CheckUnhandledOops) register_oop(); } 94 void on_destruction() { if (CheckUnhandledOops) unregister_oop(); } 95 96 public: 97 oop() : _o(nullptr) { on_construction(); } 98 oop(const oop& o) : _o(o._o) { on_construction(); } 99 oop(oopDesc* o) : _o(o) { on_construction(); } 100 ~oop() { 101 on_destruction(); 102 } 103 104 oopDesc* obj() const { on_usage(); return _o; } 105 106 oopDesc* operator->() const { return obj(); } 107 operator oopDesc* () const { return obj(); } 108 109 bool operator==(const oop& o) const { return obj() == o.obj(); } 110 bool operator!=(const oop& o) const { return obj() != o.obj(); } 111 112 bool operator==(std::nullptr_t) const { return obj() == nullptr; } 113 bool operator!=(std::nullptr_t) const { return obj() != nullptr; } 114 115 oop& operator=(const oop& o) { _o = o.obj(); return *this; } 116 }; 117 118 template<> 119 struct PrimitiveConversions::Translate<oop> : public std::true_type { 120 typedef oop Value; 121 typedef oopDesc* Decayed; 122 123 static Decayed decay(Value x) { return x.obj(); } 124 static Value recover(Decayed x) { return oop(x); } 125 }; 126 127 #define DEF_OOP(type) \ 128 class type##OopDesc; \ 129 class type##Oop : public oop { \ 130 public: \ 131 type##Oop() : oop() {} \ 132 type##Oop(const type##Oop& o) : oop(o) {} \ 133 type##Oop(const oop& o) : oop(o) {} \ 134 type##Oop(type##OopDesc* o) : oop((oopDesc*)o) {} \ 135 operator type##OopDesc* () const { return (type##OopDesc*)obj(); } \ 136 type##OopDesc* operator->() const { \ 137 return (type##OopDesc*)obj(); \ 138 } \ 139 type##Oop& operator=(const type##Oop& o) { \ 140 oop::operator=(o); \ 141 return *this; \ 142 } \ 143 }; \ 144 \ 145 template<> \ 146 struct PrimitiveConversions::Translate<type##Oop> : public std::true_type { \ 147 typedef type##Oop Value; \ 148 typedef type##OopDesc* Decayed; \ 149 \ 150 static Decayed decay(Value x) { return (type##OopDesc*)x.obj(); } \ 151 static Value recover(Decayed x) { return type##Oop(x); } \ 152 }; 153 154 DEF_OOP(instance); 155 DEF_OOP(stackChunk); 156 DEF_OOP(array); 157 DEF_OOP(objArray); 158 DEF_OOP(typeArray); 159 DEF_OOP(flatArray); 160 161 #endif // CHECK_UNHANDLED_OOPS 162 163 // Cast functions to convert to and from oops. 164 template <typename T> inline oop cast_to_oop(T value) { 165 return (oopDesc*)value; 166 } 167 template <typename T> inline T cast_from_oop(oop o) { 168 return (T)(CHECK_UNHANDLED_OOPS_ONLY((oopDesc*))o); 169 } 170 171 inline intptr_t p2i(narrowOop o) { 172 return static_cast<intptr_t>(o); 173 } 174 175 // The metadata hierarchy is separate from the oop hierarchy 176 177 // class MetaspaceObj 178 class ConstMethod; 179 class ConstantPoolCache; 180 class MethodData; 181 // class Metadata 182 class Method; 183 class ConstantPool; 184 185 // The klass hierarchy is separate from the oop hierarchy. 186 187 class Klass; 188 class InstanceKlass; 189 class InlineKlass; 190 class InstanceMirrorKlass; 191 class InstanceClassLoaderKlass; 192 class InstanceRefKlass; 193 class InstanceStackChunkKlass; 194 class ArrayKlass; 195 class ObjArrayKlass; 196 class TypeArrayKlass; 197 class FlatArrayKlass; 198 199 #endif // SHARE_OOPS_OOPSHIERARCHY_HPP