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