annotate src/share/vm/runtime/jniHandles.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ba764ed4b6f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_jniHandles.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 JNIHandleBlock* JNIHandles::_global_handles = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 JNIHandleBlock* JNIHandles::_weak_global_handles = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 oop JNIHandles::_deleted_handle = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 jobject JNIHandles::make_local(oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
37 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 Thread* thread = Thread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
39 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
40 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
41 }
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // optimized versions
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 jobject JNIHandles::make_local(Thread* thread, oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
50 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
51 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
52 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
59 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
60 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 JavaThread* thread = JavaThread::thread_from_jni_environment(env);
a61af66fc99e Initial load
duke
parents:
diff changeset
62 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
63 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65 }
a61af66fc99e Initial load
duke
parents:
diff changeset
66
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 jobject JNIHandles::make_global(Handle obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 jobject res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 if (!obj.is_null()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
72 MutexLocker ml(JNIGlobalHandle_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
73 assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
74 res = _global_handles->allocate_handle(obj());
a61af66fc99e Initial load
duke
parents:
diff changeset
75 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 jobject JNIHandles::make_weak_global(Handle obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 jobject res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 if (!obj.is_null()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
87 MutexLocker ml(JNIGlobalHandle_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
88 assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
89 res = _weak_global_handles->allocate_handle(obj());
a61af66fc99e Initial load
duke
parents:
diff changeset
90 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 jmethodID JNIHandles::make_jmethod_id(methodHandle mh) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 return (jmethodID) make_weak_global(mh);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 void JNIHandles::change_method_associated_with_jmethod_id(jmethodID jmid, methodHandle mh) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 MutexLocker ml(JNIGlobalHandle_lock); // Is this necessary?
a61af66fc99e Initial load
duke
parents:
diff changeset
104 Handle obj = (Handle)mh;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 oop* jobj = (oop*)jmid;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 *jobj = obj();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 void JNIHandles::destroy_global(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 if (handle != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 assert(is_global_handle(handle), "Invalid delete of global JNI handle");
a61af66fc99e Initial load
duke
parents:
diff changeset
113 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 void JNIHandles::destroy_weak_global(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 if (handle != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle");
a61af66fc99e Initial load
duke
parents:
diff changeset
121 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 void JNIHandles::destroy_jmethod_id(jmethodID mid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 destroy_weak_global((jobject)mid);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 void JNIHandles::oops_do(OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 f->do_oop(&_deleted_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
132 _global_handles->oops_do(f);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 _weak_global_handles->weak_oops_do(is_alive, f);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 void JNIHandles::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 _global_handles = JNIHandleBlock::allocate_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
143 _weak_global_handles = JNIHandleBlock::allocate_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
144 EXCEPTION_MARK;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // We will never reach the CATCH below since Exceptions::_throw will cause
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // the VM to exit if an exception is thrown during initialization
a61af66fc99e Initial load
duke
parents:
diff changeset
147 klassOop k = SystemDictionary::object_klass();
a61af66fc99e Initial load
duke
parents:
diff changeset
148 _deleted_handle = instanceKlass::cast(k)->allocate_permanent_instance(CATCH);
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 JNIHandleBlock* block = thread->active_handles();
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // Look back past possible native calls to jni_PushLocalFrame.
a61af66fc99e Initial load
duke
parents:
diff changeset
156 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if (block->chain_contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160 block = block->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 }
a61af66fc99e Initial load
duke
parents:
diff changeset
164
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 // Determine if the handle is somewhere in the current thread's stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // We easily can't isolate any particular stack frame the handle might
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // come from, so we'll check the whole stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // If there is no java frame, then this must be top level code, such
a61af66fc99e Initial load
duke
parents:
diff changeset
172 // as the java command executable, in which case, this type of handle
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // is not permitted.
a61af66fc99e Initial load
duke
parents:
diff changeset
174 return (thr->has_last_Java_frame() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
175 (void*)obj < (void*)thr->stack_base() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
176 (void*)obj >= (void*)thr->last_Java_sp());
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 bool JNIHandles::is_global_handle(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 return _global_handles->chain_contains(handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184
a61af66fc99e Initial load
duke
parents:
diff changeset
185 bool JNIHandles::is_weak_global_handle(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return _weak_global_handles->chain_contains(handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 long JNIHandles::global_handle_memory_usage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 return _global_handles->memory_usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 long JNIHandles::weak_global_handle_memory_usage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return _weak_global_handles->memory_usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 class AlwaysAliveClosure: public BoolObjectClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
200 bool do_object_b(oop obj) { return true; }
a61af66fc99e Initial load
duke
parents:
diff changeset
201 void do_object(oop obj) { assert(false, "Don't call"); }
a61af66fc99e Initial load
duke
parents:
diff changeset
202 };
a61af66fc99e Initial load
duke
parents:
diff changeset
203
a61af66fc99e Initial load
duke
parents:
diff changeset
204 class CountHandleClosure: public OopClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
206 int _count;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
208 CountHandleClosure(): _count(0) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
209 void do_oop(oop* unused) {
a61af66fc99e Initial load
duke
parents:
diff changeset
210 _count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 }
a61af66fc99e Initial load
duke
parents:
diff changeset
212 int count() { return _count; }
a61af66fc99e Initial load
duke
parents:
diff changeset
213 };
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // We assume this is called at a safepoint: no lock is needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
216 void JNIHandles::print_on(outputStream* st) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
a61af66fc99e Initial load
duke
parents:
diff changeset
218 assert(_global_handles != NULL && _weak_global_handles != NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
219 "JNIHandles not initialized");
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 CountHandleClosure global_handle_count;
a61af66fc99e Initial load
duke
parents:
diff changeset
222 AlwaysAliveClosure always_alive;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 oops_do(&global_handle_count);
a61af66fc99e Initial load
duke
parents:
diff changeset
224 weak_oops_do(&always_alive, &global_handle_count);
a61af66fc99e Initial load
duke
parents:
diff changeset
225
a61af66fc99e Initial load
duke
parents:
diff changeset
226 st->print_cr("JNI global references: %d", global_handle_count.count());
a61af66fc99e Initial load
duke
parents:
diff changeset
227 st->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
228 st->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
229 }
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 class VerifyHandleClosure: public OopClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
233 void do_oop(oop* root) {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 (*root)->verify();
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236 };
a61af66fc99e Initial load
duke
parents:
diff changeset
237
a61af66fc99e Initial load
duke
parents:
diff changeset
238 void JNIHandles::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 VerifyHandleClosure verify_handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
240 AlwaysAliveClosure always_alive;
a61af66fc99e Initial load
duke
parents:
diff changeset
241
a61af66fc99e Initial load
duke
parents:
diff changeset
242 oops_do(&verify_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
243 weak_oops_do(&always_alive, &verify_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 }
a61af66fc99e Initial load
duke
parents:
diff changeset
245
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 void jni_handles_init() {
a61af66fc99e Initial load
duke
parents:
diff changeset
249 JNIHandles::initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 int JNIHandleBlock::_blocks_allocated = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
254 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
255 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
256 JNIHandleBlock* JNIHandleBlock::_block_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259
a61af66fc99e Initial load
duke
parents:
diff changeset
260 void JNIHandleBlock::zap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // Zap block values
a61af66fc99e Initial load
duke
parents:
diff changeset
262 _top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 for (int index = 0; index < block_size_in_oops; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 _handles[index] = badJNIHandle;
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267
a61af66fc99e Initial load
duke
parents:
diff changeset
268 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 assert(thread == NULL || thread == Thread::current(), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
270 JNIHandleBlock* block;
a61af66fc99e Initial load
duke
parents:
diff changeset
271 // Check the thread-local free list for a block so we don't
a61af66fc99e Initial load
duke
parents:
diff changeset
272 // have to acquire a mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
273 if (thread != NULL && thread->free_handle_block() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 block = thread->free_handle_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
275 thread->set_free_handle_block(block->_next);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 // locking with safepoint checking introduces a potential deadlock:
a61af66fc99e Initial load
duke
parents:
diff changeset
279 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // - another would hold Threads_lock (jni_AttachCurrentThread) and then
a61af66fc99e Initial load
duke
parents:
diff changeset
281 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
282 MutexLockerEx ml(JNIHandleBlockFreeList_lock,
a61af66fc99e Initial load
duke
parents:
diff changeset
283 Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
284 if (_block_free_list == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // Allocate new block
a61af66fc99e Initial load
duke
parents:
diff changeset
286 block = new JNIHandleBlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
287 _blocks_allocated++;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 if (TraceJNIHandleAllocation) {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)",
a61af66fc99e Initial load
duke
parents:
diff changeset
290 block, _blocks_allocated);
a61af66fc99e Initial load
duke
parents:
diff changeset
291 }
a61af66fc99e Initial load
duke
parents:
diff changeset
292 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
293 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
294 // Link new block to list of all allocated blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
295 block->_block_list_link = _block_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 _block_list = block;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
298 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // Get block from free list
a61af66fc99e Initial load
duke
parents:
diff changeset
300 block = _block_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
301 _block_free_list = _block_free_list->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
302 }
a61af66fc99e Initial load
duke
parents:
diff changeset
303 }
a61af66fc99e Initial load
duke
parents:
diff changeset
304 block->_top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 block->_next = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
306 block->_pop_frame_link = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
a61af66fc99e Initial load
duke
parents:
diff changeset
308 debug_only(block->_last = NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 debug_only(block->_free_list = NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
310 debug_only(block->_allocate_before_rebuild = -1);
a61af66fc99e Initial load
duke
parents:
diff changeset
311 return block;
a61af66fc99e Initial load
duke
parents:
diff changeset
312 }
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314
a61af66fc99e Initial load
duke
parents:
diff changeset
315 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 assert(thread == NULL || thread == Thread::current(), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
317 JNIHandleBlock* pop_frame_link = block->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // Put returned block at the beginning of the thread-local free list.
a61af66fc99e Initial load
duke
parents:
diff changeset
319 // Note that if thread == NULL, we use it as an implicit argument that
a61af66fc99e Initial load
duke
parents:
diff changeset
320 // we _don't_ want the block to be kept on the free_handle_block.
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // See for instance JavaThread::exit().
a61af66fc99e Initial load
duke
parents:
diff changeset
322 if (thread != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
324 JNIHandleBlock* freelist = thread->free_handle_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
325 block->_pop_frame_link = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
326 thread->set_free_handle_block(block);
a61af66fc99e Initial load
duke
parents:
diff changeset
327
a61af66fc99e Initial load
duke
parents:
diff changeset
328 // Add original freelist to end of chain
a61af66fc99e Initial load
duke
parents:
diff changeset
329 if ( freelist != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
330 while ( block->_next != NULL ) block = block->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 block->_next = freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 block = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
334 }
a61af66fc99e Initial load
duke
parents:
diff changeset
335 if (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 // Return blocks to free list
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // locking with safepoint checking introduces a potential deadlock:
a61af66fc99e Initial load
duke
parents:
diff changeset
338 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // - another would hold Threads_lock (jni_AttachCurrentThread) and then
a61af66fc99e Initial load
duke
parents:
diff changeset
340 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
341 MutexLockerEx ml(JNIHandleBlockFreeList_lock,
a61af66fc99e Initial load
duke
parents:
diff changeset
342 Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
343 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
344 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
345 JNIHandleBlock* next = block->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
346 block->_next = _block_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 _block_free_list = block;
a61af66fc99e Initial load
duke
parents:
diff changeset
348 block = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
349 }
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351 if (pop_frame_link != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 // As a sanity check we release blocks pointed to by the pop_frame_link.
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // This should never happen (only if PopLocalFrame is not called the
a61af66fc99e Initial load
duke
parents:
diff changeset
354 // correct number of times).
a61af66fc99e Initial load
duke
parents:
diff changeset
355 release_block(pop_frame_link, thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357 }
a61af66fc99e Initial load
duke
parents:
diff changeset
358
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 void JNIHandleBlock::oops_do(OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 JNIHandleBlock* current_chain = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // Iterate over chain of blocks, followed by chains linked through the
a61af66fc99e Initial load
duke
parents:
diff changeset
363 // pop frame links.
a61af66fc99e Initial load
duke
parents:
diff changeset
364 while (current_chain != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
365 for (JNIHandleBlock* current = current_chain; current != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
366 current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 assert(current == current_chain || current->pop_frame_link() == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
368 "only blocks first in chain should have pop frame link set");
a61af66fc99e Initial load
duke
parents:
diff changeset
369 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 oop* root = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
371 oop value = *root;
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // traverse heap pointers only, not deleted handles or free list
a61af66fc99e Initial load
duke
parents:
diff changeset
373 // pointers
a61af66fc99e Initial load
duke
parents:
diff changeset
374 if (value != NULL && Universe::heap()->is_in_reserved(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 f->do_oop(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // the next handle block is valid only if current block is full
a61af66fc99e Initial load
duke
parents:
diff changeset
379 if (current->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
380 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
381 }
a61af66fc99e Initial load
duke
parents:
diff changeset
382 }
a61af66fc99e Initial load
duke
parents:
diff changeset
383 current_chain = current_chain->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385 }
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387
a61af66fc99e Initial load
duke
parents:
diff changeset
388 void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive,
a61af66fc99e Initial load
duke
parents:
diff changeset
389 OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
391 assert(current->pop_frame_link() == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
392 "blocks holding weak global JNI handles should not have pop frame link set");
a61af66fc99e Initial load
duke
parents:
diff changeset
393 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
394 oop* root = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
395 oop value = *root;
a61af66fc99e Initial load
duke
parents:
diff changeset
396 // traverse heap pointers only, not deleted handles or free list pointers
a61af66fc99e Initial load
duke
parents:
diff changeset
397 if (value != NULL && Universe::heap()->is_in_reserved(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
398 if (is_alive->do_object_b(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // The weakly referenced object is alive, update pointer
a61af66fc99e Initial load
duke
parents:
diff changeset
400 f->do_oop(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
401 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
402 // The weakly referenced object is not alive, clear the reference by storing NULL
a61af66fc99e Initial load
duke
parents:
diff changeset
403 if (TraceReferenceGC) {
a61af66fc99e Initial load
duke
parents:
diff changeset
404 tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root);
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
a61af66fc99e Initial load
duke
parents:
diff changeset
406 *root = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 }
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
a61af66fc99e Initial load
duke
parents:
diff changeset
409 }
a61af66fc99e Initial load
duke
parents:
diff changeset
410 // the next handle block is valid only if current block is full
a61af66fc99e Initial load
duke
parents:
diff changeset
411 if (current->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
413 }
a61af66fc99e Initial load
duke
parents:
diff changeset
414 }
a61af66fc99e Initial load
duke
parents:
diff changeset
415 }
a61af66fc99e Initial load
duke
parents:
diff changeset
416
a61af66fc99e Initial load
duke
parents:
diff changeset
417
a61af66fc99e Initial load
duke
parents:
diff changeset
418 jobject JNIHandleBlock::allocate_handle(oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
419 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
420 if (_top == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
421 // This is the first allocation or the initial block got zapped when
a61af66fc99e Initial load
duke
parents:
diff changeset
422 // entering a native function. If we have any following blocks they are
a61af66fc99e Initial load
duke
parents:
diff changeset
423 // not valid anymore.
a61af66fc99e Initial load
duke
parents:
diff changeset
424 for (JNIHandleBlock* current = _next; current != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
426 assert(current->_last == NULL, "only first block should have _last set");
a61af66fc99e Initial load
duke
parents:
diff changeset
427 assert(current->_free_list == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
428 "only first block should have _free_list set");
a61af66fc99e Initial load
duke
parents:
diff changeset
429 current->_top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
430 if (ZapJNIHandleArea) current->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
431 }
a61af66fc99e Initial load
duke
parents:
diff changeset
432 // Clear initial block
a61af66fc99e Initial load
duke
parents:
diff changeset
433 _free_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
434 _allocate_before_rebuild = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
435 _last = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
436 if (ZapJNIHandleArea) zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
437 }
a61af66fc99e Initial load
duke
parents:
diff changeset
438
a61af66fc99e Initial load
duke
parents:
diff changeset
439 // Try last block
a61af66fc99e Initial load
duke
parents:
diff changeset
440 if (_last->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
441 oop* handle = &(_last->_handles)[_last->_top++];
a61af66fc99e Initial load
duke
parents:
diff changeset
442 *handle = obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
443 return (jobject) handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
444 }
a61af66fc99e Initial load
duke
parents:
diff changeset
445
a61af66fc99e Initial load
duke
parents:
diff changeset
446 // Try free list
a61af66fc99e Initial load
duke
parents:
diff changeset
447 if (_free_list != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
448 oop* handle = _free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
449 _free_list = (oop*) *_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
450 *handle = obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
451 return (jobject) handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
452 }
a61af66fc99e Initial load
duke
parents:
diff changeset
453 // Check if unused block follow last
a61af66fc99e Initial load
duke
parents:
diff changeset
454 if (_last->_next != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
455 // update last and retry
a61af66fc99e Initial load
duke
parents:
diff changeset
456 _last = _last->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
457 return allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
458 }
a61af66fc99e Initial load
duke
parents:
diff changeset
459
a61af66fc99e Initial load
duke
parents:
diff changeset
460 // No space available, we have to rebuild free list or expand
a61af66fc99e Initial load
duke
parents:
diff changeset
461 if (_allocate_before_rebuild == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
462 rebuild_free_list(); // updates _allocate_before_rebuild counter
a61af66fc99e Initial load
duke
parents:
diff changeset
463 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
464 // Append new block
a61af66fc99e Initial load
duke
parents:
diff changeset
465 Thread* thread = Thread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
466 Handle obj_handle(thread, obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
467 // This can block, so we need to preserve obj accross call.
a61af66fc99e Initial load
duke
parents:
diff changeset
468 _last->_next = JNIHandleBlock::allocate_block(thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
469 _last = _last->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 _allocate_before_rebuild--;
a61af66fc99e Initial load
duke
parents:
diff changeset
471 obj = obj_handle();
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473 return allocate_handle(obj); // retry
a61af66fc99e Initial load
duke
parents:
diff changeset
474 }
a61af66fc99e Initial load
duke
parents:
diff changeset
475
a61af66fc99e Initial load
duke
parents:
diff changeset
476
a61af66fc99e Initial load
duke
parents:
diff changeset
477 void JNIHandleBlock::rebuild_free_list() {
a61af66fc99e Initial load
duke
parents:
diff changeset
478 assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
479 int free = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 int blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
481 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
483 oop* handle = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
484 if (*handle == JNIHandles::deleted_handle()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
485 // this handle was cleared out by a delete call, reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
486 *handle = (oop) _free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
487 _free_list = handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
488 free++;
a61af66fc99e Initial load
duke
parents:
diff changeset
489 }
a61af66fc99e Initial load
duke
parents:
diff changeset
490 }
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // we should not rebuild free list if there are unused handles at the end
a61af66fc99e Initial load
duke
parents:
diff changeset
492 assert(current->_top == block_size_in_oops, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
493 blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
494 }
a61af66fc99e Initial load
duke
parents:
diff changeset
495 // Heuristic: if more than half of the handles are free we rebuild next time
a61af66fc99e Initial load
duke
parents:
diff changeset
496 // as well, otherwise we append a corresponding number of new blocks before
a61af66fc99e Initial load
duke
parents:
diff changeset
497 // attempting a free list rebuild again.
a61af66fc99e Initial load
duke
parents:
diff changeset
498 int total = blocks * block_size_in_oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
499 int extra = total - 2*free;
a61af66fc99e Initial load
duke
parents:
diff changeset
500 if (extra > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 // Not as many free handles as we would like - compute number of new blocks to append
a61af66fc99e Initial load
duke
parents:
diff changeset
502 _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
503 }
a61af66fc99e Initial load
duke
parents:
diff changeset
504 if (TraceJNIHandleAllocation) {
a61af66fc99e Initial load
duke
parents:
diff changeset
505 tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d",
a61af66fc99e Initial load
duke
parents:
diff changeset
506 this, blocks, total-free, free, _allocate_before_rebuild);
a61af66fc99e Initial load
duke
parents:
diff changeset
507 }
a61af66fc99e Initial load
duke
parents:
diff changeset
508 }
a61af66fc99e Initial load
duke
parents:
diff changeset
509
a61af66fc99e Initial load
duke
parents:
diff changeset
510
a61af66fc99e Initial load
duke
parents:
diff changeset
511 bool JNIHandleBlock::contains(jobject handle) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
512 return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
a61af66fc99e Initial load
duke
parents:
diff changeset
513 }
a61af66fc99e Initial load
duke
parents:
diff changeset
514
a61af66fc99e Initial load
duke
parents:
diff changeset
515
a61af66fc99e Initial load
duke
parents:
diff changeset
516 bool JNIHandleBlock::chain_contains(jobject handle) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
517 for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
518 if (current->contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
519 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 }
a61af66fc99e Initial load
duke
parents:
diff changeset
521 }
a61af66fc99e Initial load
duke
parents:
diff changeset
522 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
523 }
a61af66fc99e Initial load
duke
parents:
diff changeset
524
a61af66fc99e Initial load
duke
parents:
diff changeset
525
a61af66fc99e Initial load
duke
parents:
diff changeset
526 int JNIHandleBlock::length() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
527 int result = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
528 for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 result++;
a61af66fc99e Initial load
duke
parents:
diff changeset
530 }
a61af66fc99e Initial load
duke
parents:
diff changeset
531 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533
a61af66fc99e Initial load
duke
parents:
diff changeset
534 // This method is not thread-safe, i.e., must be called whule holding a lock on the
a61af66fc99e Initial load
duke
parents:
diff changeset
535 // structure.
a61af66fc99e Initial load
duke
parents:
diff changeset
536 long JNIHandleBlock::memory_usage() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
537 return length() * sizeof(JNIHandleBlock);
a61af66fc99e Initial load
duke
parents:
diff changeset
538 }
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540
a61af66fc99e Initial load
duke
parents:
diff changeset
541 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
542
a61af66fc99e Initial load
duke
parents:
diff changeset
543 bool JNIHandleBlock::any_contains(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
544 for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
a61af66fc99e Initial load
duke
parents:
diff changeset
545 if (current->contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
546 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 }
a61af66fc99e Initial load
duke
parents:
diff changeset
548 }
a61af66fc99e Initial load
duke
parents:
diff changeset
549 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
550 }
a61af66fc99e Initial load
duke
parents:
diff changeset
551
a61af66fc99e Initial load
duke
parents:
diff changeset
552 void JNIHandleBlock::print_statistics() {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 int used_blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
554 int free_blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
555 int used_handles = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
556 int free_handles = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
557 JNIHandleBlock* block = _block_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
558 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
559 if (block->_top > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
560 used_blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
561 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
562 free_blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
563 }
a61af66fc99e Initial load
duke
parents:
diff changeset
564 used_handles += block->_top;
a61af66fc99e Initial load
duke
parents:
diff changeset
565 free_handles += (block_size_in_oops - block->_top);
a61af66fc99e Initial load
duke
parents:
diff changeset
566 block = block->_block_list_link;
a61af66fc99e Initial load
duke
parents:
diff changeset
567 }
a61af66fc99e Initial load
duke
parents:
diff changeset
568 tty->print_cr("JNIHandleBlocks statistics");
a61af66fc99e Initial load
duke
parents:
diff changeset
569 tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
570 tty->print_cr("- blocks in use: %d", used_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
571 tty->print_cr("- blocks free: %d", free_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
572 tty->print_cr("- handles in use: %d", used_handles);
a61af66fc99e Initial load
duke
parents:
diff changeset
573 tty->print_cr("- handles free: %d", free_handles);
a61af66fc99e Initial load
duke
parents:
diff changeset
574 }
a61af66fc99e Initial load
duke
parents:
diff changeset
575
a61af66fc99e Initial load
duke
parents:
diff changeset
576 #endif