diff options
author | Tom de Vries <tdevries@suse.de> | 2023-08-31 09:37:44 +0200 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2023-08-31 09:37:44 +0200 |
commit | 2774f2dad5f05e68771c07df6ab0fb23baa2118e (patch) | |
tree | 41e352c12e7f2dee518fdf504b7e687e887c9baa /gdb/dwarf2/read.c | |
parent | 0b8b932dce26ef8e907b3b3c06b01a99420245fe (diff) | |
download | gdb-2774f2dad5f05e68771c07df6ab0fb23baa2118e.zip gdb-2774f2dad5f05e68771c07df6ab0fb23baa2118e.tar.gz gdb-2774f2dad5f05e68771c07df6ab0fb23baa2118e.tar.bz2 |
[gdb/symtab] Factor out type::{alloc_fields,copy_fields}
After finding this code in buildsym_compunit::finish_block_internal:
...
ftype->set_fields
((struct field *)
TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
...
and fixing PR30810 by using TYPE_ZALLOC, I wondered if there were more
locations that needed fixing.
I decided to make things easier to spot by factoring out a new function
alloc_fields:
...
/* Allocate the fields array of this type, with NFIELDS elements. If INIT,
zero-initialize the allocated memory. */
void
type::alloc_fields (unsigned int nfields, bool init = true);
...
where:
- a regular use would be "alloc_fields (nfields)", and
- an exceptional use that needed no initialization would be
"alloc_fields (nfields, false)".
Pretty soon I discovered that most of the latter cases are due to
initialization by memcpy, so I added two variants of copy_fields as well.
After this rewrite there are 8 uses of set_fields left:
...
gdb/coffread.c: type->set_fields (nullptr);
gdb/coffread.c: type->set_fields (nullptr);
gdb/coffread.c: type->set_fields (nullptr);
gdb/eval.c: type->set_fields
gdb/gdbtypes.c: type->set_fields (args);
gdb/gdbtypes.c: t->set_fields (XRESIZEVEC (struct field, t->fields (),
gdb/dwarf2/read.c: type->set_fields (new_fields);
gdb/dwarf2/read.c: sub_type->set_fields (sub_type->fields () + 1);
...
These fall into the following categories:
- set to nullptr (coffread.c),
- type not owned by objfile or gdbarch (eval.c), and
- modifying an existing fields array, like adding an element at the end or
dropping an element at the start (the rest).
Tested on x86_64-linux.
Diffstat (limited to 'gdb/dwarf2/read.c')
-rw-r--r-- | gdb/dwarf2/read.c | 40 |
1 files changed, 7 insertions, 33 deletions
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 5b32089..7fe0e84 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -6054,11 +6054,9 @@ quirk_rust_enum (struct type *type, struct objfile *objfile) /* Smash this type to be a structure type. We have to do this because the type has already been recorded. */ type->set_code (TYPE_CODE_STRUCT); - type->set_num_fields (3); /* Save the field we care about. */ struct field saved_field = type->field (0); - type->set_fields - ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field))); + type->alloc_fields (3); /* Put the discriminant at index 0. */ type->field (0).set_type (field_type); @@ -12059,9 +12057,7 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type, /* Record the field count, allocate space for the array of fields, and create blank accessibility bitfields if necessary. */ - type->set_num_fields (nfields); - type->set_fields - ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields)); + type->alloc_fields (nfields); if (fip->non_public_fields && cu->lang () != language_ada) { @@ -12490,12 +12486,7 @@ rewrite_array_type (struct type *type) updated. Either way we want to copy the type and update everything. */ struct type *copy = copy_type (type); - int nfields = copy->num_fields (); - field *new_fields - = ((struct field *) TYPE_ZALLOC (copy, - nfields * sizeof (struct field))); - memcpy (new_fields, copy->fields (), nfields * sizeof (struct field)); - copy->set_fields (new_fields); + copy->copy_fields (type); if (new_target != nullptr) copy->set_target_type (new_target); @@ -13260,14 +13251,7 @@ update_enumeration_type_from_children (struct die_info *die, } if (!fields.empty ()) - { - type->set_num_fields (fields.size ()); - type->set_fields - ((struct field *) - TYPE_ALLOC (type, sizeof (struct field) * fields.size ())); - memcpy (type->fields (), fields.data (), - sizeof (struct field) * fields.size ()); - } + type->copy_fields (fields); else flag_enum = 0; @@ -13636,12 +13620,7 @@ quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu, struct type *bounds = alloc.new_type (); bounds->set_code (TYPE_CODE_STRUCT); - bounds->set_num_fields (range_fields.size ()); - bounds->set_fields - ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields () - * sizeof (struct field)))); - memcpy (bounds->fields (), range_fields.data (), - bounds->num_fields () * sizeof (struct field)); + bounds->copy_fields (range_fields); int last_fieldno = range_fields.size () - 1; int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8 @@ -13664,10 +13643,7 @@ quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu, struct type *result = type_allocator (objfile).new_type (); result->set_code (TYPE_CODE_STRUCT); - result->set_num_fields (2); - result->set_fields - ((struct field *) TYPE_ZALLOC (result, (result->num_fields () - * sizeof (struct field)))); + result->alloc_fields (2); /* The names are chosen to coincide with what the compiler does with -fgnat-encodings=all, which the Ada code in gdb already @@ -14709,9 +14685,7 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu) } /* Allocate storage for parameters and fill them in. */ - ftype->set_num_fields (nparams); - ftype->set_fields - ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field))); + ftype->alloc_fields (nparams); /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it even if we error out during the parameters reading below. */ |