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