1 /* 2 * Copyright (c) 1997, 2021, 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_OOPS_OOP_INLINE_HPP 26 #define SHARE_OOPS_OOP_INLINE_HPP 27 28 #include "oops/oop.hpp" 29 30 #include "memory/universe.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/markWord.inline.hpp" 36 #include "oops/oopsHierarchy.hpp" 37 #include "runtime/atomic.hpp" 38 #include "runtime/globals.hpp" 39 #include "runtime/objectMonitor.inline.hpp" 40 #include "utilities/align.hpp" 41 #include "utilities/debug.hpp" 42 #include "utilities/macros.hpp" 43 #include "utilities/globalDefinitions.hpp" 44 45 // Implementation of all inlined member functions defined in oop.hpp 46 // We need a separate file to avoid circular references 47 48 markWord oopDesc::mark() const { 49 uintptr_t v = HeapAccess<MO_RELAXED>::load_at(as_oop(), mark_offset_in_bytes()); 50 return markWord(v); 51 } 52 53 markWord oopDesc::mark_acquire() const { 54 return Atomic::load_acquire(&_mark); 55 } 56 markWord* oopDesc::mark_addr() const { 57 return (markWord*) &_mark; 58 } 59 60 void oopDesc::set_mark(markWord m) { 61 HeapAccess<MO_RELAXED>::store_at(as_oop(), mark_offset_in_bytes(), m.value()); 62 } 63 64 void oopDesc::set_mark(HeapWord* mem, markWord m) { 65 *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m; 66 } 67 68 void oopDesc::release_set_mark(markWord m) { 69 HeapAccess<MO_RELEASE>::store_at(as_oop(), mark_offset_in_bytes(), m.value()); 70 } 71 72 void oopDesc::release_set_mark(HeapWord* mem, markWord m) { 73 Atomic::release_store((markWord*)(((char*)mem) + mark_offset_in_bytes()), m); 74 } 75 76 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) { 77 uintptr_t v = HeapAccess<>::atomic_cmpxchg_at(as_oop(), mark_offset_in_bytes(), old_mark.value(), new_mark.value()); 78 return markWord(v); 79 } 80 81 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) { 82 return Atomic::cmpxchg(&_mark, old_mark, new_mark, order); 83 } 84 85 markWord oopDesc::resolve_mark() const { 86 assert(UseFastLocking, "Only safe with fast-locking"); 87 markWord hdr = mark(); 88 if (hdr.has_displaced_mark_helper()) { 89 hdr = hdr.displaced_mark_helper(); 90 } 91 return hdr; 92 } 93 94 void oopDesc::init_mark() { 95 #ifdef _LP64 96 if (UseCompactObjectHeaders) { 97 markWord header = resolve_mark(); 98 assert(UseCompressedClassPointers, "expect compressed klass pointers"); 99 set_mark(markWord((header.value() & markWord::klass_mask_in_place) | markWord::prototype().value())); 100 } else 101 #endif 102 set_mark(markWord::prototype()); 103 } 104 105 Klass* oopDesc::klass() const { 106 #ifdef _LP64 107 if (UseCompactObjectHeaders) { 108 assert(UseCompressedClassPointers, "only with compressed class pointers"); 109 markWord header = resolve_mark(); 110 return header.klass(); 111 } else if (UseCompressedClassPointers) { 112 return CompressedKlassPointers::decode_not_null(_metadata._compressed_klass); 113 } else 114 #endif 115 return _metadata._klass; 116 } 117 118 Klass* oopDesc::klass_or_null() const { 119 #ifdef _LP64 120 if (UseCompactObjectHeaders) { 121 assert(UseCompressedClassPointers, "only with compressed class pointers"); 122 markWord header = resolve_mark(); 123 return header.klass_or_null(); 124 } else if (UseCompressedClassPointers) { 125 return CompressedKlassPointers::decode(_metadata._compressed_klass); 126 } else 127 #endif 128 return _metadata._klass; 129 } 130 131 Klass* oopDesc::klass_or_null_acquire() const { 132 #ifdef _LP64 133 if (UseCompactObjectHeaders) { 134 assert(UseCompressedClassPointers, "only with compressed class pointers"); 135 markWord header = mark_acquire(); 136 if (header.has_monitor()) { 137 header = header.monitor()->header(); 138 } 139 return header.klass_or_null(); 140 } else if (UseCompressedClassPointers) { 141 narrowKlass nklass = Atomic::load_acquire(&_metadata._compressed_klass); 142 return CompressedKlassPointers::decode(nklass); 143 } else 144 #endif 145 return Atomic::load_acquire(&_metadata._klass); 146 } 147 148 void oopDesc::set_klass(Klass* k) { 149 assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass"); 150 assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers"); 151 if (UseCompressedClassPointers) { 152 _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k); 153 } else { 154 _metadata._klass = k; 155 } 156 } 157 158 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) { 159 assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass"); 160 assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers"); 161 char* raw_mem = ((char*)mem + klass_offset_in_bytes()); 162 if (UseCompressedClassPointers) { 163 Atomic::release_store((narrowKlass*)raw_mem, 164 CompressedKlassPointers::encode_not_null(k)); 165 } else { 166 Atomic::release_store((Klass**)raw_mem, k); 167 } 168 } 169 170 void oopDesc::set_klass_gap(HeapWord* mem, int v) { 171 assert(!UseCompactObjectHeaders, "don't set Klass* gap with compact headers"); 172 if (UseCompressedClassPointers) { 173 *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v; 174 } 175 } 176 177 bool oopDesc::is_a(Klass* k) const { 178 return klass()->is_subtype_of(k); 179 } 180 181 int oopDesc::size() { 182 return size_given_klass(klass()); 183 } 184 185 int oopDesc::size_given_klass(Klass* klass) { 186 int lh = klass->layout_helper(); 187 int s; 188 189 // lh is now a value computed at class initialization that may hint 190 // at the size. For instances, this is positive and equal to the 191 // size. For arrays, this is negative and provides log2 of the 192 // array element size. For other oops, it is zero and thus requires 193 // a virtual call. 194 // 195 // We go to all this trouble because the size computation is at the 196 // heart of phase 2 of mark-compaction, and called for every object, 197 // alive or dead. So the speed here is equal in importance to the 198 // speed of allocation. 199 200 if (lh > Klass::_lh_neutral_value) { 201 if (!Klass::layout_helper_needs_slow_path(lh)) { 202 s = lh >> LogHeapWordSize; // deliver size scaled by wordSize 203 } else { 204 s = klass->oop_size(this); 205 } 206 } else if (lh <= Klass::_lh_neutral_value) { 207 // The most common case is instances; fall through if so. 208 if (lh < Klass::_lh_neutral_value) { 209 // Second most common case is arrays. We have to fetch the 210 // length of the array, shift (multiply) it appropriately, 211 // up to wordSize, add the header, and align to object size. 212 size_t size_in_bytes; 213 size_t array_length = (size_t) ((arrayOop)this)->length(); 214 size_in_bytes = array_length << Klass::layout_helper_log2_element_size(lh); 215 size_in_bytes += Klass::layout_helper_header_size(lh); 216 217 // This code could be simplified, but by keeping array_header_in_bytes 218 // in units of bytes and doing it this way we can round up just once, 219 // skipping the intermediate round to HeapWordSize. 220 s = (int)(align_up(size_in_bytes, MinObjAlignmentInBytes) / HeapWordSize); 221 222 // UseParallelGC and UseG1GC can change the length field 223 // of an "old copy" of an object array in the young gen so it indicates 224 // the grey portion of an already copied array. This will cause the first 225 // disjunct below to fail if the two comparands are computed across such 226 // a concurrent change. 227 assert((s == klass->oop_size(this)) || 228 (Universe::is_gc_active() && is_objArray() && is_forwarded() && (get_UseParallelGC() || get_UseG1GC())), 229 "wrong array object size"); 230 } else { 231 // Must be zero, so bite the bullet and take the virtual call. 232 s = klass->oop_size(this); 233 } 234 } 235 236 assert(s > 0, "Oop size must be greater than zero, not %d", s); 237 assert(is_object_aligned(s), "Oop size is not properly aligned: %d", s); 238 return s; 239 } 240 241 bool oopDesc::is_instance() const { return klass()->is_instance_klass(); } 242 bool oopDesc::is_array() const { return klass()->is_array_klass(); } 243 bool oopDesc::is_objArray() const { return klass()->is_objArray_klass(); } 244 bool oopDesc::is_typeArray() const { return klass()->is_typeArray_klass(); } 245 246 void* oopDesc::field_addr(int offset) const { return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + offset); } 247 248 template <class T> 249 T* oopDesc::obj_field_addr(int offset) const { return (T*) field_addr(offset); } 250 251 template <typename T> 252 size_t oopDesc::field_offset(T* p) const { return pointer_delta((void*)p, (void*)this, 1); } 253 254 template <DecoratorSet decorators> 255 inline oop oopDesc::obj_field_access(int offset) const { return HeapAccess<decorators>::oop_load_at(as_oop(), offset); } 256 inline oop oopDesc::obj_field(int offset) const { return HeapAccess<>::oop_load_at(as_oop(), offset); } 257 258 inline void oopDesc::obj_field_put(int offset, oop value) { HeapAccess<>::oop_store_at(as_oop(), offset, value); } 259 260 inline jbyte oopDesc::byte_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 261 inline void oopDesc::byte_field_put(int offset, jbyte value) { HeapAccess<>::store_at(as_oop(), offset, value); } 262 263 inline jchar oopDesc::char_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 264 inline void oopDesc::char_field_put(int offset, jchar value) { HeapAccess<>::store_at(as_oop(), offset, value); } 265 266 inline jboolean oopDesc::bool_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 267 inline void oopDesc::bool_field_put(int offset, jboolean value) { HeapAccess<>::store_at(as_oop(), offset, jboolean(value & 1)); } 268 inline jboolean oopDesc::bool_field_volatile(int offset) const { return HeapAccess<MO_SEQ_CST>::load_at(as_oop(), offset); } 269 inline void oopDesc::bool_field_put_volatile(int offset, jboolean value) { HeapAccess<MO_SEQ_CST>::store_at(as_oop(), offset, jboolean(value & 1)); } 270 inline jshort oopDesc::short_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 271 inline void oopDesc::short_field_put(int offset, jshort value) { HeapAccess<>::store_at(as_oop(), offset, value); } 272 273 inline jint oopDesc::int_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 274 inline jint oopDesc::int_field_raw(int offset) const { return RawAccess<>::load_at(as_oop(), offset); } 275 inline void oopDesc::int_field_put(int offset, jint value) { HeapAccess<>::store_at(as_oop(), offset, value); } 276 277 inline jlong oopDesc::long_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 278 inline void oopDesc::long_field_put(int offset, jlong value) { HeapAccess<>::store_at(as_oop(), offset, value); } 279 280 inline jfloat oopDesc::float_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 281 inline void oopDesc::float_field_put(int offset, jfloat value) { HeapAccess<>::store_at(as_oop(), offset, value); } 282 283 inline jdouble oopDesc::double_field(int offset) const { return HeapAccess<>::load_at(as_oop(), offset); } 284 inline void oopDesc::double_field_put(int offset, jdouble value) { HeapAccess<>::store_at(as_oop(), offset, value); } 285 286 bool oopDesc::is_locked() const { 287 return mark().is_locked(); 288 } 289 290 bool oopDesc::is_unlocked() const { 291 return mark().is_unlocked(); 292 } 293 294 bool oopDesc::has_bias_pattern() const { 295 return mark().has_bias_pattern(); 296 } 297 298 // Used only for markSweep, scavenging 299 bool oopDesc::is_gc_marked() const { 300 return mark().is_marked(); 301 } 302 303 // Used by scavengers 304 bool oopDesc::is_forwarded() const { 305 // The extra heap check is needed since the obj might be locked, in which case the 306 // mark would point to a stack location and have the sentinel bit cleared 307 return mark().is_marked(); 308 } 309 310 // Used by scavengers 311 void oopDesc::forward_to(oop p) { 312 verify_forwardee(p); 313 markWord m = markWord::encode_pointer_as_mark(p); 314 assert(forwardee(m) == p, "encoding must be reversable"); 315 set_mark(m); 316 } 317 318 // Used by parallel scavengers 319 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) { 320 verify_forwardee(p); 321 markWord m = markWord::encode_pointer_as_mark(p); 322 assert(m.decode_pointer() == p, "encoding must be reversable"); 323 return cas_set_mark(m, compare, order) == compare; 324 } 325 326 void oopDesc::forward_to_self() { 327 #ifdef _LP64 328 verify_forwardee(this); 329 markWord m = mark(); 330 // If mark is displaced, we need to preserve the Klass* from real header. 331 assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint"); 332 if (m.has_displaced_mark_helper()) { 333 m = m.displaced_mark_helper(); 334 } 335 m = m.set_self_forwarded(); 336 assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable"); 337 set_mark(m); 338 #else 339 forward_to(oop(this)); 340 #endif 341 } 342 343 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) { 344 verify_forwardee(p); 345 markWord m = markWord::encode_pointer_as_mark(p); 346 assert(forwardee(m) == p, "encoding must be reversable"); 347 markWord old_mark = cas_set_mark(m, compare, order); 348 if (old_mark == compare) { 349 return NULL; 350 } else { 351 return forwardee(old_mark); 352 } 353 } 354 355 oop oopDesc::forward_to_self_atomic(markWord compare, atomic_memory_order order) { 356 #ifdef _LP64 357 verify_forwardee(this); 358 markWord m = compare; 359 // If mark is displaced, we need to preserve the Klass* from real header. 360 assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint"); 361 if (m.has_displaced_mark_helper()) { 362 m = m.displaced_mark_helper(); 363 } 364 m = m.set_self_forwarded(); 365 assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable"); 366 markWord old_mark = cas_set_mark(m, compare, order); 367 if (old_mark == compare) { 368 return NULL; 369 } else { 370 assert(old_mark.is_marked(), "must be marked here"); 371 return forwardee(old_mark); 372 } 373 #else 374 return forward_to_atomic(oop(this), compare, order); 375 #endif 376 } 377 378 // Note that the forwardee is not the same thing as the displaced_mark. 379 // The forwardee is used when copying during scavenge and mark-sweep. 380 // It does need to clear the low two locking- and GC-related bits. 381 oop oopDesc::forwardee() const { 382 return forwardee(mark()); 383 } 384 385 oop oopDesc::forwardee(markWord header) const { 386 assert(header.is_marked(), "must be forwarded"); 387 #ifdef _LP64 388 if (header.self_forwarded()) { 389 return cast_to_oop(this); 390 } else 391 #endif 392 { 393 assert(header.is_marked(), "only decode when actually forwarded"); 394 return cast_to_oop(header.decode_pointer()); 395 } 396 } 397 398 // The following method needs to be MT safe. 399 uint oopDesc::age() const { 400 assert(!is_forwarded(), "Attempt to read age from forwarded mark"); 401 if (has_displaced_mark()) { 402 return displaced_mark().age(); 403 } else { 404 return mark().age(); 405 } 406 } 407 408 void oopDesc::incr_age() { 409 assert(!is_forwarded(), "Attempt to increment age of forwarded mark"); 410 if (has_displaced_mark()) { 411 set_displaced_mark(displaced_mark().incr_age()); 412 } else { 413 set_mark(mark().incr_age()); 414 } 415 } 416 417 template <typename OopClosureType> 418 void oopDesc::oop_iterate(OopClosureType* cl) { 419 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass()); 420 } 421 422 template <typename OopClosureType> 423 void oopDesc::oop_iterate(OopClosureType* cl, MemRegion mr) { 424 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, klass(), mr); 425 } 426 427 template <typename OopClosureType> 428 int oopDesc::oop_iterate_size(OopClosureType* cl) { 429 Klass* k = klass(); 430 int size = size_given_klass(k); 431 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k); 432 return size; 433 } 434 435 template <typename OopClosureType> 436 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) { 437 Klass* k = klass(); 438 int size = size_given_klass(k); 439 OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr); 440 return size; 441 } 442 443 template <typename OopClosureType> 444 void oopDesc::oop_iterate_backwards(OopClosureType* cl) { 445 oop_iterate_backwards(cl, klass()); 446 } 447 448 template <typename OopClosureType> 449 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) { 450 OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k); 451 } 452 453 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) { 454 return obj == NULL || obj->klass()->is_subtype_of(klass); 455 } 456 457 intptr_t oopDesc::identity_hash() { 458 // Fast case; if the object is unlocked and the hash value is set, no locking is needed 459 // Note: The mark must be read into local variable to avoid concurrent updates. 460 markWord mrk = mark(); 461 if (mrk.is_unlocked() && !mrk.has_no_hash()) { 462 return mrk.hash(); 463 } else if (mrk.is_marked()) { 464 return mrk.hash(); 465 } else { 466 return slow_identity_hash(); 467 } 468 } 469 470 bool oopDesc::has_displaced_mark() const { 471 return mark().has_displaced_mark_helper(); 472 } 473 474 markWord oopDesc::displaced_mark() const { 475 return mark().displaced_mark_helper(); 476 } 477 478 void oopDesc::set_displaced_mark(markWord m) { 479 mark().set_displaced_mark_helper(m); 480 } 481 482 bool oopDesc::mark_must_be_preserved() const { 483 return mark_must_be_preserved(mark()); 484 } 485 486 bool oopDesc::mark_must_be_preserved(markWord m) const { 487 return m.must_be_preserved(this); 488 } 489 490 bool oopDesc::mark_must_be_preserved_for_promotion_failure(markWord m) const { 491 return m.must_be_preserved_for_promotion_failure(this); 492 } 493 494 #endif // SHARE_OOPS_OOP_INLINE_HPP