jni_env_ext.cc revision 277ccbd200ea43590dfc06a93ae184a765327ad0
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "jni_env_ext.h" 18 19#include "check_jni.h" 20#include "indirect_reference_table.h" 21#include "java_vm_ext.h" 22#include "jni_internal.h" 23 24namespace art { 25 26static constexpr size_t kMonitorsInitial = 32; // Arbitrary. 27static constexpr size_t kMonitorsMax = 4096; // Arbitrary sanity check. 28 29static constexpr size_t kLocalsInitial = 64; // Arbitrary. 30 31JNIEnvExt::JNIEnvExt(Thread* self_in, JavaVMExt* vm_in) 32 : self(self_in), 33 vm(vm_in), 34 local_ref_cookie(IRT_FIRST_SEGMENT), 35 locals(kLocalsInitial, kLocalsMax, kLocal), 36 check_jni(false), 37 critical(0), 38 monitors("monitors", kMonitorsInitial, kMonitorsMax) { 39 functions = unchecked_functions = GetJniNativeInterface(); 40 if (vm->IsCheckJniEnabled()) { 41 SetCheckJniEnabled(true); 42 } 43} 44 45JNIEnvExt::~JNIEnvExt() { 46} 47 48jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 49 if (obj == nullptr) { 50 return nullptr; 51 } 52 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj)); 53} 54 55void JNIEnvExt::DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 56 if (obj != nullptr) { 57 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj)); 58 } 59} 60 61void JNIEnvExt::SetCheckJniEnabled(bool enabled) { 62 check_jni = enabled; 63 functions = enabled ? GetCheckJniNativeInterface() : GetJniNativeInterface(); 64} 65 66void JNIEnvExt::DumpReferenceTables(std::ostream& os) { 67 locals.Dump(os); 68 monitors.Dump(os); 69} 70 71void JNIEnvExt::PushFrame(int capacity) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 72 UNUSED(capacity); // cpplint gets confused with (int) and thinks its a cast. 73 // TODO: take 'capacity' into account. 74 stacked_local_ref_cookies.push_back(local_ref_cookie); 75 local_ref_cookie = locals.GetSegmentState(); 76} 77 78void JNIEnvExt::PopFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 79 locals.SetSegmentState(local_ref_cookie); 80 local_ref_cookie = stacked_local_ref_cookies.back(); 81 stacked_local_ref_cookies.pop_back(); 82} 83 84Offset JNIEnvExt::SegmentStateOffset() { 85 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) + 86 IndirectReferenceTable::SegmentStateOffset().Int32Value()); 87} 88 89} // namespace art 90