src/share/vm/runtime/jniHandles.cpp

Tue, 07 May 2019 20:38:26 +0000

author
phh
date
Tue, 07 May 2019 20:38:26 +0000
changeset 9669
32bc598624bd
parent 9665
a8441ccaff15
child 9703
2fdf635bcf28
permissions
-rw-r--r--

8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
Summary: Add tag bit to all JNI weak handles
Reviewed-by: kbarrett, coleenp, tschatzl

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

mercurial