diff options
Diffstat (limited to 'src/H5MP.c')
-rw-r--r-- | src/H5MP.c | 186 |
1 files changed, 88 insertions, 98 deletions
@@ -27,36 +27,33 @@ *------------------------------------------------------------------------- */ -#include "H5MPmodule.h" /* This source code file is part of the H5MP module */ +#include "H5MPmodule.h" /* This source code file is part of the H5MP module */ /* Private headers */ -#include "H5private.h" /* Generic Functions */ -#include "H5Eprivate.h" /* Error handling */ -#include "H5MMprivate.h" /* Memory management */ -#include "H5MPpkg.h" /* Memory Pools */ +#include "H5private.h" /* Generic Functions */ +#include "H5Eprivate.h" /* Error handling */ +#include "H5MMprivate.h" /* Memory management */ +#include "H5MPpkg.h" /* Memory Pools */ /****************/ /* Local Macros */ /****************/ /* Minimum sized block */ -#define H5MP_MIN_BLOCK (H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGNMENT) +#define H5MP_MIN_BLOCK (H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)) + H5MP_BLOCK_ALIGNMENT) /* First block in page */ -#define H5MP_PAGE_FIRST_BLOCK(p) \ +#define H5MP_PAGE_FIRST_BLOCK(p) \ (H5MP_page_blk_t *)((void *)((unsigned char *)(p) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)))) - /******************/ /* Local Typedefs */ /******************/ - /********************/ /* Local Prototypes */ /********************/ - /********************************/ /* Package Variable Definitions */ /********************************/ @@ -64,7 +61,6 @@ /* Package initialization variable */ hbool_t H5_PKG_INIT_VAR = FALSE; - /********************/ /* Static Variables */ /********************/ @@ -72,8 +68,6 @@ hbool_t H5_PKG_INIT_VAR = FALSE; /* Declare a free list to manage the H5MP_pool_t struct */ H5FL_DEFINE(H5MP_pool_t); - - /*------------------------------------------------------------------------- * Function: H5MP_create * @@ -89,40 +83,39 @@ H5FL_DEFINE(H5MP_pool_t); H5MP_pool_t * H5MP_create(size_t page_size, unsigned flags) { - H5MP_pool_t *mp = NULL; /* New memory pool header */ - H5MP_pool_t *ret_value = NULL; /* Return value */ + H5MP_pool_t *mp = NULL; /* New memory pool header */ + H5MP_pool_t *ret_value = NULL; /* Return value */ FUNC_ENTER_NOAPI(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") + 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; + 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)); + mp->first = NULL; + mp->max_size = mp->page_size - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_t)); /* Create factory for pool pages */ - if(NULL == (mp->page_fac = H5FL_fac_init(page_size))) - HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, NULL, "can't create page factory") + if (NULL == (mp->page_fac = H5FL_fac_init(page_size))) + HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, NULL, "can't create page factory") /* Set return value */ ret_value = mp; done: - if(NULL == ret_value && mp) - if(H5MP_close(mp) < 0) + if (NULL == ret_value && mp) + if (H5MP_close(mp) < 0) HDONE_ERROR(H5E_RESOURCE, H5E_CANTFREE, NULL, "unable to free memory pool header") FUNC_LEAVE_NOAPI(ret_value) } /* end H5MP_create() */ - /*------------------------------------------------------------------------- * Function: H5MP__new_page * @@ -138,9 +131,9 @@ done: 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 = NULL; /* Return value */ + 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 = NULL; /* Return value */ FUNC_ENTER_STATIC @@ -149,31 +142,31 @@ H5MP__new_page(H5MP_pool_t *mp, size_t page_size) HDassert(page_size >= mp->page_size); /* Allocate page */ - if(page_size > mp->page_size) { - if(NULL == (new_page = (H5MP_page_t *)H5MM_malloc(page_size))) + if (page_size > mp->page_size) { + if (NULL == (new_page = (H5MP_page_t *)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(NULL == (new_page = (H5MP_page_t *)H5FL_FAC_MALLOC(mp->page_fac))) + if (NULL == (new_page = (H5MP_page_t *)H5FL_FAC_MALLOC(mp->page_fac))) 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 */ /* 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 = 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; + first_blk->prev = NULL; + first_blk->next = NULL; /* Insert into page list */ new_page->prev = NULL; new_page->next = mp->first; - if(mp->first) + if (mp->first) mp->first->prev = new_page; mp->first = new_page; @@ -188,7 +181,6 @@ done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5MP__new_page() */ - /*------------------------------------------------------------------------- * Function: H5MP_malloc * @@ -202,12 +194,12 @@ done: *------------------------------------------------------------------------- */ void * -H5MP_malloc (H5MP_pool_t *mp, size_t request) +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 = NULL; /* Return value */ + 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 = NULL; /* Return value */ FUNC_ENTER_NOAPI(NULL) @@ -219,26 +211,26 @@ H5MP_malloc (H5MP_pool_t *mp, size_t request) needed = H5MP_BLOCK_ALIGN(request) + H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)); /* 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 */ + 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; + alloc_page = mp->first; pool_free_avail = mp->free_size; - while(alloc_page && pool_free_avail >= needed) { + 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 */ + 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; + 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) { + 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 */ + 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; @@ -247,7 +239,7 @@ H5MP_malloc (H5MP_pool_t *mp, size_t request) /* Go to next block */ alloc_free = alloc_free->next; } /* end while */ - } /* end if */ + } /* end if */ /* Decrement amount of potential space left */ pool_free_avail -= alloc_page->free_size; @@ -255,17 +247,17 @@ H5MP_malloc (H5MP_pool_t *mp, size_t request) /* Go to next page */ alloc_page = alloc_page->next; } /* end while */ - } /* end if */ + } /* end if */ { - size_t page_size; /* Size of page needed */ + 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; + 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))) + 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 */ @@ -280,24 +272,24 @@ found: 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 */ + 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 *)((void *)(((unsigned char *)alloc_free) + needed)); /* Link into existing lists */ new_free->next = alloc_free->next; - if(alloc_free->next) + if (alloc_free->next) alloc_free->next->prev = new_free; - new_free->prev = alloc_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; + 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 { @@ -307,7 +299,7 @@ found: /* Update page & pool's free size information */ alloc_page->free_size -= alloc_free->size; - if(alloc_page->free_blk == alloc_free) + if (alloc_page->free_blk == alloc_free) alloc_page->free_blk = alloc_free->next; mp->free_size -= alloc_free->size; @@ -318,7 +310,6 @@ done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5MP_malloc() */ - /*------------------------------------------------------------------------- * Function: H5MP_free * @@ -336,9 +327,9 @@ done: 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 */ + 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_NOINIT_NOERR @@ -347,7 +338,8 @@ H5MP_free(H5MP_pool_t *mp, void *spc) HDassert(spc); /* Get block header for space to free */ - spc_blk = (H5MP_page_blk_t *)((void *)(((unsigned char *)spc) - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)))); + spc_blk = + (H5MP_page_blk_t *)((void *)(((unsigned char *)spc) - H5MP_BLOCK_ALIGN(sizeof(H5MP_page_blk_t)))); /* Mark block as free */ HDassert(spc_blk->is_free == FALSE); @@ -359,51 +351,50 @@ H5MP_free(H5MP_pool_t *mp, void *spc) 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) { + if (spc_page != mp->first) { /* Remove page from list */ spc_page->prev->next = spc_page->next; - if(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; + spc_page->prev = NULL; + spc_page->next = mp->first; mp->first->prev = spc_page; - mp->first = 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 */ + 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) { + if (next_blk->is_free) { spc_blk->size += next_blk->size; spc_blk->next = next_blk->next; } /* end if */ - } /* 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 */ + 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) { + if (prev_blk->is_free) { prev_blk->size += spc_blk->size; prev_blk->next = spc_blk->next; } /* end if */ - } /* 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) + 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() */ - /*------------------------------------------------------------------------- * Function: H5MP_close * @@ -419,32 +410,32 @@ H5MP_free(H5MP_pool_t *mp, void *spc) herr_t H5MP_close(H5MP_pool_t *mp) { - herr_t ret_value = SUCCEED; /* Return value */ + herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(FAIL) /* Release memory for pool pages */ - if(mp->first != NULL) { - H5MP_page_t *page, *next_page; /* Pointer to pages in pool */ + if (mp->first != NULL) { + H5MP_page_t *page, *next_page; /* Pointer to pages in pool */ /* Iterate through pages, releasing them */ page = mp->first; - while(page) { + while (page) { next_page = page->next; /* Free the page appropriately */ - if(page->fac_alloc) + if (page->fac_alloc) page = (H5MP_page_t *)H5FL_FAC_FREE(mp->page_fac, page); else page = (H5MP_page_t *)H5MM_xfree(page); page = next_page; } /* end while */ - } /* end if */ + } /* end if */ /* Release page factory */ - if(mp->page_fac) - if(H5FL_fac_term(mp->page_fac) < 0) + if (mp->page_fac) + if (H5FL_fac_term(mp->page_fac) < 0) HGOTO_ERROR(H5E_RESOURCE, H5E_CANTRELEASE, FAIL, "can't destroy page factory") done: @@ -453,4 +444,3 @@ done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5MP_close() */ - |