1 /*
2 * Copyright (C) 2016 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.h>
18
19 #include "nativehelper/JNIHelp.h"
20 #include "core_jni_helpers.h"
21
22 #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
23
jintarrayArgumentNoop(JNIEnv *,jclass,jintArray,jint)24 static void jintarrayArgumentNoop(JNIEnv*, jclass, jintArray, jint) {
25 }
26
jintarrayGetLength(JNIEnv * env,jclass,jintArray jarray)27 static jint jintarrayGetLength(JNIEnv* env, jclass, jintArray jarray) {
28 const jsize len = env->GetArrayLength(jarray);
29 return static_cast<jint>(len);
30 }
31
jintarrayCriticalAccess(JNIEnv * env,jclass,jintArray jarray,jint index)32 static jint jintarrayCriticalAccess(JNIEnv* env, jclass, jintArray jarray, jint index) {
33 const jsize len = env->GetArrayLength(jarray);
34 if (index < 0 || index >= len) {
35 return -1;
36 }
37 jint* data = (jint*) env->GetPrimitiveArrayCritical(jarray, 0);
38 jint ret = data[index];
39 env->ReleasePrimitiveArrayCritical(jarray, data, 0);
40 return ret;
41 }
42
jintarrayBasicAccess(JNIEnv * env,jclass,jintArray jarray,jint index)43 static jint jintarrayBasicAccess(JNIEnv* env, jclass, jintArray jarray, jint index) {
44 const jsize len = env->GetArrayLength(jarray);
45 if (index < 0 || index >= len) {
46 return -1;
47 }
48 jint* data = env->GetIntArrayElements(jarray, 0);
49 jint ret = data[index];
50 env->ReleaseIntArrayElements(jarray, data, 0);
51 return ret;
52 }
53
jintFastNativeAccess(JNIEnv *,jclass,jint number)54 static jint jintFastNativeAccess(JNIEnv*, jclass, jint number) {
55 return number;
56 }
57
jintCriticalNativeAccess(CRITICAL_JNI_PARAMS_COMMA jint number)58 static jint jintCriticalNativeAccess(CRITICAL_JNI_PARAMS_COMMA jint number) {
59 return number;
60 }
61
jintFastNativeCheckNullPointer(JNIEnv * env,jclass,jint number)62 static jint jintFastNativeCheckNullPointer(JNIEnv* env, jclass, jint number) {
63 if (number == 0) {
64 jniThrowNullPointerException(env, NULL);
65 return -1;
66 }
67 return number;
68 }
69
jintCriticalNativeCheckNullPointer(CRITICAL_JNI_PARAMS_COMMA jint number)70 static jint jintCriticalNativeCheckNullPointer(CRITICAL_JNI_PARAMS_COMMA jint number) {
71 if (number == 0) {
72 return -1;
73 }
74 return number;
75 }
76
77 static const JNINativeMethod sMethods[] = {
78 {"jintarrayArgumentNoop", "([II)V", (void *) jintarrayArgumentNoop},
79 {"jintarrayGetLength", "([I)I", (void *) jintarrayGetLength},
80 {"jintarrayCriticalAccess", "([II)I", (void *) jintarrayCriticalAccess},
81 {"jintarrayBasicAccess", "([II)I", (void *) jintarrayBasicAccess},
82 {"jintFastNativeAccess", "(I)I", (void *) jintFastNativeAccess},
83 {"jintCriticalNativeAccess", "(I)I", (void *) jintCriticalNativeAccess},
84 {"jintFastNativeCheckNullPointer", "(I)I", (void *) jintFastNativeCheckNullPointer},
85 {"jintCriticalNativeCheckNullPointer", "(I)I", (void *) jintCriticalNativeCheckNullPointer},
86 };
87
registerNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)88 static int registerNativeMethods(JNIEnv* env, const char* className,
89 const JNINativeMethod* gMethods, int numMethods) {
90 jclass clazz = env->FindClass(className);
91 if (clazz == NULL) {
92 return JNI_FALSE;
93 }
94 if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
95 return JNI_FALSE;
96 }
97 return JNI_TRUE;
98 }
99
JNI_OnLoad(JavaVM * jvm,void *)100 jint JNI_OnLoad(JavaVM* jvm, void*) {
101 JNIEnv *env = NULL;
102 if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6)) {
103 return JNI_ERR;
104 }
105
106 if (registerNativeMethods(env, "android/perftests/SystemPerfTest",
107 sMethods, NELEM(sMethods)) == -1) {
108 return JNI_ERR;
109 }
110
111 return JNI_VERSION_1_6;
112 }
113