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