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