< prev index next >

src/hotspot/share/oops/oop.inline.hpp

Print this page

 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   uintptr_t v = HeapAccess<MO_RELAXED>::load_at(as_oop(), mark_offset_in_bytes());
 49   return markWord(v);
 50 }
 51 



 52 markWord* oopDesc::mark_addr() const {
 53   return (markWord*) &_mark;
 54 }
 55 
 56 void oopDesc::set_mark(markWord m) {
 57   HeapAccess<MO_RELAXED>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
 58 }
 59 
 60 void oopDesc::set_mark(HeapWord* mem, markWord m) {
 61   *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m;
 62 }
 63 
 64 void oopDesc::release_set_mark(markWord m) {
 65   HeapAccess<MO_RELEASE>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
 66 }
 67 




 68 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
 69   uintptr_t v = HeapAccess<>::atomic_cmpxchg_at(as_oop(), mark_offset_in_bytes(), old_mark.value(), new_mark.value());
 70   return markWord(v);
 71 }
 72 
 73 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
 74   return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
 75 }
 76 









 77 void oopDesc::init_mark() {
 78   set_mark(markWord::prototype_for_klass(klass()));







 79 }
 80 
 81 Klass* oopDesc::klass() const {
 82   if (UseCompressedClassPointers) {





 83     return CompressedKlassPointers::decode_not_null(_metadata._compressed_klass);
 84   } else {
 85     return _metadata._klass;
 86   }
 87 }
 88 
 89 Klass* oopDesc::klass_or_null() const {
 90   if (UseCompressedClassPointers) {





 91     return CompressedKlassPointers::decode(_metadata._compressed_klass);
 92   } else {
 93     return _metadata._klass;
 94   }
 95 }
 96 
 97 Klass* oopDesc::klass_or_null_acquire() const {
 98   if (UseCompressedClassPointers) {
 99     narrowKlass nklass = Atomic::load_acquire(&_metadata._compressed_klass);
100     return CompressedKlassPointers::decode(nklass);
101   } else {
102     return Atomic::load_acquire(&_metadata._klass);
103   }








104 }
105 
106 void oopDesc::set_klass(Klass* k) {
107   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");

108   if (UseCompressedClassPointers) {
109     _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k);
110   } else {
111     _metadata._klass = k;
112   }
113 }
114 
115 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
116   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");

117   char* raw_mem = ((char*)mem + klass_offset_in_bytes());
118   if (UseCompressedClassPointers) {
119     Atomic::release_store((narrowKlass*)raw_mem,
120                           CompressedKlassPointers::encode_not_null(k));
121   } else {
122     Atomic::release_store((Klass**)raw_mem, k);
123   }
124 }
125 
126 int oopDesc::klass_gap() const {
127   return *(int*)(((intptr_t)this) + klass_gap_offset_in_bytes());
128 }
129 
130 void oopDesc::set_klass_gap(HeapWord* mem, int v) {

131   if (UseCompressedClassPointers) {
132     *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
133   }
134 }
135 
136 void oopDesc::set_klass_gap(int v) {
137   set_klass_gap((HeapWord*)this, v);
138 }
139 
140 bool oopDesc::is_a(Klass* k) const {
141   return klass()->is_subtype_of(k);
142 }
143 
144 int oopDesc::size()  {
145   return size_given_klass(klass());
146 }
147 
148 int oopDesc::size_given_klass(Klass* klass)  {
149   int lh = klass->layout_helper();
150   int s;
151 
152   // lh is now a value computed at class initialization that may hint
153   // at the size.  For instances, this is positive and equal to the
154   // size.  For arrays, this is negative and provides log2 of the
155   // array element size.  For other oops, it is zero and thus requires
156   // a virtual call.
157   //
158   // We go to all this trouble because the size computation is at the
159   // heart of phase 2 of mark-compaction, and called for every object,

257 bool oopDesc::has_bias_pattern() const {
258   return mark().has_bias_pattern();
259 }
260 
261 // Used only for markSweep, scavenging
262 bool oopDesc::is_gc_marked() const {
263   return mark().is_marked();
264 }
265 
266 // Used by scavengers
267 bool oopDesc::is_forwarded() const {
268   // The extra heap check is needed since the obj might be locked, in which case the
269   // mark would point to a stack location and have the sentinel bit cleared
270   return mark().is_marked();
271 }
272 
273 // Used by scavengers
274 void oopDesc::forward_to(oop p) {
275   verify_forwardee(p);
276   markWord m = markWord::encode_pointer_as_mark(p);
277   assert(m.decode_pointer() == p, "encoding must be reversable");
278   set_mark(m);
279 }
280 
281 // Used by parallel scavengers
282 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) {
283   verify_forwardee(p);
284   markWord m = markWord::encode_pointer_as_mark(p);
285   assert(m.decode_pointer() == p, "encoding must be reversable");
286   return cas_set_mark(m, compare, order) == compare;
287 }
288 

















