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