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