289 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
290   verify_forwardee(p);
291   markWord m = markWord::encode_pointer_as_mark(p);
292   assert(m.decode_pointer() == p, "encoding must be reversable");
293   markWord old_mark = cas_set_mark(m, compare, order);
294   if (old_mark == compare) {
295     return NULL;
296   } else {
297     return cast_to_oop(old_mark.decode_pointer());
298   }
299 }
300 























301 // Note that the forwardee is not the same thing as the displaced_mark.
302 // The forwardee is used when copying during scavenge and mark-sweep.
303 // It does need to clear the low two locking- and GC-related bits.
304 oop oopDesc::forwardee() const {
305   return cast_to_oop(mark().decode_pointer());













306 }
307 
308 // The following method needs to be MT safe.
309 uint oopDesc::age() const {
310   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
311   if (has_displaced_mark()) {
312     return displaced_mark().age();
313   } else {
314     return mark().age();
315   }
316 }
317 
318 void oopDesc::incr_age() {
319   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
320   if (has_displaced_mark()) {
321     set_displaced_mark(displaced_mark().incr_age());
322   } else {
323     set_mark(mark().incr_age());
324   }
325 }

340   int size = size_given_klass(k);
341   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
342   return size;
343 }
344 
345 template <typename OopClosureType>
346 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
347   Klass* k = klass();
348   int size = size_given_klass(k);
349   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
350   return size;
351 }
352 
353 template <typename OopClosureType>
354 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
355   oop_iterate_backwards(cl, klass());
356 }
357 
358 template <typename OopClosureType>
359 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {
360   assert(k == klass(), "wrong klass");
361   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
362 }
363 
364 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
365   return obj == NULL || obj->klass()->is_subtype_of(klass);
366 }
367 
368 intptr_t oopDesc::identity_hash() {
369   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
370   // Note: The mark must be read into local variable to avoid concurrent updates.
371   markWord mrk = mark();
372   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
373     return mrk.hash();
374   } else if (mrk.is_marked()) {
375     return mrk.hash();
376   } else {
377     return slow_identity_hash();
378   }
379 }
380 

 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/objectMonitor.inline.hpp"
 40 #include "utilities/align.hpp"
 41 #include "utilities/debug.hpp"
 42 #include "utilities/macros.hpp"
 43 #include "utilities/globalDefinitions.hpp"
 44 
 45 // Implementation of all inlined member functions defined in oop.hpp
 46 // We need a separate file to avoid circular references
 47 
 48 markWord oopDesc::mark() const {
 49   uintptr_t v = HeapAccess<MO_RELAXED>::load_at(as_oop(), mark_offset_in_bytes());
 50   return markWord(v);
 51 }
 52 
 53 markWord oopDesc::mark_acquire() const {
 54   return Atomic::load_acquire(&_mark);
 55 }
 56 markWord* oopDesc::mark_addr() const {
 57   return (markWord*) &_mark;
 58 }
 59 
 60 void oopDesc::set_mark(markWord m) {
 61   HeapAccess<MO_RELAXED>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
 62 }
 63 
 64 void oopDesc::set_mark(HeapWord* mem, markWord m) {
 65   *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m;
 66 }
 67 
 68 void oopDesc::release_set_mark(markWord m) {
 69   HeapAccess<MO_RELEASE>::store_at(as_oop(), mark_offset_in_bytes(), m.value());
 70 }
 71 
 72 void oopDesc::release_set_mark(HeapWord* mem, markWord m) {
 73   Atomic::release_store((markWord*)(((char*)mem) + mark_offset_in_bytes()), m);
 74 }
 75 
 76 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
 77   uintptr_t v = HeapAccess<>::atomic_cmpxchg_at(as_oop(), mark_offset_in_bytes(), old_mark.value(), new_mark.value());
 78   return markWord(v);
 79 }
 80 
 81 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
 82   return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
 83 }
 84 
 85 markWord oopDesc::resolve_mark() const {
 86   assert(UseFastLocking, "Only safe with fast-locking");
 87   markWord hdr = mark();
 88   if (hdr.has_displaced_mark_helper()) {
 89     hdr = hdr.displaced_mark_helper();
 90   }
 91   return hdr;
 92 }
 93 
 94 void oopDesc::init_mark() {
 95 #ifdef _LP64
 96   if (UseCompactObjectHeaders) {
 97     markWord header = resolve_mark();
 98     assert(UseCompressedClassPointers, "expect compressed klass pointers");
 99     set_mark(markWord((header.value() & markWord::klass_mask_in_place) | markWord::prototype().value()));
100   } else
101 #endif
102   set_mark(markWord::prototype());
103 }
104 
105 Klass* oopDesc::klass() const {
106 #ifdef _LP64
107   if (UseCompactObjectHeaders) {
108     assert(UseCompressedClassPointers, "only with compressed class pointers");
109     markWord header = resolve_mark();
110     return header.klass();
111   } else if (UseCompressedClassPointers) {
112     return CompressedKlassPointers::decode_not_null(_metadata._compressed_klass);
113   } else
114 #endif
115   return _metadata._klass;
116 }
117 
118 Klass* oopDesc::klass_or_null() const {
119 #ifdef _LP64
120   if (UseCompactObjectHeaders) {
121     assert(UseCompressedClassPointers, "only with compressed class pointers");
122     markWord header = resolve_mark();
123     return header.klass_or_null();
124   } else if (UseCompressedClassPointers) {
125     return CompressedKlassPointers::decode(_metadata._compressed_klass);
126   } else
127 #endif
128   return _metadata._klass;
129 }
130 
131 Klass* oopDesc::klass_or_null_acquire() const {
132 #ifdef _LP64
133   if (UseCompactObjectHeaders) {
134     assert(UseCompressedClassPointers, "only with compressed class pointers");
135     markWord header = mark_acquire();
136     if (header.has_monitor()) {
137       header = header.monitor()->header();
138     }
139     return header.klass_or_null();
140   } else if (UseCompressedClassPointers) {
141      narrowKlass nklass = Atomic::load_acquire(&_metadata._compressed_klass);
142      return CompressedKlassPointers::decode(nklass);
143   } else
144 #endif
145   return Atomic::load_acquire(&_metadata._klass);
146 }
147 
148 void oopDesc::set_klass(Klass* k) {
149   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
150   assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers");
151   if (UseCompressedClassPointers) {
152     _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k);
153   } else {
154     _metadata._klass = k;
155   }
156 }
157 
158 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
159   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
160   assert(!UseCompactObjectHeaders, "don't set Klass* with compact headers");
161   char* raw_mem = ((char*)mem + klass_offset_in_bytes());
162   if (UseCompressedClassPointers) {
163     Atomic::release_store((narrowKlass*)raw_mem,
164                           CompressedKlassPointers::encode_not_null(k));
165   } else {
166     Atomic::release_store((Klass**)raw_mem, k);
167   }
168 }
169 




170 void oopDesc::set_klass_gap(HeapWord* mem, int v) {
171   assert(!UseCompactObjectHeaders, "don't set Klass* gap with compact headers");
172   if (UseCompressedClassPointers) {
173     *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
174   }
175 }
176 




177 bool oopDesc::is_a(Klass* k) const {
178   return klass()->is_subtype_of(k);
179 }
180 
181 int oopDesc::size()  {
182   return size_given_klass(klass());
183 }
184 
185 int oopDesc::size_given_klass(Klass* klass)  {
186   int lh = klass->layout_helper();
187   int s;
188 
189   // lh is now a value computed at class initialization that may hint
190   // at the size.  For instances, this is positive and equal to the
191   // size.  For arrays, this is negative and provides log2 of the
192   // array element size.  For other oops, it is zero and thus requires
193   // a virtual call.
194   //
195   // We go to all this trouble because the size computation is at the
196   // heart of phase 2 of mark-compaction, and called for every object,

294 bool oopDesc::has_bias_pattern() const {
295   return mark().has_bias_pattern();
296 }
297 
298 // Used only for markSweep, scavenging
299 bool oopDesc::is_gc_marked() const {
300   return mark().is_marked();
301 }
302 
303 // Used by scavengers
304 bool oopDesc::is_forwarded() const {
305   // The extra heap check is needed since the obj might be locked, in which case the
306   // mark would point to a stack location and have the sentinel bit cleared
307   return mark().is_marked();
308 }
309 
310 // Used by scavengers
311 void oopDesc::forward_to(oop p) {
312   verify_forwardee(p);
313   markWord m = markWord::encode_pointer_as_mark(p);
314   assert(forwardee(m) == p, "encoding must be reversable");
315   set_mark(m);
316 }
317 
318 // Used by parallel scavengers
319 bool oopDesc::cas_forward_to(oop p, markWord compare, atomic_memory_order order) {
320   verify_forwardee(p);
321   markWord m = markWord::encode_pointer_as_mark(p);
322   assert(m.decode_pointer() == p, "encoding must be reversable");
323   return cas_set_mark(m, compare, order) == compare;
324 }
325 
326 void oopDesc::forward_to_self() {
327 #ifdef _LP64
328   verify_forwardee(this);
329   markWord m = mark();
330   // If mark is displaced, we need to preserve the Klass* from real header.
331   assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint");
332   if (m.has_displaced_mark_helper()) {
333     m = m.displaced_mark_helper();
334   }
335   m = m.set_self_forwarded();
336   assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable");
337   set_mark(m);
338 #else
339   forward_to(oop(this));
340 #endif
341 }
342 
343 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
344   verify_forwardee(p);
345   markWord m = markWord::encode_pointer_as_mark(p);
346   assert(forwardee(m) == p, "encoding must be reversable");
347   markWord old_mark = cas_set_mark(m, compare, order);
348   if (old_mark == compare) {
349     return NULL;
350   } else {
351     return forwardee(old_mark);
352   }
353 }
354 
355 oop oopDesc::forward_to_self_atomic(markWord compare, atomic_memory_order order) {
356 #ifdef _LP64
357   verify_forwardee(this);
358   markWord m = compare;
359   // If mark is displaced, we need to preserve the Klass* from real header.
360   assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint");
361   if (m.has_displaced_mark_helper()) {
362     m = m.displaced_mark_helper();
363   }
364   m = m.set_self_forwarded();
365   assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable");
366   markWord old_mark = cas_set_mark(m, compare, order);
367   if (old_mark == compare) {
368     return NULL;
369   } else {
370     assert(old_mark.is_marked(), "must be marked here");
371     return forwardee(old_mark);
372   }
373 #else
374   return forward_to_atomic(oop(this), compare, order);
375 #endif
376 }
377 
378 // Note that the forwardee is not the same thing as the displaced_mark.
379 // The forwardee is used when copying during scavenge and mark-sweep.
380 // It does need to clear the low two locking- and GC-related bits.
381 oop oopDesc::forwardee() const {
382   return forwardee(mark());
383 }
384 
385 oop oopDesc::forwardee(markWord header) const {
386   assert(header.is_marked(), "must be forwarded");
387 #ifdef _LP64
388   if (header.self_forwarded()) {
389     return cast_to_oop(this);
390   } else
391 #endif
392   {
393     assert(header.is_marked(), "only decode when actually forwarded");
394     return cast_to_oop(header.decode_pointer());
395   }
396 }
397 
398 // The following method needs to be MT safe.
399 uint oopDesc::age() const {
400   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
401   if (has_displaced_mark()) {
402     return displaced_mark().age();
403   } else {
404     return mark().age();
405   }
406 }
407 
408 void oopDesc::incr_age() {
409   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
410   if (has_displaced_mark()) {
411     set_displaced_mark(displaced_mark().incr_age());
412   } else {
413     set_mark(mark().incr_age());
414   }
415 }

430   int size = size_given_klass(k);
431   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
432   return size;
433 }
434 
435 template <typename OopClosureType>
436 int oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
437   Klass* k = klass();
438   int size = size_given_klass(k);
439   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
440   return size;
441 }
442 
443 template <typename OopClosureType>
444 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
445   oop_iterate_backwards(cl, klass());
446 }
447 
448 template <typename OopClosureType>
449 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {

450   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
451 }
452 
453 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
454   return obj == NULL || obj->klass()->is_subtype_of(klass);
455 }
456 
457 intptr_t oopDesc::identity_hash() {
458   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
459   // Note: The mark must be read into local variable to avoid concurrent updates.
460   markWord mrk = mark();
461   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
462     return mrk.hash();
463   } else if (mrk.is_marked()) {
464     return mrk.hash();
465   } else {
466     return slow_identity_hash();
467   }
468 }
469 
< prev index next >