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