Coverage Report

Created: 2025-11-17 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Dcompact.c
Line
Count
Source
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 LICENSE 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
 * Purpose:     Compact dataset I/O functions.  These routines are similar
15
 *              H5D_contig_* and H5D_chunk_*.
16
 */
17
18
/****************/
19
/* Module Setup */
20
/****************/
21
22
#include "H5Dmodule.h" /* This source code file is part of the H5D module */
23
24
/***********/
25
/* Headers */
26
/***********/
27
#include "H5private.h"   /* Generic Functions     */
28
#include "H5Dpkg.h"      /* Dataset functions     */
29
#include "H5Eprivate.h"  /* Error handling        */
30
#include "H5Fprivate.h"  /* Files       */
31
#include "H5FDprivate.h" /* File drivers        */
32
#include "H5FLprivate.h" /* Free Lists                           */
33
#include "H5Iprivate.h"  /* IDs           */
34
#include "H5MMprivate.h" /* Memory management     */
35
#include "H5Oprivate.h"  /* Object headers        */
36
#include "H5VMprivate.h" /* Vector and array functions    */
37
38
/****************/
39
/* Local Macros */
40
/****************/
41
42
/******************/
43
/* Local Typedefs */
44
/******************/
45
46
/* Callback info for I/O operation when file driver
47
 * wishes to do its own memory management
48
 */
49
typedef struct H5D_compact_iovv_memmanage_ud_t {
50
    H5F_shared_t *f_sh;   /* Shared file for dataset */
51
    void         *dstbuf; /* Pointer to buffer to be read into/written into */
52
    const void   *srcbuf; /* Pointer to buffer to be read from/written from */
53
} H5D_compact_iovv_memmanage_ud_t;
54
55
/********************/
56
/* Local Prototypes */
57
/********************/
58
59
/* Layout operation callbacks */
60
static herr_t  H5D__compact_construct(H5F_t *f, H5D_t *dset);
61
static herr_t  H5D__compact_init(H5F_t *f, H5D_t *dset, hid_t dapl_id, bool open_op);
62
static bool    H5D__compact_is_space_alloc(const H5O_storage_t *storage);
63
static herr_t  H5D__compact_io_init(H5D_io_info_t *io_info, H5D_dset_io_info_t *dinfo);
64
static herr_t  H5D__compact_iovv_memmanage_cb(hsize_t dst_off, hsize_t src_off, size_t len, void *_udata);
65
static ssize_t H5D__compact_readvv(const H5D_io_info_t *io_info, const H5D_dset_io_info_t *dset_info,
66
                                   size_t dset_max_nseq, size_t *dset_curr_seq, size_t dset_size_arr[],
67
                                   hsize_t dset_offset_arr[], size_t mem_max_nseq, size_t *mem_curr_seq,
68
                                   size_t mem_size_arr[], hsize_t mem_offset_arr[]);
69
static ssize_t H5D__compact_writevv(const H5D_io_info_t *io_info, const H5D_dset_io_info_t *dset_info,
70
                                    size_t dset_max_nseq, size_t *dset_curr_seq, size_t dset_size_arr[],
71
                                    hsize_t dset_offset_arr[], size_t mem_max_nseq, size_t *mem_curr_seq,
72
                                    size_t mem_size_arr[], hsize_t mem_offset_arr[]);
73
static herr_t  H5D__compact_flush(H5D_t *dset);
74
static herr_t  H5D__compact_dest(H5D_t *dset);
75
76
/*********************/
77
/* Package Variables */
78
/*********************/
79
80
/* Compact storage layout I/O ops */
81
const H5D_layout_ops_t H5D_LOPS_COMPACT[1] = {{
82
    H5D__compact_construct,      /* construct */
83
    H5D__compact_init,           /* init */
84
    H5D__compact_is_space_alloc, /* is_space_alloc */
85
    NULL,                        /* is_data_cached */
86
    H5D__compact_io_init,        /* io_init */
87
    NULL,                        /* mdio_init */
88
    H5D__contig_read,            /* ser_read */
89
    H5D__contig_write,           /* ser_write */
90
    H5D__compact_readvv,         /* readvv */
91
    H5D__compact_writevv,        /* writevv */
92
    H5D__compact_flush,          /* flush */
93
    NULL,                        /* io_term */
94
    H5D__compact_dest            /* dest */
95
}};
96
97
/*******************/
98
/* Local Variables */
99
/*******************/
100
101
/* Declare extern the free list to manage blocks of type conversion data */
102
H5FL_BLK_EXTERN(type_conv);
103
104
/*-------------------------------------------------------------------------
105
 * Function:  H5D__compact_fill
106
 *
107
 * Purpose: Write fill values to a compactly stored dataset.
108
 *
109
 * Return:  Non-negative on success/Negative on failure
110
 *
111
 *-------------------------------------------------------------------------
112
 */
