summaryrefslogtreecommitdiffstats
path: root/java/src/jni/h5pDAPLImp.c
blob: 01108b360b12f3c0a9510dc0cd2b2d9ecbf98aa5 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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://www.hdfgroup.org/licenses.               *
 * 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/
 *
 */

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

#include <stdlib.h>
#include "hdf5.h"
#include "h5jni.h"
#include "h5pDAPLImp.h"

/*
 * Pointer to the JNI's Virtual Machine; used for callback functions.
 */
extern JavaVM *jvm;

typedef struct _cb_wrapper {
    jobject visit_callback;
    jobject op_data;
} cb_wrapper;

static herr_t H5D_append_cb(hid_t dataset_id, hsize_t *cur_dims, void *cb_data);

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pset_chunk_cache
 * Signature: (JJJD)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1chunk_1cache(JNIEnv *env, jclass clss, jlong dapl, jlong rdcc_nslots,
                                         jlong rdcc_nbytes, jdouble rdcc_w0)
{
    UNUSED(clss);

    if (H5Pset_chunk_cache((hid_t)dapl, (size_t)rdcc_nslots, (size_t)rdcc_nbytes, (double)rdcc_w0) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return;
} /* end Java_hdf_hdf5lib_H5_H5Pset_1chunk_1cache */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pget_chunk_cache
 * Signature: (J[J[J[D)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1chunk_1cache(JNIEnv *env, jclass clss, jlong dapl, jlongArray rdcc_nslots,
                                         jlongArray rdcc_nbytes, jdoubleArray rdcc_w0)
{
    jboolean isCopy;
    jdouble *w0Array          = NULL;
    jlong   *rdcc_nslotsArray = NULL;
    jlong   *nbytesArray      = NULL;
    herr_t   status           = FAIL;

    UNUSED(clss);

    if (NULL != rdcc_w0)
        PIN_DOUBLE_ARRAY(ENVONLY, rdcc_w0, w0Array, &isCopy, "H5Pget_chunk_cache: rdcc_w0 array not pinned");
    if (NULL != rdcc_nslots)
        PIN_LONG_ARRAY(ENVONLY, rdcc_nslots, rdcc_nslotsArray, &isCopy,
                       "H5Pget_chunk_cache: rdcc_nslots array not pinned");
    if (NULL != rdcc_nbytes)
        PIN_LONG_ARRAY(ENVONLY, rdcc_nbytes, nbytesArray, &isCopy,
                       "H5Pget_chunk_cache: nbytesArray array not pinned");

    {
        /* direct cast (size_t *)variable fails on 32-bit environment */
        long long rdcc_nslots_temp = *rdcc_nslotsArray;
        long long nbytes_temp      = *nbytesArray;
        size_t    rdcc_nslots_t    = (size_t)rdcc_nslots_temp;
        size_t    nbytes_t         = (size_t)nbytes_temp;

        if ((status = H5Pget_chunk_cache((hid_t)dapl, &rdcc_nslots_t, &nbytes_t, (double *)w0Array)) < 0)
            H5_LIBRARY_ERROR(ENVONLY);

        *rdcc_nslotsArray = (jlong)rdcc_nslots_t;
        *nbytesArray      = (jlong)nbytes_t;
    } /* end direct cast special handling */

done:
    if (nbytesArray)
        UNPIN_LONG_ARRAY(ENVONLY, rdcc_nbytes, nbytesArray, (status < 0) ? JNI_ABORT : 0);
    if (rdcc_nslotsArray)
        UNPIN_LONG_ARRAY(ENVONLY, rdcc_nslots, rdcc_nslotsArray, (status < 0) ? JNI_ABORT : 0);
    if (w0Array)
        UNPIN_DOUBLE_ARRAY(ENVONLY, rdcc_w0, w0Array, (status < 0) ? JNI_ABORT : 0);
} /* end Java_hdf_hdf5lib_H5_H5Pget_1chunk_1cache */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pset_efile_prefix
 * Signature: (JLjava/lang/String;)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1efile_1prefix(JNIEnv *env, jclass clss, jlong dapl_id, jstring prefix)
{
    const char *extFilePrefix = NULL;
    herr_t      retVal        = FAIL;

    UNUSED(clss);

    if (NULL == prefix)
        H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_efile_prefix: external file prefix is NULL");

    PIN_JAVA_STRING(ENVONLY, prefix, extFilePrefix, NULL,
                    "H5Pset_efile_prefix: external file prefix not pinned");

    if ((retVal = H5Pset_efile_prefix((hid_t)dapl_id, extFilePrefix)) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    if (extFilePrefix)
        UNPIN_JAVA_STRING(ENVONLY, prefix, extFilePrefix);
} /* end Java_hdf_hdf5lib_H5_H5Pset_1efile_1prefix */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pget_efile_prefix
 * Signature: (J)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1efile_1prefix(JNIEnv *env, jclass clss, jlong dapl_id)
{
    ssize_t prefix_size = -1;
    char   *pre         = NULL;
    jstring str         = NULL;

    UNUSED(clss);

    if ((prefix_size = H5Pget_efile_prefix((hid_t)dapl_id, (char *)NULL, 0)) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

    if (NULL == (pre = (char *)HDmalloc(sizeof(char) * (size_t)prefix_size + 1)))
        H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Pget_efile_prefix: memory allocation failed");

    if (H5Pget_efile_prefix((hid_t)dapl_id, (char *)pre, (size_t)prefix_size + 1) < 0)
        H5_LIBRARY_ERROR(ENVONLY);
    pre[(size_t)prefix_size] = '\0';

    if (NULL == (str = ENVPTR->NewStringUTF(ENVONLY, pre))) {
        CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
        H5_OUT_OF_MEMORY_ERROR(
            ENVONLY, "H5Pget_efile_prefix: out of memory - unable to construct string from UTF characters");
    }

done:
    if (pre)
        HDfree(pre);

    return (jstring)str;
} /* end Java_hdf_hdf5lib_H5_H5Pget_1efile_1prefix */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pset_append_flush
 * Signature: (JI[JLjava/lang/Object;Ljava/lang/Object;)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1append_1flush(JNIEnv *env, jclass clss, jlong plist_id, jint ndims,
                                          jlongArray boundary, jobject callback_op, jobject op_data)
{
    cb_wrapper wrapper = {callback_op, op_data};
    herr_t     status  = FAIL;

    UNUSED(clss);

    ENVPTR->GetJavaVM(ENVONLY, &jvm);
    CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);

    if (NULL == op_data)
        H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_append_flush: op_data is NULL");
    if (NULL == callback_op)
        H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_append_flush: callback_op is NULL");

    if ((status = H5Pset_append_flush((hid_t)plist_id, (unsigned)ndims, (const hsize_t *)boundary,
                                      (H5D_append_cb_t)H5D_append_cb, (void *)&wrapper)) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return;
} /* end Java_hdf_hdf5lib_H5_H5Pset_1append_1flush */

/*
 * TODO: H5Pget_append_flush
 */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pset_virtual_view
 * Signature: (JI)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1virtual_1view(JNIEnv *env, jclass clss, jlong dapl_id, jint view)
{
    UNUSED(clss);

    if (H5Pset_virtual_view((hid_t)dapl_id, (H5D_vds_view_t)view) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return;
} /* end Java_hdf_hdf5lib_H5_H5Pset_1virtual_1view */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pget_virtual_view
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1virtual_1view(JNIEnv *env, jclass clss, jlong dapl_id)
{
    H5D_vds_view_t virtual_view = H5D_VDS_ERROR;

    UNUSED(clss);

    if (H5Pget_virtual_view((hid_t)dapl_id, &virtual_view) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return (jint)virtual_view;
} /* end Java_hdf_hdf5lib_H5_H5Pget_1virtual_1view */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pset_virtual_printf_gap
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1virtual_1printf_1gap(JNIEnv *env, jclass clss, jlong dapl_id, jlong gap_size)
{
    UNUSED(clss);

    if (H5Pset_virtual_printf_gap((hid_t)dapl_id, (hsize_t)gap_size) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return;
} /* end Java_hdf_hdf5lib_H5_H5Pset_1virtual_1printf_1gap */

/*
 * Class:     hdf_hdf5lib_H5
 * Method:    H5Pget_virtual_printf_gap
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1virtual_1printf_1gap(JNIEnv *env, jclass clss, jlong dapl_id)
{
    hsize_t gap_size = 0;

    UNUSED(clss);

    if (H5Pget_virtual_printf_gap((hid_t)dapl_id, &gap_size) < 0)
        H5_LIBRARY_ERROR(ENVONLY);

done:
    return (jlong)gap_size;
} /* end Java_hdf_hdf5lib_H5_H5Pget_1virtual_1printf_1gap */

static herr_t
H5D_append_cb(hid_t dataset_id, hsize_t *cur_dims, void *cb_data)
{
    cb_wrapper *wrapper = (cb_wrapper *)cb_data;
    jlongArray  cur_dimsArray;
    jmethodID   mid;
    jobject     visit_callback = wrapper->visit_callback;
    jclass      cls;
    JNIEnv     *cbenv   = NULL;
    void       *op_data = (void *)wrapper->op_data;
    jint        status  = -1;

    if (JVMPTR->AttachCurrentThread(JVMPAR, (void **)&cbenv, NULL) < 0) {
        CHECK_JNI_EXCEPTION(CBENVONLY, JNI_TRUE);
        H5_JNI_FATAL_ERROR(CBENVONLY, "H5D_append_cb: failed to attach current thread to JVM");
    }

    if (NULL == (cls = CBENVPTR->GetObjectClass(CBENVONLY, visit_callback)))
        CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);

    if (NULL == (mid = CBENVPTR->GetMethodID(CBENVONLY, cls, "callback",
                                             "(J[JLhdf/hdf5lib/callbacks/H5D_append_t;)I")))
        CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);

    if (NULL != cur_dims) {
        if (NULL == (cur_dimsArray = CBENVPTR->NewLongArray(CBENVONLY, 2)))
            CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);

        CBENVPTR->SetLongArrayRegion(CBENVONLY, cur_dimsArray, 0, 2, (const jlong *)cur_dims);
        CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);

        status = CBENVPTR->CallIntMethod(CBENVONLY, visit_callback, mid, dataset_id, cur_dims, op_data);
        CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
    }

done:
    if (CBENVONLY)
        JVMPTR->DetachCurrentThread(JVMPAR);

    return (herr_t)status;
} /* end H5D_append_cb */

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