8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
26 #define SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
27
28 #include "oops/oop.hpp"
29 #include "runtime/jniHandles.hpp"
30 #include "utilities/debug.hpp"
31 #include "utilities/globalDefinitions.hpp"
32
33 inline bool JNIHandles::is_jweak(jobject handle) {
34 STATIC_ASSERT(weak_tag_size == 1);
35 STATIC_ASSERT(weak_tag_value == 1);
36 return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
37 }
38
39 inline oop& JNIHandles::jobject_ref(jobject handle) {
40 assert(!is_jweak(handle), "precondition");
41 return *reinterpret_cast<oop*>(handle);
42 }
43
44 inline oop& JNIHandles::jweak_ref(jobject handle) {
45 assert(is_jweak(handle), "precondition");
46 char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
47 return *reinterpret_cast<oop*>(ptr);
48 }
49
50 // external_guard is true if called from resolve_external_guard.
51 template<bool external_guard>
52 inline oop JNIHandles::resolve_impl(jobject handle) {
53 assert(handle != NULL, "precondition");
54 assert(!current_thread_in_native(), "must not be in native");
55 oop result;
56 if (is_jweak(handle)) { // Unlikely
57 result = resolve_jweak(handle);
58 } else {
59 result = jobject_ref(handle);
60 // Construction of jobjects canonicalize a null value into a null
61 // jobject, so for non-jweak the pointee should never be null.
62 assert(external_guard || result != NULL, "Invalid JNI handle");
63 }
64 return result;
65 }
66
67 inline oop JNIHandles::resolve(jobject handle) {
68 oop result = NULL;
69 if (handle != NULL) {
70 result = resolve_impl<false /* external_guard */ >(handle);
71 }
72 return result;
73 }
74
75 inline oop JNIHandles::resolve_non_null(jobject handle) {
76 assert(handle != NULL, "JNI handle should not be null");
77 oop result = resolve_impl<false /* external_guard */ >(handle);
78 assert(result != NULL, "NULL read from jni handle");
79 return result;
80 }
81
82 inline void JNIHandles::destroy_local(jobject handle) {
83 if (handle != NULL) {
84 assert(!is_jweak(handle), "Invalid JNI local handle");
85 jobject_ref(handle) = NULL;
86 }
87 }
88
89 #endif // SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
90
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
26 #define SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
27
28 #include "oops/access.inline.hpp"
29 #include "oops/oop.hpp"
30 #include "runtime/jniHandles.hpp"
31 #include "utilities/debug.hpp"
32 #include "utilities/globalDefinitions.hpp"
33
34 inline bool JNIHandles::is_jweak(jobject handle) {
35 STATIC_ASSERT(weak_tag_size == 1);
36 STATIC_ASSERT(weak_tag_value == 1);
37 return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;
38 }
39
40 inline oop* JNIHandles::jobject_ptr(jobject handle) {
41 assert(!is_jweak(handle), "precondition");
42 return reinterpret_cast<oop*>(handle);
43 }
44
45 inline oop* JNIHandles::jweak_ptr(jobject handle) {
46 assert(is_jweak(handle), "precondition");
47 char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;
48 return reinterpret_cast<oop*>(ptr);
49 }
50
51 // external_guard is true if called from resolve_external_guard.
52 template<bool external_guard>
53 inline oop JNIHandles::resolve_impl(jobject handle) {
54 assert(handle != NULL, "precondition");
55 assert(!current_thread_in_native(), "must not be in native");
56 oop result;
57 if (is_jweak(handle)) { // Unlikely
58 result = resolve_jweak(handle);
59 } else {
60 result = RootAccess<IN_CONCURRENT_ROOT>::oop_load(jobject_ptr(handle));
61 // Construction of jobjects canonicalize a null value into a null
62 // jobject, so for non-jweak the pointee should never be null.
63 assert(external_guard || result != NULL, "Invalid JNI handle");
64 }
65 return result;
66 }
67
68 inline oop JNIHandles::resolve(jobject handle) {
69 oop result = NULL;
70 if (handle != NULL) {
71 result = resolve_impl<false /* external_guard */ >(handle);
72 }
73 return result;
74 }
75
76 inline oop JNIHandles::resolve_non_null(jobject handle) {
77 assert(handle != NULL, "JNI handle should not be null");
78 oop result = resolve_impl<false /* external_guard */ >(handle);
79 assert(result != NULL, "NULL read from jni handle");
80 return result;
81 }
82
83 inline void JNIHandles::destroy_local(jobject handle) {
84 if (handle != NULL) {
85 assert(!is_jweak(handle), "Invalid JNI local handle");
86 RootAccess<>::oop_store(jobject_ptr(handle), (oop)NULL);
87 }
88 }
89
90 #endif // SHARE_RUNTIME_JNIHANDLES_INLINE_HPP
91
|