summaryrefslogtreecommitdiffstats
path: root/hl/test
diff options
context:
space:
mode:
Diffstat (limited to 'hl/test')
-rw-r--r--hl/test/test_dset_opt.c461
1 files changed, 377 insertions, 84 deletions
diff --git a/hl/test/test_dset_opt.c b/hl/test/test_dset_opt.c
index 03c467a..d3a6b5c 100644
--- a/hl/test/test_dset_opt.c
+++ b/hl/test/test_dset_opt.c
@@ -76,11 +76,10 @@ const H5Z_class2_t H5Z_BOGUS2[1] = {{
/*-------------------------------------------------------------------------
* Function: test_direct_chunk_write
*
- * Purpose: Test the basic functionality of H5DOwrite_chunk
+ * Purpose: Test the basic functionality of H5DOwrite_chunk/H5DOread_chunk
*
* Return: Success: 0
- *
- * Failure: 1
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -103,24 +102,31 @@ test_direct_chunk_write (hid_t file)
int i, j, n;
unsigned filter_mask = 0;
+ unsigned read_filter_mask = 0;
int direct_buf[CHUNK_NX][CHUNK_NY];
int check_chunk[CHUNK_NX][CHUNK_NY];
hsize_t offset[2] = {0, 0};
size_t buf_size = CHUNK_NX*CHUNK_NY*sizeof(int);
const Bytef *z_src = (const Bytef*)(direct_buf);
- Bytef *z_dst; /*destination buffer */
+ Bytef *z_dst; /*destination buffer */
uLongf z_dst_nbytes = (uLongf)DEFLATE_SIZE_ADJUST(buf_size);
uLong z_src_nbytes = (uLong)buf_size;
- int aggression = 9; /* Compression aggression setting */
- void *outbuf = NULL; /* Pointer to new buffer */
+ int aggression = 9; /* Compression aggression setting */
+ void *outbuf = NULL; /* Pointer to new buffer */
+
+ /* For H5DOread_chunk() */
+ void *readbuf = NULL; /* Buffer for reading data */
+ const Bytef *pt_readbuf; /* Point to the buffer for data read */
+ hsize_t read_chunk_nbytes; /* Size of chunk on disk */
+ int read_dst_buf[CHUNK_NX][CHUNK_NY]; /* Buffer to hold un-compressed data */
hsize_t start[2]; /* Start of hyperslab */
hsize_t stride[2]; /* Stride of hyperslab */
hsize_t count[2]; /* Block count */
hsize_t block[2]; /* Block sizes */
- TESTING("basic functionality of H5DOwrite_chunk");
+ TESTING("basic functionality of H5DOwrite_chunk/H5DOread_chunk");
/*
* Create the data space with unlimited dimensions.
@@ -151,26 +157,76 @@ test_direct_chunk_write (hid_t file)
cparms, H5P_DEFAULT)) < 0)
goto error;
- /* Initialize the dataset */
- for(i = n = 0; i < NX; i++)
- for(j = 0; j < NY; j++)
- data[i][j] = n++;
-
if((dxpl = H5Pcreate(H5P_DATASET_XFER)) < 0)
goto error;
+ HDmemset(data, 0, sizeof(data));
+ /* Initialize data for the first chunk */
+ for(i = n = 0; i < CHUNK_NX; i++)
+ for(j = 0; j < CHUNK_NY; j++)
+ data[i][j] = n++;
+
/*
- * Write the data for the dataset. It should stay in the chunk cache.
- * It will be evicted from the cache by the H5DOwrite_chunk calls.
- */
+ * Write the data for the dataset. */
if((status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
dxpl, data)) < 0)
goto error;
+ if(H5Fflush(dataset, H5F_SCOPE_LOCAL) < 0)
+ goto error;
+
+ if(H5Dclose(dataset) < 0)
+ goto error;
+
+ if((dataset = H5Dopen2(file, DATASETNAME1, H5P_DEFAULT)) < 0)
+ goto error;
+
+ offset[0] = offset[1] = 0;
+
+ /* Get the size of the compressed chunk */
+ ret = H5Dget_chunk_storage_size(dataset, offset, &read_chunk_nbytes);
+
+ readbuf = HDmalloc(read_chunk_nbytes);
+ pt_readbuf = (const Bytef *)readbuf;
+
+ /* Test to use H5DOread_chunk() to read the chunk back */
+ if((status = H5DOread_chunk(dataset, H5P_DEFAULT, offset, &read_filter_mask, readbuf)) < 0)
+ goto error;
+
+ /* uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) */
+ ret = uncompress((Bytef *)read_dst_buf, (uLongf *)&buf_size, pt_readbuf, (uLong)read_chunk_nbytes);
+
+ /* Check for various zlib errors */
+ if(Z_BUF_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough room in output buffer");
+ goto error;
+ } else if(Z_MEM_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough memory");
+ goto error;
+ } else if(Z_OK != ret) {
+ HDfprintf(stderr, "error: corrupted input data");
+ goto error;
+ }
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if(data[i][j] != read_dst_buf[i][j]) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" data=%d, read_dst_buf=%d\n", data[i][j], read_dst_buf[i][j]);
+ goto error;
+ }
+ }
+ }
+
+ if(readbuf)
+ HDfree(readbuf);
+
/* Initialize data for one chunk */
for(i = n = 0; i < CHUNK_NX; i++)
for(j = 0; j < CHUNK_NY; j++)
- direct_buf[i][j] = n++;
+ direct_buf[i][j] = n++;
/* Allocate output (compressed) buffer */
outbuf = HDmalloc(z_dst_nbytes);
@@ -181,18 +237,18 @@ test_direct_chunk_write (hid_t file)
/* Check for various zlib errors */
if(Z_BUF_ERROR == ret) {
- fprintf(stderr, "overflow");
+ HDfprintf(stderr, "overflow");
goto error;
} else if(Z_MEM_ERROR == ret) {
- fprintf(stderr, "deflate memory error");
+ HDfprintf(stderr, "deflate memory error");
goto error;
} else if(Z_OK != ret) {
- fprintf(stderr, "other deflate error");
+ HDfprintf(stderr, "other deflate error");
goto error;
}
- /* Write the compressed chunk data repeatedly to cover all the chunks in the
- * dataset, using the direct writing function. */
+ /* Write the compressed chunk data repeatedly to cover all the chunks in the
+ * dataset, using the direct writing function. */
for(i=0; i<NX/CHUNK_NX; i++) {
for(j=0; j<NY/CHUNK_NY; j++) {
status = H5DOwrite_chunk(dataset, dxpl, filter_mask, offset, z_dst_nbytes, outbuf);
@@ -205,7 +261,7 @@ test_direct_chunk_write (hid_t file)
if(outbuf)
HDfree(outbuf);
- if(H5Fflush(dataset, H5F_SCOPE_LOCAL) < 0)
+ if(H5Fflush(dataset, H5F_SCOPE_LOCAL) < 0)
goto error;
if(H5Dclose(dataset) < 0)
@@ -214,6 +270,59 @@ test_direct_chunk_write (hid_t file)
if((dataset = H5Dopen2(file, DATASETNAME1, H5P_DEFAULT)) < 0)
goto error;
+ offset[0] = CHUNK_NX;
+ offset[1] = CHUNK_NY;
+
+ /* Get the size of the compressed chunk */
+ ret = H5Dget_chunk_storage_size(dataset, offset, &read_chunk_nbytes);
+
+ if(read_chunk_nbytes != (hsize_t)z_dst_nbytes) {
+ HDfprintf(stderr, "Read/write chunk size not the same.");
+ goto error;
+ }
+
+ readbuf = HDmalloc(read_chunk_nbytes);
+ pt_readbuf = (const Bytef *)readbuf;
+
+ /* Test to use H5DOread_chunk() to read the chunk back */
+ if((status = H5DOread_chunk(dataset, H5P_DEFAULT, offset, &read_filter_mask, readbuf)) < 0)
+ goto error;
+
+ if(read_filter_mask != filter_mask) {
+ HDfprintf(stderr, " Read/write filter mask not the same.");
+ goto error;
+ }
+
+ /* uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) */
+ ret = uncompress((Bytef *)read_dst_buf, (uLongf *)&buf_size, pt_readbuf, (uLong)read_chunk_nbytes);
+
+ /* Check for various zlib errors */
+ if(Z_BUF_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough room in output buffer");
+ goto error;
+ } else if(Z_MEM_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough memory");
+ goto error;
+ } else if(Z_OK != ret) {
+ HDfprintf(stderr, "error: corrupted input data");
+ goto error;
+ }
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if(direct_buf[i][j] != read_dst_buf[i][j]) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" direct_buf=%d, read_dst_buf=%d\n", direct_buf[i][j], read_dst_buf[i][j]);
+ goto error;
+ }
+ }
+ }
+
+ if(readbuf)
+ HDfree(readbuf);
+
/*
* Select hyperslab for one chunk in the file
*/
@@ -224,7 +333,7 @@ test_direct_chunk_write (hid_t file)
if((status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block)) < 0)
goto error;
- /* Read the chunk back */
+ /* Test to use H5Dread() to read the chunk back */
if((status = H5Dread(dataset, H5T_NATIVE_INT, mem_space, dataspace, H5P_DEFAULT, check_chunk)) < 0)
goto error;
@@ -234,38 +343,40 @@ test_direct_chunk_write (hid_t file)
if(direct_buf[i][j] != check_chunk[i][j]) {
printf(" 1. Read different values than written.");
printf(" At index %d,%d\n", i, j);
- printf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]);
+ printf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]);
goto error;
}
}
}
+
/* Reinitialize different data for one chunk */
for(i = 0; i < CHUNK_NX; i++)
for(j = 0; j < CHUNK_NY; j++)
- direct_buf[i][j] = i + j;
+ direct_buf[i][j] = i + j;
/* Allocate output (compressed) buffer */
outbuf = HDmalloc(z_dst_nbytes);
z_dst = (Bytef *)outbuf;
+
/* Perform compression from the source to the destination buffer */
ret = compress2(z_dst, &z_dst_nbytes, z_src, z_src_nbytes, aggression);
/* Check for various zlib errors */
if(Z_BUF_ERROR == ret) {
- fprintf(stderr, "overflow");
+ HDfprintf(stderr, "overflow");
goto error;
} else if(Z_MEM_ERROR == ret) {
- fprintf(stderr, "deflate memory error");
+ HDfprintf(stderr, "deflate memory error");
goto error;
} else if(Z_OK != ret) {
- fprintf(stderr, "other deflate error");
+ HDfprintf(stderr, "other deflate error");
goto error;
}
- /* Rewrite the compressed chunk data repeatedly to cover all the chunks in the
- * dataset, using the direct writing function. */
+ /* Rewrite the compressed chunk data repeatedly to cover all the chunks in the
+ * dataset, using the direct writing function. */
offset[0] = offset[1] = 0;
for(i=0; i<NX/CHUNK_NX; i++) {
for(j=0; j<NY/CHUNK_NY; j++) {
@@ -279,7 +390,7 @@ test_direct_chunk_write (hid_t file)
if(outbuf)
HDfree(outbuf);
- if(H5Fflush(dataset, H5F_SCOPE_LOCAL) < 0)
+ if(H5Fflush(dataset, H5F_SCOPE_LOCAL) < 0)
goto error;
if(H5Dclose(dataset) < 0)
@@ -288,7 +399,60 @@ test_direct_chunk_write (hid_t file)
if((dataset = H5Dopen2(file, DATASETNAME1, H5P_DEFAULT)) < 0)
goto error;
- /* Read the chunk back */
+ offset[0] = CHUNK_NX;
+ offset[1] = CHUNK_NY;
+
+ /* Get the size of the compressed chunk */
+ ret = H5Dget_chunk_storage_size(dataset, offset, &read_chunk_nbytes);
+
+ if(read_chunk_nbytes != (hsize_t)z_dst_nbytes) {
+ HDfprintf(stderr, "Read/write chunk size not the same.");
+ goto error;
+ }
+
+ readbuf = HDmalloc(read_chunk_nbytes);
+ pt_readbuf = (const Bytef *)readbuf;
+
+ /* Test to use H5DOread_chunk() to read the chunk back */
+ if((status = H5DOread_chunk(dataset, H5P_DEFAULT, offset, &read_filter_mask, readbuf)) < 0)
+ goto error;
+
+ if(read_filter_mask != filter_mask) {
+ HDfprintf(stderr, " Read/write filter mask not the same.");
+ goto error;
+ }
+
+ /* uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) */
+ ret = uncompress((Bytef *)read_dst_buf, (uLongf *)&buf_size, pt_readbuf, (uLong)read_chunk_nbytes);
+
+ /* Check for various zlib errors */
+ if(Z_BUF_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough room in output buffer");
+ goto error;
+ } else if(Z_MEM_ERROR == ret) {
+ HDfprintf(stderr, "error: not enough memory");
+ goto error;
+ } else if(Z_OK != ret) {
+ HDfprintf(stderr, "error: corrupted input data");
+ goto error;
+ }
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if(direct_buf[i][j] != read_dst_buf[i][j]) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" direct_buf=%d, my_buf=%d\n", direct_buf[i][j], read_dst_buf[i][j]);
+ goto error;
+ }
+ }
+ }
+
+ if(readbuf)
+ HDfree(readbuf);
+
+ /* Test to use H5Dread() to read the chunk back */
if((status = H5Dread(dataset, H5T_NATIVE_INT, mem_space, dataspace, H5P_DEFAULT, check_chunk)) < 0)
goto error;
@@ -298,7 +462,7 @@ test_direct_chunk_write (hid_t file)
if(direct_buf[i][j] != check_chunk[i][j]) {
printf(" 2. Read different values than written.");
printf(" At index %d,%d\n", i, j);
- printf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]);
+ printf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]);
goto error;
}
}
@@ -312,7 +476,7 @@ test_direct_chunk_write (hid_t file)
H5Sclose(dataspace);
H5Pclose(cparms);
H5Pclose(dxpl);
-
+
PASSED();
return 0;
@@ -327,6 +491,8 @@ error:
if(outbuf)
HDfree(outbuf);
+ if(readbuf)
+ HDfree(readbuf);
return 1;
}
@@ -335,12 +501,11 @@ error:
/*-------------------------------------------------------------------------
* Function: test_skip_compress_write1
*
- * Purpose: Test skipping compression filter when it is the only filter
+ * Purpose: Test skipping compression filter when it is the only filter
* for the dataset
*
* Return: Success: 0
- *
- * Failure: 1
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -360,6 +525,7 @@ test_skip_compress_write1(hid_t file)
int i, j, n;
unsigned filter_mask = 0;
+ unsigned read_filter_mask = 0;
int direct_buf[CHUNK_NX][CHUNK_NY];
int check_chunk[CHUNK_NX][CHUNK_NY];
hsize_t offset[2] = {0, 0};
@@ -371,7 +537,7 @@ test_skip_compress_write1(hid_t file)
hsize_t count[2]; /* Block count */
hsize_t block[2]; /* Block sizes */
- TESTING("skipping compression filter for H5DOwrite_chunk");
+ TESTING("skipping compression filter for H5DOwrite_chunk/H5DOread_chunk");
/*
* Create the data space with unlimited dimensions.
@@ -408,7 +574,7 @@ test_skip_compress_write1(hid_t file)
/* Initialize data for one chunk */
for(i = n = 0; i < CHUNK_NX; i++)
for(j = 0; j < CHUNK_NY; j++) {
- direct_buf[i][j] = n++;
+ direct_buf[i][j] = n++;
}
/* write the uncompressed chunk data repeatedly to dataset, using the direct writing function.
@@ -430,6 +596,30 @@ test_skip_compress_write1(hid_t file)
if((dataset = H5Dopen2(file, DATASETNAME2, H5P_DEFAULT)) < 0)
goto error;
+ /* Use H5DOread_chunk() to read the uncompressed data */
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &read_filter_mask, check_chunk)) < 0)
+ goto error;
+
+ if(read_filter_mask != filter_mask) {
+ HDfprintf(stderr, " Read/write filter mask not the same.");
+ goto error;
+ }
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if(direct_buf[i][j] != check_chunk[i][j]) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" direct_buf=%d, check_chunk=%d\n", direct_buf[i][j], check_chunk[i][j]);
+ goto error;
+ }
+ }
+ }
+
+ /* Clear the buffer */
+ HDmemset(check_chunk, 0, buf_size);
+
/*
* Select hyperslab for the chunk just written in the file
*/
@@ -440,7 +630,7 @@ test_skip_compress_write1(hid_t file)
if((status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block)) < 0)
goto error;
- /* Read the chunk back */
+ /* Use H5Dread() to read the chunk back */
if((status = H5Dread(dataset, H5T_NATIVE_INT, mem_space, dataspace, H5P_DEFAULT, check_chunk)) < 0)
goto error;
@@ -483,9 +673,9 @@ error:
/*-------------------------------------------------------------------------
* Function: filter_bogus1
*
- * Purpose: A bogus filte that adds ADD_ON to the original value
+ * Purpose: A bogus filter that adds ADD_ON to the original value
*
- * Return: Success: Data chunk size
+ * Return: Success: Data chunk size
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -558,12 +748,11 @@ filter_bogus2(unsigned int flags, size_t H5_ATTR_UNUSED cd_nelmts,
/*-------------------------------------------------------------------------
* Function: test_skip_compress_write2
*
- * Purpose: Test skipping compression filter when there are three filters
+ * Purpose: Test skipping compression filter when there are three filters
* for the dataset
*
- * Return: Success: 0
- *
- * Failure: 1
+ * Return: Success: 0
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -583,6 +772,7 @@ test_skip_compress_write2(hid_t file)
int i, j, n;
unsigned filter_mask = 0;
+ unsigned read_filter_mask = 0;
int origin_direct_buf[CHUNK_NX][CHUNK_NY];
int direct_buf[CHUNK_NX][CHUNK_NY];
int check_chunk[CHUNK_NX][CHUNK_NY];
@@ -618,10 +808,10 @@ test_skip_compress_write2(hid_t file)
/* Register and enable first bogus filter */
if(H5Zregister (H5Z_BOGUS1) < 0)
- goto error;
+ goto error;
if(H5Pset_filter(cparms, H5Z_FILTER_BOGUS1, 0, (size_t)0, NULL) < 0)
- goto error;
+ goto error;
/* Enable compression filter */
if((status = H5Pset_deflate( cparms, (unsigned) aggression)) < 0)
@@ -629,10 +819,10 @@ test_skip_compress_write2(hid_t file)
/* Register and enable second bogus filter */
if(H5Zregister (H5Z_BOGUS2) < 0)
- goto error;
+ goto error;
if(H5Pset_filter(cparms, H5Z_FILTER_BOGUS2, 0, (size_t)0, NULL) < 0)
- goto error;
+ goto error;
/*
* Create a new dataset within the file using cparms
@@ -648,9 +838,9 @@ test_skip_compress_write2(hid_t file)
/* Initialize data for one chunk. Apply operations of two bogus filters to the chunk */
for(i = n = 0; i < CHUNK_NX; i++)
for(j = 0; j < CHUNK_NY; j++) {
- origin_direct_buf[i][j] = n++;
- direct_buf[i][j] = (origin_direct_buf[i][j] + ADD_ON) * FACTOR;
- }
+ origin_direct_buf[i][j] = n++;
+ direct_buf[i][j] = (origin_direct_buf[i][j] + ADD_ON) * FACTOR;
+ }
/* write the uncompressed chunk data repeatedly to dataset, using the direct writing function.
* Indicate skipping the compression filter but keep the other two bogus filters */
@@ -672,6 +862,35 @@ test_skip_compress_write2(hid_t file)
if((dataset = H5Dopen2(file, DATASETNAME3, H5P_DEFAULT)) < 0)
goto error;
+ /* Use H5DOread_chunk() to read the uncompressed data */
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &read_filter_mask, check_chunk)) < 0)
+ goto error;
+
+ if(read_filter_mask != filter_mask) {
+ HDfprintf(stderr, " Read/write filter mask not the same.");
+ goto error;
+ }
+
+ /* De-compress the operations of the two bogus filters to the chunk */
+ for(i = n = 0; i < CHUNK_NX; i++)
+ for(j = 0; j < CHUNK_NY; j++)
+ check_chunk[i][j] = (check_chunk[i][j]/FACTOR) - ADD_ON;
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if(origin_direct_buf[i][j] != check_chunk[i][j]) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" origin_direct_buf=%d, check_chunk=%d\n", origin_direct_buf[i][j], check_chunk[i][j]);
+ goto error;
+ }
+ }
+ }
+
+ /* Clear the buffer */
+ HDmemset(check_chunk, 0, buf_size);
+
/*
* Select hyperslab for one chunk in the file
*/
@@ -682,7 +901,7 @@ test_skip_compress_write2(hid_t file)
if((status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block)) < 0)
goto error;
- /* Read the chunk back */
+ /* Use H5Dread() to read the chunk back */
if((status = H5Dread(dataset, H5T_NATIVE_INT, mem_space, dataspace, H5P_DEFAULT, check_chunk)) < 0)
goto error;
@@ -725,11 +944,10 @@ error:
/*-------------------------------------------------------------------------
* Function: test_data_conv
*
- * Purpose: Test data conversion
- *
- * Return: Success: 0
+ * Purpose: Test data conversion
*
- * Failure: 1
+ * Return: Success: 0
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -740,10 +958,10 @@ static int
test_data_conv(hid_t file)
{
typedef struct {
- int a, b, c[4], d, e;
+ int a, b, c[4], d, e;
} src_type_t;
typedef struct {
- int a, c[4], e;
+ int a, c[4], e;
} dst_type_t;
hid_t dataspace = -1, dataset = -1;
@@ -761,6 +979,8 @@ test_data_conv(hid_t file)
unsigned filter_mask = 0;
src_type_t direct_buf[CHUNK_NX][CHUNK_NY];
dst_type_t check_chunk[CHUNK_NX][CHUNK_NY];
+ src_type_t read_chunk[CHUNK_NX][CHUNK_NY]; /* For H5DOread_chunk */
+
hsize_t offset[2] = {0, 0};
size_t buf_size = CHUNK_NX*CHUNK_NY*sizeof(src_type_t);
@@ -769,7 +989,7 @@ test_data_conv(hid_t file)
hsize_t count[2]; /* Block count */
hsize_t block[2]; /* Block sizes */
- TESTING("data conversion for H5DOwrite_chunk");
+ TESTING("data conversion for H5DOwrite_chunk/H5DOread_chunk");
/*
* Create the data space with unlimited dimensions.
@@ -854,6 +1074,36 @@ test_data_conv(hid_t file)
if((dataset = H5Dopen2(file, DATASETNAME4, H5P_DEFAULT)) < 0)
goto error;
+ /* Use H5DOread_chunk() to read the uncompressed data */
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &filter_mask, read_chunk)) < 0)
+ goto error;
+
+ /* Check that the values read are the same as the values written */
+ for(i = 0; i < CHUNK_NX; i++) {
+ for(j = 0; j < CHUNK_NY; j++) {
+ if ((direct_buf[i][j]).a != (read_chunk[i][j]).a ||
+ (direct_buf[i][j]).b != (read_chunk[i][j]).b ||
+ (direct_buf[i][j]).c[0] != (read_chunk[i][j]).c[0] ||
+ (direct_buf[i][j]).c[1] != (read_chunk[i][j]).c[1] ||
+ (direct_buf[i][j]).c[2] != (read_chunk[i][j]).c[2] ||
+ (direct_buf[i][j]).c[3] != (read_chunk[i][j]).c[3] ||
+ (direct_buf[i][j]).d != (read_chunk[i][j]).d ||
+ (direct_buf[i][j]).e != (read_chunk[i][j]).e) {
+ printf(" 1. Read different values than written.");
+ printf(" At index %d,%d\n", i, j);
+ printf(" src={a=%d, b=%d, c=[%d,%d,%d,%d], d=%d, e=%d\n",
+ (direct_buf[i][j]).a, (direct_buf[i][j]).b, (direct_buf[i][j]).c[0], (direct_buf[i][j]).c[1],
+ (direct_buf[i][j]).c[2], (direct_buf[i][j]).c[3], (direct_buf[i][j]).d, (direct_buf[i][j]).e);
+ printf(" dst={a=%d, b=%d, c=[%d,%d,%d,%d], d=%d, e=%d\n",
+ (read_chunk[i][j]).a, (read_chunk[i][j]).b, (read_chunk[i][j]).c[0], (read_chunk[i][j]).c[1],
+ (read_chunk[i][j]).c[2], (read_chunk[i][j]).c[3], (read_chunk[i][j]).d, (read_chunk[i][j]).e);
+
+ goto error;
+ }
+ }
+ }
+
+
/*
* Select hyperslab for the chunk just written in the file
*/
@@ -879,14 +1129,14 @@ test_data_conv(hid_t file)
(direct_buf[i][j]).e != (check_chunk[i][j]).e) {
printf(" 1. Read different values than written.");
printf(" At index %d,%d\n", i, j);
- printf(" src={a=%d, b=%d, c=[%d,%d,%d,%d], d=%d, e=%d\n",
+ printf(" src={a=%d, b=%d, c=[%d,%d,%d,%d], d=%d, e=%d\n",
(direct_buf[i][j]).a, (direct_buf[i][j]).b, (direct_buf[i][j]).c[0], (direct_buf[i][j]).c[1],
(direct_buf[i][j]).c[2], (direct_buf[i][j]).c[3], (direct_buf[i][j]).d, (direct_buf[i][j]).e);
- printf(" dst={a=%d, c=[%d,%d,%d,%d], e=%d\n",
+ printf(" dst={a=%d, c=[%d,%d,%d,%d], e=%d\n",
(check_chunk[i][j]).a, (check_chunk[i][j]).c[0], (check_chunk[i][j]).c[1], (check_chunk[i][j]).c[2],
(check_chunk[i][j]).c[3], (check_chunk[i][j]).e);
- goto error;
+ goto error;
}
}
}
@@ -923,11 +1173,10 @@ error:
/*-------------------------------------------------------------------------
* Function: test_invalid_parameters
*
- * Purpose: Test invalid parameters for H5DOwrite_chunk
- *
- * Return: Success: 0
+ * Purpose: Test invalid parameters for H5DOwrite_chunk and H5DOread_chunk
*
- * Failure: 1
+ * Return: Success: 0
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -951,7 +1200,9 @@ test_invalid_parameters(hid_t file)
size_t buf_size = CHUNK_NX*CHUNK_NY*sizeof(int);
int aggression = 9; /* Compression aggression setting */
- TESTING("invalid parameters for H5DOwrite_chunk");
+ hsize_t chunk_nbytes; /* Chunk size */
+
+ TESTING("invalid parameters for H5DOwrite_chunk/H5DOread_chunk");
/*
* Create the data space with unlimited dimensions.
@@ -969,7 +1220,7 @@ test_invalid_parameters(hid_t file)
goto error;
/*
- * Create a new contiguous dataset to verify H5DOwrite_chunk doesn't work
+ * Create a new contiguous dataset to verify H5DOwrite_chunk/H5DOread_chunk doesn't work
*/
if((dataset = H5Dcreate2(file, DATASETNAME5, H5T_NATIVE_INT, dataspace, H5P_DEFAULT,
cparms, H5P_DEFAULT)) < 0)
@@ -981,8 +1232,8 @@ test_invalid_parameters(hid_t file)
/* Initialize data for one chunk */
for(i = n = 0; i < CHUNK_NX; i++)
for(j = 0; j < CHUNK_NY; j++) {
- direct_buf[i][j] = n++;
- }
+ direct_buf[i][j] = n++;
+ }
/* Try to write the chunk data to contiguous dataset. It should fail */
offset[0] = CHUNK_NX;
@@ -993,6 +1244,18 @@ test_invalid_parameters(hid_t file)
goto error;
} H5E_END_TRY;
+ /* Try to get chunk size for a contiguous dataset. It should fail */
+ H5E_BEGIN_TRY {
+ if((status = H5Dget_chunk_storage_size(dataset, offset, &chunk_nbytes)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Try to H5DOread_chunk from the contiguous dataset. It should fail */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
if(H5Dclose(dataset) < 0)
goto error;
@@ -1011,25 +1274,40 @@ test_invalid_parameters(hid_t file)
cparms, H5P_DEFAULT)) < 0)
goto error;
- /* Check invalid dataset ID */
+ /* Check invalid dataset ID for H5DOwrite_chunk and H5DOread_chunk */
H5E_BEGIN_TRY {
if((status = H5DOwrite_chunk((hid_t)-1, dxpl, filter_mask, offset, buf_size, direct_buf)) != FAIL)
goto error;
} H5E_END_TRY;
- /* Check invalid DXPL ID */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk((hid_t)-1, dxpl, offset, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Check invalid DXPL ID for H5DOwrite_chunk and H5DOread_chunk */
H5E_BEGIN_TRY {
if((status = H5DOwrite_chunk(dataset, (hid_t)-1, filter_mask, offset, buf_size, direct_buf)) != FAIL)
goto error;
} H5E_END_TRY;
- /* Check invalid OFFSET */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, (hid_t)-1, offset, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Check invalid OFFSET for H5DOwrite_chunk and H5DOread_chunk */
H5E_BEGIN_TRY {
if((status = H5DOwrite_chunk(dataset, dxpl, filter_mask, NULL, buf_size, direct_buf)) != FAIL)
goto error;
} H5E_END_TRY;
- /* Check when OFFSET is out of dataset range */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, dxpl, NULL, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Check when OFFSET is out of dataset range for H5DOwrite_chunk and H5DOread_chunk */
offset[0] = NX + 1;
offset[1] = NY;
H5E_BEGIN_TRY {
@@ -1037,7 +1315,12 @@ test_invalid_parameters(hid_t file)
goto error;
} H5E_END_TRY;
- /* Check when OFFSET is not on chunk boundary */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Check when OFFSET is not on chunk boundary for H5DOwrite_chunk and H5DOread_chunk */
offset[0] = CHUNK_NX;
offset[1] = CHUNK_NY + 1;
H5E_BEGIN_TRY {
@@ -1045,7 +1328,12 @@ test_invalid_parameters(hid_t file)
goto error;
} H5E_END_TRY;
- /* Check invalid buffer size */
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &filter_mask, direct_buf)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
+ /* Check invalid buffer size for H5DOwrite_chunk only */
offset[0] = CHUNK_NX;
offset[1] = CHUNK_NY;
buf_size = 0;
@@ -1054,13 +1342,18 @@ test_invalid_parameters(hid_t file)
goto error;
} H5E_END_TRY;
- /* Check invalid data buffer */
+ /* Check invalid data buffer for H5DOwrite_chunk and H5DOread_chunk */
buf_size = CHUNK_NX*CHUNK_NY*sizeof(int);
H5E_BEGIN_TRY {
if((status = H5DOwrite_chunk(dataset, dxpl, filter_mask, offset, buf_size, NULL)) != FAIL)
goto error;
} H5E_END_TRY;
+ H5E_BEGIN_TRY {
+ if((status = H5DOread_chunk(dataset, dxpl, offset, &filter_mask, NULL)) != FAIL)
+ goto error;
+ } H5E_END_TRY;
+
if(H5Dclose(dataset) < 0)
goto error;
@@ -1090,11 +1383,11 @@ error:
/*-------------------------------------------------------------------------
* Function: Main function
*
- * Purpose: Test direct chunk write function H5DOwrite_chunk
- *
- * Return: Success: 0
+ * Purpose: Test direct chunk write function H5DOwrite_chunk and
+ * chunk direct read function H5DOread_chunk
*
- * Failure: 1
+ * Return: Success: 0
+ * Failure: 1
*
* Programmer: Raymond Lu
* 30 November 2012
@@ -1112,7 +1405,7 @@ int main( void )
if((file_id = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto error;
- /* Test direct chunk write */
+ /* Test direct chunk write and direct chunk read */
#ifdef H5_HAVE_FILTER_DEFLATE
nerrors += test_direct_chunk_write(file_id);
#endif /* H5_HAVE_FILTER_DEFLATE */
'n1548' href='#n1548'>1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer: Quincey Koziol
 *	       Thursday, March 23, 2000
 *
 * Purpose: Manage priority queues of free-lists (of blocks of bytes).
 *      These are used in various places in the library which allocate and
 *      free differently blocks of bytes repeatedly.  Usually the same size
 *      of block is allocated and freed repeatedly in a loop, while writing out
 *      chunked data for example, but the blocks may also be of different sizes
 *      from different datasets and an attempt is made to optimize access to
 *      the proper free list of blocks by using these priority queues to
 *      move frequently accessed free lists to the head of the queue.
 */

