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 | */ |
| 23 | #ifndef _NATIVEHELPER_JNIHELP_H |
| 24 | #define _NATIVEHELPER_JNIHELP_H |
| 25 | |
| 26 | #include "jni.h" |
| 27 | #include "utils/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. |
| 40 | */ |
| 41 | int 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 McFadden | 0b9d26c | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 46 | * The "className" argument will be passed directly to FindClass, which |
| 47 | * takes strings with slashes (e.g. "java/lang/Object"). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 48 | * |
| 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 | */ |
| 54 | int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); |
| 55 | |
| 56 | /* |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame^] | 57 | * Throw a java.lang.NullPointerException, with an optional message. |
| 58 | */ |
| 59 | int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); |
| 60 | |
| 61 | /* |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 62 | * Throw a java.lang.RuntimeException, with an optional message. |
| 63 | */ |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame^] | 64 | int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * 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] | 68 | */ |
| 69 | int jniThrowIOException(C_JNIEnv* env, int errnum); |
| 70 | |
| 71 | /* |
Elliott Hughes | e4239ae | 2009-10-20 16:59:01 -0700 | [diff] [blame] | 72 | * 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 | */ |
| 77 | const char* jniStrError(int errnum, char* buf, size_t buflen); |
| 78 | |
| 79 | /* |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | * Create a java.io.FileDescriptor given an integer fd |
| 81 | */ |
| 82 | jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); |
| 83 | |
| 84 | /* |
| 85 | * Get an int file descriptor from a java.io.FileDescriptor |
| 86 | */ |
| 87 | int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); |
| 88 | |
| 89 | /* |
| 90 | * Set an int file descriptor to a java.io.FileDescriptor |
| 91 | */ |
| 92 | void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); |
| 93 | |
| 94 | #ifdef __cplusplus |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 101 | * inlines these, even on non-optimized builds. |
| 102 | */ |
| 103 | #if defined(__cplusplus) && !defined(JNI_FORCE_C) |
| 104 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, |
| 105 | const JNINativeMethod* gMethods, int numMethods) |
| 106 | { |
| 107 | return jniRegisterNativeMethods(&env->functions, className, gMethods, |
| 108 | numMethods); |
| 109 | } |
| 110 | inline int jniThrowException(JNIEnv* env, const char* className, |
| 111 | const char* msg) |
| 112 | { |
| 113 | return jniThrowException(&env->functions, className, msg); |
| 114 | } |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame^] | 115 | inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) |
| 116 | { |
| 117 | return jniThrowNullPointerException(&env->functions, msg); |
| 118 | } |
| 119 | inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) |
| 120 | { |
| 121 | return jniThrowRuntimeException(&env->functions, msg); |
| 122 | } |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 123 | inline int jniThrowIOException(JNIEnv* env, int errnum) |
| 124 | { |
| 125 | return jniThrowIOException(&env->functions, errnum); |
| 126 | } |
| 127 | inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) |
| 128 | { |
| 129 | return jniCreateFileDescriptor(&env->functions, fd); |
| 130 | } |
| 131 | inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) |
| 132 | { |
| 133 | return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); |
| 134 | } |
| 135 | inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, |
| 136 | int value) |
| 137 | { |
| 138 | return jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); |
| 139 | } |
| 140 | #endif |
| 141 | |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 142 | /* |
| 143 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 144 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 145 | * not already defined, then define it here. |
| 146 | */ |
| 147 | #ifndef TEMP_FAILURE_RETRY |
| 148 | /* Used to retry syscalls that can return EINTR. */ |
| 149 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 150 | typeof (exp) _rc; \ |
| 151 | do { \ |
| 152 | _rc = (exp); \ |
| 153 | } while (_rc == -1 && errno == EINTR); \ |
| 154 | _rc; }) |
| 155 | #endif |
| 156 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | #endif /*_NATIVEHELPER_JNIHELP_H*/ |