diff --git a/src/trexio.c b/src/trexio.c index 554d46f..a5c8c49 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -83,8 +83,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -109,8 +109,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -143,8 +143,8 @@ trexio_exit_code trexio_close(trexio_t* file) { } if (rc != TREXIO_SUCCESS) { - free(file->file_name); - free(file); + FREE(file->file_name); + FREE(file); return TREXIO_FAILURE; } @@ -170,12 +170,11 @@ trexio_exit_code trexio_close(trexio_t* file) { /* Terminate front end */ - free(file->file_name); - file->file_name = NULL; + FREE(file->file_name); int irc = pthread_mutex_destroy( &(file->thread_lock) ); - free(file); + FREE(file); if (irc != 0) return TREXIO_ERRNO; if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; diff --git a/src/trexio.org b/src/trexio.org index 3b58cf2..f464ed3 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -46,6 +46,7 @@ #define _TREXIO_S_H #include "trexio.h" +#include "trexio_private.h" #include #include #+end_src @@ -61,6 +62,24 @@ - structs are suffixed by ~_s~ - types are suffixed by ~_t~ +** Memory allocation + + Memory allocation of structures can be facilitated by using the + following macro, which ensures that the size of the allocated + object is the same as the size of the data type pointed by the pointer. + + #+begin_src c :tangle trexio_private.h +#define MALLOC(T) (T*) malloc (sizeof(T)); +#define CALLOC(N,T) (T*) calloc ((N),sizeof(T)); + #+end_src + + When a pointer is freed, it should be set to ~NULL~. + This can be facilitated by the use of the following macro: + + #+begin_src c :tangle trexio_private.h +#define FREE(X) { free(X) ; (X)=NULL; } + #+end_src + * Front end All calls to TREXIO are thread-safe. @@ -223,8 +242,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -249,8 +268,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -291,8 +310,8 @@ trexio_exit_code trexio_close(trexio_t* file) { } if (rc != TREXIO_SUCCESS) { - free(file->file_name); - free(file); + FREE(file->file_name); + FREE(file); return TREXIO_FAILURE; } @@ -318,12 +337,11 @@ trexio_exit_code trexio_close(trexio_t* file) { /* Terminate front end */ - free(file->file_name); - file->file_name = NULL; + FREE(file->file_name); int irc = pthread_mutex_destroy( &(file->thread_lock) ); - free(file); + FREE(file); if (irc != 0) return TREXIO_ERRNO; if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index 2cfcdc1..1e1b2dc 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -7,10 +7,10 @@ #include "trexio_hdf5.h" -#define NUCLEUS_GROUP_NAME "nucleus" -#define NUCLEUS_NUM_NAME "nucleus_num" -#define NUCLEUS_CHARGE_NAME "nucleus_charge" -#define NUCLEUS_COORD_NAME "nucleus_coord" + #define NUCLEUS_GROUP_NAME "nucleus" + #define NUCLEUS_NUM_NAME "nucleus_num" + #define NUCLEUS_CHARGE_NAME "nucleus_charge" + #define NUCLEUS_COORD_NAME "nucleus_coord" /* * Currently H5LTread_dataset_ is used instead of this function @@ -24,7 +24,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na /* * Low-level implementation. Involves dealing with all HDF5 handles and dimensions */ - dset_t* dset = (dset_t*) malloc(sizeof(dset_t)); + dset_t* dset = MALLOC(dset_t); assert (dset != NULL); dset->dset_id = H5Dopen(file->nucleus_group, @@ -145,7 +145,7 @@ trexio_exit_code trexio_hdf5_finalize(trexio_t* file) { h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) { /* Allocate the data structure */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + h5nucleus_t* nucleus = MALLOC(h5nucleus_t); assert (nucleus != NULL); nucleus->num = 0; @@ -323,19 +323,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) { if (nucleus == NULL) return TREXIO_FAILURE; - if (nucleus->coord != NULL) free (nucleus->coord); - nucleus->coord = NULL; + if (nucleus->coord != NULL) + FREE (nucleus->coord); - if (nucleus->charge != NULL) free (nucleus->charge); - nucleus->charge = NULL; + if (nucleus->charge != NULL) + FREE (nucleus->charge); - if (nucleus->h5_coord != NULL) free (nucleus->h5_coord); - nucleus->h5_coord = NULL; + if (nucleus->h5_coord != NULL) + FREE (nucleus->h5_coord); - if (nucleus->h5_charge != NULL) free (nucleus->h5_charge); - nucleus->h5_charge = NULL; + if (nucleus->h5_charge != NULL) + FREE (nucleus->h5_charge); - free (nucleus); + FREE (nucleus); return TREXIO_SUCCESS; } @@ -446,17 +446,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co H5Dclose(dset_id); if (status < 0) { - free(ddims); + FREE(ddims); return TREXIO_FAILURE; } for (uint32_t i=0; inucleus_group, diff --git a/src/trexio_hdf5.h b/src/trexio_hdf5.h index f71360c..89106aa 100644 --- a/src/trexio_hdf5.h +++ b/src/trexio_hdf5.h @@ -9,6 +9,7 @@ #define _TREXIO_HDF5_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index f0c30b5..90d3493 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -19,6 +19,7 @@ #define _TREXIO_HDF5_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include @@ -115,7 +116,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na /* * Low-level implementation. Involves dealing with all HDF5 handles and dimensions */ - dset_t* dset = (dset_t*) malloc(sizeof(dset_t)); + dset_t* dset = MALLOC(dset_t); assert (dset != NULL); dset->dset_id = H5Dopen(file->nucleus_group, @@ -280,7 +281,7 @@ typedef struct four_index_s { h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) { /* Allocate the data structure */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + h5nucleus_t* nucleus = MALLOC(h5nucleus_t); assert (nucleus != NULL); nucleus->num = 0; @@ -465,19 +466,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) { if (nucleus == NULL) return TREXIO_FAILURE; - if (nucleus->coord != NULL) free (nucleus->coord); - nucleus->coord = NULL; + if (nucleus->coord != NULL) + FREE (nucleus->coord); - if (nucleus->charge != NULL) free (nucleus->charge); - nucleus->charge = NULL; + if (nucleus->charge != NULL) + FREE (nucleus->charge); - if (nucleus->h5_coord != NULL) free (nucleus->h5_coord); - nucleus->h5_coord = NULL; + if (nucleus->h5_coord != NULL) + FREE (nucleus->h5_coord); - if (nucleus->h5_charge != NULL) free (nucleus->h5_charge); - nucleus->h5_charge = NULL; + if (nucleus->h5_charge != NULL) + FREE (nucleus->h5_charge); - free (nucleus); + FREE (nucleus); return TREXIO_SUCCESS; } @@ -607,17 +608,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co H5Dclose(dset_id); if (status < 0) { - free(ddims); + FREE(ddims); return TREXIO_FAILURE; } for (uint32_t i=0; inucleus_group, diff --git a/src/trexio_s.h b/src/trexio_s.h index 4810d10..315c90f 100644 --- a/src/trexio_s.h +++ b/src/trexio_s.h @@ -9,6 +9,7 @@ #define _TREXIO_S_H #include "trexio.h" +#include "trexio_private.h" #include #include diff --git a/src/trexio_text.c b/src/trexio_text.c index 6bd36c9..55019e5 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -36,7 +36,7 @@ trexio_exit_code trexio_text_init(trexio_t* file) { f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); assert (f->lock_file > 0); - free(file_name); + FREE(file_name); f->nucleus = NULL; f->electron= NULL; @@ -102,7 +102,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file->nucleus != NULL) return file->nucleus; /* Allocate the data structure */ - nucleus_t* nucleus = (nucleus_t*) malloc(sizeof(nucleus_t)); + nucleus_t* nucleus = MALLOC(nucleus_t); assert (nucleus != NULL); nucleus->file = NULL; @@ -130,7 +130,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -212,7 +212,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%lf", &(nucleus->coord[i])); assert (rc == 1); } - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -221,7 +221,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { } else { nucleus->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->nucleus = nucleus; return nucleus; } @@ -293,27 +293,22 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { } if (nucleus->dims_coord != NULL) { - free (nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE (nucleus->dims_coord); } if (nucleus->coord != NULL) { - free (nucleus->coord); - nucleus->coord = NULL; + FREE (nucleus->coord); } if (nucleus->dims_charge != NULL) { - free (nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE (nucleus->dims_charge); } if (nucleus->charge != NULL) { - free (nucleus->charge); - nucleus->charge = NULL; + FREE (nucleus->charge); } - free (nucleus); - file->nucleus = NULL; + FREE (nucleus); return TREXIO_SUCCESS; } @@ -380,13 +375,11 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->coord != NULL) { - free(nucleus->coord); - nucleus->coord = NULL; + FREE(nucleus->coord); } if (nucleus->dims_coord != NULL) { - free(nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE(nucleus->dims_coord); } nucleus->rank_coord = rank; @@ -442,13 +435,11 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->charge != NULL) { - free(nucleus->charge); - nucleus->charge = NULL; + FREE(nucleus->charge); } if (nucleus->dims_charge != NULL) { - free(nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE(nucleus->dims_charge); } nucleus->rank_charge = rank; @@ -477,7 +468,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->rdm != NULL) return file->rdm; /* Allocate the data structure */ - rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t)); + rdm_t* rdm = MALLOC(rdm_t); assert (rdm != NULL); rdm->one_e = NULL; @@ -502,7 +493,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -534,10 +525,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char)); + rdm->two_e_file_name = CALLOC (strlen(buffer),char); strcpy(rdm->two_e_file_name, buffer); - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -546,7 +537,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { } else { rdm->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->rdm = rdm ; return rdm; } @@ -600,13 +591,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { } if (rdm->one_e != NULL) { - free (rdm->one_e); - rdm->one_e = NULL; + FREE (rdm->one_e); } if (rdm->two_e_file_name != NULL) { - free (rdm->two_e_file_name); - rdm->two_e_file_name = NULL; + FREE (rdm->two_e_file_name); } free (rdm); diff --git a/src/trexio_text.h b/src/trexio_text.h index 806d180..13117fa 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -9,6 +9,7 @@ #define _TREXIO_TEXT_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include diff --git a/src/trexio_text.org b/src/trexio_text.org index 5104e4c..08be523 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -19,6 +19,7 @@ #define _TREXIO_TEXT_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include @@ -137,7 +138,7 @@ trexio_exit_code trexio_text_init(trexio_t* file) { f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); assert (f->lock_file > 0); - free(file_name); + FREE(file_name); f->nucleus = NULL; f->electron= NULL; @@ -234,7 +235,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file->nucleus != NULL) return file->nucleus; /* Allocate the data structure */ - nucleus_t* nucleus = (nucleus_t*) malloc(sizeof(nucleus_t)); + nucleus_t* nucleus = MALLOC(nucleus_t); assert (nucleus != NULL); nucleus->file = NULL; @@ -262,7 +263,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -344,7 +345,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%lf", &(nucleus->coord[i])); assert (rc == 1); } - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -353,7 +354,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { } else { nucleus->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->nucleus = nucleus; return nucleus; } @@ -443,27 +444,22 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { } if (nucleus->dims_coord != NULL) { - free (nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE (nucleus->dims_coord); } if (nucleus->coord != NULL) { - free (nucleus->coord); - nucleus->coord = NULL; + FREE (nucleus->coord); } if (nucleus->dims_charge != NULL) { - free (nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE (nucleus->dims_charge); } if (nucleus->charge != NULL) { - free (nucleus->charge); - nucleus->charge = NULL; + FREE (nucleus->charge); } - free (nucleus); - file->nucleus = NULL; + FREE (nucleus); return TREXIO_SUCCESS; } #+end_src @@ -550,13 +546,11 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->coord != NULL) { - free(nucleus->coord); - nucleus->coord = NULL; + FREE(nucleus->coord); } if (nucleus->dims_coord != NULL) { - free(nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE(nucleus->dims_coord); } nucleus->rank_coord = rank; @@ -622,13 +616,11 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->charge != NULL) { - free(nucleus->charge); - nucleus->charge = NULL; + FREE(nucleus->charge); } if (nucleus->dims_charge != NULL) { - free(nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE(nucleus->dims_charge); } nucleus->rank_charge = rank; @@ -666,7 +658,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->rdm != NULL) return file->rdm; /* Allocate the data structure */ - rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t)); + rdm_t* rdm = MALLOC(rdm_t); assert (rdm != NULL); rdm->one_e = NULL; @@ -691,7 +683,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -723,10 +715,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char)); + rdm->two_e_file_name = CALLOC (strlen(buffer),char); strcpy(rdm->two_e_file_name, buffer); - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -735,7 +727,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { } else { rdm->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->rdm = rdm ; return rdm; } @@ -807,13 +799,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { } if (rdm->one_e != NULL) { - free (rdm->one_e); - rdm->one_e = NULL; + FREE (rdm->one_e); } if (rdm->two_e_file_name != NULL) { - free (rdm->two_e_file_name); - rdm->two_e_file_name = NULL; + FREE (rdm->two_e_file_name); } free (rdm);