Coverage Report

Created: 2024-06-18 06:29

/src/hdf5/src/H5Tnative.c
Line
Count
Source (jump to first uncovered line)
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the COPYING file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*
14
 * Module Info: This module contains the functionality for querying
15
 *      a "native" datatype for the H5T interface.
16
 */
17
18
#include "H5Tmodule.h" /* This source code file is part of the H5T module */
19
20
#include "H5private.h"   /* Generic Functions            */
21
#include "H5Eprivate.h"  /* Error handling              */
22
#include "H5Iprivate.h"  /* IDs                      */
23
#include "H5MMprivate.h" /* Memory management            */
24
#include "H5Tpkg.h"      /* Datatypes                */
25
26
/* Static local functions */
27
static H5T_t *H5T__get_native_type(H5T_t *dt, H5T_direction_t direction, size_t *struct_align, size_t *offset,
28
                                   size_t *comp_size);
29
static H5T_t *H5T__get_native_integer(size_t prec, H5T_sign_t sign, H5T_direction_t direction,
30
                                      size_t *struct_align, size_t *offset, size_t *comp_size);
31
static H5T_t *H5T__get_native_float(size_t size, H5T_direction_t direction, size_t *struct_align,
32
                                    size_t *offset, size_t *comp_size);
33
static H5T_t *H5T__get_native_bitfield(size_t prec, H5T_direction_t direction, size_t *struct_align,
34
                                       size_t *offset, size_t *comp_size);
35
static herr_t H5T__cmp_offset(size_t *comp_size, size_t *offset, size_t elem_size, size_t nelems,
36
                              size_t align, size_t *struct_align);
37
38
/*-------------------------------------------------------------------------
39
 * Function:    H5Tget_native_type
40
 *
41
 * Purpose:     High-level API to return the native type of a datatype.
42
 *              The native type is chosen by matching the size and class of
43
 *              queried datatype from the following native primitive
44
 *              datatypes:
45
 *                      H5T_NATIVE_CHAR         H5T_NATIVE_UCHAR
46
 *                      H5T_NATIVE_SHORT        H5T_NATIVE_USHORT
47
 *                      H5T_NATIVE_INT          H5T_NATIVE_UINT
48
 *                      H5T_NATIVE_LONG         H5T_NATIVE_ULONG
49
 *                      H5T_NATIVE_LLONG        H5T_NATIVE_ULLONG
50
 *
51
 *                      H5T_NATIVE_FLOAT16 (if available)
52
 *                      H5T_NATIVE_FLOAT
53
 *                      H5T_NATIVE_DOUBLE
54
 *                      H5T_NATIVE_LDOUBLE
55
 *
56
 *              Compound, array, enum, and VL types all choose among these
57
 *              types for their members.  Time, Bitfield, Opaque, Reference
58
 *              types are only copy out.
59
 *
60
 * Return:      Success:        Returns the native data type if successful.
61
 *
62
 *              Failure:        negative
63
 *
64
 *-------------------------------------------------------------------------
65
 */
66
hid_t
67
H5Tget_native_type(hid_t type_id, H5T_direction_t direction)
68
0
{
69
0
    H5T_t *dt;               /* Datatype to create native datatype from */
70
0
    H5T_t *new_dt    = NULL; /* Datatype for native datatype created */
71
0
    size_t comp_size = 0;    /* Compound datatype's size */
72
0
    hid_t  ret_value;        /* Return value */
73
74
0
    FUNC_ENTER_API(H5I_INVALID_HID)
75
76
    /* Check arguments */
77
0
    if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
78
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not a data type");
79
0
    if (direction != H5T_DIR_DEFAULT && direction != H5T_DIR_ASCEND && direction != H5T_DIR_DESCEND)
80
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not valid direction value");
81
82
    /* Get the native type */
83
0
    if (NULL == (new_dt = H5T__get_native_type(dt, direction, NULL, NULL, &comp_size)))
84
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "cannot retrieve native type");
85
86
    /* Get an ID for the new type */
87
0
    if ((ret_value = H5I_register(H5I_DATATYPE, new_dt, true)) < 0)
88
0
        HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register data type");
89
90
0
done:
91
    /* Error cleanup */
92
0
    if (ret_value < 0)
93
0
        if (new_dt && H5T_close_real(new_dt) < 0)
94
0
            HDONE_ERROR(H5E_DATATYPE, H5E_CLOSEERROR, H5I_INVALID_HID, "unable to release datatype");
95
96
0
    FUNC_LEAVE_API(ret_value)
97
0
} /* end H5Tget_native_type() */
98
99
/*-------------------------------------------------------------------------
100
 * Function:    H5T__get_native_type
101
 *
102
 * Purpose:     Returns the native type of a datatype.
103
 *
104
 * Return:      Success:        Returns the native data type if successful.
105
 *
106
 *              Failure:        negative
107
 *
108
 *-------------------------------------------------------------------------
109
 */
110
static H5T_t *
111
H5T__get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *struct_align, size_t *offset,
112
                     size_t *comp_size)
113
0
{
114
0
    H5T_t  *super_type;       /* Super type of VL, array and enum datatypes */
115
0
    H5T_t  *nat_super_type;   /* Native form of VL, array & enum super datatype */
116
0
    H5T_t  *new_type  = NULL; /* New native datatype */
117
0
    H5T_t  *memb_type = NULL; /* Datatype of member */
118
0
    H5T_t **memb_list = NULL; /* List of compound member IDs */
119
0
    size_t *memb_offset =
120
0
        NULL; /* List of member offsets in compound type, including member size and alignment */
121
0
    char      **comp_mname     = NULL; /* List of member names in compound type */
122
0
    char       *memb_name      = NULL; /* Enum's member name */
123
0
    void       *memb_value     = NULL; /* Enum's member value */
124
0
    void       *tmp_memb_value = NULL; /* Enum's member value */
125
0
    hsize_t    *dims           = NULL; /* Dimension sizes for array */
126
0
    H5T_class_t h5_class;              /* Class of datatype to make native */
127
0
    size_t      size;                  /* Size of datatype to make native */
128
0
    size_t      prec;                  /* Precision of datatype to make native */
129
0
    int         snmemb;                /* Number of members in compound & enum types */
130
0
    unsigned    nmemb = 0;             /* Number of members in compound & enum types */
131
0
    unsigned    u;                     /* Local index variable */
132
0
    H5T_t      *ret_value = NULL;      /* Return value */
133
134
0
    FUNC_ENTER_PACKAGE
135
136
0
    assert(dtype);
137
138
0
    if (H5T_NO_CLASS == (h5_class = H5T_get_class(dtype, false)))
139
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid class");
140
141
0
    if (0 == (size = H5T_get_size(dtype)))
142
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid size");
143
144
0
    switch (h5_class) {
145
0
        case H5T_INTEGER: {
146
0
            H5T_sign_t sign; /* Signedness of integer type */
147
148
0
            if (H5T_SGN_ERROR == (sign = H5T_get_sign(dtype)))
149
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid signess");
150
151
0
            prec = dtype->shared->u.atomic.prec;
152
153
0
            if (NULL ==
154
0
                (ret_value = H5T__get_native_integer(prec, sign, direction, struct_align, offset, comp_size)))
155
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer type");
156
0
        } /* end case */
157
0
        break;
158
159
0
        case H5T_FLOAT:
160
0
            if (NULL == (ret_value = H5T__get_native_float(size, direction, struct_align, offset, comp_size)))
161
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type");
162
163
0
            break;
164
165
0
        case H5T_STRING:
166
0
            if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
167
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type");
168
169
0
            if (H5T_IS_VL_STRING(dtype->shared)) {
170
                /* Update size, offset and compound alignment for parent. */
171
0
                if (H5T__cmp_offset(comp_size, offset, sizeof(char *), (size_t)1, H5T_POINTER_ALIGN_g,
172
0
                                    struct_align) < 0)
173
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
174
0
            } /* end if */
175
0
            else {
176
                /* Update size, offset and compound alignment for parent. */
177
0
                if (H5T__cmp_offset(comp_size, offset, sizeof(char), size, H5T_NATIVE_SCHAR_ALIGN_g,
178
0
                                    struct_align) < 0)
179
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
180
0
            } /* end else */
181
0
            break;
182
183
        /* The time type will be supported in the future.  Simply return "not supported"
184
         * message for now.*/
185
0
        case H5T_TIME:
186
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "time type is not supported yet");
187
188
0
        case H5T_BITFIELD: {
189
0
            prec = dtype->shared->u.atomic.prec;
190
191
0
            if (NULL ==
192
0
                (ret_value = H5T__get_native_bitfield(prec, direction, struct_align, offset, comp_size)))
193
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer for bitfield type");
194
0
        } /* end case */
195
0
        break;
196
197
0
        case H5T_OPAQUE:
198
0
            if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
199
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type");
200
201
            /* Update size, offset and compound alignment for parent. */
202
0
            if (H5T__cmp_offset(comp_size, offset, sizeof(char), size, H5T_NATIVE_SCHAR_ALIGN_g,
203
0
                                struct_align) < 0)
204
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
205
0
            break;
206
207
0
        case H5T_REFERENCE: {
208
0
            H5T_t *dt; /* Datatype to make native */
209
0
            size_t align;
210
0
            size_t ref_size;
211
212
0
            if (NULL == (ret_value = H5T_copy(dtype, H5T_COPY_TRANSIENT)))
213
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy reference type");
214
215
            /* Decide if the data type is object reference. */
216
0
            if (NULL == (dt = (H5T_t *)H5I_object(H5T_STD_REF_OBJ_g)))
217
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type");
218
219
            /* Update size, offset and compound alignment for parent. */
220
0
            if (0 == H5T_cmp(ret_value, dt, false)) {
221
0
                align    = H5T_HOBJREF_ALIGN_g;
222
0
                ref_size = sizeof(hobj_ref_t);
223
0
            } /* end if */
224
0
            else {
225
                /* Decide if the data type is dataset region reference. */
226
0
                if (NULL == (dt = (H5T_t *)H5I_object(H5T_STD_REF_DSETREG_g)))
227
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type");
228
229
0
                if (0 == H5T_cmp(ret_value, dt, false)) {
230
0
                    align    = H5T_HDSETREGREF_ALIGN_g;
231
0
                    ref_size = sizeof(hdset_reg_ref_t);
232
0
                } /* end if */
233
0
                else {
234
                    /* Only pointers to underlying opaque reference types */
235
0
                    align    = H5T_REF_ALIGN_g;
236
0
                    ref_size = sizeof(H5R_ref_t);
237
0
                } /* end else */
238
0
            }     /* end else */
239
240
0
            if (H5T__cmp_offset(comp_size, offset, ref_size, (size_t)1, align, struct_align) < 0)
241
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
242
0
        } /* end case */
243
0
        break;
244
245
0
        case H5T_COMPOUND: {
246
0
            size_t children_size = 0; /* Total size of compound members */
247
0
            size_t children_st_align =
248
0
                0; /* The max alignment among compound members.  This'll be the compound alignment */
249
250
0
            if ((snmemb = H5T_get_nmembers(dtype)) <= 0)
251
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "compound data type doesn't have any member");
252
0
            H5_CHECKED_ASSIGN(nmemb, unsigned, snmemb, int);
253
254
0
            if (NULL == (memb_list = (H5T_t **)H5MM_calloc(nmemb * sizeof(H5T_t *))))
255
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
256
0
            if (NULL == (memb_offset = (size_t *)H5MM_calloc(nmemb * sizeof(size_t))))
257
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
258
0
            if (NULL == (comp_mname = (char **)H5MM_calloc(nmemb * sizeof(char *))))
259
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
260
261
            /* Construct child compound type and retrieve a list of their IDs, offsets, total size, and
262
             * alignment for compound type. */
263
0
            for (u = 0; u < nmemb; u++) {
264
0
                if (NULL == (memb_type = H5T_get_member_type(dtype, u)))
265
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member type retrieval failed");
266
267
0
                if (NULL == (comp_mname[u] = H5T__get_member_name(dtype, u)))
268
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member type retrieval failed");
269
270
0
                if (NULL == (memb_list[u] = H5T__get_native_type(memb_type, direction, &children_st_align,
271
0
                                                                 &(memb_offset[u]), &children_size)))
272
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "member identifier retrieval failed");
273
274
0
                if (H5T_close_real(memb_type) < 0)
275
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype");
276
0
            } /* end for */
277
278
            /* The alignment for whole compound type */
279
0
            if (children_st_align && children_size % children_st_align)
280
0
                children_size += children_st_align - (children_size % children_st_align);
281
282
            /* Construct new compound type based on native type */
283
0
            if (NULL == (new_type = H5T__create(H5T_COMPOUND, children_size)))
284
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot create a compound type");
285
286
            /* Insert members for the new compound type */
287
0
            for (u = 0; u < nmemb; u++)
288
0
                if (H5T__insert(new_type, comp_mname[u], memb_offset[u], memb_list[u]) < 0)
289
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot insert member to compound datatype");
290
291
            /* Update size, offset and compound alignment for parent in the case of
292
             * nested compound type.  The alignment for a compound type as one field in
293
             * a compound type is the biggest compound alignment among all its members.
294
             * e.g. in the structure
295
             *    typedef struct s1 {
296
             *        char            c;
297
             *        int             i;
298
             *        s2              st;
299
             *        unsigned long long       l;
300
             *    } s1;
301
             *    typedef struct s2 {
302
             *        short           c2;
303
             *        long            l2;
304
             *        long long       ll2;
305
             *    } s2;
306
             * The alignment for ST in S1 is the biggest structure alignment of all the
307
             * members of S2, which is probably the LL2 of 'long long'. -SLU 2010/4/28
308
             */
309
0
            if (H5T__cmp_offset(comp_size, offset, children_size, (size_t)1, children_st_align,
310
0
                                struct_align) < 0)
311
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
312
313
            /* Close member data type */
314
0
            for (u = 0; u < nmemb; u++) {
315
0
                if (H5T_close_real(memb_list[u]) < 0)
316
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype");
317
318
                /* Free member names in list */
319
0
                comp_mname[u] = (char *)H5MM_xfree(comp_mname[u]);
320
0
            } /* end for */
321
322
            /* Free lists for members */
323
0
            memb_list   = (H5T_t **)H5MM_xfree(memb_list);
324
0
            memb_offset = (size_t *)H5MM_xfree(memb_offset);
325
0
            comp_mname  = (char **)H5MM_xfree(comp_mname);
326
327
0
            ret_value = new_type;
328
0
        } /* end case */
329
0
        break;
330
331
0
        case H5T_ENUM: {
332
0
            H5T_path_t *tpath; /* Type conversion info    */
333
334
            /* Don't need to do anything special for alignment, offset since the ENUM type usually is integer.
335
             */
336
337
            /* Retrieve base type for enumerated type */
338
0
            if (NULL == (super_type = H5T_get_super(dtype)))
339
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get base type for enumerate type");
340
0
            if (NULL == (nat_super_type =
341
0
                             H5T__get_native_type(super_type, direction, struct_align, offset, comp_size)))
342
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "base native type retrieval failed");
343
344
            /* Allocate room for the enum values */
345
0
            if (NULL == (tmp_memb_value = H5MM_calloc(H5T_get_size(super_type))))
346
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
347
0
            if (NULL == (memb_value = H5MM_calloc(H5T_get_size(nat_super_type))))
348
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
349
350
            /* Construct new enum type based on native type */
351
0
            if (NULL == (new_type = H5T__enum_create(nat_super_type)))
352
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create enum type");
353
354
            /* Find the conversion function */
355
0
            if (NULL == (tpath = H5T_path_find(super_type, nat_super_type)))
356
0
                HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL,
357
0
                            "unable to convert between src and dst data types");
358
359
            /* Retrieve member info and insert members into new enum type */
360
0
            if ((snmemb = H5T_get_nmembers(dtype)) <= 0)
361
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "enumerate data type doesn't have any member");
362
0
            H5_CHECKED_ASSIGN(nmemb, unsigned, snmemb, int);
363
0
            for (u = 0; u < nmemb; u++) {
364
0
                if (NULL == (memb_name = H5T__get_member_name(dtype, u)))
365
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member name");
366
0
                if (H5T__get_member_value(dtype, u, tmp_memb_value) < 0)
367
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member value");
368
0
                H5MM_memcpy(memb_value, tmp_memb_value, H5T_get_size(super_type));
369
370
0
                if (H5T_convert(tpath, super_type, nat_super_type, (size_t)1, (size_t)0, (size_t)0,
371
0
                                memb_value, NULL) < 0)
372
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get member value");
373
374
0
                if (H5T__enum_insert(new_type, memb_name, memb_value) < 0)
375
0
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot insert member");
376
0
                memb_name = (char *)H5MM_xfree(memb_name);
377
0
            }
378
0
            memb_value     = H5MM_xfree(memb_value);
379
0
            tmp_memb_value = H5MM_xfree(tmp_memb_value);
380
381
0
            if (H5T_close(nat_super_type) < 0)
382
0
                HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, NULL, "can't close datatype");
383
0
            if (H5T_close(super_type) < 0)
384
0
                HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, NULL, "can't close datatype");
385
386
0
            ret_value = new_type;
387
0
        } /* end case */
388
0
        break;
389
390
0
        case H5T_ARRAY: {
391
0
            int      sarray_rank; /* Array's rank */
392
0
            unsigned array_rank;  /* Array's rank */
393
0
            hsize_t  nelems       = 1;
394
0
            size_t   super_offset = 0;
395
0
            size_t   super_size   = 0;
396
0
            size_t   super_align  = 0;
397
398
            /* Retrieve dimension information for array data type */
399
0
            if ((sarray_rank = H5T__get_array_ndims(dtype)) <= 0)
400
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get dimension rank");
401
0
            H5_CHECKED_ASSIGN(array_rank, unsigned, sarray_rank, int);
402
0
            if (NULL == (dims = (hsize_t *)H5MM_malloc(array_rank * sizeof(hsize_t))))
403
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot allocate memory");
404
0
            if (H5T__get_array_dims(dtype, dims) < 0)
405
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot get dimension size");
406
407
            /* Retrieve base type for array type */
408
0
            if (NULL == (super_type = H5T_get_super(dtype)))
409
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get parent type for array type");
410
0
            if (NULL == (nat_super_type = H5T__get_native_type(super_type, direction, &super_align,
411
0
                                                               &super_offset, &super_size)))
412
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "parent native type retrieval failed");
413
414
            /* Close super type */
415
0
            if (H5T_close_real(super_type) < 0)
416
0
                HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype");
417
418
            /* Create a new array type based on native type */
419
0
            if (NULL == (new_type = H5T__array_create(nat_super_type, array_rank, dims)))
420
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create array type");
421
422
            /* Close base type */
423
0
            if (H5T_close_real(nat_super_type) < 0)
424
0
                HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype");
425
426
0
            for (u = 0; u < array_rank; u++)
427
0
                nelems *= dims[u];
428
0
            H5_CHECK_OVERFLOW(nelems, hsize_t, size_t);
429
0
            if (H5T__cmp_offset(comp_size, offset, super_size, (size_t)nelems, super_align, struct_align) < 0)
430
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
431
432
0
            dims = (hsize_t *)H5MM_xfree(dims);
433
434
0
            ret_value = new_type;
435
0
        } /* end case */
436
0
        break;
437
438
0
        case H5T_VLEN: {
439
0
            size_t vl_align   = 0;
440
0
            size_t vl_size    = 0;
441
0
            size_t super_size = 0;
442
443
            /* Retrieve base type for array type */
444
0
            if (NULL == (super_type = H5T_get_super(dtype)))
445
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get parent type for VL type");
446
            /* Don't need alignment, offset information if this VL isn't a field of compound type.  If it
447
             * is, go to a few steps below to compute the information directly. */
448
0
            if (NULL ==
449
0
                (nat_super_type = H5T__get_native_type(super_type, direction, NULL, NULL, &super_size)))
450
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "parent native type retrieval failed");
451
452
            /* Close super type */
453
0
            if (H5T_close_real(super_type) < 0)
454
0
                HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype");
455
456
            /* Create a new array type based on native type */
457
0
            if (NULL == (new_type = H5T__vlen_create(nat_super_type)))
458
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to create VL type");
459
460
            /* Close base type */
461
0
            if (H5T_close_real(nat_super_type) < 0)
462
0
                HGOTO_ERROR(H5E_ARGS, H5E_CLOSEERROR, NULL, "cannot close datatype");
463
464
            /* Update size, offset and compound alignment for parent compound type directly. */
465
0
            vl_align = H5T_HVL_ALIGN_g;
466
0
            vl_size  = sizeof(hvl_t);
467
468
0
            if (H5T__cmp_offset(comp_size, offset, vl_size, (size_t)1, vl_align, struct_align) < 0)
469
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
470
471
0
            ret_value = new_type;
