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