]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/progspace: link objfiles using owning_intrusive_list
authorSimon Marchi <simon.marchi@polymtl.ca>
Thu, 11 Jul 2024 16:07:00 +0000 (12:07 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 11 Nov 2024 16:28:24 +0000 (11:28 -0500)
This simplifies things a little bit, removing some `find_if` when
inserting or removing objfiles, and the whole
unwrapping_objfile_iterator thing.

Change-Id: Idd1851d36c7834820c9c1639a6a252de643eafba

gdb/objfiles.h
gdb/progspace.c
gdb/progspace.h
gdb/source.c

index d92570a00bdb168e5db1dda1a976e44c2c434f51..bd65e2bd03002c9bd2541f5bdba733f38e3f1b84 100644 (file)
@@ -418,7 +418,7 @@ struct obj_section
    symbols, lookup_symbol is used to check if we only have a partial
    symbol and if so, read and expand the full compunit.  */
 
-struct objfile
+struct objfile : intrusive_list_node<objfile>
 {
 private:
 
index 28198c1e8a43c5927802fe72f61ae2e666e742d3..94175d36c186b732877c3a6b8333bf361d01a971 100644 (file)
@@ -124,6 +124,15 @@ program_space::~program_space ()
 
 /* See progspace.h.  */
 
+bool
+program_space::multi_objfile_p () const
+{
+  return (!objfiles_list.empty ()
+         && std::next (objfiles_list.begin ()) != objfiles_list.end ());
+}
+
+/* See progspace.h.  */
+
 void
 program_space::free_all_objfiles ()
 {
@@ -132,7 +141,7 @@ program_space::free_all_objfiles ()
     gdb_assert (so.objfile == NULL);
 
   while (!objfiles_list.empty ())
-    objfiles_list.front ()->unlink ();
+    this->remove_objfile (&objfiles_list.front ());
 }
 
 /* See progspace.h.  */
@@ -145,13 +154,9 @@ program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
     objfiles_list.push_back (std::move (objfile));
   else
     {
-      auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
-                               [=] (const std::unique_ptr<::objfile> &objf)
-                               {
-                                 return objf.get () == before;
-                               });
-      gdb_assert (iter != objfiles_list.end ());
-      objfiles_list.insert (iter, std::move (objfile));
+      gdb_assert (before->is_linked ());
+      objfiles_list.insert (objfiles_list.iterator_to (*before),
+                           std::move (objfile));
     }
 }
 
@@ -166,16 +171,11 @@ program_space::remove_objfile (struct objfile *objfile)
      reference stale info.  */
   reinit_frame_cache ();
 
-  auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
-                           [=] (const std::unique_ptr<::objfile> &objf)
-                           {
-                             return objf.get () == objfile;
-                           });
-  gdb_assert (iter != objfiles_list.end ());
-  objfiles_list.erase (iter);
-
   if (objfile == symfile_object_file)
     symfile_object_file = NULL;
+
+  gdb_assert (objfile->is_linked ());
+  objfiles_list.erase (objfiles_list.iterator_to (*objfile));
 }
 
 /* See progspace.h.  */
index 999e7a39182e82cbf9b7aa46ecba5b292c3e6337..4f98b4576a52a419b861a3b2eb42d28e000c276b 100644 (file)
@@ -29,7 +29,6 @@
 #include "gdbsupport/intrusive_list.h"
 #include "gdbsupport/refcounted-object.h"
 #include "gdbsupport/gdb_ref_ptr.h"
-#include <list>
 #include <vector>
 
 struct target_ops;
@@ -41,8 +40,6 @@ struct address_space;
 struct program_space;
 struct solib;
 
-typedef std::list<std::unique_ptr<objfile>> objfile_list;
-
 /* An address space.  It is used for comparing if
    pspaces/inferior/threads see the same address space and for
    associating caches to each address space.  */
@@ -77,55 +74,6 @@ new_address_space ()
   return address_space_ref_ptr::new_reference (new address_space);
 }
 
