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" |
| 28 | |
| 29 | #ifndef NELEM |
| 30 | # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 31 | #endif |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | * Register one or more native methods with a particular class. |
| 39 | */ |
| 40 | int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, |
| 41 | const JNINativeMethod* gMethods, int numMethods); |
| 42 | |
| 43 | /* |
| 44 | * Throw an exception with the specified class and an optional message. |
Andy McFadden | 0b9d26c | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 45 | * The "className" argument will be passed directly to FindClass, which |
| 46 | * takes strings with slashes (e.g. "java/lang/Object"). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | * |
| 48 | * Returns 0 on success, nonzero if something failed (e.g. the exception |
| 49 | * class couldn't be found). |
| 50 | * |
| 51 | * Currently aborts the VM if it can't throw the exception. |
| 52 | */ |
| 53 | int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); |
| 54 | |
| 55 | /* |
| 56 | * Throw a java.IO.IOException, generating the message from errno. |
| 57 | */ |
| 58 | int jniThrowIOException(C_JNIEnv* env, int errnum); |
| 59 | |
| 60 | /* |
Elliott Hughes | e4239ae | 2009-10-20 16:59:01 -0700 | [diff] [blame^] | 61 | * Return a pointer to a locale-dependent error string explaining errno |
| 62 | * value 'errnum'. The returned pointer may or may not be equal to 'buf'. |
| 63 | * This function is thread-safe (unlike strerror) and portable (unlike |
| 64 | * strerror_r). |
| 65 | */ |
| 66 | const char* jniStrError(int errnum, char* buf, size_t buflen); |
| 67 | |
| 68 | /* |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | * Create a java.io.FileDescriptor given an integer fd |
| 70 | */ |
| 71 | jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); |
| 72 | |
| 73 | /* |
| 74 | * Get an int file descriptor from a java.io.FileDescriptor |
| 75 | */ |
| 76 | int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); |
| 77 | |
| 78 | /* |
| 79 | * Set an int file descriptor to a java.io.FileDescriptor |
| 80 | */ |
| 81 | void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); |
| 82 | |
| 83 | #ifdef __cplusplus |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 90 | * inlines these, even on non-optimized builds. |
| 91 | */ |
| 92 | #if defined(__cplusplus) && !defined(JNI_FORCE_C) |
| 93 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, |
| 94 | const JNINativeMethod* gMethods, int numMethods) |
| 95 | { |
| 96 | return jniRegisterNativeMethods(&env->functions, className, gMethods, |
| 97 | numMethods); |
| 98 | } |
| 99 | inline int jniThrowException(JNIEnv* env, const char* className, |
| 100 | const char* msg) |
| 101 | { |
| 102 | return jniThrowException(&env->functions, className, msg); |
| 103 | } |
| 104 | inline int jniThrowIOException(JNIEnv* env, int errnum) |
| 105 | { |
| 106 | return jniThrowIOException(&env->functions, errnum); |
| 107 | } |
| 108 | inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) |
| 109 | { |
| 110 | return jniCreateFileDescriptor(&env->functions, fd); |
| 111 | } |
| 112 | inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) |
| 113 | { |
| 114 | return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); |
| 115 | } |
| 116 | inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, |
| 117 | int value) |
| 118 | { |
| 119 | return jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | #endif /*_NATIVEHELPER_JNIHELP_H*/ |