[RFA,07/11] Use gdb::byte_vector in mi_cmd_data_write_memory_bytes

Message ID 20170912185736.20436-8-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Sept. 12, 2017, 6:57 p.m. UTC
  This changes mi_cmd_data_write_memory_bytes to use gdb::byte_vector,
removing some cleanups.

ChangeLog
2017-09-12  Tom Tromey  <tom@tromey.com>

	* mi/mi-main.c (mi_cmd_data_write_memory_bytes): Use
	gdb::byte_vector.
---
 gdb/ChangeLog    |  5 +++++
 gdb/mi/mi-main.c | 20 +++++++-------------
 2 files changed, 12 insertions(+), 13 deletions(-)
  

Comments

Pedro Alves Sept. 28, 2017, 9:46 a.m. UTC | #1
On 09/12/2017 07:57 PM, Tom Tromey wrote:
> This changes mi_cmd_data_write_memory_bytes to use gdb::byte_vector,
> removing some cleanups.

Thanks.

>  
> +  gdb::byte_vector data;
>    if (len_units < count_units)
>      {
>        /* Pattern is made of less units than count:
>           repeat pattern to fill memory.  */
> -      data = (gdb_byte *) xmalloc (count_units * unit_size);
> -      make_cleanup (xfree, data);
> +      data = gdb::byte_vector (count_units * unit_size);

I think I'd have written instead:

  data.resize (count_units * unit_size)

But OK either way.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 47632fa..7158579 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@ 
 2017-09-12  Tom Tromey  <tom@tromey.com>
 
+	* mi/mi-main.c (mi_cmd_data_write_memory_bytes): Use
+	gdb::byte_vector.
+
+2017-09-12  Tom Tromey  <tom@tromey.com>
+
 	* thread.c (gdb_list_thread_ids, gdb_thread_select): Change
 	error_message to std::string.
 	* mi/mi-main.c (mi_cmd_thread_select): Use std::string.
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index d01f578..c8c4a97 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1710,11 +1710,8 @@  mi_cmd_data_write_memory_bytes (const char *command, char **argv, int argc)
 {
   CORE_ADDR addr;
   char *cdata;
-  gdb_byte *data;
-  gdb_byte *databuf;
   size_t len_hex, len_bytes, len_units, i, steps, remaining_units;
   long int count_units;
-  struct cleanup *back_to;
   int unit_size;
 
   if (argc != 2 && argc != 3)
@@ -1738,8 +1735,7 @@  mi_cmd_data_write_memory_bytes (const char *command, char **argv, int argc)
   else
     count_units = len_units;
 
-  databuf = XNEWVEC (gdb_byte, len_bytes);
-  back_to = make_cleanup (xfree, databuf);
+  gdb::byte_vector databuf (len_bytes);
 
   for (i = 0; i < len_bytes; ++i)
     {
@@ -1749,34 +1745,32 @@  mi_cmd_data_write_memory_bytes (const char *command, char **argv, int argc)
       databuf[i] = (gdb_byte) x;
     }
 
+  gdb::byte_vector data;
   if (len_units < count_units)
     {
       /* Pattern is made of less units than count:
          repeat pattern to fill memory.  */
-      data = (gdb_byte *) xmalloc (count_units * unit_size);
-      make_cleanup (xfree, data);
+      data = gdb::byte_vector (count_units * unit_size);
 
       /* Number of times the pattern is entirely repeated.  */
       steps = count_units / len_units;
       /* Number of remaining addressable memory units.  */
       remaining_units = count_units % len_units;
       for (i = 0; i < steps; i++)
-        memcpy (data + i * len_bytes, databuf, len_bytes);
+        memcpy (&data[i * len_bytes], &databuf[0], len_bytes);
 
       if (remaining_units > 0)
-        memcpy (data + steps * len_bytes, databuf,
+        memcpy (&data[steps * len_bytes], &databuf[0],
 		remaining_units * unit_size);
     }
   else
     {
       /* Pattern is longer than or equal to count:
          just copy count addressable memory units.  */
-      data = databuf;
+      data = std::move (databuf);
     }
 
-  write_memory_with_notification (addr, data, count_units);
-
-  do_cleanups (back_to);
+  write_memory_with_notification (addr, data.data (), count_units);
 }
 
 void