1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*-------------------------------------------------------------------------
15  *
16  * Created:     H5HLcache.c
17  *              Feb  5 2008
18  *              Quincey Koziol <koziol@hdfgroup.org>
19  *
20  * Purpose:     Implement local heap metadata cache methods.
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 /****************/
26 /* Module Setup */
27 /****************/
28 
29 #include "H5HLmodule.h"         /* This source code file is part of the H5HL module */
30 
31 
32 /***********/
33 /* Headers */
34 /***********/
35 #include "H5private.h"      /* Generic Functions            */
36 #include "H5Eprivate.h"     /* Error handling               */
37 #include "H5HLpkg.h"        /* Local Heaps                  */
38 #include "H5MFprivate.h"    /* File memory management       */
39 #include "H5WBprivate.h"    /* Wrapped Buffers              */
40 
41 
42 /****************/
43 /* Local Macros */
44 /****************/
45 
46 #define H5HL_VERSION    0               /* Local heap collection version    */
47 
48 /* Set the local heap size to speculatively read in
49  *      (needs to be more than the local heap prefix size to work at all and
50  *      should be larger than the default local heap size to save the
51  *      extra I/O operations)
52  */
53 #define H5HL_SPEC_READ_SIZE     512
54 
55 
56 /******************/
57 /* Local Typedefs */
58 /******************/
59 
60 
61 /********************/
62 /* Package Typedefs */
63 /********************/
64 
65 
66 /********************/
67 /* Local Prototypes */
68 /********************/
69 
70 /* Metadata cache callbacks */
71 /* Local heap prefix */
72 static herr_t H5HL__cache_prefix_get_initial_load_size(void *udata, size_t *image_len);
73 static herr_t H5HL__cache_prefix_get_final_load_size(const void *_image,
74     size_t image_len, void *udata, size_t *actual_len);
75 static void *H5HL__cache_prefix_deserialize(const void *image, size_t len,
76     void *udata, hbool_t *dirty);
77 static herr_t H5HL__cache_prefix_image_len(const void *thing, size_t *image_len);
78 static herr_t H5HL__cache_prefix_serialize(const H5F_t *f, void *image,
79     size_t len, void *thing);
80 static herr_t H5HL__cache_prefix_free_icr(void *thing);
81 
82 /* Local heap data block */
83 static herr_t H5HL__cache_datablock_get_initial_load_size(void *udata, size_t *image_len);
84 static void *H5HL__cache_datablock_deserialize(const void *image, size_t len,
85     void *udata, hbool_t *dirty);
86 static herr_t H5HL__cache_datablock_image_len(const void *thing, size_t *image_len);
87 static herr_t H5HL__cache_datablock_serialize(const H5F_t *f, void *image,
88     size_t len, void *thing);
89 static herr_t H5HL__cache_datablock_notify(H5C_notify_action_t action, void *_thing);
90 static herr_t H5HL__cache_datablock_free_icr(void *thing);
91 
92 /* Header deserialization */
93 static herr_t H5HL__hdr_deserialize(H5HL_t *heap, const uint8_t *image,
94     H5HL_cache_prfx_ud_t *udata);
95 
96 /* Free list de/serialization */
97 static herr_t H5HL__fl_deserialize(H5HL_t *heap);
98 static void H5HL__fl_serialize(const H5HL_t *heap);
99 
100 /*********************/
101 /* Package Variables */
102 /*********************/
103 
104 /* H5HL inherits cache-like properties from H5AC */
105 const H5AC_class_t H5AC_LHEAP_PRFX[1] = {{
106     H5AC_LHEAP_PRFX_ID,                 /* Metadata client ID */
107     "local heap prefix",                /* Metadata client name (for debugging) */
108     H5FD_MEM_LHEAP,                     /* File space memory type for client */
109     H5AC__CLASS_SPECULATIVE_LOAD_FLAG,  /* Client class behavior flags */
110     H5HL__cache_prefix_get_initial_load_size,   /* 'get_initial_load_size' callback */
111     H5HL__cache_prefix_get_final_load_size, /* 'get_final_load_size' callback */
112     NULL,				/* 'verify_chksum' callback */
113     H5HL__cache_prefix_deserialize,     /* 'deserialize' callback */
114     H5HL__cache_prefix_image_len,       /* 'image_len' callback */
115     NULL,                               /* 'pre_serialize' callback */
116     H5HL__cache_prefix_serialize,       /* 'serialize' callback */
117     NULL,                               /* 'notify' callback */
118     H5HL__cache_prefix_free_icr,        /* 'free_icr' callback */
119     NULL,                               /* 'fsf_size' callback */
120 }};
121 
122 const H5AC_class_t H5AC_LHEAP_DBLK[1] = {{
123     H5AC_LHEAP_DBLK_ID,                 /* Metadata client ID */
124     "local heap datablock",             /* Metadata client name (for debugging) */
125     H5FD_MEM_LHEAP,                     /* File space memory type for client */
126     H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
127     H5HL__cache_datablock_get_initial_load_size,/* 'get_initial_load_size' callback */
128     NULL,				/* 'get_final_load_size' callback */
129     NULL,				/* 'verify_chksum' callback */
130     H5HL__cache_datablock_deserialize,  /* 'deserialize' callback */
131     H5HL__cache_datablock_image_len,    /* 'image_len' callback */
132     NULL,                               /* 'pre_serialize' callback */
133     H5HL__cache_datablock_serialize,    /* 'serialize' callback */
134     H5HL__cache_datablock_notify,       /* 'notify' callback */
135     H5HL__cache_datablock_free_icr,     /* 'free_icr' callback */
136     NULL,                               /* 'fsf_size' callback */
137 }};
138 
139 
140 /*****************************/
141 /* Library Private Variables */
142 /*****************************/
143 
144 
145 /*******************/
146 /* Local Variables */
147 /*******************/
148 
149 
150 
151 /*-------------------------------------------------------------------------
152  * Function:    H5HL__hdr_deserialize()
153  *
154  * Purpose:	Decode a local heap's header
155  *
156  * Return:      Success:        SUCCEED
157  *              Failure:        FAIL
158  *
159  * Programmer:  Quincey Koziol
160  *              December 15, 2016
161  *
162  *-------------------------------------------------------------------------
163  */
164 static herr_t
H5HL__hdr_deserialize(H5HL_t * heap,const uint8_t * image,H5HL_cache_prfx_ud_t * udata)165 H5HL__hdr_deserialize( H5HL_t *heap, const uint8_t *image,
166     H5HL_cache_prfx_ud_t *udata)
167 {
168     herr_t ret_value = SUCCEED; /* Return value */
169 
170     FUNC_ENTER_STATIC
171 
172     /* Sanity checks */
173     HDassert(heap);
174     HDassert(image);
175     HDassert(udata);
176 
177     /* Check magic number */
178     if(HDmemcmp(image, H5HL_MAGIC, (size_t)H5_SIZEOF_MAGIC))
179         HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, FAIL, "bad local heap signature")
180     image += H5_SIZEOF_MAGIC;
181 
182     /* Version */
183     if(H5HL_VERSION != *image++)
184         HGOTO_ERROR(H5E_HEAP, H5E_VERSION, FAIL, "wrong version number in local heap")
185 
186     /* Reserved */
187     image += 3;
188 
189     /* Store the prefix's address & length */
190     heap->prfx_addr = udata->prfx_addr;
191     heap->prfx_size = udata->sizeof_prfx;
192 
193     /* Heap data size */
194     H5F_DECODE_LENGTH_LEN(image, heap->dblk_size, udata->sizeof_size);
195 
196     /* Free list head */
197     H5F_DECODE_LENGTH_LEN(image, heap->free_block, udata->sizeof_size);
198     if(heap->free_block != H5HL_FREE_NULL && heap->free_block >= heap->dblk_size)
199         HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, FAIL, "bad heap free list")
200 
201     /* Heap data address */
202     H5F_addr_decode_len(udata->sizeof_addr, &image, &(heap->dblk_addr));
203 
204 done:
205     FUNC_LEAVE_NOAPI(ret_value)
206 } /* end H5HL__hdr_deserialize() */
207 
208 
209 /*-------------------------------------------------------------------------
210  * Function:    H5HL__fl_deserialize
211  *
212  * Purpose:     Deserialize the free list for a heap data block
213  *
214  * Return:      SUCCEED/FAIL
215  *
216  * Programmer:  Quincey Koziol
217  *              Oct 12 2008
218  *
219  *-------------------------------------------------------------------------
220  */
221 static herr_t
H5HL__fl_deserialize(H5HL_t * heap)222 H5HL__fl_deserialize(H5HL_t *heap)
223 {
224     H5HL_free_t *fl = NULL, *tail = NULL;      /* Heap free block nodes */
225     hsize_t free_block;                 /* Offset of free block */
226     herr_t ret_value = SUCCEED;         /* Return value */
227 
228     FUNC_ENTER_STATIC
229 
230     /* check arguments */
231     HDassert(heap);
232     HDassert(!heap->freelist);
233 
234     /* Build free list */
235     free_block = heap->free_block;
236     while(H5HL_FREE_NULL != free_block) {
237         const uint8_t *image;           /* Pointer into image buffer */
238 
239         /* Sanity check */
240         if(free_block >= heap->dblk_size)
241             HGOTO_ERROR(H5E_HEAP, H5E_BADRANGE, FAIL, "bad heap free list")
242 
243         /* Allocate & initialize free list node */
244         if(NULL == (fl = H5FL_MALLOC(H5HL_free_t)))
245             HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, FAIL, "memory allocation failed")
246         fl->offset = (size_t)free_block;
247         fl->prev = tail;
248         fl->next = NULL;
249 
250         /* Decode offset of next free block */
251         image = heap->dblk_image + free_block;
252         H5F_DECODE_LENGTH_LEN(image, free_block, heap->sizeof_size);
253         if(0 == free_block)
254             HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, FAIL, "free block size is zero?")
255 
256         /* Decode length of this free block */
257         H5F_DECODE_LENGTH_LEN(image, fl->size, heap->sizeof_size);
258         if((fl->offset + fl->size) > heap->dblk_size)
259             HGOTO_ERROR(H5E_HEAP, H5E_BADRANGE, FAIL, "bad heap free list")
260 
261         /* Append node onto list */
262         if(tail)
263             tail->next = fl;
264         else
265             heap->freelist = fl;
266         tail = fl;
267         fl = NULL;
268     } /* end while */
269 
270 done:
271     if(ret_value < 0)
272         if(fl)
273             /* H5FL_FREE always returns NULL so we can't check for errors */
274             fl = H5FL_FREE(H5HL_free_t, fl);
275 
276     FUNC_LEAVE_NOAPI(ret_value)
277 } /* end H5HL__fl_deserialize() */
278 
279 
280 /*-------------------------------------------------------------------------
281  * Function:    H5HL__fl_serialize
282  *
283  * Purpose:     Serialize the free list for a heap data block
284  *
285  * Return:      Nothing (void)
286  *
287  * Programmer:  Quincey Koziol
288  *              Oct 12 2008
289  *
290  *-------------------------------------------------------------------------
291  */
292 static void
H5HL__fl_serialize(const H5HL_t * heap)293 H5HL__fl_serialize(const H5HL_t *heap)
294 {
295     H5HL_free_t *fl;                    /* Pointer to heap free list node */
296 
297     FUNC_ENTER_STATIC_NOERR
298 
299     /* check arguments */
300     HDassert(heap);
301 
302     /* Serialize the free list into the heap data's image */
303     for(fl = heap->freelist; fl; fl = fl->next) {
304         uint8_t     *image;     /* Pointer into raw data buffer */
305 
306         HDassert(fl->offset == H5HL_ALIGN(fl->offset));
307         image = heap->dblk_image + fl->offset;
308 
309         if(fl->next)
310             H5F_ENCODE_LENGTH_LEN(image, fl->next->offset, heap->sizeof_size)
311         else
312             H5F_ENCODE_LENGTH_LEN(image, H5HL_FREE_NULL, heap->sizeof_size)
313 
314         H5F_ENCODE_LENGTH_LEN(image, fl->size, heap->sizeof_size)
315     } /* end for */
316 
317     FUNC_LEAVE_NOAPI_VOID
318 
319 } /* end H5HL__fl_serialize() */
320 
321 
322 /*-------------------------------------------------------------------------
323  * Function:    H5HL__cache_prefix_get_initial_load_size()
324  *
325  * Purpose:	Return the initial size of the buffer the metadata cache should
326  *		load from file and pass to the deserialize routine.
327  *
328  * Return:      Success:        SUCCEED
329  *              Failure:        FAIL
330  *
331  * Programmer:  John Mainzer
332  *              6/21/14
333  *
334  *-------------------------------------------------------------------------
335  */
336 static herr_t
H5HL__cache_prefix_get_initial_load_size(void H5_ATTR_UNUSED * _udata,size_t * image_len)337 H5HL__cache_prefix_get_initial_load_size(void H5_ATTR_UNUSED *_udata, size_t *image_len)
338 {
339     FUNC_ENTER_STATIC_NOERR
340 
341     /* Sanity check */
342     HDassert(image_len);
343 
344     /* Set the image length size */
345     *image_len = H5HL_SPEC_READ_SIZE;
346 
347     FUNC_LEAVE_NOAPI(SUCCEED)
348 } /* end H5HL__cache_prefix_get_initial_load_size() */
349 
350 
351 /*-------------------------------------------------------------------------
352  * Function:    H5HL__cache_prefix_get_final_load_size()
353  *
354  * Purpose:	Return the final size of the buffer the metadata cache should
355  *		load from file and pass to the deserialize routine.
356  *
357  * Return:      Success:        SUCCEED
358  *              Failure:        FAIL
359  *
360  * Programmer:  Quincey Koziol
361  *              November 18, 2016
362  *
363  *-------------------------------------------------------------------------
364  */
365 static herr_t
H5HL__cache_prefix_get_final_load_size(const void * _image,size_t image_len,void * _udata,size_t * actual_len)366 H5HL__cache_prefix_get_final_load_size(const void *_image, size_t image_len,
367     void *_udata, size_t *actual_len)
368 {
369     const uint8_t *image = (const uint8_t *)_image;   			/* Pointer into raw data buffer */
370     H5HL_cache_prfx_ud_t *udata = (H5HL_cache_prfx_ud_t *)_udata; 	/* User data for callback */
371     H5HL_t heap;        	/* Local heap */
372     herr_t ret_value = SUCCEED; /* Return value */
373 
374     FUNC_ENTER_STATIC
375 
376     /* Sanity checks */
377     HDassert(image);
378     HDassert(udata);
379     HDassert(actual_len);
380     HDassert(*actual_len == image_len);
381 
382     /* Deserialize the heap's header */
383     if(H5HL__hdr_deserialize(&heap, (const uint8_t *)image, udata) < 0)
384         HGOTO_ERROR(H5E_HEAP, H5E_CANTDECODE, FAIL, "can't decode local heap header")
385 
386     /* Set the final size for the cache image */
387     *actual_len = heap.prfx_size;
388 
389     /* Check if heap block exists */
390     if(heap.dblk_size)
391         /* Check if heap data block is contiguous with header */
392         if(H5F_addr_eq((heap.prfx_addr + heap.prfx_size), heap.dblk_addr))
393             /* Note that the heap should be a single object in the cache */
394             *actual_len += heap.dblk_size;
395 
396 done:
397     FUNC_LEAVE_NOAPI(ret_value)
398 } /* end H5HL__cache_prefix_get_final_load_size() */
399 
400 
401 /*-------------------------------------------------------------------------
402  * Function:    H5HL__cache_prefix_deserialize
403  *
404  * Purpose:	Given a buffer containing the on disk image of the local
405  *		heap prefix, deserialize it, load its contents into a newly allocated
406  *		instance of H5HL_prfx_t, and return a pointer to the new instance.
407  *
408  * Return:      Success:        Pointer to in core representation
409  *              Failure:        NULL
410  *
411  * Programmer:  John Mainzer
412  *              6/21/14
413  *
414  *-------------------------------------------------------------------------
415  */
416 static void *
H5HL__cache_prefix_deserialize(const void * _image,size_t len,void * _udata,hbool_t H5_ATTR_UNUSED * dirty)417 H5HL__cache_prefix_deserialize(const void *_image, size_t len, void *_udata,
418     hbool_t H5_ATTR_UNUSED *dirty)
419 {
420     H5HL_t               *heap = NULL;  /* Local heap */
421     H5HL_prfx_t          *prfx = NULL;  /* Heap prefix deserialized */
422     const uint8_t        *image = (const uint8_t *)_image;      /* Pointer into decoding buffer */
423     H5HL_cache_prfx_ud_t *udata = (H5HL_cache_prfx_ud_t *)_udata;       /* User data for callback */
424     void                 *ret_value = NULL;     /* Return value */
425 
426     FUNC_ENTER_STATIC
427 
428     /* Check arguments */
429     HDassert(image);
430     HDassert(len > 0);
431     HDassert(udata);
432     HDassert(udata->sizeof_size > 0);
433     HDassert(udata->sizeof_addr > 0);
434     HDassert(udata->sizeof_prfx > 0);
435     HDassert(H5F_addr_defined(udata->prfx_addr));
436     HDassert(dirty);
437 
438     /* Allocate space in memory for the heap */
439     if(NULL == (heap = H5HL__new(udata->sizeof_size, udata->sizeof_addr, udata->sizeof_prfx)))
440         HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, NULL, "can't allocate local heap structure");
441 
442     /* Deserialize the heap's header */
443     if(H5HL__hdr_deserialize(heap, (const uint8_t *)image, udata) < 0)
444         HGOTO_ERROR(H5E_HEAP, H5E_CANTDECODE, NULL, "can't decode local heap header")
445 
446     /* Allocate the heap prefix */
447     if(NULL == (prfx = H5HL__prfx_new(heap)))
448         HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, NULL, "can't allocate local heap prefix");
449 
450     /* Check if heap block exists */
451     if(heap->dblk_size) {
452         /* Check if heap data block is contiguous with header */
453         if(H5F_addr_eq((heap->prfx_addr + heap->prfx_size), heap->dblk_addr)) {
454             /* Note that the heap should be a single object in the cache */
455             heap->single_cache_obj = TRUE;
456 
457             /* Allocate space for the heap data image */
458             if(NULL == (heap->dblk_image = H5FL_BLK_MALLOC(lheap_chunk, heap->dblk_size)))
459                 HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, NULL, "memory allocation failed")
460 
461             /* Set image to the start of the data block.  This is necessary
462              * because there may be a gap between the used portion of the
463              * prefix and the data block due to alignment constraints. */
464             image = ((const uint8_t *)_image) + heap->prfx_size;
465 
466             /* Copy the heap data from the speculative read buffer */
467             HDmemcpy(heap->dblk_image, image, heap->dblk_size);
468 
469             /* Build free list */
470             if(H5HL__fl_deserialize(heap) < 0)
471                 HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, NULL, "can't initialize free list")
472         } /* end if */
473         else
474             /* Note that the heap should _NOT_ be a single
475              * object in the cache
476              */
477             heap->single_cache_obj = FALSE;
478     } /* end if */
479 
480     /* Set return value */
481     ret_value = prfx;
482 
483 done:
484     /* Release the [possibly partially initialized] local heap on errors */
485     if(!ret_value) {
486         if(prfx) {
487             if(FAIL == H5HL__prfx_dest(prfx))
488                 HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, NULL, "unable to destroy local heap prefix");
489         } /* end if */
490         else {
491             if(heap && FAIL == H5HL__dest(heap))
492                 HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, NULL, "unable to destroy local heap");
493         } /* end else */
494     } /* end if */
495 
496     FUNC_LEAVE_NOAPI(ret_value)
497 } /* end H5HL__cache_prefix_deserialize() */
498 
499 
500 /*-------------------------------------------------------------------------
501  * Function:    H5HL__cache_prefix_image_len
502  *
503  * Purpose:	Return the on disk image size of a local heap prefix to the
504  *		metadata cache via the image_len.
505  *
506  * Return:      Success:        SUCCEED
507  *              Failure:        FAIL
508  *
509  * Programmer:  John Mainzer
510  *              6/21/14
511  *
512  *-------------------------------------------------------------------------
513  */
514 static herr_t
H5HL__cache_prefix_image_len(const void * _thing,size_t * image_len)515 H5HL__cache_prefix_image_len(const void *_thing, size_t *image_len)
516 {
517     const H5HL_prfx_t *prfx = (const H5HL_prfx_t *)_thing;  /* Pointer to local heap prefix to query */
518 
519     FUNC_ENTER_STATIC_NOERR
520 
521     /* Check arguments */
522     HDassert(prfx);
523     HDassert(prfx->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
524     HDassert(prfx->cache_info.type == H5AC_LHEAP_PRFX);
525     HDassert(image_len);
526 
527     /* Set the prefix's size */
528     *image_len = prfx->heap->prfx_size;
529 
530     /* If the heap is stored as a single object, add in the
531      * data block size also
532      */
533     if(prfx->heap->single_cache_obj)
534         *image_len += prfx->heap->dblk_size;
535 
536     FUNC_LEAVE_NOAPI(SUCCEED)
537 } /* end H5HL__cache_prefix_image_len() */
538 
539 
540 /*-------------------------------------------------------------------------
541  * Function:    H5HL__cache_prefix_serialize
542  *
543  * Purpose:	Given a pointer to an instance of H5HL_prfx_t and an
544  *		appropriately sized buffer, serialize the contents of the
545  *		instance for writing to disk, and copy the serialized data
546  *		into the buffer.
547  *
548  * Return:      Success:        SUCCEED
549  *              Failure:        FAIL
550  *
551  * Programmer:  John Mainzer
552  *              7/21/14
553  *
554  *-------------------------------------------------------------------------
555  */
556 static herr_t
H5HL__cache_prefix_serialize(const H5F_t * f,void * _image,size_t len,void * _thing)557 H5HL__cache_prefix_serialize(const H5F_t *f, void *_image, size_t len,
558     void *_thing)
559 {
560     H5HL_prfx_t *prfx = (H5HL_prfx_t *)_thing;  /* Pointer to local heap prefix to query */
561     H5HL_t      *heap;          /* Pointer to the local heap */
562     uint8_t     *image = (uint8_t *)_image;     /* Pointer into image buffer */
563     size_t       buf_size;      /* expected size of the image buffer */
564 
565     FUNC_ENTER_STATIC_NOERR
566 
567     /* Check arguments */
568     HDassert(f);
569     HDassert(image);
570     HDassert(prfx);
571     HDassert(prfx->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
572     HDassert(prfx->cache_info.type == H5AC_LHEAP_PRFX);
573     HDassert(H5F_addr_eq(prfx->cache_info.addr, prfx->heap->prfx_addr));
574     HDassert(prfx->heap);
575 
576     /* Get the pointer to the heap */
577     heap = prfx->heap;
578     HDassert(heap);
579 
580     /* Compute the buffer size */
581     buf_size = heap->prfx_size;
582     if(heap->single_cache_obj)
583         buf_size += heap->dblk_size;
584     HDassert(len == buf_size);
585 
586     /* Update the free block value from the free list */
587     heap->free_block = heap->freelist ? heap->freelist->offset : H5HL_FREE_NULL;
588 
589     /* Serialize the heap prefix */
590     HDmemcpy(image, H5HL_MAGIC, (size_t)H5_SIZEOF_MAGIC);
591     image += H5_SIZEOF_MAGIC;
592     *image++ = H5HL_VERSION;
593     *image++ = 0;       /*reserved*/
594     *image++ = 0;       /*reserved*/
595     *image++ = 0;       /*reserved*/
596     H5F_ENCODE_LENGTH_LEN(image, heap->dblk_size, heap->sizeof_size);
597     H5F_ENCODE_LENGTH_LEN(image, heap->free_block, heap->sizeof_size);
598     H5F_addr_encode_len(heap->sizeof_addr, &image, heap->dblk_addr);
599 
600     /* Check if the local heap is a single object in cache */
601     if(heap->single_cache_obj) {
602         if((size_t)(image - (uint8_t *)_image) < heap->prfx_size) {
603             size_t gap;         /* Size of gap between prefix and data block */
604 
605             /* Set image to the start of the data block.  This is necessary
606              * because there may be a gap between the used portion of
607              * the prefix and the data block due to alignment constraints.
608              */
609             gap = heap->prfx_size - (size_t)(image - (uint8_t *)_image);
610             HDmemset(image, 0, gap);
611             image += gap;
612         } /* end if */
613 
614         /* Serialize the free list into the heap data's image */
615         H5HL__fl_serialize(heap);
616 
617         /* Copy the heap data block into the cache image */
618         HDmemcpy(image, heap->dblk_image, heap->dblk_size);
619 
620         /* Sanity check */
621         HDassert((size_t)(image - (uint8_t *)_image) + heap->dblk_size == len);
622     } /* end if */
623     else {
624         /* Sanity check */
625         HDassert((size_t)(image - (uint8_t *)_image) <= len);
626 
627         /* Clear rest of local heap */
628         HDmemset(image, 0, len - (size_t)(image - (uint8_t *)_image));
629     } /* end else */
630 
631     FUNC_LEAVE_NOAPI(SUCCEED)
632 } /* end H5HL__cache_prefix_serialize() */
633 
634 
635 /*-------------------------------------------------------------------------
636  * Function:    H5HL__cache_prefix_free_icr
637  *
638  * Purpose:	Free the supplied in core representation of a local heap
639  *		prefix.
640  *
641  *		Note that this function handles the partially initialize prefix
642  *		from a failed speculative load attempt.  See comments below for
643  *		details.
644  *
645  * Note:	The metadata cache sets the object's cache_info.magic to
646  *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
647  *		callback (checked in assert).
648  *
649  * Return:      Success:        SUCCEED
650  *              Failure:        FAIL
651  *
652  * Programmer:  John Mainzer
653  *              6/21/14
654  *
655  *-------------------------------------------------------------------------
656  */
657 static herr_t
H5HL__cache_prefix_free_icr(void * _thing)658 H5HL__cache_prefix_free_icr(void *_thing)
659 {
660     H5HL_prfx_t *prfx = (H5HL_prfx_t *)_thing;  /* Pointer to local heap prefix to query */
661     herr_t       ret_value = SUCCEED;    /* Return value */
662 
663     FUNC_ENTER_STATIC
664 
665     /* Check arguments */
666     HDassert(prfx);
667     HDassert(prfx->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
668     HDassert(prfx->cache_info.type == H5AC_LHEAP_PRFX);
669     HDassert(H5F_addr_eq(prfx->cache_info.addr, prfx->heap->prfx_addr));
670 
671     /* Destroy local heap prefix */
672     if(H5HL__prfx_dest(prfx) < 0)
673         HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, FAIL, "can't destroy local heap prefix")
674 
675 done:
676     FUNC_LEAVE_NOAPI(ret_value)
677 } /* end H5HL__cache_prefix_free_icr() */
678 
679 
680 /*-------------------------------------------------------------------------
681  * Function:    H5HL__cache_datablock_get_initial_load_size()
682  *
683  * Purpose:	Tell the metadata cache how large a buffer to read from
684  *		file when loading a datablock.  In this case, we simply lookup
685  *		the correct value in the user data, and return it in *image_len.
686  *
687  * Return:      Success:        SUCCEED
688  *              Failure:        FAIL
689  *
690  * Programmer:  John Mainzer
691  *              6/21/14
692  *
693  *-------------------------------------------------------------------------
694  */
695 static herr_t
H5HL__cache_datablock_get_initial_load_size(void * _udata,size_t * image_len)696 H5HL__cache_datablock_get_initial_load_size(void *_udata, size_t *image_len)
697 {
698     H5HL_t *heap = (H5HL_t *)_udata;    /* User data for callback */
699 
700     FUNC_ENTER_STATIC_NOERR
701 
702     /* Check arguments */
703     HDassert(heap);
704     HDassert(heap->dblk_size > 0);
705     HDassert(image_len);
706 
707     /* Set the image length size */
708     *image_len = heap->dblk_size;
709 
710     FUNC_LEAVE_NOAPI(SUCCEED)
711 } /* end H5HL__cache_datablock_get_initial_load_size() */
712 
713 
714 /*-------------------------------------------------------------------------
715  * Function:    H5HL__cache_datablock_deserialize
716  *
717  * Purpose:	Given a buffer containing the on disk image of a local
718  *		heap data block, deserialize it, load its contents into a newly allocated
719  *		instance of H5HL_dblk_t, and return a pointer to the new instance.
720  *
721  * Return:      Success:        Pointer to in core representation
722  *              Failure:        NULL
723  *
724  * Programmer:  John Mainzer
725  *              6/21/14
726  *
727  *-------------------------------------------------------------------------
728  */
729 static void *
H5HL__cache_datablock_deserialize(const void * image,size_t len,void * _udata,hbool_t H5_ATTR_UNUSED * dirty)730 H5HL__cache_datablock_deserialize(const void *image, size_t len, void *_udata,
731     hbool_t H5_ATTR_UNUSED *dirty)
732 {
733     H5HL_dblk_t          *dblk = NULL;          /* Local heap data block deserialized */
734     H5HL_t *heap = (H5HL_t *)_udata;            /* User data for callback */
735     void                 *ret_value = NULL;     /* Return value */
736 
737     FUNC_ENTER_STATIC
738 
739     /* Check arguments */
740     HDassert(image);
741     HDassert(len > 0);
742     HDassert(heap);
743     HDassert(heap->dblk_size == len);
744     HDassert(!heap->single_cache_obj);
745     HDassert(NULL == heap->dblk);
746     HDassert(dirty);
747 
748     /* Allocate space in memory for the heap data block */
749     if(NULL == (dblk = H5HL__dblk_new(heap)))
750         HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, NULL, "memory allocation failed");
751 
752     /* Check for heap still retaining image */
753     if(NULL == heap->dblk_image) {
754         /* Allocate space for the heap data image */
755         if(NULL == (heap->dblk_image = H5FL_BLK_MALLOC(lheap_chunk, heap->dblk_size)))
756             HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, NULL, "can't allocate data block image buffer");
757 
758         /* copy the datablock from the read buffer */
759         HDmemcpy(heap->dblk_image, image, len);
760 
761         /* Build free list */
762         if(FAIL == H5HL__fl_deserialize(heap))
763             HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, NULL, "can't initialize free list");
764     } /* end if */
765 
766     /* Set return value */
767     ret_value = dblk;
768 
769 done:
770     /* Release the [possibly partially initialized] local heap on errors */
771     if(!ret_value && dblk)
772         if(FAIL == H5HL__dblk_dest(dblk))
773             HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, NULL, "unable to destroy local heap data block");
774 
775     FUNC_LEAVE_NOAPI(ret_value)
776 } /* end H5HL__cache_datablock_deserialize() */
777 
778 
779 /*-------------------------------------------------------------------------
780  * Function:    H5HL__cache_datablock_image_len
781  *
782  * Purpose:	Return the size of the on disk image of the datablock.
783  *
784  * Return:      Success:        SUCCEED
785  *              Failure:        FAIL
786  *
787  * Programmer:  John Mainzer
788  *              6/21/14
789  *
790  *-------------------------------------------------------------------------
791  */
792 static herr_t
H5HL__cache_datablock_image_len(const void * _thing,size_t * image_len)793 H5HL__cache_datablock_image_len(const void *_thing, size_t *image_len)
794 {
795     const H5HL_dblk_t *dblk = (const H5HL_dblk_t *)_thing;    /* Pointer to the local heap data block */
796 
797     FUNC_ENTER_STATIC_NOERR
798 
799     /* Check arguments */
800     HDassert(dblk);
801     HDassert(dblk->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
802     HDassert(dblk->cache_info.type == H5AC_LHEAP_DBLK);
803     HDassert(dblk->heap);
804     HDassert(dblk->heap->dblk_size > 0);
805     HDassert(image_len);
806 
807     *image_len = dblk->heap->dblk_size;
808 
809     FUNC_LEAVE_NOAPI(SUCCEED)
810 } /* end H5HL__cache_datablock_image_len() */
811 
812 
813 /*-------------------------------------------------------------------------
814  * Function:    H5HL__cache_datablock_serialize
815  *
816  * Purpose:	Serialize the supplied datablock, and copy the serialized
817  *		image into the supplied image buffer.
818  *
819  * Return:      Success:        SUCCEED
820  *              Failure:        FAIL
821  *
822  * Programmer:  John Mainzer
823  *              6/21/14
824  *
825  *-------------------------------------------------------------------------
826  */
827 static herr_t
H5HL__cache_datablock_serialize(const H5F_t * f,void * image,size_t len,void * _thing)828 H5HL__cache_datablock_serialize(const H5F_t *f, void *image, size_t len,
829     void *_thing)
830 {
831     H5HL_t      *heap;          /* Pointer to the local heap */
832     H5HL_dblk_t *dblk = (H5HL_dblk_t *)_thing;  /* Pointer to the local heap data block */
833 
834     FUNC_ENTER_STATIC_NOERR
835 
836     /* Check arguments */
837     HDassert(f);
838     HDassert(image);
839     HDassert(dblk);
840     HDassert(dblk->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
841     HDassert(dblk->cache_info.type == H5AC_LHEAP_DBLK);
842     HDassert(dblk->heap);
843     heap = dblk->heap;
844     HDassert(heap->dblk_size == len);
845     HDassert(!heap->single_cache_obj);
846 
847     /* Update the free block value from the free list */
848     heap->free_block = heap->freelist ? heap->freelist->offset : H5HL_FREE_NULL;
849 
850     /* Serialize the free list into the heap data's image */
851     H5HL__fl_serialize(heap);
852 
853     /* Copy the heap's data block into the cache's image */
854     HDmemcpy(image, heap->dblk_image, heap->dblk_size);
855 
856     FUNC_LEAVE_NOAPI(SUCCEED)
857 } /* end H5HL__cache_datablock_serialize() */
858 
859 
860 /*-------------------------------------------------------------------------
861  * Function:	H5HL__cache_datablock_notify
862  *
863  * Purpose:	This function is used to create and destroy pinned
864  *		relationships between datablocks and their prefix parent.
865  *
866  * Return:	Success:	SUCCEED
867  *		Failure:	FAIL
868  *
869  * Programmer:	Quincey Koziol
870  *		November 19, 2016
871  *
872  *-------------------------------------------------------------------------
873  */
874 static herr_t
H5HL__cache_datablock_notify(H5C_notify_action_t action,void * _thing)875 H5HL__cache_datablock_notify(H5C_notify_action_t action, void *_thing)
876 {
877     H5HL_dblk_t *dblk = (H5HL_dblk_t *)_thing;  /* Pointer to the local heap data block */
878     herr_t      	 ret_value = SUCCEED;   /* Return value */
879 
880     FUNC_ENTER_STATIC
881 
882     /* Sanity check */
883     HDassert(dblk);
884 
885     switch(action) {
886         case H5AC_NOTIFY_ACTION_AFTER_INSERT:
887 	    /* do nothing */
888 	    break;
889 
890         case H5AC_NOTIFY_ACTION_AFTER_LOAD:
891             /* Sanity checks */
892             HDassert(dblk->heap);
893             HDassert(dblk->heap->prfx);
894 
895             /* Pin the heap's prefix */
896             if(FAIL == H5AC_pin_protected_entry(dblk->heap->prfx))
897                 HGOTO_ERROR(H5E_HEAP, H5E_CANTPIN, FAIL, "unable to pin local heap prefix")
898             break;
899 
900 	case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
901         case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
902         case H5AC_NOTIFY_ACTION_ENTRY_CLEANED:
903         case H5AC_NOTIFY_ACTION_CHILD_DIRTIED:
904         case H5AC_NOTIFY_ACTION_CHILD_CLEANED:
905         case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED:
906         case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED:
907 	    /* do nothing */
908 	    break;
909 
910         case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
911             /* Sanity checks */
912             HDassert(dblk->heap);
913             HDassert(dblk->heap->prfx);
914 
915             /* Unpin the local heap prefix */
916             if(FAIL == H5AC_unpin_entry(dblk->heap->prfx))
917                 HGOTO_ERROR(H5E_HEAP, H5E_CANTUNPIN, FAIL, "unable to unpin local heap prefix")
918             break;
919 
920         default:
921             HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unknown action from metadata cache")
922             break;
923     } /* end switch */
924 
925 done:
926     FUNC_LEAVE_NOAPI(ret_value)
927 } /* end H5HL__cache_datablock_notify() */
928 
929 
930 /*-------------------------------------------------------------------------
931  * Function:    H5HL__cache_datablock_free_icr
932  *
933  * Purpose:	Free the in memory representation of the supplied local heap data block.
934  *
935  * Note:	The metadata cache sets the object's cache_info.magic to
936  *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
937  *		callback (checked in assert).
938  *
939  * Return:      Success:        SUCCEED
940  *              Failure:        FAIL
941  *
942  * Programmer:  John Mainzer
943  *              6/21/14
944  *
945  *-------------------------------------------------------------------------
946  */
947 static herr_t
H5HL__cache_datablock_free_icr(void * _thing)948 H5HL__cache_datablock_free_icr(void *_thing)
949 {
950     H5HL_dblk_t *dblk = (H5HL_dblk_t *)_thing; /* Pointer to the local heap data block */
951     herr_t       ret_value = SUCCEED;    /* Return value */
952 
953     FUNC_ENTER_STATIC
954 
955     /* Check arguments */
956     HDassert(dblk);
957     HDassert(dblk->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
958     HDassert(dblk->cache_info.type == H5AC_LHEAP_DBLK);
959 
960     /* Destroy the data block */
961     if(H5HL__dblk_dest(dblk) < 0)
962         HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to destroy local heap data block")
963 
964 done:
965     FUNC_LEAVE_NOAPI(ret_value)
966 } /* end H5HL__cache_datablock_free_icr() */
967 
968