src/share/vm/runtime/jniHandles.cpp

Mon, 21 Jun 2010 14:26:17 -0700

author
never
date
Mon, 21 Jun 2010 14:26:17 -0700
changeset 1971
38e8278318ca
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
Reviewed-by: dcubed, kvn, twisti

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

mercurial