#include "H5FLmodule.h" /* This source code file is part of the H5FL module */

/* #define H5FL_DEBUG */

#include "H5private.h"   /* Generic Functions			*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5FLprivate.h" /* Free Lists                           */
#include "H5MMprivate.h" /* Memory management			*/

/*
 * Private type definitions
 */

/*
    Default limits on how much memory can accumulate on each free list before
    it is garbage collected.
 */
static size_t H5FL_reg_glb_mem_lim = 1 * 1024 * 1024;  /* Default to 1MB limit on all regular free lists */
static size_t H5FL_reg_lst_mem_lim = 1 * 65536;        /* Default to 64KB limit on each regular free list */
static size_t H5FL_arr_glb_mem_lim = 4 * 1024 * 1024;  /* Default to 4MB limit on all array free lists */
static size_t H5FL_arr_lst_mem_lim = 4 * 65536;        /* Default to 256KB limit on each array free list */
static size_t H5FL_blk_glb_mem_lim = 16 * 1024 * 1024; /* Default to 16MB limit on all block free lists */
static size_t H5FL_blk_lst_mem_lim = 1024 * 1024; /* Default to 1024KB (1MB) limit on each block free list */
static size_t H5FL_fac_glb_mem_lim = 16 * 1024 * 1024; /* Default to 16MB limit on all factory free lists */
static size_t H5FL_fac_lst_mem_lim =
    1024 * 1024; /* Default to 1024KB (1MB) limit on each factory free list */

