Path: blob/master/src/hotspot/share/runtime/jniHandles.hpp
40951 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_RUNTIME_JNIHANDLES_HPP25#define SHARE_RUNTIME_JNIHANDLES_HPP2627#include "memory/allocation.hpp"28#include "runtime/handles.hpp"2930class JavaThread;31class OopStorage;32class Thread;3334// Interface for creating and resolving local/global JNI handles3536class JNIHandles : AllStatic {37friend class VMStructs;38private:39// These are used by the serviceability agent.40static OopStorage* _global_handles;41static OopStorage* _weak_global_handles;42friend void jni_handles_init();4344static OopStorage* global_handles();45static OopStorage* weak_global_handles();4647inline static bool is_jweak(jobject handle);48inline static oop* jobject_ptr(jobject handle); // NOT jweak!49inline static oop* jweak_ptr(jobject handle);5051template <DecoratorSet decorators, bool external_guard> inline static oop resolve_impl(jobject handle);5253// Resolve handle into oop, without keeping the object alive54inline static oop resolve_no_keepalive(jobject handle);5556// This method is not inlined in order to avoid circular includes between57// this header file and thread.hpp.58static bool current_thread_in_native();5960public:61// Low tag bit in jobject used to distinguish a jweak. jweak is62// type equivalent to jobject, but there are places where we need to63// be able to distinguish jweak values from other jobjects, and64// is_weak_global_handle is unsuitable for performance reasons. To65// provide such a test we add weak_tag_value to the (aligned) byte66// address designated by the jobject to produce the corresponding67// jweak. Accessing the value of a jobject must account for it68// being a possibly offset jweak.69static const uintptr_t weak_tag_size = 1;70static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);71static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;72static const int weak_tag_value = 1;7374// Resolve handle into oop75inline static oop resolve(jobject handle);76// Resolve handle into oop, result guaranteed not to be null77inline static oop resolve_non_null(jobject handle);78// Resolve externally provided handle into oop with some guards79static oop resolve_external_guard(jobject handle);8081// Check for equality without keeping objects alive82static bool is_same_object(jobject handle1, jobject handle2);8384// Local handles85static jobject make_local(oop obj);86static jobject make_local(Thread* thread, oop obj, // Faster version when current thread is known87AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);88inline static void destroy_local(jobject handle);8990// Global handles91static jobject make_global(Handle obj,92AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);93static void destroy_global(jobject handle);9495// Weak global handles96static jobject make_weak_global(Handle obj,97AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);98static void destroy_weak_global(jobject handle);99static bool is_global_weak_cleared(jweak handle); // Test jweak without resolution100101// Debugging102static void print_on(outputStream* st);103static void print();104static void verify();105// The category predicates all require handle != NULL.106static bool is_local_handle(Thread* thread, jobject handle);107static bool is_frame_handle(JavaThread* thread, jobject handle);108static bool is_global_handle(jobject handle);109static bool is_weak_global_handle(jobject handle);110static size_t global_handle_memory_usage();111static size_t weak_global_handle_memory_usage();112113#ifndef PRODUCT114// Is handle from any local block of any thread?115static bool is_local_handle(jobject handle);116#endif117118// precondition: handle != NULL.119static jobjectRefType handle_type(Thread* thread, jobject handle);120121// Garbage collection support(global handles only, local handles are traversed from thread)122// Traversal of regular global handles123static void oops_do(OopClosure* f);124// Traversal of weak global handles. Unreachable oops are cleared.125static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);126// Traversal of weak global handles.127static void weak_oops_do(OopClosure* f);128129static bool is_global_storage(const OopStorage* storage);130};131132133134// JNI handle blocks holding local/global JNI handles135136class JNIHandleBlock : public CHeapObj<mtInternal> {137friend class VMStructs;138friend class ZeroInterpreter;139140private:141enum SomeConstants {142block_size_in_oops = 32 // Number of handles per handle block143};144145uintptr_t _handles[block_size_in_oops]; // The handles146int _top; // Index of next unused handle147JNIHandleBlock* _next; // Link to next block148149// The following instance variables are only used by the first block in a chain.150// Having two types of blocks complicates the code and the space overhead in negligible.151JNIHandleBlock* _last; // Last block in use152JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call153uintptr_t* _free_list; // Handle free list154int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list155156// Check JNI, "planned capacity" for current frame (or push/ensure)157size_t _planned_capacity;158159#ifndef PRODUCT160JNIHandleBlock* _block_list_link; // Link for list below161static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only)162#endif163164static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks165static int _blocks_allocated; // For debugging/printing166167// Fill block with bad_handle values168void zap() NOT_DEBUG_RETURN;169170// Free list computation171void rebuild_free_list();172173// No more handles in the both the current and following blocks174void clear() { _top = 0; }175176public:177// Handle allocation178jobject allocate_handle(oop obj, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);179180// Block allocation and block free list management181static JNIHandleBlock* allocate_block(Thread* thread = NULL, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);182static void release_block(JNIHandleBlock* block, Thread* thread = NULL);183184// JNI PushLocalFrame/PopLocalFrame support185JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; }186void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; }187188// Stub generator support189static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); }190191// Garbage collection support192// Traversal of handles193void oops_do(OopClosure* f);194195// Checked JNI support196void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }197const size_t get_planned_capacity() { return _planned_capacity; }198const size_t get_number_of_live_handles();199200// Debugging201bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle202bool contains(jobject handle) const; // Does this block contain handle203size_t length() const; // Length of chain starting with this block204size_t memory_usage() const;205#ifndef PRODUCT206static bool any_contains(jobject handle); // Does any block currently in use contain handle207static void print_statistics();208#endif209};210211#endif // SHARE_RUNTIME_JNIHANDLES_HPP212213214