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