[2/2] Use unique_xmalloc_ptr for mi_parse::command

Message ID 20230609-mi-xfree-v1-2-04e9c8d9a679@adacore.com
State New
Headers
Series Remove some use of xfree from MI |

Commit Message

Tom Tromey June 9, 2023, 4:46 p.m. UTC
  This changes mi_parse::command to be a unique_xmalloc_ptr and fixes up
all the uses.  This avoids some manual memory management.  std::string
is not used here due to how the Python API works -- this approach
avoids an extra copy there.
---
 gdb/mi/mi-cmds.c      |  4 ++--
 gdb/mi/mi-main.c      |  9 +++++----
 gdb/mi/mi-parse.c     | 21 +++++++++------------
 gdb/mi/mi-parse.h     |  4 +++-
 gdb/python/py-micmd.c |  2 +-
 5 files changed, 20 insertions(+), 20 deletions(-)
  

Patch

diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index f8cae4131d8..5ea31fc98d1 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -52,10 +52,10 @@  struct mi_command_mi : public mi_command
     parse->parse_argv ();
 
     if (parse->argv == nullptr)
-      error (_("Problem parsing arguments: %s %s"), parse->command,
+      error (_("Problem parsing arguments: %s %s"), parse->command.get (),
 	     parse->args ());
 
-    this->m_argv_function (parse->command, parse->argv, parse->argc);
+    this->m_argv_function (parse->command.get (), parse->argv, parse->argc);
   }
 
 private:
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 7d671657a44..9108cf505c7 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1821,7 +1821,8 @@  captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
       if (mi_debug_p)
 	gdb_printf (gdb_stdlog,
 		    " token=`%s' command=`%s' args=`%s'\n",
-		    context->token.c_str (), context->command, context->args ());
+		    context->token.c_str (), context->command.get (),
+		    context->args ());
 
       mi_cmd_execute (context);
 
@@ -1836,7 +1837,7 @@  captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
 	  gdb_puts (context->token.c_str (), mi->raw_stdout);
 	  /* There's no particularly good reason why target-connect results
 	     in not ^done.  Should kill ^connected for MI3.  */
-	  gdb_puts (strcmp (context->command, "target-select") == 0
+	  gdb_puts (strcmp (context->command.get (), "target-select") == 0
 		    ? "^connected" : "^done", mi->raw_stdout);
 	  mi_out_put (uiout, mi->raw_stdout);
 	  mi_out_rewind (uiout);
@@ -1858,10 +1859,10 @@  captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
 	/* This "feature" will be removed as soon as we have a
 	   complete set of mi commands.  */
 	/* Echo the command on the console.  */
-	gdb_printf (gdb_stdlog, "%s\n", context->command);
+	gdb_printf (gdb_stdlog, "%s\n", context->command.get ());
 	/* Call the "console" interpreter.  */
 	argv[0] = INTERP_CONSOLE;
-	argv[1] = context->command;
+	argv[1] = context->command.get ();
 	mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
 
 	/* If we changed interpreters, DON'T print out anything.  */
diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c
index aceecad6db6..a9b9cdaf88f 100644
--- a/gdb/mi/mi-parse.c
+++ b/gdb/mi/mi-parse.c
@@ -215,7 +215,6 @@  mi_parse::parse_argv ()
 
 mi_parse::~mi_parse ()
 {
-  xfree (command);
   freeargv (argv);
 }
 
@@ -307,7 +306,7 @@  mi_parse::make (const char *cmd, std::string *token)
   if (*chp != '-')
     {
       chp = skip_spaces (chp);
-      parse->command = xstrdup (chp);
+      parse->command = make_unique_xstrdup (chp);
       parse->op = CLI_COMMAND;
 
       return parse;
@@ -319,16 +318,14 @@  mi_parse::make (const char *cmd, std::string *token)
 
     for (; *chp && !isspace (*chp); chp++)
       ;
-    parse->command = (char *) xmalloc (chp - tmp + 1);
-    memcpy (parse->command, tmp, chp - tmp);
-    parse->command[chp - tmp] = '\0';
+    parse->command = make_unique_xstrndup (tmp, chp - tmp);
   }
 
   /* Find the command in the MI table.  */
-  parse->cmd = mi_cmd_lookup (parse->command);
+  parse->cmd = mi_cmd_lookup (parse->command.get ());
   if (parse->cmd == NULL)
     throw_error (UNDEFINED_COMMAND_ERROR,
-		 _("Undefined MI command: %s"), parse->command);
+		 _("Undefined MI command: %s"), parse->command.get ());
 
   /* Skip white space following the command.  */
   chp = skip_spaces (chp);
@@ -418,19 +415,19 @@  mi_parse::make (gdb::unique_xmalloc_ptr<char> command,
 {
   std::unique_ptr<struct mi_parse> parse (new struct mi_parse);
 
-  parse->command = command.release ();
+  parse->command = std::move (command);
   parse->token = "";
 
-  if (parse->command[0] != '-')
+  if (parse->command.get ()[0] != '-')
     throw_error (UNDEFINED_COMMAND_ERROR,
 		 _("MI command '%s' does not start with '-'"),
-		 parse->command);
+		 parse->command.get ());
 
   /* Find the command in the MI table.  */
-  parse->cmd = mi_cmd_lookup (parse->command + 1);
+  parse->cmd = mi_cmd_lookup (parse->command.get () + 1);
   if (parse->cmd == NULL)
     throw_error (UNDEFINED_COMMAND_ERROR,
-		 _("Undefined MI command: %s"), parse->command);
+		 _("Undefined MI command: %s"), parse->command.get ());
 
   /* This over-allocates slightly, but it seems unimportant.  */
   parse->argv = XCNEWVEC (char *, args.size () + 1);
diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h
index 78fb414b4f0..c729e94c1f0 100644
--- a/gdb/mi/mi-parse.h
+++ b/gdb/mi/mi-parse.h
@@ -72,7 +72,9 @@  struct mi_parse
     const char *args ();
 
     enum mi_command_type op = MI_COMMAND;
-    char *command = nullptr;
+    /* This is not std::string because it avoids a copy in the Python
+       API case.  */
+    gdb::unique_xmalloc_ptr<char> command;
     std::string token;
     const struct mi_command *cmd = nullptr;
     struct mi_timestamp *cmd_start = nullptr;
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index 7027210d0d8..01fc6060ece 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -358,7 +358,7 @@  mi_command_py::invoke (struct mi_parse *parse) const
   parse->parse_argv ();
 
   if (parse->argv == nullptr)
-    error (_("Problem parsing arguments: %s %s"), parse->command,
+    error (_("Problem parsing arguments: %s %s"), parse->command.get (),
 	   parse->args ());