113
herr_t
114
H5D__compact_fill(const H5D_t *dset)
115
0
{
116
0
    H5D_fill_buf_info_t fb_info;                /* Dataset's fill buffer info */
117
0
    bool                fb_info_init = false;   /* Whether the fill value buffer has been initialized */
118
0
    herr_t              ret_value    = SUCCEED; /* Return value */
119
120
0
    FUNC_ENTER_PACKAGE
121
122
    /* Check args */
123
0
    assert(dset && H5D_COMPACT == dset->shared->layout.type);
124
0
    assert(dset->shared->layout.storage.u.compact.buf);
125
0
    assert(dset->shared->type);
126
0
    assert(dset->shared->space);
127
128
    /* Initialize the fill value buffer */
129
    /* (use the compact dataset storage buffer as the fill value buffer) */
130
0
    if (H5D__fill_init(&fb_info, dset->shared->layout.storage.u.compact.buf, NULL, NULL, NULL, NULL,
131
0
                       &dset->shared->dcpl_cache.fill, dset->shared->type, (size_t)0,
132
0
                       dset->shared->layout.storage.u.compact.size) < 0)
133
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "can't initialize fill buffer info");
134
0
    fb_info_init = true;
135
136
    /* Check for VL datatype & non-default fill value */
137
0
    if (fb_info.has_vlen_fill_type)
138
        /* Fill the buffer with VL datatype fill values */
139
0
        if (H5D__fill_refill_vl(&fb_info, fb_info.elmts_per_buf) < 0)
140
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTCONVERT, FAIL, "can't refill fill value buffer");
141
142
0
done:
143
    /* Release the fill buffer info, if it's been initialized */
144
0
    if (fb_info_init && H5D__fill_term(&fb_info) < 0)
145
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTFREE, FAIL, "Can't release fill buffer info");
146
147
0
    FUNC_LEAVE_NOAPI(ret_value)
148
0
} /* end H5D__compact_fill() */
149
150
/*-------------------------------------------------------------------------
151
 * Function:    H5D__compact_construct
152
 *
153
 * Purpose:     Constructs new compact layout information for dataset and
154
 *              upgrades layout version if appropriate
155
 *
156
 * Return:      Non-negative on success/Negative on failure
157
 *
158
 *-------------------------------------------------------------------------
159
 */
160
static herr_t
161
H5D__compact_construct(H5F_t *f, H5D_t *dset)
162
0
{
163
0
    hssize_t stmp_size;           /* Temporary holder for raw data size */
164
0
    hsize_t  tmp_size;            /* Temporary holder for raw data size */
165
0
    hsize_t  max_comp_data_size;  /* Max. allowed size of compact data */
166
0
    unsigned version;             /* Message version */
167
0
    unsigned u;                   /* Local index variable */
168
0
    herr_t   ret_value = SUCCEED; /* Return value */
169
170
0
    FUNC_ENTER_PACKAGE
171
172
    /* Sanity checks */
173
0
    assert(f);
174
0
    assert(dset);
175
0
    assert(dset->shared);
176
177
    /* Check for invalid dataset dimensions */
178
0
    for (u = 0; u < dset->shared->ndims; u++)
179
0
        if (dset->shared->max_dims[u] > dset->shared->curr_dims[u])
180
0
            HGOTO_ERROR(H5E_DATASET, H5E_UNSUPPORTED, FAIL, "extendible compact dataset not allowed");
181
182
    /*
183
     * Compact dataset is stored in dataset object header message of
184
     * layout.
185
     */
186
0
    stmp_size = H5S_GET_EXTENT_NPOINTS(dset->shared->space);
187
0
    assert(stmp_size >= 0);
188
0
    tmp_size = H5T_get_size(dset->shared->type);
189
0
    assert(tmp_size > 0);
190
0
    tmp_size = tmp_size * (hsize_t)stmp_size;
191
0
    H5_CHECKED_ASSIGN(dset->shared->layout.storage.u.compact.size, size_t, tmp_size, hssize_t);
192
193
    /* Verify data size is smaller than maximum header message size
194
     * (64KB) minus other layout message fields.
195
     */
196
0
    max_comp_data_size = H5O_MESG_MAX_SIZE - H5D__layout_meta_size(f, &(dset->shared->layout), false);
197
0
    if (dset->shared->layout.storage.u.compact.size > max_comp_data_size)
198
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL,
199
0
                    "compact dataset size is bigger than header message maximum size");
200
201
    /* If the layout is below version 3, upgrade to version 3 if allowed. Do not upgrade past version 3 since
202
     * there is no benefit. */
203
0
    if (dset->shared->layout.version < H5O_LAYOUT_VERSION_3) {
204
0
        version = MAX(dset->shared->layout.version,
205
0
                      MIN(H5O_layout_ver_bounds[H5F_LOW_BOUND(f)], H5O_LAYOUT_VERSION_3));
206
207
        /* Version bounds check */
208
0
        if (version > H5O_layout_ver_bounds[H5F_HIGH_BOUND(f)])
209
0
            HGOTO_ERROR(H5E_DATASET, H5E_BADRANGE, FAIL, "layout version out of bounds");
210
211
0
        dset->shared->layout.version = version;
212
0
    }
213
214
0
done:
215
0
    FUNC_LEAVE_NOAPI(ret_value)
216
0
} /* end H5D__compact_construct() */
217
218
/*-------------------------------------------------------------------------
219
 * Function:  H5D__compact_init
220
 *
221
 * Purpose: Initialize the info for a compact dataset.  This is
222
 *    called when the dataset is initialized.
223
 *
224
 * Return:  Non-negative on success/Negative on failure
225
 *
226
 *-------------------------------------------------------------------------
227
 */
228
static herr_t
229
H5D__compact_init(H5F_t H5_ATTR_UNUSED *f, H5D_t *dset, hid_t H5_ATTR_UNUSED dapl_id,
230
                  bool H5_ATTR_UNUSED open_op)
231
44
{
232
44
    hssize_t snelmts;             /* Temporary holder for number of elements in dataspace */
233
44
    hsize_t  nelmts;              /* Number of elements in dataspace */
234
44
    size_t   dt_size;             /* Size of datatype */
235
44
    hsize_t  data_size;           /* Dataset size, in bytes */
236
44
    herr_t   ret_value = SUCCEED; /* Return value */
237
238
44
    FUNC_ENTER_PACKAGE
239
240
    /* Sanity check */
241
44
    assert(dset);
242
44
    assert(H5D_COMPACT == dset->shared->layout.storage.type);
243
244
    /*
245
     * Now that we've read the dataset's datatype, dataspace and
246
     * layout information, perform a quick check for compact datasets
247
     * to ensure that the size of the internal buffer that was
248
     * allocated for the dataset's raw data matches the size of
249
     * the data. A corrupted file can cause a mismatch between the
250
     * two, which might result in buffer overflows during future
251
     * I/O to the dataset.
252
     */
253
44
    if (0 == (dt_size = H5T_GET_SIZE(dset->shared->type)))
254
0
        HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get datatype size");
255
44
    if ((snelmts = H5S_GET_EXTENT_NPOINTS(dset->shared->space)) < 0)
256
9
        HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get number of elements in dataset's dataspace");
257
35
    nelmts = (hsize_t)snelmts;
258
259
    /* Compute the size of the dataset's contiguous storage */
260
35
    data_size = nelmts * dt_size;
261
262
    /* Check for overflow during multiplication */
263
35
    if (nelmts != (data_size / dt_size))
264
3
        HGOTO_ERROR(H5E_DATASET, H5E_OVERFLOW, FAIL, "size of dataset's storage overflowed");
265
266
    /* Check for mismatch */
267
32
    if (dset->shared->layout.storage.u.compact.size != data_size)
268
15
        HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL,
269
32
                    "bad value from dataset header - size of compact dataset's data buffer doesn't match "
270
32
                    "size of dataset data");
271
272
44
done:
273
44
    FUNC_LEAVE_NOAPI(ret_value)
274
44
} /* end H5D__compact_init() */
275
276
/*-------------------------------------------------------------------------
277
 * Function:  H5D__compact_is_space_alloc
278
 *
279
 * Purpose: Query if space is allocated for layout
280
 *
281
 * Return:  Non-negative on success/Negative on failure
282
 *
283
 *-------------------------------------------------------------------------
284
 */
285
static bool
286
H5D__compact_is_space_alloc(const H5O_storage_t H5_ATTR_UNUSED *storage)
287
13
{
288
13
    FUNC_ENTER_PACKAGE_NOERR
289
290
    /* Sanity checks */
291
13
    assert(storage);
292
293
    /* Compact storage is currently always allocated */
294
13
    FUNC_LEAVE_NOAPI(true)
295
13
} /* end H5D__compact_is_space_alloc() */
296
297
/*-------------------------------------------------------------------------
298
 * Function:  H5D__compact_io_init
299
 *
300
 * Purpose: Performs initialization before any sort of I/O on the raw data
301
 *
302
 * Return:  Non-negative on success/Negative on failure
303
 *
304
 *-------------------------------------------------------------------------
305
 */
306
static herr_t
307
H5D__compact_io_init(H5D_io_info_t *io_info, H5D_dset_io_info_t *dinfo)
308
0
{
309
0
    FUNC_ENTER_PACKAGE_NOERR
310
311
0
    dinfo->store->compact.buf               = dinfo->dset->shared->layout.storage.u.compact.buf;
312
0
    dinfo->store->compact.dirty             = &dinfo->dset->shared->layout.storage.u.compact.dirty;
313
0
    dinfo->layout_io_info.contig_piece_info = NULL;
314
315
    /* Disable selection I/O */
316
0
    io_info->use_select_io = H5D_SELECTION_IO_MODE_OFF;
317
0
    io_info->no_selection_io_cause |= H5D_SEL_IO_NOT_CONTIGUOUS_OR_CHUNKED_DATASET;
318
319
0
    FUNC_LEAVE_NOAPI(SUCCEED)
320
0
} /* end H5D__compact_io_init() */
321
322
/*-------------------------------------------------------------------------
323
 * Function:    H5D__compact_iovv_memmanage_cb
324
 *
325
 * Purpose:     Callback operator for H5D__compact_readvv()/_writevv() to
326
 *              send a memory copy request to the underlying file driver.
327
 *
328
 * Return:      Non-negative on success/Negative on failure
329
 *
330
 *-------------------------------------------------------------------------
331
 */
332
static herr_t
333
H5D__compact_iovv_memmanage_cb(hsize_t dst_off, hsize_t src_off, size_t len, void *_udata)
334
0
{
335
0
    H5D_compact_iovv_memmanage_ud_t *udata = (H5D_compact_iovv_memmanage_ud_t *)_udata;
336
0
    H5FD_ctl_memcpy_args_t           op_args;
337
0
    uint64_t                         op_flags;
338
0
    H5FD_t                          *file_handle = NULL;
339
0
    herr_t                           ret_value   = SUCCEED;
340
341
0
    FUNC_ENTER_PACKAGE
342
343
    /* Retrieve pointer to file driver structure for ctl call */
344
0
    if (H5F_shared_get_file_driver(udata->f_sh, &file_handle) < 0)
345
0
        HGOTO_ERROR(H5E_IO, H5E_CANTGET, FAIL, "can't get file handle");
346
347
    /* Setup operation flags and arguments */
348
0
    op_flags = H5FD_CTL_ROUTE_TO_TERMINAL_VFD_FLAG | H5FD_CTL_FAIL_IF_UNKNOWN_FLAG;
349
350
0
    op_args.dstbuf  = udata->dstbuf;
351
0
    op_args.dst_off = dst_off;
352
0
    op_args.srcbuf  = udata->srcbuf;
353
0
    op_args.src_off = src_off;
354
0
    op_args.len     = len;
355
356
    /* Make request to file driver */
357
0
    if (H5FD_ctl(file_handle, H5FD_CTL_MEM_COPY, op_flags, &op_args, NULL) < 0)
358
0
        HGOTO_ERROR(H5E_IO, H5E_FCNTL, FAIL, "VFD memcpy request failed");
359
360
0
done:
361
0
    FUNC_LEAVE_NOAPI(ret_value)
362
0
} /* end H5D__compact_iovv_memmanage_cb() */
363
364
/*-------------------------------------------------------------------------
365
 * Function:    H5D__compact_readvv
366
 *
367
 * Purpose:     Reads some data vectors from a dataset into a buffer.
368
 *              The data is in compact dataset.  The address is relative
369
 *              to the beginning address of the dataset.  The offsets and
370
 *              sequence lengths are in bytes.
371
 *
372
 * Return:      Non-negative on success/Negative on failure
373
 *
374
 * Notes:
375
 *              Offsets in the sequences must be monotonically increasing
376
 *
377
 *-------------------------------------------------------------------------
378
 */
379
static ssize_t
380
H5D__compact_readvv(const H5D_io_info_t *io_info, const H5D_dset_io_info_t *dset_info, size_t dset_max_nseq,
381
                    size_t *dset_curr_seq, size_t dset_size_arr[], hsize_t dset_offset_arr[],
382
                    size_t mem_max_nseq, size_t *mem_curr_seq, size_t mem_size_arr[],
383
                    hsize_t mem_offset_arr[])
384
0
{
385
0
    ssize_t ret_value = -1; /* Return value */
386
387
0
    FUNC_ENTER_PACKAGE
388
389
0
    assert(io_info);
390
0
    assert(dset_info);
391
392
    /* Check if file driver wishes to do its own memory management */
393
0
    if (H5F_SHARED_HAS_FEATURE(io_info->f_sh, H5FD_FEAT_MEMMANAGE)) {
394
0
        H5D_compact_iovv_memmanage_ud_t udata;
395
396
        /* Set up udata for memory copy operation */
397
0
        udata.f_sh   = io_info->f_sh;
398
0
        udata.dstbuf = dset_info->buf.vp;
399
0
        udata.srcbuf = dset_info->store->compact.buf;
400
401
        /* Request that file driver does the memory copy */
402
0
        if ((ret_value = H5VM_opvv(mem_max_nseq, mem_curr_seq, mem_size_arr, mem_offset_arr, dset_max_nseq,
403
0
                                   dset_curr_seq, dset_size_arr, dset_offset_arr,
404
0
                                   H5D__compact_iovv_memmanage_cb, &udata)) < 0)
405
0
            HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "vectorized memcpy failed");
406
0
    }
407
0
    else {
408
        /* Use the vectorized memory copy routine to do actual work */
409
0
        if ((ret_value = H5VM_memcpyvv(dset_info->buf.vp, mem_max_nseq, mem_curr_seq, mem_size_arr,
410
0
                                       mem_offset_arr, dset_info->store->compact.buf, dset_max_nseq,
411
0
                                       dset_curr_seq, dset_size_arr, dset_offset_arr)) < 0)
412
0
            HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "vectorized memcpy failed");
413
0
    }
414
415
0
done:
416
0
    FUNC_LEAVE_NOAPI(ret_value)
417
0
} /* end H5D__compact_readvv() */
418
419
/*-------------------------------------------------------------------------
420
 * Function:    H5D__compact_writevv
421
 *
422
 * Purpose:     Writes some data vectors from a dataset into a buffer.
423
 *              The data is in compact dataset.  The address is relative
424
 *              to the beginning address for the file.  The offsets and
425
 *              sequence lengths are in bytes.  This function only copies
426
 *              data into the buffer in the LAYOUT struct and mark it
427
 *              as DIRTY.  Later in H5D_close, the data is copied into
428
 *              header message in memory.
429
 *
430
 * Return:      Non-negative on success/Negative on failure
431
 *
432
 * Notes:
433
 *              Offsets in the sequences must be monotonically increasing
434
 *
435
 *-------------------------------------------------------------------------
436
 */
437
static ssize_t
438
H5D__compact_writevv(const H5D_io_info_t *io_info, const H5D_dset_io_info_t *dset_info, size_t dset_max_nseq,
439
                     size_t *dset_curr_seq, size_t dset_size_arr[], hsize_t dset_offset_arr[],
440
                     size_t mem_max_nseq, size_t *mem_curr_seq, size_t mem_size_arr[],
441
                     hsize_t mem_offset_arr[])
442
0
{
443
0
    ssize_t ret_value = -1; /* Return value */
444
445
0
    FUNC_ENTER_PACKAGE
446
447
0
    assert(io_info);
448
0
    assert(dset_info);
449
450
    /* Check if file driver wishes to do its own memory management */
451
0
    if (H5F_SHARED_HAS_FEATURE(io_info->f_sh, H5FD_FEAT_MEMMANAGE)) {
452
0
        H5D_compact_iovv_memmanage_ud_t udata;
453
454
        /* Set up udata for memory copy operation */
455
0
        udata.f_sh   = io_info->f_sh;
456
0
        udata.dstbuf = dset_info->store->compact.buf;
457
0
        udata.srcbuf = dset_info->buf.cvp;
458
459
        /* Request that file driver does the memory copy */
460
0
        if ((ret_value = H5VM_opvv(dset_max_nseq, dset_curr_seq, dset_size_arr, dset_offset_arr, mem_max_nseq,
461
0
                                   mem_curr_seq, mem_size_arr, mem_offset_arr, H5D__compact_iovv_memmanage_cb,
462
0
                                   &udata)) < 0)
463
0
            HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "vectorized memcpy failed");
464
0
    }
465
0
    else {
466
        /* Use the vectorized memory copy routine to do actual work */
467
0
        if ((ret_value = H5VM_memcpyvv(dset_info->store->compact.buf, dset_max_nseq, dset_curr_seq,
468
0
                                       dset_size_arr, dset_offset_arr, dset_info->buf.cvp, mem_max_nseq,
469
0
                                       mem_curr_seq, mem_size_arr, mem_offset_arr)) < 0)
470
0
            HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "vectorized memcpy failed");
471
0
    }
472
473
    /* Mark the compact dataset's buffer as dirty */
474
0
    *dset_info->store->compact.dirty = true;
475
476
0
done:
477
0
    FUNC_LEAVE_NOAPI(ret_value)
478
0
} /* end H5D__compact_writevv() */
479
480
/*-------------------------------------------------------------------------
481
 * Function:  H5D__compact_flush
482
 *
483
 * Purpose: Writes dirty compact data to object header
484
 *
485
 * Return:  Non-negative on success/Negative on failure
486
 *
487
 *-------------------------------------------------------------------------
488
 */
489
static herr_t
490
H5D__compact_flush(H5D_t *dset)
491
13
{
492
13
    herr_t ret_value = SUCCEED; /* Return value */
493
494
13
    FUNC_ENTER_PACKAGE
495
496
    /* Sanity check */
497
13
    assert(dset);
498
499
    /* Check if the buffered compact information is dirty */
500
13
    if (dset->shared->layout.storage.u.compact.dirty) {
501
0
        dset->shared->layout.storage.u.compact.dirty = false;
502
0
        if (H5O_msg_write(&(dset->oloc), H5O_LAYOUT_ID, 0, H5O_UPDATE_TIME, &(dset->shared->layout)) < 0) {
503
0
            dset->shared->layout.storage.u.compact.dirty = true;
504
0
            HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to update layout message");
505
0
        }
506
0
    } /* end if */
507
508
13
done:
509
13
    FUNC_LEAVE_NOAPI(ret_value)
510
13
} /* end H5D__compact_flush() */
511
512
/*-------------------------------------------------------------------------
513
 * Function:  H5D__compact_dest
514
 *
515
 * Purpose: Free the compact buffer
516
 *
517
 * Return:  Non-negative on success/Negative on failure
518
 *
519
 *-------------------------------------------------------------------------
520
 */
521
static herr_t
522
H5D__compact_dest(H5D_t *dset)
523
17
{
524
17
    FUNC_ENTER_PACKAGE_NOERR
525
526
    /* Sanity check */
527
17
    assert(dset);
528
529
    /* Free the buffer for the raw data for compact datasets */
530
17
    dset->shared->layout.storage.u.compact.buf = H5MM_xfree(dset->shared->layout.storage.u.compact.buf);
531
532
17
    FUNC_LEAVE_NOAPI(SUCCEED)
533
17
} /* end H5D__compact_dest() */
534
535
/*-------------------------------------------------------------------------
536
 * Function:    H5D__compact_copy
537
 *
538
 * Purpose:     Copy compact storage raw data from SRC file to DST file.
539
 *
540
 * Return:      Non-negative on success, negative on failure.
541
 *
542
 *-------------------------------------------------------------------------
543
 */
544
herr_t
545
H5D__compact_copy(H5F_t *f_src, H5O_storage_compact_t *_storage_src, H5F_t *f_dst,
546
                  H5O_storage_compact_t *storage_dst, H5T_t *dt_src, H5O_copy_t *cpy_info)
547
0
{
548
0
    H5T_t        *dt_mem      = NULL; /* Memory datatype */
549
0
    H5T_t        *dt_dst      = NULL; /* Destination datatype */
550
0
    H5S_t        *buf_space   = NULL; /* Dataspace describing buffer */
551
0
    void         *buf         = NULL; /* Buffer for copying data */
552
0
    void         *bkg         = NULL; /* Temporary buffer for copying data */
553
0
    void         *reclaim_buf = NULL; /* Buffer for reclaiming data */
554
0
    H5D_shared_t *shared_fo =
555
0
        (H5D_shared_t *)cpy_info->shared_fo;           /* Pointer to the shared struct for dataset object */
556
0
    H5O_storage_compact_t *storage_src = _storage_src; /* Pointer to storage_src */
557
0
    herr_t                 ret_value   = SUCCEED;      /* Return value */
558
559
0
    FUNC_ENTER_PACKAGE
560
561
    /* Check args */
562
0
    assert(f_src);
563
0
    assert(storage_src);
564
0
    assert(f_dst);
565
0
    assert(storage_dst);
566
0
    assert(storage_dst->buf);
567
0
    assert(dt_src);
568
569
    /* If the dataset is open in the file, point to "layout" in the shared struct */
570
0
    if (shared_fo != NULL)
571
0
        storage_src = &(shared_fo->layout.storage.u.compact);
572
573
    /* If there's a VLEN source datatype, do type conversion information */
574
0
    if (H5T_detect_class(dt_src, H5T_VLEN, false) > 0) {
575
0
        H5T_path_t *tpath_src_mem, *tpath_mem_dst; /* Datatype conversion paths */
576
0
        size_t      buf_size;                      /* Size of copy buffer */
577
0
        size_t      nelmts;                        /* Number of elements in buffer */
578
0
        size_t      src_dt_size;                   /* Source datatype size */
579
0
        size_t      tmp_dt_size;                   /* Temporary datatype size */
580
0
        size_t      max_dt_size;                   /* Max atatype size */
581
0
        hsize_t     buf_dim;                       /* Dimension for buffer */
582
583
        /* create a memory copy of the variable-length datatype */
584
0
        if (NULL == (dt_mem = H5T_copy(dt_src, H5T_COPY_TRANSIENT)))
585
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to copy");
586
587
        /* create variable-length datatype at the destination file */
588
0
        if (NULL == (dt_dst = H5T_copy(dt_src, H5T_COPY_TRANSIENT)))
589
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to copy");
590
0
        if (H5T_set_loc(dt_dst, H5F_VOL_OBJ(f_dst), H5T_LOC_DISK) < 0) {
591
0
            (void)H5T_close_real(dt_dst);
592
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "cannot mark datatype on disk");
593
0
        } /* end if */
594
595
        /* Set up the conversion functions */
596
0
        if (NULL == (tpath_src_mem = H5T_path_find(dt_src, dt_mem)))
597
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "unable to convert between src and mem datatypes");
598
0
        if (NULL == (tpath_mem_dst = H5T_path_find(dt_mem, dt_dst)))
599
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "unable to convert between mem and dst datatypes");
600
601
        /* Determine largest datatype size */
602
0
        if (0 == (src_dt_size = H5T_get_size(dt_src)))
603
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to determine datatype size");
604
0
        if (0 == (tmp_dt_size = H5T_get_size(dt_mem)))
605
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to determine datatype size");
606
0
        max_dt_size = MAX(src_dt_size, tmp_dt_size);
607
0
        if (0 == (tmp_dt_size = H5T_get_size(dt_dst)))
608
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to determine datatype size");
609
0
        max_dt_size = MAX(max_dt_size, tmp_dt_size);
610
611
        /* Set number of whole elements that fit in buffer */
612
0
        if (0 == (nelmts = storage_src->size / src_dt_size))
613
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "element size too large");
614
615
        /* Set up number of bytes to copy, and initial buffer size */
616
0
        buf_size = nelmts * max_dt_size;
617
618
        /* Create dataspace for number of elements in buffer */
619
0
        buf_dim = nelmts;
620
621
        /* Create the space and set the initial extent */
622
0
        if (NULL == (buf_space = H5S_create_simple((unsigned)1, &buf_dim, NULL)))
623
0
            HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCREATE, FAIL, "can't create simple dataspace");
624
625
        /* Allocate memory for recclaim buf */
626
0
        if (NULL == (reclaim_buf = H5FL_BLK_MALLOC(type_conv, buf_size)))
627
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
628
629
        /* Allocate memory for copying the chunk */
630
0
        if (NULL == (buf = H5FL_BLK_MALLOC(type_conv, buf_size)))
631
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
632
633
0
        H5MM_memcpy(buf, storage_src->buf, storage_src->size);
634
635
        /* allocate temporary bkg buff for data conversion */
636
0
        if (NULL == (bkg = H5FL_BLK_MALLOC(type_conv, buf_size)))
637
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
638
639
        /* Convert from source file to memory */
640
0
        if (H5T_convert(tpath_src_mem, dt_src, dt_mem, nelmts, (size_t)0, (size_t)0, buf, bkg) < 0)
641
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCONVERT, FAIL, "datatype conversion failed");
642
643
        /* Copy into another buffer, to reclaim memory later */
644
0
        H5MM_memcpy(reclaim_buf, buf, buf_size);
645
646
        /* Set background buffer to all zeros */
647
0
        memset(bkg, 0, buf_size);
648
649
        /* Convert from memory to destination file */
650
0
        if (H5T_convert(tpath_mem_dst, dt_mem, dt_dst, nelmts, (size_t)0, (size_t)0, buf, bkg) < 0)
651
0
            HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCONVERT, FAIL, "datatype conversion failed");
652
653
0
        H5MM_memcpy(storage_dst->buf, buf, storage_dst->size);
654
655
0
        if (H5T_reclaim(dt_mem, buf_space, reclaim_buf) < 0)
656
0
            HGOTO_ERROR(H5E_DATASET, H5E_CANTFREE, FAIL, "unable to reclaim variable-length data");
657
0
    } /* end if */
658
0
    else if (H5T_get_class(dt_src, false) == H5T_REFERENCE) {
659
0
        if (f_src != f_dst) {
660
            /* Check for expanding references */
661
0
            if (cpy_info->expand_ref) {
662
                /* Copy objects referenced in source buffer to destination file and set destination elements
663
                 */
664
0
                if (H5O_copy_expand_ref(f_src, dt_src, storage_src->buf, storage_src->size, f_dst,
665
0
                                        storage_dst->buf, cpy_info) < 0)
666
0
                    HGOTO_ERROR(H5E_DATASET, H5E_CANTCOPY, FAIL, "unable to copy reference attribute");
667
0
            } /* end if */
668
0
            else
669
                /* Reset value to zero */
670
0
                memset(storage_dst->buf, 0, storage_src->size);
671
0
        } /* end if */
672
0
        else
673
            /* Type conversion not necessary */
674
0
            H5MM_memcpy(storage_dst->buf, storage_src->buf, storage_src->size);
675
0
    } /* end if */
676
0
    else
677
        /* Type conversion not necessary */
678
0
        H5MM_memcpy(storage_dst->buf, storage_src->buf, storage_src->size);
679
680
    /* Mark destination buffer as dirty */
681
0
    storage_dst->dirty = true;
682
683
0
done:
684
0
    if (dt_dst && (H5T_close(dt_dst) < 0))
685
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTCLOSEOBJ, FAIL, "can't close temporary datatype");
686
0
    if (dt_mem && (H5T_close(dt_mem) < 0))
687
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTCLOSEOBJ, FAIL, "can't close temporary datatype");
688
0
    if (buf_space && H5S_close(buf_space) < 0)
689
0
        HDONE_ERROR(H5E_DATASET, H5E_CANTCLOSEOBJ, FAIL, "can't close temporary dataspace");
690
0
    if (buf)
691
0
        buf = H5FL_BLK_FREE(type_conv, buf);
692
0
    if (reclaim_buf)
693
0
        reclaim_buf = H5FL_BLK_FREE(type_conv, reclaim_buf);
694
0
    if (bkg)
695
0
        bkg = H5FL_BLK_FREE(type_conv, bkg);
696
697
0
    FUNC_LEAVE_NOAPI(ret_value)
698
0
} /* end H5D__compact_copy() */