< prev index next >

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

Print this page

 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/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   return Atomic::load(&_mark);
 50 }
 51 
 52 markWord oopDesc::mark_acquire() const {
 53   return Atomic::load_acquire(&_mark);
 54 }
 55 
 56 markWord* oopDesc::mark_addr() const {
 57   return (markWord*) &_mark;
 58 }
 59 
 60 void oopDesc::set_mark(markWord m) {
 61   Atomic::store(&_mark, m);
 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   Atomic::release_store(&_mark, m);
 70 }
 71 




 72 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
 73   return Atomic::cmpxchg(&_mark, old_mark, new_mark);
 74 }
 75 
 76 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
 77   return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
 78 }
 79 
 80 void oopDesc::init_mark() {
 81   set_mark(markWord::prototype());







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

 89   }




 90 }
 91 
 92 Klass* oopDesc::klass_or_null() const {
 93   if (UseCompressedClassPointers) {
 94     return CompressedKlassPointers::decode(_metadata._compressed_klass);
 95   } else {
 96     return _metadata._klass;

 97   }




 98 }
 99 
100 Klass* oopDesc::klass_or_null_acquire() const {
101   if (UseCompressedClassPointers) {
102     narrowKlass nklass = Atomic::load_acquire(&_metadata._compressed_klass);
103     return CompressedKlassPointers::decode(nklass);
104   } else {
105     return Atomic::load_acquire(&_metadata._klass);
106   }




107 }
108 

109 void oopDesc::set_klass(Klass* k) {
110   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
111   if (UseCompressedClassPointers) {
112     _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k);
113   } else {
114     _metadata._klass = k;
115   }
116 }
117 
118 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
119   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
120   char* raw_mem = ((char*)mem + klass_offset_in_bytes());
121   if (UseCompressedClassPointers) {
122     Atomic::release_store((narrowKlass*)raw_mem,
123                           CompressedKlassPointers::encode_not_null(k));
124   } else {
125     Atomic::release_store((Klass**)raw_mem, k);
126   }
127 }
128 
129 void oopDesc::set_klass_gap(HeapWord* mem, int v) {
130   if (UseCompressedClassPointers) {
131     *(int*)(((char*)mem) + klass_gap_offset_in_bytes()) = v;
132   }
133 }
134 
135 bool oopDesc::is_a(Klass* k) const {
136   return klass()->is_subtype_of(k);
137 }
138 
139 size_t oopDesc::size()  {
140   return size_given_klass(klass());
141 }
142 
143 size_t oopDesc::size_given_klass(Klass* klass)  {
144   int lh = klass->layout_helper();
145   size_t s;
146 
147   // lh is now a value computed at class initialization that may hint
148   // at the size.  For instances, this is positive and equal to the
149   // size.  For arrays, this is negative and provides log2 of the
150   // array element size.  For other oops, it is zero and thus requires
151   // a virtual call.
152   //
153   // We go to all this trouble because the size computation is at the

240 bool oopDesc::is_unlocked() const {
241   return mark().is_unlocked();
242 }
243 
244 // Used only for markSweep, scavenging
245 bool oopDesc::is_gc_marked() const {
246   return mark().is_marked();
247 }
248 
249 // Used by scavengers
250 bool oopDesc::is_forwarded() const {
251   // The extra heap check is needed since the obj might be locked, in which case the
252   // mark would point to a stack location and have the sentinel bit cleared
253   return mark().is_marked();
254 }
255 
256 // Used by scavengers
257 void oopDesc::forward_to(oop p) {
258   verify_forwardee(p);
259   markWord m = markWord::encode_pointer_as_mark(p);
260   assert(m.decode_pointer() == p, "encoding must be reversible");
261   set_mark(m);
262 }
263 

















264 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
265   verify_forwardee(p);
266   markWord m = markWord::encode_pointer_as_mark(p);
267   assert(m.decode_pointer() == p, "encoding must be reversible");



















268   markWord old_mark = cas_set_mark(m, compare, order);
269   if (old_mark == compare) {
270     return NULL;
271   } else {
272     return cast_to_oop(old_mark.decode_pointer());

273   }



274 }
275 
276 // Note that the forwardee is not the same thing as the displaced_mark.
277 // The forwardee is used when copying during scavenge and mark-sweep.
278 // It does need to clear the low two locking- and GC-related bits.
279 oop oopDesc::forwardee() const {
280   assert(is_forwarded(), "only decode when actually forwarded");
281   return cast_to_oop(mark().decode_pointer());












282 }
283 
284 // The following method needs to be MT safe.
285 uint oopDesc::age() const {
286   assert(!mark().is_marked(), "Attempt to read age from forwarded mark");
287   if (has_displaced_mark()) {
288     return displaced_mark().age();
289   } else {
290     return mark().age();
291   }
292 }
293 
294 void oopDesc::incr_age() {
295   assert(!mark().is_marked(), "Attempt to increment age of forwarded mark");
296   if (has_displaced_mark()) {
297     set_displaced_mark(displaced_mark().incr_age());
298   } else {
299     set_mark(mark().incr_age());
300   }
301 }

