public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] C++-ify minidebug.c
@ 2023-08-14 19:48 Tom Tromey
  2023-08-14 20:38 ` John Baldwin
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2023-08-14 19:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I noticed minidebug.c was still using explicit malloc and free, where
a vector would be more automatic.
---
 gdb/minidebug.c | 71 +++++++++++++++++++------------------------------
 1 file changed, 28 insertions(+), 43 deletions(-)

diff --git a/gdb/minidebug.c b/gdb/minidebug.c
index a3ab0e3a5c2..979e569e639 100644
--- a/gdb/minidebug.c
+++ b/gdb/minidebug.c
@@ -60,22 +60,22 @@ static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
 struct gdb_lzma_stream
 {
   /* Section of input BFD from which we are decoding data.  */
-  asection *section;
+  asection *section = nullptr;
 
   /* lzma library decompression state.  */
-  lzma_index *index;
+  lzma_index *index = nullptr;
 
   /* Currently decoded block.  */
-  bfd_size_type data_start;
-  bfd_size_type data_end;
-  gdb_byte *data;
+  bfd_size_type data_start = 0;
+  bfd_size_type data_end = 0;
+  gdb::byte_vector data;
 };
 
 /* bfd_openr_iovec OPEN_P implementation for
    find_separate_debug_file_in_section.  OPEN_CLOSURE is 'asection *'
    of the section to decompress.
 
-   Return 'struct gdb_lzma_stream *' must be freed by caller by xfree,
+   Return 'struct gdb_lzma_stream *' must be freed by caller by delete,
    together with its INDEX lzma data.  */
 
 static void *
@@ -85,7 +85,6 @@ lzma_open (struct bfd *nbfd, void *open_closure)
   bfd_size_type size, offset;
   lzma_stream_flags options;
   gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
-  gdb_byte *indexdata;
   lzma_index *index;
   uint64_t memlimit = UINT64_MAX;
   struct gdb_lzma_stream *lstream;
@@ -105,24 +104,23 @@ lzma_open (struct bfd *nbfd, void *open_closure)
     }
 
   offset -= options.backward_size;
-  indexdata = (gdb_byte *) xmalloc (options.backward_size);
+  gdb::byte_vector indexdata (options.backward_size);
   index = NULL;
   pos = 0;
   if (bfd_seek (section->owner, offset, SEEK_SET) != 0
-      || bfd_read (indexdata, options.backward_size, section->owner)
+      || bfd_read (indexdata.data (), options.backward_size, section->owner)
 	 != options.backward_size
       || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
-				   indexdata, &pos, options.backward_size)
+				   indexdata.data (), &pos,
+				   options.backward_size)
 	 != LZMA_OK
       || lzma_index_size (index) != options.backward_size)
     {
-      xfree (indexdata);
       bfd_set_error (bfd_error_wrong_format);
       return NULL;
     }
-  xfree (indexdata);
 
-  lstream = XCNEW (struct gdb_lzma_stream);
+  lstream = new struct gdb_lzma_stream;
   lstream->section = section;
   lstream->index = index;
 
@@ -140,7 +138,6 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
   struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
   bfd_size_type chunk_size;
   lzma_index_iter iter;
-  gdb_byte *compressed, *uncompressed;
   file_ptr block_offset;
   lzma_filter filters[LZMA_FILTERS_MAX + 1];
   lzma_block block;
@@ -150,7 +147,7 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
   res = 0;
   while (nbytes > 0)
     {
-      if (lstream->data == NULL
+      if (lstream->data.empty ()
 	  || lstream->data_start > offset || offset >= lstream->data_end)
 	{
 	  asection *section = lstream->section;
@@ -159,54 +156,43 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
 	  if (lzma_index_iter_locate (&iter, offset))
 	    break;
 
-	  compressed = (gdb_byte *) xmalloc (iter.block.total_size);
+	  gdb::byte_vector compressed (iter.block.total_size);
 	  block_offset = section->filepos + iter.block.compressed_file_offset;
 	  if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
-	      || bfd_read (compressed, iter.block.total_size, section->owner)
-		 != iter.block.total_size)
-	    {
-	      xfree (compressed);
-	      break;
-	    }
+	      || bfd_read (compressed.data (), iter.block.total_size,
+			   section->owner) != iter.block.total_size)
+	    break;
 
-	  uncompressed = (gdb_byte *) xmalloc (iter.block.uncompressed_size);
+	  gdb::byte_vector uncompressed (iter.block.uncompressed_size);
 
 	  memset (&block, 0, sizeof (block));
 	  block.filters = filters;
 	  block.header_size = lzma_block_header_size_decode (compressed[0]);
-	  if (lzma_block_header_decode (&block, &gdb_lzma_allocator, compressed)
+	  if (lzma_block_header_decode (&block, &gdb_lzma_allocator,
+					compressed.data ())
 	      != LZMA_OK)
-	    {
-	      xfree (compressed);
-	      xfree (uncompressed);
-	      break;
-	    }
+	    break;
 
 	  compressed_pos = block.header_size;
 	  uncompressed_pos = 0;
 	  if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
-					compressed, &compressed_pos,
+					compressed.data (), &compressed_pos,
 					iter.block.total_size,
-					uncompressed, &uncompressed_pos,
+					uncompressed.data (),
+					&uncompressed_pos,
 					iter.block.uncompressed_size)
 	      != LZMA_OK)
-	    {
-	      xfree (compressed);
-	      xfree (uncompressed);
-	      break;
-	    }
-
-	  xfree (compressed);
+	    break;
 
-	  xfree (lstream->data);
-	  lstream->data = uncompressed;
+	  lstream->data = std::move (uncompressed);
 	  lstream->data_start = iter.block.uncompressed_file_offset;
 	  lstream->data_end = (iter.block.uncompressed_file_offset
 			       + iter.block.uncompressed_size);
 	}
 
       chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset);
-      memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size);
+      memcpy (buf, lstream->data.data () + offset - lstream->data_start,
+	      chunk_size);
       buf = (gdb_byte *) buf + chunk_size;
       offset += chunk_size;
       nbytes -= chunk_size;
@@ -227,8 +213,7 @@ lzma_close (struct bfd *nbfd,
   struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
 
   lzma_index_end (lstream->index, &gdb_lzma_allocator);
-  xfree (lstream->data);
-  xfree (lstream);
+  delete lstream;
 
   /* Zero means success.  */
   return 0;
-- 
2.41.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] C++-ify minidebug.c
  2023-08-14 19:48 [PATCH] C++-ify minidebug.c Tom Tromey
@ 2023-08-14 20:38 ` John Baldwin
  2023-08-17 23:41   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: John Baldwin @ 2023-08-14 20:38 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 8/14/23 12:48 PM, Tom Tromey wrote:
