xref: /aosp_15_r20/art/runtime/jni/jni_env_ext-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_JNI_JNI_ENV_EXT_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_JNI_JNI_ENV_EXT_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "jni_env_ext.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "local_reference_table-inl.h"
23*795d594fSAndroid Build Coastguard Worker #include "mirror/object.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker template<typename T>
AddLocalReference(ObjPtr<mirror::Object> obj)28*795d594fSAndroid Build Coastguard Worker inline T JNIEnvExt::AddLocalReference(ObjPtr<mirror::Object> obj) {
29*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
30*795d594fSAndroid Build Coastguard Worker   jobject ref = reinterpret_cast<jobject>(locals_.Add(obj, &error_msg));
31*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(ref == nullptr)) {
32*795d594fSAndroid Build Coastguard Worker     // This is really unexpected if we allow resizing LRTs...
33*795d594fSAndroid Build Coastguard Worker     LOG(FATAL) << error_msg;
34*795d594fSAndroid Build Coastguard Worker     UNREACHABLE();
35*795d594fSAndroid Build Coastguard Worker   }
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker   // TODO: fix this to understand PushLocalFrame, so we can turn it on.
38*795d594fSAndroid Build Coastguard Worker   if (false) {
39*795d594fSAndroid Build Coastguard Worker     if (check_jni_) {
40*795d594fSAndroid Build Coastguard Worker       size_t entry_count = locals_.Capacity();
41*795d594fSAndroid Build Coastguard Worker       if (entry_count > 16) {
42*795d594fSAndroid Build Coastguard Worker         locals_.Dump(LOG_STREAM(WARNING) << "Warning: more than 16 JNI local references: "
43*795d594fSAndroid Build Coastguard Worker                                         << entry_count << " (most recent was a "
44*795d594fSAndroid Build Coastguard Worker                                         << mirror::Object::PrettyTypeOf(obj) << ")\n");
45*795d594fSAndroid Build Coastguard Worker       // TODO: LOG(FATAL) in a later release?
46*795d594fSAndroid Build Coastguard Worker       }
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<T>(ref);
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker 
UpdateLocal(IndirectRef iref,ObjPtr<mirror::Object> obj)53*795d594fSAndroid Build Coastguard Worker inline void JNIEnvExt::UpdateLocal(IndirectRef iref, ObjPtr<mirror::Object> obj) {
54*795d594fSAndroid Build Coastguard Worker   locals_.Update(iref, obj);
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker }  // namespace art
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_JNI_JNI_ENV_EXT_INL_H_
60