/* A garbage collection node for regular free lists */
typedef struct H5FL_reg_gc_node_t {
    H5FL_reg_head_t           *list; /* Pointer to the head of the list to garbage collect */
    struct H5FL_reg_gc_node_t *next; /* Pointer to the next node in the list of things to garbage collect */
} H5FL_reg_gc_node_t;

/* The garbage collection head for regular free lists */
typedef struct H5FL_reg_gc_list_t {
    size_t                     mem_freed; /* Amount of free memory on list */
    struct H5FL_reg_gc_node_t *first; /* Pointer to the first node in the list of things to garbage collect */
} H5FL_reg_gc_list_t;

/* The head of the list of things to garbage collect */
static H5FL_reg_gc_list_t H5FL_reg_gc_head = {0, NULL};

/* A garbage collection node for array free lists */
typedef struct H5FL_gc_arr_node_t {
    H5FL_arr_head_t           *list; /* Pointer to the head of the list to garbage collect */
    struct H5FL_gc_arr_node_t *next; /* Pointer to the next node in the list of things to garbage collect */
} H5FL_gc_arr_node_t;

/* The garbage collection head for array free lists */
typedef struct H5FL_gc_arr_list_t {
    size_t                     mem_freed; /* Amount of free memory on list */
    struct H5FL_gc_arr_node_t *first; /* Pointer to the first node in the list of things to garbage collect */
} H5FL_gc_arr_list_t;

