@@ -599,7 +599,8 @@ H5MF__close_fstype(H5F_t *f, H5F_mem_page_t type)
599599 *-------------------------------------------------------------------------
600600 */
601601herr_t
602- H5MF__add_sect (H5F_t * f , H5FD_mem_t alloc_type , H5FS_t * fspace , H5MF_free_section_t * node )
602+ H5MF__add_sect (H5F_t * f , H5FD_mem_t alloc_type , H5FS_t * fspace , H5MF_free_section_t * node ,
603+ bool * merged_or_shrunk )
603604{
604605 H5AC_ring_t orig_ring = H5AC_RING_INV ; /* Original ring value */
605606 H5AC_ring_t fsm_ring = H5AC_RING_INV ; /* Ring of FSM */
@@ -631,7 +632,8 @@ H5MF__add_sect(H5F_t *f, H5FD_mem_t alloc_type, H5FS_t *fspace, H5MF_free_sectio
631632 __func__ , node -> sect_info .addr , node -> sect_info .size );
632633#endif /* H5MF_ALLOC_DEBUG_MORE */
633634 /* Add the section */
634- if (H5FS_sect_add (f , fspace , (H5FS_section_info_t * )node , H5FS_ADD_RETURNED_SPACE , & udata ) < 0 )
635+ if (H5FS_sect_add (f , fspace , (H5FS_section_info_t * )node , H5FS_ADD_RETURNED_SPACE , & udata ,
636+ merged_or_shrunk ) < 0 )
635637 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , FAIL , "can't re-add section to file free space" );
636638
637639done :
@@ -711,8 +713,11 @@ H5MF__find_sect(H5F_t *f, H5FD_mem_t alloc_type, hsize_t size, H5FS_t *fspace, h
711713#endif /* H5MF_ALLOC_DEBUG_MORE */
712714
713715 /* Re-add the section to the free-space manager */
714- if (H5MF__add_sect (f , alloc_type , fspace , node ) < 0 )
716+ if (H5MF__add_sect (f , alloc_type , fspace , node , NULL ) < 0 ) {
717+ node -> sect_info .addr -= size ;
718+ node -> sect_info .size += size ;
715719 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , FAIL , "can't re-add section to file free space" );
720+ }
716721 } /* end else */
717722 } /* end if */
718723
@@ -852,9 +857,10 @@ H5MF_alloc(H5F_t *f, H5FD_mem_t alloc_type, hsize_t size)
852857static haddr_t
853858H5MF__alloc_pagefs (H5F_t * f , H5FD_mem_t alloc_type , hsize_t size )
854859{
855- H5F_mem_page_t ptype ; /* Free-space manager type */
856- H5MF_free_section_t * node = NULL ; /* Free space section pointer */
857- haddr_t ret_value = HADDR_UNDEF ; /* Return value */
860+ H5F_mem_page_t ptype ; /* Free-space manager type */
861+ H5MF_free_section_t * node = NULL ; /* Free space section pointer */
862+ bool section_merged_or_shrunk = false; /* Whether free space section was merged or shrunk away */
863+ haddr_t ret_value = HADDR_UNDEF ; /* Return value */
858864
859865 FUNC_ENTER_PACKAGE
860866
@@ -900,9 +906,13 @@ H5MF__alloc_pagefs(H5F_t *f, H5FD_mem_t alloc_type, hsize_t size)
900906 "can't initialize free space section" );
901907
902908 /* Add the fragment to the large free-space manager */
903- if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [ptype ], node ) < 0 )
909+ if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [ptype ], node , & section_merged_or_shrunk ) <
910+ 0 ) {
911+ if (section_merged_or_shrunk )
912+ node = NULL ;
904913 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , HADDR_UNDEF ,
905914 "can't re-add section to file free space" );
915+ }
906916
907917 node = NULL ;
908918 } /* end if */
@@ -931,9 +941,12 @@ H5MF__alloc_pagefs(H5F_t *f, H5FD_mem_t alloc_type, hsize_t size)
931941 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINIT , HADDR_UNDEF , "can't initialize free space section" );
932942
933943 /* Add the remaining space in the page to the manager */
934- if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [ptype ], node ) < 0 )
944+ if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [ptype ], node , & section_merged_or_shrunk ) < 0 ) {
945+ if (section_merged_or_shrunk )
946+ node = NULL ;
935947 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , HADDR_UNDEF ,
936948 "can't re-add section to file free space" );
949+ }
937950
938951 node = NULL ;
939952
@@ -1154,15 +1167,21 @@ H5MF_xfree(H5F_t *f, H5FD_mem_t alloc_type, haddr_t addr, hsize_t size)
11541167
11551168 /* If size of the freed section is larger than threshold, add it to the free space manager */
11561169 if (size >= f -> shared -> fs_threshold ) {
1170+ bool section_merged_or_shrunk = false; /* Whether free space section was merged or shrunk away */
1171+
11571172 assert (f -> shared -> fs_man [fs_type ]);
11581173
11591174#ifdef H5MF_ALLOC_DEBUG_MORE
11601175 fprintf (stderr , "%s: Before H5FS_sect_add()\n" , __func__ );
11611176#endif /* H5MF_ALLOC_DEBUG_MORE */
11621177
11631178 /* Add to the free space for the file */
1164- if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [fs_type ], node ) < 0 )
1179+ if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [fs_type ], node , & section_merged_or_shrunk ) < 0 ) {
1180+ if (section_merged_or_shrunk )
1181+ node = NULL ;
11651182 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , FAIL , "can't add section to file free space" );
1183+ }
1184+
11661185 node = NULL ;
11671186
11681187#ifdef H5MF_ALLOC_DEBUG_MORE
@@ -1316,7 +1335,7 @@ H5MF_try_extend(H5F_t *f, H5FD_mem_t alloc_type, haddr_t addr, hsize_t size, hsi
13161335 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINIT , FAIL , "can't initialize free space section" );
13171336
13181337 /* Add the fragment to the large-sized free-space manager */
1319- if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [fs_type ], node ) < 0 )
1338+ if (H5MF__add_sect (f , alloc_type , f -> shared -> fs_man [fs_type ], node , NULL ) < 0 )
13201339 HGOTO_ERROR (H5E_RESOURCE , H5E_CANTINSERT , FAIL , "can't re-add section to file free space" );
13211340
13221341 node = NULL ;
@@ -3059,8 +3078,13 @@ H5MF_settle_meta_data_fsm(H5F_t *f, bool *fsm_settled)
30593078 assert (sm_fssinfo_fs_type > H5F_MEM_PAGE_DEFAULT );
30603079 assert (sm_fssinfo_fs_type < H5F_MEM_PAGE_LARGE_SUPER );
30613080
3062- assert (!H5_addr_defined (f -> shared -> fs_addr [sm_fshdr_fs_type ]));
3063- assert (!H5_addr_defined (f -> shared -> fs_addr [sm_fssinfo_fs_type ]));
3081+ if (H5_addr_defined (f -> shared -> fs_addr [sm_fshdr_fs_type ]))
3082+ HGOTO_ERROR (H5E_FSPACE , H5E_BADVALUE , FAIL ,
3083+ "small free space header block manager should not have had file space allocated" );
3084+ if (H5_addr_defined (f -> shared -> fs_addr [sm_fssinfo_fs_type ]))
3085+ HGOTO_ERROR (
3086+ H5E_FSPACE , H5E_BADVALUE , FAIL ,
3087+ "small free space serialized section manager should not have had file space allocated" );
30643088
30653089 /* Note that in most cases, sm_hdr_fspace will equal sm_sinfo_fspace. */
30663090 sm_hdr_fspace = f -> shared -> fs_man [sm_fshdr_fs_type ];
@@ -3078,8 +3102,13 @@ H5MF_settle_meta_data_fsm(H5F_t *f, bool *fsm_settled)
30783102 assert (lg_fssinfo_fs_type >= H5F_MEM_PAGE_LARGE_SUPER );
30793103 assert (lg_fssinfo_fs_type < H5F_MEM_PAGE_NTYPES );
30803104
3081- assert (!H5_addr_defined (f -> shared -> fs_addr [lg_fshdr_fs_type ]));
3082- assert (!H5_addr_defined (f -> shared -> fs_addr [lg_fssinfo_fs_type ]));
3105+ if (H5_addr_defined (f -> shared -> fs_addr [lg_fshdr_fs_type ]))
3106+ HGOTO_ERROR (H5E_FSPACE , H5E_BADVALUE , FAIL ,
3107+ "large free space header block manager should not have had file space allocated" );
3108+ if (H5_addr_defined (f -> shared -> fs_addr [lg_fssinfo_fs_type ]))
3109+ HGOTO_ERROR (
3110+ H5E_FSPACE , H5E_BADVALUE , FAIL ,
3111+ "large free space serialized section manager should not have had file space allocated" );
30833112
30843113 /* Note that in most cases, lg_hdr_fspace will equal lg_sinfo_fspace. */
30853114 lg_hdr_fspace = f -> shared -> fs_man [lg_fshdr_fs_type ];
0 commit comments