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