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







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

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

















269 oop oopDesc::forward_to_atomic(oop p, markWord compare, atomic_memory_order order) {
270   verify_forwardee(p);
271   markWord m = markWord::encode_pointer_as_mark(p);
272   assert(m.decode_pointer() == p, "encoding must be reversible");



















273   markWord old_mark = cas_set_mark(m, compare, order);
274   if (old_mark == compare) {
275     return NULL;
276   } else {
277     return cast_to_oop(old_mark.decode_pointer());

278   }



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












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

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

 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 void oopDesc::release_set_mark(HeapWord* mem, markWord m) {
 75   Atomic::release_store((markWord*)(((char*)mem) + mark_offset_in_bytes()), m);
 76 }
 77 
 78 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark) {
 79   return Atomic::cmpxchg(&_mark, old_mark, new_mark);
 80 }
 81 
 82 markWord oopDesc::cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order) {
 83   return Atomic::cmpxchg(&_mark, old_mark, new_mark, order);
 84 }
 85 
 86 void oopDesc::init_mark() {
 87 #ifdef _LP64
 88   markWord header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
 89   assert(UseCompressedClassPointers, "expect compressed klass pointers");
 90   header = markWord((header.value() & markWord::klass_mask_in_place) | markWord::prototype().value());
 91 #else
 92   markWord header = markWord::prototype();
 93 #endif
 94   set_mark(header);
 95 }
 96 
 97 Klass* oopDesc::klass() const {
 98 #ifdef _LP64
 99   assert(UseCompressedClassPointers, "only with compressed class pointers");
100   markWord header = mark();
101   if (!header.is_neutral()) {
102     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
103   }
104   return header.klass();
105 #else
106   return _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   return header.klass_or_null();
118 #else
119   return _klass;
120 #endif
121 }
122 
123 Klass* oopDesc::klass_or_null_acquire() const {
124 #ifdef _LP64
125   assert(UseCompressedClassPointers, "only with compressed class pointers");
126   markWord header = mark_acquire();
127   if (!header.is_neutral()) {
128     header = ObjectSynchronizer::stable_mark(cast_to_oop(this));
129   }
130   return header.klass_or_null();
131 #else
132   return Atomic::load_acquire(&_klass);
133 #endif
134 }
135 
136 #ifndef _LP64
137 void oopDesc::set_klass(Klass* k) {
138   assert(Universe::is_bootstrapping() || (k != NULL && k->is_klass()), "incorrect Klass");
139   _klass = k;




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





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

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

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

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