[pushed] Make the objfile destructor private

Message ID 20191212235008.BD9BE25BB4@gnutoolchain-gerrit.osci.io
State New, archived
Headers

Commit Message

Simon Marchi (Code Review) Dec. 12, 2019, 11:50 p.m. UTC
  The original change was created by Tom Tromey.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/494
......................................................................

Make the objfile destructor private

The idea behind this is that, in the long run, some code will need to
be able to hold onto an objfile after it is unlinked from the program
space.  In particular, this is needed for some functionality to be
moved to worker threads -- otherwise the objfile can be deleted while
still in use.

So, this makes ~objfile private, replacing it with an "unlink" method,
making it more obvious which operation is intended at the calling
points.

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* symfile.c (syms_from_objfile_1): Use objfile_up.
	(syms_from_objfile_1, remove_symbol_file_command): Call unlink
	method.
	(reread_symbols): Use objfile_up.
	* solib.c (update_solib_list, reload_shared_libraries_1): Call
	unlink method.
	* objfiles.h (struct objfile) <~objfile>: Now private.
	<unlink>: New method.
	(struct objfile_deleter): New.
	(objfile_up): New typedef.
	* objfiles.c (objfile::unlink): New method.
	(free_objfile_separate_debug, free_all_objfiles)
	(objfile_purge_solibs): Use it.
	* jit.c (jit_unregister_code): Remove.
	(jit_inferior_exit_hook, jit_event_handler): Call unlink on
	objfile.
	* compile/compile-object-run.c (do_module_cleanup): Call unlink on
	objfile.
	* compile/compile-object-load.c (compile_object_load): Use
	objfile_up.

Change-Id: I934bee70b26b8b24e1735828fb1e60fe8a05714f
---
M gdb/ChangeLog
M gdb/compile/compile-object-load.c
M gdb/compile/compile-object-run.c
M gdb/jit.c
M gdb/objfiles.c
M gdb/objfiles.h
M gdb/solib.c
M gdb/symfile.c
8 files changed, 66 insertions(+), 28 deletions(-)
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 7bcd2c7..07d679b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,28 @@ 
 2019-12-12  Tom Tromey  <tom@tromey.com>
 
+	* symfile.c (syms_from_objfile_1): Use objfile_up.
+	(syms_from_objfile_1, remove_symbol_file_command): Call unlink
+	method.
+	(reread_symbols): Use objfile_up.
+	* solib.c (update_solib_list, reload_shared_libraries_1): Call
+	unlink method.
+	* objfiles.h (struct objfile) <~objfile>: Now private.
+	<unlink>: New method.
+	(struct objfile_deleter): New.
+	(objfile_up): New typedef.
+	* objfiles.c (objfile::unlink): New method.
+	(free_objfile_separate_debug, free_all_objfiles)
+	(objfile_purge_solibs): Use it.
+	* jit.c (jit_unregister_code): Remove.
+	(jit_inferior_exit_hook, jit_event_handler): Call unlink on
+	objfile.
+	* compile/compile-object-run.c (do_module_cleanup): Call unlink on
+	objfile.
+	* compile/compile-object-load.c (compile_object_load): Use
+	objfile_up.
+
+2019-12-12  Tom Tromey  <tom@tromey.com>
+
 	* symfile.c (symbol_file_add_with_addrs): Pass "parent" to
 	objfile::make.
 	* objfiles.h (struct objjfile) <make>: No longer inline.
diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index c9bf151..c8762ad 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -632,9 +632,9 @@ 
 
   /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
      "Reading symbols from ..." message for automatically generated file.  */
-  std::unique_ptr<struct objfile> objfile_holder
-    (symbol_file_add_from_bfd (abfd.get (), filename.get (),
-			       0, NULL, 0, NULL));
+  objfile_up objfile_holder (symbol_file_add_from_bfd (abfd.get (),
+						       filename.get (),
+						       0, NULL, 0, NULL));
   objfile = objfile_holder.get ();
 
   func_sym = lookup_global_symbol_from_objfile (objfile,
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
index 8173cfe..72f3060 100644
--- a/gdb/compile/compile-object-run.c
+++ b/gdb/compile/compile-object-run.c
@@ -86,7 +86,7 @@ 
     if ((objfile->flags & OBJF_USERLOADED) == 0
         && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
       {
-	delete objfile;
+	objfile->unlink ();
 
 	/* It may be a bit too pervasive in this dummy_frame dtor callback.  */
 	clear_symtab_users (0);
diff --git a/gdb/jit.c b/gdb/jit.c
index 2018e2c..59da4e0 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -951,18 +951,6 @@ 
     jit_bfd_try_read_symtab (code_entry, entry_addr, gdbarch);
 }
 
-/* This function unregisters JITed code and frees the corresponding
-   objfile.  */
-
-static void
-jit_unregister_code (struct objfile *objfile)
-{
-  if (jit_debug)
-    fprintf_unfiltered (gdb_stdlog, "jit_unregister_code (%s)\n",
-			host_address_to_string (objfile));
-  delete objfile;
-}
-
 /* Look up the objfile with this code entry address.  */
 
 static struct objfile *
@@ -1380,7 +1368,7 @@ 
 	= (struct jit_objfile_data *) objfile_data (objf, jit_objfile_data);
 
       if (objf_data != NULL && objf_data->addr != 0)
-	jit_unregister_code (objf);
+	objf->unlink ();
     }
 }
 
@@ -1414,7 +1402,7 @@ 
 			     "entry at address: %s\n"),
 			   paddress (gdbarch, entry_addr));
       else
-        jit_unregister_code (objf);
+        objf->unlink ();
 
       break;
     default:
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 0ee5720..a635f77 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -553,6 +553,14 @@ 
   return result;
 }
 
+/* See objfiles.h.  */
+
+void
+objfile::unlink ()
+{
+  delete this;
+}
+
 /* Free all separate debug objfile of OBJFILE, but don't free OBJFILE
    itself.  */
 
@@ -564,7 +572,7 @@ 
   for (child = objfile->separate_debug_objfile; child;)
     {
       struct objfile *next_child = child->separate_debug_objfile_link;
-      delete child;
+      child->unlink ();
       child = next_child;
     }
 }
@@ -687,7 +695,7 @@ 
     gdb_assert (so->objfile == NULL);
 
   for (objfile *objfile : current_program_space->objfiles_safe ())
-    delete objfile;
+    objfile->unlink ();
   clear_symtab_users (0);
 }
 
@@ -996,7 +1004,7 @@ 
 	 be soon.  */
 
       if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
-	delete objf;
+	objf->unlink ();
     }
 }
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 663e639..49b4627 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -399,13 +399,18 @@ 
   /* The only way to create an objfile is to call objfile::make.  */
   objfile (bfd *, const char *, objfile_flags);
 
+  /* The only way to free an objfile is via 'unlink'.  */
+  ~objfile ();
+
 public:
 
   /* Create an objfile.  */
   static objfile *make (bfd *bfd_, const char *name_, objfile_flags flags_,
 			objfile *parent = nullptr);
 
-  ~objfile ();
+  /* Remove an objfile from the current program space, and free
+     it.  */
+  void unlink ();
 
   DISABLE_COPY_AND_ASSIGN (objfile);
 
@@ -637,6 +642,20 @@ 
   htab_up static_links;
 };
 
+/* A deleter for objfile.  */
+
+struct objfile_deleter
+{
+  void operator() (objfile *ptr) const
+  {
+    ptr->unlink ();
+  }
+};
+
+/* A unique pointer that holds an objfile.  */
+
+typedef std::unique_ptr<objfile, objfile_deleter> objfile_up;
+
 /* Declarations for functions defined in objfiles.c */
 
 extern struct gdbarch *get_objfile_arch (const struct objfile *);
diff --git a/gdb/solib.c b/gdb/solib.c
index 5483670..7eea732 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -835,7 +835,7 @@ 
 	  /* Unless the user loaded it explicitly, free SO's objfile.  */
 	  if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
 	      && !solib_used (gdb))
-	    delete gdb->objfile;
+	    gdb->objfile->unlink ();
 
 	  /* Some targets' section tables might be referring to
 	     sections from so->abfd; remove them.  */
@@ -1312,7 +1312,7 @@ 
 	{
 	  if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
 	      && !solib_used (so))
-	    delete so->objfile;
+	    so->objfile->unlink ();
 	  remove_target_sections (so);
 	  clear_so (so);
 	}
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 8e3cf7f..9652371 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -940,7 +940,7 @@ 
      if an error occurs during symbol reading.  */
   gdb::optional<clear_symtab_users_cleanup> defer_clear_users;
 
-  std::unique_ptr<struct objfile> objfile_holder (objfile);
+  objfile_up objfile_holder (objfile);
 
   /* If ADDRS is NULL, put together a dummy address list.
      We now establish the convention that an addr of zero means
@@ -958,7 +958,7 @@ 
 
       if (symfile_objfile != NULL)
 	{
-	  delete symfile_objfile;
+	  symfile_objfile->unlink ();
 	  gdb_assert (symfile_objfile == NULL);
 	}
 
@@ -2438,7 +2438,7 @@ 
 		 objfile_name (objf)))
     error (_("Not confirmed."));
 
-  delete objf;
+  objf->unlink ();
   clear_symtab_users (0);
 }
 
@@ -2495,7 +2495,7 @@ 
 	  /* If we get an error, blow away this objfile (not sure if
 	     that is the correct response for things like shared
 	     libraries).  */
-	  std::unique_ptr<struct objfile> objfile_holder (objfile);
+	  objfile_up objfile_holder (objfile);
 
 	  /* We need to do this whenever any symbols go away.  */
 	  clear_symtab_users_cleanup defer_clear_users (0);