/* The head of the list of array things to garbage collect */
static H5FL_gc_arr_list_t H5FL_arr_gc_head = {0, NULL};

/* A garbage collection node for blocks */
typedef struct H5FL_blk_gc_node_t {
    H5FL_blk_head_t           *pq;   /* Pointer to the head of the PQ to garbage collect */
    struct H5FL_blk_gc_node_t *next; /* Pointer to the next node in the list of things to garbage collect */
} H5FL_blk_gc_node_t;

/* The garbage collection head for blocks */
typedef struct H5FL_blk_gc_list_t {
    size_t                     mem_freed; /* Amount of free memory on list */
    struct H5FL_blk_gc_node_t *first; /* Pointer to the first node in the list of things to garbage collect */
} H5FL_blk_gc_list_t;

/* The head of the list of PQs to garbage collect */
static H5FL_blk_gc_list_t H5FL_blk_gc_head = {0, NULL};

/* A garbage collection node for factory free lists */
struct H5FL_fac_gc_node_t {
    H5FL_fac_head_t           *list; /* Pointer to the head of the list to garbage collect */
    struct H5FL_fac_gc_node_t *next; /* Pointer to the next node in the list of things to garbage collect */
};

/* The garbage collection head for factory free lists */
typedef struct H5FL_fac_gc_list_t {
    size_t                     mem_freed; /* Amount of free memory on list */
    struct H5FL_fac_gc_node_t *first; /* Pointer to the first node in the list of things to garbage collect */
} H5FL_fac_gc_list_t;

