blob: 1b5ff0c2e10d2839208c61a120727c22adfb74c5 [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 */
23#ifndef _NATIVEHELPER_JNIHELP_H
24#define _NATIVEHELPER_JNIHELP_H
25
26#include "jni.h"
27#include "utils/Log.h"
Dan Bornstein6edaa472009-10-26 13:33:22 -070028#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080029
30#ifndef NELEM
31# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/*
39 * Register one or more native methods with a particular class.
40 */
41int jniRegisterNativeMethods(C_JNIEnv* env, const char* className,
42 const JNINativeMethod* gMethods, int numMethods);
43
44/*
45 * Throw an exception with the specified class and an optional message.
Andy McFadden0b9d26c2009-06-04 14:34:14 -070046 * The "className" argument will be passed directly to FindClass, which
47 * takes strings with slashes (e.g. "java/lang/Object").
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080048 *
49 * Returns 0 on success, nonzero if something failed (e.g. the exception
50 * class couldn't be found).
51 *
52 * Currently aborts the VM if it can't throw the exception.
53 */
54int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
55
56/*
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080057 * Throw a java.lang.NullPointerException, with an optional message.
58 */
59int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
60
61/*
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080062 * Throw a java.lang.RuntimeException, with an optional message.
63 */
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080064int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
Dan Bornsteinf84f4c62009-12-07 15:46:23 -080065
66/*
67 * Throw a java.io.IOException, generating the message from errno.
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080068 */
69int jniThrowIOException(C_JNIEnv* env, int errnum);
70
71/*
Elliott Hughese4239ae2009-10-20 16:59:01 -070072 * Return a pointer to a locale-dependent error string explaining errno
73 * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
74 * This function is thread-safe (unlike strerror) and portable (unlike
75 * strerror_r).
76 */
77const char* jniStrError(int errnum, char* buf, size_t buflen);
78
79/*
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080080 * Create a java.io.FileDescriptor given an integer fd
81 */
82jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
83
Carl Shapiroabb4eff2010-06-08 16:37:12 -070084/*
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080085 * Get an int file descriptor from a java.io.FileDescriptor
86 */
87int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
88
89/*
90 * Set an int file descriptor to a java.io.FileDescriptor
91 */
92void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
93
Jeff Brown2c3fc7a2010-06-01 21:07:08 -070094/*
95 * Log a message and an exception.
96 * If exception is NULL, logs the current exception in the JNI environment.
97 */
98void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
99
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800100#ifdef __cplusplus
101}
102#endif
103
104
105/*
106 * For C++ code, we provide inlines that map to the C functions. g++ always
107 * inlines these, even on non-optimized builds.
108 */
Andy McFaddenb53ec152011-02-08 16:12:33 -0800109#if defined(__cplusplus)
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800110inline int jniRegisterNativeMethods(JNIEnv* env, const char* className,
111 const JNINativeMethod* gMethods, int numMethods)
112{
113 return jniRegisterNativeMethods(&env->functions, className, gMethods,
114 numMethods);
115}
116inline int jniThrowException(JNIEnv* env, const char* className,
117 const char* msg)
118{
119 return jniThrowException(&env->functions, className, msg);
120}
Elliott Hughes2f82af12011-04-08 17:15:16 -0700121
122extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className,
123 const char* fmt, va_list args);
124
125/*
126 * Equivalent to jniThrowException but with a printf-like format string and
127 * variable-length argument list. This is only available in C++.
128 */
129inline int jniThrowExceptionFmt(JNIEnv* env, const char* className,
130 const char* fmt, ...)
131{
132 va_list args;
133 va_start(args, fmt);
134 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
135 va_end(args);
136}
137
Elliott Hughes9f6e1d32010-01-28 13:43:39 -0800138inline int jniThrowNullPointerException(JNIEnv* env, const char* msg)
139{
140 return jniThrowNullPointerException(&env->functions, msg);
141}
142inline int jniThrowRuntimeException(JNIEnv* env, const char* msg)
143{
144 return jniThrowRuntimeException(&env->functions, msg);
145}
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800146inline int jniThrowIOException(JNIEnv* env, int errnum)
147{
148 return jniThrowIOException(&env->functions, errnum);
149}
150inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd)
151{
152 return jniCreateFileDescriptor(&env->functions, fd);
153}
154inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor)
155{
156 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
157}
158inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor,
159 int value)
160{
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700161 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
162}
163inline void jniLogException(JNIEnv* env, int priority, const char* tag,
164 jthrowable exception = NULL)
165{
166 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800167}
168#endif
169
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700170/* Logging macros.
171 *
172 * Logs an exception. If the exception is omitted or NULL, logs the current exception
173 * from the JNI environment, if any.
174 */
175#define LOG_EX(env, priority, tag, ...) \
176 IF_LOG(priority, tag) jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__)
177#define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__)
178#define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__)
179#define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__)
180#define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__)
181#define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__)
182
Dan Bornstein6edaa472009-10-26 13:33:22 -0700183/*
184 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
185 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
186 * not already defined, then define it here.
187 */
188#ifndef TEMP_FAILURE_RETRY
189/* Used to retry syscalls that can return EINTR. */
190#define TEMP_FAILURE_RETRY(exp) ({ \
191 typeof (exp) _rc; \
192 do { \
193 _rc = (exp); \
194 } while (_rc == -1 && errno == EINTR); \
195 _rc; })
196#endif
197
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800198#endif /*_NATIVEHELPER_JNIHELP_H*/