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_RUNTIME_HANDLES_HPP
26 #define SHARE_RUNTIME_HANDLES_HPP
27
28 #include "memory/arena.hpp"
29 #include "oops/oop.hpp"
30 #include "oops/oopsHierarchy.hpp"
31
32 class InstanceKlass;
33 class Klass;
34 class Thread;
35
36 //------------------------------------------------------------------------------------------------------------------------
37 // In order to preserve oops during garbage collection, they should be
38 // allocated and passed around via Handles within the VM. A handle is
39 // simply an extra indirection allocated in a thread local handle area.
40 //
41 // A handle is a value object, so it can be passed around as a value, can
42 // be used as a parameter w/o using &-passing, and can be returned as a
43 // return value.
44 //
45 // oop parameters and return types should be Handles whenever feasible.
46 //
47 // Handles are declared in a straight-forward manner, e.g.
48 //
49 // oop obj = ...;
50 // Handle h2(thread, obj); // allocate a new handle in thread
51 // Handle h3; // declare handle only, no allocation occurs
52 // ...
53 // h3 = h1; // make h3 refer to same indirection as h1
54 // oop obj2 = h2(); // get handle value
55 // h1->print(); // invoking operation on oop
56 //
57 // Handles are specialized for different oop types to provide extra type
58 // information and avoid unnecessary casting. For each oop type xxxOop
59 // there is a corresponding handle called xxxHandle.
60
61 //------------------------------------------------------------------------------------------------------------------------
62 // Base class for all handles. Provides overloading of frequently
63 // used operators for ease of use.
64
65 class Handle {
66 private:
67 oop* _handle;
68
69 protected:
70 oop obj() const { return _handle == nullptr ? (oop)nullptr : *_handle; }
71 oop non_null_obj() const { assert(_handle != nullptr, "resolving null handle"); return *_handle; }
72
73 public:
74 // Constructors
75 Handle() { _handle = nullptr; }
76 inline Handle(Thread* thread, oop obj);
77
78 // General access
79 oop operator () () const { return obj(); }
80 oop operator -> () const { return non_null_obj(); }
81
82 bool operator == (oop o) const { return obj() == o; }
83 bool operator != (oop o) const { return obj() != o; }
84 bool operator == (const Handle& h) const { return obj() == h.obj(); }
85 bool operator != (const Handle& h) const { return obj() != h.obj(); }
86
87 // Null checks
88 bool is_null() const { return _handle == nullptr; }
89 bool not_null() const { return _handle != nullptr; }
90
91 // Debugging
92 void print() { obj()->print(); }
93
94 // Direct interface, use very sparingly.
95 // Used by JavaCalls to quickly convert handles and to create handles static data structures.
96 // Constructor takes a dummy argument to prevent unintentional type conversion in C++.
97 Handle(oop *handle, bool dummy) { _handle = handle; }
98
99 // Raw handle access. Allows easy duplication of Handles. This can be very unsafe
100 // since duplicates is only valid as long as original handle is alive.
101 oop* raw_value() const { return _handle; }
102 static oop raw_resolve(oop *handle) { return handle == nullptr ? (oop)nullptr : *handle; }
103
104 inline void replace(oop obj);
105 };
106
107 // Specific Handles for different oop types
108 #define DEF_HANDLE(type, is_a) \
109 class type##Handle: public Handle { \
110 protected: \
111 type##Oop obj() const { return (type##Oop)Handle::obj(); } \
112 type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \
113 \
114 public: \
115 /* Constructors */ \
116 type##Handle () : Handle() {} \
117 inline type##Handle (Thread* thread, type##Oop obj); \
118 type##Handle (oop *handle, bool dummy) : Handle(handle, dummy) {} \
119 \
120 /* Operators for ease of use */ \
121 type##Oop operator () () const { return obj(); } \
122 type##Oop operator -> () const { return non_null_obj(); } \
123 };
124
125
126 DEF_HANDLE(instance , is_instance_noinline )
127 DEF_HANDLE(stackChunk , is_stackChunk_noinline )
128 DEF_HANDLE(array , is_array_noinline )
129 DEF_HANDLE(objArray , is_objArray_noinline )
130 DEF_HANDLE(typeArray , is_typeArray_noinline )
131
132 //------------------------------------------------------------------------------------------------------------------------
133
134 // Metadata Handles. Unlike oop Handles these are needed to prevent metadata
135 // from being reclaimed by RedefineClasses.
136 // Metadata Handles should be passed around as const references to avoid copy construction
137 // and destruction for parameters.
138
139 // Specific Handles for different oop types
140 #define DEF_METADATA_HANDLE(name, type) \
141 class name##Handle; \
142 class name##Handle : public StackObj { \
143 type* _value; \
144 Thread* _thread; \
145 protected: \
146 type* obj() const { return _value; } \
147 type* non_null_obj() const { assert(_value != nullptr, "resolving null _value"); return _value; } \
148 \
149 public: \
150 /* Constructors */ \
151 name##Handle () : _value(nullptr), _thread(nullptr) {} \
152 name##Handle (Thread* thread, type* obj); \
153 \
154 name##Handle (const name##Handle &h); \
155 name##Handle& operator=(const name##Handle &s); \
156 \
157 /* Destructor */ \
158 ~name##Handle (); \
159 void remove(); \
160 \
161 /* Operators for ease of use */ \
162 type* operator () () const { return obj(); } \
163 type* operator -> () const { return non_null_obj(); } \
164 \
165 bool operator == (type* o) const { return obj() == o; } \
166 bool operator == (const name##Handle& h) const { return obj() == h.obj(); } \
167 \
168 /* Null checks */ \
169 bool is_null() const { return _value == nullptr; } \
170 bool not_null() const { return _value != nullptr; } \
171 };
172
173
174 DEF_METADATA_HANDLE(method, Method)
175 DEF_METADATA_HANDLE(constantPool, ConstantPool)
176
177 //------------------------------------------------------------------------------------------------------------------------
178 // Thread local handle area
179 class HandleArea: public Arena {
180 friend class HandleMark;
181 friend class NoHandleMark;
182 friend class ResetNoHandleMark;
183 #ifdef ASSERT
184 int _handle_mark_nesting;
185 int _no_handle_mark_nesting;
186 #endif
187 public:
188 // Constructor
189 HandleArea(MemTag mem_tag) : Arena(mem_tag, Tag::tag_ha, Chunk::tiny_size) {
190 DEBUG_ONLY(_handle_mark_nesting = 0);
191 DEBUG_ONLY(_no_handle_mark_nesting = 0);
192 }
193
194 // Handle allocation
195 private:
196 oop* real_allocate_handle(oop obj) {
197 oop* handle = (oop*)internal_amalloc(oopSize);
198 *handle = obj;
199 return handle;
200 }
201 public:
202 #ifdef ASSERT
203 oop* allocate_handle(oop obj);
204 oop* allocate_null_handle();
205 #else
206 oop* allocate_handle(oop obj) { return real_allocate_handle(obj); }
207 oop* allocate_null_handle() { return allocate_handle(nullptr); }
208 #endif
209
210 // Garbage collection support
211 void oops_do(OopClosure* f);
212
213 DEBUG_ONLY(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; })
214 };
215
216
217 //------------------------------------------------------------------------------------------------------------------------
218 // Handles are allocated in a (growable) thread local handle area. Deallocation
219 // is managed using a HandleMark. It should normally not be necessary to use
220 // HandleMarks manually.
221 //
222 // A HandleMark constructor will record the current handle area top, and the
223 // destructor will reset the top, destroying all handles allocated in between.
224 // The following code will therefore NOT work:
225 //
226 // Handle h;
227 // {
228 // HandleMark hm(THREAD);
229 // h = Handle(THREAD, obj);
230 // }
231 // h()->print(); // WRONG, h destroyed by HandleMark destructor.
232 //
233 // If h has to be preserved, it can be converted to an oop or a local JNI handle
234 // across the HandleMark boundary.
235
236 // The base class of HandleMark should have been StackObj but we also heap allocate
237 // a HandleMark when a thread is created. The operator new is for this special case.
238
239 class HandleMark {
240 private:
241 Thread *_thread; // thread that owns this mark
242 HandleArea *_area; // saved handle area
243 Chunk *_chunk; // saved arena chunk
244 char *_hwm, *_max; // saved arena info
245 size_t _size_in_bytes; // size of handle area
246 // Link to previous active HandleMark in thread
247 HandleMark* _previous_handle_mark;
248
249 void initialize(Thread* thread); // common code for constructors
250 void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; }
251 HandleMark* previous_handle_mark() const { return _previous_handle_mark; }
252
253 size_t size_in_bytes() const { return _size_in_bytes; }
254 // remove all chunks beginning with the next
255 void chop_later_chunks();
256 public:
257 HandleMark(Thread* thread) { initialize(thread); }
258 ~HandleMark();
259
260 // Functions used by HandleMarkCleaner
261 // called in the constructor of HandleMarkCleaner
262 void push();
263 // called in the destructor of HandleMarkCleaner
264 void pop_and_restore();
265 // overloaded operators
266 void* operator new(size_t size) throw();
267 void* operator new [](size_t size) throw();
268 void operator delete(void* p);
269 void operator delete[](void* p);
270 };
271
272 //------------------------------------------------------------------------------------------------------------------------
273 // A NoHandleMark stack object will verify that no handles are allocated
274 // in its scope. Enabled in debug mode only.
275
276 class NoHandleMark: public StackObj {
277 public:
278 #ifdef ASSERT
279 NoHandleMark();
280 ~NoHandleMark();
281 #else
282 NoHandleMark() {}
283 ~NoHandleMark() {}
284 #endif
285 };
286
287
288 // ResetNoHandleMark is called in a context where there is an enclosing
289 // NoHandleMark. A thread in _thread_in_native must not create handles so
290 // this is used when transitioning via ThreadInVMfromNative.
291 class ResetNoHandleMark: public StackObj {
292 int _no_handle_mark_nesting;
293 public:
294 #ifdef ASSERT
295 ResetNoHandleMark();
296 ~ResetNoHandleMark();
297 #else
298 ResetNoHandleMark() {}
299 ~ResetNoHandleMark() {}
300 #endif
301 };
302
303 // The HandleMarkCleaner is a faster version of HandleMark.
304 // It relies on the fact that there is a HandleMark further
305 // down the stack (in JavaCalls::call_helper), and just resets
306 // to the saved values in that HandleMark.
307
308 class HandleMarkCleaner: public StackObj {
309 private:
310 Thread* _thread;
311 public:
312 inline HandleMarkCleaner(Thread* thread);
313 inline ~HandleMarkCleaner();
314 };
315
316 #endif // SHARE_RUNTIME_HANDLES_HPP