Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/jniHandles.hpp
32285 views
/*1* Copyright (c) 1998, 2017, 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_VM_RUNTIME_JNIHANDLES_HPP25#define SHARE_VM_RUNTIME_JNIHANDLES_HPP2627#include "runtime/handles.hpp"28#include "utilities/top.hpp"2930class JNIHandleBlock;313233// Interface for creating and resolving local/global JNI handles3435class JNIHandles : AllStatic {36friend class VMStructs;37private:38static JNIHandleBlock* _global_handles; // First global handle block39static JNIHandleBlock* _weak_global_handles; // First weak global handle block40static oop _deleted_handle; // Sentinel marking deleted handles4142inline static bool is_jweak(jobject handle);43inline static oop& jobject_ref(jobject handle); // NOT jweak!44inline static oop& jweak_ref(jobject handle);4546template<bool external_guard> inline static oop guard_value(oop value);47template<bool external_guard> inline static oop resolve_impl(jobject handle);48template<bool external_guard> static oop resolve_jweak(jweak handle);4950public:51// Low tag bit in jobject used to distinguish a jweak. jweak is52// type equivalent to jobject, but there are places where we need to53// be able to distinguish jweak values from other jobjects, and54// is_weak_global_handle is unsuitable for performance reasons. To55// provide such a test we add weak_tag_value to the (aligned) byte56// address designated by the jobject to produce the corresponding57// jweak. Accessing the value of a jobject must account for it58// being a possibly offset jweak.59static const uintptr_t weak_tag_size = 1;60static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);61static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;62static const int weak_tag_value = 1;6364// Resolve handle into oop65inline static oop resolve(jobject handle);66// Resolve externally provided handle into oop with some guards67inline static oop resolve_external_guard(jobject handle);68// Resolve handle into oop, result guaranteed not to be null69inline static oop resolve_non_null(jobject handle);7071// Local handles72static jobject make_local(oop obj);73static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known74static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known75inline static void destroy_local(jobject handle);7677// Global handles78static jobject make_global(Handle obj);79static void destroy_global(jobject handle);8081// Weak global handles82static jobject make_weak_global(Handle obj);83static void destroy_weak_global(jobject handle);8485// Sentinel marking deleted handles in block. Note that we cannot store NULL as86// the sentinel, since clearing weak global JNI refs are done by storing NULL in87// the handle. The handle may not be reused before destroy_weak_global is called.88static oop deleted_handle() { return _deleted_handle; }8990// Initialization91static void initialize();9293// Debugging94static void print_on(outputStream* st);95static void print() { print_on(tty); }96static void verify();97static bool is_local_handle(Thread* thread, jobject handle);98static bool is_frame_handle(JavaThread* thr, jobject obj);99static bool is_global_handle(jobject handle);100static bool is_weak_global_handle(jobject handle);101static long global_handle_memory_usage();102static long weak_global_handle_memory_usage();103104// Garbage collection support(global handles only, local handles are traversed from thread)105// Traversal of regular global handles106static void oops_do(OopClosure* f);107// Traversal of weak global handles. Unreachable oops are cleared.108static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);109// Traversal of weak global handles.110static void weak_oops_do(OopClosure* f);111};112113114115// JNI handle blocks holding local/global JNI handles116117class JNIHandleBlock : public CHeapObj<mtInternal> {118friend class VMStructs;119friend class CppInterpreter;120121private:122enum SomeConstants {123block_size_in_oops = 32 // Number of handles per handle block124};125126oop _handles[block_size_in_oops]; // The handles127int _top; // Index of next unused handle128JNIHandleBlock* _next; // Link to next block129130// The following instance variables are only used by the first block in a chain.131// Having two types of blocks complicates the code and the space overhead in negligble.132JNIHandleBlock* _last; // Last block in use133JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call134oop* _free_list; // Handle free list135int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list136137// Check JNI, "planned capacity" for current frame (or push/ensure)138size_t _planned_capacity;139140#ifndef PRODUCT141JNIHandleBlock* _block_list_link; // Link for list below142static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only)143#endif144145static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks146static int _blocks_allocated; // For debugging/printing147148// Fill block with bad_handle values149void zap();150151protected:152// No more handles in the both the current and following blocks153void clear() { _top = 0; }154155private:156// Free list computation157void rebuild_free_list();158159public:160// Handle allocation161jobject allocate_handle(oop obj);162163// Block allocation and block free list management164static JNIHandleBlock* allocate_block(Thread* thread = NULL);165static void release_block(JNIHandleBlock* block, Thread* thread = NULL);166167// JNI PushLocalFrame/PopLocalFrame support168JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; }169void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; }170171// Stub generator support172static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); }173174// Garbage collection support175// Traversal of regular handles176void oops_do(OopClosure* f);177// Traversal of weak handles. Unreachable oops are cleared.178void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);179180// Checked JNI support181void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }182const size_t get_planned_capacity() { return _planned_capacity; }183const size_t get_number_of_live_handles();184185// Debugging186bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle187bool contains(jobject handle) const; // Does this block contain handle188int length() const; // Length of chain starting with this block189long memory_usage() const;190#ifndef PRODUCT191static bool any_contains(jobject handle); // Does any block currently in use contain handle192static void print_statistics();193#endif194};195196inline bool JNIHandles::is_jweak(jobject handle) {197STATIC_ASSERT(weak_tag_size == 1);198STATIC_ASSERT(weak_tag_value == 1);199return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;200}201202inline oop& JNIHandles::jobject_ref(jobject handle) {203assert(!is_jweak(handle), "precondition");204return *reinterpret_cast<oop*>(handle);205}206207inline oop& JNIHandles::jweak_ref(jobject handle) {208assert(is_jweak(handle), "precondition");209char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;210return *reinterpret_cast<oop*>(ptr);211}212213// external_guard is true if called from resolve_external_guard.214// Treat deleted (and possibly zapped) as NULL for external_guard,215// else as (asserted) error.216template<bool external_guard>217inline oop JNIHandles::guard_value(oop value) {218if (!external_guard) {219assert(value != badJNIHandle, "Pointing to zapped jni handle area");220assert(value != deleted_handle(), "Used a deleted global handle");221} else if ((value == badJNIHandle) || (value == deleted_handle())) {222value = NULL;223}224return value;225}226227// external_guard is true if called from resolve_external_guard.228template<bool external_guard>229inline oop JNIHandles::resolve_impl(jobject handle) {230assert(handle != NULL, "precondition");231oop result;232if (is_jweak(handle)) { // Unlikely233result = resolve_jweak<external_guard>(handle);234} else {235result = jobject_ref(handle);236// Construction of jobjects canonicalize a null value into a null237// jobject, so for non-jweak the pointee should never be null.238assert(external_guard || result != NULL,239"Invalid value read from jni handle");240result = guard_value<external_guard>(result);241}242return result;243}244245inline oop JNIHandles::resolve(jobject handle) {246oop result = NULL;247if (handle != NULL) {248result = resolve_impl<false /* external_guard */ >(handle);249}250return result;251}252253// Resolve some erroneous cases to NULL, rather than treating them as254// possibly unchecked errors. In particular, deleted handles are255// treated as NULL (though a deleted and later reallocated handle256// isn't detected).257inline oop JNIHandles::resolve_external_guard(jobject handle) {258oop result = NULL;259if (handle != NULL) {260result = resolve_impl<true /* external_guard */ >(handle);261}262return result;263}264265inline oop JNIHandles::resolve_non_null(jobject handle) {266assert(handle != NULL, "JNI handle should not be null");267oop result = resolve_impl<false /* external_guard */ >(handle);268assert(result != NULL, "NULL read from jni handle");269return result;270}271272inline void JNIHandles::destroy_local(jobject handle) {273if (handle != NULL) {274jobject_ref(handle) = deleted_handle();275}276}277278#endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP279280281