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://www.hdfgroup.org/licenses. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14 /*
15 * Module Info: This module contains the functionality for querying
16 * a "native" datatype for the H5T interface.
17 */
18
19 #include "H5Tmodule.h" /* This source code file is part of the H5T module */
20
21 #include "H5private.h" /* Generic Functions */
22 #include "H5CXprivate.h" /* API Contexts */
23 #include "H5Eprivate.h" /* Error handling */
24 #include "H5Iprivate.h" /* IDs */
25 #include "H5Pprivate.h" /* Property lists */
26 #include "H5MMprivate.h" /* Memory management */
27 #include "H5Tpkg.h" /* Datatypes */
28
29 /* Static local functions */
30 static H5T_t *H5T__get_native_type(H5T_t *dt, H5T_direction_t direction, size_t *struct_align, size_t *offset,
31 size_t *comp_size);
32 static H5T_t *H5T__get_native_integer(size_t prec, H5T_sign_t sign, H5T_direction_t direction,
33 size_t *struct_align, size_t *offset, size_t *comp_size);
34 static H5T_t *H5T__get_native_float(size_t size, H5T_direction_t direction, size_t *struct_align,
35 size_t *offset, size_t *comp_size);
36 static H5T_t *H5T__get_native_bitfield(size_t prec, H5T_direction_t direction, size_t *struct_align,
37 size_t *offset, size_t *comp_size);
38 static herr_t H5T__cmp_offset(size_t *comp_size, size_t *offset, size_t elem_size, size_t nelems,
39 size_t align, size_t *struct_align);
40
41 /*-------------------------------------------------------------------------
42 * Function: H5Tget_native_type
43 *
44 * Purpose: High-level API to return the native type of a datatype.
45 * The native type is chosen by matching the size and class of
46 * querried datatype from the following native premitive
47 * datatypes:
48 * H5T_NATIVE_CHAR H5T_NATIVE_UCHAR
49 * H5T_NATIVE_SHORT H5T_NATIVE_USHORT
50 * H5T_NATIVE_INT H5T_NATIVE_UINT
51 * H5T_NATIVE_LONG H5T_NATIVE_ULONG
52 * H5T_NATIVE_LLONG H5T_NATIVE_ULLONG
53 *
54 * H5T_NATIVE_FLOAT
55 * H5T_NATIVE_DOUBLE
56 * H5T_NATIVE_LDOUBLE
57 *
58 * Compound, array, enum, and VL types all choose among these
59 * types for theire members. Time, Bifield, Opaque, Reference
60 * types are only copy out.
61 *
62 * Return: Success: Returns the native data type if successful.
63 *
64 * Failure: negative
65 *
66 * Programmer: Raymond Lu
67 * Oct 3, 2002
68 *
69 *-------------------------------------------------------------------------
70 */
71 hid_t
H5Tget_native_type(hid_t type_id,H5T_direction_t direction)72 H5Tget_native_type(hid_t type_id, H5T_direction_t direction)
73 {
74 H5T_t *dt; /* Datatype to create native datatype from */
75 H5T_t *new_dt = NULL; /* Datatype for native datatype created */
76 size_t comp_size = 0; /* Compound datatype's size */
77 hid_t ret_value; /* Return value */
78
79 FUNC_ENTER_API(H5I_INVALID_HID)
80 H5TRACE2("i", "iTd", type_id, direction);
81
82 /* Check arguments */
83 if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
84 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not a data type")
85 if (direction != H5T_DIR_DEFAULT && direction != H5T_DIR_ASCEND && direction != H5T_DIR_DESCEND)
86 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not valid direction value")
87
88 /* Get the native type */
89 if (NULL == (new_dt = H5T__get_native_type(dt, direction, NULL, NULL, &comp_size)))
90 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "cannot retrieve native type")
91
92 /* Get an ID for the new type */
93 if ((ret_value = H5I_register(H5I_DATATYPE, new_dt, TRUE)) < 0)
94 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register data type")
95
96 done:
97 /* Error cleanup */
98 if (ret_value < 0)
99 if (new_dt && H5T_close_real(new_dt) < 0)
100 HDONE_ERROR(H5E_DATATYPE, H5E_CLOSEERROR, H5I_INVALID_HID, "unable to release datatype")
101
102 FUNC_LEAVE_API(ret_value)
103 } /* end H5Tget_native_type() */
104
105 /*-------------------------------------------------------------------------
106 * Function: H5T__get_native_type
107 *
108 * Purpose: Returns the native type of a datatype.
109 *
110 * Return: Success: Returns the native data type if successful.
111 *
112 * Failure: negative
113 *
114 * Programmer: Raymond Lu
115 * Oct 3, 2002
116 *
117 *-------------------------------------------------------------------------
118 */
119 static H5T_t *
H5T__get_native_type(H5T_t * dtype,H5T_direction_t direction,size_t * struct_align,size_t * offset,size_t * comp_size)120 H5T__get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *struct_align, size_t *offset,
121 size_t *comp_size)
122 {
123 H5T_t * super_type; /* Super type of VL, array and enum datatypes */
124 H5T_t * nat_super_type; /* Native form of VL, array & enum super datatype */
125 H5T_t * new_type = NULL; /* New native datatype */
126 H5T_t * memb_type = NULL; /* Datatype of member */
127 H5T_t **memb_list = NULL; /* List of compound member IDs */
128 size_t *memb_offset =
129 NULL; /* List of member offsets in compound type, including member size and alignment */
130 char ** comp_mname = NULL; /* List of member names in compound type */
131 char * memb_name = NULL; /* Enum's member name */
132 void * memb_value = NULL; /* Enum's member value */
133 void * tmp_memb_value = NULL; /* Enum's member value */
134 hsize_t * dims = NULL; /* Dimension sizes for array */
135 H5T_class_t h5_class; /* Class of datatype to make native */
136 size_t size; /* Size of datatype to make native */
137 size_t prec; /* Precision of datatype to make native */
138 int snmemb; /* Number of members in compound & enum types */
139 unsigned nmemb = 0; /* Number of members in compound & enum types */
140 unsigned u; /* Local index variable */
141 H5T_t * ret_value = NULL; /* Return value */
142
143 FUNC_ENTER_STATIC
144
145 HDassert(dtype);
146
147 if (H5T_NO_CLASS == (h5_class = H5T_get_class(dtype, FALSE)))
148 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid class")
149
150 if (0 == (size = H5T_get_size(dtype)))
151 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid size")
152
153 switch (h5_class) {
154 case H5T_INTEGER: {
155 H5T_sign_t sign; /* Signedness of integer type */
156
157 if (H5T_SGN_ERROR == (sign = H5T_get_sign(dtype)))
158 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid signess")
159
160 prec = dtype->shared->u.atomic.prec;
161
162 if (NULL ==
163 (ret_value = H5T__get_native_integer(prec, sign, direction, struct_align, offset, comp_size)))
164 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer type")
165 } /* end case */
166 break;
167
168 case H5T_FLOAT:
169 if (NULL == (ret_value = H5T__get_native_float(size, direction, struct_align, offset, comp_size)))
170 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type")
171
172 break;
173
174 case H5T_STRING:
175 if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
176 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type")
177
178 if (H5T_IS_VL_STRING(dtype->shared)) {
179 /* Update size, offset and compound alignment for parent. */
180 if (H5T__cmp_offset(comp_size, offset, sizeof(char *), (size_t)1, H5T_POINTER_COMP_ALIGN_g,
181 struct_align) < 0)
182 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
183 } /* end if */
184 else {
185 /* Update size, offset and compound alignment for parent. */
186 if (H5T__cmp_offset(comp_size, offset, sizeof(char), size, H5T_NATIVE_SCHAR_COMP_ALIGN_g,
187 struct_align) < 0)
188 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
189 } /* end else */
190 break;
191
192 /* The time type will be supported in the future. Simply return "not supported"
193 * message for now.*/
194 case H5T_TIME:
195 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "time type is not supported yet")
196
197 case H5T_BITFIELD: {
198 prec = dtype->shared->u.atomic.prec;
199
200 if (NULL ==
201 (ret_value = H5T__get_native_bitfield(prec, direction, struct_align, offset, comp_size)))
202 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer for bitfield type")
203 } /* end case */
204 break;
205
206 case H5T_OPAQUE:
207 if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
208 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type")
209
210 /* Update size, offset and compound alignment for parent. */
211 if (H5T__cmp_offset(comp_size, offset, sizeof(char), size, H5T_NATIVE_SCHAR_COMP_ALIGN_g,
212 struct_align) < 0)
213 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
214 break;
215
216 case H5T_REFERENCE: {
217 H5T_t *dt; /* Datatype to make native */
218 size_t align;
219 size_t ref_size;
220
221 if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
222 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy reference type")
223
224 /* Decide if the data type is object reference. */
225 if (NULL == (dt = (H5T_t *)H5I_object(H5T_STD_REF_OBJ_g)))
226 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type")
227
228 /* Update size, offset and compound alignment for parent. */
229 if (0 == H5T_cmp(ret_value, dt, FALSE)) {
230 align = H5T_HOBJREF_COMP_ALIGN_g;
231 ref_size = sizeof(hobj_ref_t);
232 } /* end if */
233 else {
234 /* Decide if the data type is dataset region reference. */
235 if (NULL == (dt = (H5T_t *)H5I_object(H5T_STD_REF_DSETREG_g)))
236 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type")
237
238 if (0 == H5T_cmp(ret_value, dt, FALSE)) {
239 align = H5T_HDSETREGREF_COMP_ALIGN_g;
240 ref_size = sizeof(hdset_reg_ref_t);
241 } /* end if */
242 else {
243 /* Only pointers to underlying opaque reference types */
244 align = H5T_REF_COMP_ALIGN_g;
245 ref_size = sizeof(H5R_ref_t);
246 } /* end else */
247 } /* end else */
248
249 if (H5T__cmp_offset(comp_size, offset, ref_size, (size_t)1, align, struct_align) < 0)
250 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
251 } /* end case */
252 break;
253
254 case H5T_COMPOUND: {
255 size_t children_size = 0; /* Total size of compound members */
256 size_t children_st_align =
257 0; /* The max alignment among compound members. This'll be the compound alignment */
258
259 if ((snmemb = H5T_get_nmembers(dtype)) <= 0)
260 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "compound data type doesn't have any member")
261 H5_CHECKED_ASSIGN(nmemb, unsigned, snmemb, int);
262
263 if (NULL == (memb_list = (H5T_t **)H5MM_calloc(nmemb * sizeof(H5T_t *))))
264 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
265 if (NULL == (memb_offset = (size_t *)H5MM_calloc(nmemb * sizeof(size_t))))
266 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
267 if (NULL == (comp_mname = (char **)H5MM_calloc(nmemb * sizeof(char *))))
268 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
269
270 /* Construct child compound type and retrieve a list of their IDs, offsets, total size, and
271 * alignment for compound type. */
272 for (u = 0; u < nmemb; u++) {
273 if (NULL == (memb_type = H5T_get_member_type(dtype, u)))
274 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member type retrieval failed")
275
276 if (NULL == (comp_mname[u] = H5T__get_member_name(dtype, u)))
277 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member type retrieval failed")
278
279 if (NULL == (memb_list[u] = H5T__get_native_type(memb_type, direction, &children_st_align,
280 &(memb_offset[u]), &children_size)))
281 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member identifier retrieval failed")
282
283 if (H5T_close_real(memb_type) < 0)
284 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype")
285 } /* end for */
286
287 /* The alignment for whole compound type */
288 if (children_st_align && children_size % children_st_align)
289 children_size += children_st_align - (children_size % children_st_align);
290
291 /* Construct new compound type based on native type */
292 if (NULL == (new_type = H5T__create(H5T_COMPOUND, children_size)))
293 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot create a compound type")
294
295 /* Insert members for the new compound type */
296 for (u = 0; u < nmemb; u++)
297 if (H5T__insert(new_type, comp_mname[u], memb_offset[u], memb_list[u]) < 0)
298 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot insert member to compound datatype")
299
300 /* Update size, offset and compound alignment for parent in the case of
301 * nested compound type. The alignment for a compound type as one field in
302 * a compound type is the biggest compound alignment among all its members.
303 * e.g. in the structure
304 * typedef struct s1 {
305 * char c;
306 * int i;
307 * s2 st;
308 * unsigned long long l;
309 * } s1;
310 * typedef struct s2 {
311 * short c2;
312 * long l2;
313 * long long ll2;
314 * } s2;
315 * The alignment for ST in S1 is the biggest structure alignment of all the
316 * members of S2, which is probably the LL2 of 'long long'. -SLU 2010/4/28
317 */
318 if (H5T__cmp_offset(comp_size, offset, children_size, (size_t)1, children_st_align,
319 struct_align) < 0)
320 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
321
322 /* Close member data type */
323 for (u = 0; u < nmemb; u++) {
324 if (H5T_close_real(memb_list[u]) < 0)
325 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype")
326
327 /* Free member names in list */
328 comp_mname[u] = (char *)H5MM_xfree(comp_mname[u]);
329 } /* end for */
330
331 /* Free lists for members */
332 memb_list = (H5T_t **)H5MM_xfree(memb_list);
333 memb_offset = (size_t *)H5MM_xfree(memb_offset);
334 comp_mname = (char **)H5MM_xfree(comp_mname);
335
336 ret_value = new_type;
337 } /* end case */
338 break;
339
340 case H5T_ENUM: {
341 H5T_path_t *tpath; /* Type conversion info */
342 hid_t super_type_id, nat_super_type_id;
343
344 /* Don't need to do anything special for alignment, offset since the ENUM type usually is integer.
345 */
346
347 /* Retrieve base type for enumerated type */
348 if (NULL == (super_type = H5T_get_super(dtype)))
349 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get base type for enumerate type")
350 if (NULL == (nat_super_type =
351 H5T__get_native_type(super_type, direction, struct_align, offset, comp_size)))
352 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "base native type retrieval failed")
353
354 if ((super_type_id = H5I_register(H5I_DATATYPE, super_type, FALSE)) < 0)
355 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot register datatype")
356 if ((nat_super_type_id = H5I_register(H5I_DATATYPE, nat_super_type, FALSE)) < 0)
357 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot register datatype")
358
359 /* Allocate room for the enum values */
360 if (NULL == (tmp_memb_value = H5MM_calloc(H5T_get_size(super_type))))
361 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
362 if (NULL == (memb_value = H5MM_calloc(H5T_get_size(nat_super_type))))
363 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
364
365 /* Construct new enum type based on native type */
366 if (NULL == (new_type = H5T__enum_create(nat_super_type)))
367 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create enum type")
368
369 /* Find the conversion function */
370 if (NULL == (tpath = H5T_path_find(super_type, nat_super_type)))
371 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL,
372 "unable to convert between src and dst data types")
373
374 /* Retrieve member info and insert members into new enum type */
375 if ((snmemb = H5T_get_nmembers(dtype)) <= 0)
376 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "enumerate data type doesn't have any member")
377 H5_CHECKED_ASSIGN(nmemb, unsigned, snmemb, int);
378 for (u = 0; u < nmemb; u++) {
379 if (NULL == (memb_name = H5T__get_member_name(dtype, u)))
380 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member name")
381 if (H5T__get_member_value(dtype, u, tmp_memb_value) < 0)
382 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member value")
383 H5MM_memcpy(memb_value, tmp_memb_value, H5T_get_size(super_type));
384
385 if (H5T_convert(tpath, super_type_id, nat_super_type_id, (size_t)1, (size_t)0, (size_t)0,
386 memb_value, NULL) < 0)
387 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member value")
388
389 if (H5T__enum_insert(new_type, memb_name, memb_value) < 0)
390 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot insert member")
391 memb_name = (char *)H5MM_xfree(memb_name);
392 }
393 memb_value = H5MM_xfree(memb_value);
394 tmp_memb_value = H5MM_xfree(tmp_memb_value);
395
396 /* Close base type */
397 if (H5I_dec_app_ref(nat_super_type_id) < 0)
398 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype")
399 /* Close super type */
400 if (H5I_dec_app_ref(super_type_id) < 0)
401 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype")
402
403 ret_value = new_type;
404 } /* end case */
405 break;
406
407 case H5T_ARRAY: {
408 int sarray_rank; /* Array's rank */
409 unsigned array_rank; /* Array's rank */
410 hsize_t nelems = 1;
411 size_t super_offset = 0;
412 size_t super_size = 0;
413 size_t super_align = 0;
414
415 /* Retrieve dimension information for array data type */
416 if ((sarray_rank = H5T__get_array_ndims(dtype)) <= 0)
417 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get dimension rank")
418 H5_CHECKED_ASSIGN(array_rank, unsigned, sarray_rank, int);
419 if (NULL == (dims = (hsize_t *)H5MM_malloc(array_rank * sizeof(hsize_t))))
420 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory")
421 if (H5T__get_array_dims(dtype, dims) < 0)
422 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get dimension size")
423
424 /* Retrieve base type for array type */
425 if (NULL == (super_type = H5T_get_super(dtype)))
426 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get parent type for array type")
427 if (NULL == (nat_super_type = H5T__get_native_type(super_type, direction, &super_align,
428 &super_offset, &super_size)))
429 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "parent native type retrieval failed")
430
431 /* Close super type */
432 if (H5T_close_real(super_type) < 0)
433 HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype")
434
435 /* Create a new array type based on native type */
436 if (NULL == (new_type = H5T__array_create(nat_super_type, array_rank, dims)))
437 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create array type")
438
439 /* Close base type */
440 if (H5T_close_real(nat_super_type) < 0)
441 HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype")
442
443 for (u = 0; u < array_rank; u++)
444 nelems *= dims[u];
445 H5_CHECK_OVERFLOW(nelems, hsize_t, size_t);
446 if (H5T__cmp_offset(comp_size, offset, super_size, (size_t)nelems, super_align, struct_align) < 0)
447 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
448
449 dims = (hsize_t *)H5MM_xfree(dims);
450
451 ret_value = new_type;
452 } /* end case */
453 break;
454
455 case H5T_VLEN: {
456 size_t vl_align = 0;
457 size_t vl_size = 0;
458 size_t super_size = 0;
459
460 /* Retrieve base type for array type */
461 if (NULL == (super_type = H5T_get_super(dtype)))
462 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get parent type for VL type")
463 /* Don't need alignment, offset information if this VL isn't a field of compound type. If it
464 * is, go to a few steps below to compute the information directly. */
465 if (NULL ==
466 (nat_super_type = H5T__get_native_type(super_type, direction, NULL, NULL, &super_size)))
467 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "parent native type retrieval failed")
468
469 /* Close super type */
470 if (H5T_close_real(super_type) < 0)
471 HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype")
472
473 /* Create a new array type based on native type */
474 if (NULL == (new_type = H5T__vlen_create(nat_super_type)))
475 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create VL type")
476
477 /* Close base type */
478 if (H5T_close_real(nat_super_type) < 0)
479 HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype")
480
481 /* Update size, offset and compound alignment for parent compound type directly. */
482 vl_align = H5T_HVL_COMP_ALIGN_g;
483 vl_size = sizeof(hvl_t);
484
485 if (H5T__cmp_offset(comp_size, offset, vl_size, (size_t)1, vl_align, struct_align) < 0)
486 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
487
488 ret_value = new_type;
489 } /* end case */
490 break;
491
492 case H5T_NO_CLASS:
493 case H5T_NCLASSES:
494 default:
495 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "data type doesn't match any native type")
496 } /* end switch */
497
498 done:
499 /* Error cleanup */
500 if (NULL == ret_value) {
501 if (new_type)
502 if (H5T_close_real(new_type) < 0)
503 HDONE_ERROR(H5E_DATATYPE, H5E_CLOSEERROR, NULL, "unable to release datatype")
504
505 /* Free lists for members */
506 if (memb_list) {
507 for (u = 0; u < nmemb; u++)
508 if (memb_list[u] && H5T_close_real(memb_list[u]) < 0)
509 HDONE_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype")
510
511 memb_list = (H5T_t **)H5MM_xfree(memb_list);
512 } /* end if */
513 memb_offset = (size_t *)H5MM_xfree(memb_offset);
514 if (comp_mname) {
515 for (u = 0; u < nmemb; u++)
516 if (comp_mname[u])
517 H5MM_xfree(comp_mname[u]);
518 comp_mname = (char **)H5MM_xfree(comp_mname);
519 } /* end if */
520 memb_name = (char *)H5MM_xfree(memb_name);
521 memb_value = H5MM_xfree(memb_value);
522 tmp_memb_value = H5MM_xfree(tmp_memb_value);
523 dims = (hsize_t *)H5MM_xfree(dims);
524 } /* end if */
525
526 FUNC_LEAVE_NOAPI(ret_value)
527 } /* end H5T__get_native_type() */
528
529 /* Disable warning for intentional identical branches here -QAK */
530 /*
531 * This pragma only needs to surround the "duplicated branches" in
532 * the code below, but early (4.4.7, at least) gcc only allows
533 * diagnostic pragmas to be toggled outside of functions.
534 */
535 H5_GCC_DIAG_OFF("duplicated-branches")
536
537 /*-------------------------------------------------------------------------
538 * Function: H5T__get_native_integer
539 *
540 * Purpose: Returns the native integer type of a datatype.
541 *
542 * Return: Success: Returns the native data type if successful.
543 *
544 * Failure: negative
545 *
546 * Programmer: Raymond Lu
547 * Oct 3, 2002
548 *
549 *-------------------------------------------------------------------------
550 */
551 static H5T_t *
H5T__get_native_integer(size_t prec,H5T_sign_t sign,H5T_direction_t direction,size_t * struct_align,size_t * offset,size_t * comp_size)552 H5T__get_native_integer(size_t prec, H5T_sign_t sign, H5T_direction_t direction, size_t *struct_align,
553 size_t *offset, size_t *comp_size)
554 {
555 H5T_t *dt; /* Appropriate native datatype to copy */
556 hid_t tid = (-1); /* Datatype ID of appropriate native datatype */
557 size_t align = 0; /* Alignment necessary for native datatype */
558 size_t native_size = 0; /* Datatype size of the native type */
559 enum match_type { /* The different kinds of integers we can match */
560 H5T_NATIVE_INT_MATCH_CHAR,
561 H5T_NATIVE_INT_MATCH_SHORT,
562 H5T_NATIVE_INT_MATCH_INT,
563 H5T_NATIVE_INT_MATCH_LONG,
564 H5T_NATIVE_INT_MATCH_LLONG,
565 H5T_NATIVE_INT_MATCH_UNKNOWN
566 } match = H5T_NATIVE_INT_MATCH_UNKNOWN;
567 H5T_t *ret_value = NULL; /* Return value */
568
569 FUNC_ENTER_STATIC
570
571 if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
572 if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g))) {
573 match = H5T_NATIVE_INT_MATCH_CHAR;
574 native_size = sizeof(char);
575 }
576 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SHORT_g))) {
577 match = H5T_NATIVE_INT_MATCH_SHORT;
578 native_size = sizeof(short);
579 }
580 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_INT_g))) {
581 match = H5T_NATIVE_INT_MATCH_INT;
582 native_size = sizeof(int);
583 }
584 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LONG_g))) {
585 match = H5T_NATIVE_INT_MATCH_LONG;
586 native_size = sizeof(long);
587 }
588 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LLONG_g))) {
589 match = H5T_NATIVE_INT_MATCH_LLONG;
590 native_size = sizeof(long long);
591 }
592 else { /* If no native type matches the querried datatype, simply choose the type of biggest size. */
593 match = H5T_NATIVE_INT_MATCH_LLONG;
594 native_size = sizeof(long long);
595 }
596 }
597 else if (direction == H5T_DIR_DESCEND) {
598 if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LONG_g))) {
599 match = H5T_NATIVE_INT_MATCH_LLONG;
600 native_size = sizeof(long long);
601 }
602 else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_INT_g))) {
603 match = H5T_NATIVE_INT_MATCH_LONG;
604 native_size = sizeof(long);
605 }
606 else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SHORT_g))) {
607 match = H5T_NATIVE_INT_MATCH_INT;
608 native_size = sizeof(int);
609 }
610 else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g))) {
611 match = H5T_NATIVE_INT_MATCH_SHORT;
612 native_size = sizeof(short);
613 }
614 else {
615 match = H5T_NATIVE_INT_MATCH_CHAR;
616 native_size = sizeof(char);
617 }
618 }
619
620 /* Set the appropriate native datatype information */
621 switch (match) {
622 case H5T_NATIVE_INT_MATCH_CHAR:
623 if (sign == H5T_SGN_2)
624 tid = H5T_NATIVE_SCHAR;
625 else
626 tid = H5T_NATIVE_UCHAR;
627
628 align = H5T_NATIVE_SCHAR_COMP_ALIGN_g;
629 break;
630
631 case H5T_NATIVE_INT_MATCH_SHORT:
632 if (sign == H5T_SGN_2)
633 tid = H5T_NATIVE_SHORT;
634 else
635 tid = H5T_NATIVE_USHORT;
636 align = H5T_NATIVE_SHORT_COMP_ALIGN_g;
637 break;
638
639 case H5T_NATIVE_INT_MATCH_INT:
640 if (sign == H5T_SGN_2)
641 tid = H5T_NATIVE_INT;
642 else
643 tid = H5T_NATIVE_UINT;
644
645 align = H5T_NATIVE_INT_COMP_ALIGN_g;
646 break;
647
648 case H5T_NATIVE_INT_MATCH_LONG:
649 if (sign == H5T_SGN_2)
650 tid = H5T_NATIVE_LONG;
651 else
652 tid = H5T_NATIVE_ULONG;
653
654 align = H5T_NATIVE_LONG_COMP_ALIGN_g;
655 break;
656
657 case H5T_NATIVE_INT_MATCH_LLONG:
658 if (sign == H5T_SGN_2)
659 tid = H5T_NATIVE_LLONG;
660 else
661 tid = H5T_NATIVE_ULLONG;
662
663 align = H5T_NATIVE_LLONG_COMP_ALIGN_g;
664 break;
665
666 case H5T_NATIVE_INT_MATCH_UNKNOWN:
667 default:
668 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "Unknown native integer match")
669 } /* end switch */
670
671 /* Create new native type */
672 HDassert(tid >= 0);
673 if (NULL == (dt = (H5T_t *)H5I_object(tid)))
674 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type")
675
676 if (NULL == (ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)))
677 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy type")
678
679 /* compute size and offset of compound type member. */
680 if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
681 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
682
683 done:
684 FUNC_LEAVE_NOAPI(ret_value)
685 } /* end H5T__get_native_integer() */
686 H5_GCC_DIAG_ON("duplicated-branches")
687
688 /* Disable warning for intentional identical branches here -QAK */
689 /*
690 * This pragma only needs to surround the "duplicated branches" in
691 * the code below, but early (4.4.7, at least) gcc only allows
692 * diagnostic pragmas to be toggled outside of functions.
693 */
694 H5_GCC_DIAG_OFF("duplicated-branches")
695
696 /*-------------------------------------------------------------------------
697 * Function: H5T__get_native_float
698 *
699 * Purpose: Returns the native floatt type of a datatype.
700 *
701 * Return: Success: Returns the native data type if successful.
702 *
703 * Failure: negative
704 *
705 * Programmer: Raymond Lu
706 * Oct 3, 2002
707 *
708 *-------------------------------------------------------------------------
709 */
710 static H5T_t *
H5T__get_native_float(size_t size,H5T_direction_t direction,size_t * struct_align,size_t * offset,size_t * comp_size)711 H5T__get_native_float(size_t size, H5T_direction_t direction, size_t *struct_align, size_t *offset,
712 size_t *comp_size)
713 {
714 H5T_t *dt = NULL; /* Appropriate native datatype to copy */
715 hid_t tid = (-1); /* Datatype ID of appropriate native datatype */
716 size_t align = 0; /* Alignment necessary for native datatype */
717 size_t native_size = 0; /* Datatype size of the native type */
718 enum match_type { /* The different kinds of floating point types we can match */
719 H5T_NATIVE_FLOAT_MATCH_FLOAT,
720 H5T_NATIVE_FLOAT_MATCH_DOUBLE,
721 #if H5_SIZEOF_LONG_DOUBLE != 0
722 H5T_NATIVE_FLOAT_MATCH_LDOUBLE,
723 #endif
724 H5T_NATIVE_FLOAT_MATCH_UNKNOWN
725 } match = H5T_NATIVE_FLOAT_MATCH_UNKNOWN;
726 H5T_t *ret_value = NULL; /* Return value */
727
728 FUNC_ENTER_STATIC
729
730 HDassert(size > 0);
731
732 if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
733 if (size <= sizeof(float)) {
734 match = H5T_NATIVE_FLOAT_MATCH_FLOAT;
735 native_size = sizeof(float);
736 }
737 else if (size <= sizeof(double)) {
738 match = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
739 native_size = sizeof(double);
740 }
741 #if H5_SIZEOF_LONG_DOUBLE != 0
742 else if (size <= sizeof(long double)) {
743 match = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
744 native_size = sizeof(long double);
745 }
746 #endif
747 else { /* If not match, return the biggest datatype */
748 #if H5_SIZEOF_LONG_DOUBLE != 0
749 match = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
750 native_size = sizeof(long double);
751 #else
752 match = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
753 native_size = sizeof(double);
754 #endif
755 }
756 }
757 else {
758 #if H5_SIZEOF_LONG_DOUBLE != 0
759 if (size > sizeof(double)) {
760 match = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
761 native_size = sizeof(long double);
762 }
763 else if (size > sizeof(float)) {
764 match = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
765 native_size = sizeof(double);
766 }
767 else {
768 match = H5T_NATIVE_FLOAT_MATCH_FLOAT;
769 native_size = sizeof(float);
770 }
771 #else
772 if (size > sizeof(float)) {
773 match = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
774 native_size = sizeof(double);
775 }
776 else {
777 match = H5T_NATIVE_FLOAT_MATCH_FLOAT;
778 native_size = sizeof(float);
779 }
780 #endif
781 }
782
783 /* Set the appropriate native floating point information */
784 switch (match) {
785 case H5T_NATIVE_FLOAT_MATCH_FLOAT:
786 tid = H5T_NATIVE_FLOAT;
787 align = H5T_NATIVE_FLOAT_COMP_ALIGN_g;
788 break;
789
790 case H5T_NATIVE_FLOAT_MATCH_DOUBLE:
791 tid = H5T_NATIVE_DOUBLE;
792 align = H5T_NATIVE_DOUBLE_COMP_ALIGN_g;
793 break;
794
795 #if H5_SIZEOF_LONG_DOUBLE != 0
796 case H5T_NATIVE_FLOAT_MATCH_LDOUBLE:
797 tid = H5T_NATIVE_LDOUBLE;
798 align = H5T_NATIVE_LDOUBLE_COMP_ALIGN_g;
799 break;
800 #endif
801 case H5T_NATIVE_FLOAT_MATCH_UNKNOWN:
802 default:
803 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "Unknown native floating-point match")
804 } /* end switch */
805
806 /* Create new native type */
807 HDassert(tid >= 0);
808 if (NULL == (dt = (H5T_t *)H5I_object(tid)))
809 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type")
810 if ((ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)) == NULL)
811 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type")
812
813 /* compute offset of compound type member. */
814 if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
815 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
816
817 done:
818 FUNC_LEAVE_NOAPI(ret_value)
819 } /* end H5T__get_native_float() */
820 H5_GCC_DIAG_ON("duplicated-branches")
821
822 /* Disable warning for intentional identical branches here -QAK */
823 /*
824 * This pragma only needs to surround the "duplicated branches" in
825 * the code below, but early (4.4.7, at least) gcc only allows
826 * diagnostic pragmas to be toggled outside of functions.
827 */
828 H5_GCC_DIAG_OFF("duplicated-branches")
829
830 /*-------------------------------------------------------------------------
831 * Function: H5T__get_native_bitfield
832 *
833 * Purpose: Returns the native bitfield type of a datatype. Bitfield
834 * is similar to unsigned integer.
835 *
836 * Return: Success: Returns the native data type if successful.
837 *
838 * Failure: negative
839 *
840 * Programmer: Raymond Lu
841 * 1 December 2009
842 *
843 *-------------------------------------------------------------------------
844 */
845 static H5T_t *
H5T__get_native_bitfield(size_t prec,H5T_direction_t direction,size_t * struct_align,size_t * offset,size_t * comp_size)846 H5T__get_native_bitfield(size_t prec, H5T_direction_t direction, size_t *struct_align, size_t *offset,
847 size_t *comp_size)
848 {
849 H5T_t *dt; /* Appropriate native datatype to copy */
850 hid_t tid = (-1); /* Datatype ID of appropriate native datatype */
851 size_t align = 0; /* Alignment necessary for native datatype */
852 size_t native_size = 0; /* Datatype size of the native type */
853 H5T_t *ret_value = NULL; /* Return value */
854
855 FUNC_ENTER_STATIC
856
857 if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
858 if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B8_g))) {
859 tid = H5T_NATIVE_B8;
860 native_size = 1;
861 align = H5T_NATIVE_UINT8_ALIGN_g;
862 }
863 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B16_g))) {
864 tid = H5T_NATIVE_B16;
865 native_size = 2;
866 align = H5T_NATIVE_UINT16_ALIGN_g;
867 }
868 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B32_g))) {
869 tid = H5T_NATIVE_B32;
870 native_size = 4;
871 align = H5T_NATIVE_UINT32_ALIGN_g;
872 }
873 else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B64_g))) {
874 tid = H5T_NATIVE_B64;
875 native_size = 8;
876 align = H5T_NATIVE_UINT64_ALIGN_g;
877 }
878 else { /* If no native type matches the querried datatype, simply choose the type of biggest size. */
879 tid = H5T_NATIVE_B64;
880 native_size = 8;
881 align = H5T_NATIVE_UINT64_ALIGN_g;
882 }
883 }
884 else if (direction == H5T_DIR_DESCEND) {
885 if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B32_g))) {
886 tid = H5T_NATIVE_B64;
887 native_size = 8;
888 align = H5T_NATIVE_UINT64_ALIGN_g;
889 }
890 else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B16_g))) {
891 tid = H5T_NATIVE_B32;
892 native_size = 4;
893 align = H5T_NATIVE_UINT32_ALIGN_g;
894 }
895 else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B8_g))) {
896 tid = H5T_NATIVE_B16;
897 native_size = 2;
898 align = H5T_NATIVE_UINT16_ALIGN_g;
899 }
900 else {
901 tid = H5T_NATIVE_B8;
902 native_size = 1;
903 align = H5T_NATIVE_UINT8_ALIGN_g;
904 }
905 }
906
907 /* Create new native type */
908 HDassert(tid >= 0);
909 if (NULL == (dt = (H5T_t *)H5I_object(tid)))
910 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type")
911
912 if ((ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)) == NULL)
913 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy type")
914
915 /* compute size and offset of compound type member. */
916 if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
917 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset")
918
919 done:
920 FUNC_LEAVE_NOAPI(ret_value)
921 } /* end H5T__get_native_bitfield() */
922 H5_GCC_DIAG_ON("duplicated-branches")
923
924 /*-------------------------------------------------------------------------
925 * Function: H5T__cmp_offset
926 *
927 * Purpose: This function is only for convenience. It computes the
928 * compound type size, offset of the member being considered
929 * and the alignment for the whole compound type.
930 *
931 * Return: Success: Non-negative value.
932 *
933 * Failure: Negative value.
934 *
935 * Programmer: Raymond Lu
936 * December 10, 2002
937 *
938 *-------------------------------------------------------------------------
939 */
940 static herr_t
H5T__cmp_offset(size_t * comp_size,size_t * offset,size_t elem_size,size_t nelems,size_t align,size_t * struct_align)941 H5T__cmp_offset(size_t *comp_size, size_t *offset, size_t elem_size, size_t nelems, size_t align,
942 size_t *struct_align)
943 {
944 FUNC_ENTER_STATIC_NOERR
945
946 if (offset && comp_size) {
947 if (align > 1 && *comp_size % align) {
948 /* Add alignment value */
949 *offset = *comp_size + (align - *comp_size % align);
950 *comp_size += (align - *comp_size % align);
951 } /* end if */
952 else
953 *offset = *comp_size;
954
955 /* compute size of compound type member. */
956 *comp_size += nelems * elem_size;
957 } /* end if */
958
959 if (struct_align && *struct_align < align)
960 *struct_align = align;
961
962 FUNC_LEAVE_NOAPI(SUCCEED)
963 } /* end H5T__cmp_offset() */
964