< prev index next >

src/hotspot/share/oops/markWord.hpp

Print this page
@@ -28,43 +28,63 @@
  #include "cppstdlib/type_traits.hpp"
  #include "metaprogramming/primitiveConversions.hpp"
  #include "oops/compressedKlass.hpp"
  #include "oops/oopsHierarchy.hpp"
  #include "runtime/globals.hpp"
+ #include "utilities/powerOfTwo.hpp"
  
  // The markWord describes the header of an object.
  //
  // Bit-format of an object header (most significant first, big endian layout below):
  //
  //  32 bits:
  //  --------
- //             hash:25 ------------>| age:4  self-fwd:1  lock:2 (normal object)
+ //             hash:25              age:4  self-fwd:1  lock:2
  //
- //  64 bits:
- //  --------
- //  unused:22 hash:31 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
+ //  64 bits (without compact headers):
+ //  ----------------------------------
+ //  unused:22  hash:31  valhalla:4  age:4  self-fwd:1  lock:2
  //
  //  64 bits (with compact headers):
  //  -------------------------------
- //  klass:22  hash:31 -->| unused_gap:4  age:4  self-fwd:1  lock:2 (normal object)
- //
- //  - hash contains the identity hash value: largest value is
- //    31 bits, see os::random().  Also, 64-bit vm's require
- //    a hash value no bigger than 32 bits because they will not
- //    properly generate a mask larger than that: see library_call.cpp
+ //  klass:22   hash:31  valhalla:4  age:4  self-fwd:1  lock:2
  //
- //  - the two lock bits are used to describe three states: locked/unlocked and monitor.
+ //  - lock bits are used to describe lock states: locked/unlocked/monitor-locked
+ //    and to indicate that an object has been GC marked / forwarded.
  //
  //    [header          | 00]  locked             locked regular object header (fast-locking in use)
  //    [header          | 01]  unlocked           regular object header
- //    [ptr             | 10]  monitor            inflated lock (header is swapped out, UseObjectMonitorTable == false)
  //    [header          | 10]  monitor            inflated lock (UseObjectMonitorTable == true)
- //    [ptr             | 11]  marked             used to mark an object
+ //    [ptr             | 10]  monitor            inflated lock (UseObjectMonitorTable == false, header is swapped out)
+ //    [ptr             | 11]  marked             used to mark an object (header is swapped out)
+ //
+ //  - self-fwd - used by some GCs to indicate in-place forwarding.
+ //
+ //    Note the position of 'self-fwd' is not by accident. When forwarding an
+ //    object to a new heap position, HeapWord alignment guarantees the lower
+ //    bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
+ //    set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
+ //
+ //  - age - used by some GCs to track the age of objects.
+ //
+ //  - valhalla - only supported on 64-bit VMs
+ //
+ //    * inline types:      A value class instance
+ //    * flat arrays:       An array with flattened value class elements
+ //    * null-free arrays:  An array instance without null elements
+ //    * valhalla reserved: Reserved for future use
+ //
+ //    Inline types cannot be locked and do not have an identity hash.
+ //
+ //  - hash - contains the identity hash value: largest value is 31 bits, see
+ //    os::random().  Also, 64-bit VMs require a hash value no bigger than 32
+ //    bits because they will not properly generate a mask larger than that:
+ //    see library_call.cpp
+ //
+ //  - klass - klass identifier used when UseCompactObjectHeaders == true
  
- class BasicLock;
  class ObjectMonitor;
- class JavaThread;
  class outputStream;
  
  class markWord {
   private:
    uintptr_t _value;

@@ -95,81 +115,123 @@
    }
  
    // Conversion
    uintptr_t value() const { return _value; }
  
-   // Constants
-   static const int age_bits                       = 4;
+   // Constants, in least significant bit order
+ 
+   // Number of bits
    static const int lock_bits                      = 2;
    static const int self_fwd_bits                  = 1;
-   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - self_fwd_bits;
+   static const int age_bits                       = 4;
+   static const int inline_type_bits               = LP64_ONLY(1) NOT_LP64(0);
+   static const int null_free_array_bits           = LP64_ONLY(1) NOT_LP64(0);
+   static const int flat_array_bits                = LP64_ONLY(1) NOT_LP64(0);
+   static const int valhalla_reserved_bits         = LP64_ONLY(1) NOT_LP64(0);
+   static const int max_hash_bits                  = BitsPerWord - age_bits - lock_bits - inline_type_bits - valhalla_reserved_bits - flat_array_bits - null_free_array_bits - self_fwd_bits;
    static const int hash_bits                      = max_hash_bits > 31 ? 31 : max_hash_bits;
-   static const int unused_gap_bits                = LP64_ONLY(4) NOT_LP64(0); // Reserved for Valhalla.
  
+   // Shifts
    static const int lock_shift                     = 0;
    static const int self_fwd_shift                 = lock_shift + lock_bits;
    static const int age_shift                      = self_fwd_shift + self_fwd_bits;
