< prev index next >

src/share/vm/runtime/jniHandles.hpp

Print this page


   1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  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_VM_RUNTIME_JNIHANDLES_HPP
  26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP
  27 
  28 #include "runtime/handles.hpp"
  29 #include "utilities/top.hpp"
  30 
  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  public:













  44   // Resolve handle into oop
  45   inline static oop resolve(jobject handle);
  46   // Resolve externally provided handle into oop with some guards
  47   inline static oop resolve_external_guard(jobject handle);
  48   // Resolve handle into oop, result guaranteed not to be null
  49   inline static oop resolve_non_null(jobject handle);
  50 
  51   // Local handles
  52   static jobject make_local(oop obj);
  53   static jobject make_local(JNIEnv* env, oop obj);    // Fast version when env is known
  54   static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
  55   inline static void destroy_local(jobject handle);
  56 
  57   // Global handles
  58   static jobject make_global(Handle  obj);
  59   static void destroy_global(jobject handle);
  60 
  61   // Weak global handles
  62   static jobject make_weak_global(Handle obj);
  63   static void destroy_weak_global(jobject handle);


 154   void oops_do(OopClosure* f);
 155   // Traversal of weak handles. Unreachable oops are cleared.
 156   void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
 157 
 158   // Checked JNI support
 159   void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }
 160   const size_t get_planned_capacity() { return _planned_capacity; }
 161   const size_t get_number_of_live_handles();
 162 
 163   // Debugging
 164   bool chain_contains(jobject handle) const;    // Does this block or following blocks contain handle
 165   bool contains(jobject handle) const;          // Does this block contain handle
 166   int length() const;                           // Length of chain starting with this block
 167   long memory_usage() const;
 168   #ifndef PRODUCT
 169   static bool any_contains(jobject handle);     // Does any block currently in use contain handle
 170   static void print_statistics();
 171   #endif
 172 };
 173 





 174 
 175 inline oop JNIHandles::resolve(jobject handle) {
 176   oop result = (handle == NULL ? (oop)NULL : *(oop*)handle);
 177   assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle");
 178   assert(result != badJNIHandle, "Pointing to zapped jni handle area");




































 179   return result;
 180 };
 181 







 182 




 183 inline oop JNIHandles::resolve_external_guard(jobject handle) {
 184   if (handle == NULL) return NULL;
 185   oop result = *(oop*)handle;
 186   if (result == NULL || result == badJNIHandle) return NULL;

 187   return result;
 188 };
 189 
 190 
 191 inline oop JNIHandles::resolve_non_null(jobject handle) {
 192   assert(handle != NULL, "JNI handle should not be null");
 193   oop result = *(oop*)handle;
 194   assert(result != NULL, "Invalid value read from jni handle");
 195   assert(result != badJNIHandle, "Pointing to zapped jni handle area");
 196   // Don't let that private _deleted_handle object escape into the wild.
 197   assert(result != deleted_handle(), "Used a deleted global handle.");
 198   return result;
 199 };
 200 
 201 inline void JNIHandles::destroy_local(jobject handle) {
 202   if (handle != NULL) {
 203     *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
 204   }
 205 }
 206 
 207 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP
   1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  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_VM_RUNTIME_JNIHANDLES_HPP
  26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP
  27 
  28 #include "runtime/handles.hpp"
  29 #include "utilities/top.hpp"
  30 
  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);
  71 
  72   // Local handles
  73   static jobject make_local(oop obj);
  74   static jobject make_local(JNIEnv* env, oop obj);    // Fast version when env is known
  75   static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
  76   inline static void destroy_local(jobject handle);
  77 
  78   // Global handles
  79   static jobject make_global(Handle  obj);
  80   static void destroy_global(jobject handle);
  81 
  82   // Weak global handles
  83   static jobject make_weak_global(Handle obj);
  84   static void destroy_weak_global(jobject handle);


 175   void oops_do(OopClosure* f);
 176   // Traversal of weak handles. Unreachable oops are cleared.
 177   void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
 178 
 179   // Checked JNI support
 180   void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }
 181   const size_t get_planned_capacity() { return _planned_capacity; }
 182   const size_t get_number_of_live_handles();
 183 
 184   // Debugging
 185   bool chain_contains(jobject handle) const;    // Does this block or following blocks contain handle
 186   bool contains(jobject handle) const;          // Does this block contain handle
 187   int length() const;                           // Length of chain starting with this block
 188   long memory_usage() const;
 189   #ifndef PRODUCT
 190   static bool any_contains(jobject handle);     // Does any block currently in use contain handle
 191   static void print_statistics();
 192   #endif
 193 };
 194 
 195 inline bool JNIHandles::is_jweak(jobject handle) {
 196   STATIC_ASSERT(weak_tag_size == 1);
 197   STATIC_ASSERT(weak_tag_value == 1);
 198   return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
 199 }
 200 
 201 inline oop& JNIHandles::jobject_ref(jobject handle) {
 202   assert(!is_jweak(handle), "precondition");
 203   return *reinterpret_cast<oop*>(handle);
 204 }
 205 
 206 inline oop& JNIHandles::jweak_ref(jobject handle) {
 207   assert(is_jweak(handle), "precondition");
 208   char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
 209   return *reinterpret_cast<oop*>(ptr);
 210 }
 211 
 212 // external_guard is true if called from resolve_external_guard.
 213 // Treat deleted (and possibly zapped) as NULL for external_guard,
 214 // else as (asserted) error.
 215 template<bool external_guard>
 216 inline oop JNIHandles::guard_value(oop value) {
 217   if (!external_guard) {
 218     assert(value != badJNIHandle, "Pointing to zapped jni handle area");
 219     assert(value != deleted_handle(), "Used a deleted global handle");
 220   } else if ((value == badJNIHandle) || (value == deleted_handle())) {
 221     value = NULL;
 222   }
 223   return value;
 224 }
 225 
 226 // external_guard is true if called from resolve_external_guard.
 227 template<bool external_guard>
 228 inline oop JNIHandles::resolve_impl(jobject handle) {
 229   assert(handle != NULL, "precondition");
 230   oop result;
 231   if (is_jweak(handle)) {       // Unlikely
 232     result = resolve_jweak<external_guard>(handle);
 233   } else {
 234     result = jobject_ref(handle);
 235     // Construction of jobjects canonicalize a null value into a null
 236     // jobject, so for non-jweak the pointee should never be null.
 237     assert(external_guard || result != NULL,
 238            "Invalid value read from jni handle");
 239     result = guard_value<external_guard>(result);
 240   }
 241   return result;
 242 }
 243 
 244 inline oop JNIHandles::resolve(jobject handle) {
 245   oop result = NULL;
 246   if (handle != NULL) {
 247     result = resolve_impl<false /* external_guard */ >(handle);
 248   }
 249   return result;
 250 }
 251 
 252 // Resolve some erroneous cases to NULL, rather than treating them as
 253 // possibly unchecked errors.  In particular, deleted handles are
 254 // treated as NULL (though a deleted and later reallocated handle
 255 // isn't detected).
 256 inline oop JNIHandles::resolve_external_guard(jobject handle) {
 257   oop result = NULL;
 258   if (handle != NULL) {
 259     result = resolve_impl<true /* external_guard */ >(handle);
 260   }
 261   return result;
 262 }

 263 
 264 inline oop JNIHandles::resolve_non_null(jobject handle) {
 265   assert(handle != NULL, "JNI handle should not be null");
 266   oop result = resolve_impl<false /* external_guard */ >(handle);
 267   assert(result != NULL, "NULL read from jni handle");



 268   return result;
 269 }
 270 
 271 inline void JNIHandles::destroy_local(jobject handle) {
 272   if (handle != NULL) {
 273     jobject_ref(handle) = deleted_handle();
 274   }
 275 }
 276 
 277 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP
< prev index next >