OSDN Git Service

am de9e2b90: Bug fixes for ld/st elimination.
[android-x86/dalvik.git] / vm / JniInternal.h
1 /*
2  * Copyright (C) 2008 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  * JNI innards, common to the regular and "checked" interfaces.
18  */
19 #ifndef _DALVIK_JNIINTERNAL
20 #define _DALVIK_JNIINTERNAL
21
22 #include "jni.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 /* system init/shutdown */
29 bool dvmJniStartup(void);
30 void dvmJniShutdown(void);
31
32 /*
33  * Our data structures for JNIEnv and JavaVM.
34  *
35  * Native code thinks it has a pointer to a pointer.  We know better.
36  */
37 struct JavaVMExt;
38
39 typedef struct JNIEnvExt {
40     const struct JNINativeInterface* funcTable;     /* must be first */
41
42     const struct JNINativeInterface* baseFuncTable;
43
44     /* pointer to the VM we are a part of */
45     struct JavaVMExt* vm;
46
47     u4      envThreadId;
48     Thread* self;
49
50     /* if nonzero, we are in a "critical" JNI call */
51     int     critical;
52
53     /* keep a copy of this here for speed */
54     bool    forceDataCopy;
55
56     struct JNIEnvExt* prev;
57     struct JNIEnvExt* next;
58 } JNIEnvExt;
59
60 typedef struct JavaVMExt {
61     const struct JNIInvokeInterface* funcTable;     /* must be first */
62
63     const struct JNIInvokeInterface* baseFuncTable;
64
65     /* if multiple VMs are desired, add doubly-linked list stuff here */
66
67     /* per-VM feature flags */
68     bool    useChecked;
69     bool    warnError;
70     bool    forceDataCopy;
71
72     /* head of list of JNIEnvs associated with this VM */
73     JNIEnvExt*      envList;
74     pthread_mutex_t envListLock;
75 } JavaVMExt;
76
77 /*
78  * Native function return type; used by dvmPlatformInvoke().
79  *
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.
83  */
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;
94
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
100
101
102 /*
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.
105  *
106  * (This may be implemented directly in assembly in mterp, so changes here
107  * may only affect the portable interpreter.)
108  */
109 INLINE void dvmPopJniLocals(Thread* self, StackSaveArea* saveArea)
110 {
111     self->jniLocalRefTable.segmentState.all = saveArea->xtra.localRefCookie;
112 }
113
114 /*
115  * Set the envThreadId field.
116  */
117 INLINE void dvmSetJniEnvThreadId(JNIEnv* pEnv, Thread* self)
118 {
119     ((JNIEnvExt*)pEnv)->envThreadId = self->threadId;
120     ((JNIEnvExt*)pEnv)->self = self;
121 }
122
123 /*
124  * JNI call bridges.  Not called directly.
125  *
126  * The "Check" versions are used when CheckJNI is enabled.
127  */
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);
144
145 /*
146  * Configure "method" to use the JNI bridge to call "func".
147  */
148 void dvmUseJNIBridge(Method* method, void* func);
149
150
151 /*
152  * Enable the "checked" versions.
153  */
154 void dvmUseCheckedJniEnv(JNIEnvExt* pEnv);
155 void dvmUseCheckedJniVm(JavaVMExt* pVm);
156 void dvmLateEnableCheckedJni(void);
157
158 /*
159  * Decode a local, global, or weak-global reference.
160  */
161 Object* dvmDecodeIndirectRef(JNIEnv* env, jobject jobj);
162
163 /*
164  * Verify that a reference passed in from native code is valid.  Returns
165  * an indication of local/global/invalid.
166  */
167 jobjectRefType dvmGetJNIRefType(JNIEnv* env, jobject jobj);
168
169 /*
170  * Get the last method called on the interp stack.  This is the method
171  * "responsible" for calling into JNI.
172  */
173 const Method* dvmGetCurrentJNIMethod(void);
174
175 /*
176  * Create/destroy a JNIEnv for the current thread.
177  */
178 JNIEnv* dvmCreateJNIEnv(Thread* self);
179 void dvmDestroyJNIEnv(JNIEnv* env);
180
181 /*
182  * Find the JNIEnv associated with the current thread.
183  */
184 JNIEnvExt* dvmGetJNIEnvForThread(void);
185
186 /*
187  * Extract the return type enum from the "jniArgInfo" value.
188  */
189 DalvikJniReturnType dvmGetArgInfoReturnType(int jniArgInfo);
190
191 /*
192  * Release all MonitorEnter-acquired locks that are still held.  Called at
193  * DetachCurrentThread time.
194  */
195 void dvmReleaseJniMonitors(Thread* self);
196
197 /*
198  * Dump the contents of the JNI reference tables to the log file.
199  *
200  * The local ref tables associated with other threads are not included.
201  */
202 void dvmDumpJniReferenceTables(void);
203
204 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif /*_DALVIK_JNIINTERNAL*/