From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] C++-ify minidebug.c
Date: Mon, 14 Aug 2023 13:48:07 -0600 [thread overview]
Message-ID: <20230814194807.63066-1-tom@tromey.com> (raw)
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
next reply other threads:[~2023-08-14 19:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-14 19:48 Tom Tromey [this message]
2023-08-14 20:38 ` John Baldwin
2023-08-17 23:41 ` 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=20230814194807.63066-1-tom@tromey.com \
--to=tom@tromey.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).