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  * Returns a new java.io.FileDescriptor for the given int fd.
35  */
36 jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
37 
38 /*
39  * Returns the int fd from a java.io.FileDescriptor.
40  */
41 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
42 
43 /*
44  * Sets the int fd in a java.io.FileDescriptor.  Throws java.lang.NullPointerException
45  * if fileDescriptor is null.
46  */
47 void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
48 
49 /*
50  * Gets the managed heap array backing a java.nio.Buffer instance.
51  *
52  * Returns nullptr if there is no array backing.
53  *
54  * This method performs a JNI call to java.nio.NIOAccess.getBaseArray().
55  */
56 jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer);
57 
58 /*
59  * Gets the offset of the current buffer position in bytes from the start of the managed heap
60  * array backing the buffer.
61  *
62  * Returns 0 if there is no array backing.
63  *
64  * This method performs a JNI call to java.nio.NIOAccess.getBaseArrayOffset().
65  */
66 jint jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer);
67 
68 /*
69  * Gets field information from a java.nio.Buffer instance.
70  *
71  * Reads the |position|, |limit|, and |elementSizeShift| fields from the buffer instance.
72  *
73  * Returns the |address| field of the java.nio.Buffer instance which is only valid (non-zero) when
74  * the buffer is backed by a direct buffer.
75  */
76 jlong jniGetNioBufferFields(C_JNIEnv* env,
77                             jobject nioBuffer,
78                             /*out*/jint* position,
79                             /*out*/jint* limit,
80                             /*out*/jint* elementSizeShift);
81 
82 /*
83  * Gets the current position from a java.nio.Buffer as a pointer to memory in a fixed buffer.
84  *
85  * Returns 0 if |nioBuffer| is not backed by a direct buffer.
86  *
87  * This method reads the |address|, |position|, and |elementSizeShift| fields from the
88  * java.nio.Buffer instance to calculate the pointer address for the current position.
89  */
90 jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer);
91 
92 /*
93  * Clear the cache of constants libnativehelper is using.
94  */
95 void jniUninitializeConstants();
96 
97 __END_DECLS
98 
99 /*
100  * For C++ code, we provide inlines that map to the C functions.  g++ always
101  * inlines these, even on non-optimized builds.
102  */
103 #if defined(__cplusplus)
104 
jniCreateFileDescriptor(JNIEnv * env,int fd)105 inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
106     return jniCreateFileDescriptor(&env->functions, fd);
107 }
108 
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)109 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
110     return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
111 }
112 
jniSetFileDescriptorOfFD(JNIEnv * env,jobject fileDescriptor,int value)113 inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
114     jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
115 }
116 
jniGetNioBufferBaseArray(JNIEnv * env,jobject nioBuffer)117 inline jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
118     return jniGetNioBufferBaseArray(&env->functions, nioBuffer);
119 }
120 
jniGetNioBufferBaseArrayOffset(JNIEnv * env,jobject nioBuffer)121 inline jint jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) {
122     return jniGetNioBufferBaseArrayOffset(&env->functions, nioBuffer);
123 }
124 
jniGetNioBufferFields(JNIEnv * env,jobject nioBuffer,jint * position,jint * limit,jint * elementSizeShift)125 inline jlong jniGetNioBufferFields(JNIEnv* env, jobject nioBuffer,
126                                    jint* position, jint* limit, jint* elementSizeShift) {
127     return jniGetNioBufferFields(&env->functions, nioBuffer,
128                                  position, limit, elementSizeShift);
129 }
130 
jniGetNioBufferPointer(JNIEnv * env,jobject nioBuffer)131 inline jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
132     return jniGetNioBufferPointer(&env->functions, nioBuffer);
133 }
134 
135 #endif  // defined(__cplusplus)
136