1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

add memory-safe functions in single or double precision

This commit is contained in:
q-posev 2021-08-24 13:32:01 +03:00
parent 2911e91941
commit ed7e3902e2

View File

@ -1640,14 +1640,8 @@ trexio_write_$group_dset$_32 (trexio_t* const file, const $group_dset_dtype_sing
**** Source code for memory-safe functions
#+begin_src c :tangle read_dset_data_safe_front.c
trexio_exit_code
trexio_read_safe_$group_dset$ (trexio_t* const file, $group_dset_dtype_default$* const dset_out, const int64_t dim_out)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_out == NULL) return TREXIO_INVALID_ARG_2;
#+NAME:dimCheck
#+begin_src c
trexio_exit_code rc;
int64_t $group_dset_dim$ = 0;
@ -1664,51 +1658,97 @@ trexio_read_safe_$group_dset$ (trexio_t* const file, $group_dset_dtype_default$*
for (uint32_t i=0; i<rank; ++i){
dim_size *= dims[i];
}
#+end_src
#+begin_src c :tangle read_dset_data_32_safe_front.c :noweb yes
trexio_exit_code
trexio_read_safe_$group_dset$_32 (trexio_t* const file, $group_dset_dtype_single$* const dset_out, const int64_t dim_out)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_out == NULL) return TREXIO_INVALID_ARG_2;
<<dimCheck>>
if (dim_out > (int64_t) dim_size) return TREXIO_UNSAFE_ARRAY_DIM;
/* */
return trexio_read_$group_dset$_$default_prec$(file, dset_out);
return trexio_read_$group_dset$_32(file, dset_out);
}
#+end_src
#+begin_src c :tangle write_dset_data_safe_front.c
#+begin_src c :tangle write_dset_data_32_safe_front.c :noweb yes
trexio_exit_code
trexio_write_safe_$group_dset$ (trexio_t* const file, const $group_dset_dtype_default$* dset_in, const int64_t dim_in)
trexio_write_safe_$group_dset$_32 (trexio_t* const file, const $group_dset_dtype_single$* dset_in, const int64_t dim_in)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
trexio_exit_code rc;
int64_t $group_dset_dim$ = 0;
/* Error handling for this call is added by the generator */
rc = trexio_read_$group_dset_dim$_64(file, &($group_dset_dim$));
if ($group_dset_dim$ == 0L) return TREXIO_INVALID_NUM;
uint32_t rank = $group_dset_rank$;
uint64_t dims[$group_dset_rank$] = {$group_dset_dim_list$};
/* The block below is specific to safe API as it checks the boundaries */
uint64_t dim_size = 1;
for (uint32_t i=0; i<rank; ++i){
dim_size *= dims[i];
}
<<dimCheck>>
if (dim_in > (int64_t) dim_size) return TREXIO_UNSAFE_ARRAY_DIM;
/* */
return trexio_write_$group_dset$_$default_prec$(file, dset_in);
return trexio_write_$group_dset$_32(file, dset_in);
}
#+end_src
#+begin_src c :tangle read_dset_data_64_safe_front.c :noweb yes
trexio_exit_code
trexio_read_safe_$group_dset$_64 (trexio_t* const file, $group_dset_dtype_double$* const dset_out, const int64_t dim_out)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_out == NULL) return TREXIO_INVALID_ARG_2;
<<dimCheck>>
if (dim_out > (int64_t) dim_size) return TREXIO_UNSAFE_ARRAY_DIM;
return trexio_read_$group_dset$_64(file, dset_out);
}
#+end_src
#+begin_src c :tangle write_dset_data_64_safe_front.c :noweb yes
trexio_exit_code
trexio_write_safe_$group_dset$_64 (trexio_t* const file, const $group_dset_dtype_double$* dset_in, const int64_t dim_in)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset_in == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS;
<<dimCheck>>
if (dim_in > (int64_t) dim_size) return TREXIO_UNSAFE_ARRAY_DIM;
return trexio_write_$group_dset$_64(file, dset_in);
}
#+end_src
**** Source code for default functions
#+begin_src c :tangle read_dset_data_safe_def_front.c
trexio_exit_code
trexio_read_safe_$group_dset$ (trexio_t* const file, $group_dset_dtype_default$* const $group_dset$, const int64_t dim_out)
{
return trexio_read_safe_$group_dset$_$default_prec$(file, $group_dset$, dim_out);
}
#+end_src
#+begin_src c :tangle write_dset_data_safe_def_front.c
trexio_exit_code
trexio_write_safe_$group_dset$ (trexio_t* const file, const $group_dset_dtype_default$* $group_dset$, const int64_t dim_in)
{
return trexio_write_safe_$group_dset$_$default_prec$(file, $group_dset$, dim_in);
}
#+end_src
#+begin_src c :tangle read_dset_data_def_front.c
trexio_exit_code
trexio_read_$group_dset$ (trexio_t* const file, $group_dset_dtype_default$* const $group_dset$)