blob: 758b7c78ec906b1e6166eee1edb28a5519a5e5de [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.
Brian Carlstromdd8af232012-05-13 23:56:07 -070022 *
23 * TODO: remove C support.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080024 */
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -070025#ifndef NATIVEHELPER_JNIHELP_H_
26#define NATIVEHELPER_JNIHELP_H_
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080027
28#include "jni.h"
Dan Bornstein6edaa472009-10-26 13:33:22 -070029#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080030
31#ifndef NELEM
32# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
33#endif
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/*
40 * Register one or more native methods with a particular class.
Elliott Hughes73d3c2e2012-05-03 17:06:04 -070041 * "className" looks like "java/lang/String". Aborts on failure.
42 * TODO: fix all callers and change the return type to void.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080043 */
Elliott Hughes87f62a82011-04-22 19:22:54 -070044int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, 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 */
60int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
61
62/*
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080063 * Throw a java.lang.NullPointerException, with an optional message.
64 */
65int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
66
67/*
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080068 * Throw a java.lang.RuntimeException, with an optional message.
69 */
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080070int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080071
72/*
73 * Throw a java.io.IOException, generating the message from errno.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080074 */
75int jniThrowIOException(C_JNIEnv* env, int errnum);
76
77/*
Elliott Hughese4239ae2009-10-20 16:59:01 -070078 * Return a pointer to a locale-dependent error string explaining errno
79 * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
80 * This function is thread-safe (unlike strerror) and portable (unlike
81 * strerror_r).
82 */
83const char* jniStrError(int errnum, char* buf, size_t buflen);
84
85/*
Elliott Hughes87f62a82011-04-22 19:22:54 -070086 * Returns a new java.io.FileDescriptor for the given int fd.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080087 */
88jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
89
Carl Shapiroabb4eff2010-06-08 16:37:12 -070090/*
Elliott Hughes87f62a82011-04-22 19:22:54 -070091 * Returns the int fd from a java.io.FileDescriptor.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080092 */
93int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
94
95/*
Elliott Hughes87f62a82011-04-22 19:22:54 -070096 * Sets the int fd in a java.io.FileDescriptor.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080097 */
98void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
99
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700100/*
Jeff Browna7de6782013-04-02 18:09:29 -0700101 * Returns the reference from a java.lang.ref.Reference.
102 */
103jobject jniGetReferent(C_JNIEnv* env, jobject ref);
104
105/*
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700106 * Log a message and an exception.
107 * If exception is NULL, logs the current exception in the JNI environment.
108 */
109void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
110
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800111#ifdef __cplusplus
112}
113#endif
114
115
116/*
117 * For C++ code, we provide inlines that map to the C functions. g++ always
118 * inlines these, even on non-optimized builds.
119 */
Andy McFaddenb53ec152011-02-08 16:12:33 -0800120#if defined(__cplusplus)
Elliott Hughes87f62a82011-04-22 19:22:54 -0700121inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
122 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800123}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700124
125inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800126 return jniThrowException(&env->functions, className, msg);
127}
Elliott Hughes2f82af12011-04-08 17:15:16 -0700128
Elliott Hughes87f62a82011-04-22 19:22:54 -0700129extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
Elliott Hughes2f82af12011-04-08 17:15:16 -0700130
131/*
132 * Equivalent to jniThrowException but with a printf-like format string and
133 * variable-length argument list. This is only available in C++.
134 */
Elliott Hughes87f62a82011-04-22 19:22:54 -0700135inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
Elliott Hughes2f82af12011-04-08 17:15:16 -0700136 va_list args;
137 va_start(args, fmt);
138 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
139 va_end(args);
140}
141
Elliott Hughes87f62a82011-04-22 19:22:54 -0700142inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800143 return jniThrowNullPointerException(&env->functions, msg);
144}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700145
146inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800147 return jniThrowRuntimeException(&env->functions, msg);
148}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700149
150inline int jniThrowIOException(JNIEnv* env, int errnum) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800151 return jniThrowIOException(&env->functions, errnum);
152}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700153
154inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800155 return jniCreateFileDescriptor(&env->functions, fd);
156}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700157
158inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800159 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
160}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700161
162inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700163 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
164}
Elliott Hughes87f62a82011-04-22 19:22:54 -0700165
Jeff Browna7de6782013-04-02 18:09:29 -0700166inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
167 return jniGetReferent(&env->functions, ref);
168}
169
Elliott Hughes87f62a82011-04-22 19:22:54 -0700170inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700171 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800172}
Brian Carlstromdd8af232012-05-13 23:56:07 -0700173
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800174#endif
175
Dan Bornstein6edaa472009-10-26 13:33:22 -0700176/*
177 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
178 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
179 * not already defined, then define it here.
180 */
181#ifndef TEMP_FAILURE_RETRY
182/* Used to retry syscalls that can return EINTR. */
183#define TEMP_FAILURE_RETRY(exp) ({ \
184 typeof (exp) _rc; \
185 do { \
186 _rc = (exp); \
187 } while (_rc == -1 && errno == EINTR); \
188 _rc; })
189#endif
190
Carl Shapiro1ff7a0d2011-06-14 20:31:24 -0700191#endif /* NATIVEHELPER_JNIHELP_H_ */