[6/8] Add push_target overload

Message ID 20190207094016.368-7-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Feb. 7, 2019, 9:40 a.m. UTC
  From: Tom Tromey <tromey@adacore.com>

This adds a push_target overload that takes a "target_ops_up &&".
This removes some calls to release a target_ops_up, and makes the
intent here clearer.

gdb/ChangeLog
2019-02-07  Tom Tromey  <tromey@adacore.com>

	* target.h (push_target): Declare new overload.
	* target.c (push_target): New overload, taking an rvalue reference.
	* remote.c (remote_target::open_1): Use push_target overload.
	* corelow.c (core_target_open): Use push_target overload.
---
 gdb/ChangeLog | 7 +++++++
 gdb/corelow.c | 3 +--
 gdb/remote.c  | 4 +---
 gdb/target.c  | 9 +++++++++
 gdb/target.h  | 3 +++
 5 files changed, 21 insertions(+), 5 deletions(-)
  

Comments

Pedro Alves Feb. 7, 2019, 5:23 p.m. UTC | #1
On 02/07/2019 09:40 AM, Tom Tromey wrote:
> From: Tom Tromey <tromey@adacore.com>
> 
> This adds a push_target overload that takes a "target_ops_up &&".
> This removes some calls to release a target_ops_up, and makes the
> intent here clearer.

I like it.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/corelow.c b/gdb/corelow.c
index 52d6d95b3c0..6a29d6a2328 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -417,8 +417,7 @@  core_target_open (const char *arg, int from_tty)
   if (!exec_bfd)
     set_gdbarch_from_file (core_bfd);
 
-  push_target (target);
-  target_holder.release ();
+  push_target (std::move (target_holder));
 
   inferior_ptid = null_ptid;
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 18e678d07af..95201eeab5c 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -5547,9 +5547,7 @@  remote_target::open_1 (const char *name, int from_tty, int extended_p)
     }
 
   /* Switch to using the remote target now.  */
-  push_target (remote);
-  /* The target stack owns the target now.  */
-  target_holder.release ();
+  push_target (std::move (target_holder));
 
   /* Register extra event sources in the event loop.  */
   rs->remote_async_inferior_event_token
diff --git a/gdb/target.c b/gdb/target.c
index c1ab07f7608..116510e8cb8 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -585,6 +585,15 @@  push_target (struct target_ops *t)
   g_target_stack.push (t);
 }
 
+/* See target.h  */
+
+void
+push_target (target_ops_up &&t)
+{
+  g_target_stack.push (t.get ());
+  t.release ();
+}
+
 /* See target.h.  */
 
 int
diff --git a/gdb/target.h b/gdb/target.h
index 96413aa6c3a..c95151a4044 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -2333,6 +2333,9 @@  extern void add_deprecated_target_alias (const target_info &info,
 
 extern void push_target (struct target_ops *);
 
+/* An overload that deletes the target on failure.  */
+extern void push_target (target_ops_up &&);
+
 extern int unpush_target (struct target_ops *);
 
 extern void target_pre_inferior (int);