blob: eec7af8a9f121389804245ba106c7a3e16c152c7 [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 */
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
34extern "C" {
35#endif
36
37/*
38 * Register one or more native methods with a particular class.
39 */
40int 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.
45 *
46 * Returns 0 on success, nonzero if something failed (e.g. the exception
47 * class couldn't be found).
48 *
49 * Currently aborts the VM if it can't throw the exception.
50 */
51int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
52
53/*
54 * Throw a java.IO.IOException, generating the message from errno.
55 */
56int jniThrowIOException(C_JNIEnv* env, int errnum);
57
58/*
59 * Create a java.io.FileDescriptor given an integer fd
60 */
61jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
62
63/*
64 * Get an int file descriptor from a java.io.FileDescriptor
65 */
66int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
67
68/*
69 * Set an int file descriptor to a java.io.FileDescriptor
70 */
71void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
72
73#ifdef __cplusplus
74}
75#endif
76
77
78/*
79 * For C++ code, we provide inlines that map to the C functions. g++ always
80 * inlines these, even on non-optimized builds.
81 */
82#if defined(__cplusplus) && !defined(JNI_FORCE_C)
83inline int jniRegisterNativeMethods(JNIEnv* env, const char* className,
84 const JNINativeMethod* gMethods, int numMethods)
85{
86 return jniRegisterNativeMethods(&env->functions, className, gMethods,
87 numMethods);
88}
89inline int jniThrowException(JNIEnv* env, const char* className,
90 const char* msg)
91{
92 return jniThrowException(&env->functions, className, msg);
93}
94inline int jniThrowIOException(JNIEnv* env, int errnum)
95{
96 return jniThrowIOException(&env->functions, errnum);
97}
98inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd)
99{
100 return jniCreateFileDescriptor(&env->functions, fd);
101}
102inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor)
103{
104 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
105}
106inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor,
107 int value)
108{
109 return jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
110}
111#endif
112
113#endif /*_NATIVEHELPER_JNIHELP_H*/