30 #include "runtime/jniHandles.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "runtime/thread.inline.hpp" 33 #include "trace/traceMacros.hpp" 34 #include "utilities/align.hpp" 35 #if INCLUDE_ALL_GCS 36 #include "gc/g1/g1SATBCardTableModRefBS.hpp" 37 #endif 38 39 JNIHandleBlock* JNIHandles::_global_handles = NULL; 40 JNIHandleBlock* JNIHandles::_weak_global_handles = NULL; 41 oop JNIHandles::_deleted_handle = NULL; 42 43 44 jobject JNIHandles::make_local(oop obj) { 45 if (obj == NULL) { 46 return NULL; // ignore null handles 47 } else { 48 Thread* thread = Thread::current(); 49 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 50 return thread->active_handles()->allocate_handle(obj); 51 } 52 } 53 54 55 // optimized versions 56 57 jobject JNIHandles::make_local(Thread* thread, oop obj) { 58 if (obj == NULL) { 59 return NULL; // ignore null handles 60 } else { 61 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 62 return thread->active_handles()->allocate_handle(obj); 63 } 64 } 65 66 67 jobject JNIHandles::make_local(JNIEnv* env, oop obj) { 68 if (obj == NULL) { 69 return NULL; // ignore null handles 70 } else { 71 JavaThread* thread = JavaThread::thread_from_jni_environment(env); 72 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 73 return thread->active_handles()->allocate_handle(obj); 74 } 75 } 76 77 78 jobject JNIHandles::make_global(Handle obj) { 79 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 80 jobject res = NULL; 81 if (!obj.is_null()) { 82 // ignore null handles 83 MutexLocker ml(JNIGlobalHandle_lock); 84 assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 85 res = _global_handles->allocate_handle(obj()); 86 } else { 87 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 88 } 89 90 return res; 91 } 92 93 94 jobject JNIHandles::make_weak_global(Handle obj) { 95 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 96 jobject res = NULL; 97 if (!obj.is_null()) { 98 // ignore null handles 99 { 100 MutexLocker ml(JNIGlobalHandle_lock); 101 assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 102 res = _weak_global_handles->allocate_handle(obj()); 103 } 104 // Add weak tag. 105 assert(is_aligned(res, weak_tag_alignment), "invariant"); 106 char* tptr = reinterpret_cast<char*>(res) + weak_tag_value; 107 res = reinterpret_cast<jobject>(tptr); 108 } else { 109 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 110 } 111 return res; 112 } 113 114 template<bool external_guard> 115 oop JNIHandles::resolve_jweak(jweak handle) { 248 st->print_cr("JNI global references: %d", global_handle_count.count()); 249 st->cr(); 250 st->flush(); 251 } 252 253 class VerifyHandleClosure: public OopClosure { 254 public: 255 virtual void do_oop(oop* root) { 256 (*root)->verify(); 257 } 258 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } 259 }; 260 261 void JNIHandles::verify() { 262 VerifyHandleClosure verify_handle; 263 264 oops_do(&verify_handle); 265 weak_oops_do(&verify_handle); 266 } 267 268 269 270 void jni_handles_init() { 271 JNIHandles::initialize(); 272 } 273 274 275 int JNIHandleBlock::_blocks_allocated = 0; 276 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; 277 #ifndef PRODUCT 278 JNIHandleBlock* JNIHandleBlock::_block_list = NULL; 279 #endif 280 281 282 #ifdef ASSERT 283 void JNIHandleBlock::zap() { 284 // Zap block values 285 _top = 0; 286 for (int index = 0; index < block_size_in_oops; index++) { 287 _handles[index] = NULL; | 30 #include "runtime/jniHandles.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "runtime/thread.inline.hpp" 33 #include "trace/traceMacros.hpp" 34 #include "utilities/align.hpp" 35 #if INCLUDE_ALL_GCS 36 #include "gc/g1/g1SATBCardTableModRefBS.hpp" 37 #endif 38 39 JNIHandleBlock* JNIHandles::_global_handles = NULL; 40 JNIHandleBlock* JNIHandles::_weak_global_handles = NULL; 41 oop JNIHandles::_deleted_handle = NULL; 42 43 44 jobject JNIHandles::make_local(oop obj) { 45 if (obj == NULL) { 46 return NULL; // ignore null handles 47 } else { 48 Thread* thread = Thread::current(); 49 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 50 assert(!current_thread_in_native(), "must not be in native"); 51 return thread->active_handles()->allocate_handle(obj); 52 } 53 } 54 55 56 // optimized versions 57 58 jobject JNIHandles::make_local(Thread* thread, oop obj) { 59 if (obj == NULL) { 60 return NULL; // ignore null handles 61 } else { 62 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 63 assert(thread->is_Java_thread(), "not a Java thread"); 64 assert(!current_thread_in_native(), "must not be in native"); 65 return thread->active_handles()->allocate_handle(obj); 66 } 67 } 68 69 70 jobject JNIHandles::make_local(JNIEnv* env, oop obj) { 71 if (obj == NULL) { 72 return NULL; // ignore null handles 73 } else { 74 JavaThread* thread = JavaThread::thread_from_jni_environment(env); 75 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); 76 assert(!current_thread_in_native(), "must not be in native"); 77 return thread->active_handles()->allocate_handle(obj); 78 } 79 } 80 81 82 jobject JNIHandles::make_global(Handle obj) { 83 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 84 assert(!current_thread_in_native(), "must not be in native"); 85 jobject res = NULL; 86 if (!obj.is_null()) { 87 // ignore null handles 88 MutexLocker ml(JNIGlobalHandle_lock); 89 assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 90 res = _global_handles->allocate_handle(obj()); 91 } else { 92 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 93 } 94 95 return res; 96 } 97 98 99 jobject JNIHandles::make_weak_global(Handle obj) { 100 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); 101 assert(!current_thread_in_native(), "must not be in native"); 102 jobject res = NULL; 103 if (!obj.is_null()) { 104 // ignore null handles 105 { 106 MutexLocker ml(JNIGlobalHandle_lock); 107 assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); 108 res = _weak_global_handles->allocate_handle(obj()); 109 } 110 // Add weak tag. 111 assert(is_aligned(res, weak_tag_alignment), "invariant"); 112 char* tptr = reinterpret_cast<char*>(res) + weak_tag_value; 113 res = reinterpret_cast<jobject>(tptr); 114 } else { 115 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 116 } 117 return res; 118 } 119 120 template<bool external_guard> 121 oop JNIHandles::resolve_jweak(jweak handle) { 254 st->print_cr("JNI global references: %d", global_handle_count.count()); 255 st->cr(); 256 st->flush(); 257 } 258 259 class VerifyHandleClosure: public OopClosure { 260 public: 261 virtual void do_oop(oop* root) { 262 (*root)->verify(); 263 } 264 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } 265 }; 266 267 void JNIHandles::verify() { 268 VerifyHandleClosure verify_handle; 269 270 oops_do(&verify_handle); 271 weak_oops_do(&verify_handle); 272 } 273 274 // This method is implemented here to avoid circular includes between 275 // jniHandles.hpp and thread.hpp. 276 bool JNIHandles::current_thread_in_native() { 277 Thread* thread = Thread::current(); 278 return (thread->is_Java_thread() && 279 JavaThread::current()->thread_state() == _thread_in_native); 280 } 281 282 283 void jni_handles_init() { 284 JNIHandles::initialize(); 285 } 286 287 288 int JNIHandleBlock::_blocks_allocated = 0; 289 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; 290 #ifndef PRODUCT 291 JNIHandleBlock* JNIHandleBlock::_block_list = NULL; 292 #endif 293 294 295 #ifdef ASSERT 296 void JNIHandleBlock::zap() { 297 // Zap block values 298 _top = 0; 299 for (int index = 0; index < block_size_in_oops; index++) { 300 _handles[index] = NULL; |