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_object(this));
  86 }
  87 
  88 void oopDesc::init_mark_raw() {
  89   set_mark_raw(markWord::prototype_for_object(this));
  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 
 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 bool oopDesc::has_bias_pattern_raw() const {
 336   return mark_raw().has_bias_pattern();
 337 }
 338 
 339 // Used only for markSweep, scavenging
 340 bool oopDesc::is_gc_marked() const {
 341   return mark_raw().is_marked();
 342 }
 343 
 344 // Used by scavengers
 345 bool oopDesc::is_forwarded() const {
 346   // The extra heap check is needed since the obj might be locked, in which case the
 347   // mark would point to a stack location and have the sentinel bit cleared
 348   return mark_raw().is_marked();
 349 }
 350 
 351 // Used by scavengers
 352 void oopDesc::forward_to(oop p) {
 353   assert(check_obj_alignment(p),
 354          "forwarding to something not aligned");
 355   assert(Universe::heap()->is_in_reserved(p),
 356          "forwarding to something not in heap");
 357   assert(!is_archived_object(oop(this)) &&
 358          !is_archived_object(p),
 359          "forwarding archive object");
 360   markWord m = markWord::encode_pointer_as_mark(p);
 361   assert(m.decode_pointer() == p, "encoding must be reversable");
 362   set_mark_raw(m);
 363 }
 364 
 365 // Used by parallel scavengers
 366 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) {
 367   assert(check_obj_alignment(p),
 368          "forwarding to something not aligned");
 369   assert(Universe::heap()->is_in_reserved(p),
 370          "forwarding to something not in heap");
 371   markWord m = markWord::encode_pointer_as_mark(p);
 372   assert(m.decode_pointer() == p, "encoding must be reversable");
 373   return cas_set_mark_raw(m, compare, order) == compare;
 374 }
 375 
 376 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
 377   // CMS forwards some non-heap value into the mark oop to reserve oops during
 378   // promotion, so the next two asserts do not hold.
 379   assert(UseConcMarkSweepGC || check_obj_alignment(p),
 380          "forwarding to something not aligned");
 381   assert(UseConcMarkSweepGC || Universe::heap()->is_in_reserved(p),
 382          "forwarding to something not in heap");
 383   markWord m = markWord::encode_pointer_as_mark(p);
 384   assert(m.decode_pointer() == p, "encoding must be reversable");
 385   markWord old_mark = cas_set_mark_raw(m, compare, order);
 386   if (old_mark == compare) {
 387     return NULL;
 388   } else {
 389     return (oop)old_mark.decode_pointer();
 390   }
 391 }
 392 
 393 // Note that the forwardee is not the same thing as the displaced_mark.
 394 // The forwardee is used when copying during scavenge and mark-sweep.
 395 // It does need to clear the low two locking- and GC-related bits.
 396 oop oopDesc::forwardee() const {
 397   return (oop) mark_raw().decode_pointer();
 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_acquire() const {
 404   return (oop) OrderAccess::load_acquire(&_mark).decode_pointer();
 405 }
 406 
 407 // The following method needs to be MT safe.
 408 uint oopDesc::age() const {
 409   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 410   if (has_displaced_mark_raw()) {
 411     return displaced_mark_raw().age();
 412   } else {
 413     return mark_raw().age();
 414   }
 415 }
 416 
 417 void oopDesc::incr_age() {
 418   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 419   if (has_displaced_mark_raw()) {
 420     set_displaced_mark_raw(displaced_mark_raw().incr_age());
 421   } else {
 422     set_mark_raw(mark_raw().incr_age());
 423   }
 424 }
 425 
 426 template <typename OopClosureType>
 427 void oopDesc::oop_iterate(OopClosureType* cl) {
 428   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass());
 429 }
 430 
 431 template <typename OopClosureType>
 432 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) {
 433   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr);
 434 }
 435 
 436 template <typename OopClosureType>
 437 int oopDesc::oop_iterate_size(OopClosureType* cl) {
 438   Klass* k = klass();
 439   int size = size_given_klass(k);
 440   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
 441   return size;
 442 }
 443 
 444 template <typename OopClosureType>
 445 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
 446   Klass* k = klass();
 447   int size = size_given_klass(k);
 448   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
 449   return size;
 450 }
 451 
 452 template <typename OopClosureType>
 453 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
 454   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, klass());
 455 }
 456 
 457 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
 458   return obj == NULL || obj->klass()->is_subtype_of(klass);
 459 }
 460 
 461 intptr_t oopDesc::identity_hash() {
 462   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 463   // Note: The mark must be read into local variable to avoid concurrent updates.
 464   markWord mrk = mark();
 465   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
 466     return mrk.hash();
 467   } else if (mrk.is_marked()) {
 468     return mrk.hash();
 469   } else {
 470     return slow_identity_hash();
 471   }
 472 }
 473 
 474 bool oopDesc::has_displaced_mark_raw() const {
 475   return mark_raw().has_displaced_mark_helper();
 476 }
 477 
 478 markWord oopDesc::displaced_mark_raw() const {
 479   return mark_raw().displaced_mark_helper();
 480 }
 481 
 482 void oopDesc::set_displaced_mark_raw(markWord m) {
 483   mark_raw().set_displaced_mark_helper(m);
 484 }
 485 
 486 #endif // SHARE_OOPS_OOP_INLINE_HPP