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