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