Coverage Report

Created: 2026-10-28 18:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5Gcache.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:   H5Gcache.c
16
 *
17
 * Purpose:   Implement group metadata cache methods
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5Gmodule.h" /* This source code file is part of the H5G 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 "H5Gpkg.h"      /* Groups          */
35
#include "H5MMprivate.h" /* Memory management     */
36
37
/****************/
38
/* Local Macros */
39
/****************/
40
41
15.7k
#define H5G_NODE_VERS 1 /* Symbol table node version number   */
42
43
/******************/
44
/* Local Typedefs */
45
/******************/
46
47
/********************/
48
/* Package Typedefs */
49
/********************/
50
51
/********************/
52
/* Local Prototypes */
53
/********************/
54
55
/* Metadata cache (H5AC) callbacks */
56
static herr_t H5G__cache_node_get_initial_load_size(void *udata, size_t *image_len);
57
static void  *H5G__cache_node_deserialize(const void *image, size_t len, void *udata, bool *dirty);
58
static herr_t H5G__cache_node_image_len(const void *thing, size_t *image_len);
59
static herr_t H5G__cache_node_serialize(const H5F_t *f, void *image, size_t len, void *thing);
60
static herr_t H5G__cache_node_free_icr(void *thing);
61
62
/*********************/
63
/* Package Variables */
64
/*********************/
65
66
/*****************************/
67
/* Library Private Variables */
68
/*****************************/
69
70
/*******************/
71
/* Local Variables */
72
/*******************/
73
74
/* Symbol table nodes inherit cache-like properties from H5AC */
75
const H5AC_class_t H5AC_SNODE[1] = {{
76
    H5AC_SNODE_ID,                         /* Metadata client ID */
77
    "Symbol table node",                   /* Metadata client name (for debugging) */
78
    H5FD_MEM_BTREE,                        /* File space memory type for client */
79
    H5AC__CLASS_NO_FLAGS_SET,              /* Client class behavior flags */
80
    H5G__cache_node_get_initial_load_size, /* 'get_initial_load_size' callback */
81
    NULL,                                  /* 'get_final_load_size' callback */
82
    NULL,                                  /* 'verify_chksum' callback */
83
    H5G__cache_node_deserialize,           /* 'deserialize' callback */
84
    H5G__cache_node_image_len,             /* 'image_len' callback */
85
    NULL,                                  /* 'pre_serialize' callback */
86
    H5G__cache_node_serialize,             /* 'serialize' callback */
87
    NULL,                                  /* 'notify' callback */
88
    H5G__cache_node_free_icr,              /* 'free_icr' callback */
89
    NULL,                                  /* 'fsf_size' callback */
90
}};
91
92
/* Declare extern the free list to manage the H5G_node_t struct */
93
H5FL_EXTERN(H5G_node_t);
94
95
/* Declare extern the free list to manage sequences of H5G_entry_t's */
96
H5FL_SEQ_EXTERN(H5G_entry_t);
97
98
/*-------------------------------------------------------------------------
99
 * Function:    H5G__cache_node_get_initial_load_size()
100
 *
101
 * Purpose:     Determine the size of the on-disk image of the node, and
102
 *              return this value in *image_len.
103
 *
104
 * Return:      SUCCEED/FAIL
105
 *-------------------------------------------------------------------------
106
 */
107
static herr_t
108
H5G__cache_node_get_initial_load_size(void *_udata, size_t *image_len)
109
16.1k
{
110
16.1k
    H5F_t *f = (H5F_t *)_udata; /* User data for callback */
111
112
16.1k
    FUNC_ENTER_PACKAGE_NOERR
113
114
16.1k
    assert(f);
115
16.1k
    assert(image_len);
116
117
    /* Set the image length size */
118
16.1k
    *image_len = (size_t)(H5G_NODE_SIZE(f));
119
120
16.1k
    FUNC_LEAVE_NOAPI(SUCCEED)
121
16.1k
} /* end H5G__cache_node_get_initial_load_size() */
122
123
/*-------------------------------------------------------------------------
124
 * Function:    H5G__cache_node_deserialize
125
 *
126
 * Purpose:     Given a buffer containing the on disk image of a symbol table
127
 *              node, allocate an instance of H5G_node_t, load the contents of the
128
 *              image into it, and return a pointer to the instance.
129
 *
130
 *              Note that deserializing the image requires access to the file
131
 *              pointer, which is not included in the parameter list for this
132
 *              callback.  Finesse this issue by passing in the file pointer
133
 *              twice to the H5AC_protect() call -- once as the file pointer
134
 *              proper, and again as the user data
135
 *
136
 * Return:      Success:        Pointer to in core representation
137
 *              Failure:        NULL
138
 *-------------------------------------------------------------------------
139
 */
