diff options
-rw-r--r-- | src/H5T.c | 36 | ||||
-rw-r--r-- | src/H5Tcompound.c | 3 | ||||
-rw-r--r-- | src/H5Tpkg.h | 1 | ||||
-rw-r--r-- | test/dtypes.c | 72 |
4 files changed, 103 insertions, 9 deletions
@@ -1453,8 +1453,8 @@ H5Tcreate(H5T_class_t type, size_t size) FUNC_ENTER_API(H5Tcreate, FAIL); H5TRACE2("i","Ttz",type,size); - /* check args */ - if (size == 0) + /* check args. We start to support size adjustment for compound type. */ + if (size == 0 && type != H5T_COMPOUND) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid size"); /* create the type */ @@ -2796,8 +2796,6 @@ H5T_create(H5T_class_t type, size_t size) FUNC_ENTER_NOAPI(H5T_create, NULL); - assert(size != 0); - switch (type) { case H5T_INTEGER: case H5T_FLOAT: @@ -3507,7 +3505,6 @@ H5T_set_size(H5T_t *dt, size_t size) assert(size!=0); assert(H5T_REFERENCE!=dt->shared->type); assert(!(H5T_ENUM==dt->shared->type && 0==dt->shared->u.enumer.nmembs)); - assert(!(H5T_COMPOUND==dt->shared->type && 0==dt->shared->u.compnd.nmembs)); if (dt->shared->parent) { if (H5T_set_size(dt->shared->parent, size)<0) @@ -3544,8 +3541,33 @@ H5T_set_size(H5T_t *dt, size_t size) break; case H5T_COMPOUND: - if(size<dt->shared->size) - HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "can't shrink compound datatype"); + /* If decreasing size, check the last member isn't being cut. */ + if(size<dt->shared->size) { + int num_membs; + unsigned i, max_index=0; + H5T_t *memb_type; + size_t memb_offset, max_offset=0; + size_t max_size; + + if((num_membs = H5T_get_nmembers(dt))<0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to get number of members"); + + for(i=0; i<num_membs; i++) { + memb_offset = H5T_get_member_offset(dt, i); + if(memb_offset > max_offset) { + max_offset = memb_offset; + max_index = i; + } + } + + if((memb_type = H5T_get_member_type(dt, max_index))==NULL) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to get type of member"); + + max_size = H5T_get_size(memb_type); + + if(size<(max_offset+max_size)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "size shrinking will cut off last member "); + } break; case H5T_STRING: diff --git a/src/H5Tcompound.c b/src/H5Tcompound.c index 7abcafc..cbb269c 100644 --- a/src/H5Tcompound.c +++ b/src/H5Tcompound.c @@ -36,7 +36,6 @@ #define H5T_COMPND_INC 64 /*typical max numb of members per struct */ /* Static local functions */ -static size_t H5T_get_member_offset(const H5T_t *dt, unsigned membno); static herr_t H5T_pack(H5T_t *dt); @@ -134,7 +133,7 @@ done: * *------------------------------------------------------------------------- */ -static size_t +size_t H5T_get_member_offset(const H5T_t *dt, unsigned membno) { size_t ret_value; diff --git a/src/H5Tpkg.h b/src/H5Tpkg.h index 7e7203a..8999d6e 100644 --- a/src/H5Tpkg.h +++ b/src/H5Tpkg.h @@ -1090,6 +1090,7 @@ H5_DLL H5T_t * H5T_array_create(H5T_t *base, int ndims, /* Compound functions */ H5_DLL H5T_t *H5T_get_member_type(const H5T_t *dt, unsigned membno); +H5_DLL size_t H5T_get_member_offset(const H5T_t *dt, unsigned membno); H5_DLL htri_t H5T_is_packed(const H5T_t *dt); #endif diff --git a/test/dtypes.c b/test/dtypes.c index 178ae40..e07e200 100644 --- a/test/dtypes.c +++ b/test/dtypes.c @@ -2070,6 +2070,77 @@ test_compound_11(void) /*------------------------------------------------------------------------- + * Function: test_compound_12 + * + * Purpose: Tests size adjustment of compound data types. Start with + * no member and 0 size, increase the size as inserting + * members. + * + * Return: Success: 0 + * + * Failure: number of errors + * + * Programmer: Raymond Lu + * Wednesday, September 29, 2004 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +static int +test_compound_12(void) +{ + complex_t tmp; + hid_t complex_id; + size_t size = 0; + size_t offset, new_size; + herr_t ret; + + TESTING("adjust size of compound data types"); + + /* Create the empty compound type */ + if ((complex_id = H5Tcreate(H5T_COMPOUND, 0))<0) goto error; + + /* Add a couple fields and adjust the size */ + offset = size; + if((size+=H5Tget_size(H5T_NATIVE_DOUBLE))<0) goto error; + if (H5Tset_size(complex_id, size)<0) goto error; + if (H5Tinsert(complex_id, "real", offset, + H5T_NATIVE_DOUBLE)<0) goto error; + + offset = size; + if((size+=H5Tget_size(H5T_NATIVE_DOUBLE))<0) goto error; + if (H5Tset_size(complex_id, size)<0) goto error; + if (H5Tinsert(complex_id, "imaginary", offset, + H5T_NATIVE_DOUBLE)<0) goto error; + + /* Increase and decrease the size. */ + if((size+=H5Tget_size(H5T_NATIVE_DOUBLE))<0) goto error; + if (H5Tset_size(complex_id, size)<0) goto error; + + if((size-=H5Tget_size(H5T_NATIVE_DOUBLE))<0) goto error; + if (H5Tset_size(complex_id, size)<0) goto error; + + /* Verify the size */ + if((new_size=H5Tget_size(complex_id))<0) goto error; + if(new_size!=size) goto error; + + /* Tries to cut last member. Supposed to fail. */ + size--; + H5E_BEGIN_TRY { + H5Tset_size(complex_id, size); + } H5E_END_TRY; + + if (H5Tclose (complex_id)<0) goto error; + PASSED(); + return 0; + + error: + return 1; +} + + +/*------------------------------------------------------------------------- * Function: test_encode * * Purpose: Tests functions of encoding and decoding data type. @@ -6602,6 +6673,7 @@ main(void) nerrors += test_compound_9(); nerrors += test_compound_10(); nerrors += test_compound_11(); + nerrors += test_compound_12(); nerrors += test_conv_int (); nerrors += test_conv_enum_1(); nerrors += test_conv_enum_2(); |