duke@435: /* coleenp@4037: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP stefank@2314: #define SHARE_VM_RUNTIME_JNIHANDLES_HPP stefank@2314: stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "utilities/top.hpp" stefank@2314: duke@435: class JNIHandleBlock; duke@435: duke@435: duke@435: // Interface for creating and resolving local/global JNI handles duke@435: duke@435: class JNIHandles : AllStatic { duke@435: friend class VMStructs; duke@435: private: duke@435: static JNIHandleBlock* _global_handles; // First global handle block duke@435: static JNIHandleBlock* _weak_global_handles; // First weak global handle block duke@435: static oop _deleted_handle; // Sentinel marking deleted handles duke@435: duke@435: public: duke@435: // Resolve handle into oop duke@435: inline static oop resolve(jobject handle); duke@435: // Resolve externally provided handle into oop with some guards duke@435: inline static oop resolve_external_guard(jobject handle); duke@435: // Resolve handle into oop, result guaranteed not to be null duke@435: inline static oop resolve_non_null(jobject handle); duke@435: duke@435: // Local handles duke@435: static jobject make_local(oop obj); duke@435: static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known duke@435: static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known duke@435: inline static void destroy_local(jobject handle); duke@435: duke@435: // Global handles duke@435: static jobject make_global(Handle obj); duke@435: static void destroy_global(jobject handle); duke@435: duke@435: // Weak global handles duke@435: static jobject make_weak_global(Handle obj); duke@435: static void destroy_weak_global(jobject handle); duke@435: duke@435: // Sentinel marking deleted handles in block. Note that we cannot store NULL as duke@435: // the sentinel, since clearing weak global JNI refs are done by storing NULL in duke@435: // the handle. The handle may not be reused before destroy_weak_global is called. duke@435: static oop deleted_handle() { return _deleted_handle; } duke@435: duke@435: // Initialization duke@435: static void initialize(); duke@435: duke@435: // Debugging duke@435: static void print_on(outputStream* st); duke@435: static void print() { print_on(tty); } duke@435: static void verify(); duke@435: static bool is_local_handle(Thread* thread, jobject handle); duke@435: static bool is_frame_handle(JavaThread* thr, jobject obj); duke@435: static bool is_global_handle(jobject handle); duke@435: static bool is_weak_global_handle(jobject handle); duke@435: static long global_handle_memory_usage(); duke@435: static long weak_global_handle_memory_usage(); duke@435: duke@435: // Garbage collection support(global handles only, local handles are traversed from thread) duke@435: // Traversal of regular global handles duke@435: static void oops_do(OopClosure* f); duke@435: // Traversal of weak global handles. Unreachable oops are cleared. duke@435: static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); duke@435: }; duke@435: duke@435: duke@435: duke@435: // JNI handle blocks holding local/global JNI handles duke@435: zgu@3900: class JNIHandleBlock : public CHeapObj { duke@435: friend class VMStructs; never@1445: friend class CppInterpreter; never@1445: duke@435: private: duke@435: enum SomeConstants { duke@435: block_size_in_oops = 32 // Number of handles per handle block duke@435: }; duke@435: duke@435: oop _handles[block_size_in_oops]; // The handles duke@435: int _top; // Index of next unused handle duke@435: JNIHandleBlock* _next; // Link to next block duke@435: duke@435: // The following instance variables are only used by the first block in a chain. duke@435: // Having two types of blocks complicates the code and the space overhead in negligble. duke@435: JNIHandleBlock* _last; // Last block in use duke@435: JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call duke@435: oop* _free_list; // Handle free list duke@435: int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list duke@435: duke@435: #ifndef PRODUCT duke@435: JNIHandleBlock* _block_list_link; // Link for list below duke@435: static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) duke@435: #endif duke@435: duke@435: static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks duke@435: static int _blocks_allocated; // For debugging/printing duke@435: duke@435: // Fill block with bad_handle values duke@435: void zap(); duke@435: never@1445: protected: duke@435: // No more handles in the both the current and following blocks duke@435: void clear() { _top = 0; } duke@435: never@1445: private: duke@435: // Free list computation duke@435: void rebuild_free_list(); duke@435: duke@435: public: duke@435: // Handle allocation duke@435: jobject allocate_handle(oop obj); duke@435: duke@435: // Block allocation and block free list management duke@435: static JNIHandleBlock* allocate_block(Thread* thread = NULL); duke@435: static void release_block(JNIHandleBlock* block, Thread* thread = NULL); duke@435: duke@435: // JNI PushLocalFrame/PopLocalFrame support duke@435: JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } duke@435: void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } duke@435: duke@435: // Stub generator support duke@435: static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } duke@435: duke@435: // Garbage collection support duke@435: // Traversal of regular handles duke@435: void oops_do(OopClosure* f); duke@435: // Traversal of weak handles. Unreachable oops are cleared. duke@435: void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); duke@435: duke@435: // Debugging duke@435: bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle duke@435: bool contains(jobject handle) const; // Does this block contain handle duke@435: int length() const; // Length of chain starting with this block duke@435: long memory_usage() const; duke@435: #ifndef PRODUCT duke@435: static bool any_contains(jobject handle); // Does any block currently in use contain handle duke@435: static void print_statistics(); duke@435: #endif duke@435: }; duke@435: duke@435: duke@435: inline oop JNIHandles::resolve(jobject handle) { duke@435: oop result = (handle == NULL ? (oop)NULL : *(oop*)handle); duke@435: assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle"); duke@435: assert(result != badJNIHandle, "Pointing to zapped jni handle area"); duke@435: return result; duke@435: }; duke@435: duke@435: duke@435: inline oop JNIHandles::resolve_external_guard(jobject handle) { duke@435: if (handle == NULL) return NULL; duke@435: oop result = *(oop*)handle; duke@435: if (result == NULL || result == badJNIHandle) return NULL; duke@435: return result; duke@435: }; duke@435: duke@435: duke@435: inline oop JNIHandles::resolve_non_null(jobject handle) { duke@435: assert(handle != NULL, "JNI handle should not be null"); duke@435: oop result = *(oop*)handle; duke@435: assert(result != NULL, "Invalid value read from jni handle"); duke@435: assert(result != badJNIHandle, "Pointing to zapped jni handle area"); duke@435: // Don't let that private _deleted_handle object escape into the wild. duke@435: assert(result != deleted_handle(), "Used a deleted global handle."); duke@435: return result; duke@435: }; duke@435: duke@435: inline void JNIHandles::destroy_local(jobject handle) { duke@435: if (handle != NULL) { duke@435: *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it duke@435: } duke@435: } stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP