< prev index next >

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

Print this page

 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.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 

 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());








 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   }

252 bool oopDesc::is_unlocked() const {
253   return mark().is_unlocked();
254 }
255 
256 // Used only for markSweep, scavenging
257 bool oopDesc::is_gc_marked() const {
258   return mark().is_marked();
259 }
260 
261 // Used by scavengers
262 bool oopDesc::is_forwarded() const {
263   // The extra heap check is needed since the obj might be locked, in which case the
264   // mark would point to a stack location and have the sentinel bit cleared
265   return mark().is_marked();
266 }
267 
268 // Used by scavengers
269 void oopDesc::forward_to(oop p) {
270   verify_forwardee(p);
271   markWord m = markWord::encode_pointer_as_mark(p);
272   assert(m.decode_pointer() == p, "encoding must be reversable");














273   set_mark(m);



274 }
275 
276 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
277   verify_forwardee(p);
278   markWord m = markWord::encode_pointer_as_mark(p);
279   assert(m.decode_pointer() == p, "encoding must be reversable");
280   markWord old_mark = cas_set_mark(m, compare, order);
281   if (old_mark == compare) {
282     return NULL;
283   } else {
284     return cast_to_oop(old_mark.decode_pointer());
285   }
286 }
287 























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












293 }
294 
295 // The following method needs to be MT safe.
296 uint oopDesc::age() const {
297   assert(!is_forwarded(), "Attempt to read age from forwarded mark");
298   if (has_displaced_mark()) {
299     return displaced_mark().age();
300   } else {
301     return mark().age();
302   }
303 }
304 
305 void oopDesc::incr_age() {
306   assert(!is_forwarded(), "Attempt to increment age of forwarded mark");
307   if (has_displaced_mark()) {
308     set_displaced_mark(displaced_mark().incr_age());
309   } else {
310     set_mark(mark().incr_age());
311   }
312 }

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

 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 "runtime/safepoint.hpp"
 40 #include "runtime/synchronizer.hpp"
 41 #include "utilities/align.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 

 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 #ifdef _LP64
 83   markWord header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
 84   assert(_metadata._compressed_klass == header.narrow_klass(), "klass must match: " PTR_FORMAT, header.value());
 85   assert(UseCompressedClassPointers, "expect compressed klass pointers");
 86   header = markWord((header.value() & markWord::klass_mask_in_place) | markWord::prototype().value());
 87 #else
 88   markWord header = markWord::prototype();
 89 #endif
 90   set_mark(header);
 91 }
 92 
 93 Klass* oopDesc::klass() const {
 94 #ifdef _LP64
 95   assert(UseCompressedClassPointers, "only with compressed class pointers");
 96   markWord header = mark();
 97   if (!header.is_neutral()) {
 98     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
 99   }
100   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));
101   Klass* klass = header.klass();
102   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));
103   return klass;
104 #else
105   return _metadata._klass;
106 #endif
107 }
108 
109 Klass* oopDesc::klass_or_null() const {
110 #ifdef _LP64
111   assert(UseCompressedClassPointers, "only with compressed class pointers");
112   markWord header = mark();
113   if (!header.is_neutral()) {
114     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
115   }
116   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));
117   Klass* klass = header.klass_or_null();
118   assert(klass == CompressedKlassPointers::decode(_metadata._compressed_klass), "klass must match: header: " INTPTR_FORMAT ", nklass: " INTPTR_FORMAT, header.value(), intptr_t(_metadata._compressed_klass));
119   return klass;
120 #else
121   return _metadata._klass;
122 #endif
123 }
124 
125 Klass* oopDesc::klass_or_null_acquire() const {
126 #ifdef _LP64
127   assert(UseCompressedClassPointers, "only with compressed class pointers");
128   markWord header = mark_acquire();
129   if (!header.is_neutral()) {
130     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
131   }
132   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));
133   Klass* klass = header.klass_or_null();
134   assert(klass == CompressedKlassPointers::decode(_metadata._compressed_klass), "klass must match: header: " INTPTR_FORMAT ", nklass: " INTPTR_FORMAT, header.value(), intptr_t(_metadata._compressed_klass));
135   return klass;
136 #else
137   return Atomic::load_acquire(&_metadata._klass);
138 #endif
139 }
140 
141 void oopDesc::set_klass(Klass* k) {
142   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
143   if (UseCompressedClassPointers) {
144     _metadata._compressed_klass = CompressedKlassPointers::encode_not_null(k);
145   } else {
146     _metadata._klass = k;
147   }
148 }
149 
150 void oopDesc::release_set_klass(HeapWord* mem, Klass* k) {
151   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
152   char* raw_mem = ((char*)mem + klass_offset_in_bytes());
153   if (UseCompressedClassPointers) {
154     Atomic::release_store((narrowKlass*)raw_mem,
155                           CompressedKlassPointers::encode_not_null(k));
156   } else {
157     Atomic::release_store((Klass**)raw_mem, k);
158   }

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

412   size_t size = size_given_klass(k);
413   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k);
414   return size;
415 }
416 
417 template <typename OopClosureType>
418 size_t oopDesc::oop_iterate_size(OopClosureType* cl, MemRegion mr) {
419   Klass* k = klass();
420   size_t size = size_given_klass(k);
421   OopIteratorClosureDispatch::oop_oop_iterate(cl, this, k, mr);
422   return size;
423 }
424 
425 template <typename OopClosureType>
426 void oopDesc::oop_iterate_backwards(OopClosureType* cl) {
427   oop_iterate_backwards(cl, klass());
428 }
429 
430 template <typename OopClosureType>
431 void oopDesc::oop_iterate_backwards(OopClosureType* cl, Klass* k) {

432   OopIteratorClosureDispatch::oop_oop_iterate_backwards(cl, this, k);
433 }
434 
435 bool oopDesc::is_instanceof_or_null(oop obj, Klass* klass) {
436   return obj == NULL || obj->klass()->is_subtype_of(klass);
437 }
438 
439 intptr_t oopDesc::identity_hash() {
440   // Fast case; if the object is unlocked and the hash value is set, no locking is needed
441   // Note: The mark must be read into local variable to avoid concurrent updates.
442   markWord mrk = mark();
443   if (mrk.is_unlocked() && !mrk.has_no_hash()) {
444     return mrk.hash();
445   } else if (mrk.is_marked()) {
446     return mrk.hash();
447   } else {
448     return slow_identity_hash();
449   }
450 }
451 
< prev index next >