Patchwork [3/4] Use std::vector in abbrev_table_read_table

login
register
mail settings
Submitter Tom Tromey
Date Jan. 7, 2020, 10:07 p.m.
Message ID <20200107220754.24796-4-tromey@adacore.com>
Download mbox | patch
Permalink /patch/37243/
State New
Headers show

Comments

Tom Tromey - Jan. 7, 2020, 10:07 p.m.
This removes some manual memory management from
abbrev_table_read_table, replacing it with a std::vector.

gdb/ChangeLog
2020-01-07  Tom Tromey  <tromey@adacore.com>

	* dwarf2read.c (abbrev_table_read_table): Use std::vector.

Change-Id: I0b0e70ac2281d89a78f4d6a642700c9f0506871d
---
 gdb/ChangeLog    |  4 ++++
 gdb/dwarf2read.c | 27 ++++++++-------------------
 2 files changed, 12 insertions(+), 19 deletions(-)
Simon Marchi - Jan. 8, 2020, 3:52 a.m.
On 2020-01-07 5:07 p.m., Tom Tromey wrote:
> This removes some manual memory management from
> abbrev_table_read_table, replacing it with a std::vector.
> 
> gdb/ChangeLog
> 2020-01-07  Tom Tromey  <tromey@adacore.com>
> 
> 	* dwarf2read.c (abbrev_table_read_table): Use std::vector.
> 
> Change-Id: I0b0e70ac2281d89a78f4d6a642700c9f0506871d

You can remove the ATTR_ALLOC_CHUNK definition.  Otherwise, this LGTM.

Simon
Tom Tromey - Jan. 8, 2020, 6:12 p.m.
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

>> * dwarf2read.c (abbrev_table_read_table): Use std::vector.

Simon> You can remove the ATTR_ALLOC_CHUNK definition.  Otherwise, this LGTM.

Thanks, I did this.

Tom

Patch

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index e6584d96537..ad361df00b9 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -18563,8 +18563,7 @@  abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
   struct abbrev_info *cur_abbrev;
   unsigned int abbrev_number, bytes_read, abbrev_name;
   unsigned int abbrev_form;
-  struct attr_abbrev *cur_attrs;
-  unsigned int allocated_attrs;
+  std::vector<struct attr_abbrev> cur_attrs;
 
   abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
 
@@ -18573,12 +18572,10 @@  abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
   abbrev_ptr += bytes_read;
 
-  allocated_attrs = ATTR_ALLOC_CHUNK;
-  cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
-
   /* Loop until we reach an abbrev number of 0.  */
   while (abbrev_number)
     {
+      cur_attrs.clear ();
       cur_abbrev = abbrev_table->alloc_abbrev ();
 
       /* read in abbrev header */
@@ -18613,25 +18610,18 @@  abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
 	  if (abbrev_name == 0)
 	    break;
 
-	  if (cur_abbrev->num_attrs == allocated_attrs)
-	    {
-	      allocated_attrs += ATTR_ALLOC_CHUNK;
-	      cur_attrs
-		= XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
-	    }
-
-	  cur_attrs[cur_abbrev->num_attrs].name
-	    = (enum dwarf_attribute) abbrev_name;
-	  cur_attrs[cur_abbrev->num_attrs].form
-	    = (enum dwarf_form) abbrev_form;
-	  cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
+	  cur_attrs.emplace_back ();
+	  struct attr_abbrev &cur_attr = cur_attrs.back ();
+	  cur_attr.name = (enum dwarf_attribute) abbrev_name;
+	  cur_attr.form = (enum dwarf_form) abbrev_form;
+	  cur_attr.implicit_const = implicit_const;
 	  ++cur_abbrev->num_attrs;
 	}
 
       cur_abbrev->attrs =
 	XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
 		   cur_abbrev->num_attrs);
-      memcpy (cur_abbrev->attrs, cur_attrs,
+      memcpy (cur_abbrev->attrs, cur_attrs.data (),
 	      cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
 
       abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
@@ -18651,7 +18641,6 @@  abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
 	break;
     }
 
-  xfree (cur_attrs);
   return abbrev_table;
 }