public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/3] Use std::string in do_set_command
Date: Mon, 19 Jun 2023 11:51:24 -0600	[thread overview]
Message-ID: <20230619-remove-some-realloc-v1-3-8bee74d5b934@tromey.com> (raw)
In-Reply-To: <20230619-remove-some-realloc-v1-0-8bee74d5b934@tromey.com>

do_set_command manually updates a string, only to copy it to a
std::string and free the working copy.  This patch changes this code
to use std::string for everything, simplifying the code and removing a
copy.
---
 gdb/cli/cli-setshow.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c
index ecb739b94e9..c7bbac1666d 100644
--- a/gdb/cli/cli-setshow.c
+++ b/gdb/cli/cli-setshow.c
@@ -340,14 +340,12 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
     {
     case var_string:
       {
-	char *newobj;
+	std::string newobj;
 	const char *p;
-	char *q;
 	int ch;
 
-	newobj = (char *) xmalloc (strlen (arg) + 2);
+	newobj.reserve (strlen (arg));
 	p = arg;
-	q = newobj;
 	while ((ch = *p++) != '\000')
 	  {
 	    if (ch == '\\')
@@ -365,20 +363,14 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
 		if (ch == 0)
 		  break;	/* C loses */
 		else if (ch > 0)
-		  *q++ = ch;
+		  newobj.push_back (ch);
 	      }
 	    else
-	      *q++ = ch;
+	      newobj.push_back (ch);
 	  }
-#if 0
-	if (*(p - 1) != '\\')
-	  *q++ = ' ';
-#endif
-	*q++ = '\0';
-	newobj = (char *) xrealloc (newobj, q - newobj);
-
-	option_changed = c->var->set<std::string> (std::string (newobj));
-	xfree (newobj);
+	newobj.shrink_to_fit ();
+
+	option_changed = c->var->set<std::string> (std::move (newobj));
       }
       break;
     case var_string_noescape:

-- 
2.39.2


  parent reply	other threads:[~2023-06-19 17:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19 17:51 [PATCH 0/3] Remove some uses of xrealloc Tom Tromey
2023-06-19 17:51 ` [PATCH 1/3] Use std::string in linux-osdata.c Tom Tromey
2023-06-19 17:51 ` [PATCH 2/3] Use byte_vector in remote.c:readahead_cache Tom Tromey
2023-06-19 17:51 ` Tom Tromey [this message]
2023-06-19 18:27 ` [PATCH 0/3] Remove some uses of xrealloc John Baldwin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230619-remove-some-realloc-v1-3-8bee74d5b934@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).