2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * JNI innards, common to the regular and "checked" interfaces.
19 #ifndef _DALVIK_JNIINTERNAL
20 #define _DALVIK_JNIINTERNAL
28 /* system init/shutdown */
29 bool dvmJniStartup(void);
30 void dvmJniShutdown(void);
33 * Our data structures for JNIEnv and JavaVM.
35 * Native code thinks it has a pointer to a pointer. We know better.
39 typedef struct JNIEnvExt {
40 const struct JNINativeInterface* funcTable; /* must be first */
42 const struct JNINativeInterface* baseFuncTable;
44 /* pointer to the VM we are a part of */
50 /* if nonzero, we are in a "critical" JNI call */
53 /* keep a copy of this here for speed */
56 struct JNIEnvExt* prev;
57 struct JNIEnvExt* next;
60 typedef struct JavaVMExt {
61 const struct JNIInvokeInterface* funcTable; /* must be first */
63 const struct JNIInvokeInterface* baseFuncTable;
65 /* if multiple VMs are desired, add doubly-linked list stuff here */
67 /* per-VM feature flags */
72 /* head of list of JNIEnvs associated with this VM */
74 pthread_mutex_t envListLock;
78 * Native function return type; used by dvmPlatformInvoke().
80 * This is part of Method.jniArgInfo, and must fit in 3 bits.
81 * Note: Assembly code in arch/<arch>/Call<arch>.S relies on
82 * the enum values defined here.
84 typedef enum DalvikJniReturnType {
85 DALVIK_JNI_RETURN_VOID = 0, /* must be zero */
86 DALVIK_JNI_RETURN_FLOAT = 1,
87 DALVIK_JNI_RETURN_DOUBLE = 2,
88 DALVIK_JNI_RETURN_S8 = 3,
89 DALVIK_JNI_RETURN_S4 = 4,
90 DALVIK_JNI_RETURN_S2 = 5,
91 DALVIK_JNI_RETURN_U2 = 6,
92 DALVIK_JNI_RETURN_S1 = 7
93 } DalvikJniReturnType;
95 #define DALVIK_JNI_NO_ARG_INFO 0x80000000
96 #define DALVIK_JNI_RETURN_MASK 0x70000000
97 #define DALVIK_JNI_RETURN_SHIFT 28
98 #define DALVIK_JNI_COUNT_MASK 0x0f000000
99 #define DALVIK_JNI_COUNT_SHIFT 24
103 * Pop the JNI local stack when we return from a native method. "saveArea"
104 * points to the StackSaveArea for the method we're leaving.
106 * (This may be implemented directly in assembly in mterp, so changes here
107 * may only affect the portable interpreter.)
109 INLINE void dvmPopJniLocals(Thread* self, StackSaveArea* saveArea)
111 self->jniLocalRefTable.segmentState.all = saveArea->xtra.localRefCookie;
115 * Set the envThreadId field.
117 INLINE void dvmSetJniEnvThreadId(JNIEnv* pEnv, Thread* self)
119 ((JNIEnvExt*)pEnv)->envThreadId = self->threadId;
120 ((JNIEnvExt*)pEnv)->self = self;
124 * JNI call bridges. Not called directly.
126 * The "Check" versions are used when CheckJNI is enabled.
128 void dvmCallJNIMethod_general(const u4* args, JValue* pResult,
129 const Method* method, Thread* self);
130 void dvmCallJNIMethod_synchronized(const u4* args, JValue* pResult,
131 const Method* method, Thread* self);
132 void dvmCallJNIMethod_virtualNoRef(const u4* args, JValue* pResult,
133 const Method* method, Thread* self);
134 void dvmCallJNIMethod_staticNoRef(const u4* args, JValue* pResult,
135 const Method* method, Thread* self);
136 void dvmCheckCallJNIMethod_general(const u4* args, JValue* pResult,
137 const Method* method, Thread* self);
138 void dvmCheckCallJNIMethod_synchronized(const u4* args, JValue* pResult,
139 const Method* method, Thread* self);
140 void dvmCheckCallJNIMethod_virtualNoRef(const u4* args, JValue* pResult,
141 const Method* method, Thread* self);
142 void dvmCheckCallJNIMethod_staticNoRef(const u4* args, JValue* pResult,
143 const Method* method, Thread* self);
146 * Configure "method" to use the JNI bridge to call "func".
148 void dvmUseJNIBridge(Method* method, void* func);
152 * Enable the "checked" versions.
154 void dvmUseCheckedJniEnv(JNIEnvExt* pEnv);
155 void dvmUseCheckedJniVm(JavaVMExt* pVm);
156 void dvmLateEnableCheckedJni(void);
159 * Decode a local, global, or weak-global reference.
161 Object* dvmDecodeIndirectRef(JNIEnv* env, jobject jobj);
164 * Verify that a reference passed in from native code is valid. Returns
165 * an indication of local/global/invalid.
167 jobjectRefType dvmGetJNIRefType(JNIEnv* env, jobject jobj);
170 * Get the last method called on the interp stack. This is the method
171 * "responsible" for calling into JNI.
173 const Method* dvmGetCurrentJNIMethod(void);
176 * Create/destroy a JNIEnv for the current thread.
178 JNIEnv* dvmCreateJNIEnv(Thread* self);
179 void dvmDestroyJNIEnv(JNIEnv* env);
182 * Find the JNIEnv associated with the current thread.
184 JNIEnvExt* dvmGetJNIEnvForThread(void);
187 * Extract the return type enum from the "jniArgInfo" value.
189 DalvikJniReturnType dvmGetArgInfoReturnType(int jniArgInfo);
192 * Release all MonitorEnter-acquired locks that are still held. Called at
193 * DetachCurrentThread time.
195 void dvmReleaseJniMonitors(Thread* self);
198 * Dump the contents of the JNI reference tables to the log file.
200 * The local ref tables associated with other threads are not included.
202 void dvmDumpJniReferenceTables(void);
208 #endif /*_DALVIK_JNIINTERNAL*/