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