-   static const int hash_shift                     = age_shift + age_bits + unused_gap_bits;
+   static const int inline_type_shift              = age_shift + age_bits;
+   static const int null_free_array_shift          = inline_type_shift + inline_type_bits;
+   static const int flat_array_shift               = null_free_array_shift + null_free_array_bits;
+   static const int valhalla_reserved_shift        = flat_array_shift + flat_array_bits;
+   static const int hash_shift                     = valhalla_reserved_shift + valhalla_reserved_bits;
+ 
+   // Masks (in-place)
+   static const uintptr_t lock_mask_in_place       = right_n_bits(lock_bits) << lock_shift;
+   static const uintptr_t self_fwd_bit_in_place    = right_n_bits(self_fwd_bits) << self_fwd_shift;
+   static const uintptr_t age_mask_in_place        = right_n_bits(age_bits) << age_shift;
+   static const uintptr_t inline_type_bit_in_place = right_n_bits(inline_type_bits) << inline_type_shift;
+   static const uintptr_t null_free_array_bit_in_place = right_n_bits(null_free_array_bits) << null_free_array_shift;
+   static const uintptr_t flat_array_bit_in_place  = right_n_bits(flat_array_bits) << flat_array_shift;
+   static const uintptr_t valhalla_reserved_bit_in_place = right_n_bits(valhalla_reserved_bits) << valhalla_reserved_shift;
+   static const uintptr_t hash_mask_in_place       = right_n_bits(hash_bits) << hash_shift;
+ 
+   // Verify that _bit_in_place refers to constants with only one bit.
+   static_assert(is_power_of_2(self_fwd_bit_in_place));
+ #ifdef _LP64
+   static_assert(is_power_of_2(inline_type_bit_in_place));
+   static_assert(is_power_of_2(null_free_array_bit_in_place));
+   static_assert(is_power_of_2(flat_array_bit_in_place));
+   static_assert(is_power_of_2(valhalla_reserved_bit_in_place));
+ #endif
  
-   static const uintptr_t lock_mask                = right_n_bits(lock_bits);
-   static const uintptr_t lock_mask_in_place       = lock_mask << lock_shift;
-   static const uintptr_t self_fwd_mask            = right_n_bits(self_fwd_bits);
-   static const uintptr_t self_fwd_mask_in_place   = self_fwd_mask << self_fwd_shift;
-   static const uintptr_t age_mask                 = right_n_bits(age_bits);
-   static const uintptr_t age_mask_in_place        = age_mask << age_shift;
-   static const uintptr_t hash_mask                = right_n_bits(hash_bits);
-   static const uintptr_t hash_mask_in_place       = hash_mask << hash_shift;
+   // Masks (unshifted)
+   static const uintptr_t lock_mask                = lock_mask_in_place >> lock_shift;
+   static const uintptr_t age_mask                 = age_mask_in_place >> age_shift;
+   static const uintptr_t hash_mask                = hash_mask_in_place >> hash_shift;
  
  #ifdef _LP64
    // Used only with compact headers:
    // We store the (narrow) Klass* in the bits 43 to 64.
  
-   // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword
+   // These are for bit-precise extraction of the narrow Klass* from the 64-bit markWord
    static constexpr int klass_offset_in_bytes      = 4;
    static constexpr int klass_shift                = hash_shift + hash_bits;
    static constexpr int klass_shift_at_offset      = klass_shift - klass_offset_in_bytes * BitsPerByte;
    static constexpr int klass_bits                 = 22;
    static constexpr uintptr_t klass_mask           = right_n_bits(klass_bits);
    static constexpr uintptr_t klass_mask_in_place  = klass_mask << klass_shift;
  #endif
  
- 
    static const uintptr_t locked_value             = 0;
    static const uintptr_t unlocked_value           = 1;
    static const uintptr_t monitor_value            = 2;
    static const uintptr_t marked_value             = 3;
  
+   static const uintptr_t inline_type_pattern      = inline_type_bit_in_place | unlocked_value;
+   static const uintptr_t inline_type_pattern_mask = inline_type_bit_in_place | lock_mask_in_place;
+ 
    static const uintptr_t no_hash                  = 0 ;  // no hash value assigned
    static const uintptr_t no_hash_in_place         = (uintptr_t)no_hash << hash_shift;
    static const uintptr_t no_lock_in_place         = unlocked_value;
  
    static const uint max_age                       = age_mask;
  
    // Creates a markWord with all bits set to zero.
    static markWord zero() { return markWord(uintptr_t(0)); }
  
+   bool is_inline_type() const {
+ #ifdef _LP64 // 64 bit encodings only
+     return (mask_bits(value(), inline_type_pattern_mask) == inline_type_pattern);
+ #else
+     return false;
+ #endif
+   }
+ 
    // lock accessors (note that these assume lock_shift == 0)
    bool is_locked()   const {
      return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
    }
    bool is_unlocked() const {
      return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
    }
    bool is_marked()   const {
      return (mask_bits(value(), lock_mask_in_place) == marked_value);
    }
