[RFA,14/23] Use unique_xmalloc_ptr in jit.c

Message ID 20170503224626.2818-15-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey May 3, 2017, 10:46 p.m. UTC
  This removes some cleanups from jit.c by using unique_xmalloc_ptr
instead.

2017-05-02  Tom Tromey  <tom@tromey.com>

	* jit.c (jit_reader_load_command): Use unique_xmalloc_ptr.
---
 gdb/ChangeLog |  4 ++++
 gdb/jit.c     | 20 +++++++-------------
 2 files changed, 11 insertions(+), 13 deletions(-)
  

Comments

Pedro Alves June 2, 2017, 6:42 p.m. UTC | #1
On 05/03/2017 11:46 PM, Tom Tromey wrote:
>  static void
>  jit_reader_load_command (char *args, int from_tty)
>  {
> -  char *so_name;
> -  struct cleanup *prev_cleanup;
> -
>    if (args == NULL)
>      error (_("No reader name provided."));
> -  args = tilde_expand (args);
> -  prev_cleanup = make_cleanup (xfree, args);
> +  gdb::unique_xmalloc_ptr<char> file (tilde_expand (args));
>  
>    if (loaded_jit_reader != NULL)
>      error (_("JIT reader already loaded.  Run jit-reader-unload first."));
>  
> -  if (IS_ABSOLUTE_PATH (args))
> -    so_name = args;
> +  gdb::unique_xmalloc_ptr<char> so_name;
> +  if (IS_ABSOLUTE_PATH (file.get ()))
> +    so_name = std::move (file);

I think we don't really need two unique pointers, and then
moving.  I.e., this should do, I think:

  if (loaded_jit_reader != NULL)
    error (_("JIT reader already loaded.  Run jit-reader-unload first."));

  gdb::unique_xmalloc_ptr<char> so_name (tilde_expand (args));
  if (!IS_ABSOLUTE_PATH (so_name.get ()))
    so_name.reset (xstrprintf ("%s%s%s", jit_reader_dir, SLASH_STRING,
			       so_name.get ()));

Thanks,
Pedro Alves
  
Tom Tromey June 5, 2017, 1:09 p.m. UTC | #2
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> I think we don't really need two unique pointers, and then
Pedro> moving.

I made this change.

Tom
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index daa7dae..03e733b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@ 
 2017-05-02  Tom Tromey  <tom@tromey.com>
 
+	* jit.c (jit_reader_load_command): Use unique_xmalloc_ptr.
+
+2017-05-02  Tom Tromey  <tom@tromey.com>
+
 	* tui/tui-regs.c (tui_restore_gdbout): Remove.
 	(tui_register_format): Use scoped_restore.
 
diff --git a/gdb/jit.c b/gdb/jit.c
index ddf1005..00975df 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -211,29 +211,23 @@  jit_reader_load (const char *file_name)
 static void
 jit_reader_load_command (char *args, int from_tty)
 {
-  char *so_name;
-  struct cleanup *prev_cleanup;
-
   if (args == NULL)
     error (_("No reader name provided."));
-  args = tilde_expand (args);
-  prev_cleanup = make_cleanup (xfree, args);
+  gdb::unique_xmalloc_ptr<char> file (tilde_expand (args));
 
   if (loaded_jit_reader != NULL)
     error (_("JIT reader already loaded.  Run jit-reader-unload first."));
 
-  if (IS_ABSOLUTE_PATH (args))
-    so_name = args;
+  gdb::unique_xmalloc_ptr<char> so_name;
+  if (IS_ABSOLUTE_PATH (file.get ()))
+    so_name = std::move (file);
   else
-    {
-      so_name = xstrprintf ("%s%s%s", jit_reader_dir, SLASH_STRING, args);
-      make_cleanup (xfree, so_name);
-    }
+    so_name.reset (xstrprintf ("%s%s%s", jit_reader_dir, SLASH_STRING,
+			       file.get ()));
 
-  loaded_jit_reader = jit_reader_load (so_name);
+  loaded_jit_reader = jit_reader_load (so_name.get ());
   reinit_frame_cache ();
   jit_inferior_created_hook ();
-  do_cleanups (prev_cleanup);
 }
 
 /* Provides the jit-reader-unload command.  */