aoqi@0: /* poonam@7627: * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/jvmtiExport.hpp" aoqi@0: #include "runtime/jniHandles.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: JNIHandleBlock* JNIHandles::_global_handles = NULL; aoqi@0: JNIHandleBlock* JNIHandles::_weak_global_handles = NULL; aoqi@0: oop JNIHandles::_deleted_handle = NULL; aoqi@0: aoqi@0: aoqi@0: jobject JNIHandles::make_local(oop obj) { aoqi@0: if (obj == NULL) { aoqi@0: return NULL; // ignore null handles aoqi@0: } else { aoqi@0: Thread* thread = Thread::current(); aoqi@0: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); aoqi@0: return thread->active_handles()->allocate_handle(obj); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // optimized versions aoqi@0: aoqi@0: jobject JNIHandles::make_local(Thread* thread, oop obj) { aoqi@0: if (obj == NULL) { aoqi@0: return NULL; // ignore null handles aoqi@0: } else { aoqi@0: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); aoqi@0: return thread->active_handles()->allocate_handle(obj); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: jobject JNIHandles::make_local(JNIEnv* env, oop obj) { aoqi@0: if (obj == NULL) { aoqi@0: return NULL; // ignore null handles aoqi@0: } else { aoqi@0: JavaThread* thread = JavaThread::thread_from_jni_environment(env); aoqi@0: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); aoqi@0: return thread->active_handles()->allocate_handle(obj); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: jobject JNIHandles::make_global(Handle obj) { aoqi@0: assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); aoqi@0: jobject res = NULL; aoqi@0: if (!obj.is_null()) { aoqi@0: // ignore null handles aoqi@0: MutexLocker ml(JNIGlobalHandle_lock); aoqi@0: assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); aoqi@0: res = _global_handles->allocate_handle(obj()); aoqi@0: } else { aoqi@0: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); aoqi@0: } aoqi@0: aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: jobject JNIHandles::make_weak_global(Handle obj) { aoqi@0: assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); aoqi@0: jobject res = NULL; aoqi@0: if (!obj.is_null()) { aoqi@0: // ignore null handles aoqi@0: MutexLocker ml(JNIGlobalHandle_lock); aoqi@0: assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); aoqi@0: res = _weak_global_handles->allocate_handle(obj()); aoqi@0: } else { aoqi@0: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandles::destroy_global(jobject handle) { aoqi@0: if (handle != NULL) { aoqi@0: assert(is_global_handle(handle), "Invalid delete of global JNI handle"); aoqi@0: *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandles::destroy_weak_global(jobject handle) { aoqi@0: if (handle != NULL) { aoqi@0: assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle"); aoqi@0: *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandles::oops_do(OopClosure* f) { aoqi@0: f->do_oop(&_deleted_handle); aoqi@0: _global_handles->oops_do(f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { aoqi@0: _weak_global_handles->weak_oops_do(is_alive, f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandles::initialize() { aoqi@0: _global_handles = JNIHandleBlock::allocate_block(); aoqi@0: _weak_global_handles = JNIHandleBlock::allocate_block(); aoqi@0: EXCEPTION_MARK; aoqi@0: // We will never reach the CATCH below since Exceptions::_throw will cause aoqi@0: // the VM to exit if an exception is thrown during initialization aoqi@0: Klass* k = SystemDictionary::Object_klass(); aoqi@0: _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { aoqi@0: JNIHandleBlock* block = thread->active_handles(); aoqi@0: aoqi@0: // Look back past possible native calls to jni_PushLocalFrame. aoqi@0: while (block != NULL) { aoqi@0: if (block->chain_contains(handle)) { aoqi@0: return true; aoqi@0: } aoqi@0: block = block->pop_frame_link(); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Determine if the handle is somewhere in the current thread's stack. aoqi@0: // We easily can't isolate any particular stack frame the handle might aoqi@0: // come from, so we'll check the whole stack. aoqi@0: aoqi@0: bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) { aoqi@0: // If there is no java frame, then this must be top level code, such aoqi@0: // as the java command executable, in which case, this type of handle aoqi@0: // is not permitted. aoqi@0: return (thr->has_last_Java_frame() && aoqi@0: (void*)obj < (void*)thr->stack_base() && aoqi@0: (void*)obj >= (void*)thr->last_Java_sp()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JNIHandles::is_global_handle(jobject handle) { aoqi@0: return _global_handles->chain_contains(handle); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JNIHandles::is_weak_global_handle(jobject handle) { aoqi@0: return _weak_global_handles->chain_contains(handle); aoqi@0: } aoqi@0: aoqi@0: long JNIHandles::global_handle_memory_usage() { aoqi@0: return _global_handles->memory_usage(); aoqi@0: } aoqi@0: aoqi@0: long JNIHandles::weak_global_handle_memory_usage() { aoqi@0: return _weak_global_handles->memory_usage(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class AlwaysAliveClosure: public BoolObjectClosure { aoqi@0: public: aoqi@0: bool do_object_b(oop obj) { return true; } aoqi@0: }; aoqi@0: aoqi@0: class CountHandleClosure: public OopClosure { aoqi@0: private: aoqi@0: int _count; aoqi@0: public: aoqi@0: CountHandleClosure(): _count(0) {} aoqi@0: virtual void do_oop(oop* unused) { aoqi@0: _count++; aoqi@0: } aoqi@0: virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } aoqi@0: int count() { return _count; } aoqi@0: }; aoqi@0: aoqi@0: // We assume this is called at a safepoint: no lock is needed. aoqi@0: void JNIHandles::print_on(outputStream* st) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); aoqi@0: assert(_global_handles != NULL && _weak_global_handles != NULL, aoqi@0: "JNIHandles not initialized"); aoqi@0: aoqi@0: CountHandleClosure global_handle_count; aoqi@0: AlwaysAliveClosure always_alive; aoqi@0: oops_do(&global_handle_count); aoqi@0: weak_oops_do(&always_alive, &global_handle_count); aoqi@0: aoqi@0: st->print_cr("JNI global references: %d", global_handle_count.count()); aoqi@0: st->cr(); aoqi@0: st->flush(); aoqi@0: } aoqi@0: aoqi@0: class VerifyHandleClosure: public OopClosure { aoqi@0: public: aoqi@0: virtual void do_oop(oop* root) { aoqi@0: (*root)->verify(); aoqi@0: } aoqi@0: virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } aoqi@0: }; aoqi@0: aoqi@0: void JNIHandles::verify() { aoqi@0: VerifyHandleClosure verify_handle; aoqi@0: AlwaysAliveClosure always_alive; aoqi@0: aoqi@0: oops_do(&verify_handle); aoqi@0: weak_oops_do(&always_alive, &verify_handle); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: void jni_handles_init() { aoqi@0: JNIHandles::initialize(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int JNIHandleBlock::_blocks_allocated = 0; aoqi@0: JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; aoqi@0: #ifndef PRODUCT aoqi@0: JNIHandleBlock* JNIHandleBlock::_block_list = NULL; aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: void JNIHandleBlock::zap() { aoqi@0: // Zap block values aoqi@0: _top = 0; aoqi@0: for (int index = 0; index < block_size_in_oops; index++) { aoqi@0: _handles[index] = badJNIHandle; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { aoqi@0: assert(thread == NULL || thread == Thread::current(), "sanity check"); aoqi@0: JNIHandleBlock* block; aoqi@0: // Check the thread-local free list for a block so we don't aoqi@0: // have to acquire a mutex. aoqi@0: if (thread != NULL && thread->free_handle_block() != NULL) { aoqi@0: block = thread->free_handle_block(); aoqi@0: thread->set_free_handle_block(block->_next); aoqi@0: } aoqi@0: else { aoqi@0: // locking with safepoint checking introduces a potential deadlock: aoqi@0: // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock aoqi@0: // - another would hold Threads_lock (jni_AttachCurrentThread) and then aoqi@0: // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) aoqi@0: MutexLockerEx ml(JNIHandleBlockFreeList_lock, aoqi@0: Mutex::_no_safepoint_check_flag); aoqi@0: if (_block_free_list == NULL) { aoqi@0: // Allocate new block aoqi@0: block = new JNIHandleBlock(); aoqi@0: _blocks_allocated++; aoqi@0: if (TraceJNIHandleAllocation) { aoqi@0: tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)", aoqi@0: block, _blocks_allocated); aoqi@0: } aoqi@0: if (ZapJNIHandleArea) block->zap(); aoqi@0: #ifndef PRODUCT aoqi@0: // Link new block to list of all allocated blocks aoqi@0: block->_block_list_link = _block_list; aoqi@0: _block_list = block; aoqi@0: #endif aoqi@0: } else { aoqi@0: // Get block from free list aoqi@0: block = _block_free_list; aoqi@0: _block_free_list = _block_free_list->_next; aoqi@0: } aoqi@0: } aoqi@0: block->_top = 0; aoqi@0: block->_next = NULL; aoqi@0: block->_pop_frame_link = NULL; poonam@7627: block->_planned_capacity = block_size_in_oops; aoqi@0: // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle aoqi@0: debug_only(block->_last = NULL); aoqi@0: debug_only(block->_free_list = NULL); aoqi@0: debug_only(block->_allocate_before_rebuild = -1); aoqi@0: return block; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { aoqi@0: assert(thread == NULL || thread == Thread::current(), "sanity check"); aoqi@0: JNIHandleBlock* pop_frame_link = block->pop_frame_link(); aoqi@0: // Put returned block at the beginning of the thread-local free list. aoqi@0: // Note that if thread == NULL, we use it as an implicit argument that aoqi@0: // we _don't_ want the block to be kept on the free_handle_block. aoqi@0: // See for instance JavaThread::exit(). aoqi@0: if (thread != NULL ) { aoqi@0: if (ZapJNIHandleArea) block->zap(); aoqi@0: JNIHandleBlock* freelist = thread->free_handle_block(); aoqi@0: block->_pop_frame_link = NULL; aoqi@0: thread->set_free_handle_block(block); aoqi@0: aoqi@0: // Add original freelist to end of chain aoqi@0: if ( freelist != NULL ) { aoqi@0: while ( block->_next != NULL ) block = block->_next; aoqi@0: block->_next = freelist; aoqi@0: } aoqi@0: block = NULL; aoqi@0: } aoqi@0: if (block != NULL) { aoqi@0: // Return blocks to free list aoqi@0: // locking with safepoint checking introduces a potential deadlock: aoqi@0: // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock aoqi@0: // - another would hold Threads_lock (jni_AttachCurrentThread) and then aoqi@0: // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) aoqi@0: MutexLockerEx ml(JNIHandleBlockFreeList_lock, aoqi@0: Mutex::_no_safepoint_check_flag); aoqi@0: while (block != NULL) { aoqi@0: if (ZapJNIHandleArea) block->zap(); aoqi@0: JNIHandleBlock* next = block->_next; aoqi@0: block->_next = _block_free_list; aoqi@0: _block_free_list = block; aoqi@0: block = next; aoqi@0: } aoqi@0: } aoqi@0: if (pop_frame_link != NULL) { aoqi@0: // As a sanity check we release blocks pointed to by the pop_frame_link. aoqi@0: // This should never happen (only if PopLocalFrame is not called the aoqi@0: // correct number of times). aoqi@0: release_block(pop_frame_link, thread); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandleBlock::oops_do(OopClosure* f) { aoqi@0: JNIHandleBlock* current_chain = this; aoqi@0: // Iterate over chain of blocks, followed by chains linked through the aoqi@0: // pop frame links. aoqi@0: while (current_chain != NULL) { aoqi@0: for (JNIHandleBlock* current = current_chain; current != NULL; aoqi@0: current = current->_next) { aoqi@0: assert(current == current_chain || current->pop_frame_link() == NULL, aoqi@0: "only blocks first in chain should have pop frame link set"); aoqi@0: for (int index = 0; index < current->_top; index++) { aoqi@0: oop* root = &(current->_handles)[index]; aoqi@0: oop value = *root; aoqi@0: // traverse heap pointers only, not deleted handles or free list aoqi@0: // pointers aoqi@0: if (value != NULL && Universe::heap()->is_in_reserved(value)) { aoqi@0: f->do_oop(root); aoqi@0: } aoqi@0: } aoqi@0: // the next handle block is valid only if current block is full aoqi@0: if (current->_top < block_size_in_oops) { aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: current_chain = current_chain->pop_frame_link(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive, aoqi@0: OopClosure* f) { aoqi@0: for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { aoqi@0: assert(current->pop_frame_link() == NULL, aoqi@0: "blocks holding weak global JNI handles should not have pop frame link set"); aoqi@0: for (int index = 0; index < current->_top; index++) { aoqi@0: oop* root = &(current->_handles)[index]; aoqi@0: oop value = *root; aoqi@0: // traverse heap pointers only, not deleted handles or free list pointers aoqi@0: if (value != NULL && Universe::heap()->is_in_reserved(value)) { aoqi@0: if (is_alive->do_object_b(value)) { aoqi@0: // The weakly referenced object is alive, update pointer aoqi@0: f->do_oop(root); aoqi@0: } else { aoqi@0: // The weakly referenced object is not alive, clear the reference by storing NULL aoqi@0: if (TraceReferenceGC) { aoqi@0: tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root); aoqi@0: } aoqi@0: *root = NULL; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // the next handle block is valid only if current block is full aoqi@0: if (current->_top < block_size_in_oops) { aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * JVMTI data structures may also contain weak oops. The iteration of them aoqi@0: * is placed here so that we don't need to add it to each of the collectors. aoqi@0: */ aoqi@0: JvmtiExport::weak_oops_do(is_alive, f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: jobject JNIHandleBlock::allocate_handle(oop obj) { aoqi@0: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); aoqi@0: if (_top == 0) { aoqi@0: // This is the first allocation or the initial block got zapped when aoqi@0: // entering a native function. If we have any following blocks they are aoqi@0: // not valid anymore. aoqi@0: for (JNIHandleBlock* current = _next; current != NULL; aoqi@0: current = current->_next) { aoqi@0: assert(current->_last == NULL, "only first block should have _last set"); aoqi@0: assert(current->_free_list == NULL, aoqi@0: "only first block should have _free_list set"); aoqi@0: current->_top = 0; aoqi@0: if (ZapJNIHandleArea) current->zap(); aoqi@0: } aoqi@0: // Clear initial block aoqi@0: _free_list = NULL; aoqi@0: _allocate_before_rebuild = 0; aoqi@0: _last = this; aoqi@0: if (ZapJNIHandleArea) zap(); aoqi@0: } aoqi@0: aoqi@0: // Try last block aoqi@0: if (_last->_top < block_size_in_oops) { aoqi@0: oop* handle = &(_last->_handles)[_last->_top++]; aoqi@0: *handle = obj; aoqi@0: return (jobject) handle; aoqi@0: } aoqi@0: aoqi@0: // Try free list aoqi@0: if (_free_list != NULL) { aoqi@0: oop* handle = _free_list; aoqi@0: _free_list = (oop*) *_free_list; aoqi@0: *handle = obj; aoqi@0: return (jobject) handle; aoqi@0: } aoqi@0: // Check if unused block follow last aoqi@0: if (_last->_next != NULL) { aoqi@0: // update last and retry aoqi@0: _last = _last->_next; aoqi@0: return allocate_handle(obj); aoqi@0: } aoqi@0: aoqi@0: // No space available, we have to rebuild free list or expand aoqi@0: if (_allocate_before_rebuild == 0) { aoqi@0: rebuild_free_list(); // updates _allocate_before_rebuild counter aoqi@0: } else { aoqi@0: // Append new block aoqi@0: Thread* thread = Thread::current(); aoqi@0: Handle obj_handle(thread, obj); aoqi@0: // This can block, so we need to preserve obj accross call. aoqi@0: _last->_next = JNIHandleBlock::allocate_block(thread); aoqi@0: _last = _last->_next; aoqi@0: _allocate_before_rebuild--; aoqi@0: obj = obj_handle(); aoqi@0: } aoqi@0: return allocate_handle(obj); // retry aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void JNIHandleBlock::rebuild_free_list() { aoqi@0: assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); aoqi@0: int free = 0; aoqi@0: int blocks = 0; aoqi@0: for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { aoqi@0: for (int index = 0; index < current->_top; index++) { aoqi@0: oop* handle = &(current->_handles)[index]; aoqi@0: if (*handle == JNIHandles::deleted_handle()) { aoqi@0: // this handle was cleared out by a delete call, reuse it aoqi@0: *handle = (oop) _free_list; aoqi@0: _free_list = handle; aoqi@0: free++; aoqi@0: } aoqi@0: } aoqi@0: // we should not rebuild free list if there are unused handles at the end aoqi@0: assert(current->_top == block_size_in_oops, "just checking"); aoqi@0: blocks++; aoqi@0: } aoqi@0: // Heuristic: if more than half of the handles are free we rebuild next time aoqi@0: // as well, otherwise we append a corresponding number of new blocks before aoqi@0: // attempting a free list rebuild again. aoqi@0: int total = blocks * block_size_in_oops; aoqi@0: int extra = total - 2*free; aoqi@0: if (extra > 0) { aoqi@0: // Not as many free handles as we would like - compute number of new blocks to append aoqi@0: _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; aoqi@0: } aoqi@0: if (TraceJNIHandleAllocation) { aoqi@0: tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d", aoqi@0: this, blocks, total-free, free, _allocate_before_rebuild); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JNIHandleBlock::contains(jobject handle) const { aoqi@0: return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool JNIHandleBlock::chain_contains(jobject handle) const { aoqi@0: for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { aoqi@0: if (current->contains(handle)) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int JNIHandleBlock::length() const { aoqi@0: int result = 1; aoqi@0: for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { aoqi@0: result++; aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: poonam@7627: const size_t JNIHandleBlock::get_number_of_live_handles() { poonam@7627: CountHandleClosure counter; poonam@7627: oops_do(&counter); poonam@7627: return counter.count(); poonam@7627: } poonam@7627: aoqi@0: // This method is not thread-safe, i.e., must be called whule holding a lock on the aoqi@0: // structure. aoqi@0: long JNIHandleBlock::memory_usage() const { aoqi@0: return length() * sizeof(JNIHandleBlock); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: bool JNIHandleBlock::any_contains(jobject handle) { aoqi@0: for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { aoqi@0: if (current->contains(handle)) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void JNIHandleBlock::print_statistics() { aoqi@0: int used_blocks = 0; aoqi@0: int free_blocks = 0; aoqi@0: int used_handles = 0; aoqi@0: int free_handles = 0; aoqi@0: JNIHandleBlock* block = _block_list; aoqi@0: while (block != NULL) { aoqi@0: if (block->_top > 0) { aoqi@0: used_blocks++; aoqi@0: } else { aoqi@0: free_blocks++; aoqi@0: } aoqi@0: used_handles += block->_top; aoqi@0: free_handles += (block_size_in_oops - block->_top); aoqi@0: block = block->_block_list_link; aoqi@0: } aoqi@0: tty->print_cr("JNIHandleBlocks statistics"); aoqi@0: tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); aoqi@0: tty->print_cr("- blocks in use: %d", used_blocks); aoqi@0: tty->print_cr("- blocks free: %d", free_blocks); aoqi@0: tty->print_cr("- handles in use: %d", used_handles); aoqi@0: tty->print_cr("- handles free: %d", free_handles); aoqi@0: } aoqi@0: aoqi@0: #endif