From dcd54d7624bbfec6f10fd58abff83b9df0196b83 Mon Sep 17 00:00:00 2001 From: Raymond Lu Date: Wed, 11 Dec 2002 11:19:14 -0500 Subject: [svn-r6195] Purpose: Cleanup H5Tget_native_type code Description: improve code cosmetics. Solution: Platforms tested: modi4, eirene, arabica --- src/H5T.c | 233 +++++++++++++++++++++++++++++++++++++++---------------- src/H5Tpkg.h | 5 ++ src/H5Tprivate.h | 11 ++- src/H5detect.c | 105 ++++++++++++++++--------- 4 files changed, 249 insertions(+), 105 deletions(-) diff --git a/src/H5T.c b/src/H5T.c index 1aff6ab..51b14d5 100644 --- a/src/H5T.c +++ b/src/H5T.c @@ -149,6 +149,11 @@ size_t H5T_NATIVE_FLOAT_COMP_ALIGN_g = 0; size_t H5T_NATIVE_DOUBLE_COMP_ALIGN_g = 0; size_t H5T_NATIVE_LDOUBLE_COMP_ALIGN_g = 0; +size_t H5T_POINTER_COMP_ALIGN_g = 0; +size_t H5T_HVL_COMP_ALIGN_g = 0; +size_t H5T_HOBJREF_COMP_ALIGN_g = 0; +size_t H5T_HDSETREGREF_COMP_ALIGN_g = 0; + /* * Alignment constraints for native types. These are initialized at run time * in H5Tinit.c @@ -2761,32 +2766,84 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru break; case H5T_STRING: - if(H5T_is_variable_str(dtype)) { - if(NULL==(dt=H5I_object_verify(H5T_C_S1, H5I_DATATYPE))) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); - if((ret_value=H5T_copy(dt, H5T_COPY_TRANSIENT))==NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); - if(H5T_set_size(ret_value, H5T_VARIABLE)<0) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot set size"); - } else { - if(NULL==(dt=H5I_object_verify(H5T_NATIVE_UCHAR, H5I_DATATYPE))) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); - if((ret_value=H5T_copy(dt, H5T_COPY_TRANSIENT))==NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); - if(H5T_set_size(ret_value, size)<0) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot set size"); + { + size_t align; + size_t pointer_size; + + if(H5T_is_variable_str(dtype)) { + if(NULL==(dt=H5I_object_verify(H5T_C_S1, H5I_DATATYPE))) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); + if((ret_value=H5T_copy(dt, H5T_COPY_TRANSIENT))==NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); + if(H5T_set_size(ret_value, H5T_VARIABLE)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot set size"); + + /* Update size, offset and compound alignment for parent. */ + align = H5T_POINTER_COMP_ALIGN_g; + pointer_size = sizeof(char*); + + if(H5T_cmp_offset(comp_size, offset, pointer_size, 1, align, struct_align)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset"); + + } else { + /*size_t char_size;*/ + + if(NULL==(dt=H5I_object_verify(H5T_NATIVE_UCHAR, H5I_DATATYPE))) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); + if((ret_value=H5T_copy(dt, H5T_COPY_TRANSIENT))==NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); + if(H5T_set_size(ret_value, size)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot set size"); + + /* Update size, offset and compound alignment for parent. */ + align = H5T_NATIVE_SCHAR_COMP_ALIGN_g; + + if(H5T_cmp_offset(comp_size, offset, sizeof(char), size, align, struct_align)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset"); + + if(H5T_close(dt)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "can't close data type"); + } } - break; + /* These three types don't need to compute compound field information since they + * can't be used as field. */ case H5T_TIME: case H5T_BITFIELD: case H5T_OPAQUE: - case H5T_REFERENCE: if((ret_value=H5T_copy(dtype, H5T_COPY_TRANSIENT))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); break; + case H5T_REFERENCE: + { + size_t align; + size_t ref_size; + int not_equal; + H5T_t *dt; + + if((ret_value=H5T_copy(dtype, H5T_COPY_TRANSIENT))==NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve float type"); + + /* Decide if the data type is object or dataset region reference. */ + if(NULL==(dt=H5I_object_verify(H5T_STD_REF_OBJ_g, H5I_DATATYPE))) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); + not_equal = H5T_cmp(ret_value, dt); + + /* Update size, offset and compound alignment for parent. */ + if(!not_equal) { + align = H5T_HOBJREF_COMP_ALIGN_g; + ref_size = sizeof(hobj_ref_t); + } else { + align = H5T_HDSETREGREF_COMP_ALIGN_g; + ref_size = sizeof(hdset_reg_ref_t); + } + + if(H5T_cmp_offset(comp_size, offset, ref_size, 1, align, struct_align)<0) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot compute compound offset"); + } + break; case H5T_COMPOUND: { @@ -2861,14 +2918,15 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru ret_value = new_type; } - break; case H5T_ENUM: { char *memb_name; /* Enum's member name */ void *memb_value; /* Enum's member value */ - + + /* Don't need to do anything special for alignment, offset since the ENUM type usually is integer. */ + /* Retrieve base type for enumarate type */ if((super_type=H5T_get_super(dtype))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get base type for enumarate type"); @@ -2907,13 +2965,17 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru ret_value = new_type; } - break; case H5T_ARRAY: { int array_rank; /* Array's rank */ hsize_t *dims = NULL; /* Dimension sizes for array */ + hsize_t nelems = 1; + size_t super_offset=0; + size_t super_size=0; + size_t super_align=0; + int i; /* Retrieve dimension information for array data type */ if((array_rank=H5T_get_array_ndims(dtype))<=0) @@ -2926,7 +2988,8 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru /* Retrieve base type for array type */ if((super_type=H5T_get_super(dtype))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get parent type for enumarate type"); - if((nat_super_type = H5T_get_native_type(super_type, direction, struct_align, offset, comp_size))==NULL) + if((nat_super_type = H5T_get_native_type(super_type, direction, &super_align, + &super_offset, &super_size))==NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "parent native type retrieval failed"); /* Close super type */ @@ -2940,34 +3003,52 @@ H5T_t* H5T_get_native_type(H5T_t *dtype, H5T_direction_t direction, size_t *stru /* Close base type */ if(H5T_close(nat_super_type)<0) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot close datatype"); + + for(i=0; i1 && *comp_size%align) { /* Add alignment value */ - *offset = *comp_size + (align - *comp_size % align); - size = size + (align - *comp_size % align); + *offset = *comp_size + (align - *comp_size%align); + *comp_size += (align - *comp_size%align); } else *offset = *comp_size; - - /* compute size of compound type member. */ - *comp_size += size; - } /* end if */ - /* Alignment for whole compound type */ + /* compute size of compound type member. */ + *comp_size += nelems*elem_size; + } + if(struct_align && *struct_align < align) *struct_align = align; - + done: FUNC_LEAVE(ret_value); } diff --git a/src/H5Tpkg.h b/src/H5Tpkg.h index 0adc4e9..c3a9703 100644 --- a/src/H5Tpkg.h +++ b/src/H5Tpkg.h @@ -187,6 +187,11 @@ H5_DLLVAR size_t H5T_NATIVE_FLOAT_COMP_ALIGN_g; H5_DLLVAR size_t H5T_NATIVE_DOUBLE_COMP_ALIGN_g; H5_DLLVAR size_t H5T_NATIVE_LDOUBLE_COMP_ALIGN_g; +H5_DLLVAR size_t H5T_POINTER_COMP_ALIGN_g; +H5_DLLVAR size_t H5T_HVL_COMP_ALIGN_g; +H5_DLLVAR size_t H5T_HOBJREF_COMP_ALIGN_g; +H5_DLLVAR size_t H5T_HDSETREGREF_COMP_ALIGN_g; + /* * Alignment information for native types. A value of N indicates that the * data must be aligned on an address ADDR such that 0 == ADDR mod N. When diff --git a/src/H5Tprivate.h b/src/H5Tprivate.h index cd75f05..d9f4520 100644 --- a/src/H5Tprivate.h +++ b/src/H5Tprivate.h @@ -116,9 +116,14 @@ H5_DLL H5T_t *H5T_get_member_type(H5T_t *dt, int membno); H5_DLL size_t H5T_get_member_offset(H5T_t *dt, int membno); H5_DLL int H5T_get_nmembers(const H5T_t *dt); H5_DLL htri_t H5T_is_variable_str(H5T_t *dt); -H5_DLL H5T_t *H5T_get_native_type(H5T_t *dt, H5T_direction_t direction, size_t *struct_align, size_t *offset, size_t *comp_size); -H5_DLL H5T_t *H5T_get_native_integer(size_t size, H5T_sign_t sign, H5T_direction_t direction, size_t *struct_align, size_t *offset, size_t *comp_size); -H5_DLL H5T_t *H5T_get_native_float(size_t size, H5T_direction_t direction, size_t *struct_align, size_t *offset, size_t *comp_size); +H5_DLL H5T_t *H5T_get_native_type(H5T_t *dt, H5T_direction_t direction, + size_t *struct_align, size_t *offset, size_t *comp_size); +H5_DLL H5T_t *H5T_get_native_integer(size_t size, H5T_sign_t sign, H5T_direction_t direction, + size_t *struct_align, size_t *offset, size_t *comp_size); +H5_DLL H5T_t *H5T_get_native_float(size_t size, H5T_direction_t direction, + size_t *struct_align, size_t *offset, size_t *comp_size); +H5_DLL herr_t H5T_cmp_offset(size_t *comp_size, size_t *offset, size_t elem_size, + size_t nelems, size_t align, size_t *struct_align); H5_DLL int H5T_cmp(const H5T_t *dt1, const H5T_t *dt2); H5_DLL htri_t H5T_is_atomic(const H5T_t *dt); H5_DLL herr_t H5T_insert(H5T_t *parent, const char *name, size_t offset, diff --git a/src/H5detect.c b/src/H5detect.c index 78ac69c..0d19208 100644 --- a/src/H5detect.c +++ b/src/H5detect.c @@ -31,6 +31,8 @@ static const char *FileHeader = "\n\ */ #undef NDEBUG #include "H5private.h" +#include "H5Tpublic.h" +#include "H5Rpublic.h" #define MAXDETECT 64 /* @@ -51,7 +53,14 @@ typedef struct detected_t { size_t comp_align; /*alignment for structure */ } detected_t; -static void print_results(int nd, detected_t *d); +/* This structure holds structure alignment for pointers, hvl_t, hobj_ref_t, + * hdset_reg_ref_t */ +typedef struct malign_t { + const char *name; + size_t comp_align; /*alignment for structure */ +} malign_t; + +static void print_results(int nd, detected_t *d, int na, malign_t *m); static void iprint(detected_t *); static int byte_cmp(int, void *, void *); static int bit_cmp(int, int *, void *, void *); @@ -163,6 +172,11 @@ precision (detected_t *d) } \ INFO.sign = ('U'!=*(#VAR)); \ ALIGNMENT(TYPE, INFO.align); \ + if(!strcmp(INFO.varname, "SCHAR") || !strcmp(INFO.varname, "SHORT") || \ + !strcmp(INFO.varname, "INT") || !strcmp(INFO.varname, "LONG") || \ + !strcmp(INFO.varname, "LLONG")) { \ + COMP_ALIGNMENT(TYPE,INFO.comp_align); \ + } \ precision (&(INFO)); \ } @@ -246,9 +260,36 @@ precision (detected_t *d) _v1 = 1.0; \ INFO.bias = find_bias (INFO.epos, INFO.esize, INFO.perm, &_v1); \ ALIGNMENT(TYPE, INFO.align); \ + if(!strcmp(INFO.varname, "FLOAT") || !strcmp(INFO.varname, "DOUBLE") || \ + !strcmp(INFO.varname, "LDOUBLE")) { \ + COMP_ALIGNMENT(TYPE,INFO.comp_align); \ + } \ precision (&(INFO)); \ } + +/*------------------------------------------------------------------------- + * Function: DETECT_M + * + * Purpose: This macro takes only miscellaneous structures or pointer + * (pointer, hvl_t, hobj_ref_t, hdset_reg_ref_t). It + * constructs the names and decides the alignment in structure. + * + * Return: void + * + * Programmer: Raymond Lu + * slu@ncsa.uiuc.edu + * Dec 9, 2002 + * + * Modifications: + * + *------------------------------------------------------------------------- + */ +#define DETECT_M(TYPE,VAR,INFO) { \ + INFO.name = #VAR; \ + COMP_ALIGNMENT(TYPE, INFO.comp_align); \ +} + /* Detect alignment for C structure */ #define COMP_ALIGNMENT(TYPE,COMP_ALIGN) { \ struct { \ @@ -399,10 +440,10 @@ sigbus_handler(int UNUSED signo) *------------------------------------------------------------------------- */ static void -print_results(int nd, detected_t *d) +print_results(int nd, detected_t *d, int na, malign_t *misc_align) { - int i; + int i, j; /* Include files */ printf("\ @@ -514,6 +555,11 @@ H5TN_init_interface(void)\n\ } } + /* Structure alignment for pointers, hvl_t, hobj_ref_t, hdset_reg_ref_t */ + printf("\n /* Structure alignment for pointers, hvl_t, hobj_ref_t, hdset_reg_ref_t */\n"); + for(j=0; j0 - DETECT_I(long_long, LLONG, d[nd]); - COMP_ALIGNMENT(long_long, d[nd].comp_align); nd++; - + DETECT_I(long_long, LLONG, d[nd]); nd++; DETECT_I(unsigned long_long, ULLONG, d[nd]); nd++; #else /* @@ -1139,17 +1173,12 @@ main(void) * so we'll just make H5T_NATIVE_LLONG the same as H5T_NATIVE_LONG since * `long long' is probably equivalent to `long' here anyway. */ - DETECT_I(long, LLONG, d[nd]); - COMP_ALIGNMENT(long, d[nd].comp_align); nd++; - + DETECT_I(long, LLONG, d[nd]); nd++; DETECT_I(unsigned long, ULLONG, d[nd]); nd++; #endif - DETECT_F(float, FLOAT, d[nd]); - COMP_ALIGNMENT(float, d[nd].comp_align); nd++; - - DETECT_F(double, DOUBLE, d[nd]); - COMP_ALIGNMENT(double, d[nd].comp_align); nd++; + DETECT_F(float, FLOAT, d[nd]); nd++; + DETECT_F(double, DOUBLE, d[nd]); nd++; #if H5_SIZEOF_DOUBLE == H5_SIZEOF_LONG_DOUBLE /* @@ -1158,14 +1187,18 @@ main(void) * some systems and `long double' is probably the same as `double' here * anyway. */ - DETECT_F(double, LDOUBLE, d[nd]); - COMP_ALIGNMENT(double, d[nd].comp_align); nd++; + DETECT_F(double, LDOUBLE, d[nd]); nd++; #else - DETECT_F(long double, LDOUBLE, d[nd]); - COMP_ALIGNMENT(long double, d[nd].comp_align); nd++; + DETECT_F(long double, LDOUBLE, d[nd]); nd++; #endif - print_results (nd, d); + /* Detect structure alignment for pointers, hvl_t, hobj_ref_t, hdset_reg_ref_t */ + DETECT_M(void *, POINTER, m[na]); na++; + DETECT_M(hvl_t, HVL, m[na]); na++; + DETECT_M(hobj_ref_t, HOBJREF, m[na]); na++; + DETECT_M(hdset_reg_ref_t, HDSETREGREF, m[na]); na++; + + print_results (nd, d, na, m); return 0; } -- cgit v0.12