1 /*
   2  * Copyright (c) 1997, 2018, 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_VM_OOPS_OOP_INLINE_HPP
  26 #define SHARE_VM_OOPS_OOP_INLINE_HPP
  27 
  28 #include "gc/shared/collectedHeap.hpp"
  29 #include "memory/metaspaceShared.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/markOop.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 markOop  oopDesc::mark()      const {
  47   return HeapAccess<MO_VOLATILE>::load_at(as_oop(), mark_offset_in_bytes());
  48 }
  49 
  50 markOop  oopDesc::mark_raw()  const {
  51   return _mark;
  52 }
  53 
  54 markOop* oopDesc::mark_addr_raw() const {
  55   return (markOop*) &_mark;
  56 }
  57 
  58 void oopDesc::set_mark(volatile markOop m) {
  59   HeapAccess<MO_VOLATILE>::store_at(as_oop(), mark_offset_in_bytes(), m);
  60 }
  61 
  62 void oopDesc::set_mark_raw(volatile markOop m) {
  63   _mark = m;
  64 }
  65 
  66 void oopDesc::set_mark_raw(HeapWord* mem, markOop m) {
  67   *(markOop*)(((char*)mem) + mark_offset_in_bytes()) = m;
  68 }
  69 
  70 void oopDesc::release_set_mark(markOop m) {
  71   HeapAccess<MO_RELEASE>::store_at(as_oop(), mark_offset_in_bytes(), m);
  72 }
  73 
  74 markOop oopDesc::cas_set_mark(markOop new_mark, markOop old_mark) {
  75   return HeapAccess<>::atomic_cmpxchg_at(new_mark, as_oop(), mark_offset_in_bytes(), old_mark);
  76 }
  77 
  78 markOop oopDesc::cas_set_mark_raw(markOop new_mark, markOop old_mark, atomic_memory_order order) {
  79   return Atomic::cmpxchg(new_mark, &_mark, old_mark, order);
  80 }
  81 
  82 void oopDesc::init_mark() {
  83   set_mark(markOopDesc::prototype_for_object(this));
  84 }
  85 
  86 void oopDesc::init_mark_raw() {
  87   set_mark_raw(markOopDesc::prototype_for_object(this));
  88 }
  89 
  90 Klass* oopDesc::klass() const {
  91   if (UseCompressedClassPointers) {
  92     return Klass::decode_klass_not_null(_metadata._compressed_klass);
  93   } else {
  94     return _metadata._klass;
  95   }
  96 }
  97 
  98 Klass* oopDesc::klass_or_null() const volatile {
  99   if (UseCompressedClassPointers) {
 100     return Klass::decode_klass(_metadata._compressed_klass);
 101   } else {
 102     return _metadata._klass;
 103   }
 104 }
 105 
 106 Klass* oopDesc::klass_or_null_acquire() const volatile {
 107   if (UseCompressedClassPointers) {
 108     // Workaround for non-const load_acquire parameter.
 109     const volatile narrowKlass* addr = &_metadata._compressed_klass;
 110     volatile narrowKlass* xaddr = const_cast<volatile narrowKlass*>(addr);
 111     return Klass::decode_klass(OrderAccess::load_acquire(xaddr));
 112   } else {
 113     return OrderAccess::load_acquire(&_metadata._klass);
 114   }
 115 }
 116 
 117 Klass** oopDesc::klass_addr(HeapWord* mem) {
 118   // Only used internally and with CMS and will not work with
 119   // UseCompressedOops
 120   assert(!UseCompressedClassPointers, "only supported with uncompressed klass pointers");
 121   ByteSize offset = byte_offset_of(oopDesc, _metadata._klass);
 122   return (Klass**) (((char*)mem) + in_bytes(offset));
 123 }
 124 
 125 narrowKlass* oopDesc::compressed_klass_addr(HeapWord* mem) {
 126   assert(UseCompressedClassPointers, "only called by compressed klass pointers");
 127   ByteSize offset = byte_offset_of(oopDesc, _metadata._compressed_klass);
 128   return (narrowKlass*) (((char*)mem) + in_bytes(offset));
 129 }
 130 
 131 Klass** oopDesc::klass_addr() {
 132   return klass_addr((HeapWord*)this);
 133 }
 134 
 135 narrowKlass* oopDesc::compressed_klass_addr() {
 136   return compressed_klass_addr((HeapWord*)this);
 137 }
 138 
 139 #define CHECK_SET_KLASS(k)                                                \
 140   do {                                                                    \
 141     assert(Universe::is_bootstrapping() || k != NULL, "NULL Klass");      \
 142     assert(Universe::is_bootstrapping() || k->is_klass(), "not a Klass"); \
 143   } while (0)
 144 
 145 void oopDesc::set_klass(Klass* k) {
 146   CHECK_SET_KLASS(k);
 147   if (UseCompressedClassPointers) {
 148     *compressed_klass_addr() = Klass::encode_klass_not_null(k);
 149   } else {
 150     *klass_addr() = k;
 151   }
 152 }
 153 
 154 void oopDesc::release_set_klass(HeapWord* mem, Klass* klass) {
 155   CHECK_SET_KLASS(klass);
 156   if (UseCompressedClassPointers) {
 157     OrderAccess::release_store(compressed_klass_addr(mem),
 158                                Klass::encode_klass_not_null(klass));
 159   } else {
 160     OrderAccess::release_store(klass_addr(mem), klass);
 161   }
 162 }
 163 
 164 #undef CHECK_SET_KLASS
 165 
 166 int oopDesc::klass_gap() const {
 167   return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes());
 168 }
 169 
 170 void oopDesc::set_klass_gap(HeapWord* mem, int v) {
 171   if (UseCompressedClassPointers) {
 172     *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
 173   }
 174 }
 175 
 176 void oopDesc::set_klass_gap(int v) {
 177   set_klass_gap((HeapWord*)this, v);
 178 }
 179 
 180 void oopDesc::set_klass_to_list_ptr(oop k) {
 181   // This is only to be used during GC, for from-space objects, so no
 182   // barrier is needed.
 183   if (UseCompressedClassPointers) {
 184     _metadata._compressed_klass = (narrowKlass)CompressedOops::encode(k);  // may be null (parnew overflow handling)
 185   } else {
 186     _metadata._klass = (Klass*)(address)k;
 187   }
 188 }
 189 
 190 oop oopDesc::list_ptr_from_klass() {
 191   // This is only to be used during GC, for from-space objects.
 192   if (UseCompressedClassPointers) {
 193     return CompressedOops::decode((narrowOop)_metadata._compressed_klass);
 194   } else {
 195     // Special case for GC
 196     return (oop)(address)_metadata._klass;
 197   }
 198 }
 199 
 200 bool oopDesc::is_a(Klass* k) const {
 201   return klass()->is_subtype_of(k);
 202 }
 203 
 204 int oopDesc::size()  {
 205   return size_given_klass(klass());
 206 }
 207 
 208 int oopDesc::size_given_klass(Klass* klass)  {
 209   int lh = klass->layout_helper();
 210   int s;
 211 
 212   // lh is now a value computed at class initialization that may hint
 213   // at the size.  For instances, this is positive and equal to the
 214   // size.  For arrays, this is negative and provides log2 of the
 215   // array element size.  For other oops, it is zero and thus requires
 216   // a virtual call.
 217   //
 218   // We go to all this trouble because the size computation is at the
 219   // heart of phase 2 of mark-compaction, and called for every object,
 220   // alive or dead.  So the speed here is equal in importance to the
 221   // speed of allocation.
 222 
 223   if (lh > Klass::_lh_neutral_value) {
 224     if (!Klass::layout_helper_needs_slow_path(lh)) {
 225       s = lh >> LogHeapWordSize;  // deliver size scaled by wordSize
 226     } else {
 227       s = klass->oop_size(this);
 228     }
 229   } else if (lh <= Klass::_lh_neutral_value) {
 230     // The most common case is instances; fall through if so.
 231     if (lh < Klass::_lh_neutral_value) {
 232       // Second most common case is arrays.  We have to fetch the
 233       // length of the array, shift (multiply) it appropriately,
 234       // up to wordSize, add the header, and align to object size.
 235       size_t size_in_bytes;
 236       size_t array_length = (size_t) ((arrayOop)this)->length();
 237       size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh);
 238       size_in_bytes += Klass::layout_helper_header_size(lh);
 239 
 240       // This code could be simplified, but by keeping array_header_in_bytes
 241       // in units of bytes and doing it this way we can round up just once,
 242       // skipping the intermediate round to HeapWordSize.
 243       s = (int)(align_up(size_in_bytes, MinObjAlignmentInBytes) / HeapWordSize);
 244 
 245       // ParNew (used by CMS), UseParallelGC and UseG1GC can change the length field
 246       // of an "old copy" of an object array in the young gen so it indicates
 247       // the grey portion of an already copied array. This will cause the first
 248       // disjunct below to fail if the two comparands are computed across such
 249       // a concurrent change.
 250       // ParNew also runs with promotion labs (which look like int
 251       // filler arrays) which are subject to changing their declared size
 252       // when finally retiring a PLAB; this also can cause the first disjunct
 253       // to fail for another worker thread that is concurrently walking the block
 254       // offset table. Both these invariant failures are benign for their
 255       // current uses; we relax the assertion checking to cover these two cases below:
 256       //     is_objArray() && is_forwarded()   // covers first scenario above
 257       //  || is_typeArray()                    // covers second scenario above
 258       // If and when UseParallelGC uses the same obj array oop stealing/chunking
 259       // technique, we will need to suitably modify the assertion.
 260       assert((s == klass->oop_size(this)) ||
 261              (Universe::heap()->is_gc_active() &&
 262               ((is_typeArray() && UseConcMarkSweepGC) ||
 263                (is_objArray()  && is_forwarded() && (UseConcMarkSweepGC || UseParallelGC || UseG1GC)))),
 264              "wrong array object size");
 265     } else {
 266       // Must be zero, so bite the bullet and take the virtual call.
 267       s = klass->oop_size(this);
 268     }
 269   }
 270 
 271   assert(s > 0, "Oop size must be greater than zero, not %d", s);
 272   assert(is_object_aligned(s), "Oop size is not properly aligned: %d", s);
 273   return s;
 274 }
 275 
 276 bool oopDesc::is_instance()  const { return klass()->is_instance_klass();  }
 277 bool oopDesc::is_array()     const { return klass()->is_array_klass();     }
 278 bool oopDesc::is_objArray()  const { return klass()->is_objArray_klass();  }
 279 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); }
 280 bool oopDesc::is_value()     const { return klass()->is_value(); }
 281 bool oopDesc::is_valueArray()  const { return klass()->is_valueArray_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 
 307 inline jshort oopDesc::short_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 308 inline void   oopDesc::short_field_put(int offset, jshort value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 309 
 310 inline jint oopDesc::int_field(int offset) const                    { return HeapAccess<>::load_at(as_oop(), offset);  }
 311 inline jint oopDesc::int_field_raw(int offset) const                { return RawAccess<>::load_at(as_oop(), offset);   }
 312 inline void oopDesc::int_field_put(int offset, jint value)          { HeapAccess<>::store_at(as_oop(), offset, value); }
 313 
 314 inline jlong oopDesc::long_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 315 inline void  oopDesc::long_field_put(int offset, jlong value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 316 
 317 inline jfloat oopDesc::float_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 318 inline void   oopDesc::float_field_put(int offset, jfloat value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 319 
 320 inline jdouble oopDesc::double_field(int offset) const              { return HeapAccess<>::load_at(as_oop(), offset);  }
 321 inline void    oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); }
 322 
 323 bool oopDesc::is_locked() const {
 324   return mark()->is_locked();
 325 }
 326 
 327 bool oopDesc::is_unlocked() const {
 328   return mark()->is_unlocked();
 329 }
 330 
 331 bool oopDesc::has_bias_pattern() const {
 332   return mark()->has_bias_pattern();
 333 }
 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   assert(check_obj_alignment(p),
 355          "forwarding to something not aligned");
 356   assert(Universe::heap()->is_in_reserved(p),
 357          "forwarding to something not in heap");
 358   assert(!MetaspaceShared::is_archive_object(oop(this)) &&
 359          !MetaspaceShared::is_archive_object(p),
 360          "forwarding archive object");
 361   markOop m = markOopDesc::encode_pointer_as_mark(p);
 362   assert(m->decode_pointer() == p, "encoding must be reversable");
 363   set_mark_raw(m);
 364 }
 365 
 366 // Used by parallel scavengers
 367 bool oopDesc::cas_forward_to(oop p, markOop compare, atomic_memory_order order) {
 368   assert(check_obj_alignment(p),
 369          "forwarding to something not aligned");
 370   assert(Universe::heap()->is_in_reserved(p),
 371          "forwarding to something not in heap");
 372   markOop m = markOopDesc::encode_pointer_as_mark(p);
 373   assert(m->decode_pointer() == p, "encoding must be reversable");
 374   return cas_set_mark_raw(m, compare, order) == compare;
 375 }
 376 
 377 oop oopDesc::forward_to_atomic(oop p, atomic_memory_order order) {
 378   markOop oldMark = mark_raw();
 379   markOop forwardPtrMark = markOopDesc::encode_pointer_as_mark(p);
 380   markOop curMark;
 381 
 382   assert(forwardPtrMark->decode_pointer() == p, "encoding must be reversable");
 383   assert(sizeof(markOop) == sizeof(intptr_t), "CAS below requires this.");
 384 
 385   while (!oldMark->is_marked()) {
 386     curMark = cas_set_mark_raw(forwardPtrMark, oldMark, order);
 387     assert(is_forwarded(), "object should have been forwarded");
 388     if (curMark == oldMark) {
 389       return NULL;
 390     }
 391     // If the CAS was unsuccessful then curMark->is_marked()
 392     // should return true as another thread has CAS'd in another
 393     // forwarding pointer.
 394     oldMark = curMark;
 395   }
 396   return forwardee();
 397 }
 398 
 399 // Note that the forwardee is not the same thing as the displaced_mark.
 400 // The forwardee is used when copying during scavenge and mark-sweep.
 401 // It does need to clear the low two locking- and GC-related bits.
 402 oop oopDesc::forwardee() const {
 403   return (oop) mark_raw()->decode_pointer();
 404 }
 405 
 406 // Note that the forwardee is not the same thing as the displaced_mark.
 407 // The forwardee is used when copying during scavenge and mark-sweep.
 408 // It does need to clear the low two locking- and GC-related bits.
 409 oop oopDesc::forwardee_acquire() const {
 410   markOop m = OrderAccess::load_acquire(&_mark);
 411   return (oop) m->decode_pointer();
 412 }
 413 
 414 // The following method needs to be MT safe.
 415 uint oopDesc::age() const {
 416   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 417   if (has_displaced_mark_raw()) {
 418     return displaced_mark_raw()->age();
 419   } else {
 420     return mark_raw()->age();
 421   }
 422 }
 423 
 424 void oopDesc::incr_age() {
 425   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 426   if (has_displaced_mark_raw()) {
 427     set_displaced_mark_raw(displaced_mark_raw()->incr_age());
 428   } else {
 429     set_mark_raw(mark_raw()->incr_age());
 430   }
 431 }
 432 
 433 #if INCLUDE_PARALLELGC
 434 void oopDesc::pc_follow_contents(ParCompactionManager* cm) {
 435   klass()->oop_pc_follow_contents(this, cm);
 436 }
 437 
 438 void oopDesc::pc_update_contents(ParCompactionManager* cm) {
 439   Klass* k = klass();
 440   if (!k->is_typeArray_klass()) {
 441     // It might contain oops beyond the header, so take the virtual call.
 442     k->oop_pc_update_pointers(this, cm);
 443   }
 444   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 445 }
 446 
 447 void oopDesc::ps_push_contents(PSPromotionManager* pm) {
 448   Klass* k = klass();
 449   if (!k->is_typeArray_klass()) {
 450     // It might contain oops beyond the header, so take the virtual call.
 451     k->oop_ps_push_contents(this, pm);
 452   }
 453   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 454 }
 455 #endif // INCLUDE_PARALLELGC
 456 
 457 template <typename OopClosureType>
 458 void oopDesc::oop_iterate(OopClosureType* cl) {
 459   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass());
 460 }
 461 
 462 template <typename OopClosureType>
 463 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) {
 464   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr);
 465 }
 466 
 467 template <typename OopClosureType>
 468 int oopDesc::oop_iterate_size(OopClosureType* cl) {
 469   Klass* k = klass();
 470   int size = size_given_klass(k);
 471   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
 472   return size;
 473 }
 474 
 475 template <typename OopClosureType>
 476 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
 477   Klass* k = klass();
 478   int size = size_given_klass(k);
 479   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
 480   return size;
 481 }
 482 
 483 template <typename OopClosureType>
 484 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
 485   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, klass());
 486 }
 487 
 488 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
 489   return obj == NULL || obj->klass()->is_subtype_of(klass);
 490 }
 491 
 492 intptr_t oopDesc::identity_hash() {
 493   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 494   // Note: The mark must be read into local variable to avoid concurrent updates.
 495   markOop mrk = mark();
 496   if (mrk->is_unlocked() && !mrk->has_no_hash()) {
 497     return mrk->hash();
 498   } else if (mrk->is_marked()) {
 499     return mrk->hash();
 500   } else {
 501     return slow_identity_hash();
 502   }
 503 }
 504 
 505 bool oopDesc::has_displaced_mark_raw() const {
 506   return mark_raw()->has_displaced_mark_helper();
 507 }
 508 
 509 markOop oopDesc::displaced_mark_raw() const {
 510   return mark_raw()->displaced_mark_helper();
 511 }
 512 
 513 void oopDesc::set_displaced_mark_raw(markOop m) {
 514   mark_raw()->set_displaced_mark_helper(m);
 515 }
 516 
 517 #endif // SHARE_VM_OOPS_OOP_INLINE_HPP