Mon, 18 Jun 2012 12:29:21 -0700
7176856: add the JRE name to the error log
Reviewed-by: coleenp, jrose, kvn, twisti
Contributed-by: Krystal Mok <sajia@taobao.com>
1 /*
2 * Copyright (c) 1998, 2010, 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 */
25 #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP
26 #define SHARE_VM_RUNTIME_JNIHANDLES_HPP
28 #include "runtime/handles.hpp"
29 #include "utilities/top.hpp"
31 class JNIHandleBlock;
34 // Interface for creating and resolving local/global JNI handles
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
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);
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);
57 // Global handles
58 static jobject make_global(Handle obj);
59 static void destroy_global(jobject handle);
61 // Weak global handles
62 static jobject make_weak_global(Handle obj);
63 static void destroy_weak_global(jobject handle);
65 // jmethodID handling (as Weak global handles).
66 // Because the useful life-span of a jmethodID cannot be determined, once created they are
67 // never reclaimed. The methods to which they refer, however, can be GC'ed away if the class
68 // is unloaded or if the method is made obsolete or deleted -- in these cases, the jmethodID
69 // refers to NULL (as is the case for any weak reference).
70 static jmethodID make_jmethod_id(methodHandle mh);
71 static void destroy_jmethod_id(jmethodID mid);
72 // Use resolve_jmethod_id() in situations where the caller is expected
73 // to provide a valid jmethodID; the only sanity checks are in asserts;
74 // result guaranteed not to be NULL.
75 inline static methodOop resolve_jmethod_id(jmethodID mid);
76 // Use checked_resolve_jmethod_id() in situations where the caller
77 // should provide a valid jmethodID, but might not. NULL is returned
78 // when the jmethodID does not refer to a valid method.
79 inline static methodOop checked_resolve_jmethod_id(jmethodID mid);
80 static void change_method_associated_with_jmethod_id(jmethodID jmid, methodHandle mh);
82 // Sentinel marking deleted handles in block. Note that we cannot store NULL as
83 // the sentinel, since clearing weak global JNI refs are done by storing NULL in
84 // the handle. The handle may not be reused before destroy_weak_global is called.
85 static oop deleted_handle() { return _deleted_handle; }
87 // Initialization
88 static void initialize();
90 // Debugging
91 static void print_on(outputStream* st);
92 static void print() { print_on(tty); }
93 static void verify();
94 static bool is_local_handle(Thread* thread, jobject handle);
95 static bool is_frame_handle(JavaThread* thr, jobject obj);
96 static bool is_global_handle(jobject handle);
97 static bool is_weak_global_handle(jobject handle);
98 static long global_handle_memory_usage();
99 static long weak_global_handle_memory_usage();
101 // Garbage collection support(global handles only, local handles are traversed from thread)
102 // Traversal of regular global handles
103 static void oops_do(OopClosure* f);
104 // Traversal of weak global handles. Unreachable oops are cleared.
105 static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
106 };
110 // JNI handle blocks holding local/global JNI handles
112 class JNIHandleBlock : public CHeapObj {
113 friend class VMStructs;
114 friend class CppInterpreter;
116 private:
117 enum SomeConstants {
118 block_size_in_oops = 32 // Number of handles per handle block
119 };
121 oop _handles[block_size_in_oops]; // The handles
122 int _top; // Index of next unused handle
123 JNIHandleBlock* _next; // Link to next block
125 // The following instance variables are only used by the first block in a chain.
126 // Having two types of blocks complicates the code and the space overhead in negligble.
127 JNIHandleBlock* _last; // Last block in use
128 JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call
129 oop* _free_list; // Handle free list
130 int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list
132 #ifndef PRODUCT
133 JNIHandleBlock* _block_list_link; // Link for list below
134 static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only)
135 #endif
137 static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks
138 static int _blocks_allocated; // For debugging/printing
140 // Fill block with bad_handle values
141 void zap();
143 protected:
144 // No more handles in the both the current and following blocks
145 void clear() { _top = 0; }
147 private:
148 // Free list computation
149 void rebuild_free_list();
151 public:
152 // Handle allocation
153 jobject allocate_handle(oop obj);
155 // Block allocation and block free list management
156 static JNIHandleBlock* allocate_block(Thread* thread = NULL);
157 static void release_block(JNIHandleBlock* block, Thread* thread = NULL);
159 // JNI PushLocalFrame/PopLocalFrame support
160 JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; }
161 void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; }
163 // Stub generator support
164 static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); }
166 // Garbage collection support
167 // Traversal of regular handles
168 void oops_do(OopClosure* f);
169 // Traversal of weak handles. Unreachable oops are cleared.
170 void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
172 // Debugging
173 bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle
174 bool contains(jobject handle) const; // Does this block contain handle
175 int length() const; // Length of chain starting with this block
176 long memory_usage() const;
177 #ifndef PRODUCT
178 static bool any_contains(jobject handle); // Does any block currently in use contain handle
179 static void print_statistics();
180 #endif
181 };
184 inline oop JNIHandles::resolve(jobject handle) {
185 oop result = (handle == NULL ? (oop)NULL : *(oop*)handle);
186 assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle");
187 assert(result != badJNIHandle, "Pointing to zapped jni handle area");
188 return result;
189 };
192 inline oop JNIHandles::resolve_external_guard(jobject handle) {
193 if (handle == NULL) return NULL;
194 oop result = *(oop*)handle;
195 if (result == NULL || result == badJNIHandle) return NULL;
196 return result;
197 };
200 inline oop JNIHandles::resolve_non_null(jobject handle) {
201 assert(handle != NULL, "JNI handle should not be null");
202 oop result = *(oop*)handle;
203 assert(result != NULL, "Invalid value read from jni handle");
204 assert(result != badJNIHandle, "Pointing to zapped jni handle area");
205 // Don't let that private _deleted_handle object escape into the wild.
206 assert(result != deleted_handle(), "Used a deleted global handle.");
207 return result;
208 };
210 inline methodOop JNIHandles::resolve_jmethod_id(jmethodID mid) {
211 return (methodOop) resolve_non_null((jobject)mid);
212 };
214 inline methodOop JNIHandles::checked_resolve_jmethod_id(jmethodID mid) {
215 oop o = resolve_external_guard((jobject) mid);
216 if (o == NULL || !o->is_method()) {
217 return (methodOop) NULL;
218 }
220 return (methodOop) o;
221 };
224 inline void JNIHandles::destroy_local(jobject handle) {
225 if (handle != NULL) {
226 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
227 }
228 }
230 #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP