blob: 858d62564cc3761ea7a25d2e3c359be1f0dc8429 [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 */
Orion Hodson44dbb402019-10-25 10:59:51 +010023#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_
24#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080025
Dan Albert099d5212014-05-21 13:25:01 -070026#include <errno.h>
Dan Bornstein6edaa472009-10-26 13:33:22 -070027#include <unistd.h>
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080028
Orion Hodson85cdde82019-10-24 15:48:11 +010029#include "libnativehelper_api.h"
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000030
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080031#ifndef NELEM
Orion Hodson85cdde82019-10-24 15:48:11 +010032#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080033#endif
34
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080035/*
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080036 * For C++ code, we provide inlines that map to the C functions. g++ always
37 * inlines these, even on non-optimized builds.
38 */
Andy McFaddenb53ec152011-02-08 16:12:33 -080039#if defined(__cplusplus)
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000040
Elliott Hughes87f62a82011-04-22 19:22:54 -070041inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
42 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080043}
Elliott Hughes87f62a82011-04-22 19:22:54 -070044
45inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080046 return jniThrowException(&env->functions, className, msg);
47}
Elliott Hughes2f82af12011-04-08 17:15:16 -070048
Elliott Hughes2f82af12011-04-08 17:15:16 -070049/*
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 Hughes87f62a82011-04-22 19:22:54 -070053inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
Elliott Hughes2f82af12011-04-08 17:15:16 -070054 va_list args;
55 va_start(args, fmt);
56 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
57 va_end(args);
58}
59
Elliott Hughes87f62a82011-04-22 19:22:54 -070060inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080061 return jniThrowNullPointerException(&env->functions, msg);
62}
Elliott Hughes87f62a82011-04-22 19:22:54 -070063
64inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
Elliott Hughes9f6e1d32010-01-28 13:43:39 -080065 return jniThrowRuntimeException(&env->functions, msg);
66}
Elliott Hughes87f62a82011-04-22 19:22:54 -070067
68inline int jniThrowIOException(JNIEnv* env, int errnum) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080069 return jniThrowIOException(&env->functions, errnum);
70}
Elliott Hughes87f62a82011-04-22 19:22:54 -070071
72inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080073 return jniCreateFileDescriptor(&env->functions, fd);
74}
Elliott Hughes87f62a82011-04-22 19:22:54 -070075
76inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
The Android Open Source Project7f844dd2009-03-03 19:28:47 -080077 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
78}
Elliott Hughes87f62a82011-04-22 19:22:54 -070079
80inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -070081 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
82}
Elliott Hughes87f62a82011-04-22 19:22:54 -070083
Josh Gao669bc9e2018-06-25 16:22:11 -070084inline jlong jniGetOwnerIdFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
85 return jniGetOwnerIdFromFileDescriptor(&env->functions, fileDescriptor);
86}
87
Orion Hodsonbcc2b6f2019-02-22 16:41:10 +000088inline jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
89 return jniGetNioBufferBaseArray(&env->functions, nioBuffer);
90}
91
92inline jint jniGetNioBufferBaseArrayOffset(JNIEnv* env, jobject nioBuffer) {
93 return jniGetNioBufferBaseArrayOffset(&env->functions, nioBuffer);
94}
95
96inline 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
102inline jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
103 return jniGetNioBufferPointer(&env->functions, nioBuffer);
104}
105
Jeff Browna7de6782013-04-02 18:09:29 -0700106inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
107 return jniGetReferent(&env->functions, ref);
108}
109
Fredrik Roubert2e312802017-07-04 21:53:08 +0200110inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
111 return jniCreateString(&env->functions, unicodeChars, len);
112}
113
Fredrik Roubert2e312802017-07-04 21:53:08 +0200114inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) {
115 return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len);
116}
Fredrik Roubert2e312802017-07-04 21:53:08 +0200117
Elliott Hughes87f62a82011-04-22 19:22:54 -0700118inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
Jeff Brown2c3fc7a2010-06-01 21:07:08 -0700119 jniLogException(&env->functions, priority, tag, exception);
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800120}
Brian Carlstromdd8af232012-05-13 23:56:07 -0700121
Orion Hodsonb01e7fe2018-11-07 06:07:50 +0000122#endif // defined(__cplusplus)
The Android Open Source Project7f844dd2009-03-03 19:28:47 -0800123
Orion Hodson44dbb402019-10-25 10:59:51 +0100124#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_