From 0b4b91cb559bdb9268e8b3d5b7efea4d85b6a7d7 Mon Sep 17 00:00:00 2001 From: Quincey Koziol Date: Thu, 7 Nov 2002 12:48:12 -0500 Subject: [svn-r6063] Purpose: Code cleanup Description: Cleaned up some error values that were incorrect, added some assertions and refactored the integer & floating-point native conversions some. Platforms tested: FreeBSD 4.7 (sleipnir), too small for h5committest --- src/H5D.c | 12 +- src/H5T.c | 631 +++++++++++++++++++++++++++++++++----------------------------- 2 files changed, 347 insertions(+), 296 deletions(-) diff --git a/src/H5D.c b/src/H5D.c index 89656a8..3b236f9 100644 --- a/src/H5D.c +++ b/src/H5D.c @@ -3695,7 +3695,7 @@ done: * * Return: Success: the address of dataset * - * Failure: Zero + * Failure: HADDR_UNDEF * * Programmer: Raymond Lu * November 6, 2002 @@ -3710,12 +3710,12 @@ H5Dget_offset(hid_t dset_id) H5D_t *dset=NULL; haddr_t ret_value; /* Return value */ - FUNC_ENTER_API(H5Dget_offset, 0); + FUNC_ENTER_API(H5Dget_offset, HADDR_UNDEF); H5TRACE1("h","i",dset_id); /* Check args */ if (NULL==(dset=H5I_object_verify(dset_id, H5I_DATASET))) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, 0, "not a dataset"); + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, HADDR_UNDEF, "not a dataset"); /* Set return value */ ret_value = H5D_get_offset(dset); @@ -3733,7 +3733,7 @@ done: * * Return: Success: the address of dataset * - * Failure: Zero + * Failure: HADDR_UNDEF * * Programmer: Raymond Lu * November 6, 2002 @@ -3749,6 +3749,8 @@ H5D_get_offset(H5D_t *dset) FUNC_ENTER_NOAPI(H5D_get_offset, HADDR_UNDEF); + assert(dset); + switch(dset->layout.type) { case H5D_CHUNKED: case H5D_COMPACT: @@ -3762,7 +3764,7 @@ H5D_get_offset(H5D_t *dset) break; default: - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, HADDR_UNDEF, "not a dataset type"); + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, HADDR_UNDEF, "unknown dataset layout type"); } done: diff --git a/src/H5T.c b/src/H5T.c index 427b86e..080ac09 100644 --- a/src/H5T.c +++ b/src/H5T.c @@ -2585,7 +2585,7 @@ done: htri_t H5Tis_variable_str(hid_t dtype_id) { - H5T_t *dt = NULL; + H5T_t *dt; /* Datatype to query */ htri_t ret_value; /* Return value */ FUNC_ENTER_API(H5Tis_variable_str, FAIL); @@ -2630,8 +2630,6 @@ H5T_is_variable_str(H5T_t *dt) if(H5T_VLEN == dt->type && H5T_VLEN_STRING == dt->u.vlen.type) ret_value = TRUE; - else - ret_value = FALSE; done: FUNC_LEAVE(ret_value); @@ -2672,10 +2670,10 @@ done: */ hid_t H5Tget_native_type(hid_t type_id, H5T_direction_t direction) { - H5T_t *dt=NULL, *new_dt=NULL; - H5T_class_t class; - size_t comp_size=0; - hid_t ret_value; + H5T_t *dt; /* Datatype to create native datatype from */ + H5T_t *new_dt=NULL; /* Datatype for native datatype created */ + size_t comp_size=0; /* Compound datatype's size */ + hid_t ret_value; /* Return value */ FUNC_ENTER_API(H5Tget_native_type, FAIL); H5TRACE1("z","i",type_id); @@ -2695,6 +2693,12 @@ hid_t H5Tget_native_type(hid_t type_id, H5T_direction_t direction) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register data type"); done: + /* Error cleanup */ + if(ret_value<0) { + if(new_dt) + H5T_close(new_dt); + } /* end if */ + FUNC_LEAVE(ret_value); } @@ -2717,24 +2721,15 @@ done: */ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *struct_align, size_t *offset, size_t *comp_size) { - H5T_t *ret_value, *dt; - H5T_class_t class; - H5T_sign_t sign; - size_t size; - int nmemb; - int i; - H5T_t *memb_type, *base_type, *super_type, *new_type; - hid_t memb_id, base_id; - H5T_t **memb_list; /* List of compound member IDs */ - size_t *memb_offset=NULL; /* List of member offsets in compound type, including member size and alignment */ - size_t children_size=0; /* Total size of compound members */ - size_t children_st_align=0; /* The max alignment among compound members. This'll be the compound alignment */ - char **comp_mname; /* List of member names in compound type */ - char *memb_name; - void *memb_value; - int array_rank; - hsize_t *dims = NULL; - H5T_class_t child_class; + H5T_t *dt; /* Datatype to make native */ + H5T_class_t class; /* Class of datatype to make native */ + size_t size; /* Size of datatype to make native */ + int nmemb; /* Number of members in compound & enum types */ + H5T_t *super_type; /* Super type of VL, array and enum datatypes */ + H5T_t *nat_super_type; /* Native form of VL, array & enum super datatype */ + H5T_t *new_type; /* New native datatype */ + int i; /* Local index variable */ + H5T_t *ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5T_get_native_type, NULL); @@ -2748,18 +2743,23 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru switch(class) { case H5T_INTEGER: - if((sign = H5T_get_sign(dtype))==H5T_SGN_ERROR) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid signess"); - - if((ret_value = H5T_get_native_integer(size, sign, direction, struct_align, offset, comp_size))==NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer type"); - + { + H5T_sign_t sign; /* Signedness of integer type */ + + if((sign = H5T_get_sign(dtype))==H5T_SGN_ERROR) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid signess"); + + if((ret_value = H5T_get_native_integer(size, sign, direction, struct_align, offset, comp_size))==NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve integer type"); + } break; + case H5T_FLOAT: if((ret_value = H5T_get_native_float(size, direction, struct_align, offset, comp_size))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); break; + case H5T_STRING: if(H5T_is_variable_str(dtype)) { if(NULL==(dt=H5I_object_verify(H5T_C_S1, H5I_DATATYPE))) @@ -2778,6 +2778,7 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru } break; + case H5T_TIME: case H5T_BITFIELD: case H5T_OPAQUE: @@ -2786,151 +2787,171 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); break; - case H5T_COMPOUND: - if((nmemb = H5T_get_nmembers(dtype))<=0) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "compound data type doesn't have any member"); - - memb_list = (H5T_t**)HDmalloc(nmemb*sizeof(H5T_t*)); - memb_offset = (size_t*)HDcalloc(nmemb, sizeof(size_t)); - comp_mname = (char**)HDmalloc(nmemb*sizeof(char*)); - - /* Construct child compound type and retrieve a list of their IDs, offsets, total size, and alignment for compound type. */ - for(i=0; i0); if(direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) { - if(size==sizeof(char)) { + if(size==sizeof(char)) + match=H5T_NATIVE_INT_MATCH_CHAR; + else if(size==sizeof(short)) + match=H5T_NATIVE_INT_MATCH_SHORT; + else if(size==sizeof(int)) + match=H5T_NATIVE_INT_MATCH_INT; + else if(size==sizeof(long)) + match=H5T_NATIVE_INT_MATCH_LONG; + else if(size==sizeof(long long)) + match=H5T_NATIVE_INT_MATCH_LLONG; + else /* If no native type matches the querried datatype, simply choose the type of biggest size. */ + match=H5T_NATIVE_INT_MATCH_LLONG; + } else if(direction == H5T_DIR_DESCEND) { + if(size==sizeof(long long)) + match=H5T_NATIVE_INT_MATCH_LLONG; + else if(size==sizeof(long)) + match=H5T_NATIVE_INT_MATCH_LONG; + else if(size==sizeof(int)) + match=H5T_NATIVE_INT_MATCH_INT; + else if(size==sizeof(short)) + match=H5T_NATIVE_INT_MATCH_SHORT; + else if(size==sizeof(char)) + match=H5T_NATIVE_INT_MATCH_CHAR; + else /* If no native type matches the querried datatype, simple choose the type of smallest size. */ + match=H5T_NATIVE_INT_MATCH_CHAR; + } + + /* Set the appropriate native datatype information */ + switch(match) { + case H5T_NATIVE_INT_MATCH_CHAR: if(sign==H5T_SGN_2) tid = H5T_NATIVE_SCHAR; else tid = H5T_NATIVE_UCHAR; align = H5T_NATIVE_SCHAR_COMP_ALIGN_g; - } - else if(size==sizeof(short)) { + break; + + case H5T_NATIVE_INT_MATCH_SHORT: if(sign==H5T_SGN_2) tid = H5T_NATIVE_SHORT; else tid = H5T_NATIVE_USHORT; align = H5T_NATIVE_SHORT_COMP_ALIGN_g; - } - else if(size==sizeof(int)) { + break; + + case H5T_NATIVE_INT_MATCH_INT: if(sign==H5T_SGN_2) tid = H5T_NATIVE_INT; else tid = H5T_NATIVE_UINT; align = H5T_NATIVE_INT_COMP_ALIGN_g; - } - else if(size==sizeof(long)) { + break; + + case H5T_NATIVE_INT_MATCH_LONG: if(sign==H5T_SGN_2) tid = H5T_NATIVE_LONG; else tid = H5T_NATIVE_ULONG; align = H5T_NATIVE_LONG_COMP_ALIGN_g; - } - else if(size==sizeof(long long)) { - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_LLONG; - else - tid = H5T_NATIVE_ULLONG; - - align = H5T_NATIVE_LLONG_COMP_ALIGN_g; - } - else { /* If no native type matches the querried datatype, simply choose the type of biggest size. */ - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_LLONG; - else - tid = H5T_NATIVE_ULLONG; - - align = H5T_NATIVE_LLONG_COMP_ALIGN_g; - } - } else if(direction == H5T_DIR_DESCEND) { - if(size==sizeof(long long)) { + break; + + case H5T_NATIVE_INT_MATCH_LLONG: if(sign==H5T_SGN_2) tid = H5T_NATIVE_LLONG; else tid = H5T_NATIVE_ULLONG; align = H5T_NATIVE_LLONG_COMP_ALIGN_g; - } - else if(size==sizeof(long)) { - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_LONG; - else - tid = H5T_NATIVE_ULONG; - - align = H5T_NATIVE_LONG_COMP_ALIGN_g; - } - else if(size==sizeof(int)) { - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_INT; - else - tid = H5T_NATIVE_UINT; - - align = H5T_NATIVE_INT_COMP_ALIGN_g; - } - else if(size==sizeof(short)) { - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_SHORT; - else - tid = H5T_NATIVE_SHORT; - - align = H5T_NATIVE_SHORT_COMP_ALIGN_g; - } - if(size==sizeof(char)) { - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_SCHAR; - else - tid = H5T_NATIVE_UCHAR; - - align = H5T_NATIVE_SCHAR_COMP_ALIGN_g; - } - else { /* If no native type matches the querried datatype, simple choose the type of smallest size. */ - if(sign==H5T_SGN_2) - tid = H5T_NATIVE_SCHAR; - else - tid = H5T_NATIVE_UCHAR; - - align = H5T_NATIVE_SCHAR_COMP_ALIGN_g; - } - } + break; + + default: + assert(0 && "Unknown native integer match!"); + break; + } /* end switch */ /* Create new native type */ if(NULL==(dt=H5I_object_verify(tid, H5I_DATATYPE))) @@ -3088,16 +3107,18 @@ H5T_t* H5T_get_native_integer(size_t size, H5T_sign_t sign, H5T_direction_t dire HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); /* compute size and offset of compound type member. */ - if(comp_size && offset) { - /* Add alignment value */ - if(align && *comp_size%align) { - *offset = *comp_size + (align - *comp_size%align); - size = size + (align - *comp_size%align); - } else - *offset = *comp_size; + if(offset && comp_size) { + if(align && *comp_size%align) { + /* Add alignment value */ + *offset = *comp_size + (align - *comp_size%align); + size = size + (align - *comp_size%align); + } else + *offset = *comp_size; - *comp_size += size; - } + /* compute size of compound type member. */ + *comp_size += size; + } /* end if */ + /* Alignment for whole compound type */ if(struct_align && *struct_align0); + if(direction == H5T_DIR_DEFAULT || direction == H5T_DIR_ASCEND) { - if(size==sizeof(float)) { + if(size==sizeof(float)) + match=H5T_NATIVE_FLOAT_MATCH_FLOAT; + else if(size==sizeof(double)) + match=H5T_NATIVE_FLOAT_MATCH_DOUBLE; + else if(size==sizeof(long double)) + match=H5T_NATIVE_FLOAT_MATCH_LDOUBLE; + else /* If not match, return the biggest datatype */ + match=H5T_NATIVE_FLOAT_MATCH_LDOUBLE; + } else { + if(size==sizeof(long double)) + match=H5T_NATIVE_FLOAT_MATCH_LDOUBLE; + else if(size==sizeof(double)) + match=H5T_NATIVE_FLOAT_MATCH_DOUBLE; + else if(size==sizeof(float)) + match=H5T_NATIVE_FLOAT_MATCH_FLOAT; + else + match=H5T_NATIVE_FLOAT_MATCH_FLOAT; + } + + /* Set the appropriate native floating point information */ + switch(match) { + case H5T_NATIVE_FLOAT_MATCH_FLOAT: tid = H5T_NATIVE_FLOAT; align = H5T_NATIVE_FLOAT_COMP_ALIGN_g; - } - else if(size==sizeof(double)) { + break; + + case H5T_NATIVE_FLOAT_MATCH_DOUBLE: tid = H5T_NATIVE_DOUBLE; align = H5T_NATIVE_DOUBLE_COMP_ALIGN_g; - } - else if(size==sizeof(long double)) { - tid = H5T_NATIVE_LDOUBLE; - align = H5T_NATIVE_LDOUBLE_COMP_ALIGN_g; - } - else { /* If not match, return the biggest datatype */ - tid = H5T_NATIVE_LDOUBLE; - align = H5T_NATIVE_LDOUBLE_COMP_ALIGN_g; - } - } else { - if(size==sizeof(long double)) { + break; + + case H5T_NATIVE_FLOAT_MATCH_LDOUBLE: tid = H5T_NATIVE_LDOUBLE; align = H5T_NATIVE_LDOUBLE_COMP_ALIGN_g; - } - else if(size==sizeof(double)) { - tid = H5T_NATIVE_DOUBLE; - align = H5T_NATIVE_DOUBLE_COMP_ALIGN_g; - } - else if(size==sizeof(float)) { - tid = H5T_NATIVE_FLOAT; - align = H5T_NATIVE_FLOAT_COMP_ALIGN_g; - } - else { - tid = H5T_NATIVE_FLOAT; - align = H5T_NATIVE_FLOAT_COMP_ALIGN_g; - } - } + break; + + default: + assert(0 && "Unknown native floating-point match!"); + break; + } /* end switch */ /* Create new native type */ if(NULL==(dt=H5I_object_verify(tid, H5I_DATATYPE))) @@ -3173,17 +3209,19 @@ H5T_t* H5T_get_native_float(size_t size, H5T_direction_t direction, size_t *stru if((ret_value=H5T_copy(dt, H5T_COPY_TRANSIENT))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); - /* compute size and offset of compound type member. */ - if(comp_size && offset) { - /* Add alignment value */ + /* compute offset of compound type member. */ + if(offset && comp_size) { if(align && *comp_size % align) { + /* Add alignment value */ *offset = *comp_size + (align - *comp_size % align); size = size + (align - *comp_size % align); } else *offset = *comp_size; - - *comp_size += size; - } + + /* compute size of compound type member. */ + *comp_size += size; + } /* end if */ + /* Alignment for whole compound type */ if(struct_align && *struct_align < align) *struct_align = align; @@ -3774,10 +3812,14 @@ H5T_get_sign(H5T_t *dt) FUNC_ENTER_NOAPI(H5T_get_sign, H5T_SGN_ERROR); + assert(dt); + + /* Defer to parent */ + while(dt->parent) + dt = dt->parent; + /* Check args */ - if (!dt) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_SGN_ERROR, "not a valid data type"); - if (dt->parent) + if (H5T_INTEGER!=dt->type) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, H5T_SGN_ERROR, "operation not defined for data type class"); /* Sign */ @@ -4516,9 +4558,7 @@ H5T_get_nmembers(const H5T_t *dt) FUNC_ENTER_NOAPI(H5T_get_nmembers, FAIL); - /* check argument */ - if (NULL == dt) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a valid data type"); + assert(dt); if (H5T_COMPOUND==dt->type) ret_value = dt->u.compnd.nmembs; @@ -4599,6 +4639,8 @@ H5T_get_member_name(H5T_t *dt, int membno) FUNC_ENTER_NOAPI(H5T_get_member_name, NULL); + assert(dt); + switch (dt->type) { case H5T_COMPOUND: if (membno<0 || membno>=dt->u.compnd.nmembs) @@ -4750,11 +4792,8 @@ H5T_get_member_offset(H5T_t *dt, int membno) FUNC_ENTER_NOAPI(H5T_get_member_offset, 0); - /* Check args */ - if (!dt) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, 0, "not a valid data type"); - if (membno < 0 || membno >= dt->u.compnd.nmembs) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid member number"); + assert(dt); + assert(membno >= 0 && membno < dt->u.compnd.nmembs); /* Value */ ret_value = dt->u.compnd.memb[membno].offset; @@ -4883,11 +4922,8 @@ H5T_get_member_type(H5T_t *dt, int membno) FUNC_ENTER_NOAPI(H5T_get_member_type, NULL); - /* Check args */ - if (!dt) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "data type doesn't exist"); - if (membno < 0 || membno >= dt->u.compnd.nmembs) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid member number"); + assert(dt); + assert(membno >=0 && membno < dt->u.compnd.nmembs); /* Copy data type into an atom */ if (NULL == (ret_value = H5T_copy(dt->u.compnd.memb[membno].type, H5T_COPY_REOPEN))) @@ -5057,15 +5093,18 @@ done: H5T_t * H5T_enum_create(H5T_t *parent) { - H5T_t *ret_value = NULL; /*new enumeration data type */ + H5T_t *ret_value; /*new enumeration data type */ FUNC_ENTER_NOAPI(H5T_enum_create, NULL); + assert(parent); + /* Build new type */ if (NULL==(ret_value = H5FL_ALLOC(H5T_t,1))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); ret_value->type = H5T_ENUM; ret_value->parent = H5T_copy(parent, H5T_COPY_ALL); + assert(ret_value->parent); ret_value->size = ret_value->parent->size; ret_value->ent.header = HADDR_UNDEF; @@ -5192,6 +5231,8 @@ H5T_get_super(H5T_t *dt) FUNC_ENTER_NOAPI(H5T_get_super, NULL); + assert(dt); + if (!dt->parent) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "not a derived data type"); if (NULL==(ret_value=H5T_copy(dt->parent, H5T_COPY_ALL))) @@ -5269,6 +5310,9 @@ H5T_get_member_value(H5T_t *dt, int membno, void *value/*out*/) FUNC_ENTER_NOAPI(H5T_get_member_value, FAIL); + assert(dt); + assert(value); + HDmemcpy(value, dt->u.enumer.value + membno*dt->size, dt->size); done: @@ -8747,6 +8791,9 @@ H5T_get_array_ndims(H5T_t *dt) FUNC_ENTER_NOAPI(H5T_get_array_ndims, FAIL); + assert(dt); + assert(dt->type==H5T_ARRAY); + /* Retrieve the number of dimensions */ ret_value=dt->u.array.ndims; @@ -8775,7 +8822,6 @@ H5Tget_array_dims(hid_t type_id, hsize_t dims[], int perm[]) { H5T_t *dt = NULL; /* pointer to array data type */ herr_t ret_value = SUCCEED; /* return value */ - int i; /* Local index variable */ FUNC_ENTER_API(H5Tget_array_dims, FAIL); H5TRACE3("e","i*h*Is",type_id,dims,perm); @@ -8818,6 +8864,9 @@ H5T_get_array_dims(H5T_t *dt, hsize_t dims[], int perm[]) FUNC_ENTER_NOAPI(H5T_get_array_dims, FAIL); + assert(dt); + assert(dt->type==H5T_ARRAY); + /* Retrieve the sizes of the dimensions */ if(dims) for(i=0; iu.array.ndims; i++) -- cgit v0.12