blob: 08616b7d7eff8e8a09aeceb0b47d2f41a568cee5 [file] [log] [blame]
The Android Open Source Project7f844dd2009-03-03 19:28:47 -08001/*
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 */
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -070023#ifndef NATIVEHELPER_JNIHELP_H_
24#define NATIVEHELPER_JNIHELP_H_
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080025
Dan Albert099d5212014-05-21 13:25:01 -070026#include <errno.h>
Dan Bornstein6edaa472009-10-26 13:33:22 -070027#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080028
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000029#include <jni.h>
30#include "module_api.h"
31
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080032#ifndef NELEM
33# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
34#endif
35
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080036/*
37 * Register one or more native methods with a particular class.
Elliott Hughes73d3c2e2012-05-03 17:06:04 -070038 * "className" looks like "java/lang/String". Aborts on failure.
39 * TODO: fix all callers and change the return type to void.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080040 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000041MODULE_API int jniRegisterNativeMethods(C_JNIEnv* env,
42 const char* className,
43 const JNINativeMethod* gMethods,
44 int numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080045
46/*
47 * Throw an exception with the specified class and an optional message.
Elliott Hughes87f62a82011-04-22 19:22:54 -070048 *
Andy McFadden0b9d26c2009-06-04 14:34:14 -070049 * The "className" argument will be passed directly to FindClass, which
50 * takes strings with slashes (e.g. "java/lang/Object").
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080051 *
Elliott Hughes87f62a82011-04-22 19:22:54 -070052 * If an exception is currently pending, we log a warning message and
53 * clear it.
54 *
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080055 * Returns 0 on success, nonzero if something failed (e.g. the exception
Elliott Hughes87f62a82011-04-22 19:22:54 -070056 * class couldn't be found, so *an* exception will still be pending).
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080057 *
58 * Currently aborts the VM if it can't throw the exception.
59 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000060MODULE_API int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
61
62/*
63 * Throw an exception with the specified class and formatted error message.
64 *
65 * The "className" argument will be passed directly to FindClass, which
66 * takes strings with slashes (e.g. "java/lang/Object").
67 *
68 * If an exception is currently pending, we log a warning message and
69 * clear it.
70 *
71 * Returns 0 on success, nonzero if something failed (e.g. the exception
72 * class couldn't be found, so *an* exception will still be pending).
73 *
74 * Currently aborts the VM if it can't throw the exception.
75 */
76MODULE_API int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080077
78/*
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080079 * Throw a java.lang.NullPointerException, with an optional message.
80 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000081MODULE_API int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080082
83/*
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080084 * Throw a java.lang.RuntimeException, with an optional message.
85 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000086MODULE_API int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080087
88/*
89 * Throw a java.io.IOException, generating the message from errno.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080090 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000091MODULE_API int jniThrowIOException(C_JNIEnv* env, int errnum);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080092
93/*
Elliott Hughese4239ae2009-10-20 16:59:01 -070094 * Return a pointer to a locale-dependent error string explaining errno
95 * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
96 * This function is thread-safe (unlike strerror) and portable (unlike
97 * strerror_r).
98 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000099MODULE_API const char* jniStrError(int errnum, char* buf, size_t buflen);
Elliott Hughese4239ae2009-10-20 16:59:01 -0700100
101/*
Elliott Hughes87f62a82011-04-22 19:22:54 -0700102 * Returns a new java.io.FileDescriptor for the given int fd.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800103 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000104MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800105
Carl Shapiroabb4eff2010-06-08 16:37:12 -0700106/*
Elliott Hughes87f62a82011-04-22 19:22:54 -0700107 * Returns the int fd from a java.io.FileDescriptor.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800108 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000109MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800110
111/*
Pete Bentley0505b222018-07-20 18:18:44 +0100112 * Sets the int fd in a java.io.FileDescriptor. Throws java.lang.NullPointerException
113 * if fileDescriptor is null.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800114 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000115MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env,
116 jobject fileDescriptor,
117 int value);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800118
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700119/*
Josh Gao669bc9e2018-06-25 16:22:11 -0700120 * Returns the long ownerId from a java.io.FileDescriptor.
121 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000122MODULE_API jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
Josh Gao669bc9e2018-06-25 16:22:11 -0700123
124/*
Jeff Browna7de6782013-04-02 18:09:29 -0700125 * Returns the reference from a java.lang.ref.Reference.
126 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000127MODULE_API jobject jniGetReferent(C_JNIEnv* env, jobject ref);
Jeff Browna7de6782013-04-02 18:09:29 -0700128
129/*
Fredrik Roubert2e312802017-07-04 21:53:08 +0200130 * Returns a Java String object created from UTF-16 data either from jchar or,
131 * if called from C++11, char16_t (a bitwise identical distinct type).
132 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000133MODULE_API jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
Fredrik Roubert2e312802017-07-04 21:53:08 +0200134
135/*
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700136 * Log a message and an exception.
137 * If exception is NULL, logs the current exception in the JNI environment.
138 */
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000139MODULE_API void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800140
141/*
142 * For C++ code, we provide inlines that map to the C functions. g++ always
143 * inlines these, even on non-optimized builds.
144 */
Andy McFaddenb53ec152011-02-08 16:12:33 -0800145#if defined(__cplusplus)
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000146
Elliott Hughes87f62a82011-04-22 19:22:54 -0700147inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
148 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800149}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700150
151inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800152 return jniThrowException(&env->functions, className, msg);
153}
Elliott Hughes2f82af12011-04-08 17:15:16 -0700154
Elliott Hughes2f82af12011-04-08 17:15:16 -0700155/*
156 * Equivalent to jniThrowException but with a printf-like format string and
157 * variable-length argument list. This is only available in C++.
158 */
Elliott Hughes87f62a82011-04-22 19:22:54 -0700159inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
Elliott Hughes2f82af12011-04-08 17:15:16 -0700160 va_list args;
161 va_start(args, fmt);
162 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
163 va_end(args);
164}
165
Elliott Hughes87f62a82011-04-22 19:22:54 -0700166inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800167 return jniThrowNullPointerException(&env->functions, msg);
168}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700169
170inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800171 return jniThrowRuntimeException(&env->functions, msg);
172}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700173
174inline int jniThrowIOException(JNIEnv* env, int errnum) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800175 return jniThrowIOException(&env->functions, errnum);
176}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700177
178inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800179 return jniCreateFileDescriptor(&env->functions, fd);
180}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700181
182inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800183 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
184}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700185
186inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700187 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
188}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700189
Josh Gao669bc9e2018-06-25 16:22:11 -0700190inline jlong jniGetOwnerIdFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
191 return jniGetOwnerIdFromFileDescriptor(&env->functions, fileDescriptor);
192}
193
Jeff Browna7de6782013-04-02 18:09:29 -0700194inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
195 return jniGetReferent(&env->functions, ref);
196}
197
Fredrik Roubert2e312802017-07-04 21:53:08 +0200198inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
199 return jniCreateString(&env->functions, unicodeChars, len);
200}
201
Fredrik Roubert2e312802017-07-04 21:53:08 +0200202inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) {
203 return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len);
204}
Fredrik Roubert2e312802017-07-04 21:53:08 +0200205
Elliott Hughes87f62a82011-04-22 19:22:54 -0700206inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700207 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800208}
Brian Carlstromdd8af232012-05-13 23:56:07 -0700209
Ian Rogers8288dde2014-11-04 11:42:02 -0800210#if !defined(DISALLOW_COPY_AND_ASSIGN)
211// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private:
212// declarations in a class.
Ian Rogers8288dde2014-11-04 11:42:02 -0800213#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
214 TypeName(const TypeName&) = delete; \
215 void operator=(const TypeName&) = delete
Ian Rogers8288dde2014-11-04 11:42:02 -0800216#endif // !defined(DISALLOW_COPY_AND_ASSIGN)
217
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000218#endif // defined(__cplusplus)
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800219
Dan Bornstein6edaa472009-10-26 13:33:22 -0700220/*
221 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
222 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
223 * not already defined, then define it here.
224 */
225#ifndef TEMP_FAILURE_RETRY
226/* Used to retry syscalls that can return EINTR. */
227#define TEMP_FAILURE_RETRY(exp) ({ \
228 typeof (exp) _rc; \
229 do { \
230 _rc = (exp); \
231 } while (_rc == -1 && errno == EINTR); \
232 _rc; })
233#endif
234
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -0700235#endif /* NATIVEHELPER_JNIHELP_H_ */