summaryrefslogtreecommitdiffstats
path: root/java/src/jni/h5jni.h
blob: 4a94180baf0a46d1b408b061ccf1c5c4fcfdc812 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  For details of the HDF libraries, see the HDF Documentation at:
 *    http://hdfgroup.org/HDF5/doc/
 *
 */

#include <jni.h>
#include "H5version.h"
#include <string.h>
#include "H5private.h"

#ifndef _Included_h5jni
#define _Included_h5jni

#ifdef __cplusplus
  #define ENVPTR (env)
  #define ENVONLY
  #define CBENVPTR (cbenv)
  #define CBENVONLY
  #define JVMPTR (jvm)
  #define JVMPAR
#else /* __cplusplus */
  #define ENVPTR (*env)
  #define ENVONLY env
  #define CBENVPTR (*cbenv)
  #define CBENVONLY cbenv
  #define JVMPTR (*jvm)
  #define JVMPAR jvm
#endif /* __cplusplus */

/*
 * Used to silence compiler when a particular
 * function parameter is not used.
 */
#define UNUSED(o) (void) o

/* Macros for class access */
/* Calling code must define ret_obj as jobject */
#define CALL_CONSTRUCTOR(env, classname, classsig, args, ret_obj)                          \
{                                                                                          \
    jmethodID constructor;                                                                 \
    jclass    cls;                                                                         \
                                                                                           \
    if (NULL == (cls = ENVPTR->FindClass(env, (classname)))) {                             \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                \
        H5_JNI_FATAL_ERROR(env, "JNI error: GetObjectClass");                              \
    }                                                                                      \
    if (NULL == (constructor = ENVPTR->GetMethodID(ENVONLY, cls, "<init>", (classsig)))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                \
        H5_JNI_FATAL_ERROR(env, "JNI error: GetMethodID failed");                          \
    }                                                                                      \
    if (NULL == (ret_obj = ENVPTR->NewObjectA(ENVONLY, cls, constructor, (args)))) {       \
        CHECK_JNI_EXCEPTION(env, JNI_FALSE);                                               \
    }                                                                                      \
}

/*
 * Macros for pinning/unpinning objects.
 */
#define PIN_BYTE_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                   \
    if (NULL == (outBuf = ENVPTR->GetByteArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                         \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                        \
    }                                                                               \
}

