Path: blob/master/src/hotspot/share/runtime/jniHandles.inline.hpp
40951 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_RUNTIME_JNIHANDLES_INLINE_HPP25#define SHARE_RUNTIME_JNIHANDLES_INLINE_HPP2627#include "runtime/jniHandles.hpp"2829#include "oops/access.inline.hpp"30#include "oops/oop.hpp"31#include "utilities/debug.hpp"32#include "utilities/globalDefinitions.hpp"3334inline bool JNIHandles::is_jweak(jobject handle) {35STATIC_ASSERT(weak_tag_size == 1);36STATIC_ASSERT(weak_tag_value == 1);37return (reinterpret_cast<uintptr_t>(handle) & weak_tag_mask) != 0;38}3940inline oop* JNIHandles::jobject_ptr(jobject handle) {41assert(!is_jweak(handle), "precondition");42return reinterpret_cast<oop*>(handle);43}4445inline oop* JNIHandles::jweak_ptr(jobject handle) {46assert(is_jweak(handle), "precondition");47char* ptr = reinterpret_cast<char*>(handle) - weak_tag_value;48return reinterpret_cast<oop*>(ptr);49}5051// external_guard is true if called from resolve_external_guard.52template <DecoratorSet decorators, bool external_guard>53inline oop JNIHandles::resolve_impl(jobject handle) {54assert(handle != NULL, "precondition");55assert(!current_thread_in_native(), "must not be in native");56oop result;57if (is_jweak(handle)) { // Unlikely58result = NativeAccess<ON_PHANTOM_OOP_REF|decorators>::oop_load(jweak_ptr(handle));59} else {60result = NativeAccess<decorators>::oop_load(jobject_ptr(handle));61// Construction of jobjects canonicalize a null value into a null62// jobject, so for non-jweak the pointee should never be null.63assert(external_guard || result != NULL, "Invalid JNI handle");64}65return result;66}6768inline oop JNIHandles::resolve(jobject handle) {69oop result = NULL;70if (handle != NULL) {71result = resolve_impl<DECORATORS_NONE, false /* external_guard */>(handle);72}73return result;74}7576inline oop JNIHandles::resolve_no_keepalive(jobject handle) {77oop result = NULL;78if (handle != NULL) {79result = resolve_impl<AS_NO_KEEPALIVE, false /* external_guard */>(handle);80}81return result;82}8384inline bool JNIHandles::is_same_object(jobject handle1, jobject handle2) {85oop obj1 = resolve_no_keepalive(handle1);86oop obj2 = resolve_no_keepalive(handle2);87return obj1 == obj2;88}8990inline oop JNIHandles::resolve_non_null(jobject handle) {91assert(handle != NULL, "JNI handle should not be null");92oop result = resolve_impl<DECORATORS_NONE, false /* external_guard */>(handle);93assert(result != NULL, "NULL read from jni handle");94return result;95}9697inline void JNIHandles::destroy_local(jobject handle) {98if (handle != NULL) {99assert(!is_jweak(handle), "Invalid JNI local handle");100NativeAccess<>::oop_store(jobject_ptr(handle), (oop)NULL);101}102}103104#endif // SHARE_RUNTIME_JNIHANDLES_INLINE_HPP105106107