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