Skip to content

Commit

Permalink
refactor: Unify boolean arrays into 'char[]' type
Browse files Browse the repository at this point in the history
They were either 'int[]' or 'bool[]'. In C, it is suboptimal to use
'bool' types in arrays as the arrays would not be optimized for size or
speed.

Redefine the boolean array variables to use 'char' array instead. As we
are allocating them in 'char' array type, the macros
reallocate_bool_array() and allocate_bool_array() would draw confusion.
Replace the use of reallocate_bool_array() to reallocate_array().
  • Loading branch information
Explorer09 committed Apr 30, 2024
1 parent 3c97166 commit 025095c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
4 changes: 1 addition & 3 deletions src/ccl.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ int cclinit (void)
ccllen =
reallocate_integer_array (ccllen, current_maxccls);
cclng = reallocate_integer_array (cclng, current_maxccls);
ccl_has_nl =
reallocate_bool_array (ccl_has_nl,
current_maxccls);
ccl_has_nl = reallocate_array(ccl_has_nl, current_maxccls, sizeof(char));
}

if (lastccl == 1)
Expand Down
17 changes: 8 additions & 9 deletions src/flexdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,10 @@ extern int maximum_mns, current_mns, current_max_rules;
extern int num_rules, num_eof_rules, default_rule, lastnfa;
extern int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
extern int *accptnum, *assoc_rule, *state_type;
extern int *rule_type, *rule_linenum, *rule_useful;
extern bool *rule_has_nl, *ccl_has_nl;
extern int *rule_type, *rule_linenum;
/* rule_useful[], rule_has_nl[] and ccl_has_nl[] are boolean arrays,
* but allocated as char arrays for size. */
extern char *rule_useful, *rule_has_nl, *ccl_has_nl;
extern int nlch;
extern size_t footprint;

Expand Down Expand Up @@ -622,7 +624,10 @@ extern int tecfwd[CSIZE + 1], tecbck[CSIZE + 1];
* scname - start condition name
*/

extern int lastsc, *scset, *scbol, *scxclu, *sceof;
extern int lastsc, *scset, *scbol;
/* scxclu[] and sceof[] are boolean arrays, but allocated as char
* arrays for size. */
extern char *scxclu, *sceof;
extern int current_max_scs;
extern const char **scname;

Expand Down Expand Up @@ -722,12 +727,6 @@ void *reallocate_array(void *, int, size_t);
#define reallocate_integer_array(array,size) \
reallocate_array((void *) array, size, sizeof(int))

#define allocate_bool_array(size) \
allocate_array(size, sizeof(bool))

#define reallocate_bool_array(array,size) \
reallocate_array((void *) array, size, sizeof(bool))

#define allocate_int_ptr_array(size) \
allocate_array(size, sizeof(int *))

Expand Down
21 changes: 13 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ int maximum_mns, current_mns, current_max_rules;
int num_rules, num_eof_rules, default_rule, lastnfa;
int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
int *accptnum, *assoc_rule, *state_type;
int *rule_type, *rule_linenum, *rule_useful;
int *rule_type, *rule_linenum;
int current_state_type;
bool variable_trailing_context_rules;
int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs,
tecfwd[CSIZE + 1];
int tecbck[CSIZE + 1];
int lastsc, *scset, *scbol, *scxclu, *sceof;
int lastsc, *scset, *scbol;
/* scxclu[] and sceof[] are boolean arrays, but allocated as char
* arrays for size. */
char *scxclu, *sceof;
int current_max_scs;
const char **scname;
int current_max_dfa_size, current_max_xpairs;
Expand All @@ -92,7 +95,9 @@ int end_of_buffer_state;
char **input_files;
int num_input_files;
jmp_buf flex_main_jmp_buf;
bool *rule_has_nl, *ccl_has_nl;
/* rule_useful[], rule_has_nl[] and ccl_has_nl[] are boolean arrays,
* but allocated as char arrays for size. */
char *rule_useful, *rule_has_nl, *ccl_has_nl;
int nlch = '\n';

bool tablesext, tablesverify, gentables;
Expand Down Expand Up @@ -1691,21 +1696,21 @@ void set_up_initial_allocations (void)
current_max_rules = INITIAL_MAX_RULES;
rule_type = allocate_integer_array (current_max_rules);
rule_linenum = allocate_integer_array (current_max_rules);
rule_useful = allocate_integer_array (current_max_rules);
rule_has_nl = allocate_bool_array (current_max_rules);
rule_useful = allocate_array(current_max_rules, sizeof(char));
rule_has_nl = allocate_array(current_max_rules, sizeof(char));

current_max_scs = INITIAL_MAX_SCS;
scset = allocate_integer_array (current_max_scs);
scbol = allocate_integer_array (current_max_scs);
scxclu = allocate_integer_array (current_max_scs);
sceof = allocate_integer_array (current_max_scs);
scxclu = allocate_array(current_max_scs, sizeof(char));
sceof = allocate_array(current_max_scs, sizeof(char));
scname = allocate_char_ptr_array (current_max_scs);

current_maxccls = INITIAL_MAX_CCLS;
cclmap = allocate_integer_array (current_maxccls);
ccllen = allocate_integer_array (current_maxccls);
cclng = allocate_integer_array (current_maxccls);
ccl_has_nl = allocate_bool_array (current_maxccls);
ccl_has_nl = allocate_array(current_maxccls, sizeof(char));

current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
ccltbl = allocate_Character_array (current_max_ccl_tbl_size);
Expand Down
8 changes: 4 additions & 4 deletions src/nfa.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ void new_rule (void)
current_max_rules);
rule_linenum = reallocate_integer_array (rule_linenum,
current_max_rules);
rule_useful = reallocate_integer_array (rule_useful,
current_max_rules);
rule_has_nl = reallocate_bool_array (rule_has_nl,
current_max_rules);
rule_useful = reallocate_array(rule_useful,
current_max_rules, sizeof(char));
rule_has_nl = reallocate_array(rule_has_nl,
current_max_rules, sizeof(char));
}

if (num_rules > MAX_RULE)
Expand Down
5 changes: 3 additions & 2 deletions src/sym.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ void scextend (void)

scset = reallocate_integer_array (scset, current_max_scs);
scbol = reallocate_integer_array (scbol, current_max_scs);
scxclu = reallocate_integer_array (scxclu, current_max_scs);
sceof = reallocate_integer_array (sceof, current_max_scs);
scxclu = reallocate_array(scxclu, current_max_scs,
sizeof(char));
sceof = reallocate_array(sceof, current_max_scs, sizeof(char));
scname = reallocate_char_ptr_array (scname, current_max_scs);
}

Expand Down

0 comments on commit 025095c

Please sign in to comment.