Coverage Report

Created: 2026-11-22 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Fsfile.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
#include "H5Fmodule.h" /* This source code file is part of the H5F module */
14
15
/* Packages needed by this file... */
16
#include "H5private.h"   /* Generic Functions     */
17
#include "H5Eprivate.h"  /* Error handling        */
18
#include "H5Fpkg.h"      /* File access       */
19
#include "H5FDprivate.h" /* File drivers        */
20
#include "H5FLprivate.h" /* Free lists                           */
21
22
/* PRIVATE TYPEDEFS */
23
24
/* Struct for tracking "shared" file structs */
25
typedef struct H5F_sfile_node_t {
26
    H5F_shared_t            *shared; /* Pointer to "shared" file struct */
27
    struct H5F_sfile_node_t *next;   /* Pointer to next node */
28
} H5F_sfile_node_t;
29
30
/* PRIVATE PROTOTYPES */
31
32
/* PRIVATE VARIABLES */
33
34
/* Declare a free list to manage the H5F_sfile_node_t struct */
35
H5FL_DEFINE_STATIC(H5F_sfile_node_t);
36
37
/* Declare a local variable to track the shared file information */
38
static H5F_sfile_node_t *H5F_sfile_head_s = NULL;
39
40
/*-------------------------------------------------------------------------
41
 * Function:    H5F_sfile_assert_num
42
 *
43
 * Purpose:     Sanity checking that shared file list is empty
44
 *
45
 * Return:      void
46
 *
47
 *-------------------------------------------------------------------------
48
 */
49
void
50
H5F_sfile_assert_num(unsigned H5_ATTR_NDEBUG_UNUSED n)
51
1
{
52
1
    FUNC_ENTER_NOAPI_NOINIT_NOERR
53
54
    /* The only useful work this function does is asserting so when NDEBUG
55
     * is defined it's a no-op.
56
     */
57
#ifndef NDEBUG
58
    if (n == 0) {
59
        assert(H5F_sfile_head_s == NULL);
60
    }
61
    else {
62
        unsigned          count; /* Number of open shared files */
63
        H5F_sfile_node_t *curr;  /* Current shared file node */
64
65
        /* Iterate through low-level files for matching low-level file info */
66
        curr  = H5F_sfile_head_s;
67
        count = 0;
68
        while (curr) {
69
            /* Increment # of open shared file structs */
70
            count++;
71
72
            /* Advance to next shared file node */
73
            curr = curr->next;
74
        }
75
76
        assert(count == n);
77
    }
78
#endif
79
80
1
    FUNC_LEAVE_NOAPI_VOID
81
1
} /* H5F_sfile_assert_num() */
82
83
/*-------------------------------------------------------------------------
84
 * Function:    H5F__sfile_add
85
 *
86
 * Purpose:     Add a "shared" file struct to the list of open files
87
 *
88
 * Return:      SUCCEED/FAIL
89
 *
90
 *-------------------------------------------------------------------------
91
 */
92
herr_t
93
H5F__sfile_add(H5F_shared_t *shared)
94
726
{
95
726
    H5F_sfile_node_t *new_shared;          /* New shared file node */
96
726
    herr_t            ret_value = SUCCEED; /* Return value */
97
98
726
    FUNC_ENTER_PACKAGE
99
100
    /* Sanity check */
101
726
    assert(shared);
102
103
    /* Allocate new shared file node */
104
726
    if (NULL == (new_shared = H5FL_CALLOC(H5F_sfile_node_t)))
105
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
106
107
    /* Set shared file value */
108
726
    new_shared->shared = shared;
109
110
    /* Prepend to list of shared files open */
111
726
    new_shared->next = H5F_sfile_head_s;
112
726
    H5F_sfile_head_s = new_shared;
113
114
726
done:
115
726
    FUNC_LEAVE_NOAPI(ret_value)
116
726
} /* end H5F__sfile_add() */
117
118
/*-------------------------------------------------------------------------
119
 * Function:    H5F__sfile_search
120
 *
121
 * Purpose:     Search for a "shared" file with low-level file info that
122
 *              matches
123
 *
124
 * Return:      Non-NULL on success / NULL on failure
125
 *
126
 *-------------------------------------------------------------------------
127
 */
128
H5F_shared_t *
129
H5F__sfile_search(H5FD_t *lf)
130
726
{
131
726
    H5F_sfile_node_t *curr;             /* Current shared file node */
132
726
    H5F_shared_t     *ret_value = NULL; /* Return value */
133
134
726
    FUNC_ENTER_PACKAGE_NOERR
135
136
    /* Sanity check */
137
726
    assert(lf);
138
139
    /* Iterate through low-level files for matching low-level file info */
140
726
    curr = H5F_sfile_head_s;
141
4.61k
    while (curr) {
142
        /* Check for match */
143
3.88k
        if (0 == H5FD_cmp(curr->shared->lf, lf))
144
0
            HGOTO_DONE(curr->shared);
145
146
        /* Advance to next shared file node */
147
3.88k
        curr = curr->next;
148
3.88k
    } /* end while */
149
150
726
done:
151
726
    FUNC_LEAVE_NOAPI(ret_value)
152
726
} /* end H5F__sfile_search() */
153
154
/*-------------------------------------------------------------------------
155
 * Function:    H5F__sfile_remove
156
 *
157
 * Purpose:     Remove a "shared" file struct from the list of open files
158
 *
159
 * Return:      SUCCEED/FAIL
160
 *
161
 *-------------------------------------------------------------------------
162
 */
163
herr_t
164
H5F__sfile_remove(H5F_shared_t *shared)
165
726
{
166
726
    H5F_sfile_node_t *curr;                /* Current shared file node */
167
726
    H5F_sfile_node_t *last;                /* Last shared file node */
168
726
    herr_t            ret_value = SUCCEED; /* Return value */
169
170
726
    FUNC_ENTER_PACKAGE
171
172
    /* Sanity check */
173
726
    assert(shared);
174
175
    /* Locate shared file node with correct shared file */
176
726
    last = NULL;
177
726
    curr = H5F_sfile_head_s;
178
754
    while (curr && curr->shared != shared) {
179
        /* Advance to next node */
180
28
        last = curr;
181
28
        curr = curr->next;
182
28
    } /* end while */
183
184
    /* Indicate error if the node wasn't found */
185
726
    if (curr == NULL)
186
0
        HGOTO_ERROR(H5E_FILE, H5E_NOTFOUND, FAIL, "can't find shared file info");
187
188
    /* Remove node found from list */
189
726
    if (last != NULL)
190
        /* Removing middle or tail node in list */
191
8
        last->next = curr->next;
192
718
    else
193
        /* Removing head node in list */
194
718
        H5F_sfile_head_s = curr->next;
195
196
    /* Release the shared file node struct */
197
    /* (the shared file info itself is freed elsewhere) */
198
726
    curr = H5FL_FREE(H5F_sfile_node_t, curr);
199
200
726
done:
201
726
    FUNC_LEAVE_NOAPI(ret_value)
202
726
} /* end H5F__sfile_remove() */