/*------------------------------------------------------------------------- * Function: H5MP_new_page * * Purpose: Allocate new page for a memory pool * * Return: Pointer to the page allocated on success/NULL on failure * * Programmer: Quincey Koziol * [email protected] * May 4 2005 * *------------------------------------------------------------------------- */ static H5MP_page_t * H5MP_new_page(H5MP_pool_t *mp, size_t page_size) { H5MP_page_t *new_page; /* New page created */ H5MP_page_blk_t *first_blk; /* Pointer to first block in page */ H5MP_page_t *ret_value; /* Return value */ FUNC_ENTER_NOAPI_NOINIT(H5MP_new_page) /* Sanity check */ HDassert(mp); HDassert(page_size >= mp->page_size); /* Allocate page */ if(page_size > mp->page_size) { if(NULL == (new_page = H5MM_malloc(page_size))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for page") new_page->free_size = page_size - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)); new_page->fac_alloc = FALSE; } /* end if */ else { if((new_page = H5FL_FAC_MALLOC(mp->page_fac)) == NULL) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for page") new_page->free_size = mp->max_size; new_page->fac_alloc = TRUE; } /* end else */ #ifdef QAK HDfprintf(stderr,"%s: Allocating new page = %p\n", FUNC, new_page); #endif /* QAK */ /* Initialize page information */ first_blk = H5MP_PAGE_FIRST_BLOCK(new_page); first_blk->size = new_page->free_size; first_blk->page = new_page; first_blk->is_free = TRUE; first_blk->prev = NULL; first_blk->next = NULL; /* Insert into page list */ new_page->prev = NULL; new_page->next = mp->first; if(mp->first) mp->first->prev = new_page; mp->first = new_page; /* Account for new free space */ new_page->free_blk = first_blk; mp->free_size += new_page->free_size; /* Assign return value */ ret_value = new_page; done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5MP_new_page() */
/*------------------------------------------------------------------------- * Function: H5B2__create_leaf * * Purpose: Creates empty leaf node of a B-tree and update node pointer * to point to it. * * Return: Non-negative on success/Negative on failure * * Programmer: Quincey Koziol * [email protected] * Feb 2 2005 * *------------------------------------------------------------------------- */ herr_t H5B2__create_leaf(H5B2_hdr_t *hdr, hid_t dxpl_id, void *parent, H5B2_node_ptr_t *node_ptr) { H5B2_leaf_t *leaf = NULL; /* Pointer to new leaf node created */ hbool_t inserted = FALSE; /* Whether the leaf node was inserted into cache */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_PACKAGE /* Check arguments. */ HDassert(hdr); HDassert(node_ptr); /* Allocate memory for leaf information */ if(NULL == (leaf = H5FL_CALLOC(H5B2_leaf_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for B-tree leaf info") /* Increment ref. count on B-tree header */ if(H5B2__hdr_incr(hdr) < 0) HGOTO_ERROR(H5E_BTREE, H5E_CANTINC, FAIL, "can't increment ref. count on B-tree header") /* Share B-tree header information */ leaf->hdr = hdr; /* Allocate space for the native keys in memory */ if(NULL == (leaf->leaf_native = (uint8_t *)H5FL_FAC_MALLOC(hdr->node_info[0].nat_rec_fac))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for B-tree leaf native keys") HDmemset(leaf->leaf_native, 0, hdr->cls->nrec_size * hdr->node_info[0].max_nrec); /* Set parent */ leaf->parent = parent; /* Set shadowed epoch to header's epoch */ leaf->shadow_epoch = hdr->shadow_epoch; /* Allocate space on disk for the leaf */ if(HADDR_UNDEF == (node_ptr->addr = H5MF_alloc(hdr->f, H5FD_MEM_BTREE, dxpl_id, (hsize_t)hdr->node_size))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "file allocation failed for B-tree leaf node") /* Cache the new B-tree node */ if(H5AC_insert_entry(hdr->f, dxpl_id, H5AC_BT2_LEAF, node_ptr->addr, leaf, H5AC__NO_FLAGS_SET) < 0) HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "can't add B-tree leaf to cache") inserted = TRUE; /* Add leaf node as child of 'top' proxy */ if(hdr->top_proxy) { if(H5AC_proxy_entry_add_child(hdr->top_proxy, hdr->f, dxpl_id, leaf) < 0) HGOTO_ERROR(H5E_BTREE, H5E_CANTSET, FAIL, "unable to add v2 B-tree node as child of proxy") leaf->top_proxy = hdr->top_proxy; } /* end if */ done: if(ret_value < 0) { if(leaf) { /* Remove from cache, if inserted */ if(inserted) if(H5AC_remove_entry(leaf) < 0) HDONE_ERROR(H5E_BTREE, H5E_CANTREMOVE, FAIL, "unable to remove v2 B-tree leaf node from cache") /* Release leaf node's disk space */ if(H5F_addr_defined(node_ptr->addr) && H5MF_xfree(hdr->f, H5FD_MEM_BTREE, dxpl_id, node_ptr->addr, (hsize_t)hdr->node_size) < 0) HDONE_ERROR(H5E_BTREE, H5E_CANTFREE, FAIL, "unable to release file space for v2 B-tree leaf node") /* Destroy leaf node */ if(H5B2__leaf_free(leaf) < 0) HDONE_ERROR(H5E_BTREE, H5E_CANTFREE, FAIL, "unable to release v2 B-tree leaf node") } /* end if */ } /* end if */ FUNC_LEAVE_NOAPI(ret_value) } /* H5B2__create_leaf() */