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