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