1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  *  For details of the HDF libraries, see the HDF Documentation at:
16  *    http://hdfgroup.org/HDF5/doc/
17  *
18  */
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif /* __cplusplus */
23 
24 #include <stdlib.h>
25 #include "hdf5.h"
26 #include "h5jni.h"
27 #include "h5pDAPLImp.h"
28 
29 /*
30  * Pointer to the JNI's Virtual Machine; used for callback functions.
31  */
32 extern JavaVM *jvm;
33 
34 typedef struct _cb_wrapper {
35     jobject visit_callback;
36     jobject op_data;
37 } cb_wrapper;
38 
39 static herr_t H5D_append_cb(hid_t dataset_id, hsize_t *cur_dims, void *cb_data);
40 
41 /*
42  * Class:     hdf_hdf5lib_H5
43  * Method:    H5Pset_chunk_cache
44  * Signature: (JJJD)V
45  */
46 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)47 Java_hdf_hdf5lib_H5_H5Pset_1chunk_1cache
48     (JNIEnv *env, jclass clss, jlong dapl, jlong rdcc_nslots,
49         jlong rdcc_nbytes, jdouble rdcc_w0)
50 {
51     UNUSED(clss);
52 
53     if (H5Pset_chunk_cache((hid_t)dapl, (size_t)rdcc_nslots, (size_t)rdcc_nbytes, (double) rdcc_w0) < 0)
54         H5_LIBRARY_ERROR(ENVONLY);
55 
56 done:
57     return;
58 } /* end Java_hdf_hdf5lib_H5_H5Pset_1chunk_1cache */
59 
60 /*
61  * Class:     hdf_hdf5lib_H5
62  * Method:    H5Pget_chunk_cache
63  * Signature: (J[J[J[D)V
64  */
65 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)66 Java_hdf_hdf5lib_H5_H5Pget_1chunk_1cache
67     (JNIEnv *env, jclass clss, jlong dapl, jlongArray rdcc_nslots,
68         jlongArray rdcc_nbytes, jdoubleArray rdcc_w0)
69 {
70     jboolean  isCopy;
71     jdouble  *w0Array = NULL;
72     jlong    *rdcc_nslotsArray = NULL;
73     jlong    *nbytesArray = NULL;
74     herr_t    status = FAIL;
75 
76     UNUSED(clss);
77 
78     if (NULL != rdcc_w0)
79         PIN_DOUBLE_ARRAY(ENVONLY, rdcc_w0, w0Array, &isCopy, "H5Pget_chunk_cache: rdcc_w0 array not pinned");
80     if (NULL != rdcc_nslots)
81         PIN_LONG_ARRAY(ENVONLY, rdcc_nslots, rdcc_nslotsArray, &isCopy, "H5Pget_chunk_cache: rdcc_nslots array not pinned");
82     if (NULL != rdcc_nbytes)
83         PIN_LONG_ARRAY(ENVONLY, rdcc_nbytes, nbytesArray, &isCopy, "H5Pget_chunk_cache: nbytesArray array not pinned");
84 
85     {
86         /* direct cast (size_t *)variable fails on 32-bit environment */
87         long long rdcc_nslots_temp = *rdcc_nslotsArray;
88         long long nbytes_temp = *nbytesArray;
89         size_t    rdcc_nslots_t = (size_t) rdcc_nslots_temp;
90         size_t    nbytes_t = (size_t) nbytes_temp;
91 
92         if ((status = H5Pget_chunk_cache((hid_t)dapl, &rdcc_nslots_t, &nbytes_t, (double *)w0Array)) < 0)
93             H5_LIBRARY_ERROR(ENVONLY);
94 
95         *rdcc_nslotsArray = (jlong)rdcc_nslots_t;
96         *nbytesArray = (jlong)nbytes_t;
97     } /* end direct cast special handling */
98 
99 done:
100     if (nbytesArray)
101         UNPIN_LONG_ARRAY(ENVONLY, rdcc_nbytes, nbytesArray, (status < 0) ? JNI_ABORT : 0);
102     if (rdcc_nslotsArray)
103         UNPIN_LONG_ARRAY(ENVONLY, rdcc_nslots, rdcc_nslotsArray, (status < 0) ? JNI_ABORT : 0);
104     if (w0Array)
105         UNPIN_DOUBLE_ARRAY(ENVONLY, rdcc_w0, w0Array, (status < 0) ? JNI_ABORT : 0);
106 } /* end Java_hdf_hdf5lib_H5_H5Pget_1chunk_1cache */
107 
108 /*
109  * Class:     hdf_hdf5lib_H5
110  * Method:    H5Pset_efile_prefix
111  * Signature: (JLjava/lang/String;)V
112  */
113 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1efile_1prefix(JNIEnv * env,jclass clss,jlong dapl_id,jstring prefix)114 Java_hdf_hdf5lib_H5_H5Pset_1efile_1prefix
115     (JNIEnv *env, jclass clss, jlong dapl_id, jstring prefix)
116 {
117     const char *extFilePrefix = NULL;
118     herr_t      retVal = FAIL;
119 
120     UNUSED(clss);
121 
122     if (NULL == prefix)
123         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_efile_prefix: external file prefix is NULL");
124 
125     PIN_JAVA_STRING(ENVONLY, prefix, extFilePrefix, NULL, "H5Pset_efile_prefix: external file prefix not pinned");
126 
127     if ((retVal = H5Pset_efile_prefix((hid_t)dapl_id, extFilePrefix)) < 0)
128         H5_LIBRARY_ERROR(ENVONLY);
129 
130 done:
131     if (extFilePrefix)
132         UNPIN_JAVA_STRING(ENVONLY, prefix, extFilePrefix);
133 } /* end Java_hdf_hdf5lib_H5_H5Pset_1efile_1prefix */
134 
135 /*
136  * Class:     hdf_hdf5lib_H5
137  * Method:    H5Pget_efile_prefix
138  * Signature: (J)Ljava/lang/String;
139  */
140 JNIEXPORT jstring JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1efile_1prefix(JNIEnv * env,jclass clss,jlong dapl_id)141 Java_hdf_hdf5lib_H5_H5Pget_1efile_1prefix
142     (JNIEnv *env, jclass clss, jlong dapl_id)
143 {
144     ssize_t  prefix_size = -1;
145     char    *pre = NULL;
146     jstring  str = NULL;
147 
148     UNUSED(clss);
149 
150     if ((prefix_size = H5Pget_efile_prefix((hid_t)dapl_id, (char *)NULL, 0)) < 0)
151         H5_LIBRARY_ERROR(ENVONLY);
152 
153     if (NULL == (pre = (char *) HDmalloc(sizeof(char) * (size_t)prefix_size + 1)))
154         H5_JNI_FATAL_ERROR(ENVONLY, "H5Pget_efile_prefix: memory allocation failed");
155 
156     if (H5Pget_efile_prefix((hid_t)dapl_id, (char *)pre, (size_t)prefix_size + 1) < 0)
157         H5_LIBRARY_ERROR(ENVONLY);
158     pre[(size_t)prefix_size] = '\0';
159 
160     if (NULL == (str = ENVPTR->NewStringUTF(ENVONLY, pre))) {
161         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
162         H5_JNI_FATAL_ERROR(ENVONLY, "H5Pget_efile_prefix: out of memory - unable to construct string from UTF characters");
163     }
164 
165 done:
166     if (pre)
167         HDfree(pre);
168 
169     return (jstring)str;
170 } /* end Java_hdf_hdf5lib_H5_H5Pget_1efile_1prefix */
171 
172 /*
173  * Class:     hdf_hdf5lib_H5
174  * Method:    H5Pset_append_flush
175  * Signature: (JI[JLjava/lang/Object;Ljava/lang/Object;)V
176  */
177 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)178 Java_hdf_hdf5lib_H5_H5Pset_1append_1flush
179     (JNIEnv *env, jclass clss, jlong plist_id, jint ndims, jlongArray boundary, jobject callback_op, jobject op_data)
180 {
181     cb_wrapper wrapper = { callback_op, op_data };
182     herr_t     status = FAIL;
183 
184     UNUSED(clss);
185 
186     ENVPTR->GetJavaVM(ENVONLY, &jvm);
187     CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
188 
189     if (NULL == op_data)
190         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_append_flush: op_data is NULL");
191     if (NULL == callback_op)
192         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Pset_append_flush: callback_op is NULL");
193 
194     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)
195         H5_LIBRARY_ERROR(ENVONLY);
196 
197 done:
198     return;
199 } /* end Java_hdf_hdf5lib_H5_H5Pset_1append_1flush */
200 
201 /*
202  * TODO: H5Pget_append_flush
203  */
204 
205 /*
206  * Class:     hdf_hdf5lib_H5
207  * Method:    H5Pset_virtual_view
208  * Signature: (JI)V
209  */
210 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1virtual_1view(JNIEnv * env,jclass clss,jlong dapl_id,jint view)211 Java_hdf_hdf5lib_H5_H5Pset_1virtual_1view
212     (JNIEnv *env, jclass clss, jlong dapl_id, jint view)
213 {
214     UNUSED(clss);
215 
216     if (H5Pset_virtual_view((hid_t)dapl_id, (H5D_vds_view_t)view) < 0)
217         H5_LIBRARY_ERROR(ENVONLY);
218 
219 done:
220     return;
221 } /* end Java_hdf_hdf5lib_H5_H5Pset_1virtual_1view */
222 
223 /*
224  * Class:     hdf_hdf5lib_H5
225  * Method:    H5Pget_virtual_view
226  * Signature: (J)I
227  */
228 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1virtual_1view(JNIEnv * env,jclass clss,jlong dapl_id)229 Java_hdf_hdf5lib_H5_H5Pget_1virtual_1view
230     (JNIEnv *env, jclass clss, jlong dapl_id)
231 {
232     H5D_vds_view_t virtual_view = H5D_VDS_ERROR;
233 
234     UNUSED(clss);
235 
236     if (H5Pget_virtual_view((hid_t)dapl_id, &virtual_view) < 0)
237         H5_LIBRARY_ERROR(ENVONLY);
238 
239 done:
240     return (jint)virtual_view;
241 } /* end Java_hdf_hdf5lib_H5_H5Pget_1virtual_1view */
242 
243 /*
244  * Class:     hdf_hdf5lib_H5
245  * Method:    H5Pset_virtual_printf_gap
246  * Signature: (JJ)V
247  */
248 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Pset_1virtual_1printf_1gap(JNIEnv * env,jclass clss,jlong dapl_id,jlong gap_size)249 Java_hdf_hdf5lib_H5_H5Pset_1virtual_1printf_1gap
250     (JNIEnv *env, jclass clss, jlong dapl_id, jlong gap_size)
251 {
252     UNUSED(clss);
253 
254     if (H5Pset_virtual_printf_gap((hid_t)dapl_id, (hsize_t)gap_size) < 0)
255         H5_LIBRARY_ERROR(ENVONLY);
256 
257 done:
258     return;
259 } /* end Java_hdf_hdf5lib_H5_H5Pset_1virtual_1printf_1gap */
260 
261 /*
262  * Class:     hdf_hdf5lib_H5
263  * Method:    H5Pget_virtual_printf_gap
264  * Signature: (J)J
265  */
266 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Pget_1virtual_1printf_1gap(JNIEnv * env,jclass clss,jlong dapl_id)267 Java_hdf_hdf5lib_H5_H5Pget_1virtual_1printf_1gap
268     (JNIEnv *env, jclass clss, jlong dapl_id)
269 {
270     hsize_t gap_size = 0;
271 
272     UNUSED(clss);
273 
274     if (H5Pget_virtual_printf_gap((hid_t)dapl_id, &gap_size) < 0)
275         H5_LIBRARY_ERROR(ENVONLY);
276 
277 done:
278     return (jlong)gap_size;
279 } /* end Java_hdf_hdf5lib_H5_H5Pget_1virtual_1printf_1gap */
280 
281 static herr_t
H5D_append_cb(hid_t dataset_id,hsize_t * cur_dims,void * cb_data)282 H5D_append_cb
283     (hid_t dataset_id, hsize_t *cur_dims, void *cb_data)
284 {
285     cb_wrapper *wrapper = (cb_wrapper *)cb_data;
286     jlongArray  cur_dimsArray;
287     jmethodID   mid;
288     jobject     visit_callback = wrapper->visit_callback;
289     jclass      cls;
290     JNIEnv     *cbenv = NULL;
291     void       *op_data = (void *)wrapper->op_data;
292     jint        status = -1;
293 
294     if (JVMPTR->AttachCurrentThread(JVMPAR, (void **)&cbenv, NULL) < 0) {
295         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_TRUE);
296         H5_JNI_FATAL_ERROR(CBENVONLY, "H5D_append_cb: failed to attach current thread to JVM");
297     }
298 
299     if (NULL == (cls = CBENVPTR->GetObjectClass(CBENVONLY, visit_callback)))
300         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
301 
302     if (NULL == (mid = CBENVPTR->GetMethodID(CBENVONLY, cls, "callback", "(J[JLhdf/hdf5lib/callbacks/H5D_append_t;)I")))
303         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
304 
305     if (NULL != cur_dims) {
306         if (NULL == (cur_dimsArray = CBENVPTR->NewLongArray(CBENVONLY, 2)))
307             CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
308 
309         CBENVPTR->SetLongArrayRegion(CBENVONLY, cur_dimsArray, 0, 2, (const jlong *)cur_dims);
310         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
311 
312         status = CBENVPTR->CallIntMethod(CBENVONLY, visit_callback, mid, dataset_id, cur_dims, op_data);
313         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
314     }
315 
316 done:
317     if (CBENVONLY)
318         JVMPTR->DetachCurrentThread(JVMPAR);
319 
320     return (herr_t)status;
321 } /* end H5D_append_cb */
322 
323 #ifdef __cplusplus
324 } /* end extern "C" */
325 #endif /* __cplusplus */
326