src/share/vm/runtime/jniHandles.cpp

Mon, 28 May 2018 10:33:52 +0800

author
aoqi
date
Mon, 28 May 2018 10:33:52 +0800
changeset 9041
95a08233f46c
parent 8890
16939858a716
parent 7994
04ff2f6cd0eb
child 9703
2fdf635bcf28
permissions
-rw-r--r--

Merge

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

mercurial