-   bool is_forwarded() const {
-     // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
-     return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
-   }
+ 
    bool is_neutral()  const {  // Not locked, or marked - a "clean" neutral state
+     LP64_ONLY(assert(!is_unlocked() || mask_bits(value(), inline_type_bit_in_place) == 0,
+                      "Inline types should not be used for locking. _value: " PTR_FORMAT, _value));
      return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
    }
  
+   bool is_forwarded() const {
+     // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
+     return mask_bits(value(), lock_mask_in_place | self_fwd_bit_in_place) >= static_cast<intptr_t>(marked_value);
+   }
+ 
    // Should this header be preserved during GC?
    bool must_be_preserved() const {
-     return (!is_unlocked() || !has_no_hash());
+     // The reserved bits are only guaranteed to be unset if the mark word is "unlocked"
+     LP64_ONLY(assert(!is_unlocked() || mask_bits(value(),  valhalla_reserved_bit_in_place) == 0,
+                      "Reserved bits should not be used. _value: " PTR_FORMAT, _value));
+     return !is_unlocked() || !has_no_hash();
    }
  
    // WARNING: The following routines are used EXCLUSIVELY by
    // synchronization functions. They are not really gc safe.
    // They must get updated if markWord layout get changed.

@@ -202,14 +264,18 @@
      assert(!UseObjectMonitorTable, "Locking with OM table does not use markWord for monitors");
      uintptr_t tmp = (uintptr_t) monitor;
      return markWord(tmp | monitor_value);
    }
  
-   bool has_displaced_mark_helper() const {
+   bool has_monitor_pointer() const {
      intptr_t lockbits = value() & lock_mask_in_place;
      return !UseObjectMonitorTable && lockbits == monitor_value;
    }
+ 
+   bool has_displaced_mark_helper() const {
+     return has_monitor_pointer();
+   }
    markWord displaced_mark_helper() const;
    void set_displaced_mark_helper(markWord m) const;
  
    // used to encode pointers during GC
    markWord clear_lock_bits() const { return markWord(value() & ~lock_mask_in_place); }

@@ -232,10 +298,30 @@
  
    bool has_no_hash() const {
      return hash() == no_hash;
    }
  
+   bool is_flat_array() const {
+     assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
+     assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
+ #ifdef _LP64 // 64 bit encodings only
+     return (mask_bits(value(), flat_array_bit_in_place) != 0);
+ #else
+     return false;
+ #endif
+   }
+ 
+   bool is_null_free_array() const {
+     assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
+     assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
+ #ifdef _LP64 // 64 bit encodings only
+     return (mask_bits(value(), null_free_array_bit_in_place) != 0);
+ #else
+     return false;
+ #endif
+   }
+ 
    markWord copy_set_hash(intptr_t hash) const {
      uintptr_t tmp = value() & (~hash_mask_in_place);
      tmp |= ((hash & hash_mask) << hash_shift);
      return markWord(tmp);
    }

@@ -244,13 +330,33 @@
    inline Klass* klass_or_null() const;
    inline Klass* klass_without_asserts() const;
    inline narrowKlass narrow_klass() const;
    inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
  
-   // Prototype mark for initialization
+   // Prototype marks for initialization
+ 
    static markWord prototype() {
-     return markWord( no_hash_in_place | no_lock_in_place );
+     return markWord(unlocked_value);
+   }
+ 
+   static markWord inline_type_prototype() {
+     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
+     return markWord(unlocked_value | inline_type_bit_in_place);
+   }
+ 
+   static markWord flat_array_prototype(bool null_free) {
+     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
+     if (null_free) {
+       return markWord(unlocked_value | flat_array_bit_in_place | null_free_array_bit_in_place);
+     } else {
+       return markWord(unlocked_value | flat_array_bit_in_place);
+     }
+   }
+ 
+   static markWord null_free_array_prototype() {
+     NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
+     return markWord(unlocked_value | null_free_array_bit_in_place);
    }
  
    // Debugging
    void print_on(outputStream* st, bool print_monitor_info = true) const;
  

@@ -259,19 +365,19 @@
  
    // Recover address of oop from encoded form used in mark
    inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
  
    inline bool is_self_forwarded() const {
-     return mask_bits(value(), self_fwd_mask_in_place) != 0;
+     return mask_bits(value(), self_fwd_bit_in_place) != 0;
    }
  
    inline markWord set_self_forwarded() const {
-     return markWord(value() | self_fwd_mask_in_place);
+     return markWord(value() | self_fwd_bit_in_place);
    }
  
    inline markWord unset_self_forwarded() const {
-     return markWord(value() & ~self_fwd_mask_in_place);
+     return markWord(value() & ~self_fwd_bit_in_place);
    }
  
    inline oop forwardee() const {
      return cast_to_oop(decode_pointer());
    }
< prev index next >