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 | */ |
Orion Hodson | 44dbb40 | 2019-10-25 10:59:51 +0100 | [diff] [blame^] | 23 | #ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ |
| 24 | #define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
Dan Albert | 099d521 | 2014-05-21 13:25:01 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | |
Orion Hodson | 85cdde8 | 2019-10-24 15:48:11 +0100 | [diff] [blame] | 29 | #include "libnativehelper_api.h" |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 30 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 31 | #ifndef NELEM |
Orion Hodson | 85cdde8 | 2019-10-24 15:48:11 +0100 | [diff] [blame] | 32 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | #endif |
| 34 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 35 | /* |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 37 | * inlines these, even on non-optimized builds. |
| 38 | */ |
Andy McFadden | b53ec15 | 2011-02-08 16:12:33 -0800 | [diff] [blame] | 39 | #if defined(__cplusplus) |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 40 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 41 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { |
| 42 | return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 43 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 44 | |
| 45 | 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] | 46 | return jniThrowException(&env->functions, className, msg); |
| 47 | } |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Equivalent to jniThrowException but with a printf-like format string and |
| 51 | * variable-length argument list. This is only available in C++. |
| 52 | */ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 53 | inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 54 | va_list args; |
| 55 | va_start(args, fmt); |
| 56 | return jniThrowExceptionFmt(&env->functions, className, fmt, args); |
| 57 | va_end(args); |
| 58 | } |
| 59 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 60 | inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 61 | return jniThrowNullPointerException(&env->functions, msg); |
| 62 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 63 | |
| 64 | inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 65 | return jniThrowRuntimeException(&env->functions, msg); |
| 66 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 67 | |
| 68 | inline int jniThrowIOException(JNIEnv* env, int errnum) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | return jniThrowIOException(&env->functions, errnum); |
| 70 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 71 | |
| 72 | inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | return jniCreateFileDescriptor(&env->functions, fd); |
| 74 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 75 | |
| 76 | inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 77 | return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); |
| 78 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 79 | |
| 80 | inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 81 | jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); |
| 82 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 83 | |
Josh Gao | 669bc9e | 2018-06-25 16:22:11 -0700 | [diff] [blame] | 84 | inline jlong jniGetOwnerIdFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { |
| 85 | return jniGetOwnerIdFromFileDescriptor(&env->functions, fileDescriptor); |
| 86 | } |
| 87 | |
Orion Hodson | bcc2b6f | 2019-02-22 16:41:10 +0000 | [diff] [blame] | 88 | inline jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) { |
| 89 | return jniGetNioBufferBaseArray(&env->functions, nioBuffer); |
| 90 | } |
| 91 | |
| 92 | inline jint jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) { |
| 93 | return jniGetNioBufferBaseArrayOffset(&env->functions, nioBuffer); |
| 94 | } |
| 95 | |
| 96 | inline jlong jniGetNioBufferFields(JNIEnv* env, jobject nioBuffer, |
| 97 | jint* position, jint* limit, jint* elementSizeShift) { |
| 98 | return jniGetNioBufferFields(&env->functions, nioBuffer, |
| 99 | position, limit, elementSizeShift); |
| 100 | } |
| 101 | |
| 102 | inline jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) { |
| 103 | return jniGetNioBufferPointer(&env->functions, nioBuffer); |
| 104 | } |
| 105 | |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 106 | inline jobject jniGetReferent(JNIEnv* env, jobject ref) { |
| 107 | return jniGetReferent(&env->functions, ref); |
| 108 | } |
| 109 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 110 | inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) { |
| 111 | return jniCreateString(&env->functions, unicodeChars, len); |
| 112 | } |
| 113 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 114 | inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) { |
| 115 | return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len); |
| 116 | } |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 117 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 118 | 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] | 119 | jniLogException(&env->functions, priority, tag, exception); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 120 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 121 | |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame] | 122 | #endif // defined(__cplusplus) |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 123 | |
Orion Hodson | 44dbb40 | 2019-10-25 10:59:51 +0100 | [diff] [blame^] | 124 | #endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ |