[RFA,3/3] Remvoe free_dwo_file_cleanup

Message ID 20180329152757.9346-4-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey March 29, 2018, 3:27 p.m. UTC
  This removes free_dwo_file_cleanup, the last cleanup in dwarf2read.c.
This is replaced with a unique_ptr; which, despite the fact that a
dwo_file is obstack-allocated, seemed like the best fit.

gdb/ChangeLog
2018-03-29  Tom Tromey  <tom@tromey.com>

	* dwarf2read.c (struct free_dwo_file_cleanup_data): Remove.
	(struct dwo_file_deleter): New.
	(dwo_file_up): New typedef.
	(open_and_init_dwo_file): Use dwo_file_up.
	(free_dwo_file_cleanup): Remove.
---
 gdb/ChangeLog    |  8 ++++++++
 gdb/dwarf2read.c | 49 +++++++++++++++++++------------------------------
 2 files changed, 27 insertions(+), 30 deletions(-)
  

Comments

Simon Marchi March 30, 2018, 4:29 p.m. UTC | #1
"Remvoe" in the title.

Otherwise LGTM.  That's a nice milestone!

Simon
  
Simon Marchi March 30, 2018, 4:31 p.m. UTC | #2
On 2018-03-30 12:29, Simon Marchi wrote:
> "Remvoe" in the title.
> 
> Otherwise LGTM.  That's a nice milestone!
> 
> Simon

To be clear, all three patches LGTM.

Simon
  

Patch

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 1ab073804d..fd544a7f9b 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -1959,14 +1959,22 @@  static struct dwo_unit *lookup_dwo_type_unit
 
 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
 
-static void free_dwo_file_cleanup (void *);
+static void free_dwo_file (struct dwo_file *);
 
-struct free_dwo_file_cleanup_data
+/* A unique_ptr helper to free a dwo_file.  */
+
+struct dwo_file_deleter
 {
-  struct dwo_file *dwo_file;
-  struct dwarf2_per_objfile *dwarf2_per_objfile;
+  void operator() (struct dwo_file *df) const
+  {
+    free_dwo_file (df);
+  }
 };
 
+/* A unique pointer to a dwo_file.  */
+
+typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
+
 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
 static void check_producer (struct dwarf2_cu *cu);
@@ -12983,8 +12991,6 @@  open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  struct dwo_file *dwo_file;
-  struct cleanup *cleanups;
 
   gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
   if (dbfd == NULL)
@@ -12993,32 +12999,28 @@  open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
 	fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
       return NULL;
     }
-  dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
+
+  /* We use a unique pointer here, despite the obstack allocation,
+     because a dwo_file needs some cleanup if it is abandoned.  */
+  dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
+					struct dwo_file));
   dwo_file->dwo_name = dwo_name;
   dwo_file->comp_dir = comp_dir;
   dwo_file->dbfd = dbfd.release ();
 
-  free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
-  cleanup_data->dwo_file = dwo_file;
-  cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
-
-  cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
-
   bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
 			 &dwo_file->sections);
 
   create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
 			 dwo_file->cus);
 
-  create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
+  create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
 				 dwo_file->sections.types, dwo_file->tus);
 
-  discard_cleanups (cleanups);
-
   if (dwarf_read_debug)
     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
 
-  return dwo_file;
+  return dwo_file.release ();
 }
 
 /* This function is mapped across the sections and remembers the offset and
@@ -13524,19 +13526,6 @@  free_dwo_file (struct dwo_file *dwo_file)
   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
 }
 
-/* Wrapper for free_dwo_file for use in cleanups.  */
-
-static void
-free_dwo_file_cleanup (void *arg)
-{
-  struct free_dwo_file_cleanup_data *data
-    = (struct free_dwo_file_cleanup_data *) arg;
-
-  free_dwo_file (data->dwo_file);
-
-  xfree (data);
-}
-
 /* Traversal function for free_dwo_files.  */
 
 static int