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