316   size_t size = size_given_klass(k);
317   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
318   return size;
319 }
320 
321 template <typename OopClosureType>
322 size_t oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
323   Klass* k = klass();
324   size_t size = size_given_klass(k);
325   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
326   return size;
327 }
328 
329 template <typename OopClosureType>
330 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
331   oop_iterate_backwards(cl, klass());
332 }
333 
334 template <typename OopClosureType>
335 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {
336   assert(k == klass(), "wrong klass");
337   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
338 }
339 
340 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
341   return obj == NULL || obj->klass()->is_subtype_of(klass);
342 }
343 
344 intptr_t oopDesc::identity_hash() {
345   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
346   // Note: The mark must be read into local variable to avoid concurrent updates.
347   markWord mrk = mark();
348   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
349     return mrk.hash();
350   } else if (mrk.is_marked()) {
351     return mrk.hash();
352   } else {
353     return slow_identity_hash();
354   }
355 }
356 

 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/instanceKlass.hpp"
 37 #include "oops/markWord.inline.hpp"
 38 #include "oops/oopsHierarchy.hpp"
 39 #include "runtime/atomic.hpp"
 40 #include "runtime/globals.hpp"
 41 #include "runtime/safepoint.hpp"
 42 #include "runtime/synchronizer.hpp"
 43 #include "utilities/align.hpp"
 44 #include "utilities/debug.hpp"
 45 #include "utilities/macros.hpp"
 46 #include "utilities/globalDefinitions.hpp"
 47 
 48 // Implementation of all inlined member functions defined in oop.hpp
 49 // We need a separate file to avoid circular references
 50 
 51 markWord oopDesc::mark() const {
 52   return Atomic::load(&_mark);
 53 }
 54 
 55 markWord oopDesc::mark_acquire() const {
 56   return Atomic::load_acquire(&_mark);
 57 }
 58 
 59 markWord* oopDesc::mark_addr() const {
 60   return (markWord*) &_mark;
 61 }
 62 
 63 void oopDesc::set_mark(markWord m) {
 64   Atomic::store(&_mark, m);
 65 }
 66 
 67 void oopDesc::set_mark(HeapWord* mem, markWord m) {
 68   *(markWord*)(((char*)mem) + mark_offset_in_bytes()) = m;
 69 }
 70 
 71 void oopDesc::release_set_mark(markWord m) {
 72   Atomic::release_store(&_mark, m);
 73 }
 74 
 75 void oopDesc::release_set_mark(HeapWord* mem, markWord m) {
 76   Atomic::release_store((markWord*)(((char*)mem) + mark_offset_in_bytes()), m);
 77 }
 78 
 79 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
 80   return Atomic::cmpxchg(&_mark, old_mark, new_mark);
 81 }
 82 
 83 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
 84   return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
 85 }
 86 
 87 void oopDesc::init_mark() {
 88 #ifdef _LP64
 89   markWord header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
 90   assert(UseCompressedClassPointers, "expect compressed klass pointers");
 91   header = markWord((header.value() & markWord::klass_mask_in_place) | markWord::prototype().value());
 92 #else
 93   markWord header = markWord::prototype();
 94 #endif
 95   set_mark(header);
 96 }
 97 
 98 Klass* oopDesc::klass() const {
 99 #ifdef _LP64
100   assert(UseCompressedClassPointers, "only with compressed class pointers");
101   markWord header = mark();
102   if (!header.is_neutral()) {
103     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
104   }
105   return header.klass();
106 #else
107   return _klass;
108 #endif
109 }
110 
111 Klass* oopDesc::klass_or_null() const {
112 #ifdef _LP64
113   assert(UseCompressedClassPointers, "only with compressed class pointers");
114   markWord header = mark();
115   if (!header.is_neutral()) {
116     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
117   }
118   return header.klass_or_null();
119 #else
120   return _klass;
121 #endif
122 }
123 
124 Klass* oopDesc::klass_or_null_acquire() const {
125 #ifdef _LP64
126   assert(UseCompressedClassPointers, "only with compressed class pointers");
127   markWord header = mark_acquire();
128   if (!header.is_neutral()) {
129     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
130   }
131   return header.klass_or_null();
132 #else
133   return Atomic::load_acquire(&_klass);
134 #endif
135 }
136 
137 #ifndef _LP64
138 void oopDesc::set_klass(Klass* k) {
139   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
140   _klass = k;




141 }
142 
143 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
144   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
145   char* raw_mem = ((char*)mem + klass_offset_in_bytes());
146   if (UseCompressedClassPointers) {
147     Atomic::release_store((narrowKlass*)raw_mem,
148                           CompressedKlassPointers::encode_not_null(k));
149   } else {
150     Atomic::release_store((Klass**)raw_mem, k);
151   }
152 }
153 #endif





