Coverage Report

Created: 2026-11-22 16:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5FDspace.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
 *
15
 * Created:   H5FDspace.c
16
 *
17
 * Purpose:   Space allocation routines for the file driver code.
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5FDmodule.h" /* This source code file is part of the H5FD module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions     */
32
#include "H5CXprivate.h" /* API Contexts                         */
33
#include "H5Eprivate.h"  /* Error handling        */
34
#include "H5Fprivate.h"  /* File access       */
35
#include "H5FDpkg.h"     /* File Drivers        */
36
#include "H5FDmulti.h"   /* Usage-partitioned file family */
37
#include "H5FLprivate.h" /* Free lists                               */
38
39
/****************/
40
/* Local Macros */
41
/****************/
42
43
/* Define this to display information about file allocations */
44
/* #define H5FD_ALLOC_DEBUG */
45
46
/******************/
47
/* Local Typedefs */
48
/******************/
49
50
/********************/
51
/* Package Typedefs */
52
/********************/
53
54
/********************/
55
/* Local Prototypes */
56
/********************/
57
58
/*********************/
59
/* Package Variables */
60
/*********************/
61
62
/*****************************/
63
/* Library Private Variables */
64
/*****************************/
65
66
/*******************/
67
/* Local Variables */
68
/*******************/
69
70
/* Declare a free list to manage the H5FD_free_t struct */
71
H5FL_DEFINE(H5FD_free_t);
72
73
/*-------------------------------------------------------------------------
74
 * Function:    H5FD__extend
75
 *
76
 * Purpose:     Extend the EOA space of a file.
77
 *
78
 * NOTE:        Returns absolute file offset
79
 *
80
 * Return:      Success:    The address of the previous EOA.
81
 *              Failure:    The undefined address HADDR_UNDEF
82
 *
83
 *-------------------------------------------------------------------------
84
 */
85
static haddr_t
86
H5FD__extend(H5FD_t *file, H5FD_mem_t type, hsize_t size)
87
0
{
88
0
    haddr_t eoa;                     /* Address of end-of-allocated space */
89
0
    herr_t  status;                  /* Generic status return */
90
0
    haddr_t ret_value = HADDR_UNDEF; /* Return value */
91
92
0
    FUNC_ENTER_PACKAGE
93
94
    /* check args */
95
0
    assert(file);
96
0
    assert(file->cls);
97
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
98
0
    assert(size > 0);
99
100
    /* Prepare & restore library for user callback */
101
0
    H5_BEFORE_USER_CB(HADDR_UNDEF)
102
0
        {
103
            /* Get current end-of-allocated space address */
104
0
            eoa = (file->cls->get_eoa)(file, type);
105
0
        }
106
0
    H5_AFTER_USER_CB(HADDR_UNDEF)
107
108
    /* Check for overflow when extending */
109
0
    if (H5_addr_overflow(eoa, size) || (eoa + size) > file->maxaddr)
110
0
        HGOTO_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF, "file allocation request failed");
111
112
    /* Set the [NOT aligned] address to return */
113
0
    ret_value = eoa;
114
115
    /* Prepare & restore library for user callback */
116
0
    H5_BEFORE_USER_CB(HADDR_UNDEF)
117
0
        {
118
            /* Extend the end-of-allocated space address */
119
0
            eoa += size;
120
0
            status = (file->cls->set_eoa)(file, type, eoa);
121
0
        }
122
0
    H5_AFTER_USER_CB(HADDR_UNDEF)
123
0
    if (status < 0)
124
0
        HGOTO_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF, "file allocation request failed");
125
126
0
done:
127
0
    FUNC_LEAVE_NOAPI(ret_value)
128
0
} /* end H5FD__extend() */
129
130
/*-------------------------------------------------------------------------
131
 * Function:    H5FD__alloc_real
132
 *
133
 * Purpose:     Allocate space in the file with the VFD
134
 *              Note: the handling of alignment is moved up from each driver to
135
 *              this routine.
136
 *
137
 * Return:      Success:    The format address of the new file memory.
138
 *              Failure:    The undefined address HADDR_UNDEF
139
 *
140
 *-------------------------------------------------------------------------
141
 */
142
haddr_t
143
H5FD__alloc_real(H5FD_t *file, H5FD_mem_t type, hsize_t size, haddr_t *frag_addr, hsize_t *frag_size)
144
0
{
145
0
    hsize_t       orig_size = size;        /* Original allocation size */
146
0
    haddr_t       eoa;                     /* Address of end-of-allocated space */
147
0
    hsize_t       extra;                   /* Extra space to allocate, to align request */
148
0
    unsigned long flags = 0;               /* Driver feature flags */
149
0
    bool          use_alloc_size;          /* Just pass alloc size to the driver */
150
0
    herr_t        status;                  /* Generic status return */
151
0
    haddr_t       ret_value = HADDR_UNDEF; /* Return value */
152
153
0
    FUNC_ENTER_PACKAGE
154
#ifdef H5FD_ALLOC_DEBUG
155
    fprintf(stderr, "%s: type = %u, size = %" PRIuHSIZE "\n", __func__, (unsigned)type, size);
156
#endif /* H5FD_ALLOC_DEBUG */
157
158
    /* check args */
159
0
    assert(file);
160
0
    assert(file->cls);
161
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
162
0
    assert(size > 0);
163
164
    /* Check for query driver and call it */
165
0
    if (file->cls->query) {
166
        /* Prepare & restore library for user callback */
167
0
        H5_BEFORE_USER_CB(HADDR_UNDEF)
168
0
            {
169
0
                status = (file->cls->query)(file, &flags);
170
0
            }
171
0
        H5_AFTER_USER_CB(HADDR_UNDEF)
172
0
        if (status < 0)
173
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTGET, HADDR_UNDEF, "driver query request failed");
174
0
    }
175
176
    /* Check for the driver feature flag */
177
0
    use_alloc_size = flags & H5FD_FEAT_USE_ALLOC_SIZE;
178
179
    /* Prepare & restore library for user callback */
180
0
    H5_BEFORE_USER_CB(HADDR_UNDEF)
181
0
        {
182
            /* Get current end-of-allocated space address */
183
0
            eoa = (file->cls->get_eoa)(file, type);
184
0
        }
185
0
    H5_AFTER_USER_CB(HADDR_UNDEF)
186
187
    /* Compute extra space to allocate, if this should be aligned */
188
0
    extra = 0;
189
0
    if (!file->paged_aggr && file->alignment > 1 && orig_size >= file->threshold) {
190
0
        hsize_t mis_align; /* Amount EOA is misaligned */
191
192
        /* Check for EOA already aligned */
193
0
        if ((mis_align = (eoa % file->alignment)) > 0) {
194
0
            extra = file->alignment - mis_align;
195
0
            if (frag_addr)
196
0
                *frag_addr = eoa - file->base_addr; /* adjust for file's base address */
197
0
            if (frag_size)
198
0
                *frag_size = extra;
199
0
        } /* end if */
200
0
    }     /* end if */
201
202
    /* Dispatch to driver `alloc' callback or extend the end-of-address marker */
203
    /* For the multi/split driver: the size passed down to the alloc callback is the original size from
204
     * H5FD_alloc() */
205
    /* For all other drivers: the size passed down to the alloc callback is the size + [possibly] alignment
206
     * size */
207
0
    if (file->cls->alloc) {
208
        /* Prepare & restore library for user callback */
209
0
        H5_BEFORE_USER_CB(HADDR_UNDEF)
210
0
            {
211
0
                ret_value =
212
0
                    (file->cls->alloc)(file, type, H5CX_get_dxpl(), use_alloc_size ? size : size + extra);
213
0
            }
214
0
        H5_AFTER_USER_CB(HADDR_UNDEF)
215
0
        if (!H5_addr_defined(ret_value))
216
0
            HGOTO_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF, "driver allocation request failed");
217
0
    } /* end if */
218
0
    else {
219
0
        ret_value = H5FD__extend(file, type, size + extra);
220
0
        if (!H5_addr_defined(ret_value))
221
0
            HGOTO_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF, "driver eoa update request failed");
222
0
    } /* end else */
223
224
    /* Set the [possibly aligned] address to return */
225
0
    if (!use_alloc_size)
226
0
        ret_value += extra;
227
228
    /* Post-condition sanity check */
229
0
    if (!file->paged_aggr && file->alignment > 1 && orig_size >= file->threshold)
230
0
        assert(!(ret_value % file->alignment));
231
232
    /* Convert absolute file offset to relative address */
233
0
    ret_value -= file->base_addr;
234
235
0
done:
236
#ifdef H5FD_ALLOC_DEBUG
237
    fprintf(stderr, "%s: ret_value = %" PRIuHADDR "\n", __func__, ret_value);
238
#endif /* H5FD_ALLOC_DEBUG */
239
0
    FUNC_LEAVE_NOAPI(ret_value)
240
0
} /* end H5FD__alloc_real() */
241
242
/*-------------------------------------------------------------------------
243
 * Function:    H5FD_alloc
244
 *
245
 * Purpose:     Wrapper for H5FD_alloc, to make certain EOA changes are
246
 *    reflected in superblock.
247
 *
248
 * Note:  When the metadata cache routines are updated to allow
249
 *    marking an entry dirty without a H5F_t*, this routine should
250
 *    be changed to take a H5F_super_t* directly.
251
 *
252
 * Return:      Success:    The format address of the new file memory.
253
 *              Failure:    The undefined address HADDR_UNDEF
254
 *
255
 *-------------------------------------------------------------------------
256
 */
257
haddr_t
258
H5FD_alloc(H5FD_t *file, H5FD_mem_t type, H5F_t *f, hsize_t size, haddr_t *frag_addr, hsize_t *frag_size)
259
0
{
260
0
    haddr_t ret_value = HADDR_UNDEF; /* Return value */
261
262
0
    FUNC_ENTER_NOAPI(HADDR_UNDEF)
263
264
    /* check args */
265
0
    assert(file);
266
0
    assert(file->cls);
267
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
268
0
    assert(size > 0);
269
270
    /* Call the real 'alloc' routine */
271
0
    ret_value = H5FD__alloc_real(file, type, size, frag_addr, frag_size);
272
0
    if (!H5_addr_defined(ret_value))
273
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, HADDR_UNDEF, "real 'alloc' request failed");
274
275
    /* Mark EOA info dirty in cache, so change will get encoded */
276
0
    if (H5F_eoa_dirty(f) < 0)
277
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTMARKDIRTY, HADDR_UNDEF, "unable to mark EOA info as dirty");
278
279
0
done:
280
0
    FUNC_LEAVE_NOAPI(ret_value)
281
0
} /* end H5FD_alloc() */
282
283
/*-------------------------------------------------------------------------
284
 * Function:    H5FD__free_real
285
 *
286
 * Purpose:     Release space back to the VFD
287
 *
288
 * Return:      Success:        Non-negative
289
 *              Failure:        Negative
290
 *
291
 *-------------------------------------------------------------------------
292
 */
293
herr_t
294
H5FD__free_real(H5FD_t *file, H5FD_mem_t type, haddr_t addr, hsize_t size)
295
0
{
296
0
    herr_t ret_value = SUCCEED; /* Return value */
297
298
0
    FUNC_ENTER_PACKAGE
299
300
    /* Check args */
301
0
    assert(file);
302
0
    assert(file->cls);
303
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
304
0
    assert(size > 0);
305
306
#ifdef H5FD_ALLOC_DEBUG
307
    fprintf(stderr, "%s: type = %u, addr = %" PRIuHADDR ", size = %" PRIuHSIZE "\n", __func__, (unsigned)type,
308
            addr, size);
309
#endif /* H5FD_ALLOC_DEBUG */
310
311
    /* Sanity checking */
312
0
    if (!H5_addr_defined(addr))
313
0
        HGOTO_ERROR(H5E_VFL, H5E_BADVALUE, FAIL, "invalid file offset");
314
315
    /* Convert address to absolute file offset */
316
0
    addr += file->base_addr;
317
318
    /* More sanity checking */
319
0
    if (addr > file->maxaddr || H5_addr_overflow(addr, size) || (addr + size) > file->maxaddr)
320
0
        HGOTO_ERROR(H5E_VFL, H5E_BADVALUE, FAIL, "invalid file free space region to free");
321
322
    /* Check for file driver 'free' callback and call it if available */
323
0
    if (file->cls->free) {
324
#ifdef H5FD_ALLOC_DEBUG
325
        fprintf(stderr, "%s: Letting VFD free space\n", __func__);
326
#endif /* H5FD_ALLOC_DEBUG */
327
        /* Prepare & restore library for user callback */
328
0
        H5_BEFORE_USER_CB(FAIL)
329
0
            {
330
                /* Dispatch to driver */
331
0
                ret_value = (file->cls->free)(file, type, H5CX_get_dxpl(), addr, size);
332
0
            }
333
0
        H5_AFTER_USER_CB(FAIL)
334
0
        if (ret_value < 0)
335
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTFREE, FAIL, "driver free request failed");
336
0
    } /* end if */
337
    /* Check if this free block is at the end of file allocated space.
338
     * Truncate it if this is true.
339
     */
340
0
    else if (file->cls->get_eoa) {
341
0
        haddr_t eoa;
342
343
        /* Prepare & restore library for user callback */
344
0
        H5_BEFORE_USER_CB(FAIL)
345
0
            {
346
                /* Dispatch to driver */
347
0
                eoa = (file->cls->get_eoa)(file, type);
348
0
            }
349
0
        H5_AFTER_USER_CB(FAIL)
350
#ifdef H5FD_ALLOC_DEBUG
351
        fprintf(stderr, "%s: eoa = %" PRIuHADDR "\n", __func__, eoa);
352
#endif /* H5FD_ALLOC_DEBUG */
353
0
        if (eoa == (addr + size)) {
354
#ifdef H5FD_ALLOC_DEBUG
355
            fprintf(stderr, "%s: Reducing file size to = %" PRIuHADDR "\n", __func__, addr);
356
#endif /* H5FD_ALLOC_DEBUG */
357
            /* Prepare & restore library for user callback */
358
0
            H5_BEFORE_USER_CB(FAIL)
359
0
                {
360
                    /* Dispatch to driver */
361
0
                    ret_value = (file->cls->set_eoa)(file, type, addr);
362
0
                }
363
0
            H5_AFTER_USER_CB(FAIL)
364
0
            if (ret_value < 0)
365
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTSET, FAIL, "set end of space allocation request failed");
366
0
        } /* end if */
367
0
    }     /* end else-if */
368
0
    else {
369
        /* leak memory */
370
#ifdef H5FD_ALLOC_DEBUG
371
        fprintf(stderr, "%s: LEAKED MEMORY!!! type = %u, addr = %" PRIuHADDR ", size = %" PRIuHSIZE "\n",
372
                __func__, (unsigned)type, addr, size);
373
#endif /* H5FD_ALLOC_DEBUG */
374
0
    }  /* end else */
375
376
0
done:
377
0
    FUNC_LEAVE_NOAPI(ret_value)
378
0
} /* end H5FD__free_real() */
379
380
/*-------------------------------------------------------------------------
381
 * Function:    H5FD_free
382
 *
383
 * Purpose:     Wrapper for H5FD__free_real, to make certain EOA changes are
384
 *    reflected in superblock.
385
 *
386
 * Note:  When the metadata cache routines are updated to allow
387
 *    marking an entry dirty without a H5F_t*, this routine should
388
 *    be changed to take a H5F_super_t* directly.
389
 *
390
 * Return:      Success:        Non-negative
391
 *              Failure:        Negative
392
 *
393
 *-------------------------------------------------------------------------
394
 */
395
herr_t
396
H5FD_free(H5FD_t *file, H5FD_mem_t type, H5F_t *f, haddr_t addr, hsize_t size)
397
0
{
398
0
    herr_t ret_value = SUCCEED; /* Return value */
399
400
0
    FUNC_ENTER_NOAPI(FAIL)
401
402
    /* Check args */
403
0
    assert(file);
404
0
    assert(file->cls);
405
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
406
0
    assert(size > 0);
407
408
    /* Call the real 'free' routine */
409
0
    if (H5FD__free_real(file, type, addr, size) < 0)
410
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTFREE, FAIL, "real 'free' request failed");
411
412
    /* Mark EOA info dirty in cache, so change will get encoded */
413
0
    if (H5F_eoa_dirty(f) < 0)
414
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTMARKDIRTY, FAIL, "unable to mark EOA info as dirty");
415
416
0
done:
417
0
    FUNC_LEAVE_NOAPI(ret_value)
418
0
} /* end H5FD_free() */
419
420
/*-------------------------------------------------------------------------
421
 * Function:    H5FD_try_extend
422
 *
423
 * Purpose: Extend a block at the end of the file, if possible.
424
 *
425
 * Note:  When the metadata cache routines are updated to allow
426
 *    marking an entry dirty without a H5F_t*, this routine should
427
 *    be changed to take a H5F_super_t* directly.
428
 *
429
 * Return:  Success:  true(1)  - Block was extended
430
 *                              false(0) - Block could not be extended
431
 *    Failure:  FAIL
432
 *
433
 *-------------------------------------------------------------------------
434
 */
435
htri_t
436
H5FD_try_extend(H5FD_t *file, H5FD_mem_t type, H5F_t *f, haddr_t blk_end, hsize_t extra_requested)
437
0
{
438
0
    haddr_t eoa;               /* End of allocated space in file */
439
0
    htri_t  ret_value = false; /* Return value */
440
441
0
    FUNC_ENTER_NOAPI(FAIL)
442
443
    /* check args */
444
0
    assert(file);
445
0
    assert(file->cls);
446
0
    assert(type >= H5FD_MEM_DEFAULT && type < H5FD_MEM_NTYPES);
447
0
    assert(extra_requested > 0);
448
0
    assert(f);
449
450
    /* Prepare & restore library for user callback */
451
0
    H5_BEFORE_USER_CB(FAIL)
452
0
        {
453
            /* Retrieve the end of the address space */
454
0
            eoa = (file->cls->get_eoa)(file, type);
455
0
        }
456
0
    H5_AFTER_USER_CB(FAIL)
457
0
    if (!H5_addr_defined(eoa))
458
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTGET, FAIL, "driver get_eoa request failed");
459
460
    /* Adjust block end by base address of the file, to create absolute address */
461
0
    blk_end += file->base_addr;
462
463
    /* Check if the block is exactly at the end of the file */
464
0
    if (H5_addr_eq(blk_end, eoa)) {
465
        /* Extend the object by extending the underlying file */
466
0
        if (HADDR_UNDEF == H5FD__extend(file, type, extra_requested))
467
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTEXTEND, FAIL, "driver extend request failed");
468
469
        /* Mark EOA info dirty in cache, so change will get encoded */
470
0
        if (H5F_eoa_dirty(f) < 0)
471
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTMARKDIRTY, FAIL, "unable to mark EOA info as dirty");
472
473
        /* Indicate success */
474
0
        HGOTO_DONE(true);
475
0
    } /* end if */
476
477
0
done:
478
0
    FUNC_LEAVE_NOAPI(ret_value)
479
0
} /* end H5FD_try_extend() */