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(&_mark, m);
  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(as_oop(), mark_offset_in_bytes(), old_mark.value(), new_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(&_mark, old_mark, new_mark, order);
  82 }
  83 
  84 void oopDesc::init_mark() {
  85   set_mark(markWord::prototype_for_klass(klass()));
  86 }
  87 
  88 void oopDesc::init_mark_raw() {
  89   set_mark_raw(markWord::prototype_for_klass(klass()));
  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(Atomic::load_acquire(xaddr));
 114   } else {
 115     return Atomic::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     Atomic::release_store(compressed_klass_addr(mem),
 160                           CompressedKlassPointers::encode_not_null(klass));
 161   } else {
 162     Atomic::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 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       // 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       assert((s == klass->oop_size(this)) ||
 233              (Universe::heap()->is_gc_active() && is_objArray() && is_forwarded() && (UseParallelGC || UseG1GC)),
 234              "wrong array object size");
 235     } else {
 236       // Must be zero, so bite the bullet and take the virtual call.
 237       s = klass->oop_size(this);
 238     }
 239   }
 240 
 241   assert(s > 0, "Oop size must be greater than zero, not %d", s);
 242   assert(is_object_aligned(s), "Oop size is not properly aligned: %d", s);
 243   return s;
 244 }
 245 
 246 bool oopDesc::is_instance()  const { return klass()->is_instance_klass();  }
 247 bool oopDesc::is_array()     const { return klass()->is_array_klass();     }
 248 bool oopDesc::is_objArray()  const { return klass()->is_objArray_klass();  }
 249 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); }
 250 
 251 void*    oopDesc::field_addr_raw(int offset)     const { return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + offset); }
 252 void*    oopDesc::field_addr(int offset)         const { return Access<>::resolve(as_oop())->field_addr_raw(offset); }
 253 
 254 template <class T>
 255 T*       oopDesc::obj_field_addr_raw(int offset) const { return (T*) field_addr_raw(offset); }
 256 
 257 template <typename T>
 258 size_t   oopDesc::field_offset(T* p) const { return pointer_delta((void*)p, (void*)this, 1); }
 259 
 260 template <DecoratorSet decorators>
 261 inline oop  oopDesc::obj_field_access(int offset) const             { return HeapAccess<decorators>::oop_load_at(as_oop(), offset); }
 262 inline oop  oopDesc::obj_field(int offset) const                    { return HeapAccess<>::oop_load_at(as_oop(), offset);  }
 263 
 264 inline void oopDesc::obj_field_put(int offset, oop value)           { HeapAccess<>::oop_store_at(as_oop(), offset, value); }
 265 
 266 inline jbyte oopDesc::byte_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 267 inline void  oopDesc::byte_field_put(int offset, jbyte value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 268 
 269 inline jchar oopDesc::char_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 270 inline void  oopDesc::char_field_put(int offset, jchar value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 271 
 272 inline jboolean oopDesc::bool_field(int offset) const               { return HeapAccess<>::load_at(as_oop(), offset); }
 273 inline void     oopDesc::bool_field_put(int offset, jboolean value) { HeapAccess<>::store_at(as_oop(), offset, jboolean(value & 1)); }
 274 inline jboolean oopDesc::bool_field_volatile(int offset) const      { return HeapAccess<MO_SEQ_CST>::load_at(as_oop(), offset); }
 275 inline void     oopDesc::bool_field_put_volatile(int offset, jboolean value) { HeapAccess<MO_SEQ_CST>::store_at(as_oop(), offset, jboolean(value & 1)); }
 276 inline jshort oopDesc::short_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 277 inline void   oopDesc::short_field_put(int offset, jshort value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 278 
 279 inline jint oopDesc::int_field(int offset) const                    { return HeapAccess<>::load_at(as_oop(), offset);  }
 280 inline jint oopDesc::int_field_raw(int offset) const                { return RawAccess<>::load_at(as_oop(), offset);   }
 281 inline void oopDesc::int_field_put(int offset, jint value)          { HeapAccess<>::store_at(as_oop(), offset, value); }
 282 
 283 inline jlong oopDesc::long_field(int offset) const                  { return HeapAccess<>::load_at(as_oop(), offset);  }
 284 inline void  oopDesc::long_field_put(int offset, jlong value)       { HeapAccess<>::store_at(as_oop(), offset, value); }
 285 
 286 inline jfloat oopDesc::float_field(int offset) const                { return HeapAccess<>::load_at(as_oop(), offset);  }
 287 inline void   oopDesc::float_field_put(int offset, jfloat value)    { HeapAccess<>::store_at(as_oop(), offset, value); }
 288 
 289 inline jdouble oopDesc::double_field(int offset) const              { return HeapAccess<>::load_at(as_oop(), offset);  }
 290 inline void    oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); }
 291 
 292 bool oopDesc::is_locked() const {
 293   return mark().is_locked();
 294 }
 295 
 296 bool oopDesc::is_unlocked() const {
 297   return mark().is_unlocked();
 298 }
 299 
 300 bool oopDesc::has_bias_pattern() const {
 301   return mark().has_bias_pattern();
 302 }
 303 
 304 bool oopDesc::has_bias_pattern_raw() const {
 305   return mark_raw().has_bias_pattern();
 306 }
 307 
 308 // Used only for markSweep, scavenging
 309 bool oopDesc::is_gc_marked() const {
 310   return mark_raw().is_marked();
 311 }
 312 
 313 // Used by scavengers
 314 bool oopDesc::is_forwarded() const {
 315   // The extra heap check is needed since the obj might be locked, in which case the
 316   // mark would point to a stack location and have the sentinel bit cleared
 317   return mark_raw().is_marked();
 318 }
 319 
 320 // Used by scavengers
 321 void oopDesc::forward_to(oop p) {
 322   verify_forwardee(p);
 323   markWord m = markWord::encode_pointer_as_mark(p);
 324   assert(m.decode_pointer() == p, "encoding must be reversable");
 325   set_mark_raw(m);
 326 }
 327 
 328 // Used by parallel scavengers
 329 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) {
 330   verify_forwardee(p);
 331   markWord m = markWord::encode_pointer_as_mark(p);
 332   assert(m.decode_pointer() == p, "encoding must be reversable");
 333   return cas_set_mark_raw(m, compare, order) == compare;
 334 }
 335 
 336 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
 337   verify_forwardee(p);
 338   markWord m = markWord::encode_pointer_as_mark(p);
 339   assert(m.decode_pointer() == p, "encoding must be reversable");
 340   markWord old_mark = cas_set_mark_raw(m, compare, order);
 341   if (old_mark == compare) {
 342     return NULL;
 343   } else {
 344     return (oop)old_mark.decode_pointer();
 345   }
 346 }
 347 
 348 // Note that the forwardee is not the same thing as the displaced_mark.
 349 // The forwardee is used when copying during scavenge and mark-sweep.
 350 // It does need to clear the low two locking- and GC-related bits.
 351 oop oopDesc::forwardee() const {
 352   return (oop) mark_raw().decode_pointer();
 353 }
 354 
 355 // Note that the forwardee is not the same thing as the displaced_mark.
 356 // The forwardee is used when copying during scavenge and mark-sweep.
 357 // It does need to clear the low two locking- and GC-related bits.
 358 oop oopDesc::forwardee_acquire() const {
 359   return (oop) Atomic::load_acquire(&_mark).decode_pointer();
 360 }
 361 
 362 // The following method needs to be MT safe.
 363 uint oopDesc::age() const {
 364   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 365   if (has_displaced_mark_raw()) {
 366     return displaced_mark_raw().age();
 367   } else {
 368     return mark_raw().age();
 369   }
 370 }
 371 
 372 void oopDesc::incr_age() {
 373   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 374   if (has_displaced_mark_raw()) {
 375     set_displaced_mark_raw(displaced_mark_raw().incr_age());
 376   } else {
 377     set_mark_raw(mark_raw().incr_age());
 378   }
 379 }
 380 
 381 template <typename OopClosureType>
 382 void oopDesc::oop_iterate(OopClosureType* cl) {
 383   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass());
 384 }
 385 
 386 template <typename OopClosureType>
 387 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) {
 388   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr);
 389 }
 390 
 391 template <typename OopClosureType>
 392 int oopDesc::oop_iterate_size(OopClosureType* cl) {
 393   Klass* k = klass();
 394   int size = size_given_klass(k);
 395   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
 396   return size;
 397 }
 398 
 399 template <typename OopClosureType>
 400 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
 401   Klass* k = klass();
 402   int size = size_given_klass(k);
 403   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
 404   return size;
 405 }
 406 
 407 template <typename OopClosureType>
 408 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
 409   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, klass());
 410 }
 411 
 412 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
 413   return obj == NULL || obj->klass()->is_subtype_of(klass);
 414 }
 415 
 416 intptr_t oopDesc::identity_hash() {
 417   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 418   // Note: The mark must be read into local variable to avoid concurrent updates.
 419   markWord mrk = mark();
 420   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
 421     return mrk.hash();
 422   } else if (mrk.is_marked()) {
 423     return mrk.hash();
 424   } else {
 425     return slow_identity_hash();
 426   }
 427 }
 428 
 429 bool oopDesc::has_displaced_mark_raw() const {
 430   return mark_raw().has_displaced_mark_helper();
 431 }
 432 
 433 markWord oopDesc::displaced_mark_raw() const {
 434   return mark_raw().displaced_mark_helper();
 435 }
 436 
 437 void oopDesc::set_displaced_mark_raw(markWord m) {
 438   mark_raw().set_displaced_mark_helper(m);
 439 }
 440 
 441 // Supports deferred calling of obj->klass().
 442 class DeferredObjectToKlass {
 443   const oopDesc* _obj;
 444 
 445 public:
 446   DeferredObjectToKlass(const oopDesc* obj) : _obj(obj) {}
 447 
 448   // Implicitly convertible to const Klass*.
 449   operator const Klass*() const {
 450     return _obj->klass();
 451   }
 452 };
 453 
 454 bool oopDesc::mark_must_be_preserved() const {
 455   return mark_must_be_preserved(mark_raw());
 456 }
 457 
 458 bool oopDesc::mark_must_be_preserved(markWord m) const {
 459   // There's a circular dependency between oop.inline.hpp and
 460   // markWord.inline.hpp because markWord::must_be_preserved wants to call
 461   // oopDesc::klass(). This could be solved by calling klass() here. However,
 462   // not all paths inside must_be_preserved calls klass(). Defer the call until
 463   // the klass is actually needed.
 464   return m.must_be_preserved(DeferredObjectToKlass(this));
 465 }
 466 
 467 bool oopDesc::mark_must_be_preserved_for_promotion_failure(markWord m) const {
 468   return m.must_be_preserved_for_promotion_failure(DeferredObjectToKlass(this));
 469 }
 470 
 471 #endif // SHARE_OOPS_OOP_INLINE_HPP