-/* An iterator that wraps an iterator over std::unique_ptr<objfile>,
-   and dereferences the returned object.  This is useful for iterating
-   over a list of shared pointers and returning raw pointers -- which
-   helped avoid touching a lot of code when changing how objfiles are
-   managed.  */
-
-class unwrapping_objfile_iterator
-{
-public:
-
-  typedef unwrapping_objfile_iterator self_type;
-  typedef typename ::objfile *value_type;
-  typedef typename ::objfile &reference;
-  typedef typename ::objfile **pointer;
-  typedef typename objfile_list::iterator::iterator_category iterator_category;
-  typedef typename objfile_list::iterator::difference_type difference_type;
-
-  unwrapping_objfile_iterator (objfile_list::iterator iter)
-    : m_iter (std::move (iter))
-  {
-  }
-
-  objfile *operator* () const
-  {
-    return m_iter->get ();
-  }
-
-  unwrapping_objfile_iterator operator++ ()
-  {
-    ++m_iter;
-    return *this;
-  }
-
-  bool operator!= (const unwrapping_objfile_iterator &other) const
-  {
-    return m_iter != other.m_iter;
-  }
-
-private:
-
-  /* The underlying iterator.  */
-  objfile_list::iterator m_iter;
-};
-
-
-/* A range that returns unwrapping_objfile_iterators.  */
-
-using unwrapping_objfile_range = iterator_range<unwrapping_objfile_iterator>;
-
 /* A program space represents a symbolic view of an address space.
    Roughly speaking, it holds all the data associated with a
    non-running-yet program (main executable, main symbols), and when
@@ -235,7 +183,9 @@ struct program_space
      a program space.  */
   ~program_space ();
 
-  using objfiles_range = unwrapping_objfile_range;
+  using objfiles_iterator
+    = reference_to_pointer_iterator<intrusive_list<objfile>::iterator>;
+  using objfiles_range = iterator_range<objfiles_iterator>;
 
   /* Return an iterable object that can be used to iterate over all
      objfiles.  The basic use is in a foreach, like:
@@ -243,9 +193,7 @@ struct program_space
      for (objfile *objf : pspace->objfiles ()) { ... }  */
   objfiles_range objfiles ()
   {
-    return objfiles_range
-      (unwrapping_objfile_iterator (objfiles_list.begin ()),
-       unwrapping_objfile_iterator (objfiles_list.end ()));
+    return objfiles_range (objfiles_iterator (objfiles_list.begin ()));
   }
 
   using objfiles_safe_range = basic_safe_range<objfiles_range>;
@@ -260,9 +208,7 @@ struct program_space
   objfiles_safe_range objfiles_safe ()
   {
     return objfiles_safe_range
-      (objfiles_range
-        (unwrapping_objfile_iterator (objfiles_list.begin ()),
-         unwrapping_objfile_iterator (objfiles_list.end ())));
+      (objfiles_range (objfiles_iterator (objfiles_list.begin ())));
   }
 
   /* Add OBJFILE to the list of objfiles, putting it just before
@@ -276,10 +222,7 @@ struct program_space
 
   /* Return true if there is more than one object file loaded; false
      otherwise.  */
-  bool multi_objfile_p () const
-  {
-    return objfiles_list.size () > 1;
-  }
+  bool multi_objfile_p () const;
 
   /* Free all the objfiles associated with this program space.  */
   void free_all_objfiles ();
@@ -395,7 +338,7 @@ struct program_space
   struct objfile *symfile_object_file = NULL;
 
   /* All known objfiles are kept in a linked list.  */
-  std::list<std::unique_ptr<objfile>> objfiles_list;
+  owning_intrusive_list<objfile> objfiles_list;
 
   /* List of shared objects mapped into this space.  Managed by
      solib.c.  */
index 32099b9ea7d0cbfc6ebf7a476f0837ca7b748d38..3410e865cdf36de754a647826ac8e136cff8e92c 100644 (file)
@@ -27,6 +27,7 @@
 #include "value.h"
 #include "gdbsupport/filestuff.h"
 
+#include <list>
 #include <sys/types.h>
 #include <fcntl.h>
 #include "gdbcore.h"