1 /*
   2  * Copyright (c) 1997, 2019, 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 "gc/shared/collectedHeap.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/access.inline.hpp"
  31 #include "oops/arrayKlass.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/compressedOops.inline.hpp"
  34 #include "oops/klass.inline.hpp"
  35 #include "oops/markWord.inline.hpp"
  36 #include "oops/oop.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/orderAccess.hpp"
  39 #include "runtime/os.hpp"
  40 #include "utilities/align.hpp"
  41 #include "utilities/macros.hpp"
  42 
  43 // Implementation of all inlined member functions defined in oop.hpp
  44 // We need a separate file to avoid circular references
  45 
  46 markWord oopDesc::mark() const {
  47   uintptr_t v = HeapAccess<MO_VOLATILE>::load_at(as_oop(), mark_offset_in_bytes());
  48   return markWord(v);
  49 }
  50 
  51 markWord oopDesc::mark_raw() const {
  52   return Atomic::load(&_mark);
  53 }
  54 
  55 markWord* oopDesc::mark_addr_raw() const {
  56   return (markWord*) &_mark;
  57 }
  58 
  59 void oopDesc::set_mark(markWord m) {
  60   HeapAccess<MO_VOLATILE>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
  61 }
  62 
  63 void oopDesc::set_mark_raw(markWord m) {
  64   Atomic::store(m, &_mark);
  65 }
  66 
  67 void oopDesc::set_mark_raw(HeapWord* mem, markWord m) {
  68   *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m;
  69 }
  70 
  71 void oopDesc::release_set_mark(markWord m) {
  72   HeapAccess<MO_RELEASE>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
  73 }
  74 
  75 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
  76   uintptr_t v = HeapAccess<>::atomic_cmpxchg_at(new_mark.value(), as_oop(), mark_offset_in_bytes(), old_mark.value());
  77   return markWord(v);
  78 }
  79 
  80 markWord oopDesc::cas_set_mark_raw(markWord new_mark, markWord old_mark, atomic_memory_order order) {
  81   return Atomic::cmpxchg(new_mark, &_mark, old_mark, order);
  82 }
  83 
  84 void oopDesc::init_mark() {
  85   set_mark(markWord::prototype_for_klass(klass()));
  86 }
  87 
  88 void oopDesc::init_mark_raw() {
  89   set_mark_raw(markWord::prototype_for_klass(klass()));
  90 }
  91 
  92 Klass* oopDesc::klass() const {
  93   if (UseCompressedClassPointers) {
  94     return CompressedKlassPointers::decode_not_null(_metadata._compressed_klass);
  95   } else {
  96     return _metadata._klass;
  97   }
  98 }
  99 
 100 Klass* oopDesc::klass_or_null() const volatile {
 101   if (UseCompressedClassPointers) {
 102     return CompressedKlassPointers::decode(_metadata._compressed_klass);
 103   } else {
 104     return _metadata._klass;
 105   }
 106 }
 107 
 108 Klass* oopDesc::klass_or_null_acquire() const volatile {
 109   if (UseCompressedClassPointers) {
 110     // Workaround for non-const load_acquire parameter.
 111     const volatile narrowKlass* addr = &_metadata._compressed_klass;
 112     volatile narrowKlass* xaddr = const_cast<volatile narrowKlass*>(addr);
 113     return CompressedKlassPointers::decode(OrderAccess::load_acquire(xaddr));
 114   } else {
 115     return OrderAccess::load_acquire(&_metadata._klass);
 116   }
 117 }
 118 
 119 Klass** oopDesc::klass_addr(HeapWord* mem) {
 120   // Only used internally and with CMS and will not work with
 121   // UseCompressedOops
 122   assert(!UseCompressedClassPointers, "only supported with uncompressed klass pointers");
 123   ByteSize offset = byte_offset_of(oopDesc, _metadata._klass);
 124   return (Klass**) (((char*)mem) + in_bytes(offset));
 125 }
 126 
 127 narrowKlass* oopDesc::compressed_klass_addr(HeapWord* mem) {
 128   assert(UseCompressedClassPointers, "only called by compressed klass pointers");
 129   ByteSize offset = byte_offset_of(oopDesc, _metadata._compressed_klass);
 130   return (narrowKlass*) (((char*)mem) + in_bytes(offset));
 131 }
 132 
 133 Klass** oopDesc::klass_addr() {
 134   return klass_addr((HeapWord*)this);
 135 }
 136 
 137 narrowKlass* oopDesc::compressed_klass_addr() {
 138   return compressed_klass_addr((HeapWord*)this);
 139 }
 140 
 141 #define CHECK_SET_KLASS(k)                                                \
 142   do {                                                                    \
 143     assert(Universe::is_bootstrapping() || k != NULL, "NULL Klass");      \
 144     assert(Universe::is_bootstrapping() || k->is_klass(), "not a Klass"); \
 145   } while (0)
 146 
 147 void oopDesc::set_klass(Klass* k) {
 148   CHECK_SET_KLASS(k);
 149   if (UseCompressedClassPointers) {
 150     *compressed_klass_addr() = CompressedKlassPointers::encode_not_null(k);
 151   } else {
 152     *klass_addr() = k;
 153   }
 154 }
 155 
 156 void oopDesc::release_set_klass(HeapWord* mem, Klass* klass) {
 157   CHECK_SET_KLASS(klass);
 158   if (UseCompressedClassPointers) {
 159     OrderAccess::release_store(compressed_klass_addr(mem),
 160                                CompressedKlassPointers::encode_not_null(klass));
 161   } else {
 162     OrderAccess::release_store(klass_addr(mem), klass);
 163   }
 164 }
 165 
 166 #undef CHECK_SET_KLASS
 167 
 168 int oopDesc::klass_gap() const {
 169   return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes());
 170 }
 171 
 172 void oopDesc::set_klass_gap(HeapWord* mem, int v) {
 173   if (UseCompressedClassPointers) {
 174     *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
 175   }
 176 }
 177 
 178 void oopDesc::set_klass_gap(int v) {
 179   set_klass_gap((HeapWord*)this, v);
 180 }
 181 
 182 void oopDesc::set_klass_to_list_ptr(oop k) {
 183   // This is only to be used during GC, for from-space objects, so no
 184   // barrier is needed.
 185   if (UseCompressedClassPointers) {
 186     _metadata._compressed_klass = (narrowKlass)CompressedOops::encode(k);  // may be null (parnew overflow handling)
 187   } else {
 188     _metadata._klass = (Klass*)(address)k;
 189   }
 190 }
 191 
 192 oop oopDesc::list_ptr_from_klass() {
 193   // This is only to be used during GC, for from-space objects.
 194   if (UseCompressedClassPointers) {
 195     return CompressedOops::decode((narrowOop)_metadata._compressed_klass);
 196   } else {
 197     // Special case for GC
 198     return (oop)(address)_metadata._klass;
 199   }
 200 }
 201 
 202 bool oopDesc::is_a(Klass* k) const {
 203   return klass()->is_subtype_of(k);
 204 }
 205 
 206 int oopDesc::size()  {
 207   return size_given_klass(klass());
 208 }
 209 
 210 int oopDesc::size_given_klass(Klass* klass)  {
 211   int lh = klass->layout_helper();
 212   int s;
 213 
 214   // lh is now a value computed at class initialization that may hint
 215   // at the size.  For instances, this is positive and equal to the
 216   // size.  For arrays, this is negative and provides log2 of the
 217   // array element size.  For other oops, it is zero and thus requires
 218   // a virtual call.
 219   //
 220   // We go to all this trouble because the size computation is at the
 221   // heart of phase 2 of mark-compaction, and called for every object,
 222   // alive or dead.  So the speed here is equal in importance to the
 223   // speed of allocation.
 224 
 225   if (lh > Klass::_lh_neutral_value) {
 226     if (!Klass::layout_helper_needs_slow_path(lh)) {
 227       s = lh >> LogHeapWordSize;  // deliver size scaled by wordSize
 228     } else {
 229       s = klass->oop_size(this);
 230     }
 231   } else if (lh <= Klass::_lh_neutral_value) {
 232     // The most common case is instances; fall through if so.
 233     if (lh < Klass::_lh_neutral_value) {
 234       // Second most common case is arrays.  We have to fetch the
 235       // length of the array, shift (multiply) it appropriately,
 236       // up to wordSize, add the header, and align to object size.
 237       size_t size_in_bytes;
 238       size_t array_length = (size_t) ((arrayOop)this)->length();
 239       size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh);
 240       size_in_bytes += Klass::layout_helper_header_size(lh);
 241 
 242       // This code could be simplified, but by keeping array_header_in_bytes
 243       // in units of bytes and doing it this way we can round up just once,
 244       // skipping the intermediate round to HeapWordSize.
 245       s = (int)(align_up(size_in_bytes, MinObjAlignmentInBytes) / HeapWordSize);
 246 
 247       // ParNew (used by CMS), UseParallelGC and UseG1GC can change the length field
 248       // of an "old copy" of an object array in the young gen so it indicates
 249       // the grey portion of an already copied array. This will cause the first
 250       // disjunct below to fail if the two comparands are computed across such
 251       // a concurrent change.
 252       // ParNew also runs with promotion labs (which look like int
 253       // filler arrays) which are subject to changing their declared size
 254       // when finally retiring a PLAB; this also can cause the first disjunct
 255       // to fail for another worker thread that is concurrently walking the block
 256       // offset table. Both these invariant failures are benign for their
 257       // current uses; we relax the assertion checking to cover these two cases below:
 258       //     is_objArray() && is_forwarded()   // covers first scenario above
 259       //  || is_typeArray()                    // covers second scenario above
 260       // If and when UseParallelGC uses the same obj array oop stealing/chunking
 261       // technique, we will need to suitably modify the assertion.
 262       assert((s == klass->oop_size(this)) ||
 263              (Universe::heap()->is_gc_active() &&
 264               ((is_typeArray() && UseConcMarkSweepGC) ||
 265                (is_objArray()  && is_forwarded() && (UseConcMarkSweepGC || UseParallelGC || UseG1GC)))),
 266              "wrong array object size");
 267     } else {
 268       // Must be zero, so bite the bullet and take the virtual call.
 269       s = klass->oop_size(this);
 270     }
 271   }
 272 
 273   assert(s > 0, "Oop size must be greater than zero, not %d", s);
 274   assert(is_object_aligned(s), "Oop size is not properly aligned: %d", s);
 275   return s;
 276 }
 277 
 278 bool oopDesc::is_instance()  const { return klass()->is_instance_klass();  }
 279 bool oopDesc::is_array()     const { return klass()->is_array_klass();     }
 280 bool oopDesc::is_objArray()  const { return klass()->is_objArray_klass();  }
 281 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); }
 282 
 283 void*    oopDesc::field_addr_raw(int offset)     const { return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + offset); }
 284 void*    oopDesc::field_addr(int offset)         const { return Access<>::resolve(as_oop())->field_addr_raw(offset); }
 285 
 286 template <class T>
 287 T*       oopDesc::obj_field_addr_raw(int offset) const { return (T*) field_addr_raw(offset); }
 288 
 289 template <typename T>
 290 size_t   oopDesc::field_offset(T* p) const { return pointer_delta((void*)p, (void*)this, 1); }
 291 
 292 template <DecoratorSet decorators>
 293 inline oop  oopDesc::obj_field_access(int offset) const             { return HeapAccess<decorators>::oop_load_at(as_oop(), offset); }
 294 inline oop  oopDesc::obj_field(int offset) const                    { return HeapAccess<>::oop_load_at(as_oop(), offset);  }
 295 
 296 inline void oopDesc::obj_field_put(int offset, oop value)           { HeapAccess<>::oop_store_at(as_oop(), offset, value); }
 297 
 298 inline jbyte oopDesc::byte_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 299 inline void  oopDesc::byte_field_put(int offset, jbyte value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 300 
 301 inline jchar oopDesc::char_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 302 inline void  oopDesc::char_field_put(int offset, jchar value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 303 
 304 inline jboolean oopDesc::bool_field(int offset) const               { return HeapAccess<>::load_at(as_oop(), offset); }
 305 inline void     oopDesc::bool_field_put(int offset, jboolean value) { HeapAccess<>::store_at(as_oop(), offset, jboolean(value & 1)); }
 306 inline jboolean oopDesc::bool_field_volatile(int offset) const      { return HeapAccess<MO_SEQ_CST>::load_at(as_oop(), offset); }
 307 inline void     oopDesc::bool_field_put_volatile(int offset, jboolean value) { HeapAccess<MO_SEQ_CST>::store_at(as_oop(), offset, jboolean(value & 1)); }
 308 inline jshort oopDesc::short_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 309 inline void   oopDesc::short_field_put(int offset, jshort value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 310 
 311 inline jint oopDesc::int_field(int offset) const                    { return HeapAccess<>::load_at(as_oop(), offset);  }
 312 inline jint oopDesc::int_field_raw(int offset) const                { return RawAccess<>::load_at(as_oop(), offset);   }
 313 inline void oopDesc::int_field_put(int offset, jint value)          { HeapAccess<>::store_at(as_oop(), offset, value); }
 314 
 315 inline jlong oopDesc::long_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 316 inline void  oopDesc::long_field_put(int offset, jlong value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 317 
 318 inline jfloat oopDesc::float_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 319 inline void   oopDesc::float_field_put(int offset, jfloat value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 320 
 321 inline jdouble oopDesc::double_field(int offset) const              { return HeapAccess<>::load_at(as_oop(), offset);  }
 322 inline void    oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); }
 323 
 324 bool oopDesc::is_locked() const {
 325   return mark().is_locked();
 326 }
 327 
 328 bool oopDesc::is_unlocked() const {
 329   return mark().is_unlocked();
 330 }
 331 
 332 bool oopDesc::has_bias_pattern() const {
 333   return mark().has_bias_pattern();
 334 }
 335 
 336 bool oopDesc::has_bias_pattern_raw() const {
 337   return mark_raw().has_bias_pattern();
 338 }
 339 
 340 // Used only for markSweep, scavenging
 341 bool oopDesc::is_gc_marked() const {
 342   return mark_raw().is_marked();
 343 }
 344 
 345 // Used by scavengers
 346 bool oopDesc::is_forwarded() const {
 347   // The extra heap check is needed since the obj might be locked, in which case the
 348   // mark would point to a stack location and have the sentinel bit cleared
 349   return mark_raw().is_marked();
 350 }
 351 
 352 // Used by scavengers
 353 void oopDesc::forward_to(oop p) {
 354   verify_forwardee(p);
 355   markWord m = markWord::encode_pointer_as_mark(p);
 356   assert(m.decode_pointer() == p, "encoding must be reversable");
 357   set_mark_raw(m);
 358 }
 359 
 360 // Used by parallel scavengers
 361 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) {
 362   verify_forwardee(p);
 363   markWord m = markWord::encode_pointer_as_mark(p);
 364   assert(m.decode_pointer() == p, "encoding must be reversable");
 365   return cas_set_mark_raw(m, compare, order) == compare;
 366 }
 367 
 368 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
 369   verify_forwardee(p);
 370   markWord m = markWord::encode_pointer_as_mark(p);
 371   assert(m.decode_pointer() == p, "encoding must be reversable");
 372   markWord old_mark = cas_set_mark_raw(m, compare, order);
 373   if (old_mark == compare) {
 374     return NULL;
 375   } else {
 376     return (oop)old_mark.decode_pointer();
 377   }
 378 }
 379 
 380 // Note that the forwardee is not the same thing as the displaced_mark.
 381 // The forwardee is used when copying during scavenge and mark-sweep.
 382 // It does need to clear the low two locking- and GC-related bits.
 383 oop oopDesc::forwardee() const {
 384   return (oop) mark_raw().decode_pointer();
 385 }
 386 
 387 // Note that the forwardee is not the same thing as the displaced_mark.
 388 // The forwardee is used when copying during scavenge and mark-sweep.
 389 // It does need to clear the low two locking- and GC-related bits.
 390 oop oopDesc::forwardee_acquire() const {
 391   return (oop) OrderAccess::load_acquire(&_mark).decode_pointer();
 392 }
 393 
 394 // The following method needs to be MT safe.
 395 uint oopDesc::age() const {
 396   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 397   if (has_displaced_mark_raw()) {
 398     return displaced_mark_raw().age();
 399   } else {
 400     return mark_raw().age();
 401   }
 402 }
 403 
 404 void oopDesc::incr_age() {
 405   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 406   if (has_displaced_mark_raw()) {
 407     set_displaced_mark_raw(displaced_mark_raw().incr_age());
 408   } else {
 409     set_mark_raw(mark_raw().incr_age());
 410   }
 411 }
 412 
 413 template <typename OopClosureType>
 414 void oopDesc::oop_iterate(OopClosureType* cl) {
 415   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass());
 416 }
 417 
 418 template <typename OopClosureType>
 419 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) {
 420   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr);
 421 }
 422 
 423 template <typename OopClosureType>
 424 int oopDesc::oop_iterate_size(OopClosureType* cl) {
 425   Klass* k = klass();
 426   int size = size_given_klass(k);
 427   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
 428   return size;
 429 }
 430 
 431 template <typename OopClosureType>
 432 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
 433   Klass* k = klass();
 434   int size = size_given_klass(k);
 435   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
 436   return size;
 437 }
 438 
 439 template <typename OopClosureType>
 440 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
 441   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, klass());
 442 }
 443 
 444 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
 445   return obj == NULL || obj->klass()->is_subtype_of(klass);
 446 }
 447 
 448 intptr_t oopDesc::identity_hash() {
 449   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 450   // Note: The mark must be read into local variable to avoid concurrent updates.
 451   markWord mrk = mark();
 452   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
 453     return mrk.hash();
 454   } else if (mrk.is_marked()) {
 455     return mrk.hash();
 456   } else {
 457     return slow_identity_hash();
 458   }
 459 }
 460 
 461 bool oopDesc::has_displaced_mark_raw() const {
 462   return mark_raw().has_displaced_mark_helper();
 463 }
 464 
 465 markWord oopDesc::displaced_mark_raw() const {
 466   return mark_raw().displaced_mark_helper();
 467 }
 468 
 469 void oopDesc::set_displaced_mark_raw(markWord m) {
 470   mark_raw().set_displaced_mark_helper(m);
 471 }
 472 
 473 // Supports deferred calling of obj->klass().
 474 class DeferredObjectToKlass {
 475   const oopDesc* _obj;
 476 
 477 public:
 478   DeferredObjectToKlass(const oopDesc* obj) : _obj(obj) {}
 479 
 480   // Implicitly convertible to const Klass*.
 481   operator const Klass*() const {
 482     return _obj->klass();
 483   }
 484 };
 485 
 486 bool oopDesc::mark_must_be_preserved() const {
 487   return mark_must_be_preserved(mark_raw());
 488 }
 489 
 490 bool oopDesc::mark_must_be_preserved(markWord m) const {
 491   // There's a circular dependency between oop.inline.hpp and
 492   // markWord.inline.hpp because markWord::must_be_preserved wants to call
 493   // oopDesc::klass(). This could be solved by calling klass() here. However,
 494   // not all paths inside must_be_preserved calls klass(). Defer the call until
 495   // the klass is actually needed.
 496   return m.must_be_preserved(DeferredObjectToKlass(this));
 497 }
 498 
 499 bool oopDesc::mark_must_be_preserved_for_promotion_failure(markWord m) const {
 500   return m.must_be_preserved_for_promotion_failure(DeferredObjectToKlass(this));
 501 }
 502 
 503 #endif // SHARE_OOPS_OOP_INLINE_HPP