#define H5F_FRIEND
#include "H5FSmodule.h"
#include "H5private.h"
#include "H5Eprivate.h"
#include "H5Fpkg.h"
#include "H5FSpkg.h"
#include "H5MFprivate.h"
#include "H5VMprivate.h"
typedef struct {
H5FS_t *fspace;
H5FS_operator_t op;
void *op_data;
} H5FS_iter_ud_t;
static herr_t H5FS__sect_increase(H5FS_t *fspace, const H5FS_section_class_t *cls,
unsigned flags);
static herr_t H5FS__sect_decrease(H5FS_t *fspace, const H5FS_section_class_t *cls);
static herr_t H5FS__size_node_decr(H5FS_sinfo_t *sinfo, unsigned bin, H5FS_node_t *fspace_node,
const H5FS_section_class_t *cls);
static herr_t H5FS__sect_unlink_size(H5FS_sinfo_t *sinfo, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect);
static herr_t H5FS__sect_unlink_rest(H5FS_t *fspace,
const H5FS_section_class_t *cls, H5FS_section_info_t *sect);
static herr_t H5FS__sect_remove_real(H5FS_t *fspace, H5FS_section_info_t *sect);
static herr_t H5FS__sect_link_size(H5FS_sinfo_t *sinfo, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect);
static herr_t H5FS__sect_link_rest(H5FS_t *fspace, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect, unsigned flags);
static herr_t H5FS__sect_link(H5FS_t *fspace, H5FS_section_info_t *sect,
unsigned flags);
static herr_t H5FS__sect_merge(H5FS_t *fspace, H5FS_section_info_t **sect,
void *op_data);
static htri_t H5FS__sect_find_node(H5FS_t *fspace, hsize_t request, H5FS_section_info_t **node);
static herr_t H5FS__sect_serialize_size(H5FS_t *fspace);
H5FL_DEFINE(H5FS_node_t);
H5FL_SEQ_DEFINE(H5FS_bin_t);
H5FL_DEFINE(H5FS_sinfo_t);
H5FS_sinfo_t *
H5FS__sinfo_new(H5F_t *f, H5FS_t *fspace)
{
H5FS_sinfo_t *sinfo = NULL;
H5FS_sinfo_t *ret_value = NULL;
FUNC_ENTER_PACKAGE
HDassert(f);
HDassert(fspace);
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: fspace->addr = %a\n", FUNC, fspace->addr);
#endif
if(NULL == (sinfo = H5FL_CALLOC(H5FS_sinfo_t)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
sinfo->nbins = H5VM_log2_gen(fspace->max_sect_size);
sinfo->sect_prefix_size = H5FS_SINFO_PREFIX_SIZE(f);
sinfo->sect_off_size = (fspace->max_sect_addr + 7) / 8;
sinfo->sect_len_size = H5VM_limit_enc_size((uint64_t)fspace->max_sect_size);
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: fspace->max_sect_size = %Hu\n", FUNC, fspace->max_sect_size);
HDfprintf(stderr, "%s: fspace->max_sect_addr = %u\n", FUNC, fspace->max_sect_addr);
HDfprintf(stderr, "%s: sinfo->nbins = %u\n", FUNC, sinfo->nbins);
HDfprintf(stderr, "%s: sinfo->sect_off_size = %u, sinfo->sect_len_size = %u\n", FUNC, sinfo->sect_off_size, sinfo->sect_len_size);
#endif
if(NULL == (sinfo->bins = H5FL_SEQ_CALLOC(H5FS_bin_t, (size_t)sinfo->nbins)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for free space section bin array")
if(H5FS__incr(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINC, NULL, "unable to increment ref. count on free space header")
sinfo->fspace = fspace;
HDassert(fspace->sinfo == NULL);
fspace->sinfo = sinfo;
ret_value = sinfo;
done:
if(ret_value == NULL && sinfo) {
if(sinfo->bins)
sinfo->bins = H5FL_SEQ_FREE(H5FS_bin_t, sinfo->bins);
sinfo = H5FL_FREE(H5FS_sinfo_t, sinfo);
}
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sinfo_lock(H5F_t *f, H5FS_t *fspace, unsigned accmode)
{
H5FS_sinfo_cache_ud_t cache_udata;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Called, fspace->addr = %a, fspace->sinfo = %p, fspace->sect_addr = %a\n", FUNC, fspace->addr, fspace->sinfo, fspace->sect_addr);
HDfprintf(stderr, "%s: fspace->alloc_sect_size = %Hu, fspace->sect_size = %Hu\n", FUNC, fspace->alloc_sect_size, fspace->sect_size);
#endif
HDassert(f);
HDassert(fspace);
HDassert((accmode & (unsigned)(~H5AC__READ_ONLY_FLAG)) == 0);
if(fspace->sinfo) {
HDassert(((fspace->sinfo_accmode) & (unsigned)(~H5AC__READ_ONLY_FLAG)) == 0);
if(fspace->sinfo_protected && accmode != fspace->sinfo_accmode) {
if(0 == (accmode & (unsigned)(~H5AC__READ_ONLY_FLAG))) {
if(H5AC_unprotect(f, H5AC_FSPACE_SINFO, fspace->sect_addr, fspace->sinfo, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTUNPROTECT, FAIL, "unable to release free space section info")
cache_udata.f = f;
cache_udata.fspace = fspace;
if(NULL == (fspace->sinfo = (H5FS_sinfo_t *)H5AC_protect(f, H5AC_FSPACE_SINFO, fspace->sect_addr, &cache_udata, H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTPROTECT, FAIL, "unable to load free space sections")
fspace->sinfo_accmode = H5AC__NO_FLAGS_SET;
}
}
}
else {
if(H5F_addr_defined(fspace->sect_addr)) {
HDassert(fspace->sinfo_protected == FALSE);
HDassert(H5F_addr_defined(fspace->addr));
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Reading in existing sections, fspace->sect_addr = %a\n", FUNC, fspace->sect_addr);
#endif
cache_udata.f = f;
cache_udata.fspace = fspace;
if(NULL == (fspace->sinfo = (H5FS_sinfo_t *)H5AC_protect(f, H5AC_FSPACE_SINFO, fspace->sect_addr, &cache_udata, accmode)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTPROTECT, FAIL, "unable to load free space sections")
fspace->sinfo_protected = TRUE;
fspace->sinfo_accmode = accmode;
}
else {
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Creating new section info\n", FUNC);
#endif
HDassert(fspace->tot_sect_count == 0);
HDassert(fspace->serial_sect_count == 0);
HDassert(fspace->ghost_sect_count == 0);
if(NULL == (fspace->sinfo = H5FS__sinfo_new(f, fspace)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCREATE, FAIL, "can't create section info")
fspace->sect_size = fspace->alloc_sect_size = 0;
}
}
HDassert(fspace->rc == 2);
fspace->sinfo_lock_count++;
done:
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Leaving, fspace->addr = %a, fspace->sinfo = %p, fspace->sect_addr = %a\n", FUNC, fspace->addr, fspace->sinfo, fspace->sect_addr);
HDfprintf(stderr, "%s: fspace->alloc_sect_size = %Hu, fspace->sect_size = %Hu\n", FUNC, fspace->alloc_sect_size, fspace->sect_size);
#endif
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sinfo_unlock(H5F_t *f, H5FS_t *fspace, hbool_t modified)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Called, modified = %t, fspace->addr = %a, fspace->sect_addr = %a\n", FUNC, modified, fspace->addr, fspace->sect_addr);
HDfprintf(stderr, "%s: fspace->sinfo_lock_count = %u, fspace->sinfo_modified = %t, fspace->sinfo_protected = %t\n", FUNC, fspace->sinfo_lock_count, fspace->sinfo_modified, fspace->sinfo_protected);
HDfprintf(stderr, "%s: fspace->alloc_sect_size = %Hu, fspace->sect_size = %Hu\n", FUNC, fspace->alloc_sect_size, fspace->sect_size);
#endif
HDassert(f);
HDassert(fspace);
HDassert(fspace->rc == 2);
HDassert(fspace->sinfo);
if(modified) {
if(fspace->sinfo_protected && (0 != ((fspace->sinfo_accmode) & H5AC__READ_ONLY_FLAG)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTDIRTY, FAIL, "attempt to modify read-only section info")
fspace->sinfo->dirty = TRUE;
fspace->sinfo_modified = TRUE;
if(H5FS__dirty(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMARKDIRTY, FAIL, "unable to mark free space header as dirty")
}
fspace->sinfo_lock_count--;
if(fspace->sinfo_lock_count == 0) {
hbool_t release_sinfo_space = FALSE;
if(fspace->sinfo_protected) {
unsigned cache_flags = H5AC__NO_FLAGS_SET;
HDassert(H5F_addr_defined(fspace->addr));
if(fspace->sinfo_modified) {
cache_flags |= H5AC__DIRTIED_FLAG;
if(fspace->sect_size != fspace->alloc_sect_size)
cache_flags |= H5AC__DELETED_FLAG | H5AC__TAKE_OWNERSHIP_FLAG;
}
HDassert(H5F_addr_defined(fspace->sect_addr));
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Unprotecting section info, cache_flags = %u\n", FUNC, cache_flags);
#endif
if(H5AC_unprotect(f, H5AC_FSPACE_SINFO, fspace->sect_addr, fspace->sinfo, cache_flags) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTUNPROTECT, FAIL, "unable to release free space section info")
fspace->sinfo_protected = FALSE;
if((cache_flags & H5AC__TAKE_OWNERSHIP_FLAG)) {
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Taking ownership of section info\n", FUNC);
#endif
release_sinfo_space = TRUE;
}
else {
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Relinquishing section info ownership\n", FUNC);
#endif
fspace->sinfo = NULL;
}
}
else {
if(fspace->sinfo_modified) {
if(H5F_addr_defined(fspace->sect_addr))
release_sinfo_space = TRUE;
else
HDassert(fspace->alloc_sect_size == 0);
}
else {
if(H5F_addr_defined(fspace->sect_addr))
HDassert(fspace->alloc_sect_size == fspace->sect_size);
else
HDassert(fspace->alloc_sect_size == 0);
}
}
fspace->sinfo_modified = FALSE;
if(release_sinfo_space) {
haddr_t old_sect_addr = fspace->sect_addr;
hsize_t old_alloc_sect_size = fspace->alloc_sect_size;
HDassert(H5F_addr_defined(fspace->addr));
fspace->sect_addr = HADDR_UNDEF;
fspace->alloc_sect_size = 0;
if(!modified)
if(H5FS__dirty(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMARKDIRTY, FAIL, "unable to mark free space header as dirty")
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Freeing section info on disk, old_sect_addr = %a, old_alloc_sect_size = %Hu\n", FUNC, old_sect_addr, old_alloc_sect_size);
#endif
if(!H5F_IS_TMP_ADDR(f, old_sect_addr))
if(H5MF_xfree(f, H5FD_MEM_FSPACE_SINFO, old_sect_addr, old_alloc_sect_size) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "unable to free free space sections")
}
}
done:
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Leaving, ret_value = %d\n", FUNC, ret_value);
#endif
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_serialize_size(H5FS_t *fspace)
{
FUNC_ENTER_STATIC_NOERR
HDassert(fspace);
if(fspace->serial_sect_count > 0) {
size_t sect_buf_size;
sect_buf_size = fspace->sinfo->sect_prefix_size;
sect_buf_size += fspace->sinfo->serial_size_count * H5VM_limit_enc_size((uint64_t)fspace->serial_sect_count);
sect_buf_size += fspace->sinfo->serial_size_count * fspace->sinfo->sect_len_size;
sect_buf_size += fspace->serial_sect_count * fspace->sinfo->sect_off_size;
sect_buf_size += fspace->serial_sect_count * 1 ;
sect_buf_size += fspace->sinfo->serial_size;
fspace->sect_size = sect_buf_size;
}
else
fspace->sect_size = fspace->sinfo->sect_prefix_size;
FUNC_LEAVE_NOAPI(SUCCEED)
}
static herr_t
H5FS__sect_increase(H5FS_t *fspace, const H5FS_section_class_t *cls,
unsigned flags)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(cls);
fspace->tot_sect_count++;
if(cls->flags & H5FS_CLS_GHOST_OBJ) {
HDassert(cls->serial_size == 0);
fspace->ghost_sect_count++;
}
else {
fspace->serial_sect_count++;
fspace->sinfo->serial_size += cls->serial_size;
if(!(flags & H5FS_ADD_DESERIALIZING)) {
if(H5FS__sect_serialize_size(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCOMPUTE, FAIL, "can't adjust free space section size on disk")
}
}
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_decrease(H5FS_t *fspace, const H5FS_section_class_t *cls)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(cls);
fspace->tot_sect_count--;
if(cls->flags & H5FS_CLS_GHOST_OBJ) {
HDassert(cls->serial_size == 0);
fspace->ghost_sect_count--;
}
else {
fspace->serial_sect_count--;
fspace->sinfo->serial_size -= cls->serial_size;
if(H5FS__sect_serialize_size(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCOMPUTE, FAIL, "can't adjust free space section size on disk")
}
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__size_node_decr(H5FS_sinfo_t *sinfo, unsigned bin, H5FS_node_t *fspace_node,
const H5FS_section_class_t *cls)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(sinfo);
HDassert(fspace_node);
HDassert(cls);
sinfo->bins[bin].tot_sect_count--;
if(cls->flags & H5FS_CLS_GHOST_OBJ) {
fspace_node->ghost_count--;
sinfo->bins[bin].ghost_sect_count--;
if(fspace_node->ghost_count == 0)
sinfo->ghost_size_count--;
}
else {
fspace_node->serial_count--;
sinfo->bins[bin].serial_sect_count--;
if(fspace_node->serial_count == 0)
sinfo->serial_size_count--;
}
if(H5SL_count(fspace_node->sect_list) == 0) {
H5FS_node_t *tmp_fspace_node;
HDassert(fspace_node->ghost_count == 0);
HDassert(fspace_node->serial_count == 0);
tmp_fspace_node = (H5FS_node_t *)H5SL_remove(sinfo->bins[bin].bin_list, &fspace_node->sect_size);
if(tmp_fspace_node == NULL || tmp_fspace_node != fspace_node)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space node from skip list")
if(H5SL_close(fspace_node->sect_list) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCLOSEOBJ, FAIL, "can't destroy size tracking node's skip list")
fspace_node = H5FL_FREE(H5FS_node_t, fspace_node);
sinfo->tot_size_count--;
}
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_unlink_size(H5FS_sinfo_t *sinfo, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect)
{
H5FS_node_t *fspace_node;
H5FS_section_info_t *tmp_sect_node;
unsigned bin;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(sinfo);
HDassert(sinfo->bins);
HDassert(sect);
HDassert(cls);
bin = H5VM_log2_gen(sect->size);
HDassert(bin < sinfo->nbins);
if(sinfo->bins[bin].bin_list == NULL)
HGOTO_ERROR(H5E_FSPACE, H5E_NOTFOUND, FAIL, "node's bin is empty?")
if((fspace_node = (H5FS_node_t *)H5SL_search(sinfo->bins[bin].bin_list, §->size)) == NULL)
HGOTO_ERROR(H5E_FSPACE, H5E_NOTFOUND, FAIL, "can't find section size node")
tmp_sect_node = (H5FS_section_info_t *)H5SL_remove(fspace_node->sect_list, §->addr);
if(tmp_sect_node == NULL || tmp_sect_node != sect)
HGOTO_ERROR(H5E_FSPACE, H5E_NOTFOUND, FAIL, "can't find section node on size list")
if(H5FS__size_node_decr(sinfo, bin, fspace_node, cls) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space size node from skip list")
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_unlink_rest(H5FS_t *fspace, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(cls);
HDassert(sect);
if(!(cls->flags & H5FS_CLS_SEPAR_OBJ)) {
H5FS_section_info_t *tmp_sect_node;
tmp_sect_node = (H5FS_section_info_t *)H5SL_remove(fspace->sinfo->merge_list, §->addr);
if(tmp_sect_node == NULL || tmp_sect_node != sect)
HGOTO_ERROR(H5E_FSPACE, H5E_NOTFOUND, FAIL, "can't find section node on size list")
}
if(H5FS__sect_decrease(fspace, cls) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't increase free space section size on disk")
fspace->tot_space -= sect->size;
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_remove_real(H5FS_t *fspace, H5FS_section_info_t *sect)
{
const H5FS_section_class_t *cls;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(sect);
cls = &fspace->sect_cls[sect->type];
if(H5FS__sect_unlink_size(fspace->sinfo, cls, sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't remove section from size tracking data structures")
if(H5FS__sect_unlink_rest(fspace, cls, sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't remove section from non-size tracking data structures")
done:
FUNC_LEAVE_NOAPI(ret_value)
}
herr_t
H5FS_sect_remove(H5F_t *f, H5FS_t *fspace, H5FS_section_info_t *sect)
{
hbool_t sinfo_valid = FALSE;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(f);
HDassert(fspace);
HDassert(sect);
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
if(H5FS__sect_remove_real(fspace, sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove section")
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, TRUE) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_link_size(H5FS_sinfo_t *sinfo, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect)
{
H5FS_node_t *fspace_node = NULL;
hbool_t fspace_node_alloc = FALSE;
unsigned bin;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(sinfo);
HDassert(sect);
HDassert(H5F_addr_defined(sect->addr));
HDassert(sect->size);
bin = H5VM_log2_gen(sect->size);
HDassert(bin < sinfo->nbins);
if(sinfo->bins[bin].bin_list == NULL) {
if(NULL == (sinfo->bins[bin].bin_list = H5SL_create(H5SL_TYPE_HSIZE, NULL)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCREATE, FAIL, "can't create skip list for free space nodes")
}
else
fspace_node = (H5FS_node_t *)H5SL_search(sinfo->bins[bin].bin_list, §->size);
if(fspace_node == NULL) {
if(NULL == (fspace_node = H5FL_MALLOC(H5FS_node_t)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for free space node")
fspace_node_alloc = TRUE;
fspace_node->sect_size = sect->size;
fspace_node->serial_count = fspace_node->ghost_count = 0;
if(NULL == (fspace_node->sect_list = H5SL_create(H5SL_TYPE_HADDR, NULL)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCREATE, FAIL, "can't create skip list for free space nodes")
if(H5SL_insert(sinfo->bins[bin].bin_list, fspace_node, &fspace_node->sect_size) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space node into skip list")
fspace_node_alloc = FALSE;
sinfo->tot_size_count++;
}
sinfo->bins[bin].tot_sect_count++;
if(cls->flags & H5FS_CLS_GHOST_OBJ) {
sinfo->bins[bin].ghost_sect_count++;
fspace_node->ghost_count++;
if(fspace_node->ghost_count == 1)
sinfo->ghost_size_count++;
}
else {
sinfo->bins[bin].serial_sect_count++;
fspace_node->serial_count++;
if(fspace_node->serial_count == 1)
sinfo->serial_size_count++;
}
if(H5SL_insert(fspace_node->sect_list, sect, §->addr) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space node into skip list")
done:
if(ret_value < 0)
if(fspace_node && fspace_node_alloc) {
if(fspace_node->sect_list && H5SL_close(fspace_node->sect_list) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTCLOSEOBJ, FAIL, "can't destroy size free space node's skip list")
fspace_node = H5FL_FREE(H5FS_node_t, fspace_node);
}
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_link_rest(H5FS_t *fspace, const H5FS_section_class_t *cls,
H5FS_section_info_t *sect, unsigned flags)
{
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(sect);
if(!(cls->flags & H5FS_CLS_SEPAR_OBJ)) {
if(fspace->sinfo->merge_list == NULL)
if(NULL == (fspace->sinfo->merge_list = H5SL_create(H5SL_TYPE_HADDR, NULL)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCREATE, FAIL, "can't create skip list for merging free space sections")
if(H5SL_insert(fspace->sinfo->merge_list, sect, §->addr) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space node into merging skip list")
}
if(H5FS__sect_increase(fspace, cls, flags) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't increase free space section size on disk")
fspace->tot_space += sect->size;
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_link(H5FS_t *fspace, H5FS_section_info_t *sect, unsigned flags)
{
const H5FS_section_class_t *cls;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(sect);
cls = &fspace->sect_cls[sect->type];
if(H5FS__sect_link_size(fspace->sinfo, cls, sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't add section to size tracking data structures")
if(H5FS__sect_link_rest(fspace, cls, sect, flags) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't add section to non-size tracking data structures")
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS__sect_merge(H5FS_t *fspace, H5FS_section_info_t **sect, void *op_data)
{
H5FS_section_class_t *sect_cls;
hbool_t modified;
hbool_t remove_sect = FALSE;
htri_t status;
herr_t ret_value = SUCCEED;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(*sect);
HDassert(H5F_addr_defined((*sect)->addr));
HDassert((*sect)->size);
if(fspace->sinfo->merge_list) {
do {
H5SL_node_t *less_sect_node;
H5SL_node_t *greater_sect_node;
H5FS_section_info_t *tmp_sect;
H5FS_section_class_t *tmp_sect_cls;
hbool_t greater_sect_node_valid = FALSE;
modified = FALSE;
less_sect_node = H5SL_below(fspace->sinfo->merge_list, &(*sect)->addr);
if(less_sect_node) {
greater_sect_node = H5SL_next(less_sect_node);
greater_sect_node_valid = TRUE;
tmp_sect = (H5FS_section_info_t *)H5SL_item(less_sect_node);
tmp_sect_cls = &fspace->sect_cls[tmp_sect->type];
sect_cls = &fspace->sect_cls[(*sect)->type];
if((!(tmp_sect_cls->flags & H5FS_CLS_MERGE_SYM) || (tmp_sect->type == (*sect)->type))
&& tmp_sect_cls->can_merge) {
if((status = (*tmp_sect_cls->can_merge)(tmp_sect, *sect, op_data)) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMERGE, FAIL, "can't check for merging sections")
if(status > 0) {
HDassert(tmp_sect_cls->merge);
if(H5FS__sect_remove_real(fspace, tmp_sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't remove section from internal data structures")
if((*tmp_sect_cls->merge)(&tmp_sect, *sect, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't merge two sections")
*sect = tmp_sect;
if(*sect == NULL)
HGOTO_DONE(ret_value);
modified = TRUE;
}
}
}
if(!greater_sect_node_valid)
greater_sect_node = H5SL_above(fspace->sinfo->merge_list, &(*sect)->addr);
if(greater_sect_node) {
tmp_sect = (H5FS_section_info_t *)H5SL_item(greater_sect_node);
sect_cls = &fspace->sect_cls[(*sect)->type];
tmp_sect_cls = &fspace->sect_cls[tmp_sect->type];
if((!(sect_cls->flags & H5FS_CLS_MERGE_SYM) || ((*sect)->type == tmp_sect->type))
&& sect_cls->can_merge) {
if((status = (*sect_cls->can_merge)(*sect, tmp_sect, op_data)) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMERGE, FAIL, "can't check for merging sections")
if(status > 0) {
HDassert(sect_cls->merge);
if(H5FS__sect_remove_real(fspace, tmp_sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't remove section from internal data structures")
if((*sect_cls->merge)(sect, tmp_sect, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't merge two sections")
if(*sect == NULL)
HGOTO_DONE(ret_value);
modified = TRUE;
}
}
}
} while(modified);
}
HDassert(*sect);
do {
modified = FALSE;
sect_cls = &fspace->sect_cls[(*sect)->type];
if(sect_cls->can_shrink) {
if((status = (*sect_cls->can_shrink)(*sect, op_data)) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTSHRINK, FAIL, "can't check for shrinking container")
if(status > 0) {
if(remove_sect) {
if(H5FS__sect_remove_real(fspace, *sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't remove section from internal data structures")
remove_sect = FALSE;
}
HDassert(sect_cls->shrink);
if((*sect_cls->shrink)(sect, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't shrink free space container")
if(*sect == NULL) {
if(fspace->sinfo->merge_list) {
H5SL_node_t *last_node;
if(NULL != (last_node = H5SL_last(fspace->sinfo->merge_list))) {
*sect = (H5FS_section_info_t *)H5SL_item(last_node);
HDassert(*sect);
remove_sect = TRUE;
}
}
}
modified = TRUE;
}
}
} while(modified && *sect);
if(remove_sect && (*sect != NULL))
*sect = NULL;
done:
FUNC_LEAVE_NOAPI(ret_value)
}
herr_t
H5FS_sect_add(H5F_t *f, H5FS_t *fspace, H5FS_section_info_t *sect, unsigned flags,
void *op_data)
{
H5FS_section_class_t *cls;
hbool_t sinfo_valid = FALSE;
hbool_t sinfo_modified = FALSE;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI(FAIL)
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: *sect = {%a, %Hu, %u, %s}\n", FUNC, sect->addr, sect->size, sect->type, (sect->state == H5FS_SECT_LIVE ? "H5FS_SECT_LIVE" : "H5FS_SECT_SERIALIZED"));
#endif
HDassert(fspace);
HDassert(sect);
HDassert(H5F_addr_defined(sect->addr));
HDassert(sect->size);
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
cls = &fspace->sect_cls[sect->type];
if(cls->add)
if((*cls->add)(§, &flags, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "'add' section class callback failed")
if(flags & H5FS_ADD_RETURNED_SPACE) {
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Returning space\n", FUNC);
#endif
if(H5FS__sect_merge(fspace, §, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMERGE, FAIL, "can't merge sections")
}
if(sect)
if(H5FS__sect_link(fspace, sect, flags) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space section into skip list")
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: fspace->tot_space = %Hu\n", FUNC, fspace->tot_space);
#endif
if(!(flags & (H5FS_ADD_DESERIALIZING | H5FS_PAGE_END_NO_ADD)))
sinfo_modified = TRUE;
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, sinfo_modified) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
#ifdef H5FS_DEBUG_ASSERT
if(!(flags & (H5FS_ADD_DESERIALIZING | H5FS_ADD_SKIP_VALID)))
H5FS__assert(fspace);
#endif
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: Leaving, ret_value = %d\n", FUNC, ret_value);
#endif
FUNC_LEAVE_NOAPI(ret_value)
}
htri_t
H5FS_sect_try_extend(H5F_t *f, H5FS_t *fspace, haddr_t addr,
hsize_t size, hsize_t extra_requested, unsigned flags, void *op_data)
{
hbool_t sinfo_valid = FALSE;
hbool_t sinfo_modified = FALSE;
htri_t ret_value = FALSE;
FUNC_ENTER_NOAPI(FAIL)
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: addr = %a, size = %Hu, extra_requested = %hu\n", FUNC, addr, size, extra_requested);
#endif
HDassert(f);
HDassert(fspace);
HDassert(H5F_addr_defined(addr));
HDassert(size > 0);
HDassert(extra_requested > 0);
#ifdef H5FS_SINFO_DEBUG
HDfprintf(stderr, "%s: fspace->tot_sect_count = %Hu\n", FUNC, fspace->tot_sect_count);
HDfprintf(stderr, "%s: fspace->serial_sect_count = %Hu\n", FUNC, fspace->serial_sect_count);
HDfprintf(stderr, "%s: fspace->ghost_sect_count = %Hu\n", FUNC, fspace->ghost_sect_count);
#endif
if(fspace->tot_sect_count > 0) {
H5FS_section_info_t *sect;
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
if((sect = (H5FS_section_info_t *)H5SL_greater(fspace->sinfo->merge_list, &addr))) {
if(sect->size >= extra_requested && (addr + size) == sect->addr) {
H5FS_section_class_t *cls;
if(H5FS__sect_remove_real(fspace, sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't remove section from internal data structures")
cls = &fspace->sect_cls[sect->type];
if(sect->size > extra_requested) {
HDassert(cls->flags & H5FS_CLS_ADJUST_OK);
sect->addr += extra_requested;
sect->size -= extra_requested;
if(cls->add)
if((*cls->add)(§, &flags, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "'add' section class callback failed")
if(sect) {
if(H5FS__sect_link(fspace, sect, 0) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space section into skip list")
}
}
else {
HDassert(sect->size == extra_requested);
if((*cls->free)(sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't free section")
}
sinfo_modified = TRUE;
HGOTO_DONE(TRUE);
}
}
}
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, sinfo_modified) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
htri_t
H5FS_sect_try_merge(H5F_t *f, H5FS_t *fspace, H5FS_section_info_t *sect,
unsigned flags, void *op_data)
{
hbool_t sinfo_valid = FALSE;
hbool_t sinfo_modified = FALSE;
hsize_t saved_fs_size;
htri_t ret_value = FALSE;
FUNC_ENTER_NOAPI(FAIL)
HDassert(f);
HDassert(fspace);
HDassert(sect);
HDassert(H5F_addr_defined(sect->addr));
HDassert(sect->size);
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
saved_fs_size = sect->size;
if(H5FS__sect_merge(fspace, §, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMERGE, FAIL, "can't merge sections")
if(!sect) {
sinfo_modified = TRUE;
HGOTO_DONE(TRUE)
}
else {
if(sect->size > saved_fs_size) {
if(H5FS__sect_link(fspace, sect, flags) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space section into skip list")
sinfo_modified = TRUE;
HGOTO_DONE(TRUE)
}
}
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, sinfo_modified) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
static htri_t
H5FS__sect_find_node(H5FS_t *fspace, hsize_t request, H5FS_section_info_t **node)
{
H5FS_node_t *fspace_node;
unsigned bin;
htri_t ret_value = FALSE;
H5SL_node_t *curr_size_node=NULL;
const H5FS_section_class_t *cls;
hsize_t alignment;
FUNC_ENTER_STATIC
HDassert(fspace);
HDassert(fspace->sinfo);
HDassert(fspace->sinfo->bins);
HDassert(request > 0);
HDassert(node);
bin = H5VM_log2_gen(request);
HDassert(bin < fspace->sinfo->nbins);
alignment = fspace->alignment;
if(!((alignment > 1) && (request >= fspace->align_thres)))
alignment = 0;
do {
if(fspace->sinfo->bins[bin].bin_list) {
if (!alignment) {
if((fspace_node = (H5FS_node_t *)H5SL_greater(fspace->sinfo->bins[bin].bin_list, &request))) {
if(NULL == (*node = (H5FS_section_info_t *)H5SL_remove_first(fspace_node->sect_list)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space node from skip list")
cls = &fspace->sect_cls[(*node)->type];
if(H5FS__size_node_decr(fspace->sinfo, bin, fspace_node, cls) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space size node from skip list")
if(H5FS__sect_unlink_rest(fspace, cls, *node) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't remove section from non-size tracking data structures")
HGOTO_DONE(TRUE)
}
}
else {
curr_size_node = H5SL_first(fspace->sinfo->bins[bin].bin_list);
while (curr_size_node != NULL) {
H5FS_node_t *curr_fspace_node=NULL;
H5SL_node_t *curr_sect_node=NULL;
curr_fspace_node = (H5FS_node_t *)H5SL_item(curr_size_node);
curr_sect_node = (H5SL_node_t *)H5SL_first(curr_fspace_node->sect_list);
while(curr_sect_node != NULL) {
H5FS_section_info_t *curr_sect=NULL;
hsize_t mis_align=0, frag_size=0;
H5FS_section_info_t *split_sect=NULL;
curr_sect = (H5FS_section_info_t *)H5SL_item(curr_sect_node);
HDassert(H5F_addr_defined(curr_sect->addr));
HDassert(curr_fspace_node->sect_size == curr_sect->size);
cls = &fspace->sect_cls[curr_sect->type];
HDassert(alignment);
HDassert(cls);
if((mis_align = curr_sect->addr % alignment))
frag_size = alignment - mis_align;
if((curr_sect->size >= (request + frag_size)) && (cls->split)) {
if(NULL == (*node = (H5FS_section_info_t *)H5SL_remove(curr_fspace_node->sect_list, &curr_sect->addr)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space node from skip list")
if(H5FS__size_node_decr(fspace->sinfo, bin, curr_fspace_node, cls) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTREMOVE, FAIL, "can't remove free space size node from skip list")
if(H5FS__sect_unlink_rest(fspace, cls, *node) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't remove section from non-size tracking data structures")
if(mis_align) {
split_sect = cls->split(*node, frag_size);
if((H5FS__sect_link(fspace, split_sect, 0) < 0))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space section into skip list")
HDassert(split_sect->addr < (*node)->addr);
HDassert(request <= (*node)->size);
}
HGOTO_DONE(TRUE)
}
curr_sect_node = H5SL_next(curr_sect_node);
}
curr_size_node = H5SL_next(curr_size_node);
}
}
}
bin++;
} while(bin < fspace->sinfo->nbins);
done:
FUNC_LEAVE_NOAPI(ret_value)
}
htri_t
H5FS_sect_find(H5F_t *f, H5FS_t *fspace, hsize_t request, H5FS_section_info_t **node)
{
hbool_t sinfo_valid = FALSE;
hbool_t sinfo_modified = FALSE;
htri_t ret_value = FALSE;
FUNC_ENTER_NOAPI(FAIL)
HDassert(fspace);
HDassert(fspace->nclasses);
HDassert(request);
HDassert(node);
if(fspace->tot_sect_count > 0) {
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
if((ret_value = H5FS__sect_find_node(fspace, request, node)) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "can't remove section from bins")
if(ret_value > 0) {
sinfo_modified = TRUE;
}
}
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, sinfo_modified) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
#ifdef H5FS_DEBUG_ASSERT
H5FS__assert(fspace);
#endif
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS_iterate_sect_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata)
{
H5FS_section_info_t *sect_info = (H5FS_section_info_t *)_item;
H5FS_iter_ud_t *udata = (H5FS_iter_ud_t *)_udata;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(sect_info);
HDassert(udata->fspace);
HDassert(udata->op);
if((*udata->op)(sect_info, udata->op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "iteration callback failed")
done:
FUNC_LEAVE_NOAPI(ret_value)
}
static herr_t
H5FS_iterate_node_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata)
{
H5FS_node_t *fspace_node = (H5FS_node_t *)_item;
H5FS_iter_ud_t *udata = (H5FS_iter_ud_t *)_udata;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(fspace_node);
HDassert(udata->fspace);
HDassert(udata->op);
HDassert(fspace_node->sect_list);
if(H5SL_iterate(fspace_node->sect_list, H5FS_iterate_sect_cb, udata) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over section nodes")
done:
FUNC_LEAVE_NOAPI(ret_value)
}
herr_t
H5FS_sect_iterate(H5F_t *f, H5FS_t *fspace, H5FS_operator_t op, void *op_data)
{
H5FS_iter_ud_t udata;
hbool_t sinfo_valid = FALSE;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(fspace);
HDassert(op);
udata.fspace = fspace;
udata.op = op;
udata.op_data = op_data;
if(fspace->tot_sect_count) {
unsigned bin;
if(H5FS__sinfo_lock(f, fspace, H5AC__READ_ONLY_FLAG) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
for(bin = 0; bin < fspace->sinfo->nbins; bin++) {
if(fspace->sinfo->bins[bin].bin_list) {
if(H5SL_iterate(fspace->sinfo->bins[bin].bin_list, H5FS_iterate_node_cb, &udata) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over section size nodes")
}
}
}
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, FALSE) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
herr_t
H5FS_sect_stats(const H5FS_t *fspace, hsize_t *tot_space, hsize_t *nsects)
{
FUNC_ENTER_NOAPI_NOINIT_NOERR
HDassert(fspace);
if(tot_space)
*tot_space = fspace->tot_space;
if(nsects)
*nsects = fspace->tot_sect_count;
FUNC_LEAVE_NOAPI(SUCCEED)
}
herr_t
H5FS_sect_change_class(H5F_t *f, H5FS_t *fspace, H5FS_section_info_t *sect,
uint16_t new_class)
{
const H5FS_section_class_t *old_cls;
const H5FS_section_class_t *new_cls;
unsigned old_class;
hbool_t sinfo_valid = FALSE;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(fspace);
HDassert(sect);
HDassert(sect->type < fspace->nclasses);
HDassert(new_class < fspace->nclasses);
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
old_class = sect->type;
old_cls = &fspace->sect_cls[sect->type];
new_cls = &fspace->sect_cls[new_class];
if((old_cls->flags & H5FS_CLS_GHOST_OBJ) != (new_cls->flags & H5FS_CLS_GHOST_OBJ)) {
H5FS_node_t *fspace_node;
unsigned bin;
hbool_t to_ghost;
if(old_cls->flags & H5FS_CLS_GHOST_OBJ)
to_ghost = FALSE;
else
to_ghost = TRUE;
HDassert(fspace->sinfo->bins);
bin = H5VM_log2_gen(sect->size);
HDassert(bin < fspace->sinfo->nbins);
HDassert(fspace->sinfo->bins[bin].bin_list);
fspace_node = (H5FS_node_t *)H5SL_search(fspace->sinfo->bins[bin].bin_list, §->size);
HDassert(fspace_node);
if(to_ghost) {
fspace->serial_sect_count--;
fspace->ghost_sect_count++;
fspace->sinfo->bins[bin].serial_sect_count--;
fspace->sinfo->bins[bin].ghost_sect_count++;
fspace_node->serial_count--;
fspace_node->ghost_count++;
if(fspace_node->serial_count == 0)
fspace->sinfo->serial_size_count--;
if(fspace_node->ghost_count == 1)
fspace->sinfo->ghost_size_count++;
}
else {
fspace->serial_sect_count++;
fspace->ghost_sect_count--;
fspace->sinfo->bins[bin].serial_sect_count++;
fspace->sinfo->bins[bin].ghost_sect_count--;
fspace_node->serial_count++;
fspace_node->ghost_count--;
if(fspace_node->serial_count == 1)
fspace->sinfo->serial_size_count++;
if(fspace_node->ghost_count == 0)
fspace->sinfo->ghost_size_count--;
}
}
if((old_cls->flags & H5FS_CLS_SEPAR_OBJ) != (new_cls->flags & H5FS_CLS_SEPAR_OBJ)) {
hbool_t to_mergable;
if(old_cls->flags & H5FS_CLS_SEPAR_OBJ)
to_mergable = TRUE;
else
to_mergable = FALSE;
if(to_mergable) {
if(fspace->sinfo->merge_list == NULL)
if(NULL == (fspace->sinfo->merge_list = H5SL_create(H5SL_TYPE_HADDR, NULL)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCREATE, FAIL, "can't create skip list for merging free space sections")
if(H5SL_insert(fspace->sinfo->merge_list, sect, §->addr) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't insert free space node into merging skip list")
}
else {
H5FS_section_info_t *tmp_sect_node;
tmp_sect_node = (H5FS_section_info_t *)H5SL_remove(fspace->sinfo->merge_list, §->addr);
if(tmp_sect_node == NULL || tmp_sect_node != sect)
HGOTO_ERROR(H5E_FSPACE, H5E_NOTFOUND, FAIL, "can't find section node on size list")
}
}
sect->type = new_class;
fspace->sinfo->serial_size -= fspace->sect_cls[old_class].serial_size;
fspace->sinfo->serial_size += fspace->sect_cls[new_class].serial_size;
if(H5FS__sect_serialize_size(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTCOMPUTE, FAIL, "can't adjust free space section size on disk")
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, TRUE) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
#ifdef H5FS_DEBUG_ASSERT
void
H5FS__sect_assert(const H5FS_t *fspace)
{
hsize_t separate_obj;
FUNC_ENTER_PACKAGE_NOERR
separate_obj = 0;
if(fspace->sinfo->bins) {
hsize_t acc_tot_sect_count;
hsize_t acc_serial_sect_count;
hsize_t acc_ghost_sect_count;
size_t acc_tot_size_count;
size_t acc_serial_size_count;
size_t acc_ghost_size_count;
unsigned u;
acc_tot_sect_count = 0;
acc_serial_sect_count = 0;
acc_ghost_sect_count = 0;
acc_tot_size_count = 0;
acc_serial_size_count = 0;
acc_ghost_size_count = 0;
for(u = 0; u < fspace->sinfo->nbins; u++) {
acc_tot_sect_count += fspace->sinfo->bins[u].tot_sect_count;
acc_serial_sect_count += fspace->sinfo->bins[u].serial_sect_count;
acc_ghost_sect_count += fspace->sinfo->bins[u].ghost_sect_count;
if(fspace->sinfo->bins[u].bin_list) {
H5SL_node_t *curr_size_node;
size_t bin_serial_count;
size_t bin_ghost_count;
acc_tot_size_count += H5SL_count(fspace->sinfo->bins[u].bin_list);
curr_size_node = H5SL_first(fspace->sinfo->bins[u].bin_list);
bin_serial_count = 0;
bin_ghost_count = 0;
while(curr_size_node != NULL) {
H5FS_node_t *fspace_node;
H5SL_node_t *curr_sect_node;
size_t size_serial_count;
size_t size_ghost_count;
fspace_node = (H5FS_node_t *)H5SL_item(curr_size_node);
curr_sect_node = H5SL_first(fspace_node->sect_list);
size_serial_count = 0;
size_ghost_count = 0;
while(curr_sect_node != NULL) {
H5FS_section_class_t *cls;
H5FS_section_info_t *sect;
sect = (H5FS_section_info_t *)H5SL_item(curr_sect_node);
cls = &fspace->sect_cls[sect->type];
HDassert(H5F_addr_defined(sect->addr));
HDassert(fspace_node->sect_size == sect->size);
if(cls->valid)
(*cls->valid)(cls, sect);
if(cls->flags & H5FS_CLS_GHOST_OBJ)
size_ghost_count++;
else
size_serial_count++;
if(cls->flags & H5FS_CLS_SEPAR_OBJ)
separate_obj++;
curr_sect_node = H5SL_next(curr_sect_node);
}
HDassert(fspace_node->serial_count == size_serial_count);
HDassert(fspace_node->ghost_count == size_ghost_count);
if(fspace_node->serial_count > 0)
acc_serial_size_count++;
if(fspace_node->ghost_count > 0)
acc_ghost_size_count++;
bin_serial_count += size_serial_count;
bin_ghost_count += size_ghost_count;
curr_size_node = H5SL_next(curr_size_node);
}
HDassert(fspace->sinfo->bins[u].tot_sect_count == (bin_serial_count + bin_ghost_count));
HDassert(fspace->sinfo->bins[u].serial_sect_count == bin_serial_count);
HDassert(fspace->sinfo->bins[u].ghost_sect_count == bin_ghost_count);
}
}
HDassert(fspace->sinfo->tot_size_count == acc_tot_size_count);
HDassert(fspace->sinfo->serial_size_count == acc_serial_size_count);
HDassert(fspace->sinfo->ghost_size_count == acc_ghost_size_count);
HDassert(fspace->tot_sect_count == acc_tot_sect_count);
HDassert(fspace->serial_sect_count == acc_serial_sect_count);
HDassert(fspace->ghost_sect_count == acc_ghost_sect_count);
}
else {
HDassert(fspace->tot_sect_count == 0);
HDassert(fspace->serial_sect_count == 0);
HDassert(fspace->ghost_sect_count == 0);
}
if(fspace->sinfo->merge_list)
HDassert(fspace->tot_sect_count == (separate_obj + H5SL_count(fspace->sinfo->merge_list)));
FUNC_LEAVE_NOAPI_VOID
}
#endif
htri_t
H5FS_sect_try_shrink_eoa(H5F_t *f, H5FS_t *fspace, void *op_data)
{
hbool_t sinfo_valid = FALSE;
hbool_t section_removed = FALSE;
htri_t ret_value = FALSE;
FUNC_ENTER_NOAPI(FAIL)
HDassert(fspace);
if(H5FS__sinfo_lock(f, fspace, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info")
sinfo_valid = TRUE;
if(fspace->sinfo && fspace->sinfo->merge_list) {
H5SL_node_t *last_node;
if(NULL != (last_node = H5SL_last(fspace->sinfo->merge_list))) {
H5FS_section_info_t *tmp_sect;
H5FS_section_class_t *tmp_sect_cls;
tmp_sect = (H5FS_section_info_t *)H5SL_item(last_node);
HDassert(tmp_sect);
tmp_sect_cls = &fspace->sect_cls[tmp_sect->type];
if(tmp_sect_cls->can_shrink) {
if((ret_value = (*tmp_sect_cls->can_shrink)(tmp_sect, op_data)) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTSHRINK, FAIL, "can't check for shrinking container")
if(ret_value > 0) {
HDassert(tmp_sect_cls->shrink);
if(H5FS__sect_remove_real(fspace, tmp_sect) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't remove section from internal data structures")
section_removed = TRUE;
if((*tmp_sect_cls->shrink)(&tmp_sect, op_data) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, FAIL, "can't shrink free space container")
}
}
}
}
done:
if(sinfo_valid && H5FS__sinfo_unlock(f, fspace, section_removed) < 0)
HDONE_ERROR(H5E_FSPACE, H5E_CANTRELEASE, FAIL, "can't release section info")
FUNC_LEAVE_NOAPI(ret_value)
}
herr_t
H5FS_vfd_alloc_hdr_and_section_info_if_needed(H5F_t *f, H5FS_t *fspace,
haddr_t *fs_addr_ptr)
{
hsize_t hdr_alloc_size;
hsize_t sinfo_alloc_size;
haddr_t sect_addr = HADDR_UNDEF;
haddr_t eoa_frag_addr = HADDR_UNDEF;
hsize_t eoa_frag_size = 0;
haddr_t eoa = HADDR_UNDEF;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI_NOINIT
HDassert(f);
HDassert(f->shared);
HDassert(f->shared->lf);
HDassert(fspace);
HDassert(fs_addr_ptr);
HDassert(fspace->sinfo_lock_count == 0);
HDassert(*fs_addr_ptr == HADDR_UNDEF);
HDassert(fspace->addr == HADDR_UNDEF);
HDassert(fspace->sect_addr == HADDR_UNDEF);
HDassert(fspace->alloc_sect_size == 0);
HDassert(f->shared->fs_persist);
HDassert((f->shared->fs_strategy == H5F_FSPACE_STRATEGY_FSM_AGGR) ||
(f->shared->fs_strategy == H5F_FSPACE_STRATEGY_PAGE));
if(fspace->serial_sect_count > 0) {
HDassert(fspace->sinfo);
if(HADDR_UNDEF == (eoa = H5F_get_eoa(f, H5FD_MEM_FSPACE_HDR)))
HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGET, FAIL, "Unable to get eoa")
if(H5F_IS_TMP_ADDR(f, (eoa + fspace->sect_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_BADRANGE, FAIL, "hdr file space alloc will overlap into 'temporary' file space")
hdr_alloc_size = H5FS_HEADER_SIZE(f);
if(H5F_PAGED_AGGR(f)) {
HDassert(0 == (eoa % f->shared->fs_page_size));
hdr_alloc_size = ((hdr_alloc_size / f->shared->fs_page_size) + 1) * f->shared->fs_page_size;
HDassert(hdr_alloc_size >= H5FS_HEADER_SIZE(f));
HDassert((hdr_alloc_size % f->shared->fs_page_size) == 0);
}
if(HADDR_UNDEF == (fspace->addr = H5FD_alloc(f->shared->lf, H5FD_MEM_FSPACE_HDR,
f, hdr_alloc_size, &eoa_frag_addr, &eoa_frag_size)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTALLOC, FAIL, "can't allocate file space for hdr")
HDassert((eoa_frag_size == 0) || (f->shared->alignment != 1));
if(H5AC_insert_entry(f, H5AC_FSPACE_HDR, fspace->addr, fspace, H5AC__PIN_ENTRY_FLAG) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINIT, FAIL, "can't add free space header to cache")
*fs_addr_ptr = fspace->addr;
if(HADDR_UNDEF == (eoa = H5F_get_eoa(f, H5FD_MEM_FSPACE_SINFO)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "Unable to get eoa")
if(H5F_IS_TMP_ADDR(f, (eoa + fspace->sect_size)))
HGOTO_ERROR(H5E_FSPACE, H5E_BADRANGE, FAIL, "sinfo file space alloc will overlap into 'temporary' file space")
sinfo_alloc_size = fspace->sect_size;
if(H5F_PAGED_AGGR(f)) {
HDassert(0 == (eoa % f->shared->fs_page_size));
sinfo_alloc_size = ((sinfo_alloc_size / f->shared->fs_page_size) + 1) * f->shared->fs_page_size;
HDassert(sinfo_alloc_size >= fspace->sect_size);
HDassert((sinfo_alloc_size % f->shared->fs_page_size) == 0);
}
if(HADDR_UNDEF == (sect_addr = H5FD_alloc(f->shared->lf, H5FD_MEM_FSPACE_SINFO,
f, sinfo_alloc_size, &eoa_frag_addr, &eoa_frag_size)))
HGOTO_ERROR(H5E_FSPACE, H5E_CANTALLOC, FAIL, "can't allocate file space")
HDassert((eoa_frag_size == 0) || (f->shared->alignment != 1));
fspace->alloc_sect_size = fspace->sect_size;
fspace->sect_addr = sect_addr;
if(H5AC_insert_entry(f, H5AC_FSPACE_SINFO, sect_addr, fspace->sinfo, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTINIT, FAIL, "can't add free space sinfo to cache")
if(H5AC_mark_entry_dirty(fspace) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTMARKDIRTY, FAIL, "unable to mark free space header as dirty")
fspace->sinfo = NULL;
}
done:
FUNC_LEAVE_NOAPI(ret_value)
}