gdb/progspace: link objfiles using owning_intrusive_list

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
This commit is contained in:
Simon Marchi 2024-07-11 12:07:00 -04:00 committed by Simon Marchi
parent 427cc3b541
commit fa15972b68
4 changed files with 25 additions and 81 deletions

View 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:

View 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. */

View 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. */

View File

@ -27,6 +27,7 @@
#include "value.h"
#include "gdbsupport/filestuff.h"
#include <list>
#include <sys/types.h>
#include <fcntl.h>
#include "gdbcore.h"