472
0
        } /* end case */
473
0
        break;
474
475
0
        case H5T_NO_CLASS:
476
0
        case H5T_NCLASSES:
477
0
        default:
478
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "data type doesn't match any native type");
479
0
    } /* end switch */
480
481
0
done:
482
    /* Error cleanup */
483
0
    if (NULL == ret_value) {
484
0
        if (new_type)
485
0
            if (H5T_close_real(new_type) < 0)
486
0
                HDONE_ERROR(H5E_DATATYPE, H5E_CLOSEERROR, NULL, "unable to release datatype");
487
488
        /* Free lists for members */
489
0
        if (memb_list) {
490
0
            for (u = 0; u < nmemb; u++)
491
0
                if (memb_list[u] && H5T_close_real(memb_list[u]) < 0)
492
0
                    HDONE_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype");
493
494
0
            memb_list = (H5T_t **)H5MM_xfree(memb_list);
495
0
        } /* end if */
496
0
        memb_offset = (size_t *)H5MM_xfree(memb_offset);
497
0
        if (comp_mname) {
498
0
            for (u = 0; u < nmemb; u++)
499
0
                if (comp_mname[u])
500
0
                    H5MM_xfree(comp_mname[u]);
501
0
            comp_mname = (char **)H5MM_xfree(comp_mname);
502
0
        } /* end if */
503
0
        memb_name      = (char *)H5MM_xfree(memb_name);
504
0
        memb_value     = H5MM_xfree(memb_value);
505
0
        tmp_memb_value = H5MM_xfree(tmp_memb_value);
506
0
        dims           = (hsize_t *)H5MM_xfree(dims);
507
0
    } /* end if */
508
509
0
    FUNC_LEAVE_NOAPI(ret_value)
510
0
} /* end H5T__get_native_type() */
511
512
/* Disable warning for intentional identical branches here -QAK */
513
/*
514
 *       This pragma only needs to surround the "duplicated branches" in
515
 *       the code below, but early (4.4.7, at least) gcc only allows
516
 *       diagnostic pragmas to be toggled outside of functions.
517
 */
518
H5_GCC_DIAG_OFF("duplicated-branches")
519
520
/*-------------------------------------------------------------------------
521
 * Function:    H5T__get_native_integer
522
 *
523
 * Purpose:     Returns the native integer type of a datatype.
524
 *
525
 * Return:      Success:        Returns the native data type if successful.
526
 *
527
 *              Failure:        negative
528
 *
529
 *-------------------------------------------------------------------------
530
 */
531
static H5T_t *
532
H5T__get_native_integer(size_t prec, H5T_sign_t sign, H5T_direction_t direction, size_t *struct_align,
533
                        size_t *offset, size_t *comp_size)
534
0
{
535
0
    H5T_t *dt;                 /* Appropriate native datatype to copy */
536
0
    hid_t  tid         = (-1); /* Datatype ID of appropriate native datatype */
537
0
    size_t align       = 0;    /* Alignment necessary for native datatype */
538
0
    size_t native_size = 0;    /* Datatype size of the native type */
539
0
    enum match_type {          /* The different kinds of integers we can match */
540
0
                      H5T_NATIVE_INT_MATCH_CHAR,
541
0
                      H5T_NATIVE_INT_MATCH_SHORT,
542
0
                      H5T_NATIVE_INT_MATCH_INT,
543
0
                      H5T_NATIVE_INT_MATCH_LONG,
544
0
                      H5T_NATIVE_INT_MATCH_LLONG,
545
0
                      H5T_NATIVE_INT_MATCH_UNKNOWN
546
0
    } match          = H5T_NATIVE_INT_MATCH_UNKNOWN;
547
0
    H5T_t *ret_value = NULL; /* Return value */
548
549
0
    FUNC_ENTER_PACKAGE
550
551
0
    if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
552
0
        if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g))) {
553
0
            match       = H5T_NATIVE_INT_MATCH_CHAR;
554
0
            native_size = sizeof(char);
555
0
        }
556
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SHORT_g))) {
557
0
            match       = H5T_NATIVE_INT_MATCH_SHORT;
558
0
            native_size = sizeof(short);
559
0
        }
560
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_INT_g))) {
561
0
            match       = H5T_NATIVE_INT_MATCH_INT;
562
0
            native_size = sizeof(int);
563
0
        }
564
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LONG_g))) {
565
0
            match       = H5T_NATIVE_INT_MATCH_LONG;
566
0
            native_size = sizeof(long);
567
0
        }
568
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LLONG_g))) {
569
0
            match       = H5T_NATIVE_INT_MATCH_LLONG;
570
0
            native_size = sizeof(long long);
571
0
        }
572
0
        else { /* If no native type matches the queried datatype, simply choose the type of biggest size. */
573
0
            match       = H5T_NATIVE_INT_MATCH_LLONG;
574
0
            native_size = sizeof(long long);
575
0
        }
576
0
    }
577
0
    else if (direction == H5T_DIR_DESCEND) {
578
0
        if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_LONG_g))) {
579
0
            match       = H5T_NATIVE_INT_MATCH_LLONG;
580
0
            native_size = sizeof(long long);
581
0
        }
582
0
        else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_INT_g))) {
583
0
            match       = H5T_NATIVE_INT_MATCH_LONG;
584
0
            native_size = sizeof(long);
585
0
        }
586
0
        else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SHORT_g))) {
587
0
            match       = H5T_NATIVE_INT_MATCH_INT;
588
0
            native_size = sizeof(int);
589
0
        }
590
0
        else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g))) {
591
0
            match       = H5T_NATIVE_INT_MATCH_SHORT;
592
0
            native_size = sizeof(short);
593
0
        }
594
0
        else {
595
0
            match       = H5T_NATIVE_INT_MATCH_CHAR;
596
0
            native_size = sizeof(char);
597
0
        }
598
0
    }
599
600
    /* Set the appropriate native datatype information */
601
0
    switch (match) {
602
0
        case H5T_NATIVE_INT_MATCH_CHAR:
603
0
            if (sign == H5T_SGN_2)
604
0
                tid = H5T_NATIVE_SCHAR;
605
0
            else
606
0
                tid = H5T_NATIVE_UCHAR;
607
608
0
            align = H5T_NATIVE_SCHAR_ALIGN_g;
609
0
            break;
610
611
0
        case H5T_NATIVE_INT_MATCH_SHORT:
612
0
            if (sign == H5T_SGN_2)
613
0
                tid = H5T_NATIVE_SHORT;
614
0
            else
615
0
                tid = H5T_NATIVE_USHORT;
616
0
            align = H5T_NATIVE_SHORT_ALIGN_g;
617
0
            break;
618
619
0
        case H5T_NATIVE_INT_MATCH_INT:
620
0
            if (sign == H5T_SGN_2)
621
0
                tid = H5T_NATIVE_INT;
622
0
            else
623
0
                tid = H5T_NATIVE_UINT;
624
625
0
            align = H5T_NATIVE_INT_ALIGN_g;
626
0
            break;
627
628
0
        case H5T_NATIVE_INT_MATCH_LONG:
629
0
            if (sign == H5T_SGN_2)
630
0
                tid = H5T_NATIVE_LONG;
631
0
            else
632
0
                tid = H5T_NATIVE_ULONG;
633
634
0
            align = H5T_NATIVE_LONG_ALIGN_g;
635
0
            break;
636
637
0
        case H5T_NATIVE_INT_MATCH_LLONG:
638
0
            if (sign == H5T_SGN_2)
639
0
                tid = H5T_NATIVE_LLONG;
640
0
            else
641
0
                tid = H5T_NATIVE_ULLONG;
642
643
0
            align = H5T_NATIVE_LLONG_ALIGN_g;
644
0
            break;
645
646
0
        case H5T_NATIVE_INT_MATCH_UNKNOWN:
647
0
        default:
648
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "Unknown native integer match");
649
0
    } /* end switch */
