Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
poonam@7627 | 2 | * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP |
aoqi@0 | 26 | #define SHARE_VM_RUNTIME_JNIHANDLES_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "runtime/handles.hpp" |
aoqi@0 | 29 | #include "utilities/top.hpp" |
aoqi@0 | 30 | |
aoqi@0 | 31 | class JNIHandleBlock; |
aoqi@0 | 32 | |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Interface for creating and resolving local/global JNI handles |
aoqi@0 | 35 | |
aoqi@0 | 36 | class JNIHandles : AllStatic { |
aoqi@0 | 37 | friend class VMStructs; |
aoqi@0 | 38 | private: |
aoqi@0 | 39 | static JNIHandleBlock* _global_handles; // First global handle block |
aoqi@0 | 40 | static JNIHandleBlock* _weak_global_handles; // First weak global handle block |
aoqi@0 | 41 | static oop _deleted_handle; // Sentinel marking deleted handles |
aoqi@0 | 42 | |
aoqi@0 | 43 | public: |
aoqi@0 | 44 | // Resolve handle into oop |
aoqi@0 | 45 | inline static oop resolve(jobject handle); |
aoqi@0 | 46 | // Resolve externally provided handle into oop with some guards |
aoqi@0 | 47 | inline static oop resolve_external_guard(jobject handle); |
aoqi@0 | 48 | // Resolve handle into oop, result guaranteed not to be null |
aoqi@0 | 49 | inline static oop resolve_non_null(jobject handle); |
aoqi@0 | 50 | |
aoqi@0 | 51 | // Local handles |
aoqi@0 | 52 | static jobject make_local(oop obj); |
aoqi@0 | 53 | static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known |
aoqi@0 | 54 | static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known |
aoqi@0 | 55 | inline static void destroy_local(jobject handle); |
aoqi@0 | 56 | |
aoqi@0 | 57 | // Global handles |
aoqi@0 | 58 | static jobject make_global(Handle obj); |
aoqi@0 | 59 | static void destroy_global(jobject handle); |
aoqi@0 | 60 | |
aoqi@0 | 61 | // Weak global handles |
aoqi@0 | 62 | static jobject make_weak_global(Handle obj); |
aoqi@0 | 63 | static void destroy_weak_global(jobject handle); |
aoqi@0 | 64 | |
aoqi@0 | 65 | // Sentinel marking deleted handles in block. Note that we cannot store NULL as |
aoqi@0 | 66 | // the sentinel, since clearing weak global JNI refs are done by storing NULL in |
aoqi@0 | 67 | // the handle. The handle may not be reused before destroy_weak_global is called. |
aoqi@0 | 68 | static oop deleted_handle() { return _deleted_handle; } |
aoqi@0 | 69 | |
aoqi@0 | 70 | // Initialization |
aoqi@0 | 71 | static void initialize(); |
aoqi@0 | 72 | |
aoqi@0 | 73 | // Debugging |
aoqi@0 | 74 | static void print_on(outputStream* st); |
aoqi@0 | 75 | static void print() { print_on(tty); } |
aoqi@0 | 76 | static void verify(); |
aoqi@0 | 77 | static bool is_local_handle(Thread* thread, jobject handle); |
aoqi@0 | 78 | static bool is_frame_handle(JavaThread* thr, jobject obj); |
aoqi@0 | 79 | static bool is_global_handle(jobject handle); |
aoqi@0 | 80 | static bool is_weak_global_handle(jobject handle); |
aoqi@0 | 81 | static long global_handle_memory_usage(); |
aoqi@0 | 82 | static long weak_global_handle_memory_usage(); |
aoqi@0 | 83 | |
aoqi@0 | 84 | // Garbage collection support(global handles only, local handles are traversed from thread) |
aoqi@0 | 85 | // Traversal of regular global handles |
aoqi@0 | 86 | static void oops_do(OopClosure* f); |
aoqi@0 | 87 | // Traversal of weak global handles. Unreachable oops are cleared. |
aoqi@0 | 88 | static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); |
aoqi@0 | 89 | }; |
aoqi@0 | 90 | |
aoqi@0 | 91 | |
aoqi@0 | 92 | |
aoqi@0 | 93 | // JNI handle blocks holding local/global JNI handles |
aoqi@0 | 94 | |
aoqi@0 | 95 | class JNIHandleBlock : public CHeapObj<mtInternal> { |
aoqi@0 | 96 | friend class VMStructs; |
aoqi@0 | 97 | friend class CppInterpreter; |
aoqi@0 | 98 | |
aoqi@0 | 99 | private: |
aoqi@0 | 100 | enum SomeConstants { |
aoqi@0 | 101 | block_size_in_oops = 32 // Number of handles per handle block |
aoqi@0 | 102 | }; |
aoqi@0 | 103 | |
aoqi@0 | 104 | oop _handles[block_size_in_oops]; // The handles |
aoqi@0 | 105 | int _top; // Index of next unused handle |
aoqi@0 | 106 | JNIHandleBlock* _next; // Link to next block |
aoqi@0 | 107 | |
aoqi@0 | 108 | // The following instance variables are only used by the first block in a chain. |
aoqi@0 | 109 | // Having two types of blocks complicates the code and the space overhead in negligble. |
aoqi@0 | 110 | JNIHandleBlock* _last; // Last block in use |
aoqi@0 | 111 | JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call |
aoqi@0 | 112 | oop* _free_list; // Handle free list |
aoqi@0 | 113 | int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list |
aoqi@0 | 114 | |
poonam@7627 | 115 | // Check JNI, "planned capacity" for current frame (or push/ensure) |
poonam@7627 | 116 | size_t _planned_capacity; |
poonam@7627 | 117 | |
aoqi@0 | 118 | #ifndef PRODUCT |
aoqi@0 | 119 | JNIHandleBlock* _block_list_link; // Link for list below |
aoqi@0 | 120 | static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) |
aoqi@0 | 121 | #endif |
aoqi@0 | 122 | |
aoqi@0 | 123 | static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks |
aoqi@0 | 124 | static int _blocks_allocated; // For debugging/printing |
aoqi@0 | 125 | |
aoqi@0 | 126 | // Fill block with bad_handle values |
aoqi@0 | 127 | void zap(); |
aoqi@0 | 128 | |
aoqi@0 | 129 | protected: |
aoqi@0 | 130 | // No more handles in the both the current and following blocks |
aoqi@0 | 131 | void clear() { _top = 0; } |
aoqi@0 | 132 | |
aoqi@0 | 133 | private: |
aoqi@0 | 134 | // Free list computation |
aoqi@0 | 135 | void rebuild_free_list(); |
aoqi@0 | 136 | |
aoqi@0 | 137 | public: |
aoqi@0 | 138 | // Handle allocation |
aoqi@0 | 139 | jobject allocate_handle(oop obj); |
aoqi@0 | 140 | |
aoqi@0 | 141 | // Block allocation and block free list management |
aoqi@0 | 142 | static JNIHandleBlock* allocate_block(Thread* thread = NULL); |
aoqi@0 | 143 | static void release_block(JNIHandleBlock* block, Thread* thread = NULL); |
aoqi@0 | 144 | |
aoqi@0 | 145 | // JNI PushLocalFrame/PopLocalFrame support |
aoqi@0 | 146 | JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } |
aoqi@0 | 147 | void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } |
aoqi@0 | 148 | |
aoqi@0 | 149 | // Stub generator support |
aoqi@0 | 150 | static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } |
aoqi@0 | 151 | |
aoqi@0 | 152 | // Garbage collection support |
aoqi@0 | 153 | // Traversal of regular handles |
aoqi@0 | 154 | void oops_do(OopClosure* f); |
aoqi@0 | 155 | // Traversal of weak handles. Unreachable oops are cleared. |
aoqi@0 | 156 | void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); |
aoqi@0 | 157 | |
poonam@7627 | 158 | // Checked JNI support |
poonam@7627 | 159 | void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; } |
poonam@7627 | 160 | const size_t get_planned_capacity() { return _planned_capacity; } |
poonam@7627 | 161 | const size_t get_number_of_live_handles(); |
poonam@7627 | 162 | |
aoqi@0 | 163 | // Debugging |
aoqi@0 | 164 | bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle |
aoqi@0 | 165 | bool contains(jobject handle) const; // Does this block contain handle |
aoqi@0 | 166 | int length() const; // Length of chain starting with this block |
aoqi@0 | 167 | long memory_usage() const; |
aoqi@0 | 168 | #ifndef PRODUCT |
aoqi@0 | 169 | static bool any_contains(jobject handle); // Does any block currently in use contain handle |
aoqi@0 | 170 | static void print_statistics(); |
aoqi@0 | 171 | #endif |
aoqi@0 | 172 | }; |
aoqi@0 | 173 | |
aoqi@0 | 174 | |
aoqi@0 | 175 | inline oop JNIHandles::resolve(jobject handle) { |
aoqi@0 | 176 | oop result = (handle == NULL ? (oop)NULL : *(oop*)handle); |
aoqi@0 | 177 | assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle"); |
aoqi@0 | 178 | assert(result != badJNIHandle, "Pointing to zapped jni handle area"); |
aoqi@0 | 179 | return result; |
aoqi@0 | 180 | }; |
aoqi@0 | 181 | |
aoqi@0 | 182 | |
aoqi@0 | 183 | inline oop JNIHandles::resolve_external_guard(jobject handle) { |
aoqi@0 | 184 | if (handle == NULL) return NULL; |
aoqi@0 | 185 | oop result = *(oop*)handle; |
aoqi@0 | 186 | if (result == NULL || result == badJNIHandle) return NULL; |
aoqi@0 | 187 | return result; |
aoqi@0 | 188 | }; |
aoqi@0 | 189 | |
aoqi@0 | 190 | |
aoqi@0 | 191 | inline oop JNIHandles::resolve_non_null(jobject handle) { |
aoqi@0 | 192 | assert(handle != NULL, "JNI handle should not be null"); |
aoqi@0 | 193 | oop result = *(oop*)handle; |
aoqi@0 | 194 | assert(result != NULL, "Invalid value read from jni handle"); |
aoqi@0 | 195 | assert(result != badJNIHandle, "Pointing to zapped jni handle area"); |
aoqi@0 | 196 | // Don't let that private _deleted_handle object escape into the wild. |
aoqi@0 | 197 | assert(result != deleted_handle(), "Used a deleted global handle."); |
aoqi@0 | 198 | return result; |
aoqi@0 | 199 | }; |
aoqi@0 | 200 | |
aoqi@0 | 201 | inline void JNIHandles::destroy_local(jobject handle) { |
aoqi@0 | 202 | if (handle != NULL) { |
aoqi@0 | 203 | *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it |
aoqi@0 | 204 | } |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP |