Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5HFiter.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:   H5HFiter.c
16
 *
17
 * Purpose:   Block iteration routines for fractal heaps.
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5HFmodule.h" /* This source code file is part of the H5HF module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions     */
32
#include "H5Eprivate.h"  /* Error handling        */
33
#include "H5FLprivate.h" /* Free Lists                               */
34
#include "H5HFpkg.h"     /* Fractal heaps     */
35
#include "H5VMprivate.h" /* Vectors and arrays      */
36
37
/****************/
38
/* Local Macros */
39
/****************/
40
41
/******************/
42
/* Local Typedefs */
43
/******************/
44
45
/********************/
46
/* Package Typedefs */
47
/********************/
48
49
/********************/
50
/* Local Prototypes */
51
/********************/
52
53
/*********************/
54
/* Package Variables */
55
/*********************/
56
57
/*****************************/
58
/* Library Private Variables */
59
/*****************************/
60
61
/*******************/
62
/* Local Variables */
63
/*******************/
64
65
/* Declare a free list to manage the H5HF_block_loc_t struct */
66
H5FL_DEFINE_STATIC(H5HF_block_loc_t);
67
68
/*-------------------------------------------------------------------------
69
 * Function:  H5HF__man_iter_init
70
 *
71
 * Purpose: Initialize a block iterator for walking over all the blocks
72
 *              in a fractal heap.  (initialization finishes when iterator is
73
 *              actually used)
74
 *
75
 * Return:  SUCCEED/FAIL
76
 *
77
 *-------------------------------------------------------------------------
78
 */
79
herr_t
80
H5HF__man_iter_init(H5HF_block_iter_t *biter)
81
0
{
82
0
    FUNC_ENTER_PACKAGE_NOERR
83
84
    /*
85
     * Check arguments.
86
     */
87
0
    assert(biter);
88
89
    /* Reset block iterator information */
90
0
    memset(biter, 0, sizeof(H5HF_block_iter_t));
91
92
0
    FUNC_LEAVE_NOAPI(SUCCEED)
93
0
} /* end H5HF__man_iter_init() */
94
95
/*-------------------------------------------------------------------------
96
 * Function:  H5HF__man_iter_start_offset
97
 *
98
 * Purpose: Initialize a block iterator to a particular location, given
99
 *              an offset in the heap
100
 *
101
 * Return:  SUCCEED/FAIL
102
 *
103
 *-------------------------------------------------------------------------
104
 */
105
herr_t
106
H5HF__man_iter_start_offset(H5HF_hdr_t *hdr, H5HF_block_iter_t *biter, hsize_t offset)
107
0
{
108
0
    H5HF_indirect_t *iblock;               /* Indirect block for location context */
109
0
    haddr_t          iblock_addr;          /* Address of indirect block */
110
0
    unsigned         iblock_nrows;         /* # of rows in indirect block */
111
0
    H5HF_indirect_t *iblock_parent;        /* Parent indirect block of location context */
112
0
    unsigned         iblock_par_entry;     /* Entry within parent indirect block */
113
0
    hsize_t          curr_offset;          /* Current offset, as adjusted */
114
0
    unsigned         row;                  /* Current row we are on */
115
0
    unsigned         col;                  /* Column in row */
116
0
    bool             root_block = true;    /* Flag to indicate the current block is the root indirect block */
117
0
    herr_t           ret_value  = SUCCEED; /* Return value */
118
119
0
    FUNC_ENTER_PACKAGE
120
121
    /*
122
     * Check arguments.
123
     */
124
0
    assert(biter);
125
0
    assert(!biter->ready);
126
127
    /* Check for empty heap */
128
0
    assert(offset >= hdr->man_dtable.cparam.start_block_size);
129
130
    /* Allocate level structure */
131
0
    if (NULL == (biter->curr = H5FL_MALLOC(H5HF_block_loc_t)))
132
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
133
0
                    "memory allocation failed for direct block free list section");
134
135
    /*
136
    1:  <Scan down block offsets for dtable rows until find a row >= offset>
137
        <Set current location's row, col, entry & size>
138
        <If row < max_direct_rows>
139
            <Done>
140
        <Else - row > max_direct_rows>
141
            <Create new block level>
142
            <Link new block level into iterator>
143
            <Adjust offset for block offset for row>
144
            <Make new block level the current context>
145
            <Goto 1>
146
147
    */
148
0
    do {
149
0
        bool did_protect; /* Whether we protected the indirect block or not */
150
151
        /* Walk down the rows in the doubling table until we've found the correct row for the next block */
152
0
        for (row = 0; row < hdr->man_dtable.max_root_rows; row++)
153
0
            if ((offset >= hdr->man_dtable.row_block_off[row]) &&
154
0
                (offset < hdr->man_dtable.row_block_off[row] +
155
0
                              (hdr->man_dtable.cparam.width * hdr->man_dtable.row_block_size[row])))
156
0
                break;
157
158
        /* Adjust offset by row offset */
159
0
        curr_offset = offset - hdr->man_dtable.row_block_off[row];
160
161
        /* Compute column */
162
0
        H5_CHECK_OVERFLOW((curr_offset / hdr->man_dtable.row_block_size[row]), hsize_t, unsigned);
163
0
        col = (unsigned)(curr_offset / hdr->man_dtable.row_block_size[row]);
164
165
        /* Set the current level's context */
166
0
        biter->curr->row   = row;
167
0
        biter->curr->col   = col;
168
0
        biter->curr->entry = (row * hdr->man_dtable.cparam.width) + col;
169
170
        /* Get the context indirect block's information */
171
0
        if (root_block) {
172
0
            iblock_addr      = hdr->man_dtable.table_addr;
173
0
            iblock_nrows     = hdr->man_dtable.curr_root_rows;
174
0
            iblock_parent    = NULL;
175
0
            iblock_par_entry = 0;
176
177
            /* The root block can't go up further... */
178
0
            biter->curr->up = NULL;
179
180
            /* Next time through the loop will not be with the root indirect block */
181
0
            root_block = false;
182
0
        } /* end if */
183
0
        else {
184
0
            hsize_t child_size; /* Size of new indirect block to create */
185
186
            /* Retrieve the parent information from the previous context location */
187
0
            iblock_parent    = biter->curr->up->context;
188
0
            iblock_par_entry = biter->curr->up->entry;
189
190
            /* Look up the address of context indirect block */
191
0
            iblock_addr = iblock_parent->ents[iblock_par_entry].addr;
192
193
            /* Compute # of rows in context indirect block */
194
0
            child_size   = hdr->man_dtable.row_block_size[biter->curr->up->row];
195
0
            iblock_nrows = (H5VM_log2_gen(child_size) - hdr->man_dtable.first_row_bits) + 1;
196
0
        } /* end else */
197
198
        /* Load indirect block for this context location */
199
0
        if (NULL ==
200
0
            (iblock = H5HF__man_iblock_protect(hdr, iblock_addr, iblock_nrows, iblock_parent,
201
0
                                               iblock_par_entry, false, H5AC__NO_FLAGS_SET, &did_protect)))
202
0
            HGOTO_ERROR(H5E_HEAP, H5E_CANTPROTECT, FAIL, "unable to protect fractal heap indirect block");
203
204
        /* Make indirect block the context for the current location */
205
0
        biter->curr->context = iblock;
206
207
        /* Hold the indirect block with the location */
208
0
        if (H5HF__iblock_incr(biter->curr->context) < 0)
209
0
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, FAIL,
210
0
                        "can't increment reference count on shared indirect block");
211
212
        /* Release the current indirect block */
213
0
        if (H5HF__man_iblock_unprotect(iblock, H5AC__NO_FLAGS_SET, did_protect) < 0)
214
0
            HGOTO_ERROR(H5E_HEAP, H5E_CANTUNPROTECT, FAIL, "unable to release fractal heap indirect block");
215
0
        iblock = NULL;
216
217
        /* See if the location falls in a direct block row */
218
        /* Or, if the offset has just filled up a direct or indirect block */
219
0
        if (curr_offset == (col * hdr->man_dtable.row_block_size[row]) ||
220
0
            row < hdr->man_dtable.max_direct_rows) {
221
0
            assert(curr_offset - (col * hdr->man_dtable.row_block_size[row]) == 0);
222
0
            break; /* Done now */
223
0
        }          /* end if */
224
        /* Indirect block row */
225
0
        else {
226
0
            H5HF_block_loc_t *new_loc; /* Pointer to new block location */
227
228
            /* Allocate level structure */
229
0
            if (NULL == (new_loc = H5FL_MALLOC(H5HF_block_loc_t)))
230
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
231
0
                            "memory allocation failed for direct block free list section");
232
233
            /* Link new level into iterator */
234
0
            new_loc->up = biter->curr;
235
236
            /* Adjust offset for new level */
237
0
            offset = curr_offset - (col * hdr->man_dtable.row_block_size[row]);
238
239
            /* Make new block the current context */
240
0
            biter->curr = new_loc;
241
0
        }        /* end else */
242
0
    } while (1); /* Breaks out in middle */
243
244
    /* Set flag to indicate block iterator finished initializing */
245
0
    biter->ready = true;
246
247
0
done:
248
0
    FUNC_LEAVE_NOAPI(ret_value)
249
0
} /* end H5HF__man_iter_start_offset() */
250
251
/*-------------------------------------------------------------------------
252
 * Function:  H5HF__man_iter_set_entry
253
 *
254
 * Purpose: Set the current entry for the iterator
255
 *
256
 * Return:  SUCCEED/FAIL
257
 *
258
 *-------------------------------------------------------------------------
259
 */
260
herr_t
261
H5HF__man_iter_set_entry(const H5HF_hdr_t *hdr, H5HF_block_iter_t *biter, unsigned entry)
262
0
{
263
0
    FUNC_ENTER_PACKAGE_NOERR
264
265
    /*
266
     * Check arguments.
267
     */
268
0
    assert(biter);
269
270
    /* Set location context */
271
0
    biter->curr->entry = entry;
272
0
    biter->curr->row   = entry / hdr->man_dtable.cparam.width;
273
0
    biter->curr->col   = entry % hdr->man_dtable.cparam.width;
274
275
0
    FUNC_LEAVE_NOAPI(SUCCEED)
276
0
} /* end H5HF__man_iter_set_entry() */
277
278
/*-------------------------------------------------------------------------
279
 * Function:  H5HF__man_iter_start_entry
280
 *
281
 * Purpose: Initialize a block iterator to a particular location within
282
 *              an indirect block
283
 *
284
 * Return:  SUCCEED/FAIL
285
 *
286
 *-------------------------------------------------------------------------
287
 */
288
herr_t
289
H5HF__man_iter_start_entry(H5HF_hdr_t *hdr, H5HF_block_iter_t *biter, H5HF_indirect_t *iblock,
290
                           unsigned start_entry)
291
0
{
292
0
    H5HF_block_loc_t *new_loc   = NULL;    /* Pointer to new block location */
293
0
    herr_t            ret_value = SUCCEED; /* Return value */
294
295
0
    FUNC_ENTER_PACKAGE
296
297
    /*
298
     * Check arguments.
299
     */
300
0
    assert(hdr);
301
0
    assert(biter);
302
0
    assert(!biter->ready);
303
0
    assert(iblock);
304
305
    /* Create new location for iterator */
306
0
    if (NULL == (new_loc = H5FL_MALLOC(H5HF_block_loc_t)))
307
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
308
0
                    "memory allocation failed for direct block free list section");
309
310
    /* Set up location context */
311
0
    new_loc->entry   = start_entry;
312
0
    new_loc->row     = start_entry / hdr->man_dtable.cparam.width;
313
0
    new_loc->col     = start_entry % hdr->man_dtable.cparam.width;
314
0
    new_loc->context = iblock;
315
0
    new_loc->up      = NULL;
316
317
    /* Increment reference count on indirect block */
318
0
    if (H5HF__iblock_incr(new_loc->context) < 0)
319
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, FAIL, "can't increment reference count on shared indirect block");
320
321
    /* Make new location the current location */
322
0
    biter->curr = new_loc;
323
324
    /* Set flag to indicate block iterator finished initializing */
325
0
    biter->ready = true;
326
327
0
done:
328
0
    if (ret_value < 0 && new_loc)
329
0
        new_loc = H5FL_FREE(H5HF_block_loc_t, new_loc);
330
331
0
    FUNC_LEAVE_NOAPI(ret_value)
332
0
} /* end H5HF__man_iter_start_entry() */
333
334
/*-------------------------------------------------------------------------
335
 * Function:  H5HF__man_iter_reset
336
 *
337
 * Purpose: Reset a block iterator to it's initial state, freeing any
338
 *              location context it currently has
339
 *
340
 * Return:  SUCCEED/FAIL
341
 *
342
 *-------------------------------------------------------------------------
343
 */
344
herr_t
345
H5HF__man_iter_reset(H5HF_block_iter_t *biter)
346
0
{
347
0
    herr_t ret_value = SUCCEED; /* Return value */
348
349
0
    FUNC_ENTER_PACKAGE
350
351
    /*
352
     * Check arguments.
353
     */
354
0
    assert(biter);
355
356
    /* Free any location contexts that exist */
357
0
    if (biter->curr) {
358
0
        H5HF_block_loc_t *curr_loc; /* Pointer to current block location */
359
0
        H5HF_block_loc_t *next_loc; /* Pointer to next block location */
360
361
        /* Free location contexts */
362
0
        curr_loc = biter->curr;
363
0
        while (curr_loc) {
364
            /* Get pointer to next node */
365
0
            next_loc = curr_loc->up;
366
367
            /* If this node is holding an indirect block, release the block */
368
0
            if (curr_loc->context)
369
0
                if (H5HF__iblock_decr(curr_loc->context) < 0)
370
0
                    HGOTO_ERROR(H5E_HEAP, H5E_CANTDEC, FAIL,
371
0
                                "can't decrement reference count on shared indirect block");
372
373
            /* Free the current location context */
374
0
            curr_loc = H5FL_FREE(H5HF_block_loc_t, curr_loc);
375
376
            /* Advance to next location */
377
0
            curr_loc = next_loc;
378
0
        } /* end while */
379
380
        /* Reset top location context */
381
0
        biter->curr = NULL;
382
0
    } /* end if */
383
384
    /* Reset block iterator flags */
385
0
    biter->ready = false;
386
387
0
done:
388
0
    FUNC_LEAVE_NOAPI(ret_value)
389
0
} /* end H5HF__man_iter_reset() */
390
391
/*-------------------------------------------------------------------------
392
 * Function:  H5HF__man_iter_next
393
 *
394
 * Purpose: Advance to the next block within the current block of the heap
395
 *
396
 * Return:  SUCCEED/FAIL
397
 *
398
 *-------------------------------------------------------------------------
399
 */
400
herr_t
401
H5HF__man_iter_next(H5HF_hdr_t *hdr, H5HF_block_iter_t *biter, unsigned nentries)
402
0
{
403
0
    FUNC_ENTER_PACKAGE_NOERR
404
405
    /*
406
     * Check arguments.
407
     */
408
0
    assert(biter);
409
0
    assert(biter->curr);
410
0
    assert(biter->curr->context);
411
0
    assert(biter->curr->row < biter->curr->context->nrows);
412
413
    /* Advance entry in current block */
414
0
    biter->curr->entry += nentries;
415
0
    biter->curr->row = biter->curr->entry / hdr->man_dtable.cparam.width;
416
0
    biter->curr->col = biter->curr->entry % hdr->man_dtable.cparam.width;
417
    /*    assert(biter->curr->row <= biter->curr->context->nrows); */
418
419
0
    FUNC_LEAVE_NOAPI(SUCCEED)
420
0
} /* end H5HF__man_iter_next() */
421
422
/*-------------------------------------------------------------------------
423
 * Function:  H5HF__man_iter_up
424
 *
425
 * Purpose: Move iterator up one level
426
 *
427
 * Return:  SUCCEED/FAIL
428
 *
429
 *-------------------------------------------------------------------------
430
 */
431
herr_t
432
H5HF__man_iter_up(H5HF_block_iter_t *biter)
433
0
{
434
0
    H5HF_block_loc_t *up_loc;              /* Pointer to 'up' block location */
435
0
    herr_t            ret_value = SUCCEED; /* Return value */
436
437
0
    FUNC_ENTER_PACKAGE
438
439
    /*
440
     * Check arguments.
441
     */
442
0
    assert(biter);
443
0
    assert(biter->ready);
444
0
    assert(biter->curr);
445
0
    assert(biter->curr->up);
446
0
    assert(biter->curr->context);
447
448
    /* Release hold on current location's indirect block */
449
0
    if (H5HF__iblock_decr(biter->curr->context) < 0)
450
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTDEC, FAIL, "can't decrement reference count on shared indirect block");
451
452
    /* Get pointer to location context above this one */
453
0
    up_loc = biter->curr->up;
454
455
    /* Release this location */
456
0
    biter->curr = H5FL_FREE(H5HF_block_loc_t, biter->curr);
457
458
    /* Point location to next location up */
459
0
    biter->curr = up_loc;
460
461
0
done:
462
0
    FUNC_LEAVE_NOAPI(ret_value)
463
0
} /* end H5HF__man_iter_up() */
464
465
/*-------------------------------------------------------------------------
466
 * Function:  H5HF__man_iter_down
467
 *
468
 * Purpose: Move iterator down one level
469
 *
470
 * Return:  SUCCEED/FAIL
471
 *
472
 *-------------------------------------------------------------------------
473
 */
474
herr_t
475
H5HF__man_iter_down(H5HF_block_iter_t *biter, H5HF_indirect_t *iblock)
476
0
{
477
0
    H5HF_block_loc_t *down_loc  = NULL;    /* Pointer to new 'down' block location */
478
0
    herr_t            ret_value = SUCCEED; /* Return value */
479
480
0
    FUNC_ENTER_PACKAGE
481
482
    /*
483
     * Check arguments.
484
     */
485
0
    assert(biter);
486
0
    assert(biter->ready);
487
0
    assert(biter->curr);
488
0
    assert(biter->curr->context);
489
490
    /* Create new location to move down to */
491
0
    if (NULL == (down_loc = H5FL_MALLOC(H5HF_block_loc_t)))
492
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
493
0
                    "memory allocation failed for direct block free list section");
494
495
    /* Initialize down location */
496
0
    down_loc->row     = 0;
497
0
    down_loc->col     = 0;
498
0
    down_loc->entry   = 0;
499
0
    down_loc->context = iblock;
500
0
    down_loc->up      = biter->curr;
501
502
    /* Increment reference count on indirect block */
503
0
    if (H5HF__iblock_incr(down_loc->context) < 0)
504
0
        HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, FAIL, "can't increment reference count on shared indirect block");
505
506
    /* Make down location the current location */
507
0
    biter->curr = down_loc;
508
509
0
done:
510
0
    if (ret_value < 0 && down_loc)
511
0
        down_loc = H5FL_FREE(H5HF_block_loc_t, down_loc);
512
513
0
    FUNC_LEAVE_NOAPI(ret_value)
514
0
} /* end H5HF__man_iter_down() */
515
516
/*-------------------------------------------------------------------------
517
 * Function:  H5HF__man_iter_curr
518
 *
519
 * Purpose: Retrieve information about the current block iterator location
520
 *
521
 * Return:  SUCCEED/FAIL
522
 *
523
 *-------------------------------------------------------------------------
524
 */
525
herr_t
526
H5HF__man_iter_curr(H5HF_block_iter_t *biter, unsigned *row, unsigned *col, unsigned *entry,
527
                    H5HF_indirect_t **block)
528
0
{
529
0
    FUNC_ENTER_PACKAGE_NOERR
530
531
    /*
532
     * Check arguments.
533
     */
534
0
    assert(biter);
535
0
    assert(biter->ready);
536
537
    /* Retrieve the information asked for */
538
0
    if (row)
539
0
        *row = biter->curr->row;
540
0
    if (col)
541
0
        *col = biter->curr->col;
542
0
    if (entry)
543
0
        *entry = biter->curr->entry;
544
0
    if (block)
545
0
        *block = biter->curr->context;
546
547
0
    FUNC_LEAVE_NOAPI(SUCCEED)
548
0
} /* end H5HF__man_iter_curr() */
549
550
/*-------------------------------------------------------------------------
551
 * Function:  H5HF__man_iter_ready
552
 *
553
 * Purpose: Query if iterator is ready to use
554
 *
555
 * Return:  SUCCEED/FAIL
556
 *
557
 *-------------------------------------------------------------------------
558
 */
559
bool
560
H5HF__man_iter_ready(H5HF_block_iter_t *biter)
561
0
{
562
0
    FUNC_ENTER_PACKAGE_NOERR
563
564
    /*
565
     * Check arguments.
566
     */
567
0
    assert(biter);
568
569
0
    FUNC_LEAVE_NOAPI(biter->ready)
570
0
} /* end H5HF__man_iter_ready() */