예제 #1
0
파일: H5MP.c 프로젝트: ekjstm/permafrost
/*-------------------------------------------------------------------------
 * Function:	H5MP_create
 *
 * Purpose:	Create a new memory pool
 *
 * Return:	Pointer to the memory pool "header" on success/NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		May  2 2005
 *
 *-------------------------------------------------------------------------
 */
H5MP_pool_t *
H5MP_create (size_t page_size, unsigned flags)
{
    H5MP_pool_t *mp;                    /* New memory pool header */
    H5MP_pool_t *ret_value;             /* Return value */

    FUNC_ENTER_NOAPI(H5MP_create, NULL)

    /* Allocate space for the pool header */
    if (NULL==(mp = H5FL_MALLOC(H5MP_pool_t)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for memory pool header")

    /* Assign information */
    mp->page_size = H5MP_BLOCK_ALIGN(page_size);
    mp->flags = flags;

    /* Initialize information */
    mp->free_size = 0;
    mp->first = NULL;
    mp->max_size = mp->page_size - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t));

    /* Create factory for pool pages */
    if((mp->page_fac=H5FL_fac_init(page_size))==NULL)
	HGOTO_ERROR (H5E_RESOURCE, H5E_CANTINIT, NULL, "can't create page factory")

    /* Set return value */
    ret_value = mp;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5MP_create() */
예제 #2
0
파일: pool.c 프로젝트: chaako/sceptic3D
/*-------------------------------------------------------------------------
 * Function:	test_allocate_many_small
 *
 * Purpose:	Tests allocating many small blocks in a pool
 *
 * Return:	Success:	0
 *
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, May 6, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_allocate_many_small(void)
{
    H5MP_pool_t *mp;            /* Memory pool */
    size_t free_size;           /* Free size in pool */
    void *spc[MPOOL_NUM_SMALL_BLOCKS]; /* Pointers to space allocated */
    int i;                      /* Local index variable */

    /*
     * Test memory pool allocation
     */
    TESTING("allocating many small blocks");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    for(i = 0; i < MPOOL_NUM_SMALL_BLOCKS; i++)
        if(NULL == (spc[i] = H5MP_malloc(mp, (size_t)MPOOL_SMALL_BLOCK)))
            TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (((H5MP_BLOCK_ALIGN(MPOOL_SMALL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t))) * MPOOL_NUM_SMALL_BLOCKS) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Free blocks in pool */
    /* (Tests free block merging with block after it */
    for(i = (MPOOL_NUM_SMALL_BLOCKS - 1); i >= 0; i--)
        H5MP_free(mp, spc[i]);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    return 0;

error:
    H5E_BEGIN_TRY {
    } H5E_END_TRY;
    return 1;
} /* test_allocate_many_small() */
예제 #3
0
파일: H5MP.c 프로젝트: ekjstm/permafrost
/*-------------------------------------------------------------------------
 * 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() */
예제 #4
0
파일: pool.c 프로젝트: chaako/sceptic3D
/*-------------------------------------------------------------------------
 * Function:	test_allocate_new_page
 *
 * Purpose:	Tests allocating block in pool that requires allocating
 *              new page
 *
 * Return:	Success:	0
 *
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Friday, May 6, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_allocate_new_page(void)
{
    H5MP_pool_t *mp;            /* Memory pool */
    size_t free_size;           /* Free size in pool */
    size_t u;                   /* Local index variable */
    void *spc[MPOOL_NUM_NORMAL_BLOCKS]; /* Pointer to space allocated */
    void *spc1;                 /* Pointer to space allocated */
    void *spc2;                 /* Pointer to space allocated */

    /*
     * Test memory pool allocation
     */
    TESTING("allocate normal-sized block in new page");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    for(u = 0; u < MPOOL_NUM_NORMAL_BLOCKS; u++)
        if(NULL == (spc[u] = H5MP_malloc(mp, (size_t)MPOOL_NORMAL_BLOCK)))
            TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != (MPOOL_PAGE_SIZE * 3) - (((H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t))) * MPOOL_NUM_NORMAL_BLOCKS) + (H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)) * 3)))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Free blocks in pool */
    /* (Free alternating blocks, in two passes, which tests block merging w/both neighbors) */
    for(u = 0; u < MPOOL_NUM_NORMAL_BLOCKS; u+=2)
        H5MP_free(mp, spc[u]);
    for(u = 1; u < MPOOL_NUM_NORMAL_BLOCKS; u+=2)
        H5MP_free(mp, spc[u]);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != ((MPOOL_PAGE_SIZE - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))) * 3))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    TESTING("allocate large-sized block in new page");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    /* (Normal sized block) */
    if(NULL == (spc1 = H5MP_malloc(mp, (size_t)MPOOL_NORMAL_BLOCK)))
        TEST_ERROR
    /* (Larger sized block) */
    if(NULL == (spc2 = H5MP_malloc(mp, (size_t)MPOOL_LARGE_BLOCK)))
        TEST_ERROR

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Free blocks in pool */
    H5MP_free(mp, spc1);
    H5MP_free(mp, spc2);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != ((MPOOL_PAGE_SIZE - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))) +
            MPOOL_LARGE_BLOCK + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    return 0;

error:
    H5E_BEGIN_TRY {
    } H5E_END_TRY;
    return 1;
} /* test_allocate_new_page() */
예제 #5
0
파일: pool.c 프로젝트: chaako/sceptic3D
/*-------------------------------------------------------------------------
 * Function:	test_allocate_split
 *
 * Purpose:	Tests allocating block in pool that requires splitting
 *              existing block
 *
 * Return:	Success:	0
 *
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, May 3, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_allocate_split(void)
{
    H5MP_pool_t *mp;            /* Memory pool */
    size_t free_size;           /* Free size in pool */
    void *spc1;                 /* Pointer to space allocated */
    void *spc2;                 /* Pointer to space allocated */

    /*
     * Test memory pool allocation
     */
    TESTING("splitting block in pool");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    if(NULL == (spc1 = H5MP_malloc(mp, (size_t)MPOOL_NORMAL_BLOCK)))
        TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Allocate more space in pool */
    if(NULL == (spc2 = H5MP_malloc(mp, (size_t)MPOOL_NORMAL_BLOCK)))
        TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (((H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t))) * 2) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Free first block in pool */
    H5MP_free(mp, spc1);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Free second block in pool (should merge with first block) */
    H5MP_free(mp, spc2);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    return 0;

error:
    H5E_BEGIN_TRY {
    } H5E_END_TRY;
    return 1;
} /* test_allocate_split() */
예제 #6
0
파일: pool.c 프로젝트: chaako/sceptic3D
/*-------------------------------------------------------------------------
 * Function:	test_allocate_first
 *
 * Purpose:	Tests allocating first block in pool
 *
 * Return:	Success:	0
 *
 *		Failure:	1
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, May 3, 2005
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
test_allocate_first(void)
{
    H5MP_pool_t *mp;            /* Memory pool */
    H5MP_page_t *page;          /* Memory pool page */
    size_t free_size;           /* Free size in pool */
    void *spc;                  /* Pointer to space allocated */

    /*
     * Test memory pool allocation
     */
    TESTING("allocating first block in pool");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    if(NULL == (spc = H5MP_malloc(mp, (size_t)MPOOL_NORMAL_BLOCK)))
        TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Get first page */
    if (H5MP_get_pool_first_page(mp, &page) < 0)
        TEST_ERROR;
    if(page == NULL)
        TEST_ERROR

    /* Check page's free space */
    if (H5MP_get_page_free_size(page, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - (H5MP_BLOCK_ALIGN(MPOOL_NORMAL_BLOCK) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Check next page */
    if (H5MP_get_page_next_page(page, &page) < 0)
        TEST_ERROR;
    if(page != NULL)
        TEST_ERROR

    /* Free space in pool */
    H5MP_free(mp, spc);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_PAGE_SIZE - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    TESTING("allocating large first block in pool");

    /* Create a memory pool */
    if(NULL == (mp = H5MP_create((size_t)MPOOL_PAGE_SIZE, MPOOL_FLAGS)))
        TEST_ERROR

    /* Allocate space in pool */
    if(NULL == (spc = H5MP_malloc(mp, (size_t)MPOOL_LARGE_BLOCK)))
        TEST_ERROR

    /* Check pool's free space */
    if(H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != 0)
        TEST_ERROR

    /* Get first page */
    if (H5MP_get_pool_first_page(mp, &page) < 0)
        TEST_ERROR;
    if(page == NULL)
        TEST_ERROR

    /* Check page's free space */
    if (H5MP_get_page_free_size(page, &free_size) < 0)
        TEST_ERROR;
    if(free_size != 0)
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Check next page */
    if (H5MP_get_page_next_page(page, &page) < 0)
        TEST_ERROR;
    if(page != NULL)
        TEST_ERROR

    /* Free space in pool */
    H5MP_free(mp, spc);

    /* Check pool's free space */
    if (H5MP_get_pool_free_size(mp, &free_size) < 0)
        TEST_ERROR;
    if(free_size != MPOOL_LARGE_BLOCK + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)))
        TEST_ERROR

    /* Check that free space totals match */
    if (H5MP_pool_is_free_size_correct(mp) <= 0)
        TEST_ERROR;

    /* Close the memory pool */
    if (H5MP_close(mp) < 0)
        TEST_ERROR

    PASSED();

    return 0;

error:
    H5E_BEGIN_TRY {
    } H5E_END_TRY;
    return 1;
} /* test_allocate_first() */
예제 #7
0
파일: H5MP.c 프로젝트: ekjstm/permafrost
/*-------------------------------------------------------------------------
 * Function:	H5MP_free
 *
 * Purpose:	Release space in a memory pool
 *
 * Return:	NULL on success/NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		May  3 2005
 *
 * Note:        Should we release pages that have no used blocks?
 *
 *-------------------------------------------------------------------------
 */
