summaryrefslogtreecommitdiffstats
path: root/src/H5Gcache.c
blob: 13a33a3b7c1a1587b3fefae297fdc554e48e2caa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5Gcache.c
 *			Feb  5 2008
 *			Quincey Koziol <koziol@hdfgroup.org>
 *
 * Purpose:		Implement group metadata cache methods.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5Gmodule.h"          /* This source code file is part of the H5G module */


/***********/
/* Headers */
/***********/
#include "H5private.h"		/* Generic Functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Gpkg.h"		/* Groups		  		*/
#include "H5MFprivate.h"	/* File memory management		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5WBprivate.h"        /* Wrapped Buffers                      */


/****************/
/* Local Macros */
/****************/

#define H5G_NODE_VERS           1       /* Symbol table node version number   */


/******************/
/* Local Typedefs */
/******************/


/********************/
/* Package Typedefs */
/********************/


/********************/
/* Local Prototypes */
/********************/

/* Metadata cache (H5AC) callbacks */
static herr_t H5G__cache_node_get_initial_load_size(void *udata, size_t *image_len);
static void *H5G__cache_node_deserialize(const void *image, size_t len,
    void *udata, hbool_t *dirty);
static herr_t H5G__cache_node_image_len(const void *thing, size_t *image_len);
static herr_t H5G__cache_node_serialize(const H5F_t *f, void *image,
    size_t len, void *thing);
static herr_t H5G__cache_node_free_icr(void *thing);


/*********************/
/* Package Variables */
/*********************/


/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/

/* Symbol table nodes inherit cache-like properties from H5AC */
const H5AC_class_t H5AC_SNODE[1] = {{
    H5AC_SNODE_ID,                      /* Metadata client ID */
    "Symbol table node",                /* Metadata client name (for debugging) */
    H5FD_MEM_BTREE,                     /* File space memory type for client */
    H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
    H5G__cache_node_get_initial_load_size,      /* 'get_initial_load_size' callback */
    NULL,				/* 'get_final_load_size' callback */
    NULL,				/* 'verify_chksum' callback */
    H5G__cache_node_deserialize,        /* 'deserialize' callback */
    H5G__cache_node_image_len,          /* 'image_len' callback */
    NULL,                               /* 'pre_serialize' callback */
    H5G__cache_node_serialize,          /* 'serialize' callback */
    NULL,                               /* 'notify' callback */
    H5G__cache_node_free_icr,           /* 'free_icr' callback */
    NULL,                               /* 'fsf_size' callback */
}};


/* Declare extern the free list to manage the H5G_node_t struct */
H5FL_EXTERN(H5G_node_t);

/* Declare extern the free list to manage sequences of H5G_entry_t's */
H5FL_SEQ_EXTERN(H5G_entry_t);


/*-------------------------------------------------------------------------
 * Function:    H5G__cache_node_get_initial_load_size()
 *
 * Purpose:	Determine the size of the on-disk image of the node, and
 *		return this value in *image_len.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__cache_node_get_initial_load_size(void *_udata, size_t *image_len)
{
    H5F_t *f = (H5F_t *)_udata;   		/* User data for callback */

    FUNC_ENTER_STATIC_NOERR

    /* Sanity checks */
    HDassert(f);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = (size_t)(H5G_NODE_SIZE(f));

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5G__cache_node_get_initial_load_size() */


/*-------------------------------------------------------------------------
 * Function:    H5G__cache_node_deserialize
 *
 * Purpose:	Given a buffer containing the on disk image of a symbol table
 *		node, allocate an instance of H5G_node_t, load the contence of the
 *		image into it, and return a pointer to the instance.
 *
 *		Note that deserializing the image requires access to the file
 *		pointer, which is not included in the parameter list for this
 *		callback.  Finesse this issue by passing in the file pointer
 *		twice to the H5AC_protect() call -- once as the file pointer
 *		proper, and again as the user data
 *
 * Return:      Success:        Pointer to in core representation
 *              Failure:        NULL
 *
 * Programmer:  John Mainzer
 *              6/21/14
 *
 *-------------------------------------------------------------------------
 */
