• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 /*
18  * JNI helper functions.
19  *
20  * This file may be included by C or C++ code, which is trouble because jni.h
21  * uses different typedefs for JNIEnv in each language.
22  */
23 #pragma once
24 
25 #include <sys/cdefs.h>
26 
27 #include <jni.h>
28 
29 #include <nativehelper/JNIHelp.h>
30 
31 __BEGIN_DECLS
32 
33 /*
34  * Gets the managed heap array backing a java.nio.Buffer instance.
35  *
36  * Returns nullptr if there is no array backing.
37  *
38  * This method performs a JNI call to java.nio.NIOAccess.getBaseArray().
39  */
40 jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer);
41 
42 /*
43  * Gets the offset of the current buffer position in bytes from the start of the managed heap
44  * array backing the buffer.
45  *
46  * Returns 0 if there is no array backing.
47  *
48  * This method performs a JNI call to java.nio.NIOAccess.getBaseArrayOffset().
49  */
50 jint jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer);
51 
52 /*
53  * Gets field information from a java.nio.Buffer instance.
54  *
55  * Reads the |position|, |limit|, and |elementSizeShift| fields from the buffer instance.
56  *
57  * Returns the |address| field of the java.nio.Buffer instance which is only valid (non-zero) when
58  * the buffer is backed by a direct buffer.
59  */
60 jlong jniGetNioBufferFields(C_JNIEnv* env,
61                             jobject nioBuffer,
62                             /*out*/jint* position,
63                             /*out*/jint* limit,
64                             /*out*/jint* elementSizeShift);
65 
66 /*
67  * Gets the current position from a java.nio.Buffer as a pointer to memory in a fixed buffer.
68  *
69  * Returns 0 if |nioBuffer| is not backed by a direct buffer.
70  *
71  * This method reads the |address|, |position|, and |elementSizeShift| fields from the
72  * java.nio.Buffer instance to calculate the pointer address for the current position.
73  */
74 jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer);
75 
76 /*
77  * Clear the cache of constants libnativehelper is using.
78  */
79 void jniUninitializeConstants();
80 
81 __END_DECLS
82 
83 /*
84  * For C++ code, we provide inlines that map to the C functions.  g++ always
85  * inlines these, even on non-optimized builds.
86  */
87 
88 #if defined(__cplusplus)
89 
90 #include <android/file_descriptor_jni.h>
91 
jniCreateFileDescriptor(JNIEnv * env,int fd)92 inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
93     jobject fileDescriptor = AFileDescriptor_create(env);
94     if (fileDescriptor != nullptr) {
95       AFileDescriptor_setFd(env, fileDescriptor, fd);
96     }
97     return fileDescriptor;
98 }
99 
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)100 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
101     if (fileDescriptor == nullptr) {
102       return -1;
103     }
104     return AFileDescriptor_getFd(env, fileDescriptor);
105 }
106 
jniSetFileDescriptorOfFD(JNIEnv * env,jobject fileDescriptor,int value)107 inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
108     if (fileDescriptor == nullptr) {
109         jniThrowNullPointerException(env, "fileDescriptor is null");
110         return;
111     }
112     AFileDescriptor_setFd(env, fileDescriptor, value);
113 }
114 
jniGetNioBufferBaseArray(JNIEnv * env,jobject nioBuffer)115 inline jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
116     return jniGetNioBufferBaseArray(&env->functions, nioBuffer);
117 }
118 
jniGetNioBufferBaseArrayOffset(JNIEnv * env,jobject nioBuffer)119 inline jint jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) {
120     return jniGetNioBufferBaseArrayOffset(&env->functions, nioBuffer);
121 }
122 
jniGetNioBufferFields(JNIEnv * env,jobject nioBuffer,jint * position,jint * limit,jint * elementSizeShift)123 inline jlong jniGetNioBufferFields(JNIEnv* env, jobject nioBuffer,
124                                    jint* position, jint* limit, jint* elementSizeShift) {
125     return jniGetNioBufferFields(&env->functions, nioBuffer,
126                                  position, limit, elementSizeShift);
127 }
128 
jniGetNioBufferPointer(JNIEnv * env,jobject nioBuffer)129 inline jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
130     return jniGetNioBufferPointer(&env->functions, nioBuffer);
131 }
132 
133 #endif  // defined(__cplusplus)
134