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