140
static void *
141
H5G__cache_node_deserialize(const void *_image, size_t len, void *_udata, bool H5_ATTR_UNUSED *dirty)
142
16.0k
{
143
16.0k
    H5F_t         *f         = (H5F_t *)_udata;         /* User data for callback */
144
16.0k
    H5G_node_t    *sym       = NULL;                    /* Symbol table node created */
145
16.0k
    const uint8_t *image     = (const uint8_t *)_image; /* Pointer to image to deserialize */
146
16.0k
    const uint8_t *image_end = image + len - 1;         /* Pointer to end of image buffer */
147
16.0k
    void          *ret_value = NULL;
148
149
16.0k
    FUNC_ENTER_PACKAGE
150
151
16.0k
    assert(image);
152
16.0k
    assert(len > 0);
153
16.0k
    assert(f);
154
16.0k
    assert(dirty);
155
156
    /* Allocate symbol table data structures */
157
16.0k
    if (NULL == (sym = H5FL_CALLOC(H5G_node_t)))
158
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
159
16.0k
    sym->node_size = (size_t)(H5G_NODE_SIZE(f));
160
16.0k
    if (NULL == (sym->entry = H5FL_SEQ_CALLOC(H5G_entry_t, (size_t)(2 * H5F_SYM_LEAF_K(f)))))
161
0
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed");
162
163
    /* Magic */
164
16.0k
    if (H5_IS_BUFFER_OVERFLOW(image, H5_SIZEOF_MAGIC, image_end))
165
0
        HGOTO_ERROR(H5E_SYM, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
166
16.0k
    if (memcmp(image, H5G_NODE_MAGIC, (size_t)H5_SIZEOF_MAGIC) != 0)
167
225
        HGOTO_ERROR(H5E_SYM, H5E_BADVALUE, NULL, "bad symbol table node signature");
168
15.7k
    image += H5_SIZEOF_MAGIC;
169
170
    /* Version */
171
15.7k
    if (H5_IS_BUFFER_OVERFLOW(image, 1, image_end))
172
0
        HGOTO_ERROR(H5E_SYM, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
173
15.7k
    if (H5G_NODE_VERS != *image++)
174
19
        HGOTO_ERROR(H5E_SYM, H5E_VERSION, NULL, "bad symbol table node version");
175
176
    /* Reserved */
177
15.7k
    if (H5_IS_BUFFER_OVERFLOW(image, 1, image_end))
178
0
        HGOTO_ERROR(H5E_SYM, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
179
15.7k
    image++;
180
181
    /* Number of symbols */
182
15.7k
    if (H5_IS_BUFFER_OVERFLOW(image, 2, image_end))
183
0
        HGOTO_ERROR(H5E_SYM, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding");
184
15.7k
    UINT16DECODE(image, sym->nsyms);
185
186
    /* Entries */
187
15.7k
    if (H5G__ent_decode_vec(f, &image, image_end, sym->entry, sym->nsyms) < 0)
188
84
        HGOTO_ERROR(H5E_SYM, H5E_CANTLOAD, NULL, "unable to decode symbol table entries");
189
190
    /* Set return value */
191
15.6k
    ret_value = sym;
192
193
16.0k
done:
194
16.0k
    if (!ret_value)
195
328
        if (sym && H5G__node_free(sym) < 0)
196
0
            HDONE_ERROR(H5E_SYM, H5E_CANTFREE, NULL, "unable to destroy symbol table node");
197
198
16.0k
    FUNC_LEAVE_NOAPI(ret_value)
199
16.0k
} /* end H5G__cache_node_deserialize() */
200
201
/*-------------------------------------------------------------------------
202
 * Function:    H5G__cache_node_image_len
203
 *
204
 * Purpose:     Compute the size of the data structure on disk and return
205
 *              it in *image_len
206
 *
207
 * Return:      SUCCEED/FAIL
208
 *-------------------------------------------------------------------------
209
 */
210
static herr_t
211
H5G__cache_node_image_len(const void *_thing, size_t *image_len)
212
0
{
213
0
    const H5G_node_t *sym = (const H5G_node_t *)_thing; /* Pointer to object */
214
215
0
    FUNC_ENTER_PACKAGE_NOERR
216
217
0
    assert(sym);
218
0
    assert(sym->cache_info.type == H5AC_SNODE);
219
0
    assert(image_len);
220
221
0
    *image_len = sym->node_size;
222
223
0
    FUNC_LEAVE_NOAPI(SUCCEED)
224
0
} /* end H5G__cache_node_image_len() */
225
226
/*-------------------------------------------------------------------------
227
 * Function:    H5G__cache_node_serialize
228
 *
229
 * Purpose:     Given a correctly sized buffer and an instance of H5G_node_t,
230
 *              serialize the contents of the instance of H5G_node_t, and write
231
 *              this data into the supplied buffer.  This buffer will be written
232
 *              to disk.
233
 *
234
 * Return:      SUCCEED/FAIL
235
 *-------------------------------------------------------------------------
236
 */
237
static herr_t
238
H5G__cache_node_serialize(const H5F_t *f, void *_image, size_t len, void *_thing)
239
0
{
240
0
    H5G_node_t *sym       = (H5G_node_t *)_thing; /* Pointer to object */
241
0
    uint8_t    *image     = (uint8_t *)_image;    /* Pointer into raw data buffer */
242
0
    herr_t      ret_value = SUCCEED;
243
244
0
    FUNC_ENTER_PACKAGE
245
246
0
    assert(f);
247
0
    assert(image);
248
0
    assert(sym);
249
0
    assert(sym->cache_info.type == H5AC_SNODE);
250
0
    assert(len == sym->node_size);
251
252
    /* Magic number */
253
0
    H5MM_memcpy(image, H5G_NODE_MAGIC, (size_t)H5_SIZEOF_MAGIC);
254
0
    image += H5_SIZEOF_MAGIC;
255
256
    /* Version number */
257
0
    *image++ = H5G_NODE_VERS;
258
259
    /* Reserved */
260
0
    *image++ = 0;
261
262
    /* Number of symbols */
263
0
    UINT16ENCODE(image, sym->nsyms);
264
265
    /* Entries */
266
0
    if (H5G__ent_encode_vec(f, &image, sym->entry, sym->nsyms) < 0)
267
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTENCODE, FAIL, "can't serialize");
268
269
    /* Clear rest of symbol table node */
270
0
    memset(image, 0, len - (size_t)(image - (uint8_t *)_image));
271
272
0
done:
273
0
    FUNC_LEAVE_NOAPI(ret_value)
274
0
} /* end H5G__cache_node_serialize() */
275
276
/*-------------------------------------------------------------------------
277
 * Function:    H5G__cache_node_free_icr
278
 *
279
 * Purpose:     Destroy a symbol table node in memory
280
 *
281
 * Return:      SUCCEED/FAIL
282
 *-------------------------------------------------------------------------
283
 */
284
static herr_t
285
H5G__cache_node_free_icr(void *_thing)
286
15.6k
{
287
15.6k
    H5G_node_t *sym       = (H5G_node_t *)_thing; /* Pointer to the object */
288
15.6k
    herr_t      ret_value = SUCCEED;
289
290
15.6k
    FUNC_ENTER_PACKAGE
291
292
15.6k
    assert(sym);
293
15.6k
    assert(sym->cache_info.type == H5AC_SNODE);
294
295
    /* Destroy symbol table node */
296
15.6k
    if (H5G__node_free(sym) < 0)
297
0
        HGOTO_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to destroy symbol table node");
298
299
15.6k
done:
300
15.6k
    FUNC_LEAVE_NOAPI(ret_value)
301
15.6k
} /* end H5G__cache_node_free_icr() */