/* Data structure to store each block in factory free list */
struct H5FL_fac_node_t {
    struct H5FL_fac_node_t *next; /* Pointer to next block in free list */
};

/* The head of the list of factory things to garbage collect */
static H5FL_fac_gc_list_t H5FL_fac_gc_head = {0, NULL};

#ifdef H5FL_TRACK

/* Extra headers needed */
#include "H5CSprivate.h" /* Function stack			*/

/* Head of "outstanding allocations" list */
static H5FL_track_t *H5FL_out_head_g = NULL;
#endif /* H5FL_TRACK */

/* Forward declarations of local static functions */
static void            *H5FL__malloc(size_t mem_size);
static herr_t           H5FL__reg_init(H5FL_reg_head_t *head);
static herr_t           H5FL__reg_gc(void);
static herr_t           H5FL__reg_gc_list(H5FL_reg_head_t *head);
static int              H5FL__reg_term(void);
static H5FL_blk_node_t *H5FL__blk_find_list(H5FL_blk_node_t **head, size_t size);
static H5FL_blk_node_t *H5FL__blk_create_list(H5FL_blk_node_t **head, size_t size);
static herr_t           H5FL__blk_init(H5FL_blk_head_t *head);
static herr_t           H5FL__blk_gc_list(H5FL_blk_head_t *head);
static herr_t           H5FL__blk_gc(void);
static int              H5FL__blk_term(void);
static herr_t           H5FL__arr_init(H5FL_arr_head_t *head);
static herr_t           H5FL__arr_gc_list(H5FL_arr_head_t *head);
static herr_t           H5FL__arr_gc(void);
static int              H5FL__arr_term(void);
static herr_t           H5FL__fac_gc_list(H5FL_fac_head_t *head);
static herr_t           H5FL__fac_gc(void);
static int              H5FL__fac_term_all(void);

