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