650
651
    /* Create new native type */
652
0
    assert(tid >= 0);
653
0
    if (NULL == (dt = (H5T_t *)H5I_object(tid)))
654
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type");
655
656
0
    if (NULL == (ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)))
657
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy type");
658
659
    /* compute size and offset of compound type member. */
660
0
    if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
661
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
662
663
0
done:
664
0
    FUNC_LEAVE_NOAPI(ret_value)
665
0
} /* end H5T__get_native_integer() */
666
H5_GCC_DIAG_ON("duplicated-branches")
667
668
/* Disable warning for intentional identical branches here -QAK */
669
/*
670
 *       This pragma only needs to surround the "duplicated branches" in
671
 *       the code below, but early (4.4.7, at least) gcc only allows
672
 *       diagnostic pragmas to be toggled outside of functions.
673
 */
674
H5_GCC_DIAG_OFF("duplicated-branches")
675
676
/*-------------------------------------------------------------------------
677
 * Function:    H5T__get_native_float
678
 *
679
 * Purpose:     Returns the native float type of a datatype.
680
 *
681
 * Return:      Success:        Returns the native data type if successful.
682
 *
683
 *              Failure:        negative
684
 *
685
 *-------------------------------------------------------------------------
686
 */
687
static H5T_t *
688
H5T__get_native_float(size_t size, H5T_direction_t direction, size_t *struct_align, size_t *offset,
689
                      size_t *comp_size)
690
0
{
691
0
    H5T_t *dt          = NULL; /* Appropriate native datatype to copy */
692
0
    hid_t  tid         = (-1); /* Datatype ID of appropriate native datatype */
693
0
    size_t align       = 0;    /* Alignment necessary for native datatype */
694
0
    size_t native_size = 0;    /* Datatype size of the native type */
695
0
    enum match_type {          /* The different kinds of floating point types we can match */
696
0
                      H5T_NATIVE_FLOAT_MATCH_FLOAT16,
697
0
                      H5T_NATIVE_FLOAT_MATCH_FLOAT,
698
0
                      H5T_NATIVE_FLOAT_MATCH_DOUBLE,
699
0
                      H5T_NATIVE_FLOAT_MATCH_LDOUBLE,
700
0
                      H5T_NATIVE_FLOAT_MATCH_UNKNOWN
701
0
    } match          = H5T_NATIVE_FLOAT_MATCH_UNKNOWN;
702
0
    H5T_t *ret_value = NULL; /* Return value */
703
704
0
    FUNC_ENTER_PACKAGE
705
706
0
    assert(size > 0);
707
708
0
    if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
709
#ifdef H5_HAVE__FLOAT16
710
        if (size <= sizeof(H5__Float16)) {
711
            match       = H5T_NATIVE_FLOAT_MATCH_FLOAT16;
712
            native_size = sizeof(H5__Float16);
713
        }
714
        else
715
#endif
716
0
            if (size <= sizeof(float)) {
717
0
            match       = H5T_NATIVE_FLOAT_MATCH_FLOAT;
718
0
            native_size = sizeof(float);
719
0
        }
720
0
        else if (size <= sizeof(double)) {
721
0
            match       = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
722
0
            native_size = sizeof(double);
723
0
        }
724
0
        else if (size <= sizeof(long double)) {
725
0
            match       = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
726
0
            native_size = sizeof(long double);
727
0
        }
728
0
        else { /* If not match, return the biggest datatype */
729
0
            match       = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
730
0
            native_size = sizeof(long double);
731
0
        }
732
0
    }
733
0
    else {
734
0
        if (size > sizeof(double)) {
735
0
            match       = H5T_NATIVE_FLOAT_MATCH_LDOUBLE;
736
0
            native_size = sizeof(long double);
737
0
        }
738
0
        else if (size > sizeof(float)) {
739
0
            match       = H5T_NATIVE_FLOAT_MATCH_DOUBLE;
740
0
            native_size = sizeof(double);
741
0
        }
742
0
        else
743
#ifdef H5_HAVE__FLOAT16
744
            if (size > sizeof(H5__Float16))
745
#endif
746
0
        {
747
0
            match       = H5T_NATIVE_FLOAT_MATCH_FLOAT;
748
0
            native_size = sizeof(float);
749
0
        }
750
#ifdef H5_HAVE__FLOAT16
751
        else {
752
            match       = H5T_NATIVE_FLOAT_MATCH_FLOAT16;
753
            native_size = sizeof(H5__Float16);
754
        }
755
#endif
756
0
    }
757
758
    /* Set the appropriate native floating point information */
759
0
    switch (match) {
760
0
        case H5T_NATIVE_FLOAT_MATCH_FLOAT16:
761
0
            tid   = H5T_NATIVE_FLOAT16;
762
0
            align = H5T_NATIVE_FLOAT16_ALIGN_g;
763
0
            break;
764
765
0
        case H5T_NATIVE_FLOAT_MATCH_FLOAT:
766
0
            tid   = H5T_NATIVE_FLOAT;
767
0
            align = H5T_NATIVE_FLOAT_ALIGN_g;
768
0
            break;
769
770
0
        case H5T_NATIVE_FLOAT_MATCH_DOUBLE:
771
0
            tid   = H5T_NATIVE_DOUBLE;
772
0
            align = H5T_NATIVE_DOUBLE_ALIGN_g;
773
0
            break;
774
775
0
        case H5T_NATIVE_FLOAT_MATCH_LDOUBLE:
776
0
            tid   = H5T_NATIVE_LDOUBLE;
777
0
            align = H5T_NATIVE_LDOUBLE_ALIGN_g;
778
0
            break;
779
780
0
        case H5T_NATIVE_FLOAT_MATCH_UNKNOWN:
781
0
        default:
782
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "Unknown native floating-point match");
783
0
    } /* end switch */
784
785
    /* Create new native type */
786
0
    assert(tid >= 0);
787
0
    if (NULL == (dt = (H5T_t *)H5I_object(tid)))
788
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type");
789
0
    if ((ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)) == NULL)
790
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type");
791
792
    /* compute offset of compound type member. */
793
0
    if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
794
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
795
796
0
done:
797
0
    FUNC_LEAVE_NOAPI(ret_value)
798
0
} /* end H5T__get_native_float() */
799
H5_GCC_DIAG_ON("duplicated-branches")
800
801
/* Disable warning for intentional identical branches here -QAK */
802
/*
803
 *       This pragma only needs to surround the "duplicated branches" in
804
 *       the code below, but early (4.4.7, at least) gcc only allows
805
 *       diagnostic pragmas to be toggled outside of functions.
806
 */
807
H5_GCC_DIAG_OFF("duplicated-branches")
808
809
/*-------------------------------------------------------------------------
810
 * Function:    H5T__get_native_bitfield
811
 *
812
 * Purpose:     Returns the native bitfield type of a datatype.  Bitfield
813
 *              is similar to unsigned integer.
814
 *
815
 * Return:      Success:        Returns the native data type if successful.
816
 *
817
 *              Failure:        negative
818
 *
819
 *-------------------------------------------------------------------------
820
 */
821
static H5T_t *
822
H5T__get_native_bitfield(size_t prec, H5T_direction_t direction, size_t *struct_align, size_t *offset,
823
                         size_t *comp_size)
824
0
{
825
0
    H5T_t *dt;                 /* Appropriate native datatype to copy */
826
0
    hid_t  tid         = (-1); /* Datatype ID of appropriate native datatype */
827
0
    size_t align       = 0;    /* Alignment necessary for native datatype */
828
0
    size_t native_size = 0;    /* Datatype size of the native type */
829
0
    H5T_t *ret_value   = NULL; /* Return value */
830
831
0
    FUNC_ENTER_PACKAGE
832
833
0
    if (direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) {
834
0
        if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B8_g))) {
835
0
            tid         = H5T_NATIVE_B8;
836
0
            native_size = 1;
837
0
            align       = H5T_NATIVE_UINT8_ALIGN_g;
838
0
        }
839
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B16_g))) {
840
0
            tid         = H5T_NATIVE_B16;
841
0
            native_size = 2;
842
0
            align       = H5T_NATIVE_UINT16_ALIGN_g;
843
0
        }
844
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B32_g))) {
845
0
            tid         = H5T_NATIVE_B32;
846
0
            native_size = 4;
847
0
            align       = H5T_NATIVE_UINT32_ALIGN_g;
848
0
        }
849
0
        else if (prec <= H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B64_g))) {
850
0
            tid         = H5T_NATIVE_B64;
851
0
            native_size = 8;
852
0
            align       = H5T_NATIVE_UINT64_ALIGN_g;
853
0
        }
854
0
        else { /* If no native type matches the queried datatype, simply choose the type of biggest size. */
855
0
            tid         = H5T_NATIVE_B64;
856
0
            native_size = 8;
857
0
            align       = H5T_NATIVE_UINT64_ALIGN_g;
858
0
        }
859
0
    }
860
0
    else if (direction == H5T_DIR_DESCEND) {
861
0
        if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B32_g))) {
862
0
            tid         = H5T_NATIVE_B64;
863
0
            native_size = 8;
864
0
            align       = H5T_NATIVE_UINT64_ALIGN_g;
865
0
        }
866
0
        else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B16_g))) {
867
0
            tid         = H5T_NATIVE_B32;
868
0
            native_size = 4;
869
0
            align       = H5T_NATIVE_UINT32_ALIGN_g;
870
0
        }
871
0
        else if (prec > H5T_get_precision((H5T_t *)H5I_object(H5T_NATIVE_B8_g))) {
872
0
            tid         = H5T_NATIVE_B16;
873
0
            native_size = 2;
874
0
            align       = H5T_NATIVE_UINT16_ALIGN_g;
875
0
        }
876
0
        else {
877
0
            tid         = H5T_NATIVE_B8;
878
0
            native_size = 1;
879
0
            align       = H5T_NATIVE_UINT8_ALIGN_g;
880
0
        }
881
0
    }
882
883
    /* Create new native type */
884
0
    assert(tid >= 0);
885
0
    if (NULL == (dt = (H5T_t *)H5I_object(tid)))
886
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type");
887
888
0
    if ((ret_value = H5T_copy(dt, H5T_COPY_TRANSIENT)) == NULL)
889
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot copy type");
890
891
    /* compute size and offset of compound type member. */
892
0
    if (H5T__cmp_offset(comp_size, offset, native_size, (size_t)1, align, struct_align) < 0)
893
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset");
894
895
0
done:
896
0
    FUNC_LEAVE_NOAPI(ret_value)
897
0
} /* end H5T__get_native_bitfield() */
898
H5_GCC_DIAG_ON("duplicated-branches")
899
900
/*-------------------------------------------------------------------------
901
 * Function:    H5T__cmp_offset
902
 *
903
 * Purpose:    This function is only for convenience.  It computes the
904
 *              compound type size, offset of the member being considered
905
 *              and the alignment for the whole compound type.
906
 *
907
 * Return:    Success:        Non-negative value.
908
 *
909
 *            Failure:        Negative value.
910
 *
911
 *-------------------------------------------------------------------------
912
 */
913
static herr_t
914
H5T__cmp_offset(size_t *comp_size, size_t *offset, size_t elem_size, size_t nelems, size_t align,
915
                size_t *struct_align)
916
0
{
917
0
    FUNC_ENTER_PACKAGE_NOERR
918
919
0
    if (offset && comp_size) {
920
0
        if (align > 1 && *comp_size % align) {
921
            /* Add alignment value */
922
0
            *offset = *comp_size + (align - *comp_size % align);
923
0
            *comp_size += (align - *comp_size % align);
924
0
        } /* end if */
925
0
        else
926
0
            *offset = *comp_size;
927
928
        /* compute size of compound type member. */
929
0
        *comp_size += nelems * elem_size;
930
0
    } /* end if */
931
932
0
    if (struct_align && *struct_align < align)
933
0
        *struct_align = align;
934
935
0
    FUNC_LEAVE_NOAPI(SUCCEED)
936
0
} /* end H5T__cmp_offset() */
937
938
41
#define TAG_ALIGNMENT(tag) (offsetof(alignments_t, tag.x) - offsetof(alignments_t, tag))
939
940
/* clang-format off */
941
36
#define NATIVE_ENTRY_INITIALIZER(tag, type, precision, has_sign) {  \
942
36
  .alignmentp = &H5T_NATIVE_##tag##_ALIGN_g                         \
943
36
, .alignment = TAG_ALIGNMENT(tag)                                   \
944
36
, .hidp = &H5T_NATIVE_##tag##_g                                     \
945
36
, .size = sizeof(type)                                              \
946
36
, .atomic = {                                                       \
947
36
      .offset   = 0                                                 \
948
36
    , .prec     = (precision != 0) ? precision : (sizeof(type) * 8) \
949
36
    , .lsb_pad  = H5T_PAD_ZERO                                      \
950
36
    , .msb_pad  = H5T_PAD_ZERO                                      \
951
36
    , .u.i.sign = has_sign ? H5T_SGN_2 : H5T_SGN_NONE               \
952
36
    }                                                               \
953
36
}
954
/* clang-format on */
955
956
static H5T_order_t
957
get_host_byte_order(void)
958
1
{
959
1
    static const union {
960
1
        uint64_t u64;
961
1
        char     byte[8];
962
1
    } endian_exemplar = {.byte = {1}};
963
964
1
    return (endian_exemplar.u64 == 1) ? H5T_ORDER_LE : H5T_ORDER_BE;
965
1
}
966
967
/* Establish `H5T_t`s for C99 integer types including fixed- and
968
 * minimum-width types (uint16_t, uint_least16_t, uint_fast16_t, ...).
969
 *
970
 * Also establish alignment for some miscellaneous types: pointers,
971
 * HDF5 references, and so on.
972
 */
973
herr_t
974
H5T__init_native_internal(void)
975
1
{
976
    /* Here we construct a type that lets us find alignment constraints
977
     * without using the alignof operator, which is not available in C99.
978
     *
979
     * Between each sub-struct's `char` member `c` and member `x`, the
980
     * compiler must insert padding to ensure proper alignment of `x`.
981
     * We can find the alignment constraint of each `x` by looking at
982
     * its offset from the beginning of its sub-struct.
983
     */
984
1
    typedef struct {
985
1
        struct {
986
1
            char        c;
987
1
            signed char x;
988
1
        } SCHAR;
989
1
        struct {
990
1
            char          c;
991
1
            unsigned char x;
992
1
        } UCHAR;
993
1
        struct {
994
1
            char  c;
995
1
            short x;
996
1
        } SHORT;
997
1
        struct {
998
1
            char           c;
999
1
            unsigned short x;
1000
1
        } USHORT;
1001
1
        struct {
1002
1
            char c;
1003
1
            int  x;
1004
1
        } INT;
1005
1
        struct {
1006
1
            char         c;
1007
1
            unsigned int x;
1008
1
        } UINT;
1009
1
        struct {
1010
1
            char c;
1011
1
            long x;
1012
1
        } LONG;
1013
1
        struct {
1014
1
            char          c;
1015
1
            unsigned long x;
1016
1
        } ULONG;
1017
1
        struct {
1018
1
            char      c;
1019
1
            long long x;
1020
1
        } LLONG;
1021
1
        struct {
1022
1
            char               c;
1023
1
            unsigned long long x;
1024
1
        } ULLONG;
1025
1
        struct {
1026
1
            char   c;
1027
1
            int8_t x;
1028
1
        } INT8;
1029
1
        struct {
1030
1
            char    c;
1031
1
            uint8_t x;
1032
1
        } UINT8;
1033
1
        struct {
1034
1
            char         c;
1035
1
            int_least8_t x;
1036
1
        } INT_LEAST8;
1037
1
        struct {
1038
1
            char          c;
1039
1
            uint_least8_t x;
1040
1
        } UINT_LEAST8;
1041
1
        struct {
1042
1
            char        c;
1043
1
            int_fast8_t x;
1044
1
        } INT_FAST8;
1045
1
        struct {
1046
1
            char         c;
1047
1
            uint_fast8_t x;
1048
1
        } UINT_FAST8;
1049
1
        struct {
1050
1
            char    c;
1051
1
            int16_t x;
1052
1
        } INT16;
1053
1
        struct {
1054
1
            char     c;
1055
1
            uint16_t x;
1056
1
        } UINT16;
1057
1
        struct {
1058
1
            char          c;
1059
1
            int_least16_t x;
1060
1
        } INT_LEAST16;
1061
1
        struct {
1062
1
            char           c;
1063
1
            uint_least16_t x;
1064
1
        } UINT_LEAST16;
1065
1
        struct {
1066
1
            char         c;
1067
1
            int_fast16_t x;
1068
1
        } INT_FAST16;
1069
1
        struct {
1070
1
            char          c;
1071
1
            uint_fast16_t x;
1072
1
        } UINT_FAST16;
1073
1
        struct {
1074
1
            char    c;
1075
1
            int32_t x;
1076
1
        } INT32;
1077
1
        struct {
1078
1
            char     c;
1079
1
            uint32_t x;
1080
1
        } UINT32;
1081
1
        struct {
1082
1
            char          c;
1083
1
            int_least32_t x;
1084
1
        } INT_LEAST32;
1085
1
        struct {
1086
1
            char           c;
1087
1
            uint_least32_t x;
1088
1
        } UINT_LEAST32;
1089
1
        struct {
1090
1
            char         c;
1091
1
            int_fast32_t x;
1092
1
        } INT_FAST32;
1093
1
        struct {
1094
1
            char          c;
1095
1
            uint_fast32_t x;
1096
1
        } UINT_FAST32;
1097
1
        struct {
1098
1
            char    c;
1099
1
            int64_t x;
1100
1
        } INT64;
1101
1
        struct {
1102
1
            char     c;
1103
1
            uint64_t x;
1104
1
        } UINT64;
1105
1
        struct {
1106
1
            char          c;
1107
1
            int_least64_t x;
1108
1
        } INT_LEAST64;
1109
1
        struct {
1110
1
            char           c;
1111
1
            uint_least64_t x;
1112
1
        } UINT_LEAST64;
1113
1
        struct {
1114
1
            char         c;
1115
1
            int_fast64_t x;
1116
1
        } INT_FAST64;
1117
1
        struct {
1118
1
            char          c;
1119
1
            uint_fast64_t x;
1120
1
        } UINT_FAST64;
1121
1
        struct {
1122
1
            char  c;
1123
1
            void *x;
1124
1
        } pointer;
1125
1
        struct {
1126
1
            char  c;
1127
1
            hvl_t x;
1128
1
        } hvl;
1129
1
        struct {
1130
1
            char       c;
1131
1
            hobj_ref_t x;
1132
1
        } hobjref;
1133
1
        struct {
1134
1
            char            c;
1135
1
            hdset_reg_ref_t x;
1136
1
        } hdsetregref;
1137
1
        struct {
1138
1
            char      c;
1139
1
            H5R_ref_t x;
1140
1
        } ref;
1141
1
    } alignments_t;
1142
1143
    /* Describe a C99 type, `type`, and tell where to write its
1144
     * H5T_t identifier and alignment.  Tables of these descriptions
1145
     * drive the initialization of `H5T_t`s.
1146
     */
1147
1
    typedef struct {
1148
        /* Pointer to the global variable that receives the
1149
         * alignment of `type`:
1150
         */
1151
1
        size_t *alignmentp;
1152
1
        size_t  alignment; // natural alignment of `type`
1153
        /* Pointer to the global variable that receives the
1154
         * identifier for `type`'s H5T_t:
1155
         */
1156
1
        hid_t       *hidp;
1157
1
        size_t       size;   // sizeof(`type`)
1158
1
        H5T_atomic_t atomic; // `type` facts such as signedness
1159
1
    } native_int_t;
1160
1161
1
    typedef struct {
1162
1
        const native_int_t *table;
1163
1
        size_t              nelmts;
1164
1
    } native_int_table_t;
1165
1166
    /* clang-format off */
1167
1168
    /* Version 19.10 of the PGI C compiler croaks on the following
1169
     * tables if they are `static`, so make them `static` only if
1170
     * some other compiler is used.
1171
     */
1172
#if defined(__PGIC__) && __PGIC__ == 19 && __PGIC_MINOR__ == 10
1173
#   define static_unless_buggy_pgic
1174
#else
1175
4
#   define static_unless_buggy_pgic static
1176
1
#endif
1177
1178
    /* The library compiles with a limit on `static` object size, so
1179
     * I broke this table into three.
1180
     */
1181
1
    static_unless_buggy_pgic const native_int_t table1[] = {
1182
1
      NATIVE_ENTRY_INITIALIZER(SCHAR, signed char, 0, true)
1183
1
    , NATIVE_ENTRY_INITIALIZER(UCHAR, unsigned char, 0, false)
1184
1
    , NATIVE_ENTRY_INITIALIZER(SHORT, short, 0, true)
1185
1
    , NATIVE_ENTRY_INITIALIZER(USHORT, unsigned short, 0, false)
1186
1
    , NATIVE_ENTRY_INITIALIZER(INT, int, 0, true)
1187
1
    , NATIVE_ENTRY_INITIALIZER(UINT, unsigned int, 0, false)
1188
1
    , NATIVE_ENTRY_INITIALIZER(INT, int, 0, true)
1189
1
    , NATIVE_ENTRY_INITIALIZER(UINT, unsigned int, 0, false)
1190
1
    , NATIVE_ENTRY_INITIALIZER(LONG, long, 0, true)
1191
1
    , NATIVE_ENTRY_INITIALIZER(ULONG, unsigned long, 0, false)
1192
1
    , NATIVE_ENTRY_INITIALIZER(LLONG, long long, 0, true)
1193
1
    , NATIVE_ENTRY_INITIALIZER(ULLONG, unsigned long long, 0, false)
1194
1
    };
1195
1
    static_unless_buggy_pgic const native_int_t table2[] = {
1196
1
      NATIVE_ENTRY_INITIALIZER(INT8, int8_t, 0, true)
1197
1
    , NATIVE_ENTRY_INITIALIZER(UINT8, uint8_t, 0, false)
1198
1
    , NATIVE_ENTRY_INITIALIZER(INT_LEAST8, int_least8_t, 0, true)
1199
1
    , NATIVE_ENTRY_INITIALIZER(UINT_LEAST8, uint_least8_t, 0, false)
1200
1
    , NATIVE_ENTRY_INITIALIZER(INT_FAST8, int_fast8_t, 0, true)
1201
1
    , NATIVE_ENTRY_INITIALIZER(UINT_FAST8, uint_fast8_t, 0, false)
1202
1
    , NATIVE_ENTRY_INITIALIZER(INT16, int16_t, 0, true)
1203
1
    , NATIVE_ENTRY_INITIALIZER(UINT16, uint16_t, 0, false)
1204
1
    , NATIVE_ENTRY_INITIALIZER(INT_LEAST16, int_least16_t, 0, true)
1205
1
    , NATIVE_ENTRY_INITIALIZER(UINT_LEAST16, uint_least16_t, 0, false)
1206
1
    , NATIVE_ENTRY_INITIALIZER(INT_FAST16, int_fast16_t, 0, true)
1207
1
    , NATIVE_ENTRY_INITIALIZER(UINT_FAST16, uint_fast16_t, 0, false)
1208
1
    };
1209
1
    static_unless_buggy_pgic const native_int_t table3[] = {
1210
1
      NATIVE_ENTRY_INITIALIZER(INT32, int32_t, 0, true)
1211
1
    , NATIVE_ENTRY_INITIALIZER(UINT32, uint32_t, 0, false)
1212
1
    , NATIVE_ENTRY_INITIALIZER(INT_LEAST32, int_least32_t, 0, true)
1213
1
    , NATIVE_ENTRY_INITIALIZER(UINT_LEAST32, uint_least32_t, 0, false)
1214
1
    , NATIVE_ENTRY_INITIALIZER(INT_FAST32, int_fast32_t, 0, true)
1215
1
    , NATIVE_ENTRY_INITIALIZER(UINT_FAST32, uint_fast32_t, 0, false)
1216
1
    , NATIVE_ENTRY_INITIALIZER(INT64, int64_t, 0, true)
1217
1
    , NATIVE_ENTRY_INITIALIZER(UINT64, uint64_t, 0, false)
1218
1
    , NATIVE_ENTRY_INITIALIZER(INT_LEAST64, int_least64_t, 0, true)
1219
1
    , NATIVE_ENTRY_INITIALIZER(UINT_LEAST64, uint_least64_t, 0, false)
1220
1
    , NATIVE_ENTRY_INITIALIZER(INT_FAST64, int_fast64_t, 0, true)
1221
1
    , NATIVE_ENTRY_INITIALIZER(UINT_FAST64, uint_fast64_t, 0, false)
1222
1
    };
1223
1
    static_unless_buggy_pgic const native_int_table_t table_table[] = {
1224
1
      {table1, NELMTS(table1)}
1225
1
    , {table2, NELMTS(table2)}
1226
1
    , {table3, NELMTS(table3)}
1227
1
    };
1228
1
#undef static_unless_buggy_pgic
1229
    /* clang-format on */
1230
1231
1
    size_t      i, j;
1232
1
    H5T_order_t byte_order = get_host_byte_order();
1233
1234
4
    for (i = 0; i < NELMTS(table_table); i++) {
1235
3
        const native_int_t *table  = table_table[i].table;
1236
3
        size_t              nelmts = table_table[i].nelmts;
1237
1238
        /* For each C99 type in `table`, create its H5T_t,
1239
         * register a hid_t for the H5T_t, and record the type's
1240
         * alignment and hid_t in the variables named by the
1241
         * table.
1242
         */
1243
39
        for (j = 0; j < nelmts; j++) {
1244
36
            H5T_t *dt;
1245
1246
36
            if (NULL == (dt = H5T__alloc()))
1247
0
                return FAIL;
1248
1249
36
            dt->shared->state          = H5T_STATE_IMMUTABLE;
1250
36
            dt->shared->type           = H5T_INTEGER;
1251
36
            dt->shared->size           = table[j].size;
1252
36
            dt->shared->u.atomic       = table[j].atomic;
1253
36
            dt->shared->u.atomic.order = byte_order;
1254
36
            *table[j].alignmentp       = table[j].alignment;
1255
1256
36
            if ((*table[j].hidp = H5I_register(H5I_DATATYPE, dt, false)) < 0)
1257
0
                return FAIL;
1258
36
        }
1259
3
    }
1260
1261
1
    H5T_POINTER_ALIGN_g     = TAG_ALIGNMENT(pointer);
1262
1
    H5T_HVL_ALIGN_g         = TAG_ALIGNMENT(hvl);
1263
1
    H5T_HOBJREF_ALIGN_g     = TAG_ALIGNMENT(hobjref);
1264
1
    H5T_HDSETREGREF_ALIGN_g = TAG_ALIGNMENT(hdsetregref);
1265
1
    H5T_REF_ALIGN_g         = TAG_ALIGNMENT(ref);
1266
1267
1
    return SUCCEED;
1268
1
}