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