1 /*
   2  * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/oopStorage.inline.hpp"
  27 #include "logging/log.hpp"
  28 #include "memory/iterator.hpp"
  29 #include "oops/access.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "runtime/jniHandles.inline.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "runtime/thread.inline.hpp"
  35 #include "utilities/align.hpp"
  36 #include "utilities/debug.hpp"
  37 
  38 OopStorage* JNIHandles::_global_handles = NULL;
  39 OopStorage* JNIHandles::_weak_global_handles = NULL;
  40 
  41 OopStorage* JNIHandles::global_handles() {
  42   assert(_global_handles != NULL, "Uninitialized JNI global handles");
  43   return _global_handles;
  44 }
  45 
  46 OopStorage* JNIHandles::weak_global_handles() {
  47   assert(_weak_global_handles != NULL, "Uninitialized JNI weak global handles");
  48   return _weak_global_handles;
  49 }
  50 
  51 
  52 jobject JNIHandles::make_local(oop obj) {
  53   if (obj == NULL) {
  54     return NULL;                // ignore null handles
  55   } else {
  56     Thread* thread = Thread::current();
  57     assert(oopDesc::is_oop(obj), "not an oop");
  58     assert(!current_thread_in_native(), "must not be in native");
  59     return thread->active_handles()->allocate_handle(obj);
  60   }
  61 }
  62 
  63 
  64 // optimized versions
  65 
  66 jobject JNIHandles::make_local(Thread* thread, oop obj) {
  67   if (obj == NULL) {
  68     return NULL;                // ignore null handles
  69   } else {
  70     assert(oopDesc::is_oop(obj), "not an oop");
  71     assert(thread->is_Java_thread(), "not a Java thread");
  72     assert(!current_thread_in_native(), "must not be in native");
  73     return thread->active_handles()->allocate_handle(obj);
  74   }
  75 }
  76 
  77 
  78 jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
  79   if (obj == NULL) {
  80     return NULL;                // ignore null handles
  81   } else {
  82     JavaThread* thread = JavaThread::thread_from_jni_environment(env);
  83     assert(oopDesc::is_oop(obj), "not an oop");
  84     assert(!current_thread_in_native(), "must not be in native");
  85     return thread->active_handles()->allocate_handle(obj);
  86   }
  87 }
  88 
  89 
  90 static void report_handle_allocation_failure(AllocFailType alloc_failmode,
  91                                              const char* handle_kind) {
  92   if (alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  93     // Fake size value, since we don't know the min allocation size here.
  94     vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR,
  95                           "Cannot create %s JNI handle", handle_kind);
  96   } else {
  97     assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "invariant");
  98   }
  99 }
 100 
 101 jobject JNIHandles::make_global(Handle obj, AllocFailType alloc_failmode) {
 102   assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
 103   assert(!current_thread_in_native(), "must not be in native");
 104   jobject res = NULL;
 105   if (!obj.is_null()) {
 106     // ignore null handles
 107     assert(oopDesc::is_oop(obj()), "not an oop");
 108     oop* ptr = global_handles()->allocate();
 109     // Return NULL on allocation failure.
 110     if (ptr != NULL) {
 111       assert(*ptr == NULL, "invariant");
 112       NativeAccess<>::oop_store(ptr, obj());
 113       res = reinterpret_cast<jobject>(ptr);
 114     } else {
 115       report_handle_allocation_failure(alloc_failmode, "global");
 116     }
 117   } else {
 118     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
 119   }
 120 
 121   return res;
 122 }
 123 
 124 
 125 jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) {
 126   assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
 127   assert(!current_thread_in_native(), "must not be in native");
 128   jobject res = NULL;
 129   if (!obj.is_null()) {
 130     // ignore null handles
 131     assert(oopDesc::is_oop(obj()), "not an oop");
 132     oop* ptr = weak_global_handles()->allocate();
 133     // Return NULL on allocation failure.
 134     if (ptr != NULL) {
 135       assert(*ptr == NULL, "invariant");
 136       NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(ptr, obj());
 137       char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value;
 138       res = reinterpret_cast<jobject>(tptr);
 139     } else {
 140       report_handle_allocation_failure(alloc_failmode, "weak global");
 141     }
 142   } else {
 143     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
 144   }
 145   return res;
 146 }
 147 
 148 // Resolve some erroneous cases to NULL, rather than treating them as
 149 // possibly unchecked errors.  In particular, deleted handles are
 150 // treated as NULL (though a deleted and later reallocated handle
 151 // isn't detected).
 152 oop JNIHandles::resolve_external_guard(jobject handle) {
 153   oop result = NULL;
 154   if (handle != NULL) {
 155     result = resolve_impl<true /* external_guard */ >(handle);
 156   }
 157   return result;
 158 }
 159 
 160 oop JNIHandles::resolve_jweak(jweak handle) {
 161   assert(handle != NULL, "precondition");
 162   assert(is_jweak(handle), "precondition");
 163   return NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(jweak_ptr(handle));
 164 }
 165 
 166 bool JNIHandles::is_global_weak_cleared(jweak handle) {
 167   assert(handle != NULL, "precondition");
 168   assert(is_jweak(handle), "not a weak handle");
 169   oop* oop_ptr = jweak_ptr(handle);
 170   oop value = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(oop_ptr);
 171   return value == NULL;
 172 }
 173 
 174 void JNIHandles::destroy_global(jobject handle) {
 175   if (handle != NULL) {
 176     assert(!is_jweak(handle), "wrong method for detroying jweak");
 177     oop* oop_ptr = jobject_ptr(handle);
 178     NativeAccess<>::oop_store(oop_ptr, (oop)NULL);
 179     global_handles()->release(oop_ptr);
 180   }
 181 }
 182 
 183 
 184 void JNIHandles::destroy_weak_global(jobject handle) {
 185   if (handle != NULL) {
 186     assert(is_jweak(handle), "JNI handle not jweak");
 187     oop* oop_ptr = jweak_ptr(handle);
 188     NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_ptr, (oop)NULL);
 189     weak_global_handles()->release(oop_ptr);
 190   }
 191 }
 192 
 193 
 194 void JNIHandles::oops_do(OopClosure* f) {
 195   global_handles()->oops_do(f);
 196 }
 197 
 198 
 199 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
 200   weak_global_handles()->weak_oops_do(is_alive, f);
 201 }
 202 
 203 
 204 void JNIHandles::weak_oops_do(OopClosure* f) {
 205   weak_global_handles()->weak_oops_do(f);
 206 }
 207 
 208 
 209 void JNIHandles::initialize() {
 210   _global_handles = new OopStorage("JNI Global",
 211                                    JNIGlobalAlloc_lock,
 212                                    JNIGlobalActive_lock);
 213   _weak_global_handles = new OopStorage("JNI Weak",
 214                                         JNIWeakAlloc_lock,
 215                                         JNIWeakActive_lock);
 216 }
 217 
 218 
 219 inline bool is_storage_handle(const OopStorage* storage, const oop* ptr) {
 220   return storage->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY;
 221 }
 222 
 223 
 224 jobjectRefType JNIHandles::handle_type(Thread* thread, jobject handle) {
 225   assert(handle != NULL, "precondition");
 226   jobjectRefType result = JNIInvalidRefType;
 227   if (is_jweak(handle)) {
 228     if (is_storage_handle(weak_global_handles(), jweak_ptr(handle))) {
 229       result = JNIWeakGlobalRefType;
 230     }
 231   } else {
 232     switch (global_handles()->allocation_status(jobject_ptr(handle))) {
 233     case OopStorage::ALLOCATED_ENTRY:
 234       result = JNIGlobalRefType;
 235       break;
 236 
 237     case OopStorage::UNALLOCATED_ENTRY:
 238       break;                    // Invalid global handle
 239 
 240     case OopStorage::INVALID_ENTRY:
 241       // Not in global storage.  Might be a local handle.
 242       if (is_local_handle(thread, handle) ||
 243           (thread->is_Java_thread() &&
 244            is_frame_handle((JavaThread*)thread, handle))) {
 245         result = JNILocalRefType;
 246       }
 247       break;
 248 
 249     default:
 250       ShouldNotReachHere();
 251     }
 252   }
 253   return result;
 254 }
 255 
 256 
 257 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
 258   assert(handle != NULL, "precondition");
 259   JNIHandleBlock* block = thread->active_handles();
 260 
 261   // Look back past possible native calls to jni_PushLocalFrame.
 262   while (block != NULL) {
 263     if (block->chain_contains(handle)) {
 264       return true;
 265     }
 266     block = block->pop_frame_link();
 267   }
 268   return false;
 269 }
 270 
 271 
 272 // Determine if the handle is somewhere in the current thread's stack.
 273 // We easily can't isolate any particular stack frame the handle might
 274 // come from, so we'll check the whole stack.
 275 
 276 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject handle) {
 277   assert(handle != NULL, "precondition");
 278   // If there is no java frame, then this must be top level code, such
 279   // as the java command executable, in which case, this type of handle
 280   // is not permitted.
 281   return (thr->has_last_Java_frame() &&
 282          (void*)handle < (void*)thr->stack_base() &&
 283          (void*)handle >= (void*)thr->last_Java_sp());
 284 }
 285 
 286 
 287 bool JNIHandles::is_global_handle(jobject handle) {
 288   assert(handle != NULL, "precondition");
 289   return !is_jweak(handle) && is_storage_handle(global_handles(), jobject_ptr(handle));
 290 }
 291 
 292 
 293 bool JNIHandles::is_weak_global_handle(jobject handle) {
 294   assert(handle != NULL, "precondition");
 295   return is_jweak(handle) && is_storage_handle(weak_global_handles(), jweak_ptr(handle));
 296 }
 297 
 298 size_t JNIHandles::global_handle_memory_usage() {
 299   return global_handles()->total_memory_usage();
 300 }
 301 
 302 size_t JNIHandles::weak_global_handle_memory_usage() {
 303   return weak_global_handles()->total_memory_usage();
 304 }
 305 
 306 
 307 // We assume this is called at a safepoint: no lock is needed.
 308 void JNIHandles::print_on(outputStream* st) {
 309   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 310 
 311   st->print_cr("JNI global refs: " SIZE_FORMAT ", weak refs: " SIZE_FORMAT,
 312                global_handles()->allocation_count(),
 313                weak_global_handles()->allocation_count());
 314   st->cr();
 315   st->flush();
 316 }
 317 
 318 class VerifyJNIHandles: public OopClosure {
 319 public:
 320   virtual void do_oop(oop* root) {
 321     oopDesc::verify(*root);
 322   }
 323   virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
 324 };
 325 
 326 void JNIHandles::verify() {
 327   VerifyJNIHandles verify_handle;
 328 
 329   oops_do(&verify_handle);
 330 
 331   // JNI weaks are handled concurrently in ZGC, so they can't be verified here
 332   if (!UseZGC) {
 333     weak_oops_do(&verify_handle);
 334   }
 335 }
 336 
 337 // This method is implemented here to avoid circular includes between
 338 // jniHandles.hpp and thread.hpp.
 339 bool JNIHandles::current_thread_in_native() {
 340   Thread* thread = Thread::current();
 341   return (thread->is_Java_thread() &&
 342           JavaThread::current()->thread_state() == _thread_in_native);
 343 }
 344 
 345 
 346 void jni_handles_init() {
 347   JNIHandles::initialize();
 348 }
 349 
 350 
 351 int             JNIHandleBlock::_blocks_allocated     = 0;
 352 JNIHandleBlock* JNIHandleBlock::_block_free_list      = NULL;
 353 #ifndef PRODUCT
 354 JNIHandleBlock* JNIHandleBlock::_block_list           = NULL;
 355 #endif
 356 
 357 
 358 #ifdef ASSERT
 359 void JNIHandleBlock::zap() {
 360   // Zap block values
 361   _top = 0;
 362   for (int index = 0; index < block_size_in_oops; index++) {
 363     // NOT using Access here; just bare clobbering to NULL, since the
 364     // block no longer contains valid oops.
 365     _handles[index] = NULL;
 366   }
 367 }
 368 #endif // ASSERT
 369 
 370 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread)  {
 371   assert(thread == NULL || thread == Thread::current(), "sanity check");
 372   JNIHandleBlock* block;
 373   // Check the thread-local free list for a block so we don't
 374   // have to acquire a mutex.
 375   if (thread != NULL && thread->free_handle_block() != NULL) {
 376     block = thread->free_handle_block();
 377     thread->set_free_handle_block(block->_next);
 378   }
 379   else {
 380     // locking with safepoint checking introduces a potential deadlock:
 381     // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
 382     // - another would hold Threads_lock (jni_AttachCurrentThread) and then
 383     //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
 384     MutexLockerEx ml(JNIHandleBlockFreeList_lock,
 385                      Mutex::_no_safepoint_check_flag);
 386     if (_block_free_list == NULL) {
 387       // Allocate new block
 388       block = new JNIHandleBlock();
 389       _blocks_allocated++;
 390       block->zap();
 391       #ifndef PRODUCT
 392       // Link new block to list of all allocated blocks
 393       block->_block_list_link = _block_list;
 394       _block_list = block;
 395       #endif
 396     } else {
 397       // Get block from free list
 398       block = _block_free_list;
 399       _block_free_list = _block_free_list->_next;
 400     }
 401   }
 402   block->_top = 0;
 403   block->_next = NULL;
 404   block->_pop_frame_link = NULL;
 405   block->_planned_capacity = block_size_in_oops;
 406   // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
 407   debug_only(block->_last = NULL);
 408   debug_only(block->_free_list = NULL);
 409   debug_only(block->_allocate_before_rebuild = -1);
 410   return block;
 411 }
 412 
 413 
 414 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
 415   assert(thread == NULL || thread == Thread::current(), "sanity check");
 416   JNIHandleBlock* pop_frame_link = block->pop_frame_link();
 417   // Put returned block at the beginning of the thread-local free list.
 418   // Note that if thread == NULL, we use it as an implicit argument that
 419   // we _don't_ want the block to be kept on the free_handle_block.
 420   // See for instance JavaThread::exit().
 421   if (thread != NULL ) {
 422     block->zap();
 423     JNIHandleBlock* freelist = thread->free_handle_block();
 424     block->_pop_frame_link = NULL;
 425     thread->set_free_handle_block(block);
 426 
 427     // Add original freelist to end of chain
 428     if ( freelist != NULL ) {
 429       while ( block->_next != NULL ) block = block->_next;
 430       block->_next = freelist;
 431     }
 432     block = NULL;
 433   }
 434   if (block != NULL) {
 435     // Return blocks to free list
 436     // locking with safepoint checking introduces a potential deadlock:
 437     // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
 438     // - another would hold Threads_lock (jni_AttachCurrentThread) and then
 439     //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
 440     MutexLockerEx ml(JNIHandleBlockFreeList_lock,
 441                      Mutex::_no_safepoint_check_flag);
 442     while (block != NULL) {
 443       block->zap();
 444       JNIHandleBlock* next = block->_next;
 445       block->_next = _block_free_list;
 446       _block_free_list = block;
 447       block = next;
 448     }
 449   }
 450   if (pop_frame_link != NULL) {
 451     // As a sanity check we release blocks pointed to by the pop_frame_link.
 452     // This should never happen (only if PopLocalFrame is not called the
 453     // correct number of times).
 454     release_block(pop_frame_link, thread);
 455   }
 456 }
 457 
 458 
 459 void JNIHandleBlock::oops_do(OopClosure* f) {
 460   JNIHandleBlock* current_chain = this;
 461   // Iterate over chain of blocks, followed by chains linked through the
 462   // pop frame links.
 463   while (current_chain != NULL) {
 464     for (JNIHandleBlock* current = current_chain; current != NULL;
 465          current = current->_next) {
 466       assert(current == current_chain || current->pop_frame_link() == NULL,
 467         "only blocks first in chain should have pop frame link set");
 468       for (int index = 0; index < current->_top; index++) {
 469         oop* root = &(current->_handles)[index];
 470         oop value = *root;
 471         // traverse heap pointers only, not deleted handles or free list
 472         // pointers
 473         if (value != NULL && Universe::heap()->is_in_reserved(value)) {
 474           f->do_oop(root);
 475         }
 476       }
 477       // the next handle block is valid only if current block is full
 478       if (current->_top < block_size_in_oops) {
 479         break;
 480       }
 481     }
 482     current_chain = current_chain->pop_frame_link();
 483   }
 484 }
 485 
 486 
 487 jobject JNIHandleBlock::allocate_handle(oop obj) {
 488   assert(Universe::heap()->is_in_reserved(obj), "sanity check");
 489   if (_top == 0) {
 490     // This is the first allocation or the initial block got zapped when
 491     // entering a native function. If we have any following blocks they are
 492     // not valid anymore.
 493     for (JNIHandleBlock* current = _next; current != NULL;
 494          current = current->_next) {
 495       assert(current->_last == NULL, "only first block should have _last set");
 496       assert(current->_free_list == NULL,
 497              "only first block should have _free_list set");
 498       if (current->_top == 0) {
 499         // All blocks after the first clear trailing block are already cleared.
 500 #ifdef ASSERT
 501         for (current = current->_next; current != NULL; current = current->_next) {
 502           assert(current->_top == 0, "trailing blocks must already be cleared");
 503         }
 504 #endif
 505         break;
 506       }
 507       current->_top = 0;
 508       current->zap();
 509     }
 510     // Clear initial block
 511     _free_list = NULL;
 512     _allocate_before_rebuild = 0;
 513     _last = this;
 514     zap();
 515   }
 516 
 517   // Try last block
 518   if (_last->_top < block_size_in_oops) {
 519     oop* handle = &(_last->_handles)[_last->_top++];
 520     NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj);
 521     return (jobject) handle;
 522   }
 523 
 524   // Try free list
 525   if (_free_list != NULL) {
 526     oop* handle = _free_list;
 527     _free_list = (oop*) *_free_list;
 528     NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj);
 529     return (jobject) handle;
 530   }
 531   // Check if unused block follow last
 532   if (_last->_next != NULL) {
 533     // update last and retry
 534     _last = _last->_next;
 535     return allocate_handle(obj);
 536   }
 537 
 538   // No space available, we have to rebuild free list or expand
 539   if (_allocate_before_rebuild == 0) {
 540       rebuild_free_list();        // updates _allocate_before_rebuild counter
 541   } else {
 542     // Append new block
 543     Thread* thread = Thread::current();
 544     Handle obj_handle(thread, obj);
 545     // This can block, so we need to preserve obj across call.
 546     _last->_next = JNIHandleBlock::allocate_block(thread);
 547     _last = _last->_next;
 548     _allocate_before_rebuild--;
 549     obj = obj_handle();
 550   }
 551   return allocate_handle(obj);  // retry
 552 }
 553 
 554 void JNIHandleBlock::rebuild_free_list() {
 555   assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
 556   int free = 0;
 557   int blocks = 0;
 558   for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
 559     for (int index = 0; index < current->_top; index++) {
 560       oop* handle = &(current->_handles)[index];
 561       if (*handle == NULL) {
 562         // this handle was cleared out by a delete call, reuse it
 563         *handle = (oop) _free_list;
 564         _free_list = handle;
 565         free++;
 566       }
 567     }
 568     // we should not rebuild free list if there are unused handles at the end
 569     assert(current->_top == block_size_in_oops, "just checking");
 570     blocks++;
 571   }
 572   // Heuristic: if more than half of the handles are free we rebuild next time
 573   // as well, otherwise we append a corresponding number of new blocks before
 574   // attempting a free list rebuild again.
 575   int total = blocks * block_size_in_oops;
 576   int extra = total - 2*free;
 577   if (extra > 0) {
 578     // Not as many free handles as we would like - compute number of new blocks to append
 579     _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
 580   }
 581 }
 582 
 583 
 584 bool JNIHandleBlock::contains(jobject handle) const {
 585   return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
 586 }
 587 
 588 
 589 bool JNIHandleBlock::chain_contains(jobject handle) const {
 590   for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
 591     if (current->contains(handle)) {
 592       return true;
 593     }
 594   }
 595   return false;
 596 }
 597 
 598 
 599 size_t JNIHandleBlock::length() const {
 600   size_t result = 1;
 601   for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
 602     result++;
 603   }
 604   return result;
 605 }
 606 
 607 class CountJNIHandleClosure: public OopClosure {
 608 private:
 609   int _count;
 610 public:
 611   CountJNIHandleClosure(): _count(0) {}
 612   virtual void do_oop(oop* ooph) { _count++; }
 613   virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
 614   int count() { return _count; }
 615 };
 616 
 617 const size_t JNIHandleBlock::get_number_of_live_handles() {
 618   CountJNIHandleClosure counter;
 619   oops_do(&counter);
 620   return counter.count();
 621 }
 622 
 623 // This method is not thread-safe, i.e., must be called while holding a lock on the
 624 // structure.
 625 size_t JNIHandleBlock::memory_usage() const {
 626   return length() * sizeof(JNIHandleBlock);
 627 }
 628 
 629 
 630 #ifndef PRODUCT
 631 
 632 bool JNIHandles::is_local_handle(jobject handle) {
 633   return JNIHandleBlock::any_contains(handle);
 634 }
 635 
 636 bool JNIHandleBlock::any_contains(jobject handle) {
 637   assert(handle != NULL, "precondition");
 638   for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
 639     if (current->contains(handle)) {
 640       return true;
 641     }
 642   }
 643   return false;
 644 }
 645 
 646 void JNIHandleBlock::print_statistics() {
 647   int used_blocks = 0;
 648   int free_blocks = 0;
 649   int used_handles = 0;
 650   int free_handles = 0;
 651   JNIHandleBlock* block = _block_list;
 652   while (block != NULL) {
 653     if (block->_top > 0) {
 654       used_blocks++;
 655     } else {
 656       free_blocks++;
 657     }
 658     used_handles += block->_top;
 659     free_handles += (block_size_in_oops - block->_top);
 660     block = block->_block_list_link;
 661   }
 662   tty->print_cr("JNIHandleBlocks statistics");
 663   tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
 664   tty->print_cr("- blocks in use:    %d", used_blocks);
 665   tty->print_cr("- blocks free:      %d", free_blocks);
 666   tty->print_cr("- handles in use:   %d", used_handles);
 667   tty->print_cr("- handles free:     %d", free_handles);
 668 }
 669 
 670 #endif