/* Declare a free list to manage the H5FL_blk_node_t struct */
H5FL_DEFINE(H5FL_blk_node_t);

/* Declare a free list to manage the H5FL_fac_gc_node_t struct */
H5FL_DEFINE(H5FL_fac_gc_node_t);

/* Declare a free list to manage the H5FL_fac_head_t struct */
H5FL_DEFINE(H5FL_fac_head_t);

/*--------------------------------------------------------------------------
 NAME
    H5FL_term_package
 PURPOSE
    Terminate various H5FL objects
 USAGE
    void H5FL_term_package()
 RETURNS
    Success:	Positive if any action might have caused a change in some
                other interface; zero otherwise.
        Failure:	Negative
 DESCRIPTION
    Release any resources allocated.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
int
H5FL_term_package(void)
{
    int n = 0;

    FUNC_ENTER_NOAPI_NOINIT_NOERR

        /* Garbage collect any nodes on the free lists */
        (void)
    H5FL_garbage_coll();

    /* Shut down the various kinds of free lists */
    n += H5FL__reg_term();
    n += H5FL__fac_term_all();
    n += H5FL__arr_term();
    n += H5FL__blk_term();

#ifdef H5FL_TRACK
    /* If we haven't freed all the allocated memory, dump out the list now */
    if (n > 0 && H5FL_out_head_g) {
        H5FL_track_t *trk = H5FL_out_head_g;

        /* Dump information about all the outstanding allocations */
        while (trk != NULL) {
            /* Print information about the outstanding block */
            HDfprintf(stderr, "%s: Outstanding allocation:\n", __func__);
            HDfprintf(stderr, "\tPtr: %p, File: %s, Function: %s, Line: %d\n",
                      (((unsigned char *)trk) + sizeof(H5FL_track_t)), trk->file, trk->func, trk->line);
            H5CS_print_stack(trk->stack, stderr);

            /* Advance to next node */
            trk = trk->next;
        } /* end while */
    }     /* end if */
#endif    /* H5FL_TRACK */

    FUNC_LEAVE_NOAPI(n)
} /* end H5FL_term_package() */

/*-------------------------------------------------------------------------
 * Function:	H5FL__malloc
 *
 * Purpose:	Attempt to allocate space using malloc.  If malloc fails, garbage
 *      collect and try again.  If malloc fails again, then return NULL.
 *
 * Return:	Success:	non-NULL
 * 		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, August 1, 2000
 *
 *-------------------------------------------------------------------------
 */
