1 /* 2 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. 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 * 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. 22 * 23 */ 24 25 #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP 26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/handles.hpp" 30 31 class OopStorage; 32 33 // Interface for creating and resolving local/global JNI handles 34 35 class JNIHandles : AllStatic { 36 friend class VMStructs; 37 private: 38 static OopStorage* _global_handles; 39 static OopStorage* _weak_global_handles; 40 41 inline static bool is_jweak(jobject handle); 42 inline static oop* jobject_ptr(jobject handle); // NOT jweak! 43 inline static oop* jweak_ptr(jobject handle); 44 45 template<bool external_guard> inline static oop resolve_impl(jobject handle); 46 static oop resolve_jweak(jweak handle); 47 48 // This method is not inlined in order to avoid circular includes between 49 // this header file and thread.hpp. 50 static bool current_thread_in_native(); 51 52 public: 53 // Low tag bit in jobject used to distinguish a jweak. jweak is 54 // type equivalent to jobject, but there are places where we need to 55 // be able to distinguish jweak values from other jobjects, and 56 // is_weak_global_handle is unsuitable for performance reasons. To 57 // provide such a test we add weak_tag_value to the (aligned) byte 58 // address designated by the jobject to produce the corresponding 59 // jweak. Accessing the value of a jobject must account for it 60 // being a possibly offset jweak. 61 static const uintptr_t weak_tag_size = 1; 62 static const uintptr_t weak_tag_alignment = (1u << weak_tag_size); 63 static const uintptr_t weak_tag_mask = weak_tag_alignment - 1; 64 static const int weak_tag_value = 1; 65 66 // Resolve handle into oop 67 inline static oop resolve(jobject handle); 68 // Resolve handle into oop, result guaranteed not to be null 69 inline static oop resolve_non_null(jobject handle); 70 // Resolve externally provided handle into oop with some guards 71 static oop resolve_external_guard(jobject handle); 72 73 // Local handles 74 static jobject make_local(oop obj); 75 static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known 76 static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known 77 inline static void destroy_local(jobject handle); 78 79 // Global handles 80 static jobject make_global(Handle obj, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 81 static void destroy_global(jobject handle); 82 83 // Weak global handles 84 static jobject make_weak_global(Handle obj, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); 85 static void destroy_weak_global(jobject handle); 86 static bool is_global_weak_cleared(jweak handle); // Test jweak without resolution 87 88 // Initialization 89 static void initialize(); 90 91 // Debugging 92 static void print_on(outputStream* st); 93 static void print() { print_on(tty); } 94 static void verify(); 95 // The category predicates all require handle != NULL. 96 static bool is_local_handle(Thread* thread, jobject handle); 97 static bool is_frame_handle(JavaThread* thread, jobject handle); 98 static bool is_global_handle(jobject handle); 99 static bool is_weak_global_handle(jobject handle); 100 static size_t global_handle_memory_usage(); 101 static size_t weak_global_handle_memory_usage(); 102 103 #ifndef PRODUCT 104 // Is handle from any local block of any thread? 105 static bool is_local_handle(jobject handle); 106 #endif 107 108 // precondition: handle != NULL. 109 static jobjectRefType handle_type(Thread* thread, jobject handle); 110 111 // Garbage collection support(global handles only, local handles are traversed from thread) 112 // Traversal of regular global handles 113 static void oops_do(OopClosure* f); 114 // Traversal of weak global handles. Unreachable oops are cleared. 115 static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); 116 // Traversal of weak global handles. 117 static void weak_oops_do(OopClosure* f); 118 }; 119 120 121 122 // JNI handle blocks holding local/global JNI handles 123 124 class JNIHandleBlock : public CHeapObj<mtInternal> { 125 friend class VMStructs; 126 friend class CppInterpreter; 127 128 private: 129 enum SomeConstants { 130 block_size_in_oops = 32 // Number of handles per handle block 131 }; 132 133 oop _handles[block_size_in_oops]; // The handles 134 int _top; // Index of next unused handle 135 JNIHandleBlock* _next; // Link to next block 136 137 // The following instance variables are only used by the first block in a chain. 138 // Having two types of blocks complicates the code and the space overhead in negligible. 139 JNIHandleBlock* _last; // Last block in use 140 JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call 141 oop* _free_list; // Handle free list 142 int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list 143 144 // Check JNI, "planned capacity" for current frame (or push/ensure) 145 size_t _planned_capacity; 146 147 #ifndef PRODUCT 148 JNIHandleBlock* _block_list_link; // Link for list below 149 static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) 150 #endif 151 152 static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks 153 static int _blocks_allocated; // For debugging/printing 154 155 // Fill block with bad_handle values 156 void zap() NOT_DEBUG_RETURN; 157 158 // Free list computation 159 void rebuild_free_list(); 160 161 // No more handles in the both the current and following blocks 162 void clear() { _top = 0; } 163 164 public: 165 // Handle allocation 166 jobject allocate_handle(oop obj); 167 168 // Block allocation and block free list management 169 static JNIHandleBlock* allocate_block(Thread* thread = NULL); 170 static void release_block(JNIHandleBlock* block, Thread* thread = NULL); 171 172 // JNI PushLocalFrame/PopLocalFrame support 173 JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } 174 void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } 175 176 // Stub generator support 177 static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } 178 179 // Garbage collection support 180 // Traversal of handles 181 void oops_do(OopClosure* f); 182 183 // Checked JNI support 184 void set_planned_capacity(size_t planned_capacity) { _planned_capacity = planned_capacity; } 185 const size_t get_planned_capacity() { return _planned_capacity; } 186 const size_t get_number_of_live_handles(); 187 188 // Debugging 189 bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle 190 bool contains(jobject handle) const; // Does this block contain handle 191 size_t length() const; // Length of chain starting with this block 192 size_t memory_usage() const; 193 #ifndef PRODUCT 194 static bool any_contains(jobject handle); // Does any block currently in use contain handle 195 static void print_statistics(); 196 #endif 197 }; 198 199 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP