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