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