> I noticed minidebug.c was still using explicit malloc and free, where
> a vector would be more automatic.
> ---
>   gdb/minidebug.c | 71 +++++++++++++++++++------------------------------
>   1 file changed, 28 insertions(+), 43 deletions(-)

Reviewed-by: John Baldwin <jhb@FreeBSD.org>

-- 
John Baldwin


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] C++-ify minidebug.c
  2023-08-14 20:38 ` John Baldwin
@ 2023-08-17 23:41   ` Tom Tromey
  0 siblings, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2023-08-17 23:41 UTC (permalink / raw)
  To: John Baldwin; +Cc: Tom Tromey, gdb-patches

>>>>> "John" == John Baldwin <jhb@FreeBSD.org> writes:

John> On 8/14/23 12:48 PM, Tom Tromey wrote:
>> I noticed minidebug.c was still using explicit malloc and free, where
>> a vector would be more automatic.
>> ---
>> gdb/minidebug.c | 71 +++++++++++++++++++------------------------------
>> 1 file changed, 28 insertions(+), 43 deletions(-)

John> Reviewed-by: John Baldwin <jhb@FreeBSD.org>

Thanks, I'm checking it in.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-17 23:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-14 19:48 [PATCH] C++-ify minidebug.c Tom Tromey
2023-08-14 20:38 ` John Baldwin
2023-08-17 23:41   ` Tom Tromey

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).