public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 11/13] gdb: remove some uses of alloca from remote.c
Date: Mon, 27 Feb 2023 21:29:24 +0000	[thread overview]
Message-ID: <4818cb026330713c1cbce9ff36a77fa6a3ec3618.1677533215.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1677533215.git.aburgess@redhat.com>

Remove some of the uses of alloca from remote.c and replace them with
either gdb::byte_vector or std::vector<gdb_byte>.

In one case I've used std::vector<gdb_byte> because this handles
automatically zeroing the contents, and this allows me to remove an
associated memset.

There are other uses of alloca in this file (remote.c), but these are
all for types other than gdb_byte so need replacing with something
other than gdb::byte_vector.

There should be no user visible changes after this commit.
---
 gdb/remote.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index ba7a7520cb4..3f0552e7c9e 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -8554,7 +8554,6 @@ remote_target::fetch_register_using_p (struct regcache *regcache,
   struct gdbarch *gdbarch = regcache->arch ();
   struct remote_state *rs = get_remote_state ();
   char *buf, *p;
-  gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
   int i;
 
   if (m_features.packet_support (PACKET_p) == PACKET_DISABLE)
@@ -8593,6 +8592,7 @@ remote_target::fetch_register_using_p (struct regcache *regcache,
     }
 
   /* Otherwise, parse and supply the value.  */
+  gdb::byte_vector regp (register_size (gdbarch, reg->regnum));
   p = buf;
   i = 0;
   while (p[0] != 0)
@@ -8603,7 +8603,7 @@ remote_target::fetch_register_using_p (struct regcache *regcache,
       regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
       p += 2;
     }
-  regcache->raw_supply (reg->regnum, regp);
+  regcache->raw_supply (reg->regnum, regp.data ());
   return 1;
 }
 
@@ -8862,7 +8862,7 @@ remote_target::store_register_using_P (const struct regcache *regcache,
   struct remote_state *rs = get_remote_state ();
   /* Try storing a single register.  */
   char *buf = rs->buf.data ();
-  gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
+  gdb::byte_vector regp (register_size (gdbarch, reg->regnum));
   char *p;
 
   if (m_features.packet_support (PACKET_P) == PACKET_DISABLE)
@@ -8873,8 +8873,8 @@ remote_target::store_register_using_P (const struct regcache *regcache,
 
   xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
   p = buf + strlen (buf);
-  regcache->raw_collect (reg->regnum, regp);
-  bin2hex (regp, p, register_size (gdbarch, reg->regnum));
+  regcache->raw_collect (reg->regnum, regp.data ());
+  bin2hex (regp.data (), p, register_size (gdbarch, reg->regnum));
   putpkt (rs->buf);
   getpkt (&rs->buf, 0);
 
@@ -8900,30 +8900,26 @@ remote_target::store_registers_using_G (const struct regcache *regcache)
 {
   struct remote_state *rs = get_remote_state ();
   remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
-  gdb_byte *regs;
-  char *p;
 
-  /* Extract all the registers in the regcache copying them into a
-     local buffer.  */
-  {
-    int i;
+  /* Extract all the registers in the REGCACHE copying them into a local
+     buffer REGS.
 
-    regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
-    memset (regs, 0, rsa->sizeof_g_packet);
-    for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
-      {
-	struct packet_reg *r = &rsa->regs[i];
+     Use std::vector<gdb_byte> here (instead of gdb::byte_vector) because
+     we want the contents to be zero initialized.  */
+  std::vector<gdb_byte> regs (rsa->sizeof_g_packet);
+  for (int i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
+    {
+      struct packet_reg *r = &rsa->regs[i];
 
-	if (r->in_g_packet)
-	  regcache->raw_collect (r->regnum, regs + r->offset);
-      }
-  }
+      if (r->in_g_packet)
+	regcache->raw_collect (r->regnum, regs.data () + r->offset);
+    }
 
   /* Command describes registers byte by byte,
      each byte encoded as two hex characters.  */
-  p = rs->buf.data ();
+  char *p = rs->buf.data ();
   *p++ = 'G';
-  bin2hex (regs, p, rsa->sizeof_g_packet);
+  bin2hex (regs.data (), p, rsa->sizeof_g_packet);
   putpkt (rs->buf);
   getpkt (&rs->buf, 0);
   if (packet_check_result (rs->buf) == PACKET_ERROR)
-- 
2.25.4


  parent reply	other threads:[~2023-02-27 21:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-27 21:29 [PATCH 00/13] Remove a bunch of alloca uses Andrew Burgess
2023-02-27 21:29 ` [PATCH 01/13] gdb: remove uses of alloca from arch-utils.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 02/13] gdb: remove use of alloca from auxv.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 03/13] gdb: remove use of alloca from c-lang.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 04/13] gdb: remove use of alloca from corefile.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 05/13] gdb: remove uses of alloca from dwarf2/expr.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 06/13] gdb: remove a use of alloca from elfread.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 07/13] gdb: remove use of alloca from findvar.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 08/13] gdb: remove use of alloca from linux-nat-trad.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 09/13] gdb: remove use of alloca from mem-break.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 10/13] gdb: remove some uses of alloca from printcmd.c Andrew Burgess
2023-02-27 21:29 ` Andrew Burgess [this message]
2023-02-27 21:29 ` [PATCH 12/13] gdb: remove uses of alloca from valprint.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 13/13] gdb: remove a use of alloca from symfile.c Andrew Burgess
2023-02-28  2:47 ` [PATCH 00/13] Remove a bunch of alloca uses Simon Marchi
2023-03-07 21:57   ` Tom Tromey

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=4818cb026330713c1cbce9ff36a77fa6a3ec3618.1677533215.git.aburgess@redhat.com \
    --to=aburgess@redhat.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).