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