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_OOP_INLINE_HPP
26 #define SHARE_OOPS_OOP_INLINE_HPP
27
28 #include "oops/oop.hpp"
29
30 #include "memory/universe.hpp"
31 #include "memory/iterator.inline.hpp"
32 #include "oops/access.inline.hpp"
33 #include "oops/arrayKlass.hpp"
34 #include "oops/arrayOop.hpp"
35 #include "oops/compressedKlass.inline.hpp"
36 #include "oops/instanceKlass.hpp"
37 #include "oops/objLayout.inline.hpp"
38 #include "oops/markWord.inline.hpp"
39 #include "oops/oopsHierarchy.hpp"
40 #include "runtime/atomic.hpp"
41 #include "runtime/globals.hpp"
42 #include "utilities/align.hpp"
43 #include "utilities/debug.hpp"
44 #include "utilities/macros.hpp"
45 #include "utilities/globalDefinitions.hpp"
46
47 // Implementation of all inlined member functions defined in oop.hpp
48 // We need a separate file to avoid circular references
49
50 markWord oopDesc::mark() const {
51 return Atomic::load(&_mark);
52 }
53
54 markWord oopDesc::mark_acquire() const {
55 return Atomic::load_acquire(&_mark);
56 }
57
58 markWord* oopDesc::mark_addr() const {
59 return (markWord*) &_mark;
60 }
61
62 void oopDesc::set_mark(markWord m) {
63 Atomic::store(&_mark, m);
64 }
65
66 void oopDesc::set_mark(HeapWord* mem, markWord m) {
67 *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m;
68 }
69
70 void oopDesc::release_set_mark(HeapWord* mem, markWord m) {
71 Atomic::release_store((markWord*)(((char*)mem) + mark_offset_in_bytes()), m);
72 }
73
74 void oopDesc::release_set_mark(markWord m) {
75 Atomic::release_store(&_mark, m);
76 }
77
78 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
79 return Atomic::cmpxchg(&_mark, old_mark, new_mark);
80 }
81
82 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
83 return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
84 }
85
86 markWord oopDesc::prototype_mark() const {
87 if (UseCompactObjectHeaders) {
88 return klass()->prototype_header();
89 } else {
90 return markWord::prototype();
91 }
92 }
93
94 void oopDesc::init_mark() {
95 set_mark(prototype_mark());
96 }
97
98 Klass* oopDesc::klass() const {
99 switch (ObjLayout::klass_mode()) {
100 case ObjLayout::Compact:
101 return mark().klass();
102 case ObjLayout::Compressed:
103 return CompressedKlassPointers::decode_not_null(_metadata._compressed_klass);
104 default:
105 return _metadata._klass;
106 }
107 }
108
109 Klass* oopDesc::klass_or_null() const {
110 switch (ObjLayout::klass_mode()) {
111 case ObjLayout::Compact:
112 return mark().klass_or_null();
113 case ObjLayout::Compressed:
114 return CompressedKlassPointers::decode(_metadata._compressed_klass);
115 default:
116 return _metadata._klass;
117 }
118 }
119
120 Klass* oopDesc::klass_or_null_acquire() const {
121 switch (ObjLayout::klass_mode()) {
122 case ObjLayout::Compact:
123 return mark_acquire().klass();
124 case ObjLayout::Compressed: {
125 narrowKlass narrow_klass = Atomic::load_acquire(&_metadata._compressed_klass);
126 return CompressedKlassPointers::decode(narrow_klass);
127 }
128 default:
129 return Atomic::load_acquire(&_metadata._klass);
130 }
131 }
132
133 Klass* oopDesc::klass_without_asserts() const {
134 switch (ObjLayout::klass_mode()) {
135 case ObjLayout::Compact:
136 return mark().klass_without_asserts();
137 case ObjLayout::Compressed:
138 return CompressedKlassPointers::decode_without_asserts(_metadata._compressed_klass);
139 default:
140 return _metadata._klass;
141 }
142 }
143
144 narrowKlass oopDesc::narrow_klass() const {
145 switch (ObjLayout::klass_mode()) {
146 case ObjLayout::Compact:
147 return mark().narrow_klass();
148 case ObjLayout::Compressed:
149 return _metadata._compressed_klass;
150 default:
151 ShouldNotReachHere();
152 }
153 }
154
155 void oopDesc::set_klass(Klass* k) {
156 assert(Universe::is_bootstrapping() || (k != nullptr && k->is_klass()), "incorrect Klass");
157 assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers");
158 if (UseCompressedClassPointers) {
159 _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k);
160 } else {
161 _metadata._klass = k;
162 }
163 }
164
165 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
166 assert(Universe::is_bootstrapping() || (k != nullptr && k->is_klass()), "incorrect Klass");
167 assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers");
168 char* raw_mem = ((char*)mem + klass_offset_in_bytes());
169 if (UseCompressedClassPointers) {
170 Atomic::release_store((narrowKlass*)raw_mem,
171 CompressedKlassPointers::encode_not_null(k));
172 } else {
173 Atomic::release_store((Klass**)raw_mem, k);
174 }
175 }
176
177 void oopDesc::set_klass_gap(HeapWord* mem, int v) {
178 assert(has_klass_gap(), "precondition");
179 *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
180 }
181
182 bool oopDesc::is_a(Klass* k) const {
183 return klass()->is_subtype_of(k);
184 }
185
186 size_t oopDesc::size() {
187 return size_given_klass(klass());
188 }
189
190 size_t oopDesc::size_given_klass(Klass* klass) {
191 int lh = klass->layout_helper();
192 size_t s;
193
194 // lh is now a value computed at class initialization that may hint
195 // at the size. For instances, this is positive and equal to the
196 // size. For arrays, this is negative and provides log2 of the
197 // array element size. For other oops, it is zero and thus requires
198 // a virtual call.
199 //
200 // We go to all this trouble because the size computation is at the
201 // heart of phase 2 of mark-compaction, and called for every object,
202 // alive or dead. So the speed here is equal in importance to the
203 // speed of allocation.
204
205 if (lh > Klass::_lh_neutral_value) {
206 if (!Klass::layout_helper_needs_slow_path(lh)) {
207 s = lh >> LogHeapWordSize; // deliver size scaled by wordSize
208 } else {
209 s = klass->oop_size(this);
210 }
211 } else if (lh <= Klass::_lh_neutral_value) {
212 // The most common case is instances; fall through if so.
213 if (lh < Klass::_lh_neutral_value) {
214 // Second most common case is arrays. We have to fetch the
215 // length of the array, shift (multiply) it appropriately,
216 // up to wordSize, add the header, and align to object size.
217 size_t size_in_bytes;
218 size_t array_length = (size_t) ((arrayOop)this)->length();
219 size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh);
220 size_in_bytes += Klass::layout_helper_header_size(lh);
221
222 // This code could be simplified, but by keeping array_header_in_bytes
223 // in units of bytes and doing it this way we can round up just once,
224 // skipping the intermediate round to HeapWordSize.
225 s = align_up(size_in_bytes, MinObjAlignmentInBytes) / HeapWordSize;
226
227 assert(s == klass->oop_size(this), "wrong array object size");
228 } else {
229 // Must be zero, so bite the bullet and take the virtual call.
230 s = klass->oop_size(this);
231 }
232 }
233
234 assert(s > 0, "Oop size must be greater than zero, not %zu", s);
235 assert(is_object_aligned(s), "Oop size is not properly aligned: %zu", s);
236 return s;
237 }
238
239 bool oopDesc::is_instance() const { return klass()->is_instance_klass(); }
240 bool oopDesc::is_instanceRef() const { return klass()->is_reference_instance_klass(); }
241 bool oopDesc::is_stackChunk() const { return klass()->is_stack_chunk_instance_klass(); }
242 bool oopDesc::is_array() const { return klass()->is_array_klass(); }
243 bool oopDesc::is_objArray() const { return klass()->is_objArray_klass(); }
244 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); }
245
246 template<typename T>
247 T* oopDesc::field_addr(int offset) const { return reinterpret_cast<T*>(cast_from_oop<intptr_t>(as_oop()) + offset); }
248
249 template <typename T>
250 size_t oopDesc::field_offset(T* p) const { return pointer_delta((void*)p, (void*)this, 1); }
251
252 template <DecoratorSet decorators>
253 inline oop oopDesc::obj_field_access(int offset) const { return HeapAccess<decorators>::oop_load_at(as_oop(), offset); }
254 inline oop oopDesc::obj_field(int offset) const { return HeapAccess<>::oop_load_at(as_oop(), offset); }
255
256 inline void oopDesc::obj_field_put(int offset, oop value) { HeapAccess<>::oop_store_at(as_oop(), offset, value); }
257 template <DecoratorSet decorators>
258 inline void oopDesc::obj_field_put_access(int offset, oop value) { HeapAccess<decorators>::oop_store_at(as_oop(), offset, value); }
259
260 inline jbyte oopDesc::byte_field(int offset) const { return *field_addr<jbyte>(offset); }
261 inline void oopDesc::byte_field_put(int offset, jbyte value) { *field_addr<jbyte>(offset) = value; }
262
263 inline jchar oopDesc::char_field(int offset) const { return *field_addr<jchar>(offset); }
264 inline void oopDesc::char_field_put(int offset, jchar value) { *field_addr<jchar>(offset) = value; }
265
266 inline jboolean oopDesc::bool_field(int offset) const { return *field_addr<jboolean>(offset); }
267 inline void oopDesc::bool_field_put(int offset, jboolean value) { *field_addr<jboolean>(offset) = jboolean(value & 1); }
268 inline jboolean oopDesc::bool_field_volatile(int offset) const { return RawAccess<MO_SEQ_CST>::load(field_addr<jboolean>(offset)); }
269 inline void oopDesc::bool_field_put_volatile(int offset, jboolean value) { RawAccess<MO_SEQ_CST>::store(field_addr<jboolean>(offset), jboolean(value & 1)); }
270 inline jshort oopDesc::short_field(int offset) const { return *field_addr<jshort>(offset); }
271 inline void oopDesc::short_field_put(int offset, jshort value) { *field_addr<jshort>(offset) = value; }
272
273 inline jint oopDesc::int_field(int offset) const { return *field_addr<jint>(offset); }
274 inline void oopDesc::int_field_put(int offset, jint value) { *field_addr<jint>(offset) = value; }
275 inline jint oopDesc::int_field_relaxed(int offset) const { return Atomic::load(field_addr<jint>(offset)); }
276 inline void oopDesc::int_field_put_relaxed(int offset, jint value) { Atomic::store(field_addr<jint>(offset), value); }
277
278 inline jlong oopDesc::long_field(int offset) const { return *field_addr<jlong>(offset); }
279 inline void oopDesc::long_field_put(int offset, jlong value) { *field_addr<jlong>(offset) = value; }
280
281 inline jfloat oopDesc::float_field(int offset) const { return *field_addr<jfloat>(offset); }
282 inline void oopDesc::float_field_put(int offset, jfloat value) { *field_addr<jfloat>(offset) = value; }
283
284 inline jdouble oopDesc::double_field(int offset) const { return *field_addr<jdouble>(offset); }
285 inline void oopDesc::double_field_put(int offset, jdouble value) { *field_addr<jdouble>(offset) = value; }
286
287 bool oopDesc::is_locked() const {
288 return mark().is_locked();
289 }
290
291 bool oopDesc::is_unlocked() const {
292 return mark().is_unlocked();
293 }
294
295 bool oopDesc::is_gc_marked() const {
296 return mark().is_marked();
297 }
298
299 // Used by scavengers
300 bool oopDesc::is_forwarded() const {
301 return mark().is_forwarded();
302 }
303
304 bool oopDesc::is_self_forwarded() const {
305 return mark().is_self_forwarded();
306 }
307
308 // Used by scavengers
309 void oopDesc::forward_to(oop p) {
310 assert(cast_from_oop<oopDesc*>(p) != this,
311 "must not be used for self-forwarding, use forward_to_self() instead");
312 markWord m = markWord::encode_pointer_as_mark(p);
313 assert(m.decode_pointer() == p, "encoding must be reversible");
314 set_mark(m);
315 }
316
317 void oopDesc::forward_to_self() {
318 set_mark(mark().set_self_forwarded());
319 }
320
321 oop oopDesc::cas_set_forwardee(markWord new_mark, markWord compare, atomic_memory_order order) {
322 markWord old_mark = cas_set_mark(new_mark, compare, order);
323 if (old_mark == compare) {
324 return nullptr;
325 } else {
326 assert(old_mark.is_forwarded(), "must be forwarded here");
327 return forwardee(old_mark);
328 }
329 }
330
331 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
332 assert(cast_from_oop<oopDesc*>(p) != this,
333 "must not be used for self-forwarding, use forward_to_self_atomic() instead");
334 markWord m = markWord::encode_pointer_as_mark(p);
335 assert(forwardee(m) == p, "encoding must be reversible");
336 return cas_set_forwardee(m, compare, order);
337 }
338
339 oop oopDesc::forward_to_self_atomic(markWord old_mark, atomic_memory_order order) {
340 markWord new_mark = old_mark.set_self_forwarded();
341 assert(forwardee(new_mark) == cast_to_oop(this), "encoding must be reversible");
342 return cas_set_forwardee(new_mark, old_mark, order);
343 }
344
345 oop oopDesc::forwardee(markWord mark) const {
346 assert(mark.is_forwarded(), "only decode when actually forwarded");
347 if (mark.is_self_forwarded()) {
348 return cast_to_oop(this);
349 } else {
350 return mark.forwardee();
351 }
352 }
353
354 // Note that the forwardee is not the same thing as the displaced_mark.
355 // The forwardee is used when copying during scavenge and mark-sweep.
356 // It does need to clear the low two locking- and GC-related bits.
357 oop oopDesc::forwardee() const {
358 return forwardee(mark());
359 }
360
361 void oopDesc::unset_self_forwarded() {
362 set_mark(mark().unset_self_forwarded());
363 }
364
365 // The following method needs to be MT safe.
366 uint oopDesc::age() const {
367 markWord m = mark();
368 assert(!m.is_marked(), "Attempt to read age from forwarded mark");
369 if (m.has_displaced_mark_helper()) {
370 return m.displaced_mark_helper().age();
371 } else {
372 return m.age();
373 }
374 }
375
376 void oopDesc::incr_age() {
377 markWord m = mark();
378 assert(!m.is_marked(), "Attempt to increment age of forwarded mark");
379 if (m.has_displaced_mark_helper()) {
380 m.set_displaced_mark_helper(m.displaced_mark_helper().incr_age());
381 } else {
382 set_mark(m.incr_age());
383 }
384 }
385
386 template <typename OopClosureType>
387 void oopDesc::oop_iterate(OopClosureType* cl) {
388 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass());
389 }
390
391 template <typename OopClosureType>
392 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) {
393 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr);
394 }
395
396 template <typename OopClosureType>
397 size_t oopDesc::oop_iterate_size(OopClosureType* cl) {
398 Klass* k = klass();
399 size_t size = size_given_klass(k);
400 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
401 return size;
402 }
403
404 template <typename OopClosureType>
405 size_t oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
406 Klass* k = klass();
407 size_t size = size_given_klass(k);
408 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
409 return size;
410 }
411
412 template <typename OopClosureType>
413 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
414 oop_iterate_backwards(cl, klass());
415 }
416
417 template <typename OopClosureType>
418 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {
419 // In this assert, we cannot safely access the Klass* with compact headers.
420 assert(k == klass(), "wrong klass");
421 OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
422 }
423
424 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
425 return obj == nullptr || obj->klass()->is_subtype_of(klass);
426 }
427
428 intptr_t oopDesc::identity_hash() {
429 // Fast case; if the object is unlocked and the hash value is set, no locking is needed
430 // Note: The mark must be read into local variable to avoid concurrent updates.
431 markWord mrk = mark();
432 if (mrk.is_unlocked() && !mrk.has_no_hash()) {
433 return mrk.hash();
434 } else if (mrk.is_marked()) {
435 return mrk.hash();
436 } else {
437 return slow_identity_hash();
438 }
439 }
440
441 // This checks fast simple case of whether the oop has_no_hash,
442 // to optimize JVMTI table lookup.
443 bool oopDesc::fast_no_hash_check() {
444 markWord mrk = mark_acquire();
445 assert(!mrk.is_marked(), "should never be marked");
446 return mrk.is_unlocked() && mrk.has_no_hash();
447 }
448
449 bool oopDesc::has_displaced_mark() const {
450 return mark().has_displaced_mark_helper();
451 }
452
453 markWord oopDesc::displaced_mark() const {
454 return mark().displaced_mark_helper();
455 }
456
457 void oopDesc::set_displaced_mark(markWord m) {
458 mark().set_displaced_mark_helper(m);
459 }
460
461 bool oopDesc::mark_must_be_preserved() const {
462 return mark_must_be_preserved(mark());
463 }
464
465 bool oopDesc::mark_must_be_preserved(markWord m) const {
466 return m.must_be_preserved();
467 }
468
469 #endif // SHARE_OOPS_OOP_INLINE_HPP