Make the objfile destructor private
authorTom Tromey <tom@tromey.com>
Fri, 1 Nov 2019 22:21:04 +0000 (16:21 -0600)
committerTom Tromey <tom@tromey.com>
Thu, 12 Dec 2019 22:50:52 +0000 (15:50 -0700)
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

gdb/ChangeLog
gdb/compile/compile-object-load.c
gdb/compile/compile-object-run.c
gdb/jit.c
gdb/objfiles.c
gdb/objfiles.h
gdb/solib.c
gdb/symfile.c

index 7bcd2c7fbd0d48b76bee8c136fad7b53f87f92f0..07d679b0edc86848cfcbde7536e631e8f75063ce 100644 (file)
@@ -1,3 +1,26 @@
+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
index c9bf1518372eead0d936288cf460d6265cb62e2c..c8762ad6595a956cf5b34c7bc5bb19c1507d272a 100644 (file)
@@ -632,9 +632,9 @@ compile_object_load (const compile_file_names &file_names,
 
   /* 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,
index 8173cfea6809a21366585a1473a05db4ae8298a0..72f3060243e8094901e0be206f38bd3ed1740968 100644 (file)
@@ -86,7 +86,7 @@ do_module_cleanup (void *arg, int registers_valid)
     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);
index 2018e2c6f0d2826354817b15f085735add479e81..59da4e0cee1d8c2e42d680f3ba3bb73ab3d86bd4 100644 (file)
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -951,18 +951,6 @@ jit_register_code (struct gdbarch *gdbarch,
     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 @@ jit_inferior_exit_hook (struct inferior *inf)
        = (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 @@ jit_event_handler (struct gdbarch *gdbarch)
                             "entry at address: %s\n"),
                           paddress (gdbarch, entry_addr));
       else
-        jit_unregister_code (objf);
+        objf->unlink ();
 
       break;
     default:
index 0ee5720cf67bc671359db45e42a020728b7be359..a635f7783e6e9e545ae141bb3292592040bc1306 100644 (file)
@@ -553,6 +553,14 @@ objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
   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 @@ free_objfile_separate_debug (struct objfile *objfile)
   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 @@ free_all_objfiles (void)
     gdb_assert (so->objfile == NULL);
 
   for (objfile *objfile : current_program_space->objfiles_safe ())
-    delete objfile;
+    objfile->unlink ();
   clear_symtab_users (0);
 }
 \f
@@ -996,7 +1004,7 @@ objfile_purge_solibs (void)
         be soon.  */
 
       if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
-       delete objf;
+       objf->unlink ();
     }
 }
 
index 663e6390dcd6f22e5874d99f1c509a243b82fc04..49b4627d67cc5e27370d24cbcc86fc369e15cae6 100644 (file)
@@ -399,13 +399,18 @@ private:
   /* 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 @@ public:
   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 *);
index 548367060a8a3f049adf836c3f59947a1ae06376..7eea732d61574cc9bb08b5da82c19d74011ae65e 100644 (file)
@@ -835,7 +835,7 @@ update_solib_list (int from_tty)
          /* 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 @@ reload_shared_libraries_1 (int from_tty)
        {
          if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
              && !solib_used (so))
-           delete so->objfile;
+           so->objfile->unlink ();
          remove_target_sections (so);
          clear_so (so);
        }
index 8e3cf7f23e57b8740edbd54dc6e22eb9d23c5abf..965237191ad197e8c173f07c62c052ead83b95df 100644 (file)
@@ -940,7 +940,7 @@ syms_from_objfile_1 (struct objfile *objfile,
      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 @@ syms_from_objfile_1 (struct objfile *objfile,
 
       if (symfile_objfile != NULL)
        {
-         delete symfile_objfile;
+         symfile_objfile->unlink ();
          gdb_assert (symfile_objfile == NULL);
        }
 
@@ -2438,7 +2438,7 @@ remove_symbol_file_command (const char *args, int from_tty)
                 objfile_name (objf)))
     error (_("Not confirmed."));
 
-  delete objf;
+  objf->unlink ();
   clear_symtab_users (0);
 }
 
@@ -2495,7 +2495,7 @@ reread_symbols (void)
          /* 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);
This page took 0.036102 seconds and 4 git commands to generate.