Various global data items relating to reading .md files are
currently initialized in rtx_reader::read_md_files, and are
not cleaned up.

The selftests for the RTL frontend require supporting multiple
reader instances being alive one after another in-process, so
this lack of cleanup would become a leak.

To fix this, this patch moves these initializations to the
rtx_reader constructor, and adds matching cleanups to the destructor,
along with a cleanup of m_base_dir.

gcc/ChangeLog:
        * read-md.c (rtx_reader::rtx_reader): Move initializations
        of various global data from rtx_reader::read_md_files to here.
        (rtx_reader::~rtx_reader): Clean up m_base_dir, and various
        global data.
        (rtx_reader::read_md_files): Move initializations of various
        global data from here to the ctor.
---
 gcc/read-md.c | 49 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 14 deletions(-)

diff --git a/gcc/read-md.c b/gcc/read-md.c
index e158be5..1a13916 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -925,12 +925,47 @@ rtx_reader::rtx_reader ()
 {
   /* Set the global singleton pointer.  */
   rtx_reader_ptr = this;
+
+  /* Initialize global data.  */
+  obstack_init (&string_obstack);
+  ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
+  obstack_init (&ptr_loc_obstack);
+  joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
+  obstack_init (&joined_conditions_obstack);
+  md_constants = htab_create (31, leading_string_hash,
+                             leading_string_eq_p, (htab_del) 0);
+  enum_types = htab_create (31, leading_string_hash,
+                           leading_string_eq_p, (htab_del) 0);
+
+  /* Unlock the stdio streams.  */
+  unlock_std_streams ();
 }
 
 /* rtx_reader's destructor.  */
 
 rtx_reader::~rtx_reader ()
 {
+  free (m_base_dir);
+
+  /* Clean up global data.  */
+  htab_delete (enum_types);
+  enum_types = NULL;
+
+  htab_delete (md_constants);
+  md_constants = NULL;
+
+  obstack_free (&joined_conditions_obstack, NULL);
+
+  htab_delete (joined_conditions);
+  joined_conditions = NULL;
+
+  obstack_free (&ptr_loc_obstack, NULL);
+
+  htab_delete (ptr_locs);
+  ptr_locs = NULL;
+
+  obstack_free (&string_obstack, NULL);
+
   /* Clear the global singleton pointer.  */
   rtx_reader_ptr = NULL;
 }
@@ -1105,20 +1140,6 @@ rtx_reader::read_md_files (int argc, const char **argv,
   bool already_read_stdin;
   int num_files;
 
-  /* Initialize global data.  */
-  obstack_init (&string_obstack);
-  ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
-  obstack_init (&ptr_loc_obstack);
-  joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
-  obstack_init (&joined_conditions_obstack);
-  md_constants = htab_create (31, leading_string_hash,
-                             leading_string_eq_p, (htab_del) 0);
-  enum_types = htab_create (31, leading_string_hash,
-                           leading_string_eq_p, (htab_del) 0);
-
-  /* Unlock the stdio streams.  */
-  unlock_std_streams ();
-
   /* First we loop over all the options.  */
   for (i = 1; i < argc; i++)
     if (argv[i][0] == '-')
-- 
1.8.5.3

Reply via email to