static void *
H5FL__malloc(size_t mem_size)
{
    void *ret_value = NULL; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Attempt to allocate the memory requested */
    if (NULL == (ret_value = H5MM_malloc(mem_size))) {
        /* If we can't allocate the memory now, try garbage collecting first */
        if (H5FL_garbage_coll() < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGC, NULL, "garbage collection failed during allocation")

        /* Now try allocating the memory again */
        if (NULL == (ret_value = H5MM_malloc(mem_size)))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for chunk")
    } /* end if */

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

/*-------------------------------------------------------------------------
 * Function:	H5FL__reg_init
 *
 * Purpose:	Initialize a free list for a certain type.  Right now, this just
 *      adds the free list to the list of things to garbage collect.
 *
 * Return:	Success:	Non-negative
 * 		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *              Friday, March 24, 2000
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FL__reg_init(H5FL_reg_head_t *head)
{
    H5FL_reg_gc_node_t *new_node;            /* Pointer to the node for the new list to garbage collect */
    herr_t              ret_value = SUCCEED; /* return value*/

    FUNC_ENTER_PACKAGE

    /* Allocate a new garbage collection node */
    if (NULL == (new_node = (H5FL_reg_gc_node_t *)H5MM_malloc(sizeof(H5FL_reg_gc_node_t))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")

    /* Initialize the new garbage collection node */
    new_node->list = head;

    /* Link in to the garbage collection list */
    new_node->next         = H5FL_reg_gc_head.first;
    H5FL_reg_gc_head.first = new_node;

    /* Indicate that the free list is initialized */
    head->init = TRUE;

    /* Make certain that the space allocated is large enough to store a free list pointer (eventually) */
    if (head->size < sizeof(H5FL_reg_node_t))
        head->size = sizeof(H5FL_reg_node_t);

        /* Make certain there's room for tracking information, if any */
#ifdef H5FL_TRACK
    head->size += sizeof(H5FL_track_t);
#endif /* H5FL_TRACK */

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

/*-------------------------------------------------------------------------
 * Function:	H5FL_reg_free
 *
 * Purpose:	Release an object & put on free list
 *
 * Return:	Always returns NULL
 *
 * Programmer:	Quincey Koziol
 *              Friday, March 24, 2000
 *
 *-------------------------------------------------------------------------
 */
void *
H5FL_reg_free(H5FL_reg_head_t *head, void *obj)
{
    void *ret_value = NULL; /* Return value */

    /* NOINIT OK here because this must be called after H5FL_reg_malloc/calloc
     * -NAF */
    FUNC_ENTER_NOAPI_NOINIT

    /* Double check parameters */
    HDassert(head);
    HDassert(obj);

#ifdef H5FL_TRACK
    {
        H5FL_track_t *trk = obj = ((unsigned char *)obj) - sizeof(H5FL_track_t);

        /* Free tracking information about the allocation location */
        H5CS_close_stack(trk->stack);
        /* The 'func' & 'file' strings are statically allocated (by the compiler)
         * and are not allocated, so there's no need to free them.
         */
        trk->file = NULL;
        trk->func = NULL;

        /* Remove from "outstanding allocations" list */
        if (trk == H5FL_out_head_g) {
            H5FL_out_head_g = H5FL_out_head_g->next;
            if (H5FL_out_head_g)
                H5FL_out_head_g->prev = NULL;
        } /* end if */
        else {
            trk->prev->next = trk->next;
            if (trk->next)
                trk->next->prev = trk->prev;
        } /* end else */
    }
#endif /* H5FL_TRACK */

#ifdef H5FL_DEBUG
    HDmemset(obj, 255, head->size);
#endif /* H5FL_DEBUG */

    /* Make certain that the free list is initialized */
    HDassert(head->init);

    /* Link into the free list */
    ((H5FL_reg_node_t *)obj)->next = head->list;

    /* Point free list at the node freed */
    head->list = (H5FL_reg_node_t *)obj;

    /* Increment the number of blocks on free list */
    head->onlist++;

    /* Increment the amount of "regular" freed memory globally */
    H5FL_reg_gc_head.mem_freed += head->size;

    /* Check for exceeding free list memory use limits */
    /* First check this particular list */
    if (head->onlist * head->size > H5FL_reg_lst_mem_lim)
        if (H5FL__reg_gc_list(head) < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGC, NULL, "garbage collection failed during free")

    /* Then check the global amount memory on regular free lists */
    if (H5FL_reg_gc_head.mem_freed > H5FL_reg_glb_mem_lim)
        if (H5FL__reg_gc() < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGC, NULL, "garbage collection failed during free")

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

/*-------------------------------------------------------------------------
 * Function:	H5FL_reg_malloc
 *
 * Purpose:	Allocate a block on a free list
 *
 * Return:	Success:	Pointer to a valid object
 * 		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *              Friday, March 24, 2000
 *
 *-------------------------------------------------------------------------
 */
void *
H5FL_reg_malloc(H5FL_reg_head_t *head H5FL_TRACK_PARAMS)
{
    void *ret_value = NULL; /* Pointer to object to return */

    FUNC_ENTER_NOAPI(NULL)

    /* Double check parameters */
    HDassert(head);

    /* Make certain the list is initialized first */
    if (!head->init)
        if (H5FL__reg_init(head) < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, NULL, "can't initialize 'regular' blocks")

    /* Check for nodes available on the free list first */
    if (head->list != NULL) {
        /* Get a pointer to the block on the free list */
        ret_value = (void *)(head->list);

        /* Remove node from free list */
        head->list = head->list->next;

        /* Decrement the number of blocks & memory on free list */
        head->onlist--;

        /* Decrement the amount of global "regular" free list memory in use */
        H5FL_reg_gc_head.mem_freed -= (head->size);
    } /* end if */
    /* Otherwise allocate a node */
    else {
        if (NULL == (ret_value = H5FL__malloc(head->size)))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

        /* Increment the number of blocks allocated in list */
        head->allocated++;
    } /* end else */

#ifdef H5FL_TRACK
    /* Copy allocation location information */
    ((H5FL_track_t *)ret_value)->stack = H5CS_copy_stack();
    HDassert(((H5FL_track_t *)ret_value)->stack);
    /* The 'call_func' & 'call_file' strings are statically allocated (by the compiler)
     * there's no need to duplicate them.
     */
    ((H5FL_track_t *)ret_value)->file = call_file;
    ((H5FL_track_t *)ret_value)->func = call_func;
    ((H5FL_track_t *)ret_value)->line = call_line;

    /* Add to "outstanding allocations" list */
    ((H5FL_track_t *)ret_value)->prev = NULL;
    ((H5FL_track_t *)ret_value)->next = H5FL_out_head_g;
    if (H5FL_out_head_g)
        H5FL_out_head_g->prev = (H5FL_track_t *)ret_value;
    H5FL_out_head_g = (H5FL_track_t *)ret_value;

    /* Adjust for allocation tracking information */
    ret_value = ((unsigned char *)ret_value) + sizeof(H5FL_track_t);
#endif /* H5FL_TRACK */

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

/*-------------------------------------------------------------------------
 * Function:	H5FL_reg_calloc
 *
 * Purpose:	Allocate a block on a free list and clear it to zeros
 *
 * Return:	Success:	Pointer to a valid object
 * 		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *              Monday, December 23, 2002
 *
 *-------------------------------------------------------------------------
 */
void *
H5FL_reg_calloc(H5FL_reg_head_t *head H5FL_TRACK_PARAMS)
{
    void *ret_value = NULL; /* Pointer to object to return */

    FUNC_ENTER_NOAPI(NULL)

    /* Double check parameters */
    HDassert(head);

    /* Allocate the block */
    if (NULL == (ret_value = H5FL_reg_malloc(head H5FL_TRACK_INFO_INT)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Clear to zeros */
    /* (Accommodate tracking information, if present) */
    HDmemset(ret_value, 0, head->size - H5FL_TRACK_SIZE);

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

/*-------------------------------------------------------------------------
 * Function:	H5FL__reg_gc_list
 *
 * Purpose:	Garbage collect on a particular object free list
 *
 * Return:	Success:	Non-negative
 * 		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *              Tuesday, July 25, 2000
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FL__reg_gc_list(H5FL_reg_head_t *head)
{
    H5FL_reg_node_t *free_list; /* Pointer to nodes in free list being garbage collected */

    FUNC_ENTER_PACKAGE_NOERR

    /* For each free list being garbage collected, walk through the nodes and free them */
    free_list = head->list;
    while (free_list != NULL) {
        H5FL_reg_node_t *tmp; /* Temporary node pointer */

        /* Get the pointer to the next node */
        tmp = free_list->next;

        /* Free the block */
        H5MM_free(free_list);

        /* Advance to the next node */
        free_list = tmp;
    } /* end while */

    /* Decrement the count of nodes allocated and free the node */
    head->allocated -= head->onlist;

    /* Decrement global count of free memory on "regular" lists */
    H5FL_reg_gc_head.mem_freed -= (head->onlist * head->size);

    /* Indicate no free nodes on the free list */
    head->list   = NULL;
    head->onlist = 0;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5FL__reg_gc_list() */

/*-------------------------------------------------------------------------
 * Function:	H5FL__reg_gc
 *
 * Purpose:	Garbage collect on all the object free lists
 *
 * Return:	Success:	Non-negative
 * 		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *              Friday, March 24, 2000
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FL__reg_gc(void)
{
    H5FL_reg_gc_node_t *gc_node;             /* Pointer into the list of things to garbage collect */
    herr_t              ret_value = SUCCEED; /* return value*/

    FUNC_ENTER_PACKAGE

    /* Walk through all the free lists, free()'ing the nodes */
    gc_node = H5FL_reg_gc_head.first;
    while (gc_node != NULL) {
        /* Release the free nodes on the list */
        if (H5FL__reg_gc_list(gc_node->list) < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTGC, FAIL, "garbage collection of list failed")

        /* Go on to the next free list to garbage collect */
        gc_node = gc_node->next;
    } /* end while */

    /* Double check that all the memory on the free lists is recycled */
    HDassert(H5FL_reg_gc_head.mem_freed == 0);

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

/*--------------------------------------------------------------------------
 NAME
    H5FL_reg_term
 PURPOSE
    Terminate various H5FL object free lists
 USAGE
    int H5FL_reg_term()
 RETURNS
    Success:	Positive if any action might have caused a change in some
                other interface; zero otherwise.
        Failure:	Negative
 DESCRIPTION
    Release any resources allocated.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
        Robb Matzke, 2000-04-25
        If a list cannot be freed because something is using it then return
        zero (failure to free a list doesn't affect any other part of the
        library). If some other layer frees something during its termination
        it will return non-zero, which will cause this function to get called
        again to reclaim this layer's memory.
--------------------------------------------------------------------------*/
static int
H5FL__reg_term(void)
{
    H5FL_reg_gc_node_t *left; /* pointer to garbage collection lists with work left */

    FUNC_ENTER_PACKAGE_NOERR

    /* Free the nodes on the garbage collection list, keeping nodes with allocations outstanding */
    left = NULL;
    while (H5FL_reg_gc_head.first != NULL) {
        H5FL_reg_gc_node_t *tmp; /* Temporary pointer to a garbage collection node */

        /* Get a copy of the next node */
        tmp = H5FL_reg_gc_head.first->next;

#ifdef H5FL_DEBUG
        HDprintf("%s: head->name = %s, head->allocated = %d\n", __func__, H5FL_reg_gc_head.first->list->name,
                 (int)H5FL_reg_gc_head.first->list->allocated);
#endif /* H5FL_DEBUG */
        /* Check if the list has allocations outstanding */
        if (H5FL_reg_gc_head.first->list->allocated > 0) {
            /* Add free list to the list of nodes with allocations open still */
            H5FL_reg_gc_head.first->next = left;
            left                         = H5FL_reg_gc_head.first;
        } /* end if */
        /* No allocations left open for list, get rid of it */
        else {
            /* Reset the "initialized" flag, in case we restart this list somehow (I don't know how..) */
            H5FL_reg_gc_head.first->list->init = FALSE;

            /* Free the node from the garbage collection list */
            H5MM_xfree(H5FL_reg_gc_head.first);
        } /* end else */

        H5FL_reg_gc_head.first = tmp;
    } /* end while */

    /* Point to the list of nodes left with allocations open, if any */
    H5FL_reg_gc_head.first = left;

    FUNC_LEAVE_NOAPI(H5FL_reg_gc_head.first != NULL ? 1 : 0)
} /* end H5FL__reg_term() */

/*-------------------------------------------------------------------------
 * Function:	H5FL__blk_find_list
 *
 * Purpose:	Finds the free list for blocks of a given size.  Also moves that
 *      free list node to the head of the priority queue (if it isn't there
 *      already).  This routine does not manage the actual free list, it just
 *      works with the priority queue.
 *
 * Return:	Success:	valid pointer to the free list node
 *
 *		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *		Thursday, March  23, 2000
 *
 *-------------------------------------------------------------------------
 */
static H5FL_blk_node_t *
H5FL__blk_find_list(H5FL_blk_node_t **head, size_t size)
{
    H5FL_blk_node_t *temp = NULL; /* Temp. pointer to node in the native list */

    FUNC_ENTER_PACKAGE_NOERR

    /* Find the correct free list */
    temp = *head;

    /* Check if the node is at the head of the list */
    if (temp && temp->size != size) {
        temp = temp->next;

        while (temp != NULL) {
            /* Check if we found the correct node */
            if (temp->size == size) {
                /* Take the node found out of it's current position */
                if (temp->next == NULL) {
                    temp->prev->next = NULL;
                } /* end if */
                else {
                    temp->prev->next = temp->next;
                    temp->next->prev = temp->prev;
                } /* end else */

                /* Move the found node to the head of the list */
                temp->prev    = NULL;
                temp->next    = *head;
                (*head)->prev = temp;
                *head         = temp;

                /* Get out */
                break;
            } /* end if */