< prev index next >

src/hotspot/share/runtime/jniHandles.hpp

Print this page




  31 class JNIHandleBlock;
  32 
  33 
  34 // Interface for creating and resolving local/global JNI handles
  35 
  36 class JNIHandles : AllStatic {
  37   friend class VMStructs;
  38  private:
  39   static JNIHandleBlock* _global_handles;             // First global handle block
  40   static JNIHandleBlock* _weak_global_handles;        // First weak global handle block
  41   static oop _deleted_handle;                         // Sentinel marking deleted handles
  42 
  43   inline static bool is_jweak(jobject handle);
  44   inline static oop& jobject_ref(jobject handle); // NOT jweak!
  45   inline static oop& jweak_ref(jobject handle);
  46 
  47   template<bool external_guard> inline static oop guard_value(oop value);
  48   template<bool external_guard> inline static oop resolve_impl(jobject handle);
  49   template<bool external_guard> static oop resolve_jweak(jweak handle);
  50 




  51  public:
  52   // Low tag bit in jobject used to distinguish a jweak.  jweak is
  53   // type equivalent to jobject, but there are places where we need to
  54   // be able to distinguish jweak values from other jobjects, and
  55   // is_weak_global_handle is unsuitable for performance reasons.  To
  56   // provide such a test we add weak_tag_value to the (aligned) byte
  57   // address designated by the jobject to produce the corresponding
  58   // jweak.  Accessing the value of a jobject must account for it
  59   // being a possibly offset jweak.
  60   static const uintptr_t weak_tag_size = 1;
  61   static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);
  62   static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;
  63   static const int weak_tag_value = 1;
  64 
  65   // Resolve handle into oop
  66   inline static oop resolve(jobject handle);
  67   // Resolve externally provided handle into oop with some guards
  68   inline static oop resolve_external_guard(jobject handle);
  69   // Resolve handle into oop, result guaranteed not to be null
  70   inline static oop resolve_non_null(jobject handle);


 213   return *reinterpret_cast<oop*>(ptr);
 214 }
 215 
 216 // external_guard is true if called from resolve_external_guard.
 217 // Treat deleted (and possibly zapped) as NULL for external_guard,
 218 // else as (asserted) error.
 219 template<bool external_guard>
 220 inline oop JNIHandles::guard_value(oop value) {
 221   if (!external_guard) {
 222     assert(value != deleted_handle(), "Used a deleted global handle");
 223   } else if (value == deleted_handle()) {
 224     value = NULL;
 225   }
 226   return value;
 227 }
 228 
 229 // external_guard is true if called from resolve_external_guard.
 230 template<bool external_guard>
 231 inline oop JNIHandles::resolve_impl(jobject handle) {
 232   assert(handle != NULL, "precondition");

 233   oop result;
 234   if (is_jweak(handle)) {       // Unlikely
 235     result = resolve_jweak<external_guard>(handle);
 236   } else {
 237     result = jobject_ref(handle);
 238     // Construction of jobjects canonicalize a null value into a null
 239     // jobject, so for non-jweak the pointee should never be null.
 240     assert(external_guard || result != NULL,
 241            "Invalid value read from jni handle");
 242     result = guard_value<external_guard>(result);
 243   }
 244   return result;
 245 }
 246 
 247 inline oop JNIHandles::resolve(jobject handle) {
 248   oop result = NULL;
 249   if (handle != NULL) {
 250     result = resolve_impl<false /* external_guard */ >(handle);
 251   }
 252   return result;




  31 class JNIHandleBlock;
  32 
  33 
  34 // Interface for creating and resolving local/global JNI handles
  35 
  36 class JNIHandles : AllStatic {
  37   friend class VMStructs;
  38  private:
  39   static JNIHandleBlock* _global_handles;             // First global handle block
  40   static JNIHandleBlock* _weak_global_handles;        // First weak global handle block
  41   static oop _deleted_handle;                         // Sentinel marking deleted handles
  42 
  43   inline static bool is_jweak(jobject handle);
  44   inline static oop& jobject_ref(jobject handle); // NOT jweak!
  45   inline static oop& jweak_ref(jobject handle);
  46 
  47   template<bool external_guard> inline static oop guard_value(oop value);
  48   template<bool external_guard> inline static oop resolve_impl(jobject handle);
  49   template<bool external_guard> static oop resolve_jweak(jweak handle);
  50 
  51   // This method is not inlined in order to avoid circular includes between
  52   // this header file and thread.hpp.
  53   static bool current_thread_in_native();
  54 
  55  public:
  56   // Low tag bit in jobject used to distinguish a jweak.  jweak is
  57   // type equivalent to jobject, but there are places where we need to
  58   // be able to distinguish jweak values from other jobjects, and
  59   // is_weak_global_handle is unsuitable for performance reasons.  To
  60   // provide such a test we add weak_tag_value to the (aligned) byte
  61   // address designated by the jobject to produce the corresponding
  62   // jweak.  Accessing the value of a jobject must account for it
  63   // being a possibly offset jweak.
  64   static const uintptr_t weak_tag_size = 1;
  65   static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);
  66   static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;
  67   static const int weak_tag_value = 1;
  68 
  69   // Resolve handle into oop
  70   inline static oop resolve(jobject handle);
  71   // Resolve externally provided handle into oop with some guards
  72   inline static oop resolve_external_guard(jobject handle);
  73   // Resolve handle into oop, result guaranteed not to be null
  74   inline static oop resolve_non_null(jobject handle);


 217   return *reinterpret_cast<oop*>(ptr);
 218 }
 219 
 220 // external_guard is true if called from resolve_external_guard.
 221 // Treat deleted (and possibly zapped) as NULL for external_guard,
 222 // else as (asserted) error.
 223 template<bool external_guard>
 224 inline oop JNIHandles::guard_value(oop value) {
 225   if (!external_guard) {
 226     assert(value != deleted_handle(), "Used a deleted global handle");
 227   } else if (value == deleted_handle()) {
 228     value = NULL;
 229   }
 230   return value;
 231 }
 232 
 233 // external_guard is true if called from resolve_external_guard.
 234 template<bool external_guard>
 235 inline oop JNIHandles::resolve_impl(jobject handle) {
 236   assert(handle != NULL, "precondition");
 237   assert(!current_thread_in_native(), "must not be in native");
 238   oop result;
 239   if (is_jweak(handle)) {       // Unlikely
 240     result = resolve_jweak<external_guard>(handle);
 241   } else {
 242     result = jobject_ref(handle);
 243     // Construction of jobjects canonicalize a null value into a null
 244     // jobject, so for non-jweak the pointee should never be null.
 245     assert(external_guard || result != NULL,
 246            "Invalid value read from jni handle");
 247     result = guard_value<external_guard>(result);
 248   }
 249   return result;
 250 }
 251 
 252 inline oop JNIHandles::resolve(jobject handle) {
 253   oop result = NULL;
 254   if (handle != NULL) {
 255     result = resolve_impl<false /* external_guard */ >(handle);
 256   }
 257   return result;


< prev index next >