From 56389752de43294f090800cb88e298cc8a779059 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sun, 28 Mar 2021 22:38:32 +0200 Subject: [PATCH] const --- src/Makefile | 10 +- src/templates_front/templator_front.org | 14 +-- src/templates_text/templator_text.org | 140 +++++++++++++++--------- 3 files changed, 99 insertions(+), 65 deletions(-) diff --git a/src/Makefile b/src/Makefile index d7a4354..3f55133 100644 --- a/src/Makefile +++ b/src/Makefile @@ -44,17 +44,17 @@ export CC CFLAGS FC FFLAGS LIBS .PHONY: clean -cppcheck.out: $(HEADER_FILES) $(SOURCE_FILES) - cppcheck --addon=cert -q --error-exitcode=0 \ - --enable=style,warning,unusedFunction,performance,portability,missingInclude \ - --language=c -rp --std=c99 -v $(SOURCE_FILES) 2>$@ - libtrexio.so: $(OBJECT_FILES) $(HEADER_FILES) cppcheck.out $(CC) -shared $(OBJECT_FILES) -o libtrexio.so fortran: libtrexio.so trexio_f.f90 $(FC) $(FFLAGS) -c trexio_f.f90 -o trexio_f.o +cppcheck.out: $(HEADER_FILES) $(SOURCE_FILES) + cppcheck --addon=cert -q --error-exitcode=0 \ + --enable=style,warning,unusedFunction,performance,portability,missingInclude \ + --language=c -rp --std=c99 -v $(SOURCE_FILES) 2>$@ + test_c: libtrexio.so test.c $(CC) $(CFLAGS) $(INCLUDE) -Wl,-rpath,$(PWD) -L. test.c -ltrexio $(LIBS) -o test_c diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 430e7c2..3af1f6c 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -390,8 +390,8 @@ end interface ** Template for frontend read/write a number #+begin_src c :tangle rw_num_front.h -trexio_exit_code trexio_read_$group_num$(trexio_t* file, int64_t* num); -trexio_exit_code trexio_write_$group_num$(trexio_t* file, const int64_t num); +trexio_exit_code trexio_read_$group_num$(trexio_t* const file, int64_t* const num); +trexio_exit_code trexio_write_$group_num$(trexio_t* const file, const int64_t num); #+end_src #+begin_src c :tangle read_num_front.c @@ -426,7 +426,7 @@ trexio_exit_code trexio_read_$group_num$(trexio_t* file, int64_t* num) { #+begin_src c :tangle write_num_front.c -trexio_exit_code trexio_write_$group_num$(trexio_t* file, const int64_t num) { +trexio_exit_code trexio_write_$group_num$(trexio_t* const file, const int64_t num) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (num < 0 ) return TREXIO_INVALID_ARG_2; @@ -483,12 +483,12 @@ end interface ** Template for frontend read/write a dataset #+begin_src c :tangle rw_dset_front.h -trexio_exit_code trexio_read_$group$_$group_dset$(trexio_t* file, $group_dset_dtype$* $group_dset$); -trexio_exit_code trexio_write_$group$_$group_dset$(trexio_t* file, const $group_dset_dtype$* $group_dset$); +trexio_exit_code trexio_read_$group$_$group_dset$(trexio_t* const file, $group_dset_dtype$* const $group_dset$); +trexio_exit_code trexio_write_$group$_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$); #+end_src #+begin_src c :tangle read_dset_front.c -trexio_exit_code trexio_read_$group$_$group_dset$(trexio_t* file, $group_dset_dtype$* $group_dset$) { +trexio_exit_code trexio_read_$group$_$group_dset$(trexio_t* const file, $group_dset_dtype$* const $group_dset$) { if (file == NULL) return TREXIO_INVALID_ARG_1; if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2; @@ -540,7 +540,7 @@ trexio_exit_code trexio_read_$group$_$group_dset$(trexio_t* file, $group_dset_dt #+begin_src c :tangle write_dset_front.c -trexio_exit_code trexio_write_$group$_$group_dset$(trexio_t* file, const $group_dset_dtype$* $group_dset$) { +trexio_exit_code trexio_write_$group$_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$) { if (file == NULL) return TREXIO_INVALID_ARG_1; if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2; diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 5d471ed..a520829 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -113,14 +113,14 @@ typedef struct trexio_text_s { *** Init/deinit functions (constant part) #+begin_src c :tangle basic_text.h -trexio_exit_code trexio_text_init(trexio_t* file); +trexio_exit_code trexio_text_init(trexio_t* const file); #+end_src #+begin_src c :tangle basic_text.c -trexio_exit_code trexio_text_init(trexio_t* file) { +trexio_exit_code trexio_text_init(trexio_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - trexio_text_t* f = (trexio_text_t*) file; + trexio_text_t* const f = (trexio_text_t*) file; /* Put all pointers to NULL but leave parent untouched */ memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t)); @@ -163,14 +163,14 @@ trexio_exit_code trexio_text_init(trexio_t* file) { #+end_src #+begin_src c :tangle basic_text.h -trexio_exit_code trexio_text_lock(trexio_t* file); +trexio_exit_code trexio_text_lock(trexio_t* const file); #+end_src #+begin_src c :tangle basic_text.c -trexio_exit_code trexio_text_lock(trexio_t* file) { +trexio_exit_code trexio_text_lock(trexio_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - trexio_text_t* f = (trexio_text_t*) file; + trexio_text_t* const f = (trexio_text_t*) file; struct flock fl; @@ -189,18 +189,18 @@ trexio_exit_code trexio_text_lock(trexio_t* file) { #+begin_src c :tangle basic_text.h -trexio_exit_code trexio_text_finalize(trexio_t* file); +trexio_exit_code trexio_text_finalize(trexio_t* const file); #+end_src #+begin_src c :tangle basic_text.h -trexio_exit_code trexio_text_unlock(trexio_t* file); +trexio_exit_code trexio_text_unlock(trexio_t* const file); #+end_src #+begin_src c :tangle basic_text.c -trexio_exit_code trexio_text_unlock(trexio_t* file) { +trexio_exit_code trexio_text_unlock(trexio_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - trexio_text_t* f = (trexio_text_t*) file; + trexio_text_t* const f = (trexio_text_t*) file; struct flock fl; @@ -220,7 +220,7 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) { *** Init/deinit functions (templated part) #+begin_src c :tangle basic_text_group.c -trexio_exit_code trexio_text_finalize(trexio_t* file) { +trexio_exit_code trexio_text_finalize(trexio_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; trexio_exit_code rc; @@ -239,12 +239,11 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) { *** Template for text read struct #+begin_src c :tangle read_group_text.h -$group$_t* trexio_text_read_$group$(trexio_text_t* file); +$group$_t* trexio_text_read_$group$(trexio_text_t* const file); #+end_src #+begin_src c :tangle read_group_text.c - -$group$_t* trexio_text_read_$group$(trexio_text_t* file) { +$group$_t* trexio_text_read_$group$(trexio_text_t* const file) { if (file == NULL) return NULL; /* If the data structure exists, return it */ @@ -438,11 +437,11 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* file) { *** Template for text flush struct #+begin_src c :tangle flush_group_text.h -trexio_exit_code trexio_text_flush_$group$(const trexio_text_t* file); +trexio_exit_code trexio_text_flush_$group$(trexio_text_t* const file); #+end_src #+begin_src c :tangle flush_group_text.c -trexio_exit_code trexio_text_flush_$group$(const trexio_text_t* file) { +trexio_exit_code trexio_text_flush_$group$(trexio_text_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (file->parent.mode == 'r') return TREXIO_READONLY; @@ -502,11 +501,11 @@ trexio_exit_code trexio_text_flush_$group$(const trexio_text_t* file) { Memory is allocated when reading. The following function frees memory. #+begin_src c :tangle free_group_text.h -trexio_exit_code trexio_text_free_$group$(trexio_text_t* file); +trexio_exit_code trexio_text_free_$group$(trexio_text_t* const file); #+end_src #+begin_src c :tangle free_group_text.c -trexio_exit_code trexio_text_free_$group$(trexio_text_t* file) { +trexio_exit_code trexio_text_free_$group$(trexio_text_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (file->parent.mode != 'r') { @@ -538,12 +537,12 @@ trexio_exit_code trexio_text_free_$group$(trexio_text_t* file) { *** Template for read/write the $group_num$ attribute #+begin_src c :tangle rw_num_text.h -trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* num); -trexio_exit_code trexio_text_write_$group_num$(const trexio_t* file, const uint64_t num); +trexio_exit_code trexio_text_read_$group_num$ (trexio_t* const file, uint64_t* const num); +trexio_exit_code trexio_text_write_$group_num$(trexio_t* const file, const uint64_t num); #+end_src #+begin_src c :tangle read_num_text.c -trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* num) { +trexio_exit_code trexio_text_read_$group_num$(trexio_t* const file, uint64_t* const num) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (num == NULL) return TREXIO_INVALID_ARG_2; @@ -551,7 +550,7 @@ trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* nu $group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file); if ($group$ == NULL) return TREXIO_FAILURE; - /**/ *num = $group$->$group_num$; + *num = $group$->$group_num$; return TREXIO_SUCCESS; } @@ -559,7 +558,7 @@ trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* nu #+begin_src c :tangle write_num_text.c -trexio_exit_code trexio_text_write_$group_num$(const trexio_t* file, const uint64_t num) { +trexio_exit_code trexio_text_write_$group_num$(trexio_t* const file, const uint64_t num) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -580,17 +579,17 @@ trexio_exit_code trexio_text_write_$group_num$(const trexio_t* file, const uint6 The ~dset~ array is assumed allocated with the appropriate size. #+begin_src c :tangle rw_dset_text.h -trexio_exit_code trexio_text_read_$group_dset$(const trexio_t* file, $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims); -trexio_exit_code trexio_text_write_$group_dset$(const trexio_t* file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims); #+end_src #+begin_src c :tangle read_dset_text.c -trexio_exit_code trexio_text_read_$group_dset$(const trexio_t* file, $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) { +trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2; - $group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file); + $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); if ($group$ == NULL) return TREXIO_FAILURE; if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3; @@ -610,14 +609,13 @@ trexio_exit_code trexio_text_read_$group_dset$(const trexio_t* file, $group_dset #+end_src #+begin_src c :tangle write_dset_text.c - -trexio_exit_code trexio_text_write_$group_dset$(const trexio_t* file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) { +trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2; if (file->mode == 'r') return TREXIO_READONLY; - $group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file); + $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); if ($group$ == NULL) return TREXIO_FAILURE; if ($group$->$group_dset$ != NULL) { @@ -648,17 +646,17 @@ trexio_exit_code trexio_text_write_$group_dset$(const trexio_t* file, const $gro **** Read the complete struct #+begin_src c :tangle rdm_text.h -rdm_t* trexio_text_read_rdm(trexio_text_t* file); +rdm_t* trexio_text_read_rdm(trexio_text_t* const file); #+end_src #+begin_src c :tangle rdm_text.c -rdm_t* trexio_text_read_rdm(trexio_text_t* file) { +rdm_t* trexio_text_read_rdm(trexio_text_t* const file) { if (file == NULL) return NULL; if (file->rdm != NULL) return file->rdm; /* Allocate the data structure */ - rdm_t* rdm = MALLOC(rdm_t); + rdm_t* const rdm = MALLOC(rdm_t); assert (rdm != NULL); rdm->one_e = NULL; @@ -738,16 +736,16 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { **** Flush the complete struct #+begin_src c :tangle rdm_text.h -trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file); +trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file); #+end_src #+begin_src c :tangle rdm_text.c -trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { +trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (file->parent.mode == 'r') return TREXIO_READONLY; - rdm_t* rdm = file->rdm; + rdm_t* const rdm = file->rdm; if (rdm == NULL) return TREXIO_SUCCESS; if (rdm->to_flush == 0) return TREXIO_SUCCESS; @@ -779,11 +777,11 @@ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { Memory is allocated when reading. The followig function frees memory. #+begin_src c :tangle rdm_text.h -trexio_exit_code trexio_text_free_rdm(trexio_text_t* file); +trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file); #+end_src #+begin_src c :tangle rdm_text.c -trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { +trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (file->parent.mode != 'r') { @@ -791,7 +789,7 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; } - rdm_t* rdm = file->rdm; + rdm_t* const rdm = file->rdm; if (rdm == NULL) return TREXIO_SUCCESS; if (rdm->file != NULL) { @@ -818,17 +816,27 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { The ~one_e~ array is assumed allocated with the appropriate size. #+begin_src c :tangle rdm_text.h -trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e); -trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* one_e, const uint64_t dim_one_e); +trexio_exit_code +trexio_text_read_rdm_one_e(trexio_t* const file, + double* const one_e, + const uint64_t dim_one_e); + +trexio_exit_code +trexio_text_write_rdm_one_e(trexio_t* const file, + const double* one_e, + const uint64_t dim_one_e); #+end_src #+begin_src c :tangle rdm_text.c -trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e) { - +trexio_exit_code +trexio_text_read_rdm_one_e(trexio_t* const file, + double* const one_e, + const uint64_t dim_one_e) +{ if (file == NULL) return TREXIO_INVALID_ARG_1; if (one_e == NULL) return TREXIO_INVALID_ARG_2; - rdm_t* rdm = trexio_text_read_rdm((trexio_text_t*) file); + rdm_t* const rdm = trexio_text_read_rdm((trexio_text_t*) file); if (rdm == NULL) return TREXIO_FAILURE; if (dim_one_e != rdm->dim_one_e) return TREXIO_INVALID_ARG_3; @@ -841,12 +849,16 @@ trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, } -trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* one_e, const uint64_t dim_one_e) { +trexio_exit_code +trexio_text_write_rdm_one_e(trexio_t* const file, + const double* one_e, + const uint64_t dim_one_e) +{ if (file == NULL) return TREXIO_INVALID_ARG_1; if (one_e == NULL) return TREXIO_INVALID_ARG_2; if (file->mode != 'r') return TREXIO_READONLY; - rdm_t* rdm = trexio_text_read_rdm((trexio_text_t*) file); + rdm_t* const rdm = trexio_text_read_rdm((trexio_text_t*) file); if (rdm == NULL) return TREXIO_FAILURE; rdm->dim_one_e = dim_one_e; @@ -868,18 +880,34 @@ trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* file for each sparse float structure. #+begin_src c :tangle rdm_text.h -trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value); -trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, const int64_t* index, const double* value); +trexio_exit_code +trexio_text_buffered_read_rdm_two_e(trexio_t* const file, + const uint64_t offset, + const uint64_t size, + int64_t* const index, + double* const value); + +trexio_exit_code +trexio_text_buffered_write_rdm_two_e(trexio_t* const file, + const uint64_t offset, + const uint64_t size, + const int64_t* index, + const double* value); #+end_src #+begin_src c :tangle rdm_text.c -trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value) { - +trexio_exit_code +trexio_text_buffered_read_rdm_two_e(trexio_t* const file, + const uint64_t offset, + const uint64_t size, + int64_t* const index, + double* const value) +{ if (file == NULL) return TREXIO_INVALID_ARG_1; if (index == NULL) return TREXIO_INVALID_ARG_4; if (value == NULL) return TREXIO_INVALID_ARG_5; - rdm_t* rdm = trexio_text_read_rdm((trexio_text_t*) file); + rdm_t* const rdm = trexio_text_read_rdm((trexio_text_t*) file); if (rdm == NULL) return TREXIO_FAILURE; FILE* f = fopen(rdm->two_e_file_name, "r"); @@ -906,13 +934,19 @@ trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const } -trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, const int64_t* index, const double* value) { +trexio_exit_code +trexio_text_buffered_write_rdm_two_e(trexio_t* const file, + const uint64_t offset, + const uint64_t size, + const int64_t* index, + const double* value) +{ if (file == NULL) return TREXIO_INVALID_ARG_1; if (index == NULL) return TREXIO_INVALID_ARG_4; if (value == NULL) return TREXIO_INVALID_ARG_5; if (file->mode != 'r') return TREXIO_READONLY; - rdm_t* rdm = trexio_text_read_rdm((trexio_text_t*) file); + rdm_t* const rdm = trexio_text_read_rdm((trexio_text_t*) file); if (rdm == NULL) return TREXIO_FAILURE; FILE* f = fopen(rdm->two_e_file_name, "w");