public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/2] Make cached_reg_t own its data
Date: Fri, 15 Dec 2023 18:12:56 +0000	[thread overview]
Message-ID: <20231215181257.1196830-2-pedro@palves.net> (raw)
In-Reply-To: <20231215181257.1196830-1-pedro@palves.net>

struct cached_reg_t own its data buffer, but currently that is managed
manually.  Convert it to use a unique_xmalloc_ptr.

Change-Id: I05a107098b717299e76de76aaba00d7fbaeac77b
---
 gdb/python/py-unwind.c | 13 ++++++-------
 gdb/regcache.h         |  5 ++++-
 gdb/remote.c           | 24 ++++++------------------
 3 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index f1162f22290..31b74c67310 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -785,7 +785,7 @@ pyuw_prev_register (frame_info_ptr this_frame, void **cache_ptr,
   for (; reg_info < reg_info_end; ++reg_info)
     {
       if (regnum == reg_info->num)
-	return frame_unwind_got_bytes (this_frame, regnum, reg_info->data);
+	return frame_unwind_got_bytes (this_frame, regnum, reg_info->data.get ());
     }
 
   return frame_unwind_got_optimized (this_frame, regnum);
@@ -903,15 +903,14 @@ pyuw_sniffer (const struct frame_unwind *self, frame_info_ptr this_frame,
 	struct value *value = value_object_to_value (reg->value.get ());
 	size_t data_size = register_size (gdbarch, reg->number);
 
-	cached_frame->reg[i].num = reg->number;
-
 	/* `value' validation was done before, just assert.  */
 	gdb_assert (value != NULL);
 	gdb_assert (data_size == value->type ()->length ());
 
-	cached_frame->reg[i].data = (gdb_byte *) xmalloc (data_size);
-	memcpy (cached_frame->reg[i].data,
-		value->contents ().data (), data_size);
+	cached_reg_t *cached = new (&cached_frame->reg[i]) cached_reg_t ();
+	cached->num = reg->number;
+	cached->data.reset ((gdb_byte *) xmalloc (data_size));
+	memcpy (cached->data.get (), value->contents ().data (), data_size);
       }
   }
 
@@ -928,7 +927,7 @@ pyuw_dealloc_cache (frame_info *this_frame, void *cache)
   cached_frame_info *cached_frame = (cached_frame_info *) cache;
 
   for (int i = 0; i < cached_frame->reg_count; i++)
-    xfree (cached_frame->reg[i].data);
+    cached_frame->reg[i].~cached_reg_t ();
 
   xfree (cache);
 }
diff --git a/gdb/regcache.h b/gdb/regcache.h
index d90f74bfbb0..85890b66cbc 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -176,7 +176,10 @@ using register_read_ftype
 struct cached_reg_t
 {
   int num;
-  gdb_byte *data;
+  gdb::unique_xmalloc_ptr<gdb_byte> data;
+
+  cached_reg_t () = default;
+  cached_reg_t (cached_reg_t &&rhs) = default;
 };
 
 /* Buffer of registers.  */
diff --git a/gdb/remote.c b/gdb/remote.c
index 84daa8567b6..7611396c949 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1353,8 +1353,6 @@ class extended_remote_target final : public remote_target
 
 struct stop_reply : public notif_event
 {
-  ~stop_reply ();
-
   /* The identifier of the thread about this event  */
   ptid_t ptid;
 
@@ -7604,12 +7602,6 @@ remote_notif_stop_can_get_pending_events (remote_target *remote,
   return 0;
 }
 
-stop_reply::~stop_reply ()
-{
-  for (cached_reg_t &reg : regcache)
-    xfree (reg.data);
-}
-
 static notif_event_up
 remote_notif_stop_alloc_reply ()
 {
@@ -8094,17 +8086,18 @@ Packet: '%s'\n"),
 			   hex_string (pnum), p, buf);
 
 		  cached_reg.num = reg->regnum;
-		  cached_reg.data = (gdb_byte *)
-		    xmalloc (register_size (event->arch, reg->regnum));
+		  cached_reg.data.reset ((gdb_byte *)
+					 xmalloc (register_size (event->arch,
+								 reg->regnum)));
 
 		  p = p1 + 1;
-		  fieldsize = hex2bin (p, cached_reg.data,
+		  fieldsize = hex2bin (p, cached_reg.data.get (),
 				       register_size (event->arch, reg->regnum));
 		  p += 2 * fieldsize;
 		  if (fieldsize < register_size (event->arch, reg->regnum))
 		    warning (_("Remote reply is too short: %s"), buf);
 
-		  event->regcache.push_back (cached_reg);
+		  event->regcache.push_back (std::move (cached_reg));
 		}
 	      else
 		{
@@ -8436,12 +8429,7 @@ remote_target::process_stop_reply (struct stop_reply *stop_reply,
 					stop_reply->arch);
 
 	  for (cached_reg_t &reg : stop_reply->regcache)
-	    {
-	      regcache->raw_supply (reg.num, reg.data);
-	      xfree (reg.data);
-	    }
-
-	  stop_reply->regcache.clear ();
+	    regcache->raw_supply (reg.num, reg.data.get ());
 	}
 
       remote_thread_info *remote_thr = get_remote_thread_info (this, ptid);
-- 
2.43.0


  reply	other threads:[~2023-12-15 18:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15 18:12 [PATCH 0/2] Use unique_ptr more in the remote target Pedro Alves
2023-12-15 18:12 ` Pedro Alves [this message]
2023-12-20 23:48   ` [PATCH 1/2] Make cached_reg_t own its data Simon Marchi
2023-12-21 11:11     ` Pedro Alves
2023-12-15 18:12 ` [PATCH 2/2] Complete use of unique_ptr with notif_event and stop_reply Pedro Alves
2023-12-15 19:10 ` [PATCH 0/2] Use unique_ptr more in the remote target Tom Tromey
2023-12-20 20:27   ` [pushed] Fix bug in previous remote unique_ptr change (Re: [PATCH 0/2] Use unique_ptr more in the remote target) Pedro Alves
2023-12-20 23:30     ` 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=20231215181257.1196830-2-pedro@palves.net \
    --to=pedro@palves.net \
    --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).