Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 2/3] Don't make copies when calling find_cmd
Date: Thu, 22 Jan 2026 13:49:12 -0700	[thread overview]
Message-ID: <20260122-command-no-alloca-v1-2-402aa429bbbf@adacore.com> (raw)
In-Reply-To: <20260122-command-no-alloca-v1-0-402aa429bbbf@adacore.com>

Currently both callers of find_cmd make a temporary string -- one with
std::string and one with the dreaded alloca.

However, as the previous change to find_cmd points out, these copies
are not needed.  Remove them and use a string_view instead.
---
 gdb/cli/cli-decode.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index c9bd610a3ac..f581f128a34 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -2258,7 +2258,6 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
 	      struct cmd_list_element **result_list, std::string *default_args,
 	      int ignore_help_classes, bool lookup_for_completion_p)
 {
-  char *command;
   int len, nfound;
   struct cmd_list_element *found, *c;
   bool found_alias = false;
@@ -2273,18 +2272,11 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
   if (len == 0)
     return 0;
 
-  /* *text and p now bracket the first command word to lookup (and
-     it's length is len).  We copy this into a local temporary.  */
-
-
-  command = (char *) alloca (len + 1);
-  memcpy (command, *text, len);
-  command[len] = '\0';
-
-  /* Look it up.  */
+  /* *TEXT is the first command word to lookup (and its length is
+     LEN).  Look it up.  */
   found = 0;
   nfound = 0;
-  found = find_cmd (std::string_view (command, len),
+  found = find_cmd (std::string_view (*text, len),
 		    clist, ignore_help_classes, &nfound);
 
   /* If nothing matches, we have a simple failure.  */
@@ -2666,8 +2658,8 @@ lookup_cmd_composition_1 (const char *text,
 	return 0;
 
       /* TEXT is the start of the first command word to lookup (and
-	 it's length is LEN).  We copy this into a local temporary.  */
-      std::string command (text, len);
+	 its length is LEN).  */
+      std::string_view command (text, len);
 
       /* Look it up.  */
       int nfound = 0;

-- 
2.52.0


  parent reply	other threads:[~2026-01-22 20:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 20:49 [PATCH 0/3] Remove some uses of alloca from cli/ Tom Tromey
2026-01-22 20:49 ` [PATCH 1/3] Use string_view in cli-decode.c:find_cmd Tom Tromey
2026-01-22 20:49 ` Tom Tromey [this message]
2026-01-22 20:49 ` [PATCH 3/3] Remove alloca from lookup_cmd Tom Tromey
2026-01-29 19:07 ` [PATCH 0/3] Remove some uses of alloca from cli/ Kevin Buettner

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=20260122-command-no-alloca-v1-2-402aa429bbbf@adacore.com \
    --to=tromey@adacore.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