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