154 
155 bool oopDesc::is_a(Klass* k) const {
156   return klass()->is_subtype_of(k);
157 }
158 
159 size_t oopDesc::size()  {
160   return size_given_klass(klass());
161 }
162 
163 size_t oopDesc::size_given_klass(Klass* klass)  {
164   int lh = klass->layout_helper();
165   size_t s;
166 
167   // lh is now a value computed at class initialization that may hint
168   // at the size.  For instances, this is positive and equal to the
169   // size.  For arrays, this is negative and provides log2 of the
170   // array element size.  For other oops, it is zero and thus requires
171   // a virtual call.
172   //
173   // We go to all this trouble because the size computation is at the

260 bool oopDesc::is_unlocked() const {
261   return mark().is_unlocked();
262 }
263 
264 // Used only for markSweep, scavenging
265 bool oopDesc::is_gc_marked() const {
266   return mark().is_marked();
267 }
268 
269 // Used by scavengers
270 bool oopDesc::is_forwarded() const {
271   // The extra heap check is needed since the obj might be locked, in which case the
272   // mark would point to a stack location and have the sentinel bit cleared
273   return mark().is_marked();
274 }
275 
276 // Used by scavengers
277 void oopDesc::forward_to(oop p) {
278   verify_forwardee(p);
279   markWord m = markWord::encode_pointer_as_mark(p);
280   assert(forwardee(m) == p, "encoding must be reversable");
281   set_mark(m);
282 }
283 
284 void oopDesc::forward_to_self() {
285 #ifdef _LP64
286   verify_forwardee(this);
287   markWord m = mark();
288   // If mark is displaced, we need to preserve the Klass* from real header.
289   assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint");
290   if (m.has_displaced_mark_helper()) {
291     m = m.displaced_mark_helper();
292   }
293   m = m.set_self_forwarded();
294   assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable");
295   set_mark(m);
296 #else
297   forward_to(oop(this));
298 #endif
299 }
300 
301 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
302   verify_forwardee(p);
303   markWord m = markWord::encode_pointer_as_mark(p);
304   assert(forwardee(m) == p, "encoding must be reversable");
305   markWord old_mark = cas_set_mark(m, compare, order);
306   if (old_mark == compare) {
307     return NULL;
308   } else {
309     return forwardee(old_mark);
310   }
311 }
312 
313 oop oopDesc::forward_to_self_atomic(markWord compare, atomic_memory_order order) {
314 #ifdef _LP64
315   verify_forwardee(this);
316   markWord m = compare;
317   // If mark is displaced, we need to preserve the Klass* from real header.
318   assert(SafepointSynchronize::is_at_safepoint(), "we can only safely fetch the displaced header at safepoint");
319   if (m.has_displaced_mark_helper()) {
320     m = m.displaced_mark_helper();
321   }
322   m = m.set_self_forwarded();
323   assert(forwardee(m) == cast_to_oop(this), "encoding must be reversable");
324   markWord old_mark = cas_set_mark(m, compare, order);
325   if (old_mark == compare) {
326     return NULL;
327   } else {
328     assert(old_mark.is_marked(), "must be marked here");
329     return forwardee(old_mark);
330   }
331 #else
332   return forward_to_atomic(oop(this), compare, order);
333 #endif
334 }
335 
336 // Note that the forwardee is not the same thing as the displaced_mark.
337 // The forwardee is used when copying during scavenge and mark-sweep.
338 // It does need to clear the low two locking- and GC-related bits.
339 oop oopDesc::forwardee() const {
340   return forwardee(mark());
341 }
342 
343 oop oopDesc::forwardee(markWord header) const {
344   assert(header.is_marked(), "must be forwarded");
345 #ifdef _LP64
346   if (header.self_forwarded()) {
347     return cast_to_oop(this);
348   } else
349 #endif
350   {
351     assert(header.is_marked(), "only decode when actually forwarded");
352     return cast_to_oop(header.decode_pointer());
353   }
354 }
355 
356 // The following method needs to be MT safe.
357 uint oopDesc::age() const {
358   assert(!mark().is_marked(), "Attempt to read age from forwarded mark");
359   if (has_displaced_mark()) {
360     return displaced_mark().age();
361   } else {
362     return mark().age();
363   }
364 }
365 
366 void oopDesc::incr_age() {
367   assert(!mark().is_marked(), "Attempt to increment age of forwarded mark");
368   if (has_displaced_mark()) {
369     set_displaced_mark(displaced_mark().incr_age());
370   } else {
371     set_mark(mark().incr_age());
372   }
373 }

388   size_t size = size_given_klass(k);
389   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
390   return size;
391 }
392 
393 template <typename OopClosureType>
394 size_t oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
395   Klass* k = klass();
396   size_t size = size_given_klass(k);
397   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
398   return size;
399 }
400 
401 template <typename OopClosureType>
402 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
403   oop_iterate_backwards(cl, klass());
404 }
405 
406 template <typename OopClosureType>
407 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {

408   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
409 }
410 
411 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
412   return obj == NULL || obj->klass()->is_subtype_of(klass);
413 }
414 
415 intptr_t oopDesc::identity_hash() {
416   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
417   // Note: The mark must be read into local variable to avoid concurrent updates.
418   markWord mrk = mark();
419   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
420     return mrk.hash();
421   } else if (mrk.is_marked()) {
422     return mrk.hash();
423   } else {
424     return slow_identity_hash();
425   }
426 }
427 
< prev index next >