• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
20 
jintarrayArgumentNoop(JNIEnv *,jclass,jintArray,jint)21 static void jintarrayArgumentNoop(JNIEnv*, jclass, jintArray, jint) {
22 }
23 
jintarrayGetLength(JNIEnv * env,jclass,jintArray jarray)24 static jint jintarrayGetLength(JNIEnv* env, jclass, jintArray jarray) {
25     const jsize len = env->GetArrayLength(jarray);
26     return static_cast<jint>(len);
27 }
28 
jintarrayCriticalAccess(JNIEnv * env,jclass,jintArray jarray,jint index)29 static jint jintarrayCriticalAccess(JNIEnv* env, jclass, jintArray jarray, jint index) {
30     const jsize len = env->GetArrayLength(jarray);
31     if (index < 0 || index >= len) {
32         return -1;
33     }
34     jint* data = (jint*) env->GetPrimitiveArrayCritical(jarray, 0);
35     jint ret = data[index];
36     env->ReleasePrimitiveArrayCritical(jarray, data, 0);
37     return ret;
38 }
39 
jintarrayBasicAccess(JNIEnv * env,jclass,jintArray jarray,jint index)40 static jint jintarrayBasicAccess(JNIEnv* env, jclass, jintArray jarray, jint index) {
41     const jsize len = env->GetArrayLength(jarray);
42     if (index < 0 || index >= len) {
43         return -1;
44     }
45     jint* data = env->GetIntArrayElements(jarray, 0);
46     jint ret = data[index];
47     env->ReleaseIntArrayElements(jarray, data, 0);
48     return ret;
49 }
50 
51 static const JNINativeMethod sMethods[] = {
52     {"jintarrayArgumentNoop", "([II)V", (void *) jintarrayArgumentNoop},
53     {"jintarrayGetLength", "([I)I", (void *) jintarrayGetLength},
54     {"jintarrayCriticalAccess", "([II)I", (void *) jintarrayCriticalAccess},
55     {"jintarrayBasicAccess", "([II)I", (void *) jintarrayBasicAccess},
56 };
57 
registerNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)58 static int registerNativeMethods(JNIEnv* env, const char* className,
59         const JNINativeMethod* gMethods, int numMethods) {
60     jclass clazz = env->FindClass(className);
61     if (clazz == NULL) {
62         return JNI_FALSE;
63     }
64     if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
65         return JNI_FALSE;
66     }
67     return JNI_TRUE;
68 }
69 
JNI_OnLoad(JavaVM * jvm,void *)70 jint JNI_OnLoad(JavaVM* jvm, void*) {
71     JNIEnv *env = NULL;
72     if (jvm->GetEnv((void**) &env, JNI_VERSION_1_6)) {
73         return JNI_ERR;
74     }
75 
76     if (registerNativeMethods(env, "android/perftests/SystemPerfTest",
77             sMethods, NELEM(sMethods)) == -1) {
78         return JNI_ERR;
79     }
80 
81     return JNI_VERSION_1_6;
82 }
83