src/share/vm/runtime/jniHandles.hpp

Thu, 05 Sep 2019 18:52:27 +0800

author
aoqi
date
Thu, 05 Sep 2019 18:52:27 +0800
changeset 9703
2fdf635bcf28
parent 7994
04ff2f6cd0eb
parent 9669
32bc598624bd
permissions
-rw-r--r--

Merge

duke@435 1 /*
phh@9669 2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP
stefank@2314 26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP
stefank@2314 27
stefank@2314 28 #include "runtime/handles.hpp"
stefank@2314 29 #include "utilities/top.hpp"
stefank@2314 30
duke@435 31 class JNIHandleBlock;
duke@435 32
duke@435 33
duke@435 34 // Interface for creating and resolving local/global JNI handles
duke@435 35
duke@435 36 class JNIHandles : AllStatic {
duke@435 37 friend class VMStructs;
duke@435 38 private:
duke@435 39 static JNIHandleBlock* _global_handles; // First global handle block
duke@435 40 static JNIHandleBlock* _weak_global_handles; // First weak global handle block
duke@435 41 static oop _deleted_handle; // Sentinel marking deleted handles
duke@435 42
phh@9669 43 inline static bool is_jweak(jobject handle);
phh@9669 44 inline static oop& jobject_ref(jobject handle); // NOT jweak!
phh@9669 45 inline static oop& jweak_ref(jobject handle);
phh@9669 46
phh@9669 47 template<bool external_guard> inline static oop guard_value(oop value);
phh@9669 48 template<bool external_guard> inline static oop resolve_impl(jobject handle);
phh@9669 49 template<bool external_guard> static oop resolve_jweak(jweak handle);
phh@9669 50
duke@435 51 public:
phh@9669 52 // Low tag bit in jobject used to distinguish a jweak. jweak is
phh@9669 53 // type equivalent to jobject, but there are places where we need to
phh@9669 54 // be able to distinguish jweak values from other jobjects, and
phh@9669 55 // is_weak_global_handle is unsuitable for performance reasons. To
phh@9669 56 // provide such a test we add weak_tag_value to the (aligned) byte
phh@9669 57 // address designated by the jobject to produce the corresponding
phh@9669 58 // jweak. Accessing the value of a jobject must account for it
phh@9669 59 // being a possibly offset jweak.
phh@9669 60 static const uintptr_t weak_tag_size = 1;
phh@9669 61 static const uintptr_t weak_tag_alignment = (1u << weak_tag_size);
phh@9669 62 static const uintptr_t weak_tag_mask = weak_tag_alignment - 1;
phh@9669 63 static const int weak_tag_value = 1;
phh@9669 64
duke@435 65 // Resolve handle into oop
duke@435 66 inline static oop resolve(jobject handle);
duke@435 67 // Resolve externally provided handle into oop with some guards
duke@435 68 inline static oop resolve_external_guard(jobject handle);
duke@435 69 // Resolve handle into oop, result guaranteed not to be null
duke@435 70 inline static oop resolve_non_null(jobject handle);
duke@435 71
duke@435 72 // Local handles
duke@435 73 static jobject make_local(oop obj);
duke@435 74 static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known
duke@435 75 static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
duke@435 76 inline static void destroy_local(jobject handle);
duke@435 77
duke@435 78 // Global handles
duke@435 79 static jobject make_global(Handle obj);
duke@435 80 static void destroy_global(jobject handle);
duke@435 81
duke@435 82 // Weak global handles
duke@435 83 static jobject make_weak_global(Handle obj);
duke@435 84 static void destroy_weak_global(jobject handle);
duke@435 85
duke@435 86 // Sentinel marking deleted handles in block. Note that we cannot store NULL as
duke@435 87 // the sentinel, since clearing weak global JNI refs are done by storing NULL in
duke@435 88 // the handle. The handle may not be reused before destroy_weak_global is called.
duke@435 89 static oop deleted_handle() { return _deleted_handle; }
duke@435 90
duke@435 91 // Initialization
duke@435 92 static void initialize();
duke@435 93
duke@435 94 // Debugging
duke@435 95 static void print_on(outputStream* st);
duke@435 96 static void print() { print_on(tty); }
duke@435 97 static void verify();
duke@435 98 static bool is_local_handle(Thread* thread, jobject handle);
duke@435 99 static bool is_frame_handle(JavaThread* thr, jobject obj);
duke@435 100 static bool is_global_handle(jobject handle);
duke@435 101 static bool is_weak_global_handle(jobject handle);
duke@435 102 static long global_handle_memory_usage();
duke@435 103 static long weak_global_handle_memory_usage();
duke@435 104
duke@435 105 // Garbage collection support(global handles only, local handles are traversed from thread)
duke@435 106 // Traversal of regular global handles
duke@435 107 static void oops_do(OopClosure* f);
duke@435 108 // Traversal of weak global handles. Unreachable oops are cleared.
duke@435 109 static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
stefank@9665 110 // Traversal of weak global handles.
stefank@9665 111 static void weak_oops_do(OopClosure* f);
duke@435 112 };
duke@435 113
duke@435 114
duke@435 115
duke@435 116 // JNI handle blocks holding local/global JNI handles
duke@435 117
zgu@3900 118 class JNIHandleBlock : public CHeapObj<mtInternal> {
duke@435 119 friend class VMStructs;
never@1445 120 friend class CppInterpreter;
never@1445 121
duke@435 122 private:
duke@435 123 enum SomeConstants {
duke@435 124 block_size_in_oops = 32 // Number of handles per handle block
duke@435 125 };
duke@435 126
duke@435 127 oop _handles[block_size_in_oops]; // The handles
duke@435 128 int _top; // Index of next unused handle
duke@435 129 JNIHandleBlock* _next; // Link to next block
duke@435 130
duke@435 131 // The following instance variables are only used by the first block in a chain.
duke@435 132 // Having two types of blocks complicates the code and the space overhead in negligble.
duke@435 133 JNIHandleBlock* _last; // Last block in use
duke@435 134 JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call
duke@435 135 oop* _free_list; // Handle free list
duke@435 136 int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list
duke@435 137
poonam@7627 138 // Check JNI, "planned capacity" for current frame (or push/ensure)
poonam@7627 139 size_t _planned_capacity;
poonam@7627 140
duke@435 141 #ifndef PRODUCT
duke@435 142 JNIHandleBlock* _block_list_link; // Link for list below
duke@435 143 static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only)
duke@435 144 #endif
duke@435 145
duke@435 146 static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks
duke@435 147 static int _blocks_allocated; // For debugging/printing
duke@435 148
duke@435 149 // Fill block with bad_handle values
duke@435 150 void zap();
duke@435 151
never@1445 152 protected:
duke@435 153 // No more handles in the both the current and following blocks
duke@435 154 void clear() { _top = 0; }
duke@435 155
never@1445 156 private:
duke@435 157 // Free list computation
duke@435 158 void rebuild_free_list();
duke@435 159
duke@435 160 public:
duke@435 161 // Handle allocation
duke@435 162 jobject allocate_handle(oop obj);
duke@435 163
duke@435 164 // Block allocation and block free list management
duke@435 165 static JNIHandleBlock* allocate_block(Thread* thread = NULL);
duke@435 166 static void release_block(JNIHandleBlock* block, Thread* thread = NULL);
duke@435 167
duke@435 168 // JNI PushLocalFrame/PopLocalFrame support
duke@435 169 JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; }
duke@435 170 void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; }
duke@435 171
duke@435 172 // Stub generator support
duke@435 173 static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); }
duke@435 174
duke@435 175 // Garbage collection support
duke@435 176 // Traversal of regular handles
duke@435 177 void oops_do(OopClosure* f);
duke@435 178 // Traversal of weak handles. Unreachable oops are cleared.
duke@435 179 void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
duke@435 180
poonam@7627 181 // Checked JNI support
poonam@7627 182 void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; }
poonam@7627 183 const size_t get_planned_capacity() { return _planned_capacity; }
poonam@7627 184 const size_t get_number_of_live_handles();
poonam@7627 185
duke@435 186 // Debugging
duke@435 187 bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle
duke@435 188 bool contains(jobject handle) const; // Does this block contain handle
duke@435 189 int length() const; // Length of chain starting with this block
duke@435 190 long memory_usage() const;
duke@435 191 #ifndef PRODUCT
duke@435 192 static bool any_contains(jobject handle); // Does any block currently in use contain handle
duke@435 193 static void print_statistics();
duke@435 194 #endif
duke@435 195 };
duke@435 196
phh@9669 197 inline bool JNIHandles::is_jweak(jobject handle) {
phh@9669 198 STATIC_ASSERT(weak_tag_size == 1);
phh@9669 199 STATIC_ASSERT(weak_tag_value == 1);
phh@9669 200 return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
phh@9669 201 }
phh@9669 202
phh@9669 203 inline oop& JNIHandles::jobject_ref(jobject handle) {
phh@9669 204 assert(!is_jweak(handle), "precondition");
phh@9669 205 return *reinterpret_cast<oop*>(handle);
phh@9669 206 }
phh@9669 207
phh@9669 208 inline oop& JNIHandles::jweak_ref(jobject handle) {
phh@9669 209 assert(is_jweak(handle), "precondition");
phh@9669 210 char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
phh@9669 211 return *reinterpret_cast<oop*>(ptr);
phh@9669 212 }
phh@9669 213
phh@9669 214 // external_guard is true if called from resolve_external_guard.
phh@9669 215 // Treat deleted (and possibly zapped) as NULL for external_guard,
phh@9669 216 // else as (asserted) error.
phh@9669 217 template<bool external_guard>
phh@9669 218 inline oop JNIHandles::guard_value(oop value) {
phh@9669 219 if (!external_guard) {
phh@9669 220 assert(value != badJNIHandle, "Pointing to zapped jni handle area");
phh@9669 221 assert(value != deleted_handle(), "Used a deleted global handle");
phh@9669 222 } else if ((value == badJNIHandle) || (value == deleted_handle())) {
phh@9669 223 value = NULL;
phh@9669 224 }
phh@9669 225 return value;
phh@9669 226 }
phh@9669 227
phh@9669 228 // external_guard is true if called from resolve_external_guard.
phh@9669 229 template<bool external_guard>
phh@9669 230 inline oop JNIHandles::resolve_impl(jobject handle) {
phh@9669 231 assert(handle != NULL, "precondition");
phh@9669 232 oop result;
phh@9669 233 if (is_jweak(handle)) { // Unlikely
phh@9669 234 result = resolve_jweak<external_guard>(handle);
phh@9669 235 } else {
phh@9669 236 result = jobject_ref(handle);
phh@9669 237 // Construction of jobjects canonicalize a null value into a null
phh@9669 238 // jobject, so for non-jweak the pointee should never be null.
phh@9669 239 assert(external_guard || result != NULL,
phh@9669 240 "Invalid value read from jni handle");
phh@9669 241 result = guard_value<external_guard>(result);
phh@9669 242 }
phh@9669 243 return result;
phh@9669 244 }
duke@435 245
duke@435 246 inline oop JNIHandles::resolve(jobject handle) {
phh@9669 247 oop result = NULL;
phh@9669 248 if (handle != NULL) {
phh@9669 249 result = resolve_impl<false /* external_guard */ >(handle);
phh@9669 250 }
duke@435 251 return result;
phh@9669 252 }
duke@435 253
phh@9669 254 // Resolve some erroneous cases to NULL, rather than treating them as
phh@9669 255 // possibly unchecked errors. In particular, deleted handles are
phh@9669 256 // treated as NULL (though a deleted and later reallocated handle
phh@9669 257 // isn't detected).
duke@435 258 inline oop JNIHandles::resolve_external_guard(jobject handle) {
phh@9669 259 oop result = NULL;
phh@9669 260 if (handle != NULL) {
phh@9669 261 result = resolve_impl<true /* external_guard */ >(handle);
phh@9669 262 }
duke@435 263 return result;
phh@9669 264 }
duke@435 265
duke@435 266 inline oop JNIHandles::resolve_non_null(jobject handle) {
duke@435 267 assert(handle != NULL, "JNI handle should not be null");
phh@9669 268 oop result = resolve_impl<false /* external_guard */ >(handle);
phh@9669 269 assert(result != NULL, "NULL read from jni handle");
duke@435 270 return result;
phh@9669 271 }
duke@435 272
duke@435 273 inline void JNIHandles::destroy_local(jobject handle) {
duke@435 274 if (handle != NULL) {
phh@9669 275 jobject_ref(handle) = deleted_handle();
duke@435 276 }
duke@435 277 }
stefank@2314 278
stefank@2314 279 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP

mercurial