1 /*
   2  * Copyright (c) 1997, 2017, 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/barrierSet.inline.hpp"
  30 #include "gc/shared/cardTableModRefBS.hpp"
  31 #include "gc/shared/collectedHeap.inline.hpp"
  32 #include "gc/shared/genCollectedHeap.hpp"
  33 #include "gc/shared/generation.hpp"
  34 #include "oops/arrayKlass.hpp"
  35 #include "oops/arrayOop.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/macros.hpp"
  43 
  44 inline void update_barrier_set(void* p, oop v, bool release = false) {
  45   assert(oopDesc::bs() != NULL, "Uninitialized bs in oop!");
  46   oopDesc::bs()->write_ref_field(p, v, release);
  47 }
  48 
  49 template <class T> inline void update_barrier_set_pre(T* p, oop v) {
  50   oopDesc::bs()->write_ref_field_pre(p, v);
  51 }
  52 
  53 template <class T> void oop_store(T* p, oop v) {
  54   if (always_do_update_barrier) {
  55     oop_store((volatile T*)p, v);
  56   } else {
  57     update_barrier_set_pre(p, v);
  58     oopDesc::encode_store_heap_oop(p, v);
  59     // always_do_update_barrier == false =>
  60     // Either we are at a safepoint (in GC) or CMS is not used. In both
  61     // cases it's unnecessary to mark the card as dirty with release sematics.
  62     update_barrier_set((void*)p, v, false /* release */);  // cast away type
  63   }
  64 }
  65 
  66 template <class T> void oop_store(volatile T* p, oop v) {
  67   update_barrier_set_pre((T*)p, v);   // cast away volatile
  68   // Used by release_obj_field_put, so use release_store_ptr.
  69   oopDesc::release_encode_store_heap_oop(p, v);
  70   // When using CMS we must mark the card corresponding to p as dirty
  71   // with release sematics to prevent that CMS sees the dirty card but
  72   // not the new value v at p due to reordering of the two
  73   // stores. Note that CMS has a concurrent precleaning phase, where
  74   // it reads the card table while the Java threads are running.
  75   update_barrier_set((void*)p, v, true /* release */);    // cast away type
  76 }
  77 
  78 // Should replace *addr = oop assignments where addr type depends on UseCompressedOops
  79 // (without having to remember the function name this calls).
  80 inline void oop_store_raw(HeapWord* addr, oop value) {
  81   if (UseCompressedOops) {
  82     oopDesc::encode_store_heap_oop((narrowOop*)addr, value);
  83   } else {
  84     oopDesc::encode_store_heap_oop((oop*)addr, value);
  85   }
  86 }
  87 
  88 // Implementation of all inlined member functions defined in oop.hpp
  89 // We need a separate file to avoid circular references
  90 
  91 void oopDesc::release_set_mark(markOop m) {
  92   OrderAccess::release_store_ptr(&_mark, m);
  93 }
  94 
  95 markOop oopDesc::cas_set_mark(markOop new_mark, markOop old_mark) {
  96   return (markOop) Atomic::cmpxchg_ptr(new_mark, &_mark, old_mark);
  97 }
  98 
  99 void oopDesc::init_mark() {
 100   set_mark(markOopDesc::prototype_for_object(this));
 101 }
 102 
 103 Klass* oopDesc::klass() const {
 104   if (UseCompressedClassPointers) {
 105     return Klass::decode_klass_not_null(_metadata._compressed_klass);
 106   } else {
 107     return _metadata._klass;
 108   }
 109 }
 110 
 111 Klass* oopDesc::klass_or_null() const volatile {
 112   if (UseCompressedClassPointers) {
 113     return Klass::decode_klass(_metadata._compressed_klass);
 114   } else {
 115     return _metadata._klass;
 116   }
 117 }
 118 
 119 Klass* oopDesc::klass_or_null_acquire() const volatile {
 120   if (UseCompressedClassPointers) {
 121     // Workaround for non-const load_acquire parameter.
 122     const volatile narrowKlass* addr = &_metadata._compressed_klass;
 123     volatile narrowKlass* xaddr = const_cast<volatile narrowKlass*>(addr);
 124     return Klass::decode_klass(OrderAccess::load_acquire(xaddr));
 125   } else {
 126     return (Klass*)OrderAccess::load_ptr_acquire(&_metadata._klass);
 127   }
 128 }
 129 
 130 Klass** oopDesc::klass_addr() {
 131   // Only used internally and with CMS and will not work with
 132   // UseCompressedOops
 133   assert(!UseCompressedClassPointers, "only supported with uncompressed klass pointers");
 134   return (Klass**) &_metadata._klass;
 135 }
 136 
 137 narrowKlass* oopDesc::compressed_klass_addr() {
 138   assert(UseCompressedClassPointers, "only called by compressed klass pointers");
 139   return &_metadata._compressed_klass;
 140 }
 141 
 142 #define CHECK_SET_KLASS(k)                                                \
 143   do {                                                                    \
 144     assert(Universe::is_bootstrapping() || k != NULL, "NULL Klass");      \
 145     assert(Universe::is_bootstrapping() || k->is_klass(), "not a Klass"); \
 146   } while (0)
 147 
 148 void oopDesc::set_klass(Klass* k) {
 149   CHECK_SET_KLASS(k);
 150   if (UseCompressedClassPointers) {
 151     *compressed_klass_addr() = Klass::encode_klass_not_null(k);
 152   } else {
 153     *klass_addr() = k;
 154   }
 155 }
 156 
 157 void oopDesc::release_set_klass(Klass* k) {
 158   CHECK_SET_KLASS(k);
 159   if (UseCompressedClassPointers) {
 160     OrderAccess::release_store(compressed_klass_addr(),
 161                                Klass::encode_klass_not_null(k));
 162   } else {
 163     OrderAccess::release_store_ptr(klass_addr(), k);
 164   }
 165 }
 166 
 167 #undef CHECK_SET_KLASS
 168 
 169 int oopDesc::klass_gap() const {
 170   return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes());
 171 }
 172 
 173 void oopDesc::set_klass_gap(int v) {
 174   if (UseCompressedClassPointers) {
 175     *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes()) = v;
 176   }
 177 }
 178 
 179 void oopDesc::set_klass_to_list_ptr(oop k) {
 180   // This is only to be used during GC, for from-space objects, so no
 181   // barrier is needed.
 182   if (UseCompressedClassPointers) {
 183     _metadata._compressed_klass = (narrowKlass)encode_heap_oop(k);  // may be null (parnew overflow handling)
 184   } else {
 185     _metadata._klass = (Klass*)(address)k;
 186   }
 187 }
 188 
 189 oop oopDesc::list_ptr_from_klass() {
 190   // This is only to be used during GC, for from-space objects.
 191   if (UseCompressedClassPointers) {
 192     return decode_heap_oop((narrowOop)_metadata._compressed_klass);
 193   } else {
 194     // Special case for GC
 195     return (oop)(address)_metadata._klass;
 196   }
 197 }
 198 
 199 bool oopDesc::is_a(Klass* k) const {
 200   return klass()->is_subtype_of(k);
 201 }
 202 
 203 int oopDesc::size()  {
 204   return size_given_klass(klass());
 205 }
 206 
 207 int oopDesc::size_given_klass(Klass* klass)  {
 208   int lh = klass->layout_helper();
 209   int s;
 210 
 211   // lh is now a value computed at class initialization that may hint
 212   // at the size.  For instances, this is positive and equal to the
 213   // size.  For arrays, this is negative and provides log2 of the
 214   // array element size.  For other oops, it is zero and thus requires
 215   // a virtual call.
 216   //
 217   // We go to all this trouble because the size computation is at the
 218   // heart of phase 2 of mark-compaction, and called for every object,
 219   // alive or dead.  So the speed here is equal in importance to the
 220   // speed of allocation.
 221 
 222   if (lh > Klass::_lh_neutral_value) {
 223     if (!Klass::layout_helper_needs_slow_path(lh)) {
 224       s = lh >> LogHeapWordSize;  // deliver size scaled by wordSize
 225     } else {
 226       s = klass->oop_size(this);
 227     }
 228   } else if (lh <= Klass::_lh_neutral_value) {
 229     // The most common case is instances; fall through if so.
 230     if (lh < Klass::_lh_neutral_value) {
 231       // Second most common case is arrays.  We have to fetch the
 232       // length of the array, shift (multiply) it appropriately,
 233       // up to wordSize, add the header, and align to object size.
 234       size_t size_in_bytes;














 235       size_t array_length = (size_t) ((arrayOop)this)->length();
 236       size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh);

 237       size_in_bytes += Klass::layout_helper_header_size(lh);
 238 
 239       // This code could be simplified, but by keeping array_header_in_bytes
 240       // in units of bytes and doing it this way we can round up just once,
 241       // skipping the intermediate round to HeapWordSize.  Cast the result
 242       // of round_to to size_t to guarantee unsigned division == right shift.
 243       s = (int)((size_t)round_to(size_in_bytes, MinObjAlignmentInBytes) /
 244         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 % MinObjAlignment == 0, "Oop size is not properly aligned: %d", s);
 273   assert(s > 0, "Oop size must be greater than zero, not %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 
 282 void*      oopDesc::field_base(int offset)          const { return (void*)&((char*)this)[offset]; }
 283 
 284 jbyte*     oopDesc::byte_field_addr(int offset)     const { return (jbyte*)    field_base(offset); }
 285 jchar*     oopDesc::char_field_addr(int offset)     const { return (jchar*)    field_base(offset); }
 286 jboolean*  oopDesc::bool_field_addr(int offset)     const { return (jboolean*) field_base(offset); }
 287 jint*      oopDesc::int_field_addr(int offset)      const { return (jint*)     field_base(offset); }
 288 jshort*    oopDesc::short_field_addr(int offset)    const { return (jshort*)   field_base(offset); }
 289 jlong*     oopDesc::long_field_addr(int offset)     const { return (jlong*)    field_base(offset); }
 290 jfloat*    oopDesc::float_field_addr(int offset)    const { return (jfloat*)   field_base(offset); }
 291 jdouble*   oopDesc::double_field_addr(int offset)   const { return (jdouble*)  field_base(offset); }
 292 Metadata** oopDesc::metadata_field_addr(int offset) const { return (Metadata**)field_base(offset); }
 293 
 294 template <class T> T* oopDesc::obj_field_addr(int offset) const { return (T*)  field_base(offset); }
 295 address*   oopDesc::address_field_addr(int offset)  const { return (address*)  field_base(offset); }
 296 
 297 
 298 // Functions for getting and setting oops within instance objects.
 299 // If the oops are compressed, the type passed to these overloaded functions
 300 // is narrowOop.  All functions are overloaded so they can be called by
 301 // template functions without conditionals (the compiler instantiates via
 302 // the right type and inlines the appopriate code).
 303 
 304 // Algorithm for encoding and decoding oops from 64 bit pointers to 32 bit
 305 // offset from the heap base.  Saving the check for null can save instructions
 306 // in inner GC loops so these are separated.
 307 
 308 inline bool check_obj_alignment(oop obj) {
 309   return (cast_from_oop<intptr_t>(obj) & MinObjAlignmentInBytesMask) == 0;
 310 }
 311 
 312 oop oopDesc::decode_heap_oop_not_null(narrowOop v) {
 313   assert(!is_null(v), "narrow oop value can never be zero");
 314   address base = Universe::narrow_oop_base();
 315   int    shift = Universe::narrow_oop_shift();
 316   oop result = (oop)(void*)((uintptr_t)base + ((uintptr_t)v << shift));
 317   assert(check_obj_alignment(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result));
 318   return result;
 319 }
 320 
 321 oop oopDesc::decode_heap_oop(narrowOop v) {
 322   return is_null(v) ? (oop)NULL : decode_heap_oop_not_null(v);
 323 }
 324 
 325 narrowOop oopDesc::encode_heap_oop_not_null(oop v) {
 326   assert(!is_null(v), "oop value can never be zero");
 327   assert(check_obj_alignment(v), "Address not aligned");
 328   assert(Universe::heap()->is_in_reserved(v), "Address not in heap");
 329   address base = Universe::narrow_oop_base();
 330   int    shift = Universe::narrow_oop_shift();
 331   uint64_t  pd = (uint64_t)(pointer_delta((void*)v, (void*)base, 1));
 332   assert(OopEncodingHeapMax > pd, "change encoding max if new encoding");
 333   uint64_t result = pd >> shift;
 334   assert((result & CONST64(0xffffffff00000000)) == 0, "narrow oop overflow");
 335   assert(decode_heap_oop(result) == v, "reversibility");
 336   return (narrowOop)result;
 337 }
 338 
 339 narrowOop oopDesc::encode_heap_oop(oop v) {
 340   return (is_null(v)) ? (narrowOop)0 : encode_heap_oop_not_null(v);
 341 }
 342 
 343 // Load and decode an oop out of the Java heap into a wide oop.
 344 oop oopDesc::load_decode_heap_oop_not_null(narrowOop* p) {
 345   return decode_heap_oop_not_null(*p);
 346 }
 347 
 348 // Load and decode an oop out of the heap accepting null
 349 oop oopDesc::load_decode_heap_oop(narrowOop* p) {
 350   return decode_heap_oop(*p);
 351 }
 352 
 353 // Encode and store a heap oop.
 354 void oopDesc::encode_store_heap_oop_not_null(narrowOop* p, oop v) {
 355   *p = encode_heap_oop_not_null(v);
 356 }
 357 
 358 // Encode and store a heap oop allowing for null.
 359 void oopDesc::encode_store_heap_oop(narrowOop* p, oop v) {
 360   *p = encode_heap_oop(v);
 361 }
 362 
 363 // Store heap oop as is for volatile fields.
 364 void oopDesc::release_store_heap_oop(volatile oop* p, oop v) {
 365   OrderAccess::release_store_ptr(p, v);
 366 }
 367 void oopDesc::release_store_heap_oop(volatile narrowOop* p, narrowOop v) {
 368   OrderAccess::release_store(p, v);
 369 }
 370 
 371 void oopDesc::release_encode_store_heap_oop_not_null(volatile narrowOop* p, oop v) {
 372   // heap oop is not pointer sized.
 373   OrderAccess::release_store(p, encode_heap_oop_not_null(v));
 374 }
 375 void oopDesc::release_encode_store_heap_oop_not_null(volatile oop* p, oop v) {
 376   OrderAccess::release_store_ptr(p, v);
 377 }
 378 
 379 void oopDesc::release_encode_store_heap_oop(volatile oop* p, oop v) {
 380   OrderAccess::release_store_ptr(p, v);
 381 }
 382 void oopDesc::release_encode_store_heap_oop(volatile narrowOop* p, oop v) {
 383   OrderAccess::release_store(p, encode_heap_oop(v));
 384 }
 385 
 386 // These functions are only used to exchange oop fields in instances,
 387 // not headers.
 388 oop oopDesc::atomic_exchange_oop(oop exchange_value, volatile HeapWord *dest) {
 389   if (UseCompressedOops) {
 390     // encode exchange value from oop to T
 391     narrowOop val = encode_heap_oop(exchange_value);
 392     narrowOop old = (narrowOop)Atomic::xchg(val, (narrowOop*)dest);
 393     // decode old from T to oop
 394     return decode_heap_oop(old);
 395   } else {
 396     return (oop)Atomic::xchg_ptr(exchange_value, (oop*)dest);
 397   }
 398 }
 399 
 400 oop oopDesc::atomic_compare_exchange_oop(oop exchange_value,
 401                                          volatile HeapWord *dest,
 402                                          oop compare_value,
 403                                          bool prebarrier) {
 404   if (UseCompressedOops) {
 405     if (prebarrier) {
 406       update_barrier_set_pre((narrowOop*)dest, exchange_value);
 407     }
 408     // encode exchange and compare value from oop to T
 409     narrowOop val = encode_heap_oop(exchange_value);
 410     narrowOop cmp = encode_heap_oop(compare_value);
 411 
 412     narrowOop old = (narrowOop) Atomic::cmpxchg(val, (narrowOop*)dest, cmp);
 413     // decode old from T to oop
 414     return decode_heap_oop(old);
 415   } else {
 416     if (prebarrier) {
 417       update_barrier_set_pre((oop*)dest, exchange_value);
 418     }
 419     return (oop)Atomic::cmpxchg_ptr(exchange_value, (oop*)dest, compare_value);
 420   }
 421 }
 422 
 423 // In order to put or get a field out of an instance, must first check
 424 // if the field has been compressed and uncompress it.
 425 oop oopDesc::obj_field(int offset) const {
 426   return UseCompressedOops ?
 427     load_decode_heap_oop(obj_field_addr<narrowOop>(offset)) :
 428     load_decode_heap_oop(obj_field_addr<oop>(offset));
 429 }
 430 
 431 void oopDesc::obj_field_put(int offset, oop value) {
 432   UseCompressedOops ? oop_store(obj_field_addr<narrowOop>(offset), value) :
 433                       oop_store(obj_field_addr<oop>(offset),       value);
 434 }
 435 
 436 void oopDesc::obj_field_put_raw(int offset, oop value) {
 437   UseCompressedOops ?
 438     encode_store_heap_oop(obj_field_addr<narrowOop>(offset), value) :
 439     encode_store_heap_oop(obj_field_addr<oop>(offset),       value);
 440 }
 441 void oopDesc::obj_field_put_volatile(int offset, oop value) {
 442   OrderAccess::release();
 443   obj_field_put(offset, value);
 444   OrderAccess::fence();
 445 }
 446 
 447 Metadata* oopDesc::metadata_field(int offset) const           { return *metadata_field_addr(offset);   }
 448 void oopDesc::metadata_field_put(int offset, Metadata* value) { *metadata_field_addr(offset) = value;  }
 449 
 450 jbyte oopDesc::byte_field(int offset) const                   { return (jbyte) *byte_field_addr(offset);    }
 451 void oopDesc::byte_field_put(int offset, jbyte contents)      { *byte_field_addr(offset) = (jint) contents; }
 452 
 453 jchar oopDesc::char_field(int offset) const                   { return (jchar) *char_field_addr(offset);    }
 454 void oopDesc::char_field_put(int offset, jchar contents)      { *char_field_addr(offset) = (jint) contents; }
 455 
 456 jboolean oopDesc::bool_field(int offset) const                { return (jboolean) *bool_field_addr(offset); }
 457 void oopDesc::bool_field_put(int offset, jboolean contents)   { *bool_field_addr(offset) = (((jint) contents) & 1); }
 458 
 459 jint oopDesc::int_field(int offset) const                     { return *int_field_addr(offset);        }
 460 void oopDesc::int_field_put(int offset, jint contents)        { *int_field_addr(offset) = contents;    }
 461 
 462 jshort oopDesc::short_field(int offset) const                 { return (jshort) *short_field_addr(offset);  }
 463 void oopDesc::short_field_put(int offset, jshort contents)    { *short_field_addr(offset) = (jint) contents;}
 464 
 465 jlong oopDesc::long_field(int offset) const                   { return *long_field_addr(offset);       }
 466 void oopDesc::long_field_put(int offset, jlong contents)      { *long_field_addr(offset) = contents;   }
 467 
 468 jfloat oopDesc::float_field(int offset) const                 { return *float_field_addr(offset);      }
 469 void oopDesc::float_field_put(int offset, jfloat contents)    { *float_field_addr(offset) = contents;  }
 470 
 471 jdouble oopDesc::double_field(int offset) const               { return *double_field_addr(offset);     }
 472 void oopDesc::double_field_put(int offset, jdouble contents)  { *double_field_addr(offset) = contents; }
 473 
 474 address oopDesc::address_field(int offset) const              { return *address_field_addr(offset);     }
 475 void oopDesc::address_field_put(int offset, address contents) { *address_field_addr(offset) = contents; }
 476 
 477 oop oopDesc::obj_field_acquire(int offset) const {
 478   return UseCompressedOops ?
 479              decode_heap_oop((narrowOop)
 480                OrderAccess::load_acquire(obj_field_addr<narrowOop>(offset)))
 481            : decode_heap_oop((oop)
 482                OrderAccess::load_ptr_acquire(obj_field_addr<oop>(offset)));
 483 }
 484 void oopDesc::release_obj_field_put(int offset, oop value) {
 485   UseCompressedOops ?
 486     oop_store((volatile narrowOop*)obj_field_addr<narrowOop>(offset), value) :
 487     oop_store((volatile oop*)      obj_field_addr<oop>(offset),       value);
 488 }
 489 
 490 jbyte oopDesc::byte_field_acquire(int offset) const                   { return OrderAccess::load_acquire(byte_field_addr(offset));     }
 491 void oopDesc::release_byte_field_put(int offset, jbyte contents)      { OrderAccess::release_store(byte_field_addr(offset), contents); }
 492 
 493 jchar oopDesc::char_field_acquire(int offset) const                   { return OrderAccess::load_acquire(char_field_addr(offset));     }
 494 void oopDesc::release_char_field_put(int offset, jchar contents)      { OrderAccess::release_store(char_field_addr(offset), contents); }
 495 
 496 jboolean oopDesc::bool_field_acquire(int offset) const                { return OrderAccess::load_acquire(bool_field_addr(offset));     }
 497 void oopDesc::release_bool_field_put(int offset, jboolean contents)   { OrderAccess::release_store(bool_field_addr(offset), (contents & 1)); }
 498 
 499 jint oopDesc::int_field_acquire(int offset) const                     { return OrderAccess::load_acquire(int_field_addr(offset));      }
 500 void oopDesc::release_int_field_put(int offset, jint contents)        { OrderAccess::release_store(int_field_addr(offset), contents);  }
 501 
 502 jshort oopDesc::short_field_acquire(int offset) const                 { return (jshort)OrderAccess::load_acquire(short_field_addr(offset)); }
 503 void oopDesc::release_short_field_put(int offset, jshort contents)    { OrderAccess::release_store(short_field_addr(offset), contents);     }
 504 
 505 jlong oopDesc::long_field_acquire(int offset) const                   { return OrderAccess::load_acquire(long_field_addr(offset));       }
 506 void oopDesc::release_long_field_put(int offset, jlong contents)      { OrderAccess::release_store(long_field_addr(offset), contents);   }
 507 
 508 jfloat oopDesc::float_field_acquire(int offset) const                 { return OrderAccess::load_acquire(float_field_addr(offset));      }
 509 void oopDesc::release_float_field_put(int offset, jfloat contents)    { OrderAccess::release_store(float_field_addr(offset), contents);  }
 510 
 511 jdouble oopDesc::double_field_acquire(int offset) const               { return OrderAccess::load_acquire(double_field_addr(offset));     }
 512 void oopDesc::release_double_field_put(int offset, jdouble contents)  { OrderAccess::release_store(double_field_addr(offset), contents); }
 513 
 514 address oopDesc::address_field_acquire(int offset) const              { return (address) OrderAccess::load_ptr_acquire(address_field_addr(offset)); }
 515 void oopDesc::release_address_field_put(int offset, address contents) { OrderAccess::release_store_ptr(address_field_addr(offset), contents); }
 516 
 517 bool oopDesc::is_locked() const {
 518   return mark()->is_locked();
 519 }
 520 
 521 bool oopDesc::is_unlocked() const {
 522   return mark()->is_unlocked();
 523 }
 524 
 525 bool oopDesc::has_bias_pattern() const {
 526   return mark()->has_bias_pattern();
 527 }
 528 
 529 // used only for asserts
 530 bool oopDesc::is_oop(bool ignore_mark_word) const {
 531   oop obj = (oop) this;
 532   if (!check_obj_alignment(obj)) return false;
 533   if (!Universe::heap()->is_in_reserved(obj)) return false;
 534   // obj is aligned and accessible in heap
 535   if (Universe::heap()->is_in_reserved(obj->klass_or_null())) return false;
 536 
 537   // Header verification: the mark is typically non-NULL. If we're
 538   // at a safepoint, it must not be null.
 539   // Outside of a safepoint, the header could be changing (for example,
 540   // another thread could be inflating a lock on this object).
 541   if (ignore_mark_word) {
 542     return true;
 543   }
 544   if (mark() != NULL) {
 545     return true;
 546   }
 547   return !SafepointSynchronize::is_at_safepoint();
 548 }
 549 
 550 
 551 // used only for asserts
 552 bool oopDesc::is_oop_or_null(bool ignore_mark_word) const {
 553   return this == NULL ? true : is_oop(ignore_mark_word);
 554 }
 555 
 556 #ifndef PRODUCT
 557 // used only for asserts
 558 bool oopDesc::is_unlocked_oop() const {
 559   if (!Universe::heap()->is_in_reserved(this)) return false;
 560   return mark()->is_unlocked();
 561 }
 562 #endif // PRODUCT
 563 
 564 // Used only for markSweep, scavenging
 565 bool oopDesc::is_gc_marked() const {
 566   return mark()->is_marked();
 567 }
 568 
 569 bool oopDesc::is_scavengable() const {
 570   return Universe::heap()->is_scavengable(this);
 571 }
 572 
 573 // Used by scavengers
 574 bool oopDesc::is_forwarded() const {
 575   // The extra heap check is needed since the obj might be locked, in which case the
 576   // mark would point to a stack location and have the sentinel bit cleared
 577   return mark()->is_marked();
 578 }
 579 
 580 // Used by scavengers
 581 void oopDesc::forward_to(oop p) {
 582   assert(check_obj_alignment(p),
 583          "forwarding to something not aligned");
 584   assert(Universe::heap()->is_in_reserved(p),
 585          "forwarding to something not in heap");
 586   markOop m = markOopDesc::encode_pointer_as_mark(p);
 587   assert(m->decode_pointer() == p, "encoding must be reversable");
 588   set_mark(m);
 589 }
 590 
 591 // Used by parallel scavengers
 592 bool oopDesc::cas_forward_to(oop p, markOop compare) {
 593   assert(check_obj_alignment(p),
 594          "forwarding to something not aligned");
 595   assert(Universe::heap()->is_in_reserved(p),
 596          "forwarding to something not in heap");
 597   markOop m = markOopDesc::encode_pointer_as_mark(p);
 598   assert(m->decode_pointer() == p, "encoding must be reversable");
 599   return cas_set_mark(m, compare) == compare;
 600 }
 601 
 602 #if INCLUDE_ALL_GCS
 603 oop oopDesc::forward_to_atomic(oop p) {
 604   markOop oldMark = mark();
 605   markOop forwardPtrMark = markOopDesc::encode_pointer_as_mark(p);
 606   markOop curMark;
 607 
 608   assert(forwardPtrMark->decode_pointer() == p, "encoding must be reversable");
 609   assert(sizeof(markOop) == sizeof(intptr_t), "CAS below requires this.");
 610 
 611   while (!oldMark->is_marked()) {
 612     curMark = (markOop)Atomic::cmpxchg_ptr(forwardPtrMark, &_mark, oldMark);
 613     assert(is_forwarded(), "object should have been forwarded");
 614     if (curMark == oldMark) {
 615       return NULL;
 616     }
 617     // If the CAS was unsuccessful then curMark->is_marked()
 618     // should return true as another thread has CAS'd in another
 619     // forwarding pointer.
 620     oldMark = curMark;
 621   }
 622   return forwardee();
 623 }
 624 #endif
 625 
 626 // Note that the forwardee is not the same thing as the displaced_mark.
 627 // The forwardee is used when copying during scavenge and mark-sweep.
 628 // It does need to clear the low two locking- and GC-related bits.
 629 oop oopDesc::forwardee() const {
 630   return (oop) mark()->decode_pointer();
 631 }
 632 
 633 // The following method needs to be MT safe.
 634 uint oopDesc::age() const {
 635   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 636   if (has_displaced_mark()) {
 637     return displaced_mark()->age();
 638   } else {
 639     return mark()->age();
 640   }
 641 }
 642 
 643 void oopDesc::incr_age() {
 644   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 645   if (has_displaced_mark()) {
 646     set_displaced_mark(displaced_mark()->incr_age());
 647   } else {
 648     set_mark(mark()->incr_age());
 649   }
 650 }
 651 
 652 #if INCLUDE_ALL_GCS
 653 void oopDesc::pc_follow_contents(ParCompactionManager* cm) {
 654   klass()->oop_pc_follow_contents(this, cm);
 655 }
 656 
 657 void oopDesc::pc_update_contents(ParCompactionManager* cm) {
 658   Klass* k = klass();
 659   if (!k->is_typeArray_klass()) {
 660     // It might contain oops beyond the header, so take the virtual call.
 661     k->oop_pc_update_pointers(this, cm);
 662   }
 663   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 664 }
 665 
 666 void oopDesc::ps_push_contents(PSPromotionManager* pm) {
 667   Klass* k = klass();
 668   if (!k->is_typeArray_klass()) {
 669     // It might contain oops beyond the header, so take the virtual call.
 670     k->oop_ps_push_contents(this, pm);
 671   }
 672   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 673 }
 674 #endif // INCLUDE_ALL_GCS
 675 
 676 #define OOP_ITERATE_DEFN(OopClosureType, nv_suffix)                 \
 677                                                                     \
 678 void oopDesc::oop_iterate(OopClosureType* blk) {                    \
 679   klass()->oop_oop_iterate##nv_suffix(this, blk);                   \
 680 }                                                                   \
 681                                                                     \
 682 void oopDesc::oop_iterate(OopClosureType* blk, MemRegion mr) {      \
 683   klass()->oop_oop_iterate_bounded##nv_suffix(this, blk, mr);       \
 684 }
 685 
 686 #define OOP_ITERATE_SIZE_DEFN(OopClosureType, nv_suffix)            \
 687                                                                     \
 688 int oopDesc::oop_iterate_size(OopClosureType* blk) {                \
 689   Klass* k = klass();                                               \
 690   int size = size_given_klass(k);                                   \
 691   k->oop_oop_iterate##nv_suffix(this, blk);                         \
 692   return size;                                                      \
 693 }                                                                   \
 694                                                                     \
 695 int oopDesc::oop_iterate_size(OopClosureType* blk, MemRegion mr) {  \
 696   Klass* k = klass();                                               \
 697   int size = size_given_klass(k);                                   \
 698   k->oop_oop_iterate_bounded##nv_suffix(this, blk, mr);             \
 699   return size;                                                      \
 700 }
 701 
 702 int oopDesc::oop_iterate_no_header(OopClosure* blk) {
 703   // The NoHeaderExtendedOopClosure wraps the OopClosure and proxies all
 704   // the do_oop calls, but turns off all other features in ExtendedOopClosure.
 705   NoHeaderExtendedOopClosure cl(blk);
 706   return oop_iterate_size(&cl);
 707 }
 708 
 709 int oopDesc::oop_iterate_no_header(OopClosure* blk, MemRegion mr) {
 710   NoHeaderExtendedOopClosure cl(blk);
 711   return oop_iterate_size(&cl, mr);
 712 }
 713 
 714 #if INCLUDE_ALL_GCS
 715 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)       \
 716                                                                     \
 717 inline void oopDesc::oop_iterate_backwards(OopClosureType* blk) {   \
 718   klass()->oop_oop_iterate_backwards##nv_suffix(this, blk);         \
 719 }
 720 #else
 721 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)
 722 #endif // INCLUDE_ALL_GCS
 723 
 724 #define ALL_OOPDESC_OOP_ITERATE(OopClosureType, nv_suffix)  \
 725   OOP_ITERATE_DEFN(OopClosureType, nv_suffix)               \
 726   OOP_ITERATE_SIZE_DEFN(OopClosureType, nv_suffix)          \
 727   OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)
 728 
 729 ALL_OOP_OOP_ITERATE_CLOSURES_1(ALL_OOPDESC_OOP_ITERATE)
 730 ALL_OOP_OOP_ITERATE_CLOSURES_2(ALL_OOPDESC_OOP_ITERATE)
 731 
 732 intptr_t oopDesc::identity_hash() {
 733   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 734   // Note: The mark must be read into local variable to avoid concurrent updates.
 735   markOop mrk = mark();
 736   if (mrk->is_unlocked() && !mrk->has_no_hash()) {
 737     return mrk->hash();
 738   } else if (mrk->is_marked()) {
 739     return mrk->hash();
 740   } else {
 741     return slow_identity_hash();
 742   }
 743 }
 744 
 745 bool oopDesc::has_displaced_mark() const {
 746   return mark()->has_displaced_mark_helper();
 747 }
 748 
 749 markOop oopDesc::displaced_mark() const {
 750   return mark()->displaced_mark_helper();
 751 }
 752 
 753 void oopDesc::set_displaced_mark(markOop m) {
 754   mark()->set_displaced_mark_helper(m);
 755 }
 756 
 757 #endif // SHARE_VM_OOPS_OOP_INLINE_HPP
--- EOF ---