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