#define PIN_BYTE_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                  \
    if (NULL == (outBuf = (jbyte *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                        \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                       \
    }                                                                                              \
}

#define UNPIN_BYTE_ARRAY(env, pinnedArray, bufToRelease, freeMode)                        \
{                                                                                         \
    ENVPTR->ReleaseByteArrayElements(env, pinnedArray, (jbyte *) bufToRelease, freeMode); \
}

#define PIN_SHORT_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                    \
    if (NULL == (outBuf = ENVPTR->GetShortArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                          \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                         \
    }                                                                                \
}

#define PIN_SHORT_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                   \
    if (NULL == (outBuf = (jshort *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                         \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                        \
    }                                                                                               \
}

#define UNPIN_SHORT_ARRAY(env, pinnedArray, bufToRelease, freeMode)                         \
{                                                                                           \
    ENVPTR->ReleaseShortArrayElements(env, pinnedArray, (jshort *) bufToRelease, freeMode); \
}

#define PIN_INT_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                  \
    if (NULL == (outBuf = ENVPTR->GetIntArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                        \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                       \
    }                                                                              \
}

#define PIN_INT_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                 \
    if (NULL == (outBuf = (jint *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                       \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                      \
    }                                                                                             \
}

#define UNPIN_INT_ARRAY(env, pinnedArray, bufToRelease, freeMode)                       \
{                                                                                       \
    ENVPTR->ReleaseIntArrayElements(env, pinnedArray, (jint *) bufToRelease, freeMode); \
}

#define PIN_LONG_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                   \
    if (NULL == (outBuf = ENVPTR->GetLongArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                         \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                        \
    }                                                                               \
}

#define PIN_LONG_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                  \
    if (NULL == (outBuf = (jlong *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                        \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                       \
    }                                                                                              \
}

#define UNPIN_LONG_ARRAY(env, pinnedArray, bufToRelease, freeMode)                        \
{                                                                                         \
    ENVPTR->ReleaseLongArrayElements(env, pinnedArray, (jlong *) bufToRelease, freeMode); \
}

#define PIN_FLOAT_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                    \
    if (NULL == (outBuf = ENVPTR->GetFloatArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                          \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                         \
    }                                                                                \
}

#define PIN_FLOAT_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                   \
    if (NULL == (outBuf = (jfloat *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                         \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                        \
    }                                                                                               \
}

#define UNPIN_FLOAT_ARRAY(env, pinnedArray, bufToRelease, freeMode)                         \
{                                                                                           \
    ENVPTR->ReleaseFloatArrayElements(env, pinnedArray, (jfloat *) bufToRelease, freeMode); \
}

#define PIN_DOUBLE_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                 \
{                                                                                     \
    if (NULL == (outBuf = ENVPTR->GetDoubleArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                           \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                          \
    }                                                                                 \
}

#define PIN_DOUBLE_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                       \
{                                                                                                    \
    if (NULL == (outBuf = (jdouble *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                          \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                         \
    }                                                                                                \
}

#define UNPIN_DOUBLE_ARRAY(env, pinnedArray, bufToRelease, freeMode)                          \
{                                                                                             \
    ENVPTR->ReleaseDoubleArrayElements(env, pinnedArray, (jdouble *) bufToRelease, freeMode); \
}

#define PIN_BOOL_ARRAY(env, arrayToPin, outBuf, isCopy, failErrMsg)                    \
{                                                                                      \
    if (NULL == (outBuf = ENVPTR->GetBooleanArrayElements(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                            \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                           \
    }                                                                                  \
}

#define PIN_BOOL_ARRAY_CRITICAL(env, arrayToPin, outBuf, isCopy, failErrMsg)                          \
{                                                                                                     \
    if (NULL == (outBuf = (jboolean *) ENVPTR->GetPrimitiveArrayCritical(env, arrayToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                                           \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                                          \
    }                                                                                                 \
}

#define UNPIN_BOOL_ARRAY(env, pinnedArray, bufToRelease, freeMode)                              \
{                                                                                               \
    ENVPTR->ReleaseBooleanArrayElements(env, pinnedArray, (jboolean *) bufToRelease, freeMode); \
}

#define UNPIN_ARRAY_CRITICAL(env, pinnedArray, bufToRelease, freeMode)               \
{                                                                                    \
    ENVPTR->ReleasePrimitiveArrayCritical(env, pinnedArray, bufToRelease, freeMode); \
}

/* Macros for string access */
#define PIN_JAVA_STRING(env, stringToPin, outString, isCopy, failErrMsg)             \
{                                                                                    \
    if (NULL == (outString = ENVPTR->GetStringUTFChars(env, stringToPin, isCopy))) { \
        CHECK_JNI_EXCEPTION(env, JNI_TRUE);                                          \
        H5_JNI_FATAL_ERROR(env, failErrMsg);                                         \
    }                                                                                \
}

#define UNPIN_JAVA_STRING(env, pinnedString, stringToRelease)          \
{                                                                      \
    ENVPTR->ReleaseStringUTFChars(env, pinnedString, stringToRelease); \
}

/*
 * Macro to check for a JNI exception after a JNI method is called.
 * If an exception occurred, the value of 'clearException' will determine
 * whether or not the exception will be cleared in order for the native
 * method to do its own error handling.
 *
 * If the exception does not get cleared, this macro will skip to the
 * cleanup+return section of the native method, since at that point
 * cleaning up and returning is the only safe thing that can be done.
 */
#define CHECK_JNI_EXCEPTION(env, clearException)   \
{                                                  \
    if (JNI_TRUE == (*env)->ExceptionCheck(env)) { \
        if (JNI_TRUE == clearException)            \
            (*env)->ExceptionClear(env);           \
        else                                       \
            goto done;                             \
    }                                              \
}

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

extern jboolean h5JNIFatalError(JNIEnv *env, const char *);
extern jboolean h5nullArgument(JNIEnv *env, const char *);
extern jboolean h5badArgument(JNIEnv *env, const char *);
extern jboolean h5outOfMemory(JNIEnv *env, const char *);
extern jboolean h5libraryError(JNIEnv *env);
extern jboolean h5raiseException(JNIEnv *env, const char *, const char *);
extern jboolean h5unimplemented( JNIEnv *env, const char *functName);

/*
 * The following macros are to facilitate immediate cleanup+return
 * from a native JNI method when an exception is to be thrown.
 * Since, in general, the "cleanup" methods are the only safe JNI
 * methods to call once an exception has occurred, we want to immediately
 * cleanup and return instead of letting the native method continue.
 *
 * Note that a native method can clear the exception when one occurs and
 * then do its own error handling, but we instead opt to immediately return.
 */
#define H5_JNI_FATAL_ERROR(env, message) \
{                                        \
    h5JNIFatalError(env, message);       \
    goto done;                           \
}

#define H5_NULL_ARGUMENT_ERROR(env, message) \
{                                            \
    h5nullArgument(env, message);            \
    goto done;                               \
}

#define H5_BAD_ARGUMENT_ERROR(env, message) \
{                                           \
    h5badArgument(env, message);            \
    goto done;                              \
}

#define H5_OUT_OF_MEMORY_ERROR(env, message) \
{                                            \
    h5outOfMemory(env, message);             \
    goto done;                               \
}

#define H5_LIBRARY_ERROR(env) \
{                             \
    h5libraryError(env);      \
    goto done;                \
}

#define H5_RAISE_EXCEPTION(env, message, exception) \
{                                                   \
    h5raiseException(env, message, exception);      \
    goto done;                                      \
}

#define H5_UNIMPLEMENTED(env, message) \
{                                      \
    h5unimplemented(env, message);     \
    goto done;                         \
}

/* implemented at H5.c */
extern jint get_enum_value(JNIEnv *env, jobject enum_obj);
extern jobject get_enum_object(JNIEnv *env, const char* enum_class_name,
    jint enum_val, const char* enum_field_desc);

/* implemented at H5G.c */
extern jobject create_H5G_info_t(JNIEnv *env, H5G_info_t group_info);

#ifdef __cplusplus
} /* end extern "C" */
#endif /* __cplusplus */

#endif /* _Included_h5jni */