The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 1ff7a0d | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 23 | #ifndef NATIVEHELPER_JNIHELP_H_ |
| 24 | #define NATIVEHELPER_JNIHELP_H_ |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
| 26 | #include "jni.h" |
Elliott Hughes | a6f951c | 2011-06-08 15:54:05 -0700 | [diff] [blame] | 27 | #include "cutils/log.h" |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | |
| 30 | #ifndef NELEM |
| 31 | # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 32 | #endif |
| 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /* |
| 39 | * Register one or more native methods with a particular class. |
Elliott Hughes | 73d3c2e | 2012-05-03 17:06:04 -0700 | [diff] [blame] | 40 | * "className" looks like "java/lang/String". Aborts on failure. |
| 41 | * TODO: fix all callers and change the return type to void. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | */ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 43 | int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Throw an exception with the specified class and an optional message. |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 47 | * |
Andy McFadden | 0b9d26c | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 48 | * The "className" argument will be passed directly to FindClass, which |
| 49 | * takes strings with slashes (e.g. "java/lang/Object"). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 50 | * |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 51 | * If an exception is currently pending, we log a warning message and |
| 52 | * clear it. |
| 53 | * |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 54 | * Returns 0 on success, nonzero if something failed (e.g. the exception |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 55 | * class couldn't be found, so *an* exception will still be pending). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | * |
| 57 | * Currently aborts the VM if it can't throw the exception. |
| 58 | */ |
| 59 | int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); |
| 60 | |
| 61 | /* |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 62 | * Throw a java.lang.NullPointerException, with an optional message. |
| 63 | */ |
| 64 | int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); |
| 65 | |
| 66 | /* |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 67 | * Throw a java.lang.RuntimeException, with an optional message. |
| 68 | */ |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 69 | int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Throw a java.io.IOException, generating the message from errno. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | */ |
| 74 | int jniThrowIOException(C_JNIEnv* env, int errnum); |
| 75 | |
| 76 | /* |
Elliott Hughes | e4239ae | 2009-10-20 16:59:01 -0700 | [diff] [blame] | 77 | * Return a pointer to a locale-dependent error string explaining errno |
| 78 | * value 'errnum'. The returned pointer may or may not be equal to 'buf'. |
| 79 | * This function is thread-safe (unlike strerror) and portable (unlike |
| 80 | * strerror_r). |
| 81 | */ |
| 82 | const char* jniStrError(int errnum, char* buf, size_t buflen); |
| 83 | |
| 84 | /* |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 85 | * Returns a new java.io.FileDescriptor for the given int fd. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 86 | */ |
| 87 | jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); |
| 88 | |
Carl Shapiro | abb4eff | 2010-06-08 16:37:12 -0700 | [diff] [blame] | 89 | /* |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 90 | * Returns the int fd from a java.io.FileDescriptor. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 91 | */ |
| 92 | int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); |
| 93 | |
| 94 | /* |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 95 | * Sets the int fd in a java.io.FileDescriptor. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 96 | */ |
| 97 | void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); |
| 98 | |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 99 | /* |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame^] | 100 | * Returns the reference from a java.lang.ref.Reference. |
| 101 | */ |
| 102 | jobject jniGetReferent(C_JNIEnv* env, jobject ref); |
| 103 | |
| 104 | /* |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 105 | * Log a message and an exception. |
| 106 | * If exception is NULL, logs the current exception in the JNI environment. |
| 107 | */ |
| 108 | void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); |
| 109 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | #ifdef __cplusplus |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 117 | * inlines these, even on non-optimized builds. |
| 118 | */ |
Andy McFadden | b53ec15 | 2011-02-08 16:12:33 -0800 | [diff] [blame] | 119 | #if defined(__cplusplus) |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 120 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { |
| 121 | return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 123 | |
| 124 | inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | return jniThrowException(&env->functions, className, msg); |
| 126 | } |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 128 | extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Equivalent to jniThrowException but with a printf-like format string and |
| 132 | * variable-length argument list. This is only available in C++. |
| 133 | */ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 134 | inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 135 | va_list args; |
| 136 | va_start(args, fmt); |
| 137 | return jniThrowExceptionFmt(&env->functions, className, fmt, args); |
| 138 | va_end(args); |
| 139 | } |
| 140 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 141 | inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 142 | return jniThrowNullPointerException(&env->functions, msg); |
| 143 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 144 | |
| 145 | inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 146 | return jniThrowRuntimeException(&env->functions, msg); |
| 147 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 148 | |
| 149 | inline int jniThrowIOException(JNIEnv* env, int errnum) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 150 | return jniThrowIOException(&env->functions, errnum); |
| 151 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 152 | |
| 153 | inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | return jniCreateFileDescriptor(&env->functions, fd); |
| 155 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 156 | |
| 157 | inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 158 | return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); |
| 159 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 160 | |
| 161 | inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 162 | jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); |
| 163 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 164 | |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame^] | 165 | inline jobject jniGetReferent(JNIEnv* env, jobject ref) { |
| 166 | return jniGetReferent(&env->functions, ref); |
| 167 | } |
| 168 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 169 | inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 170 | jniLogException(&env->functions, priority, tag, exception); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | } |
| 172 | #endif |
| 173 | |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 174 | /* Logging macros. |
| 175 | * |
| 176 | * Logs an exception. If the exception is omitted or NULL, logs the current exception |
| 177 | * from the JNI environment, if any. |
| 178 | */ |
| 179 | #define LOG_EX(env, priority, tag, ...) \ |
Steve Block | 4ed586e | 2011-10-12 17:24:48 +0100 | [diff] [blame] | 180 | IF_ALOG(priority, tag) jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__) |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 181 | #define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__) |
| 182 | #define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__) |
| 183 | #define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__) |
| 184 | #define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__) |
| 185 | #define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__) |
| 186 | |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 187 | /* |
| 188 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 189 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 190 | * not already defined, then define it here. |
| 191 | */ |
| 192 | #ifndef TEMP_FAILURE_RETRY |
| 193 | /* Used to retry syscalls that can return EINTR. */ |
| 194 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 195 | typeof (exp) _rc; \ |
| 196 | do { \ |
| 197 | _rc = (exp); \ |
| 198 | } while (_rc == -1 && errno == EINTR); \ |
| 199 | _rc; }) |
| 200 | #endif |
| 201 | |
Carl Shapiro | 1ff7a0d | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 202 | #endif /* NATIVEHELPER_JNIHELP_H_ */ |