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