* [PATCH] Use unique_ptr for objfiles
@ 2022-05-30 19:14 Tom Tromey
2022-05-31 14:27 ` Bruno Larsen
2022-05-31 14:44 ` Andrew Burgess
0 siblings, 2 replies; 3+ messages in thread
From: Tom Tromey @ 2022-05-30 19:14 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
A while back, I changed objfiles to be held via a shared_ptr. The
idea at the time was that this was a step toward writing to the index
cache in the background, and this would let gdb keep a reference alive
to do so. However, since then we've rewritten the DWARF reader, and
the new index can do this without requiring a shared pointer -- in
fact there are patches pending to implement this.
This patch switches objfile management to unique_ptr, which makes more
sense now.
Regression tested on x86-64 Fedora 34.
---
gdb/objfiles.c | 4 +---
gdb/objfiles.h | 2 +-
gdb/progspace.c | 6 +++---
gdb/progspace.h | 8 ++++----
4 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 80f68fda1c1..60d8aa5cb78 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -468,9 +468,7 @@ objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
if (parent != nullptr)
add_separate_debug_objfile (result, parent);
- /* Using std::make_shared might be a bit nicer here, but that would
- require making the constructor public. */
- current_program_space->add_objfile (std::shared_ptr<objfile> (result),
+ current_program_space->add_objfile (std::unique_ptr<objfile> (result),
parent);
/* Rebuild section map next time we need it. */
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 9da12ff12e0..a7098b46279 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -409,7 +409,7 @@ struct objfile
remove it from the program space's list. In some cases, you may
need to hold a reference to an objfile that is independent of its
existence on the program space's list; for this case, the
- destructor must be public so that shared_ptr can reference
+ destructor must be public so that unique_ptr can reference
it. */
~objfile ();
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 1ee4fe3f940..8735343fabc 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -174,7 +174,7 @@ program_space::free_all_objfiles ()
/* See progspace.h. */
void
-program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
+program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
struct objfile *before)
{
if (before == nullptr)
@@ -182,7 +182,7 @@ program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
else
{
auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
- [=] (const std::shared_ptr<::objfile> &objf)
+ [=] (const std::unique_ptr<::objfile> &objf)
{
return objf.get () == before;
});
@@ -203,7 +203,7 @@ program_space::remove_objfile (struct objfile *objfile)
reinit_frame_cache ();
auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
- [=] (const std::shared_ptr<::objfile> &objf)
+ [=] (const std::unique_ptr<::objfile> &objf)
{
return objf.get () == objfile;
});
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 73beb7a4710..662e569488e 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -41,9 +41,9 @@ struct program_space_data;
struct address_space_data;
struct so_list;
-typedef std::list<std::shared_ptr<objfile>> objfile_list;
+typedef std::list<std::unique_ptr<objfile>> objfile_list;
-/* An iterator that wraps an iterator over std::shared_ptr<objfile>,
+/* 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
@@ -234,7 +234,7 @@ struct program_space
/* Add OBJFILE to the list of objfiles, putting it just before
BEFORE. If BEFORE is nullptr, it will go at the end of the
list. */
- void add_objfile (std::shared_ptr<objfile> &&objfile,
+ void add_objfile (std::unique_ptr<objfile> &&objfile,
struct objfile *before);
/* Remove OBJFILE from the list of objfiles. */
@@ -354,7 +354,7 @@ struct program_space
struct objfile *symfile_object_file = NULL;
/* All known objfiles are kept in a linked list. */
- std::list<std::shared_ptr<objfile>> objfiles_list;
+ std::list<std::unique_ptr<objfile>> objfiles_list;
/* List of shared objects mapped into this space. Managed by
solib.c. */
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Use unique_ptr for objfiles
2022-05-30 19:14 [PATCH] Use unique_ptr for objfiles Tom Tromey
@ 2022-05-31 14:27 ` Bruno Larsen
2022-05-31 14:44 ` Andrew Burgess
1 sibling, 0 replies; 3+ messages in thread
From: Bruno Larsen @ 2022-05-31 14:27 UTC (permalink / raw)
To: Tom Tromey, gdb-patches
On 5/30/22 16:14, Tom Tromey wrote:
> A while back, I changed objfiles to be held via a shared_ptr. The
> idea at the time was that this was a step toward writing to the index
> cache in the background, and this would let gdb keep a reference alive
> to do so. However, since then we've rewritten the DWARF reader, and
> the new index can do this without requiring a shared pointer -- in
> fact there are patches pending to implement this.
>
> This patch switches objfile management to unique_ptr, which makes more
> sense now.
>
This patch seems obvious enough. It seems fine for me, I'd suggest you approve your patch.
> Regression tested on x86-64 Fedora 34.
Also tested on x86 fedora 35 and using clang. No regressions here either.
Cheers!
Bruno Larsen
> ---
> gdb/objfiles.c | 4 +---
> gdb/objfiles.h | 2 +-
> gdb/progspace.c | 6 +++---
> gdb/progspace.h | 8 ++++----
> 4 files changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/gdb/objfiles.c b/gdb/objfiles.c
> index 80f68fda1c1..60d8aa5cb78 100644
> --- a/gdb/objfiles.c
> +++ b/gdb/objfiles.c
> @@ -468,9 +468,7 @@ objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
> if (parent != nullptr)
> add_separate_debug_objfile (result, parent);
>
> - /* Using std::make_shared might be a bit nicer here, but that would
> - require making the constructor public. */
> - current_program_space->add_objfile (std::shared_ptr<objfile> (result),
> + current_program_space->add_objfile (std::unique_ptr<objfile> (result),
> parent);
>
> /* Rebuild section map next time we need it. */
> diff --git a/gdb/objfiles.h b/gdb/objfiles.h
> index 9da12ff12e0..a7098b46279 100644
> --- a/gdb/objfiles.h
> +++ b/gdb/objfiles.h
> @@ -409,7 +409,7 @@ struct objfile
> remove it from the program space's list. In some cases, you may
> need to hold a reference to an objfile that is independent of its
> existence on the program space's list; for this case, the
> - destructor must be public so that shared_ptr can reference
> + destructor must be public so that unique_ptr can reference
> it. */
> ~objfile ();
>
> diff --git a/gdb/progspace.c b/gdb/progspace.c
> index 1ee4fe3f940..8735343fabc 100644
> --- a/gdb/progspace.c
> +++ b/gdb/progspace.c
> @@ -174,7 +174,7 @@ program_space::free_all_objfiles ()
> /* See progspace.h. */
>
> void
> -program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
> +program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
> struct objfile *before)
> {
> if (before == nullptr)
> @@ -182,7 +182,7 @@ program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
> else
> {
> auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
> - [=] (const std::shared_ptr<::objfile> &objf)
> + [=] (const std::unique_ptr<::objfile> &objf)
> {
> return objf.get () == before;
> });
> @@ -203,7 +203,7 @@ program_space::remove_objfile (struct objfile *objfile)
> reinit_frame_cache ();
>
> auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
> - [=] (const std::shared_ptr<::objfile> &objf)
> + [=] (const std::unique_ptr<::objfile> &objf)
> {
> return objf.get () == objfile;
> });
> diff --git a/gdb/progspace.h b/gdb/progspace.h
> index 73beb7a4710..662e569488e 100644
> --- a/gdb/progspace.h
> +++ b/gdb/progspace.h
> @@ -41,9 +41,9 @@ struct program_space_data;
> struct address_space_data;
> struct so_list;
>
> -typedef std::list<std::shared_ptr<objfile>> objfile_list;
> +typedef std::list<std::unique_ptr<objfile>> objfile_list;
>
> -/* An iterator that wraps an iterator over std::shared_ptr<objfile>,
> +/* 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
> @@ -234,7 +234,7 @@ struct program_space
> /* Add OBJFILE to the list of objfiles, putting it just before
> BEFORE. If BEFORE is nullptr, it will go at the end of the
> list. */
> - void add_objfile (std::shared_ptr<objfile> &&objfile,
> + void add_objfile (std::unique_ptr<objfile> &&objfile,
> struct objfile *before);
>
> /* Remove OBJFILE from the list of objfiles. */
> @@ -354,7 +354,7 @@ struct program_space
> struct objfile *symfile_object_file = NULL;
>
> /* All known objfiles are kept in a linked list. */
> - std::list<std::shared_ptr<objfile>> objfiles_list;
> + std::list<std::unique_ptr<objfile>> objfiles_list;
>
> /* List of shared objects mapped into this space. Managed by
> solib.c. */
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Use unique_ptr for objfiles
2022-05-30 19:14 [PATCH] Use unique_ptr for objfiles Tom Tromey
2022-05-31 14:27 ` Bruno Larsen
@ 2022-05-31 14:44 ` Andrew Burgess
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2022-05-31 14:44 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
* Tom Tromey <tom@tromey.com> [2022-05-30 13:14:21 -0600]:
> A while back, I changed objfiles to be held via a shared_ptr. The
> idea at the time was that this was a step toward writing to the index
> cache in the background, and this would let gdb keep a reference alive
> to do so. However, since then we've rewritten the DWARF reader, and
> the new index can do this without requiring a shared pointer -- in
> fact there are patches pending to implement this.
>
> This patch switches objfile management to unique_ptr, which makes more
> sense now.
LGTM.
Thanks,
Andrew
>
> Regression tested on x86-64 Fedora 34.
> ---
> gdb/objfiles.c | 4 +---
> gdb/objfiles.h | 2 +-
> gdb/progspace.c | 6 +++---
> gdb/progspace.h | 8 ++++----
> 4 files changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/gdb/objfiles.c b/gdb/objfiles.c
> index 80f68fda1c1..60d8aa5cb78 100644
> --- a/gdb/objfiles.c
> +++ b/gdb/objfiles.c
> @@ -468,9 +468,7 @@ objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
> if (parent != nullptr)
> add_separate_debug_objfile (result, parent);
>
> - /* Using std::make_shared might be a bit nicer here, but that would
> - require making the constructor public. */
> - current_program_space->add_objfile (std::shared_ptr<objfile> (result),
> + current_program_space->add_objfile (std::unique_ptr<objfile> (result),
> parent);
>
> /* Rebuild section map next time we need it. */
> diff --git a/gdb/objfiles.h b/gdb/objfiles.h
> index 9da12ff12e0..a7098b46279 100644
> --- a/gdb/objfiles.h
> +++ b/gdb/objfiles.h
> @@ -409,7 +409,7 @@ struct objfile
> remove it from the program space's list. In some cases, you may
> need to hold a reference to an objfile that is independent of its
> existence on the program space's list; for this case, the
> - destructor must be public so that shared_ptr can reference
> + destructor must be public so that unique_ptr can reference
> it. */
> ~objfile ();
>
> diff --git a/gdb/progspace.c b/gdb/progspace.c
> index 1ee4fe3f940..8735343fabc 100644
> --- a/gdb/progspace.c
> +++ b/gdb/progspace.c
> @@ -174,7 +174,7 @@ program_space::free_all_objfiles ()
> /* See progspace.h. */
>
> void
> -program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
> +program_space::add_objfile (std::unique_ptr<objfile> &&objfile,
> struct objfile *before)
> {
> if (before == nullptr)
> @@ -182,7 +182,7 @@ program_space::add_objfile (std::shared_ptr<objfile> &&objfile,
> else
> {
> auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
> - [=] (const std::shared_ptr<::objfile> &objf)
> + [=] (const std::unique_ptr<::objfile> &objf)
> {
> return objf.get () == before;
> });
> @@ -203,7 +203,7 @@ program_space::remove_objfile (struct objfile *objfile)
> reinit_frame_cache ();
>
> auto iter = std::find_if (objfiles_list.begin (), objfiles_list.end (),
> - [=] (const std::shared_ptr<::objfile> &objf)
> + [=] (const std::unique_ptr<::objfile> &objf)
> {
> return objf.get () == objfile;
> });
> diff --git a/gdb/progspace.h b/gdb/progspace.h
> index 73beb7a4710..662e569488e 100644
> --- a/gdb/progspace.h
> +++ b/gdb/progspace.h
> @@ -41,9 +41,9 @@ struct program_space_data;
> struct address_space_data;
> struct so_list;
>
> -typedef std::list<std::shared_ptr<objfile>> objfile_list;
> +typedef std::list<std::unique_ptr<objfile>> objfile_list;
>
> -/* An iterator that wraps an iterator over std::shared_ptr<objfile>,
> +/* 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
> @@ -234,7 +234,7 @@ struct program_space
> /* Add OBJFILE to the list of objfiles, putting it just before
> BEFORE. If BEFORE is nullptr, it will go at the end of the
> list. */
> - void add_objfile (std::shared_ptr<objfile> &&objfile,
> + void add_objfile (std::unique_ptr<objfile> &&objfile,
> struct objfile *before);
>
> /* Remove OBJFILE from the list of objfiles. */
> @@ -354,7 +354,7 @@ struct program_space
> struct objfile *symfile_object_file = NULL;
>
> /* All known objfiles are kept in a linked list. */
> - std::list<std::shared_ptr<objfile>> objfiles_list;
> + std::list<std::unique_ptr<objfile>> objfiles_list;
>
> /* List of shared objects mapped into this space. Managed by
> solib.c. */
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-05-31 14:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-30 19:14 [PATCH] Use unique_ptr for objfiles Tom Tromey
2022-05-31 14:27 ` Bruno Larsen
2022-05-31 14:44 ` Andrew Burgess
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).