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