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