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