< prev index next >

src/hotspot/share/runtime/jfieldIDWorkaround.hpp

Print this page




  29   // This workaround is because JVMTI doesn't have distinct entry points
  30   // for methods that use static jfieldIDs and instance jfieldIDs.
  31   // The workaround is to steal a low-order bit:
  32   //   a 1 means the jfieldID is an instance jfieldID,
  33   //             and the rest of the word is the offset of the field.
  34   //   a 0 means the jfieldID is a static jfieldID,
  35   //             and the rest of the word is the JNIid*.
  36   //
  37   // Another low-order bit is used to mark if an instance field
  38   // is accompanied by an indication of which class it applies to.
  39   //
  40   // Bit-format of a jfieldID (most significant first):
  41   //  address:30        instance=0:1 checked=0:1
  42   //  offset:30         instance=1:1 checked=0:1
  43   //  klass:23 offset:7 instance=1:1 checked=1:1
  44   //
  45   // If the offset does not fit in 7 bits, or if the fieldID is
  46   // not checked, then the checked bit is zero and the rest of
  47   // the word (30 bits) contains only the offset.
  48   //



  49  private:
  50   enum {
  51     checked_bits           = 1,
  52     instance_bits          = 1,
  53     address_bits           = BitsPerWord - checked_bits - instance_bits,

  54 
  55     large_offset_bits      = address_bits,  // unioned with address
  56     small_offset_bits      = 7,
  57     klass_bits             = address_bits - small_offset_bits,
  58 
  59     checked_shift          = 0,
  60     instance_shift         = checked_shift  + checked_bits,
  61     address_shift          = instance_shift + instance_bits,

  62 
  63     offset_shift           = address_shift,  // unioned with address
  64     klass_shift            = offset_shift + small_offset_bits,
  65 
  66     checked_mask_in_place  = right_n_bits(checked_bits)  << checked_shift,
  67     instance_mask_in_place = right_n_bits(instance_bits) << instance_shift,

  68 #ifndef _WIN64
  69     large_offset_mask      = right_n_bits(large_offset_bits),
  70     small_offset_mask      = right_n_bits(small_offset_bits),
  71     klass_mask             = right_n_bits(klass_bits)
  72 #endif
  73     };
  74 
  75 #ifdef _WIN64
  76     // These values are too big for Win64
  77     const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits);
  78     const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits);
  79     const static uintptr_t klass_mask        = right_n_bits(klass_bits);
  80 #endif
  81 
  82   // helper routines:
  83   static bool is_checked_jfieldID(jfieldID id) {
  84     uintptr_t as_uint = (uintptr_t) id;
  85     return ((as_uint & checked_mask_in_place) != 0);
  86   }
  87   static intptr_t raw_instance_offset(jfieldID id) {


  90       result &= small_offset_mask;  // cut off the hash bits
  91     }
  92     return (intptr_t)result;
  93   }
  94   static intptr_t encode_klass_hash(Klass* k, intptr_t offset);
  95   static bool             klass_hash_ok(Klass* k, jfieldID id);
  96   static void  verify_instance_jfieldID(Klass* k, jfieldID id);
  97 
  98  public:
  99   static bool is_valid_jfieldID(Klass* k, jfieldID id);
 100 
 101   static bool is_instance_jfieldID(Klass* k, jfieldID id) {
 102     uintptr_t as_uint = (uintptr_t) id;
 103     return ((as_uint & instance_mask_in_place) != 0);
 104   }
 105   static bool is_static_jfieldID(jfieldID id) {
 106     uintptr_t as_uint = (uintptr_t) id;
 107     return ((as_uint & instance_mask_in_place) == 0);
 108   }
 109 
 110   static jfieldID to_instance_jfieldID(Klass* k, int offset) {
 111     intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place;







 112     if (VerifyJNIFields) {
 113       as_uint |= encode_klass_hash(k, offset);
 114     }
 115     jfieldID result = (jfieldID) as_uint;
 116 #ifndef ASSERT
 117     // always verify in debug mode; switchable in anything else
 118     if (VerifyJNIFields)
 119 #endif // ASSERT
 120     {
 121       verify_instance_jfieldID(k, result);
 122     }
 123     assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset");
 124     return result;
 125   }
 126 
 127   static intptr_t from_instance_jfieldID(Klass* k, jfieldID id) {
 128 #ifndef ASSERT
 129     // always verify in debug mode; switchable in anything else
 130     if (VerifyJNIFields)
 131 #endif // ASSERT


 133       verify_instance_jfieldID(k, id);
 134     }
 135     return raw_instance_offset(id);
 136   }
 137 
 138   static jfieldID to_static_jfieldID(JNIid* id) {
 139     assert(id->is_static_field_id(), "from_JNIid, but not static field id");
 140     jfieldID result = (jfieldID) id;
 141     assert(from_static_jfieldID(result) == id, "must produce the same static id");
 142     return result;
 143   }
 144 
 145   static JNIid* from_static_jfieldID(jfieldID id) {
 146     assert(jfieldIDWorkaround::is_static_jfieldID(id),
 147            "to_JNIid, but not static jfieldID");
 148     JNIid* result = (JNIid*) id;
 149     assert(result->is_static_field_id(), "to_JNIid, but not static field id");
 150     return result;
 151   }
 152 
 153   static jfieldID to_jfieldID(InstanceKlass* k, int offset, bool is_static) {
 154     if (is_static) {
 155       JNIid *id = k->jni_id_for(offset);
 156       debug_only(id->set_is_static_field_id());
 157       return jfieldIDWorkaround::to_static_jfieldID(id);
 158     } else {
 159       return jfieldIDWorkaround::to_instance_jfieldID(k, offset);
 160     }
 161   }
 162 };
 163 
 164 #endif // SHARE_RUNTIME_JFIELDIDWORKAROUND_HPP


  29   // This workaround is because JVMTI doesn't have distinct entry points
  30   // for methods that use static jfieldIDs and instance jfieldIDs.
  31   // The workaround is to steal a low-order bit:
  32   //   a 1 means the jfieldID is an instance jfieldID,
  33   //             and the rest of the word is the offset of the field.
  34   //   a 0 means the jfieldID is a static jfieldID,
  35   //             and the rest of the word is the JNIid*.
  36   //
  37   // Another low-order bit is used to mark if an instance field
  38   // is accompanied by an indication of which class it applies to.
  39   //
  40   // Bit-format of a jfieldID (most significant first):
  41   //  address:30        instance=0:1 checked=0:1
  42   //  offset:30         instance=1:1 checked=0:1
  43   //  klass:23 offset:7 instance=1:1 checked=1:1
  44   //
  45   // If the offset does not fit in 7 bits, or if the fieldID is
  46   // not checked, then the checked bit is zero and the rest of
  47   // the word (30 bits) contains only the offset.
  48   //
  49 
  50   friend class JNI_FastGetField;
  51 
  52  private:
  53   enum {
  54     checked_bits           = 1,
  55     instance_bits          = 1,
  56     flattened_bits         = 1,
  57     address_bits           = BitsPerWord - checked_bits - instance_bits - flattened_bits,
  58 
  59     large_offset_bits      = address_bits,  // unioned with address
  60     small_offset_bits      = 7,
  61     klass_bits             = address_bits - small_offset_bits,
  62 
  63     checked_shift          = 0,
  64     instance_shift         = checked_shift  + checked_bits,
  65     flattened_shift        = instance_shift + instance_bits,
  66     address_shift          = flattened_shift + flattened_bits,
  67 
  68     offset_shift           = address_shift,  // unioned with address
  69     klass_shift            = offset_shift + small_offset_bits,
  70 
  71     checked_mask_in_place  = right_n_bits(checked_bits)  << checked_shift,
  72     instance_mask_in_place = right_n_bits(instance_bits) << instance_shift,
  73     flattened_mask_in_place = right_n_bits(flattened_bits) << flattened_shift,
  74 #ifndef _WIN64
  75     large_offset_mask      = right_n_bits(large_offset_bits),
  76     small_offset_mask      = right_n_bits(small_offset_bits),
  77     klass_mask             = right_n_bits(klass_bits)
  78 #endif
  79     };
  80 
  81 #ifdef _WIN64
  82     // These values are too big for Win64
  83     const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits);
  84     const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits);
  85     const static uintptr_t klass_mask        = right_n_bits(klass_bits);
  86 #endif
  87 
  88   // helper routines:
  89   static bool is_checked_jfieldID(jfieldID id) {
  90     uintptr_t as_uint = (uintptr_t) id;
  91     return ((as_uint & checked_mask_in_place) != 0);
  92   }
  93   static intptr_t raw_instance_offset(jfieldID id) {


  96       result &= small_offset_mask;  // cut off the hash bits
  97     }
  98     return (intptr_t)result;
  99   }
 100   static intptr_t encode_klass_hash(Klass* k, intptr_t offset);
 101   static bool             klass_hash_ok(Klass* k, jfieldID id);
 102   static void  verify_instance_jfieldID(Klass* k, jfieldID id);
 103 
 104  public:
 105   static bool is_valid_jfieldID(Klass* k, jfieldID id);
 106 
 107   static bool is_instance_jfieldID(Klass* k, jfieldID id) {
 108     uintptr_t as_uint = (uintptr_t) id;
 109     return ((as_uint & instance_mask_in_place) != 0);
 110   }
 111   static bool is_static_jfieldID(jfieldID id) {
 112     uintptr_t as_uint = (uintptr_t) id;
 113     return ((as_uint & instance_mask_in_place) == 0);
 114   }
 115 
 116   static bool is_flattened_field(jfieldID id) {
 117     uintptr_t as_uint = (uintptr_t) id;
 118     return ((as_uint & flattened_mask_in_place) != 0);
 119   }
 120 
 121   static jfieldID to_instance_jfieldID(Klass* k, int offset, bool flattened) {
 122     intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) |
 123                        (flattened ? flattened_mask_in_place : 0) |
 124                         instance_mask_in_place;
 125     if (VerifyJNIFields) {
 126       as_uint |= encode_klass_hash(k, offset);
 127     }
 128     jfieldID result = (jfieldID) as_uint;
 129 #ifndef ASSERT
 130     // always verify in debug mode; switchable in anything else
 131     if (VerifyJNIFields)
 132 #endif // ASSERT
 133     {
 134       verify_instance_jfieldID(k, result);
 135     }
 136     assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset");
 137     return result;
 138   }
 139 
 140   static intptr_t from_instance_jfieldID(Klass* k, jfieldID id) {
 141 #ifndef ASSERT
 142     // always verify in debug mode; switchable in anything else
 143     if (VerifyJNIFields)
 144 #endif // ASSERT


 146       verify_instance_jfieldID(k, id);
 147     }
 148     return raw_instance_offset(id);
 149   }
 150 
 151   static jfieldID to_static_jfieldID(JNIid* id) {
 152     assert(id->is_static_field_id(), "from_JNIid, but not static field id");
 153     jfieldID result = (jfieldID) id;
 154     assert(from_static_jfieldID(result) == id, "must produce the same static id");
 155     return result;
 156   }
 157 
 158   static JNIid* from_static_jfieldID(jfieldID id) {
 159     assert(jfieldIDWorkaround::is_static_jfieldID(id),
 160            "to_JNIid, but not static jfieldID");
 161     JNIid* result = (JNIid*) id;
 162     assert(result->is_static_field_id(), "to_JNIid, but not static field id");
 163     return result;
 164   }
 165 
 166   static jfieldID to_jfieldID(InstanceKlass* k, int offset, bool is_static, bool is_flattened) {
 167     if (is_static) {
 168       JNIid *id = k->jni_id_for(offset);
 169       debug_only(id->set_is_static_field_id());
 170       return jfieldIDWorkaround::to_static_jfieldID(id);
 171     } else {
 172       return jfieldIDWorkaround::to_instance_jfieldID(k, offset, is_flattened);
 173     }
 174   }
 175 };
 176 
 177 #endif // SHARE_RUNTIME_JFIELDIDWORKAROUND_HPP
< prev index next >