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

434   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
435 }
436 
437 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
438   return obj == NULL || obj->klass()->is_subtype_of(klass);
439 }
440 
441 intptr_t oopDesc::identity_hash() {
442   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
443   // Note: The mark must be read into local variable to avoid concurrent updates.
444   markWord mrk = mark();
445   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
446     return mrk.hash();
447   } else if (mrk.is_marked()) {
448     return mrk.hash();
449   } else {
450     return slow_identity_hash();
451   }
452 }
453 
454 bool oopDesc::has_displaced_mark() const {
455   return mark().has_displaced_mark_helper();
456 }
457 
458 markWord oopDesc::displaced_mark() const {
459   return mark().displaced_mark_helper();
460 }
461 
462 void oopDesc::set_displaced_mark(markWord m) {
463   mark().set_displaced_mark_helper(m);
464 }
465 
466 bool oopDesc::mark_must_be_preserved() const {
467   return mark_must_be_preserved(mark());
468 }
469 
470 bool oopDesc::mark_must_be_preserved(markWord m) const {
471   return m.must_be_preserved(this);
472 }
473 
474 #endif // SHARE_OOPS_OOP_INLINE_HPP
--- EOF ---