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