From 1e84834a9c0993a3ec5229812c16479d41a99945 Mon Sep 17 00:00:00 2001 From: Robert Izzard <r.izzard@surrey.ac.uk> Date: Sun, 8 Sep 2019 13:11:02 +0100 Subject: [PATCH] cleanup the new ALPHA_ARGS: free memory, replace magic numbers with macros --- src/binary_c_macros.h | 15 +++++++++++++++ src/memory/free_tmpstore.c | 14 +++++++++++++- src/setup/parse_arguments.c | 24 ++++++++++++++---------- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/binary_c_macros.h b/src/binary_c_macros.h index c63056d94..a244e3d77 100644 --- a/src/binary_c_macros.h +++ b/src/binary_c_macros.h @@ -1008,5 +1008,20 @@ #define NOVA_TYPE_HYDROGEN 0 #define NOVA_TYPE_HELIUM 1 +/* + * Number of letters in the (English) alphabet + */ +#define NUMBER_OF_LETTERS_IN_THE_ALPHABET 26 + +/* + * Number of alphabetical cases + */ +#define NUMBER_OF_ALPHABETICAL_CASES 2 + +/* + * Alpha-args parameters list size (see parse_arguments.c) + */ +#define ALPHA_ARGS_SIZE \ + (NUMBER_OF_LETTERS_IN_THE_ALPHABET * NUMBER_OF_ALPHABETICAL_CASES) #endif /* BINARY_MACROS_H */ diff --git a/src/memory/free_tmpstore.c b/src/memory/free_tmpstore.c index 3aa7ceed9..8b629bbb9 100644 --- a/src/memory/free_tmpstore.c +++ b/src/memory/free_tmpstore.c @@ -71,7 +71,7 @@ void free_tmpstore(struct tmpstore_t * RESTRICT const tmpstore) #endif #ifdef WTTS_LOG { - int i; + unsigned int i; for(i=0;i<=1;i++) { Safe_fclose(tmpstore->fp_star[i]); @@ -92,6 +92,18 @@ void free_tmpstore(struct tmpstore_t * RESTRICT const tmpstore) Safe_free(tmpstore->rinterpolate_data); } +#ifdef ALPHA_ARGS + { + unsigned int i; + for(i=0;i < ALPHA_ARGS_SIZE;i++) + { + Safe_free(tmpstore->cmd_line_args_alpha[i]); + } + Safe_free(tmpstore->cmd_line_args_alpha); + Safe_free(tmpstore->cmd_line_args_alpha_count); + } +#endif + /* * Free the memory in tmpstore, but don't set it * to NULL diff --git a/src/setup/parse_arguments.c b/src/setup/parse_arguments.c index 1255761d9..6bf6eec5f 100644 --- a/src/setup/parse_arguments.c +++ b/src/setup/parse_arguments.c @@ -71,29 +71,31 @@ void parse_arguments(const int start, * between 0 and 51 (inclusive). */ tmpstore->cmd_line_args_alpha = - Malloc(sizeof(struct cmd_line_arg_t *)*26*2); + Malloc(sizeof(struct cmd_line_arg_t *)*ALPHA_ARGS_SIZE); /* * Set all counts to zero */ - tmpstore->cmd_line_args_alpha_count = Calloc(26*2,sizeof(unsigned int)); + tmpstore->cmd_line_args_alpha_count = + Calloc(ALPHA_ARGS_SIZE,sizeof(unsigned int)); unsigned int u; - for(u=0;u<2;u++) + for(u=0; u<NUMBER_OF_ALPHABETICAL_CASES; u++) { /* * u = 0 -> lower case * u = 1 -> upper case */ unsigned int a; - const unsigned int offset = u == 0 ? 97 : 65; - for(a=0;a<26;a++) + const unsigned int offset = + (const unsigned int) (u == 0 ? 'a' : 'A'); + for(a=0; a<NUMBER_OF_LETTERS_IN_THE_ALPHABET; a++) { /* * Hence the char that is the first letter */ - unsigned char c = offset + a; - const unsigned int index = a + u * 26; + const unsigned char c = offset + a; + const unsigned int index = a + u * NUMBER_OF_LETTERS_IN_THE_ALPHABET; Aprint("allocate alpha structure %d (char %d = %c = %c)\n", a, (int)c, @@ -207,9 +209,11 @@ void parse_arguments(const int start, /* * First character is a letter: use the alpha map */ - const unsigned int u = ASCII_lower_case(arg[0]) ? 0 : 1; - const unsigned int offset = u == 0 ? 97 : 65; - const unsigned int index = arg[0] - offset + 26 * u; + const unsigned int u = ASCII_lower_case(arg[0])==TRUE ? 0 : 1; + const unsigned int offset = + (const unsigned int) u == 0 ? 'a' : 'A'; + const unsigned int index = + arg[0] - offset + NUMBER_OF_LETTERS_IN_THE_ALPHABET * u; arg_count = stardata->tmpstore->cmd_line_args_alpha_count[index]; cmd_line_args = stardata->tmpstore->cmd_line_args_alpha[index]; -- GitLab