From 7fec997ecf710b459c2d2b2fcb195b2130c32369 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 27 Jan 2019 20:28:13 +0100 Subject: [PATCH] closure: initialize it once for all The memory allocated by 'closure' (and some data such as 'fderives') is used to computed a state's full itemset from its core. This is needed during the construction of the LR(0) automaton, and the memory is reclaimed immediately afterwards. Unfortunately the reports (graph, text, xml) also need this information when describing the states with their full itemsets. As a consequence the memory was allocated again, fderives computed again too, and more --trace reports are generated which only duplicate what was already reported. Stop that. It does mean that we release the memory later (hence the peak memory usage is higher now), but I don't think that's a problem today. * src/lr0.c (generate_states): Don't call closure_free. * src/state.c (states_free): Do it here. (for symmetry with closure_new which is called in generate_states). * src/print-graph.c, src/print-xml.c, src/print.c: You can now expect the closure module to be functional. --- src/lr0.c | 1 - src/main.c | 2 +- src/print-graph.c | 2 -- src/print-xml.c | 2 -- src/print.c | 6 ------ src/state.c | 5 ++++- 6 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/lr0.c b/src/lr0.c index 48ec30c5..a54f1e15 100644 --- a/src/lr0.c +++ b/src/lr0.c @@ -345,7 +345,6 @@ generate_states (void) } /* discard various storage */ - closure_free (); free_storage (); /* Set up STATES. */ diff --git a/src/main.c b/src/main.c index 065540db..7704bef4 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -30,7 +31,6 @@ #include /* relocate2 */ #include -#include "closeout.h" #include "complain.h" #include "conflicts.h" #include "derives.h" diff --git a/src/print-graph.c b/src/print-graph.c index a007ab7a..3e3b7230 100644 --- a/src/print-graph.c +++ b/src/print-graph.c @@ -184,10 +184,8 @@ print_graph (void) start_graph (fgraph); /* Output nodes and edges. */ - closure_new (nritems); for (int i = 0; i < nstates; i++) print_state (states[i], fgraph); - closure_free (); finish_graph (fgraph); xfclose (fgraph); diff --git a/src/print-xml.c b/src/print-xml.c index cbaec8c7..29339844 100644 --- a/src/print-xml.c +++ b/src/print-xml.c @@ -517,7 +517,6 @@ print_xml (void) /* print grammar */ print_grammar (out, level + 1); - closure_new (nritems); no_reduce_set = bitset_create (ntokens, BITSET_FIXED); /* print automaton */ @@ -531,7 +530,6 @@ print_xml (void) xml_puts (out, level + 1, ""); bitset_free (no_reduce_set); - closure_free (); xml_puts (out, 0, ""); diff --git a/src/print.c b/src/print.c index 356fd62a..42adb1d7 100644 --- a/src/print.c +++ b/src/print.c @@ -507,17 +507,11 @@ print_results (void) print_terminal_symbols (out); print_nonterminal_symbols (out); - /* If the whole state item sets, not only the kernels, are wanted, - 'closure' will be run, which needs memory allocation/deallocation. */ - if (report_flag & report_itemsets) - closure_new (nritems); /* Storage for print_reductions. */ no_reduce_set = bitset_create (ntokens, BITSET_FIXED); for (state_number i = 0; i < nstates; i++) print_state (out, states[i]); bitset_free (no_reduce_set); - if (report_flag & report_itemsets) - closure_free (); xfclose (out); } diff --git a/src/state.c b/src/state.c index 1b5bb396..18d3da8b 100644 --- a/src/state.c +++ b/src/state.c @@ -19,13 +19,15 @@ along with this program. If not, see . */ #include +#include "state.h" + #include "system.h" #include +#include "closure.h" #include "complain.h" #include "gram.h" -#include "state.h" #include "print-xml.h" @@ -443,6 +445,7 @@ state **states = NULL; void states_free (void) { + closure_free (); for (state_number i = 0; i < nstates; ++i) state_free (states[i]); free (states);