1 /* 2 * Copyright (c) 1997, 2018, 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/collectedHeap.hpp" 30 #include "gc/shared/generation.hpp" 31 #include "oops/access.inline.hpp" 32 #include "oops/arrayKlass.hpp" 33 #include "oops/arrayOop.hpp" 34 #include "oops/compressedOops.inline.hpp" 35 #include "oops/klass.inline.hpp" 36 #include "oops/markOop.inline.hpp" 37 #include "oops/oop.hpp" 38 #include "runtime/atomic.hpp" 39 #include "runtime/orderAccess.inline.hpp" 40 #include "runtime/os.hpp" 41 #include "utilities/align.hpp" 42 #include "utilities/macros.hpp" 43 44 // Implementation of all inlined member functions defined in oop.hpp 45 // We need a separate file to avoid circular references 46 47 void oopDesc::release_set_mark(markOop m) { 48 OrderAccess::release_store(&_mark, m); 49 } 50 51 markOop oopDesc::cas_set_mark(markOop new_mark, markOop old_mark) { 52 return Atomic::cmpxchg(new_mark, &_mark, old_mark); 53 } 54 55 void oopDesc::init_mark() { 56 set_mark(markOopDesc::prototype_for_object(this)); 57 } 58 59 Klass* oopDesc::klass() const { 60 if (UseCompressedClassPointers) { 61 return Klass::decode_klass_not_null(_metadata._compressed_klass); 62 } else { 63 return _metadata._klass; 64 } 65 } 66 67 Klass* oopDesc::klass_or_null() const volatile { 68 if (UseCompressedClassPointers) { 69 return Klass::decode_klass(_metadata._compressed_klass); 70 } else { 71 return _metadata._klass; 72 } 73 } 74 75 Klass* oopDesc::klass_or_null_acquire() const volatile { 76 if (UseCompressedClassPointers) { 77 // Workaround for non-const load_acquire parameter. 78 const volatile narrowKlass* addr = &_metadata._compressed_klass; 79 volatile narrowKlass* xaddr = const_cast<volatile narrowKlass*>(addr); 80 return Klass::decode_klass(OrderAccess::load_acquire(xaddr)); 81 } else { 82 return OrderAccess::load_acquire(&_metadata._klass); 83 } 84 } 85 86 Klass** oopDesc::klass_addr() { 87 // Only used internally and with CMS and will not work with 88 // UseCompressedOops 89 assert(!UseCompressedClassPointers, "only supported with uncompressed klass pointers"); 90 return (Klass**) &_metadata._klass; 91 } 92 93 narrowKlass* oopDesc::compressed_klass_addr() { 94 assert(UseCompressedClassPointers, "only called by compressed klass pointers"); 95 return &_metadata._compressed_klass; 96 } 97 98 #define CHECK_SET_KLASS(k) \ 99 do { \ 100 assert(Universe::is_bootstrapping() || k != NULL, "NULL Klass"); \ 101 assert(Universe::is_bootstrapping() || k->is_klass(), "not a Klass"); \ 102 } while (0) 103 104 void oopDesc::set_klass(Klass* k) { 105 CHECK_SET_KLASS(k); 106 if (UseCompressedClassPointers) { 107 *compressed_klass_addr() = Klass::encode_klass_not_null(k); 108 } else { 109 *klass_addr() = k; 110 } 111 } 112 113 void oopDesc::release_set_klass(Klass* k) { 114 CHECK_SET_KLASS(k); 115 if (UseCompressedClassPointers) { 116 OrderAccess::release_store(compressed_klass_addr(), 117 Klass::encode_klass_not_null(k)); 118 } else { 119 OrderAccess::release_store(klass_addr(), k); 120 } 121 } 122 123 #undef CHECK_SET_KLASS 124 125 int oopDesc::klass_gap() const { 126 return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes()); 127 } 128 129 void oopDesc::set_klass_gap(int v) { 130 if (UseCompressedClassPointers) { 131 *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes()) = v; 132 } 133 } 134 135 void oopDesc::set_klass_to_list_ptr(oop k) { 136 // This is only to be used during GC, for from-space objects, so no 137 // barrier is needed. 138 if (UseCompressedClassPointers) { 139 _metadata._compressed_klass = (narrowKlass)CompressedOops::encode(k); // may be null (parnew overflow handling) 140 } else { 141 _metadata._klass = (Klass*)(address)k; 142 } 143 } 144 145 oop oopDesc::list_ptr_from_klass() { 146 // This is only to be used during GC, for from-space objects. 147 if (UseCompressedClassPointers) { 148 return CompressedOops::decode((narrowOop)_metadata._compressed_klass); 149 } else { 150 // Special case for GC 151 return (oop)(address)_metadata._klass; 152 } 153 } 154 155 bool oopDesc::is_a(Klass* k) const { 156 return klass()->is_subtype_of(k); 157 } 158 159 int oopDesc::size() { 160 return size_given_klass(klass()); 161 } 162 163 int oopDesc::size_given_klass(Klass* klass) { 164 int lh = klass->layout_helper(); 165 int s; 166 167 // lh is now a value computed at class initialization that may hint 168 // at the size. For instances, this is positive and equal to the 169 // size. For arrays, this is negative and provides log2 of the 170 // array element size. For other oops, it is zero and thus requires 171 // a virtual call. 172 // 173 // We go to all this trouble because the size computation is at the 174 // heart of phase 2 of mark-compaction, and called for every object, 175 // alive or dead. So the speed here is equal in importance to the 176 // speed of allocation. 177 178 if (lh > Klass::_lh_neutral_value) { 179 if (!Klass::layout_helper_needs_slow_path(lh)) { 180 s = lh >> LogHeapWordSize; // deliver size scaled by wordSize 181 } else { 182 s = klass->oop_size(this); 183 } 184 } else if (lh <= Klass::_lh_neutral_value) { 185 // The most common case is instances; fall through if so. 186 if (lh < Klass::_lh_neutral_value) { 187 // Second most common case is arrays. We have to fetch the 188 // length of the array, shift (multiply) it appropriately, 189 // up to wordSize, add the header, and align to object size. 190 size_t size_in_bytes; 191 size_t array_length = (size_t) ((arrayOop)this)->length(); 192 size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh); 193 size_in_bytes += Klass::layout_helper_header_size(lh); 194 195 // This code could be simplified, but by keeping array_header_in_bytes 196 // in units of bytes and doing it this way we can round up just once, 197 // skipping the intermediate round to HeapWordSize. 198 s = (int)(align_up(size_in_bytes, MinObjAlignmentInBytes) / HeapWordSize); 199 200 // ParNew (used by CMS), UseParallelGC and UseG1GC can change the length field 201 // of an "old copy" of an object array in the young gen so it indicates 202 // the grey portion of an already copied array. This will cause the first 203 // disjunct below to fail if the two comparands are computed across such 204 // a concurrent change. 205 // ParNew also runs with promotion labs (which look like int 206 // filler arrays) which are subject to changing their declared size 207 // when finally retiring a PLAB; this also can cause the first disjunct 208 // to fail for another worker thread that is concurrently walking the block 209 // offset table. Both these invariant failures are benign for their 210 // current uses; we relax the assertion checking to cover these two cases below: 211 // is_objArray() && is_forwarded() // covers first scenario above 212 // || is_typeArray() // covers second scenario above 213 // If and when UseParallelGC uses the same obj array oop stealing/chunking 214 // technique, we will need to suitably modify the assertion. 215 assert((s == klass->oop_size(this)) || 216 (Universe::heap()->is_gc_active() && 217 ((is_typeArray() && UseConcMarkSweepGC) || 218 (is_objArray() && is_forwarded() && (UseConcMarkSweepGC || UseParallelGC || UseG1GC)))), 219 "wrong array object size"); 220 } else { 221 // Must be zero, so bite the bullet and take the virtual call. 222 s = klass->oop_size(this); 223 } 224 } 225 226 assert(s > 0, "Oop size must be greater than zero, not %d", s); 227 assert(is_object_aligned(s), "Oop size is not properly aligned: %d", s); 228 return s; 229 } 230 231 bool oopDesc::is_instance() const { return klass()->is_instance_klass(); } 232 bool oopDesc::is_array() const { return klass()->is_array_klass(); } 233 bool oopDesc::is_objArray() const { return klass()->is_objArray_klass(); } 234 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); } 235 236 void* oopDesc::field_addr_raw(int offset) const { return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + offset); } 237 void* oopDesc::field_addr(int offset) const { return Access<>::resolve(as_oop())->field_addr_raw(offset); } 238 239 template <class T> 240 T* oopDesc::obj_field_addr_raw(int offset) const { return (T*) field_addr_raw(offset); } 241 242 template <DecoratorSet decorators> 243 inline oop oopDesc::obj_field_access(int offset) const { return HeapAccess<decorators>::oop_load_at(as_oop(), offset); } 244 inline oop oopDesc::obj_field(int offset) const { return HeapAccess<>::oop_load_at(as_oop(), offset); } 245 246 inline void oopDesc::obj_field_put(int offset, oop value) { HeapAccess<>::oop_store_at(as_oop(), offset, value); } 247 248 inline jbyte oopDesc::byte_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 249 inline void oopDesc::byte_field_put(int offset, jbyte value) { HeapAccess<>::store_at(as_oop(), offset, value); } 250 251 inline jchar oopDesc::char_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 252 inline void oopDesc::char_field_put(int offset, jchar value) { HeapAccess<>::store_at(as_oop(), offset, value); } 253 254 inline jboolean oopDesc::bool_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 255 inline void oopDesc::bool_field_put(int offset, jboolean value) { HeapAccess<>::store_at(as_oop(), offset, jboolean(value & 1)); } 256 257 inline jshort oopDesc::short_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 258 inline void oopDesc::short_field_put(int offset, jshort value) { HeapAccess<>::store_at(as_oop(), offset, value); } 259 260 inline jint oopDesc::int_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 261 inline void oopDesc::int_field_put(int offset, jint value) { HeapAccess<>::store_at(as_oop(), offset, value); } 262 263 inline jlong oopDesc::long_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 264 inline void oopDesc::long_field_put(int offset, jlong value) { HeapAccess<>::store_at(as_oop(), offset, value); } 265 266 inline jfloat oopDesc::float_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 267 inline void oopDesc::float_field_put(int offset, jfloat value) { HeapAccess<>::store_at(as_oop(), offset, value); } 268 269 inline jdouble oopDesc::double_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 270 inline void oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); } 271 272 bool oopDesc::is_locked() const { 273 return mark()->is_locked(); 274 } 275 276 bool oopDesc::is_unlocked() const { 277 return mark()->is_unlocked(); 278 } 279 280 bool oopDesc::has_bias_pattern() const { 281 return mark()->has_bias_pattern(); 282 } 283 284 // Used only for markSweep, scavenging 285 bool oopDesc::is_gc_marked() const { 286 return mark()->is_marked(); 287 } 288 289 // Used by scavengers 290 bool oopDesc::is_forwarded() const { 291 // The extra heap check is needed since the obj might be locked, in which case the 292 // mark would point to a stack location and have the sentinel bit cleared 293 return mark()->is_marked(); 294 } 295 296 // Used by scavengers 297 void oopDesc::forward_to(oop p) { 298 assert(check_obj_alignment(p), 299 "forwarding to something not aligned"); 300 assert(Universe::heap()->is_in_reserved(p), 301 "forwarding to something not in heap"); 302 assert(!is_archive_object(oop(this)) && 303 !is_archive_object(p), 304 "forwarding archive object"); 305 markOop m = markOopDesc::encode_pointer_as_mark(p); 306 assert(m->decode_pointer() == p, "encoding must be reversable"); 307 set_mark(m); 308 } 309 310 // Used by parallel scavengers 311 bool oopDesc::cas_forward_to(oop p, markOop compare) { 312 assert(check_obj_alignment(p), 313 "forwarding to something not aligned"); 314 assert(Universe::heap()->is_in_reserved(p), 315 "forwarding to something not in heap"); 316 markOop m = markOopDesc::encode_pointer_as_mark(p); 317 assert(m->decode_pointer() == p, "encoding must be reversable"); 318 return cas_set_mark(m, compare) == compare; 319 } 320 321 #if INCLUDE_ALL_GCS 322 oop oopDesc::forward_to_atomic(oop p) { 323 markOop oldMark = mark(); 324 markOop forwardPtrMark = markOopDesc::encode_pointer_as_mark(p); 325 markOop curMark; 326 327 assert(forwardPtrMark->decode_pointer() == p, "encoding must be reversable"); 328 assert(sizeof(markOop) == sizeof(intptr_t), "CAS below requires this."); 329 330 while (!oldMark->is_marked()) { 331 curMark = Atomic::cmpxchg(forwardPtrMark, &_mark, oldMark); 332 assert(is_forwarded(), "object should have been forwarded"); 333 if (curMark == oldMark) { 334 return NULL; 335 } 336 // If the CAS was unsuccessful then curMark->is_marked() 337 // should return true as another thread has CAS'd in another 338 // forwarding pointer. 339 oldMark = curMark; 340 } 341 return forwardee(); 342 } 343 #endif 344 345 // Note that the forwardee is not the same thing as the displaced_mark. 346 // The forwardee is used when copying during scavenge and mark-sweep. 347 // It does need to clear the low two locking- and GC-related bits. 348 oop oopDesc::forwardee() const { 349 return (oop) mark()->decode_pointer(); 350 } 351 352 // The following method needs to be MT safe. 353 uint oopDesc::age() const { 354 assert(!is_forwarded(), "Attempt to read age from forwarded mark"); 355 if (has_displaced_mark()) { 356 return displaced_mark()->age(); 357 } else { 358 return mark()->age(); 359 } 360 } 361 362 void oopDesc::incr_age() { 363 assert(!is_forwarded(), "Attempt to increment age of forwarded mark"); 364 if (has_displaced_mark()) { 365 set_displaced_mark(displaced_mark()->incr_age()); 366 } else { 367 set_mark(mark()->incr_age()); 368 } 369 } 370 371 #if INCLUDE_ALL_GCS 372 void oopDesc::pc_follow_contents(ParCompactionManager* cm) { 373 klass()->oop_pc_follow_contents(this, cm); 374 } 375 376 void oopDesc::pc_update_contents(ParCompactionManager* cm) { 377 Klass* k = klass(); 378 if (!k->is_typeArray_klass()) { 379 // It might contain oops beyond the header, so take the virtual call. 380 k->oop_pc_update_pointers(this, cm); 381 } 382 // Else skip it. The TypeArrayKlass in the header never needs scavenging. 383 } 384 385 void oopDesc::ps_push_contents(PSPromotionManager* pm) { 386 Klass* k = klass(); 387 if (!k->is_typeArray_klass()) { 388 // It might contain oops beyond the header, so take the virtual call. 389 k->oop_ps_push_contents(this, pm); 390 } 391 // Else skip it. The TypeArrayKlass in the header never needs scavenging. 392 } 393 #endif // INCLUDE_ALL_GCS 394 395 #define OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ 396 \ 397 void oopDesc::oop_iterate(OopClosureType* blk) { \ 398 klass()->oop_oop_iterate##nv_suffix(this, blk); \ 399 } \ 400 \ 401 void oopDesc::oop_iterate(OopClosureType* blk, MemRegion mr) { \ 402 klass()->oop_oop_iterate_bounded##nv_suffix(this, blk, mr); \ 403 } 404 405 #define OOP_ITERATE_SIZE_DEFN(OopClosureType, nv_suffix) \ 406 \ 407 int oopDesc::oop_iterate_size(OopClosureType* blk) { \ 408 Klass* k = klass(); \ 409 int size = size_given_klass(k); \ 410 k->oop_oop_iterate##nv_suffix(this, blk); \ 411 return size; \ 412 } \ 413 \ 414 int oopDesc::oop_iterate_size(OopClosureType* blk, MemRegion mr) { \ 415 Klass* k = klass(); \ 416 int size = size_given_klass(k); \ 417 k->oop_oop_iterate_bounded##nv_suffix(this, blk, mr); \ 418 return size; \ 419 } 420 421 int oopDesc::oop_iterate_no_header(OopClosure* blk) { 422 // The NoHeaderExtendedOopClosure wraps the OopClosure and proxies all 423 // the do_oop calls, but turns off all other features in ExtendedOopClosure. 424 NoHeaderExtendedOopClosure cl(blk); 425 return oop_iterate_size(&cl); 426 } 427 428 int oopDesc::oop_iterate_no_header(OopClosure* blk, MemRegion mr) { 429 NoHeaderExtendedOopClosure cl(blk); 430 return oop_iterate_size(&cl, mr); 431 } 432 433 #if INCLUDE_ALL_GCS 434 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \ 435 \ 436 inline void oopDesc::oop_iterate_backwards(OopClosureType* blk) { \ 437 klass()->oop_oop_iterate_backwards##nv_suffix(this, blk); \ 438 } 439 #else 440 #define OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) 441 #endif // INCLUDE_ALL_GCS 442 443 #define ALL_OOPDESC_OOP_ITERATE(OopClosureType, nv_suffix) \ 444 OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ 445 OOP_ITERATE_SIZE_DEFN(OopClosureType, nv_suffix) \ 446 OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) 447 448 ALL_OOP_OOP_ITERATE_CLOSURES_1(ALL_OOPDESC_OOP_ITERATE) 449 ALL_OOP_OOP_ITERATE_CLOSURES_2(ALL_OOPDESC_OOP_ITERATE) 450 451 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) { 452 return obj == NULL || obj->klass()->is_subtype_of(klass); 453 } 454 455 intptr_t oopDesc::identity_hash() { 456 // Fast case; if the object is unlocked and the hash value is set, no locking is needed 457 // Note: The mark must be read into local variable to avoid concurrent updates. 458 markOop mrk = mark(); 459 if (mrk->is_unlocked() && !mrk->has_no_hash()) { 460 return mrk->hash(); 461 } else if (mrk->is_marked()) { 462 return mrk->hash(); 463 } else { 464 return slow_identity_hash(); 465 } 466 } 467 468 bool oopDesc::has_displaced_mark() const { 469 return mark()->has_displaced_mark_helper(); 470 } 471 472 markOop oopDesc::displaced_mark() const { 473 return mark()->displaced_mark_helper(); 474 } 475 476 void oopDesc::set_displaced_mark(markOop m) { 477 mark()->set_displaced_mark_helper(m); 478 } 479 480 #endif // SHARE_VM_OOPS_OOP_INLINE_HPP