src/share/vm/runtime/jniHandles.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/jniHandles.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,574 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "oops/oop.inline.hpp"
    1.31 +#include "prims/jvmtiExport.hpp"
    1.32 +#include "runtime/jniHandles.hpp"
    1.33 +#include "runtime/mutexLocker.hpp"
    1.34 +#include "runtime/thread.inline.hpp"
    1.35 +
    1.36 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.37 +
    1.38 +JNIHandleBlock* JNIHandles::_global_handles       = NULL;
    1.39 +JNIHandleBlock* JNIHandles::_weak_global_handles  = NULL;
    1.40 +oop             JNIHandles::_deleted_handle       = NULL;
    1.41 +
    1.42 +
    1.43 +jobject JNIHandles::make_local(oop obj) {
    1.44 +  if (obj == NULL) {
    1.45 +    return NULL;                // ignore null handles
    1.46 +  } else {
    1.47 +    Thread* thread = Thread::current();
    1.48 +    assert(Universe::heap()->is_in_reserved(obj), "sanity check");
    1.49 +    return thread->active_handles()->allocate_handle(obj);
    1.50 +  }
    1.51 +}
    1.52 +
    1.53 +
    1.54 +// optimized versions
    1.55 +
    1.56 +jobject JNIHandles::make_local(Thread* thread, oop obj) {
    1.57 +  if (obj == NULL) {
    1.58 +    return NULL;                // ignore null handles
    1.59 +  } else {
    1.60 +    assert(Universe::heap()->is_in_reserved(obj), "sanity check");
    1.61 +    return thread->active_handles()->allocate_handle(obj);
    1.62 +  }
    1.63 +}
    1.64 +
    1.65 +
    1.66 +jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
    1.67 +  if (obj == NULL) {
    1.68 +    return NULL;                // ignore null handles
    1.69 +  } else {
    1.70 +    JavaThread* thread = JavaThread::thread_from_jni_environment(env);
    1.71 +    assert(Universe::heap()->is_in_reserved(obj), "sanity check");
    1.72 +    return thread->active_handles()->allocate_handle(obj);
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +
    1.77 +jobject JNIHandles::make_global(Handle obj) {
    1.78 +  assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
    1.79 +  jobject res = NULL;
    1.80 +  if (!obj.is_null()) {
    1.81 +    // ignore null handles
    1.82 +    MutexLocker ml(JNIGlobalHandle_lock);
    1.83 +    assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
    1.84 +    res = _global_handles->allocate_handle(obj());
    1.85 +  } else {
    1.86 +    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
    1.87 +  }
    1.88 +
    1.89 +  return res;
    1.90 +}
    1.91 +
    1.92 +
    1.93 +jobject JNIHandles::make_weak_global(Handle obj) {
    1.94 +  assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
    1.95 +  jobject res = NULL;
    1.96 +  if (!obj.is_null()) {
    1.97 +    // ignore null handles
    1.98 +    MutexLocker ml(JNIGlobalHandle_lock);
    1.99 +    assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
   1.100 +    res = _weak_global_handles->allocate_handle(obj());
   1.101 +  } else {
   1.102 +    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
   1.103 +  }
   1.104 +  return res;
   1.105 +}
   1.106 +
   1.107 +
   1.108 +void JNIHandles::destroy_global(jobject handle) {
   1.109 +  if (handle != NULL) {
   1.110 +    assert(is_global_handle(handle), "Invalid delete of global JNI handle");
   1.111 +    *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +
   1.116 +void JNIHandles::destroy_weak_global(jobject handle) {
   1.117 +  if (handle != NULL) {
   1.118 +    assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle");
   1.119 +    *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
   1.120 +  }
   1.121 +}
   1.122 +
   1.123 +
   1.124 +void JNIHandles::oops_do(OopClosure* f) {
   1.125 +  f->do_oop(&_deleted_handle);
   1.126 +  _global_handles->oops_do(f);
   1.127 +}
   1.128 +
   1.129 +
   1.130 +void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
   1.131 +  _weak_global_handles->weak_oops_do(is_alive, f);
   1.132 +}
   1.133 +
   1.134 +
   1.135 +void JNIHandles::initialize() {
   1.136 +  _global_handles      = JNIHandleBlock::allocate_block();
   1.137 +  _weak_global_handles = JNIHandleBlock::allocate_block();
   1.138 +  EXCEPTION_MARK;
   1.139 +  // We will never reach the CATCH below since Exceptions::_throw will cause
   1.140 +  // the VM to exit if an exception is thrown during initialization
   1.141 +  Klass* k      = SystemDictionary::Object_klass();
   1.142 +  _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH);
   1.143 +}
   1.144 +
   1.145 +
   1.146 +bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
   1.147 +  JNIHandleBlock* block = thread->active_handles();
   1.148 +
   1.149 +  // Look back past possible native calls to jni_PushLocalFrame.
   1.150 +  while (block != NULL) {
   1.151 +    if (block->chain_contains(handle)) {
   1.152 +      return true;
   1.153 +    }
   1.154 +    block = block->pop_frame_link();
   1.155 +  }
   1.156 +  return false;
   1.157 +}
   1.158 +
   1.159 +
   1.160 +// Determine if the handle is somewhere in the current thread's stack.
   1.161 +// We easily can't isolate any particular stack frame the handle might
   1.162 +// come from, so we'll check the whole stack.
   1.163 +
   1.164 +bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) {
   1.165 +  // If there is no java frame, then this must be top level code, such
   1.166 +  // as the java command executable, in which case, this type of handle
   1.167 +  // is not permitted.
   1.168 +  return (thr->has_last_Java_frame() &&
   1.169 +         (void*)obj < (void*)thr->stack_base() &&
   1.170 +         (void*)obj >= (void*)thr->last_Java_sp());
   1.171 +}
   1.172 +
   1.173 +
   1.174 +bool JNIHandles::is_global_handle(jobject handle) {
   1.175 +  return _global_handles->chain_contains(handle);
   1.176 +}
   1.177 +
   1.178 +
   1.179 +bool JNIHandles::is_weak_global_handle(jobject handle) {
   1.180 +  return _weak_global_handles->chain_contains(handle);
   1.181 +}
   1.182 +
   1.183 +long JNIHandles::global_handle_memory_usage() {
   1.184 +  return _global_handles->memory_usage();
   1.185 +}
   1.186 +
   1.187 +long JNIHandles::weak_global_handle_memory_usage() {
   1.188 +  return _weak_global_handles->memory_usage();
   1.189 +}
   1.190 +
   1.191 +
   1.192 +class AlwaysAliveClosure: public BoolObjectClosure {
   1.193 +public:
   1.194 +  bool do_object_b(oop obj) { return true; }
   1.195 +};
   1.196 +
   1.197 +class CountHandleClosure: public OopClosure {
   1.198 +private:
   1.199 +  int _count;
   1.200 +public:
   1.201 +  CountHandleClosure(): _count(0) {}
   1.202 +  virtual void do_oop(oop* unused) {
   1.203 +    _count++;
   1.204 +  }
   1.205 +  virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
   1.206 +  int count() { return _count; }
   1.207 +};
   1.208 +
   1.209 +// We assume this is called at a safepoint: no lock is needed.
   1.210 +void JNIHandles::print_on(outputStream* st) {
   1.211 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
   1.212 +  assert(_global_handles != NULL && _weak_global_handles != NULL,
   1.213 +         "JNIHandles not initialized");
   1.214 +
   1.215 +  CountHandleClosure global_handle_count;
   1.216 +  AlwaysAliveClosure always_alive;
   1.217 +  oops_do(&global_handle_count);
   1.218 +  weak_oops_do(&always_alive, &global_handle_count);
   1.219 +
   1.220 +  st->print_cr("JNI global references: %d", global_handle_count.count());
   1.221 +  st->cr();
   1.222 +  st->flush();
   1.223 +}
   1.224 +
   1.225 +class VerifyHandleClosure: public OopClosure {
   1.226 +public:
   1.227 +  virtual void do_oop(oop* root) {
   1.228 +    (*root)->verify();
   1.229 +  }
   1.230 +  virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
   1.231 +};
   1.232 +
   1.233 +void JNIHandles::verify() {
   1.234 +  VerifyHandleClosure verify_handle;
   1.235 +  AlwaysAliveClosure always_alive;
   1.236 +
   1.237 +  oops_do(&verify_handle);
   1.238 +  weak_oops_do(&always_alive, &verify_handle);
   1.239 +}
   1.240 +
   1.241 +
   1.242 +
   1.243 +void jni_handles_init() {
   1.244 +  JNIHandles::initialize();
   1.245 +}
   1.246 +
   1.247 +
   1.248 +int             JNIHandleBlock::_blocks_allocated     = 0;
   1.249 +JNIHandleBlock* JNIHandleBlock::_block_free_list      = NULL;
   1.250 +#ifndef PRODUCT
   1.251 +JNIHandleBlock* JNIHandleBlock::_block_list           = NULL;
   1.252 +#endif
   1.253 +
   1.254 +
   1.255 +void JNIHandleBlock::zap() {
   1.256 +  // Zap block values
   1.257 +  _top  = 0;
   1.258 +  for (int index = 0; index < block_size_in_oops; index++) {
   1.259 +    _handles[index] = badJNIHandle;
   1.260 +  }
   1.261 +}
   1.262 +
   1.263 +JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread)  {
   1.264 +  assert(thread == NULL || thread == Thread::current(), "sanity check");
   1.265 +  JNIHandleBlock* block;
   1.266 +  // Check the thread-local free list for a block so we don't
   1.267 +  // have to acquire a mutex.
   1.268 +  if (thread != NULL && thread->free_handle_block() != NULL) {
   1.269 +    block = thread->free_handle_block();
   1.270 +    thread->set_free_handle_block(block->_next);
   1.271 +  }
   1.272 +  else {
   1.273 +    // locking with safepoint checking introduces a potential deadlock:
   1.274 +    // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
   1.275 +    // - another would hold Threads_lock (jni_AttachCurrentThread) and then
   1.276 +    //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
   1.277 +    MutexLockerEx ml(JNIHandleBlockFreeList_lock,
   1.278 +                     Mutex::_no_safepoint_check_flag);
   1.279 +    if (_block_free_list == NULL) {
   1.280 +      // Allocate new block
   1.281 +      block = new JNIHandleBlock();
   1.282 +      _blocks_allocated++;
   1.283 +      if (TraceJNIHandleAllocation) {
   1.284 +        tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)",
   1.285 +                      block, _blocks_allocated);
   1.286 +      }
   1.287 +      if (ZapJNIHandleArea) block->zap();
   1.288 +      #ifndef PRODUCT
   1.289 +      // Link new block to list of all allocated blocks
   1.290 +      block->_block_list_link = _block_list;
   1.291 +      _block_list = block;
   1.292 +      #endif
   1.293 +    } else {
   1.294 +      // Get block from free list
   1.295 +      block = _block_free_list;
   1.296 +      _block_free_list = _block_free_list->_next;
   1.297 +    }
   1.298 +  }
   1.299 +  block->_top  = 0;
   1.300 +  block->_next = NULL;
   1.301 +  block->_pop_frame_link = NULL;
   1.302 +  // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
   1.303 +  debug_only(block->_last = NULL);
   1.304 +  debug_only(block->_free_list = NULL);
   1.305 +  debug_only(block->_allocate_before_rebuild = -1);
   1.306 +  return block;
   1.307 +}
   1.308 +
   1.309 +
   1.310 +void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
   1.311 +  assert(thread == NULL || thread == Thread::current(), "sanity check");
   1.312 +  JNIHandleBlock* pop_frame_link = block->pop_frame_link();
   1.313 +  // Put returned block at the beginning of the thread-local free list.
   1.314 +  // Note that if thread == NULL, we use it as an implicit argument that
   1.315 +  // we _don't_ want the block to be kept on the free_handle_block.
   1.316 +  // See for instance JavaThread::exit().
   1.317 +  if (thread != NULL ) {
   1.318 +    if (ZapJNIHandleArea) block->zap();
   1.319 +    JNIHandleBlock* freelist = thread->free_handle_block();
   1.320 +    block->_pop_frame_link = NULL;
   1.321 +    thread->set_free_handle_block(block);
   1.322 +
   1.323 +    // Add original freelist to end of chain
   1.324 +    if ( freelist != NULL ) {
   1.325 +      while ( block->_next != NULL ) block = block->_next;
   1.326 +      block->_next = freelist;
   1.327 +    }
   1.328 +    block = NULL;
   1.329 +  }
   1.330 +  if (block != NULL) {
   1.331 +    // Return blocks to free list
   1.332 +    // locking with safepoint checking introduces a potential deadlock:
   1.333 +    // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
   1.334 +    // - another would hold Threads_lock (jni_AttachCurrentThread) and then
   1.335 +    //   JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
   1.336 +    MutexLockerEx ml(JNIHandleBlockFreeList_lock,
   1.337 +                     Mutex::_no_safepoint_check_flag);
   1.338 +    while (block != NULL) {
   1.339 +      if (ZapJNIHandleArea) block->zap();
   1.340 +      JNIHandleBlock* next = block->_next;
   1.341 +      block->_next = _block_free_list;
   1.342 +      _block_free_list = block;
   1.343 +      block = next;
   1.344 +    }
   1.345 +  }
   1.346 +  if (pop_frame_link != NULL) {
   1.347 +    // As a sanity check we release blocks pointed to by the pop_frame_link.
   1.348 +    // This should never happen (only if PopLocalFrame is not called the
   1.349 +    // correct number of times).
   1.350 +    release_block(pop_frame_link, thread);
   1.351 +  }
   1.352 +}
   1.353 +
   1.354 +
   1.355 +void JNIHandleBlock::oops_do(OopClosure* f) {
   1.356 +  JNIHandleBlock* current_chain = this;
   1.357 +  // Iterate over chain of blocks, followed by chains linked through the
   1.358 +  // pop frame links.
   1.359 +  while (current_chain != NULL) {
   1.360 +    for (JNIHandleBlock* current = current_chain; current != NULL;
   1.361 +         current = current->_next) {
   1.362 +      assert(current == current_chain || current->pop_frame_link() == NULL,
   1.363 +        "only blocks first in chain should have pop frame link set");
   1.364 +      for (int index = 0; index < current->_top; index++) {
   1.365 +        oop* root = &(current->_handles)[index];
   1.366 +        oop value = *root;
   1.367 +        // traverse heap pointers only, not deleted handles or free list
   1.368 +        // pointers
   1.369 +        if (value != NULL && Universe::heap()->is_in_reserved(value)) {
   1.370 +          f->do_oop(root);
   1.371 +        }
   1.372 +      }
   1.373 +      // the next handle block is valid only if current block is full
   1.374 +      if (current->_top < block_size_in_oops) {
   1.375 +        break;
   1.376 +      }
   1.377 +    }
   1.378 +    current_chain = current_chain->pop_frame_link();
   1.379 +  }
   1.380 +}
   1.381 +
   1.382 +
   1.383 +void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive,
   1.384 +                                  OopClosure* f) {
   1.385 +  for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
   1.386 +    assert(current->pop_frame_link() == NULL,
   1.387 +      "blocks holding weak global JNI handles should not have pop frame link set");
   1.388 +    for (int index = 0; index < current->_top; index++) {
   1.389 +      oop* root = &(current->_handles)[index];
   1.390 +      oop value = *root;
   1.391 +      // traverse heap pointers only, not deleted handles or free list pointers
   1.392 +      if (value != NULL && Universe::heap()->is_in_reserved(value)) {
   1.393 +        if (is_alive->do_object_b(value)) {
   1.394 +          // The weakly referenced object is alive, update pointer
   1.395 +          f->do_oop(root);
   1.396 +        } else {
   1.397 +          // The weakly referenced object is not alive, clear the reference by storing NULL
   1.398 +          if (TraceReferenceGC) {
   1.399 +            tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root);
   1.400 +          }
   1.401 +          *root = NULL;
   1.402 +        }
   1.403 +      }
   1.404 +    }
   1.405 +    // the next handle block is valid only if current block is full
   1.406 +    if (current->_top < block_size_in_oops) {
   1.407 +      break;
   1.408 +    }
   1.409 +  }
   1.410 +
   1.411 +  /*
   1.412 +   * JVMTI data structures may also contain weak oops.  The iteration of them
   1.413 +   * is placed here so that we don't need to add it to each of the collectors.
   1.414 +   */
   1.415 +  JvmtiExport::weak_oops_do(is_alive, f);
   1.416 +}
   1.417 +
   1.418 +
   1.419 +jobject JNIHandleBlock::allocate_handle(oop obj) {
   1.420 +  assert(Universe::heap()->is_in_reserved(obj), "sanity check");
   1.421 +  if (_top == 0) {
   1.422 +    // This is the first allocation or the initial block got zapped when
   1.423 +    // entering a native function. If we have any following blocks they are
   1.424 +    // not valid anymore.
   1.425 +    for (JNIHandleBlock* current = _next; current != NULL;
   1.426 +         current = current->_next) {
   1.427 +      assert(current->_last == NULL, "only first block should have _last set");
   1.428 +      assert(current->_free_list == NULL,
   1.429 +             "only first block should have _free_list set");
   1.430 +      current->_top = 0;
   1.431 +      if (ZapJNIHandleArea) current->zap();
   1.432 +    }
   1.433 +    // Clear initial block
   1.434 +    _free_list = NULL;
   1.435 +    _allocate_before_rebuild = 0;
   1.436 +    _last = this;
   1.437 +    if (ZapJNIHandleArea) zap();
   1.438 +  }
   1.439 +
   1.440 +  // Try last block
   1.441 +  if (_last->_top < block_size_in_oops) {
   1.442 +    oop* handle = &(_last->_handles)[_last->_top++];
   1.443 +    *handle = obj;
   1.444 +    return (jobject) handle;
   1.445 +  }
   1.446 +
   1.447 +  // Try free list
   1.448 +  if (_free_list != NULL) {
   1.449 +    oop* handle = _free_list;
   1.450 +    _free_list = (oop*) *_free_list;
   1.451 +    *handle = obj;
   1.452 +    return (jobject) handle;
   1.453 +  }
   1.454 +  // Check if unused block follow last
   1.455 +  if (_last->_next != NULL) {
   1.456 +    // update last and retry
   1.457 +    _last = _last->_next;
   1.458 +    return allocate_handle(obj);
   1.459 +  }
   1.460 +
   1.461 +  // No space available, we have to rebuild free list or expand
   1.462 +  if (_allocate_before_rebuild == 0) {
   1.463 +      rebuild_free_list();        // updates _allocate_before_rebuild counter
   1.464 +  } else {
   1.465 +    // Append new block
   1.466 +    Thread* thread = Thread::current();
   1.467 +    Handle obj_handle(thread, obj);
   1.468 +    // This can block, so we need to preserve obj accross call.
   1.469 +    _last->_next = JNIHandleBlock::allocate_block(thread);
   1.470 +    _last = _last->_next;
   1.471 +    _allocate_before_rebuild--;
   1.472 +    obj = obj_handle();
   1.473 +  }
   1.474 +  return allocate_handle(obj);  // retry
   1.475 +}
   1.476 +
   1.477 +
   1.478 +void JNIHandleBlock::rebuild_free_list() {
   1.479 +  assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
   1.480 +  int free = 0;
   1.481 +  int blocks = 0;
   1.482 +  for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
   1.483 +    for (int index = 0; index < current->_top; index++) {
   1.484 +      oop* handle = &(current->_handles)[index];
   1.485 +      if (*handle ==  JNIHandles::deleted_handle()) {
   1.486 +        // this handle was cleared out by a delete call, reuse it
   1.487 +        *handle = (oop) _free_list;
   1.488 +        _free_list = handle;
   1.489 +        free++;
   1.490 +      }
   1.491 +    }
   1.492 +    // we should not rebuild free list if there are unused handles at the end
   1.493 +    assert(current->_top == block_size_in_oops, "just checking");
   1.494 +    blocks++;
   1.495 +  }
   1.496 +  // Heuristic: if more than half of the handles are free we rebuild next time
   1.497 +  // as well, otherwise we append a corresponding number of new blocks before
   1.498 +  // attempting a free list rebuild again.
   1.499 +  int total = blocks * block_size_in_oops;
   1.500 +  int extra = total - 2*free;
   1.501 +  if (extra > 0) {
   1.502 +    // Not as many free handles as we would like - compute number of new blocks to append
   1.503 +    _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
   1.504 +  }
   1.505 +  if (TraceJNIHandleAllocation) {
   1.506 +    tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d",
   1.507 +      this, blocks, total-free, free, _allocate_before_rebuild);
   1.508 +  }
   1.509 +}
   1.510 +
   1.511 +
   1.512 +bool JNIHandleBlock::contains(jobject handle) const {
   1.513 +  return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
   1.514 +}
   1.515 +
   1.516 +
   1.517 +bool JNIHandleBlock::chain_contains(jobject handle) const {
   1.518 +  for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
   1.519 +    if (current->contains(handle)) {
   1.520 +      return true;
   1.521 +    }
   1.522 +  }
   1.523 +  return false;
   1.524 +}
   1.525 +
   1.526 +
   1.527 +int JNIHandleBlock::length() const {
   1.528 +  int result = 1;
   1.529 +  for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
   1.530 +    result++;
   1.531 +  }
   1.532 +  return result;
   1.533 +}
   1.534 +
   1.535 +// This method is not thread-safe, i.e., must be called whule holding a lock on the
   1.536 +// structure.
   1.537 +long JNIHandleBlock::memory_usage() const {
   1.538 +  return length() * sizeof(JNIHandleBlock);
   1.539 +}
   1.540 +
   1.541 +
   1.542 +#ifndef PRODUCT
   1.543 +
   1.544 +bool JNIHandleBlock::any_contains(jobject handle) {
   1.545 +  for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
   1.546 +    if (current->contains(handle)) {
   1.547 +      return true;
   1.548 +    }
   1.549 +  }
   1.550 +  return false;
   1.551 +}
   1.552 +
   1.553 +void JNIHandleBlock::print_statistics() {
   1.554 +  int used_blocks = 0;
   1.555 +  int free_blocks = 0;
   1.556 +  int used_handles = 0;
   1.557 +  int free_handles = 0;
   1.558 +  JNIHandleBlock* block = _block_list;
   1.559 +  while (block != NULL) {
   1.560 +    if (block->_top > 0) {
   1.561 +      used_blocks++;
   1.562 +    } else {
   1.563 +      free_blocks++;
   1.564 +    }
   1.565 +    used_handles += block->_top;
   1.566 +    free_handles += (block_size_in_oops - block->_top);
   1.567 +    block = block->_block_list_link;
   1.568 +  }
   1.569 +  tty->print_cr("JNIHandleBlocks statistics");
   1.570 +  tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
   1.571 +  tty->print_cr("- blocks in use:    %d", used_blocks);
   1.572 +  tty->print_cr("- blocks free:      %d", free_blocks);
   1.573 +  tty->print_cr("- handles in use:   %d", used_handles);
   1.574 +  tty->print_cr("- handles free:     %d", free_handles);
   1.575 +}
   1.576 +
   1.577 +#endif

mercurial