1 /*
   2  * Copyright (c) 1997, 2015, 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/serial/markSweep.inline.hpp"
  29 #include "gc/shared/ageTable.hpp"
  30 #include "gc/shared/barrierSet.inline.hpp"
  31 #include "gc/shared/cardTableModRefBS.hpp"
  32 #include "gc/shared/collectedHeap.inline.hpp"
  33 #include "gc/shared/genCollectedHeap.hpp"
  34 #include "gc/shared/generation.hpp"
  35 #include "oops/arrayKlass.hpp"
  36 #include "oops/arrayOop.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/markOop.inline.hpp"
  39 #include "oops/oop.hpp"
  40 #include "runtime/atomic.inline.hpp"
  41 #include "runtime/orderAccess.inline.hpp"
  42 #include "runtime/os.hpp"
  43 #include "utilities/macros.hpp"
  44 
  45 // Implementation of all inlined member functions defined in oop.hpp
  46 // We need a separate file to avoid circular references
  47 
  48 inline void oopDesc::release_set_mark(markOop m) {
  49   OrderAccess::release_store_ptr(&_mark, m);
  50 }
  51 
  52 inline markOop oopDesc::cas_set_mark(markOop new_mark, markOop old_mark) {
  53   return (markOop) Atomic::cmpxchg_ptr(new_mark, &_mark, old_mark);
  54 }
  55 
  56 inline Klass* oopDesc::klass() const {
  57   if (UseCompressedClassPointers) {
  58     return Klass::decode_klass_not_null(_metadata._compressed_klass);
  59   } else {
  60     return _metadata._klass;
  61   }
  62 }
  63 
  64 inline Klass* oopDesc::klass_or_null() const volatile {
  65   // can be NULL in CMS
  66   if (UseCompressedClassPointers) {
  67     return Klass::decode_klass(_metadata._compressed_klass);
  68   } else {
  69     return _metadata._klass;
  70   }
  71 }
  72 
  73 inline Klass** oopDesc::klass_addr() {
  74   // Only used internally and with CMS and will not work with
  75   // UseCompressedOops
  76   assert(!UseCompressedClassPointers, "only supported with uncompressed klass pointers");
  77   return (Klass**) &_metadata._klass;
  78 }
  79 
  80 inline narrowKlass* oopDesc::compressed_klass_addr() {
  81   assert(UseCompressedClassPointers, "only called by compressed klass pointers");
  82   return &_metadata._compressed_klass;
  83 }
  84 
  85 inline void oopDesc::set_klass(Klass* k) {
  86   // since klasses are promoted no store check is needed
  87   assert(Universe::is_bootstrapping() || k != NULL, "must be a real Klass*");
  88   assert(Universe::is_bootstrapping() || k->is_klass(), "not a Klass*");
  89   if (UseCompressedClassPointers) {
  90     *compressed_klass_addr() = Klass::encode_klass_not_null(k);
  91   } else {
  92     *klass_addr() = k;
  93   }
  94 }
  95 
  96 inline int oopDesc::klass_gap() const {
  97   return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes());
  98 }
  99 
 100 inline void oopDesc::set_klass_gap(int v) {
 101   if (UseCompressedClassPointers) {
 102     *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes()) = v;
 103   }
 104 }
 105 
 106 inline void oopDesc::set_klass_to_list_ptr(oop k) {
 107   // This is only to be used during GC, for from-space objects, so no
 108   // barrier is needed.
 109   if (UseCompressedClassPointers) {
 110     _metadata._compressed_klass = (narrowKlass)encode_heap_oop(k);  // may be null (parnew overflow handling)
 111   } else {
 112     _metadata._klass = (Klass*)(address)k;
 113   }
 114 }
 115 
 116 inline oop oopDesc::list_ptr_from_klass() {
 117   // This is only to be used during GC, for from-space objects.
 118   if (UseCompressedClassPointers) {
 119     return decode_heap_oop((narrowOop)_metadata._compressed_klass);
 120   } else {
 121     // Special case for GC
 122     return (oop)(address)_metadata._klass;
 123   }
 124 }
 125 
 126 inline void   oopDesc::init_mark()                 { set_mark(markOopDesc::prototype_for_object(this)); }
 127 
 128 inline bool oopDesc::is_a(Klass* k)        const { return klass()->is_subtype_of(k); }
 129 
 130 inline bool oopDesc::is_instance()            const { return klass()->oop_is_instance(); }
 131 inline bool oopDesc::is_instanceClassLoader() const { return klass()->oop_is_instanceClassLoader(); }
 132 inline bool oopDesc::is_instanceMirror()      const { return klass()->oop_is_instanceMirror(); }
 133 inline bool oopDesc::is_instanceRef()         const { return klass()->oop_is_instanceRef(); }
 134 inline bool oopDesc::is_array()               const { return klass()->oop_is_array(); }
 135 inline bool oopDesc::is_objArray()            const { return klass()->oop_is_objArray(); }
 136 inline bool oopDesc::is_typeArray()           const { return klass()->oop_is_typeArray(); }
 137 
 138 inline void*     oopDesc::field_base(int offset)        const { return (void*)&((char*)this)[offset]; }
 139 
 140 template <class T> inline T* oopDesc::obj_field_addr(int offset) const { return (T*)field_base(offset); }
 141 inline Metadata** oopDesc::metadata_field_addr(int offset) const { return (Metadata**)field_base(offset); }
 142 inline jbyte*    oopDesc::byte_field_addr(int offset)   const { return (jbyte*)   field_base(offset); }
 143 inline jchar*    oopDesc::char_field_addr(int offset)   const { return (jchar*)   field_base(offset); }
 144 inline jboolean* oopDesc::bool_field_addr(int offset)   const { return (jboolean*)field_base(offset); }
 145 inline jint*     oopDesc::int_field_addr(int offset)    const { return (jint*)    field_base(offset); }
 146 inline jshort*   oopDesc::short_field_addr(int offset)  const { return (jshort*)  field_base(offset); }
 147 inline jlong*    oopDesc::long_field_addr(int offset)   const { return (jlong*)   field_base(offset); }
 148 inline jfloat*   oopDesc::float_field_addr(int offset)  const { return (jfloat*)  field_base(offset); }
 149 inline jdouble*  oopDesc::double_field_addr(int offset) const { return (jdouble*) field_base(offset); }
 150 inline address*  oopDesc::address_field_addr(int offset) const { return (address*) field_base(offset); }
 151 
 152 
 153 // Functions for getting and setting oops within instance objects.
 154 // If the oops are compressed, the type passed to these overloaded functions
 155 // is narrowOop.  All functions are overloaded so they can be called by
 156 // template functions without conditionals (the compiler instantiates via
 157 // the right type and inlines the appopriate code).
 158 
 159 inline bool oopDesc::is_null(oop obj)       { return obj == NULL; }
 160 inline bool oopDesc::is_null(narrowOop obj) { return obj == 0; }
 161 
 162 // Algorithm for encoding and decoding oops from 64 bit pointers to 32 bit
 163 // offset from the heap base.  Saving the check for null can save instructions
 164 // in inner GC loops so these are separated.
 165 
 166 inline bool check_obj_alignment(oop obj) {
 167   return cast_from_oop<intptr_t>(obj) % MinObjAlignmentInBytes == 0;
 168 }
 169 
 170 inline narrowOop oopDesc::encode_heap_oop_not_null(oop v) {
 171   assert(!is_null(v), "oop value can never be zero");
 172   assert(check_obj_alignment(v), "Address not aligned");
 173   assert(Universe::heap()->is_in_reserved(v), "Address not in heap");
 174   address base = Universe::narrow_oop_base();
 175   int    shift = Universe::narrow_oop_shift();
 176   uint64_t  pd = (uint64_t)(pointer_delta((void*)v, (void*)base, 1));
 177   assert(OopEncodingHeapMax > pd, "change encoding max if new encoding");
 178   uint64_t result = pd >> shift;
 179   assert((result & CONST64(0xffffffff00000000)) == 0, "narrow oop overflow");
 180   assert(decode_heap_oop(result) == v, "reversibility");
 181   return (narrowOop)result;
 182 }
 183 
 184 inline narrowOop oopDesc::encode_heap_oop(oop v) {
 185   return (is_null(v)) ? (narrowOop)0 : encode_heap_oop_not_null(v);
 186 }
 187 
 188 inline oop oopDesc::decode_heap_oop_not_null(narrowOop v) {
 189   assert(!is_null(v), "narrow oop value can never be zero");
 190   address base = Universe::narrow_oop_base();
 191   int    shift = Universe::narrow_oop_shift();
 192   oop result = (oop)(void*)((uintptr_t)base + ((uintptr_t)v << shift));
 193   assert(check_obj_alignment(result), err_msg("address not aligned: " INTPTR_FORMAT, p2i((void*) result)));
 194   return result;
 195 }
 196 
 197 inline oop oopDesc::decode_heap_oop(narrowOop v) {
 198   return is_null(v) ? (oop)NULL : decode_heap_oop_not_null(v);
 199 }
 200 
 201 inline oop oopDesc::decode_heap_oop_not_null(oop v) { return v; }
 202 inline oop oopDesc::decode_heap_oop(oop v)  { return v; }
 203 
 204 // Load an oop out of the Java heap as is without decoding.
 205 // Called by GC to check for null before decoding.
 206 inline oop       oopDesc::load_heap_oop(oop* p)          { return *p; }
 207 inline narrowOop oopDesc::load_heap_oop(narrowOop* p)    { return *p; }
 208 
 209 // Load and decode an oop out of the Java heap into a wide oop.
 210 inline oop oopDesc::load_decode_heap_oop_not_null(oop* p)       { return *p; }
 211 inline oop oopDesc::load_decode_heap_oop_not_null(narrowOop* p) {
 212   return decode_heap_oop_not_null(*p);
 213 }
 214 
 215 // Load and decode an oop out of the heap accepting null
 216 inline oop oopDesc::load_decode_heap_oop(oop* p) { return *p; }
 217 inline oop oopDesc::load_decode_heap_oop(narrowOop* p) {
 218   return decode_heap_oop(*p);
 219 }
 220 
 221 // Store already encoded heap oop into the heap.
 222 inline void oopDesc::store_heap_oop(oop* p, oop v)                 { *p = v; }
 223 inline void oopDesc::store_heap_oop(narrowOop* p, narrowOop v)     { *p = v; }
 224 
 225 // Encode and store a heap oop.
 226 inline void oopDesc::encode_store_heap_oop_not_null(narrowOop* p, oop v) {
 227   *p = encode_heap_oop_not_null(v);
 228 }
 229 inline void oopDesc::encode_store_heap_oop_not_null(oop* p, oop v) { *p = v; }
 230 
 231 // Encode and store a heap oop allowing for null.
 232 inline void oopDesc::encode_store_heap_oop(narrowOop* p, oop v) {
 233   *p = encode_heap_oop(v);
 234 }
 235 inline void oopDesc::encode_store_heap_oop(oop* p, oop v) { *p = v; }
 236 
 237 // Store heap oop as is for volatile fields.
 238 inline void oopDesc::release_store_heap_oop(volatile oop* p, oop v) {
 239   OrderAccess::release_store_ptr(p, v);
 240 }
 241 inline void oopDesc::release_store_heap_oop(volatile narrowOop* p,
 242                                             narrowOop v) {
 243   OrderAccess::release_store(p, v);
 244 }
 245 
 246 inline void oopDesc::release_encode_store_heap_oop_not_null(
 247                                                 volatile narrowOop* p, oop v) {
 248   // heap oop is not pointer sized.
 249   OrderAccess::release_store(p, encode_heap_oop_not_null(v));
 250 }
 251 
 252 inline void oopDesc::release_encode_store_heap_oop_not_null(
 253                                                       volatile oop* p, oop v) {
 254   OrderAccess::release_store_ptr(p, v);
 255 }
 256 
 257 inline void oopDesc::release_encode_store_heap_oop(volatile oop* p,
 258                                                            oop v) {
 259   OrderAccess::release_store_ptr(p, v);
 260 }
 261 inline void oopDesc::release_encode_store_heap_oop(
 262                                                 volatile narrowOop* p, oop v) {
 263   OrderAccess::release_store(p, encode_heap_oop(v));
 264 }
 265 
 266 
 267 // These functions are only used to exchange oop fields in instances,
 268 // not headers.
 269 inline oop oopDesc::atomic_exchange_oop(oop exchange_value, volatile HeapWord *dest) {
 270   if (UseCompressedOops) {
 271     // encode exchange value from oop to T
 272     narrowOop val = encode_heap_oop(exchange_value);
 273     narrowOop old = (narrowOop)Atomic::xchg(val, (narrowOop*)dest);
 274     // decode old from T to oop
 275     return decode_heap_oop(old);
 276   } else {
 277     return (oop)Atomic::xchg_ptr(exchange_value, (oop*)dest);
 278   }
 279 }
 280 
 281 // In order to put or get a field out of an instance, must first check
 282 // if the field has been compressed and uncompress it.
 283 inline oop oopDesc::obj_field(int offset) const {
 284   return UseCompressedOops ?
 285     load_decode_heap_oop(obj_field_addr<narrowOop>(offset)) :
 286     load_decode_heap_oop(obj_field_addr<oop>(offset));
 287 }
 288 inline volatile oop oopDesc::obj_field_volatile(int offset) const {
 289   volatile oop value = obj_field(offset);
 290   OrderAccess::acquire();
 291   return value;
 292 }
 293 inline void oopDesc::obj_field_put(int offset, oop value) {
 294   UseCompressedOops ? oop_store(obj_field_addr<narrowOop>(offset), value) :
 295                       oop_store(obj_field_addr<oop>(offset),       value);
 296 }
 297 
 298 inline Metadata* oopDesc::metadata_field(int offset) const {
 299   return *metadata_field_addr(offset);
 300 }
 301 
 302 inline void oopDesc::metadata_field_put(int offset, Metadata* value) {
 303   *metadata_field_addr(offset) = value;
 304 }
 305 
 306 inline void oopDesc::obj_field_put_raw(int offset, oop value) {
 307   UseCompressedOops ?
 308     encode_store_heap_oop(obj_field_addr<narrowOop>(offset), value) :
 309     encode_store_heap_oop(obj_field_addr<oop>(offset),       value);
 310 }
 311 inline void oopDesc::obj_field_put_volatile(int offset, oop value) {
 312   OrderAccess::release();
 313   obj_field_put(offset, value);
 314   OrderAccess::fence();
 315 }
 316 
 317 inline jbyte oopDesc::byte_field(int offset) const                  { return (jbyte) *byte_field_addr(offset);    }
 318 inline void oopDesc::byte_field_put(int offset, jbyte contents)     { *byte_field_addr(offset) = (jint) contents; }
 319 
 320 inline jboolean oopDesc::bool_field(int offset) const               { return (jboolean) *bool_field_addr(offset); }
 321 inline void oopDesc::bool_field_put(int offset, jboolean contents)  { *bool_field_addr(offset) = (jint) contents; }
 322 
 323 inline jchar oopDesc::char_field(int offset) const                  { return (jchar) *char_field_addr(offset);    }
 324 inline void oopDesc::char_field_put(int offset, jchar contents)     { *char_field_addr(offset) = (jint) contents; }
 325 
 326 inline jint oopDesc::int_field(int offset) const                    { return *int_field_addr(offset);        }
 327 inline void oopDesc::int_field_put(int offset, jint contents)       { *int_field_addr(offset) = contents;    }
 328 
 329 inline jshort oopDesc::short_field(int offset) const                { return (jshort) *short_field_addr(offset);  }
 330 inline void oopDesc::short_field_put(int offset, jshort contents)   { *short_field_addr(offset) = (jint) contents;}
 331 
 332 inline jlong oopDesc::long_field(int offset) const                  { return *long_field_addr(offset);       }
 333 inline void oopDesc::long_field_put(int offset, jlong contents)     { *long_field_addr(offset) = contents;   }
 334 
 335 inline jfloat oopDesc::float_field(int offset) const                { return *float_field_addr(offset);      }
 336 inline void oopDesc::float_field_put(int offset, jfloat contents)   { *float_field_addr(offset) = contents;  }
 337 
 338 inline jdouble oopDesc::double_field(int offset) const              { return *double_field_addr(offset);     }
 339 inline void oopDesc::double_field_put(int offset, jdouble contents) { *double_field_addr(offset) = contents; }
 340 
 341 inline address oopDesc::address_field(int offset) const              { return *address_field_addr(offset);     }
 342 inline void oopDesc::address_field_put(int offset, address contents) { *address_field_addr(offset) = contents; }
 343 
 344 inline oop oopDesc::obj_field_acquire(int offset) const {
 345   return UseCompressedOops ?
 346              decode_heap_oop((narrowOop)
 347                OrderAccess::load_acquire(obj_field_addr<narrowOop>(offset)))
 348            : decode_heap_oop((oop)
 349                OrderAccess::load_ptr_acquire(obj_field_addr<oop>(offset)));
 350 }
 351 inline void oopDesc::release_obj_field_put(int offset, oop value) {
 352   UseCompressedOops ?
 353     oop_store((volatile narrowOop*)obj_field_addr<narrowOop>(offset), value) :
 354     oop_store((volatile oop*)      obj_field_addr<oop>(offset),       value);
 355 }
 356 
 357 inline jbyte oopDesc::byte_field_acquire(int offset) const                  { return OrderAccess::load_acquire(byte_field_addr(offset));     }
 358 inline void oopDesc::release_byte_field_put(int offset, jbyte contents)     { OrderAccess::release_store(byte_field_addr(offset), contents); }
 359 
 360 inline jboolean oopDesc::bool_field_acquire(int offset) const               { return OrderAccess::load_acquire(bool_field_addr(offset));     }
 361 inline void oopDesc::release_bool_field_put(int offset, jboolean contents)  { OrderAccess::release_store(bool_field_addr(offset), contents); }
 362 
 363 inline jchar oopDesc::char_field_acquire(int offset) const                  { return OrderAccess::load_acquire(char_field_addr(offset));     }
 364 inline void oopDesc::release_char_field_put(int offset, jchar contents)     { OrderAccess::release_store(char_field_addr(offset), contents); }
 365 
 366 inline jint oopDesc::int_field_acquire(int offset) const                    { return OrderAccess::load_acquire(int_field_addr(offset));      }
 367 inline void oopDesc::release_int_field_put(int offset, jint contents)       { OrderAccess::release_store(int_field_addr(offset), contents);  }
 368 
 369 inline jshort oopDesc::short_field_acquire(int offset) const                { return (jshort)OrderAccess::load_acquire(short_field_addr(offset)); }
 370 inline void oopDesc::release_short_field_put(int offset, jshort contents)   { OrderAccess::release_store(short_field_addr(offset), contents);     }
 371 
 372 inline jlong oopDesc::long_field_acquire(int offset) const                  { return OrderAccess::load_acquire(long_field_addr(offset));       }
 373 inline void oopDesc::release_long_field_put(int offset, jlong contents)     { OrderAccess::release_store(long_field_addr(offset), contents);   }
 374 
 375 inline jfloat oopDesc::float_field_acquire(int offset) const                { return OrderAccess::load_acquire(float_field_addr(offset));      }
 376 inline void oopDesc::release_float_field_put(int offset, jfloat contents)   { OrderAccess::release_store(float_field_addr(offset), contents);  }
 377 
 378 inline jdouble oopDesc::double_field_acquire(int offset) const              { return OrderAccess::load_acquire(double_field_addr(offset));     }
 379 inline void oopDesc::release_double_field_put(int offset, jdouble contents) { OrderAccess::release_store(double_field_addr(offset), contents); }
 380 
 381 inline address oopDesc::address_field_acquire(int offset) const             { return (address) OrderAccess::load_ptr_acquire(address_field_addr(offset)); }
 382 inline void oopDesc::release_address_field_put(int offset, address contents) { OrderAccess::release_store_ptr(address_field_addr(offset), contents); }
 383 
 384 inline int oopDesc::size_given_klass(Klass* klass)  {
 385   int lh = klass->layout_helper();
 386   int s;
 387 
 388   // lh is now a value computed at class initialization that may hint
 389   // at the size.  For instances, this is positive and equal to the
 390   // size.  For arrays, this is negative and provides log2 of the
 391   // array element size.  For other oops, it is zero and thus requires
 392   // a virtual call.
 393   //
 394   // We go to all this trouble because the size computation is at the
 395   // heart of phase 2 of mark-compaction, and called for every object,
 396   // alive or dead.  So the speed here is equal in importance to the
 397   // speed of allocation.
 398 
 399   if (lh > Klass::_lh_neutral_value) {
 400     if (!Klass::layout_helper_needs_slow_path(lh)) {
 401       s = lh >> LogHeapWordSize;  // deliver size scaled by wordSize
 402     } else {
 403       s = klass->oop_size(this);
 404     }
 405   } else if (lh <= Klass::_lh_neutral_value) {
 406     // The most common case is instances; fall through if so.
 407     if (lh < Klass::_lh_neutral_value) {
 408       // Second most common case is arrays.  We have to fetch the
 409       // length of the array, shift (multiply) it appropriately,
 410       // up to wordSize, add the header, and align to object size.
 411       size_t size_in_bytes;
 412 #ifdef _M_IA64
 413       // The Windows Itanium Aug 2002 SDK hoists this load above
 414       // the check for s < 0.  An oop at the end of the heap will
 415       // cause an access violation if this load is performed on a non
 416       // array oop.  Making the reference volatile prohibits this.
 417       // (%%% please explain by what magic the length is actually fetched!)
 418       volatile int *array_length;
 419       array_length = (volatile int *)( (intptr_t)this +
 420                           arrayOopDesc::length_offset_in_bytes() );
 421       assert(array_length > 0, "Integer arithmetic problem somewhere");
 422       // Put into size_t to avoid overflow.
 423       size_in_bytes = (size_t) array_length;
 424       size_in_bytes = size_in_bytes << Klass::layout_helper_log2_element_size(lh);
 425 #else
 426       size_t array_length = (size_t) ((arrayOop)this)->length();
 427       size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh);
 428 #endif
 429       size_in_bytes += Klass::layout_helper_header_size(lh);
 430 
 431       // This code could be simplified, but by keeping array_header_in_bytes
 432       // in units of bytes and doing it this way we can round up just once,
 433       // skipping the intermediate round to HeapWordSize.  Cast the result
 434       // of round_to to size_t to guarantee unsigned division == right shift.
 435       s = (int)((size_t)round_to(size_in_bytes, MinObjAlignmentInBytes) /
 436         HeapWordSize);
 437 
 438       // ParNew (used by CMS), UseParallelGC and UseG1GC can change the length field
 439       // of an "old copy" of an object array in the young gen so it indicates
 440       // the grey portion of an already copied array. This will cause the first
 441       // disjunct below to fail if the two comparands are computed across such
 442       // a concurrent change.
 443       // ParNew also runs with promotion labs (which look like int
 444       // filler arrays) which are subject to changing their declared size
 445       // when finally retiring a PLAB; this also can cause the first disjunct
 446       // to fail for another worker thread that is concurrently walking the block
 447       // offset table. Both these invariant failures are benign for their
 448       // current uses; we relax the assertion checking to cover these two cases below:
 449       //     is_objArray() && is_forwarded()   // covers first scenario above
 450       //  || is_typeArray()                    // covers second scenario above
 451       // If and when UseParallelGC uses the same obj array oop stealing/chunking
 452       // technique, we will need to suitably modify the assertion.
 453       assert((s == klass->oop_size(this)) ||
 454              (Universe::heap()->is_gc_active() &&
 455               ((is_typeArray() && UseConcMarkSweepGC) ||
 456                (is_objArray()  && is_forwarded() && (UseConcMarkSweepGC || UseParallelGC || UseG1GC)))),
 457              "wrong array object size");
 458     } else {
 459       // Must be zero, so bite the bullet and take the virtual call.
 460       s = klass->oop_size(this);
 461     }
 462   }
 463 
 464   assert(s % MinObjAlignment == 0, "alignment check");
 465   assert(s > 0, "Bad size calculated");
 466   return s;
 467 }
 468 
 469 
 470 inline int oopDesc::size()  {
 471   return size_given_klass(klass());
 472 }
 473 
 474 inline void update_barrier_set(void* p, oop v, bool release = false) {
 475   assert(oopDesc::bs() != NULL, "Uninitialized bs in oop!");
 476   oopDesc::bs()->write_ref_field(p, v, release);
 477 }
 478 
 479 template <class T> inline void update_barrier_set_pre(T* p, oop v) {
 480   oopDesc::bs()->write_ref_field_pre(p, v);
 481 }
 482 
 483 template <class T> inline void oop_store(T* p, oop v) {
 484   if (always_do_update_barrier) {
 485     oop_store((volatile T*)p, v);
 486   } else {
 487     update_barrier_set_pre(p, v);
 488     oopDesc::encode_store_heap_oop(p, v);
 489     // always_do_update_barrier == false =>
 490     // Either we are at a safepoint (in GC) or CMS is not used. In both
 491     // cases it's unnecessary to mark the card as dirty with release sematics.
 492     update_barrier_set((void*)p, v, false /* release */);  // cast away type
 493   }
 494 }
 495 
 496 template <class T> inline void oop_store(volatile T* p, oop v) {
 497   update_barrier_set_pre((T*)p, v);   // cast away volatile
 498   // Used by release_obj_field_put, so use release_store_ptr.
 499   oopDesc::release_encode_store_heap_oop(p, v);
 500   // When using CMS we must mark the card corresponding to p as dirty
 501   // with release sematics to prevent that CMS sees the dirty card but
 502   // not the new value v at p due to reordering of the two
 503   // stores. Note that CMS has a concurrent precleaning phase, where
 504   // it reads the card table while the Java threads are running.
 505   update_barrier_set((void*)p, v, true /* release */);    // cast away type
 506 }
 507 
 508 // Should replace *addr = oop assignments where addr type depends on UseCompressedOops
 509 // (without having to remember the function name this calls).
 510 inline void oop_store_raw(HeapWord* addr, oop value) {
 511   if (UseCompressedOops) {
 512     oopDesc::encode_store_heap_oop((narrowOop*)addr, value);
 513   } else {
 514     oopDesc::encode_store_heap_oop((oop*)addr, value);
 515   }
 516 }
 517 
 518 inline oop oopDesc::atomic_compare_exchange_oop(oop exchange_value,
 519                                                 volatile HeapWord *dest,
 520                                                 oop compare_value,
 521                                                 bool prebarrier) {
 522   if (UseCompressedOops) {
 523     if (prebarrier) {
 524       update_barrier_set_pre((narrowOop*)dest, exchange_value);
 525     }
 526     // encode exchange and compare value from oop to T
 527     narrowOop val = encode_heap_oop(exchange_value);
 528     narrowOop cmp = encode_heap_oop(compare_value);
 529 
 530     narrowOop old = (narrowOop) Atomic::cmpxchg(val, (narrowOop*)dest, cmp);
 531     // decode old from T to oop
 532     return decode_heap_oop(old);
 533   } else {
 534     if (prebarrier) {
 535       update_barrier_set_pre((oop*)dest, exchange_value);
 536     }
 537     return (oop)Atomic::cmpxchg_ptr(exchange_value, (oop*)dest, compare_value);
 538   }
 539 }
 540 
 541 // Used only for markSweep, scavenging
 542 inline bool oopDesc::is_gc_marked() const {
 543   assert(!CMSParallelFullGC, "CMSParallelFullGC should call parallel version.");
 544   return mark()->is_marked();
 545 }
 546 
 547 inline bool oopDesc::is_locked() const {
 548   return mark()->is_locked();
 549 }
 550 
 551 inline bool oopDesc::is_unlocked() const {
 552   return mark()->is_unlocked();
 553 }
 554 
 555 inline bool oopDesc::has_bias_pattern() const {
 556   return mark()->has_bias_pattern();
 557 }
 558 
 559 
 560 // used only for asserts
 561 inline bool oopDesc::is_oop(bool ignore_mark_word) const {
 562   oop obj = (oop) this;
 563   if (!check_obj_alignment(obj)) return false;
 564   if (!Universe::heap()->is_in_reserved(obj)) return false;
 565   // obj is aligned and accessible in heap
 566   if (Universe::heap()->is_in_reserved(obj->klass_or_null())) return false;
 567 
 568   // Header verification: the mark is typically non-NULL. If we're
 569   // at a safepoint, it must not be null.
 570   // Outside of a safepoint, the header could be changing (for example,
 571   // another thread could be inflating a lock on this object).
 572   if (ignore_mark_word) {
 573     return true;
 574   }
 575   if (mark() != NULL) {
 576     return true;
 577   }
 578   return !SafepointSynchronize::is_at_safepoint();
 579 }
 580 
 581 
 582 // used only for asserts
 583 inline bool oopDesc::is_oop_or_null(bool ignore_mark_word) const {
 584   return this == NULL ? true : is_oop(ignore_mark_word);
 585 }
 586 
 587 #ifndef PRODUCT
 588 // used only for asserts
 589 inline bool oopDesc::is_unlocked_oop() const {
 590   if (!Universe::heap()->is_in_reserved(this)) return false;
 591   return mark()->is_unlocked();
 592 }
 593 #endif // PRODUCT
 594 
 595 inline bool oopDesc::is_scavengable() const {
 596   return Universe::heap()->is_scavengable(this);
 597 }
 598 
 599 // Used by scavengers
 600 inline bool oopDesc::is_forwarded() const {
 601   // The extra heap check is needed since the obj might be locked, in which case the
 602   // mark would point to a stack location and have the sentinel bit cleared
 603   return mark()->is_marked();
 604 }
 605 
 606 // Used by scavengers
 607 inline void oopDesc::forward_to(oop p) {
 608   assert(check_obj_alignment(p),
 609          "forwarding to something not aligned");
 610   assert(Universe::heap()->is_in_reserved(p),
 611          "forwarding to something not in heap");
 612   markOop m = markOopDesc::encode_pointer_as_mark(p);
 613   assert(m->decode_pointer() == p, "encoding must be reversable");
 614   set_mark(m);
 615 }
 616 
 617 // Used by parallel scavengers
 618 inline bool oopDesc::cas_forward_to(oop p, markOop compare) {
 619   assert(check_obj_alignment(p),
 620          "forwarding to something not aligned");
 621   assert(Universe::heap()->is_in_reserved(p),
 622          "forwarding to something not in heap");
 623   markOop m = markOopDesc::encode_pointer_as_mark(p);
 624   assert(m->decode_pointer() == p, "encoding must be reversable");
 625   return cas_set_mark(m, compare) == compare;
 626 }
 627 
 628 #if INCLUDE_ALL_GCS
 629 inline oop oopDesc::forward_to_atomic(oop p) {
 630   markOop oldMark = mark();
 631   markOop forwardPtrMark = markOopDesc::encode_pointer_as_mark(p);
 632   markOop curMark;
 633 
 634   assert(forwardPtrMark->decode_pointer() == p, "encoding must be reversable");
 635   assert(sizeof(markOop) == sizeof(intptr_t), "CAS below requires this.");
 636 
 637   while (!oldMark->is_marked()) {
 638     curMark = (markOop)Atomic::cmpxchg_ptr(forwardPtrMark, &_mark, oldMark);
 639     assert(is_forwarded(), "object should have been forwarded");
 640     if (curMark == oldMark) {
 641       return NULL;
 642     }
 643     // If the CAS was unsuccessful then curMark->is_marked()
 644     // should return true as another thread has CAS'd in another
 645     // forwarding pointer.
 646     oldMark = curMark;
 647   }
 648   return forwardee();
 649 }
 650 #endif
 651 
 652 // Note that the forwardee is not the same thing as the displaced_mark.
 653 // The forwardee is used when copying during scavenge and mark-sweep.
 654 // It does need to clear the low two locking- and GC-related bits.
 655 inline oop oopDesc::forwardee() const {
 656   return (oop) mark()->decode_pointer();
 657 }
 658 
 659 inline bool oopDesc::has_displaced_mark() const {
 660   return mark()->has_displaced_mark_helper();
 661 }
 662 
 663 inline markOop oopDesc::displaced_mark() const {
 664   return mark()->displaced_mark_helper();
 665 }
 666 
 667 inline void oopDesc::set_displaced_mark(markOop m) {
 668   mark()->set_displaced_mark_helper(m);
 669 }
 670 
 671 // The following method needs to be MT safe.
 672 inline uint oopDesc::age() const {
 673   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
 674   if (has_displaced_mark()) {
 675     return displaced_mark()->age();
 676   } else {
 677     return mark()->age();
 678   }
 679 }
 680 
 681 inline void oopDesc::incr_age() {
 682   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
 683   if (has_displaced_mark()) {
 684     set_displaced_mark(displaced_mark()->incr_age());
 685   } else {
 686     set_mark(mark()->incr_age());
 687   }
 688 }
 689 
 690 
 691 inline intptr_t oopDesc::identity_hash() {
 692   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
 693   // Note: The mark must be read into local variable to avoid concurrent updates.
 694   markOop mrk = mark();
 695   if (mrk->is_unlocked() && !mrk->has_no_hash()) {
 696     return mrk->hash();
 697   } else if (mrk->is_marked()) {
 698     return mrk->hash();
 699   } else {
 700     return slow_identity_hash();
 701   }
 702 }
 703 
 704 inline void oopDesc::ms_follow_contents() {
 705 #ifdef ASSERT
 706   if (!CMSParallelFullGC) {
 707     assert (is_gc_marked(), "should be marked");
 708   } else {
 709     assert(MarkSweep::is_object_marked(this), "should be marked");
 710   }
 711 #endif
 712   klass()->oop_ms_follow_contents(this);
 713 }
 714 
 715 inline int oopDesc::ms_adjust_pointers() {
 716   debug_only(int check_size = size());
 717   int s = klass()->oop_ms_adjust_pointers(this);
 718   assert(s == check_size, "should be the same");
 719   return s;
 720 }
 721 
 722 #if INCLUDE_ALL_GCS
 723 inline void oopDesc::pc_follow_contents(ParCompactionManager* cm) {
 724   klass()->oop_pc_follow_contents(this, cm);
 725 }
 726 
 727 inline void oopDesc::pc_update_contents() {
 728   Klass* k = klass();
 729   if (!k->oop_is_typeArray()) {
 730     // It might contain oops beyond the header, so take the virtual call.
 731     k->oop_pc_update_pointers(this);
 732   }
 733   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 734 }
 735 
 736 inline void oopDesc::ps_push_contents(PSPromotionManager* pm) {
 737   Klass* k = klass();
 738   if (!k->oop_is_typeArray()) {
 739     // It might contain oops beyond the header, so take the virtual call.
 740     k->oop_ps_push_contents(this, pm);
 741   }
 742   // Else skip it.  The TypeArrayKlass in the header never needs scavenging.
 743 }
 744 #endif
 745 
 746 #define OOP_ITERATE_DEFN(OopClosureType, nv_suffix)                   \
 747                                                                       \
 748 inline int oopDesc::oop_iterate(OopClosureType* blk) {                \
 749   return klass()->oop_oop_iterate##nv_suffix(this, blk);              \
 750 }                                                                     \
 751                                                                       \
 752 inline int oopDesc::oop_iterate(OopClosureType* blk, MemRegion mr) {  \
 753   return klass()->oop_oop_iterate_bounded##nv_suffix(this, blk, mr);  \
 754 }
 755 
 756 
 757 inline int oopDesc::oop_iterate_no_header(OopClosure* blk) {
 758   // The NoHeaderExtendedOopClosure wraps the OopClosure and proxies all
 759   // the do_oop calls, but turns off all other features in ExtendedOopClosure.
 760   NoHeaderExtendedOopClosure cl(blk);
 761   return oop_iterate(&cl);
 762 }
 763 
 764 inline int oopDesc::oop_iterate_no_header(OopClosure* blk, MemRegion mr) {
 765   NoHeaderExtendedOopClosure cl(blk);
 766   return oop_iterate(&cl, mr);
 767 }
 768 
 769 #if INCLUDE_ALL_GCS
 770 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)       \
 771                                                                     \
 772 inline int oopDesc::oop_iterate_backwards(OopClosureType* blk) {    \
 773   return klass()->oop_oop_iterate_backwards##nv_suffix(this, blk);  \
 774 }
 775 #else
 776 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)
 777 #endif
 778 
 779 #define ALL_OOPDESC_OOP_ITERATE(OopClosureType, nv_suffix)  \
 780   OOP_ITERATE_DEFN(OopClosureType, nv_suffix)               \
 781   OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix)
 782 
 783 ALL_OOP_OOP_ITERATE_CLOSURES_1(ALL_OOPDESC_OOP_ITERATE)
 784 ALL_OOP_OOP_ITERATE_CLOSURES_2(ALL_OOPDESC_OOP_ITERATE)
 785 
 786 #endif // SHARE_VM_OOPS_OOP_INLINE_HPP