summaryrefslogtreecommitdiffstats
path: root/src/H5FScache.c
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2007-06-29 03:12:45 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2007-06-29 03:12:45 (GMT)
commitcad9846d77ea4926bb0f13cf5151c1a7ac05ec87 (patch)
treec88b30412b25b6fc5df9534a6ff2cf1742393a36 /src/H5FScache.c
parent58467956ba56fc52dd03e3540f14b062f9a6c5bb (diff)
downloadhdf5-cad9846d77ea4926bb0f13cf5151c1a7ac05ec87.zip
hdf5-cad9846d77ea4926bb0f13cf5151c1a7ac05ec87.tar.gz
hdf5-cad9846d77ea4926bb0f13cf5151c1a7ac05ec87.tar.bz2
[svn-r13926] Description:
Add small interface to "wrap" a static buffer (usually on the stack), but still allow for buffers larger than the static buffer to be allocated. This can eliminate _many_ short-lived buffer allocations in situations where the buffer is a predictable size (or at least a "very likely" size). Also, some minor code cleanups, particularly in the SOHM caching code. Tested on: Mac OS X/32 10.4.10 (amazon)
Diffstat (limited to 'src/H5FScache.c')
-rw-r--r--src/H5FScache.c67
1 files changed, 42 insertions, 25 deletions
diff --git a/src/H5FScache.c b/src/H5FScache.c
index 001916e..700f026 100644
--- a/src/H5FScache.c
+++ b/src/H5FScache.c
@@ -37,6 +37,7 @@
#include "H5Eprivate.h" /* Error handling */
#include "H5FSpkg.h" /* File free space */
#include "H5Vprivate.h" /* Vectors and arrays */
+#include "H5WBprivate.h" /* Wrapped Buffers */
/****************/
/* Local Macros */
@@ -46,6 +47,9 @@
#define H5FS_HDR_VERSION 0 /* Header */
#define H5FS_SINFO_VERSION 0 /* Serialized sections */
+/* Size of stack buffer for serialized headers */
+#define H5FS_HDR_BUF_SIZE 256
+
/******************/
/* Local Typedefs */
@@ -118,9 +122,6 @@ const H5AC_class_t H5AC_FSPACE_SINFO[1] = {{
/* Local Variables */
/*******************/
-/* Declare a free list to manage free space header data to/from disk */
-H5FL_BLK_DEFINE_STATIC(header_block);
-
/* Declare a free list to manage free space section data to/from disk */
H5FL_BLK_DEFINE_STATIC(sect_block);
@@ -146,7 +147,9 @@ H5FS_cache_hdr_load(H5F_t *f, hid_t dxpl_id, haddr_t addr, const void *_fs_prot,
H5FS_t *fspace = NULL; /* Free space header info */
const H5FS_prot_t *fs_prot = (const H5FS_prot_t *)_fs_prot; /* User data for protecting */
size_t size; /* Header size */
- uint8_t *buf = NULL; /* Temporary buffer */
+ H5WB_t *wb = NULL; /* Wrapped buffer for header data */
+ uint8_t hdr_buf[H5FS_HDR_BUF_SIZE]; /* Buffer for header */
+ uint8_t *hdr; /* Pointer to header buffer */
const uint8_t *p; /* Pointer into raw data buffer */
uint32_t stored_chksum; /* Stored metadata checksum value */
uint32_t computed_chksum; /* Computed metadata checksum value */
@@ -170,18 +173,22 @@ HDfprintf(stderr, "%s: Load free space header, addr = %a\n", FUNC, addr);
/* Set free space manager's internal information */
fspace->addr = addr;
+ /* Wrap the local buffer for serialized header info */
+ if(NULL == (wb = H5WB_wrap(hdr_buf, sizeof(hdr_buf))))
+ HGOTO_ERROR(H5E_FSPACE, H5E_CANTINIT, NULL, "can't wrap buffer")
+
/* Compute the size of the free space header on disk */
size = H5FS_HEADER_SIZE(f);
- /* Allocate temporary buffer */
- if((buf = H5FL_BLK_MALLOC(header_block, size)) == NULL)
- HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
+ /* Get a pointer to a buffer that's large enough for header */
+ if(NULL == (hdr = H5WB_actual(wb, size)))
+ HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, NULL, "can't get actual buffer")
/* Read header from disk */
- if(H5F_block_read(f, H5FD_MEM_FSPACE_HDR, addr, size, dxpl_id, buf) < 0)
+ if(H5F_block_read(f, H5FD_MEM_FSPACE_HDR, addr, size, dxpl_id, hdr) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_READERROR, NULL, "can't read free space header")
- p = buf;
+ p = hdr;
/* Magic number */
if(HDmemcmp(p, H5FS_HDR_MAGIC, (size_t)H5FS_SIZEOF_MAGIC))
@@ -237,12 +244,12 @@ HDfprintf(stderr, "%s: Load free space header, addr = %a\n", FUNC, addr);
H5F_DECODE_LENGTH(f, p, fspace->alloc_sect_size);
/* Compute checksum on indirect block */
- computed_chksum = H5_checksum_metadata(buf, (size_t)(p - buf), 0);
+ computed_chksum = H5_checksum_metadata(hdr, (size_t)(p - hdr), 0);
/* Metadata checksum */
UINT32DECODE(p, stored_chksum);
- HDassert((size_t)(p - buf) == size);
+ HDassert((size_t)(p - hdr) == size);
/* Verify checksum */
if(stored_chksum != computed_chksum)
@@ -252,8 +259,9 @@ HDfprintf(stderr, "%s: Load free space header, addr = %a\n", FUNC, addr);
ret_value = fspace;
done:
- if(buf)
- H5FL_BLK_FREE(header_block, buf);
+ /* Release resources */
+ if(wb && H5WB_unwrap(wb) < 0)
+ HDONE_ERROR(H5E_FSPACE, H5E_CLOSEERROR, NULL, "can't close wrapped buffer")
if(!ret_value && fspace)
(void)H5FS_cache_hdr_dest(f, fspace);
@@ -284,6 +292,8 @@ done:
static herr_t
H5FS_cache_hdr_flush(H5F_t *f, hid_t dxpl_id, hbool_t destroy, haddr_t addr, H5FS_t *fspace, unsigned UNUSED * flags_ptr)
{
+ H5WB_t *wb = NULL; /* Wrapped buffer for header data */
+ uint8_t hdr_buf[H5FS_HDR_BUF_SIZE]; /* Buffer for header */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT(H5FS_cache_hdr_flush)
@@ -297,19 +307,24 @@ HDfprintf(stderr, "%s: Flushing free space header, addr = %a, destroy = %u\n", F
HDassert(fspace);
if(fspace->cache_info.is_dirty) {
- uint8_t *buf = NULL; /* Temporary raw data buffer */
- uint8_t *p; /* Pointer into raw data buffer */
+ uint8_t *hdr; /* Pointer to header buffer */
+ uint8_t *p; /* Pointer into raw data buffer */
uint32_t metadata_chksum; /* Computed metadata checksum value */
- size_t size; /* Header size on disk */
+ size_t size; /* Header size on disk */
+
+ /* Wrap the local buffer for serialized header info */
+ if(NULL == (wb = H5WB_wrap(hdr_buf, sizeof(hdr_buf))))
+ HGOTO_ERROR(H5E_FSPACE, H5E_CANTINIT, FAIL, "can't wrap buffer")
/* Compute the size of the free space header on disk */
size = H5FS_HEADER_SIZE(f);
- /* Allocate temporary buffer */
- if((buf = H5FL_BLK_MALLOC(header_block, size)) == NULL)
- HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
+ /* Get a pointer to a buffer that's large enough for header */
+ if(NULL == (hdr = H5WB_actual(wb, size)))
+ HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "can't get actual buffer")
- p = buf;
+ /* Get temporary pointer to header */
+ p = hdr;
/* Magic number */
HDmemcpy(p, H5FS_HDR_MAGIC, (size_t)H5FS_SIZEOF_MAGIC);
@@ -358,18 +373,16 @@ HDfprintf(stderr, "%s: Flushing free space header, addr = %a, destroy = %u\n", F
H5F_ENCODE_LENGTH(f, p, fspace->alloc_sect_size);
/* Compute checksum */
- metadata_chksum = H5_checksum_metadata(buf, (size_t)(p - buf), 0);
+ metadata_chksum = H5_checksum_metadata(hdr, (size_t)(p - hdr), 0);
/* Metadata checksum */
UINT32ENCODE(p, metadata_chksum);
/* Write the free space header. */
- HDassert((size_t)(p - buf) == size);
- if(H5F_block_write(f, H5FD_MEM_FSPACE_HDR, addr, size, dxpl_id, buf) < 0)
+ HDassert((size_t)(p - hdr) == size);
+ if(H5F_block_write(f, H5FD_MEM_FSPACE_HDR, addr, size, dxpl_id, hdr) < 0)
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFLUSH, FAIL, "unable to save free space header to disk")
- H5FL_BLK_FREE(header_block, buf);
-
fspace->cache_info.is_dirty = FALSE;
} /* end if */
@@ -378,6 +391,10 @@ HDfprintf(stderr, "%s: Flushing free space header, addr = %a, destroy = %u\n", F
HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "unable to destroy free space header")
done:
+ /* Release resources */
+ if(wb && H5WB_unwrap(wb) < 0)
+ HDONE_ERROR(H5E_FSPACE, H5E_CLOSEERROR, FAIL, "can't close wrapped buffer")
+
FUNC_LEAVE_NOAPI(ret_value)
} /* H5FS_cache_hdr_flush() */