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