static void *
H5G__cache_node_deserialize(const void *_image, size_t len, void *_udata,
    hbool_t H5_ATTR_UNUSED *dirty)
{
    H5F_t                  *f = (H5F_t *)_udata;        /* User data for callback */
    H5G_node_t             *sym = NULL; /* Symbol table node created */
    const uint8_t          *image = (const uint8_t *)_image;    /* Pointer to image to deserialize */
    const uint8_t          *image_end = image + len - 1;        /* Pointer to end of image buffer */
    void                   *ret_value = NULL;   /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(image);
    HDassert(len > 0);
    HDassert(f);
    HDassert(dirty);

    /* Allocate symbol table data structures */
    if(NULL == (sym = H5FL_CALLOC(H5G_node_t)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
    sym->node_size = (size_t)(H5G_NODE_SIZE(f));
    if(NULL == (sym->entry = H5FL_SEQ_CALLOC(H5G_entry_t, (size_t)(2 * H5F_SYM_LEAF_K(f)))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* magic */
    if(HDmemcmp(image, H5G_NODE_MAGIC, (size_t)H5_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_SYM, H5E_BADVALUE, NULL, "bad symbol table node signature")
    image += H5_SIZEOF_MAGIC;

    /* version */
    if(H5G_NODE_VERS != *image++)
        HGOTO_ERROR(H5E_SYM, H5E_VERSION, NULL, "bad symbol table node version")

    /* reserved */
    image++;

    /* number of symbols */
    UINT16DECODE(image, sym->nsyms);

    /* entries */
    if(H5G__ent_decode_vec(f, &image, image_end, sym->entry, sym->nsyms) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTLOAD, NULL, "unable to decode symbol table entries")

    /* Set return value */
    ret_value = sym;

done:
    if(!ret_value)
        if(sym && H5G__node_free(sym) < 0)
            HDONE_ERROR(H5E_SYM, H5E_CANTFREE, NULL, "unable to destroy symbol table node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__cache_node_deserialize() */


/*-------------------------------------------------------------------------
 * Function:    H5G__cache_node_image_len
 *
 * Purpose:     Compute the size of the data structure on disk and return
 *              it in *image_len.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__cache_node_image_len(const void *_thing, size_t *image_len)
{
    const H5G_node_t *sym = (const H5G_node_t *)_thing; /* Pointer to object */

    FUNC_ENTER_STATIC_NOERR

    /* Sanity checks */
    HDassert(sym);
    HDassert(sym->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sym->cache_info.type == H5AC_SNODE);
    HDassert(image_len);

    *image_len = sym->node_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5G__cache_node_image_len() */


/*-------------------------------------------------------------------------
 * Function:    H5G__cache_node_serialize
 *
 * Purpose:	Given a correctly sized buffer and an instace of H5G_node_t,
 *		serialize the contents of the instance of H5G_node_t, and write
 *		this data into the supplied buffer.  This buffer will be written
 *		to disk.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__cache_node_serialize(const H5F_t *f, void *_image, size_t len,
    void *_thing)
{
    H5G_node_t *sym = (H5G_node_t *)_thing;     /* Pointer to object */
    uint8_t    *image = (uint8_t *)_image;      /* Pointer into raw data buffer */
    herr_t      ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(f);
    HDassert(image);
    HDassert(sym);
    HDassert(sym->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sym->cache_info.type == H5AC_SNODE);
    HDassert(len == sym->node_size);

    /* magic number */
    H5MM_memcpy(image, H5G_NODE_MAGIC, (size_t)H5_SIZEOF_MAGIC);
    image += H5_SIZEOF_MAGIC;

    /* version number */
    *image++ = H5G_NODE_VERS;

    /* reserved */
    *image++ = 0;

    /* number of symbols */
    UINT16ENCODE(image, sym->nsyms);

    /* entries */
    if(H5G__ent_encode_vec(f, &image, sym->entry, sym->nsyms) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTENCODE, FAIL, "can't serialize")

    /* Clear rest of symbol table node */
    HDmemset(image, 0, len - (size_t)(image - (uint8_t *)_image));

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__cache_node_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5G__cache_node_free_icr
 *
 * Purpose:	Destroys a symbol table node in memory.
 *
 * Note:	The metadata cache sets the object's cache_info.magic to
 *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
 *		callback (checked in assert).
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__cache_node_free_icr(void *_thing)
{
    H5G_node_t *sym = (H5G_node_t *)_thing;     /* Pointer to the object */
    herr_t      ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(sym);
    HDassert(sym->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
    HDassert(sym->cache_info.type == H5AC_SNODE);

    /* Destroy symbol table node */
    if(H5G__node_free(sym) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to destroy symbol table node")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__cache_node_free_icr() */