void *
H5MP_free (H5MP_pool_t *mp, void *spc)
{
    H5MP_page_blk_t *spc_blk;           /* Block for space to free */
    H5MP_page_t *spc_page;              /* Page containing block to free */
    void *ret_value = NULL;             /* Return value */

    FUNC_ENTER_NOAPI_NOFUNC(H5MP_free)

    /* Sanity check */
    HDassert(mp);
    HDassert(spc);

    /* Get block header for space to free */
    spc_blk = (H5MP_page_blk_t *)(((unsigned char *)spc) - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)));

    /* Mark block as free */
    HDassert(spc_blk->is_free == FALSE);
    spc_blk->is_free = TRUE;

    /* Add it's space to the amount of free space in the page & pool */
    spc_page = spc_blk->page;
#ifdef QAK
HDfprintf(stderr,"%s: Freeing from page = %p\n", "H5MP_free", spc_page);
#endif /* QAK */
    spc_page->free_size += spc_blk->size;
    mp->free_size += spc_blk->size;

    /* Move page with newly freed space to front of list of pages in pool */
    if(spc_page != mp->first) {
        /* Remove page from list */
        spc_page->prev->next = spc_page->next;
        if(spc_page->next)
            spc_page->next->prev = spc_page->prev;

        /* Insert page at beginning of list */
        spc_page->prev = NULL;
        spc_page->next = mp->first;
        mp->first->prev = spc_page;
        mp->first = spc_page;
    } /* end if */

    /* Check if block can be merged with free space after it on page */
    if(spc_blk->next != NULL) {
        H5MP_page_blk_t *next_blk;          /* Block following space to free */

        next_blk = spc_blk->next;
        HDassert(next_blk->prev == spc_blk);
        if(next_blk->is_free) {
            spc_blk->size += next_blk->size;
            spc_blk->next = next_blk->next;
        } /* end if */
    } /* end if */

    /* Check if block can be merged with free space before it on page */
    if(spc_blk->prev != NULL) {
        H5MP_page_blk_t *prev_blk;          /* Block before space to free */

        prev_blk = spc_blk->prev;
        HDassert(prev_blk->next == spc_blk);
        if(prev_blk->is_free) {
            prev_blk->size += spc_blk->size;
            prev_blk->next = spc_blk->next;
        } /* end if */
    } /* end if */

    /* Check if the block freed becomes the first free block on the page */
    if(spc_page->free_blk == NULL || spc_blk < spc_page->free_blk)
        spc_page->free_blk = spc_blk;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5MP_free() */
예제 #8
0
파일: H5MP.c 프로젝트: ekjstm/permafrost
/*-------------------------------------------------------------------------
 * Function:	H5MP_malloc
 *
 * Purpose:	Allocate space in a memory pool
 *
 * Return:	Pointer to the space allocated on success/NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		[email protected]
 *		May  2 2005
 *
 *-------------------------------------------------------------------------
 */
void *
H5MP_malloc (H5MP_pool_t *mp, size_t request)
{
    H5MP_page_t *alloc_page = NULL; /* Page to allocate space from */
    H5MP_page_blk_t *alloc_free;    /* Pointer to free space in page */
    size_t needed;                  /* Size requested, plus block header and alignment */
    void *ret_value;                /* Return value */

    FUNC_ENTER_NOAPI(H5MP_malloc, NULL)

    /* Sanity check */
    HDassert(mp);
    HDassert(request > 0);

    /* Compute actual size needed */
    needed = H5MP_BLOCK_ALIGN(request) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t));
#ifdef QAK
HDfprintf(stderr,"%s: sizeof(H5MP_page_blk_t) = %Zu\n", FUNC, sizeof(H5MP_page_blk_t));
HDfprintf(stderr,"%s: request = %Zu, needed = %Zu\n", FUNC, request, needed);
#endif /* QAK */

    /* See if the request can be handled by existing free space */
    if(needed <= mp->free_size) {
        size_t pool_free_avail;      /* Amount of free space possibly available in pool */

        /* Locate page with enough free space */
        alloc_page = mp->first;
        pool_free_avail = mp->free_size;
        while(alloc_page && pool_free_avail >= needed) {
            /* If we found a page with enough free space, search for large
             * enough free block on that page */
            if(alloc_page->free_size >= needed) {
                size_t page_free_avail;      /* Amount of free space possibly available */

                /* Locate large enough block */
                alloc_free = alloc_page->free_blk;
                page_free_avail = alloc_page->free_size;
                while(alloc_free && page_free_avail >= needed) {
                    if(alloc_free->is_free) {
                        /* If we found a large enough block, leave now */
                        if(alloc_free->size >= needed)
                            goto found;     /* Needed to escape double "while" loop */

                        /* Decrement amount of potential space left */
                        page_free_avail -= alloc_free->size;
                    } /* end if */

                    /* Go to next block */
                    alloc_free = alloc_free->next;
                } /* end while */
            } /* end if */

            /* Decrement amount of potential space left */
            pool_free_avail -= alloc_page->free_size;

            /* Go to next page */
            alloc_page = alloc_page->next;
        } /* end while */
    } /* end if */

    {
        size_t page_size;       /* Size of page needed */

        /* Check if the request is too large for a standard page */
        page_size = (needed > mp->max_size) ?
            (needed + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t))) : mp->page_size;

        /* Allocate new page */
        if(NULL == (alloc_page = H5MP_new_page(mp, page_size)))
            HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for page")

        /* Set the block to allocate from */
        alloc_free = alloc_page->free_blk;
    } /* end block */

    /* Allocate space in page */
found:

    /* Sanity check */
    HDassert(alloc_page);
    HDassert(alloc_free);

    /* Check if we can subdivide the free space */
    if(alloc_free->size > (needed + H5MP_MIN_BLOCK)) {
        H5MP_page_blk_t *new_free;          /* New free block created */

        /* Carve out new free block after block to allocate */
        new_free = (H5MP_page_blk_t *)(((unsigned char *)alloc_free) + needed);

        /* Link into existing lists */
        new_free->next = alloc_free->next;
        if(alloc_free->next)
            alloc_free->next->prev = new_free;
        new_free->prev = alloc_free;
        alloc_free->next = new_free;

        /* Set blocks' information */
        new_free->size = alloc_free->size - needed;
        new_free->is_free = TRUE;
        new_free->page = alloc_free->page;
        alloc_free->size = needed;
        alloc_free->is_free = FALSE;
    } /* end if */
    else {
        /* Use whole free space block for new block */
        alloc_free->is_free = FALSE;
    } /* end else */

    /* Update page & pool's free size information */
    alloc_page->free_size -= alloc_free->size;
    if(alloc_page->free_blk == alloc_free)
        alloc_page->free_blk = alloc_free->next;
    mp->free_size -= alloc_free->size;

    /* Set new space pointer for the return value */
    ret_value = ((unsigned char *)alloc_free) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t));
#ifdef QAK
HDfprintf(stderr,"%s: Allocating space from page, ret_value = %p\n", FUNC, ret_value);
#endif /* QAK */

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