From 9b1f59fc95da32963f3ba22f8587ea0c12899e05 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 9 Aug 2021 21:47:02 -0400 Subject: [PATCH] gdb: make gdbarch_printable_names return a vector I noticed that gdbarch_selftest::operator() leaked the value returned by gdbarch_printable_names. Make gdbarch_printable_names return an std::vector and update callers. That makes it easier for everyone involved, less manual memory management. Change-Id: Ia8fc028bdb91f787410cca34f10bf3c5a6da1498 --- gdb/arch-utils.c | 25 ++++++++++++++----------- gdb/gdbarch.c | 30 ++++++++++-------------------- gdb/gdbarch.h | 9 ++++----- gdb/gdbarch.sh | 39 ++++++++++++++------------------------- gdb/selftest-arch.c | 24 ++++++++++++------------ 5 files changed, 54 insertions(+), 73 deletions(-) diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index 4290d637ce1..862f26b6cf7 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -669,10 +669,14 @@ static const bfd_target *default_bfd_vec; static enum bfd_endian default_byte_order = BFD_ENDIAN_UNKNOWN; +/* Printable names of architectures. Used as the enum list of the + "set arch" command. */ +static std::vector arches; + void initialize_current_architecture (void) { - const char **arches = gdbarch_printable_names (); + arches = gdbarch_printable_names (); /* Find a default architecture. */ if (default_bfd_arch == NULL) @@ -680,15 +684,17 @@ initialize_current_architecture (void) /* Choose the architecture by taking the first one alphabetically. */ const char *chosen = arches[0]; - const char **arch; - for (arch = arches; *arch != NULL; arch++) + + for (const char *arch : arches) { - if (strcmp (*arch, chosen) < 0) - chosen = *arch; + if (strcmp (arch, chosen) < 0) + chosen = arch; } + if (chosen == NULL) internal_error (__FILE__, __LINE__, _("initialize_current_architecture: No arch")); + default_bfd_arch = bfd_scan_arch (chosen); if (default_bfd_arch == NULL) internal_error (__FILE__, __LINE__, @@ -743,14 +749,11 @@ initialize_current_architecture (void) list of architectures. */ { /* Append ``auto''. */ - int nr; - for (nr = 0; arches[nr] != NULL; nr++); - arches = XRESIZEVEC (const char *, arches, nr + 2); - arches[nr + 0] = "auto"; - arches[nr + 1] = NULL; + arches.push_back ("auto"); + arches.push_back (nullptr); set_show_commands architecture_cmds = add_setshow_enum_cmd ("architecture", class_support, - arches, &set_architecture_string, + arches.data (), &set_architecture_string, _("Set architecture of target."), _("Show architecture of target."), NULL, set_architecture, show_architecture, diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c index 830a86df89f..f89dcc57754 100644 --- a/gdb/gdbarch.c +++ b/gdb/gdbarch.c @@ -5549,40 +5549,30 @@ struct gdbarch_registration static struct gdbarch_registration *gdbarch_registry = NULL; -static void -append_name (const char ***buf, int *nr, const char *name) -{ - *buf = XRESIZEVEC (const char *, *buf, *nr + 1); - (*buf)[*nr] = name; - *nr += 1; -} - -const char ** -gdbarch_printable_names (void) +std::vector +gdbarch_printable_names () { /* Accumulate a list of names based on the registed list of architectures. */ - int nr_arches = 0; - const char **arches = NULL; - struct gdbarch_registration *rego; + std::vector arches; - for (rego = gdbarch_registry; - rego != NULL; + for (gdbarch_registration *rego = gdbarch_registry; + rego != nullptr; rego = rego->next) { - const struct bfd_arch_info *ap; - ap = bfd_lookup_arch (rego->bfd_architecture, 0); - if (ap == NULL) + const struct bfd_arch_info *ap + = bfd_lookup_arch (rego->bfd_architecture, 0); + if (ap == nullptr) internal_error (__FILE__, __LINE__, _("gdbarch_architecture_names: multi-arch unknown")); do { - append_name (&arches, &nr_arches, ap->printable_name); + arches.push_back (ap->printable_name); ap = ap->next; } while (ap != NULL); } - append_name (&arches, &nr_arches, NULL); + return arches; } diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h index 7db3e36d76a..979159ba2f5 100644 --- a/gdb/gdbarch.h +++ b/gdb/gdbarch.h @@ -1824,12 +1824,11 @@ extern void gdbarch_register (enum bfd_architecture architecture, gdbarch_dump_tdep_ftype *); -/* Return a freshly allocated, NULL terminated, array of the valid - architecture names. Since architectures are registered during the - _initialize phase this function only returns useful information - once initialization has been completed. */ +/* Return a vector of the valid architecture names. Since architectures are + registered during the _initialize phase this function only returns useful + information once initialization has been completed. */ -extern const char **gdbarch_printable_names (void); +extern std::vector gdbarch_printable_names (); /* Helper function. Search the list of ARCHES for a GDBARCH that diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh index 9bc9de91c30..39a99d0d5f3 100755 --- a/gdb/gdbarch.sh +++ b/gdb/gdbarch.sh @@ -1576,12 +1576,11 @@ extern void gdbarch_register (enum bfd_architecture architecture, gdbarch_dump_tdep_ftype *); -/* Return a freshly allocated, NULL terminated, array of the valid - architecture names. Since architectures are registered during the - _initialize phase this function only returns useful information - once initialization has been completed. */ +/* Return a vector of the valid architecture names. Since architectures are + registered during the _initialize phase this function only returns useful + information once initialization has been completed. */ -extern const char **gdbarch_printable_names (void); +extern std::vector gdbarch_printable_names (); /* Helper function. Search the list of ARCHES for a GDBARCH that @@ -2330,40 +2329,30 @@ struct gdbarch_registration static struct gdbarch_registration *gdbarch_registry = NULL; -static void -append_name (const char ***buf, int *nr, const char *name) -{ - *buf = XRESIZEVEC (const char *, *buf, *nr + 1); - (*buf)[*nr] = name; - *nr += 1; -} - -const char ** -gdbarch_printable_names (void) +std::vector +gdbarch_printable_names () { /* Accumulate a list of names based on the registed list of architectures. */ - int nr_arches = 0; - const char **arches = NULL; - struct gdbarch_registration *rego; + std::vector arches; - for (rego = gdbarch_registry; - rego != NULL; + for (gdbarch_registration *rego = gdbarch_registry; + rego != nullptr; rego = rego->next) { - const struct bfd_arch_info *ap; - ap = bfd_lookup_arch (rego->bfd_architecture, 0); - if (ap == NULL) + const struct bfd_arch_info *ap + = bfd_lookup_arch (rego->bfd_architecture, 0); + if (ap == nullptr) internal_error (__FILE__, __LINE__, _("gdbarch_architecture_names: multi-arch unknown")); do { - append_name (&arches, &nr_arches, ap->printable_name); + arches.push_back (ap->printable_name); ap = ap->next; } while (ap != NULL); } - append_name (&arches, &nr_arches, NULL); + return arches; } diff --git a/gdb/selftest-arch.c b/gdb/selftest-arch.c index 0eef134d484..052daed9196 100644 --- a/gdb/selftest-arch.c +++ b/gdb/selftest-arch.c @@ -36,23 +36,23 @@ struct gdbarch_selftest : public selftest void operator() () const override { - const char **arches = gdbarch_printable_names (); + std::vector arches = gdbarch_printable_names (); bool pass = true; - for (int i = 0; arches[i] != NULL; i++) + for (const char *arch : arches) { - if (strcmp ("fr300", arches[i]) == 0) + if (strcmp ("fr300", arch) == 0) { /* PR 20946 */ continue; } - else if (strcmp ("powerpc:EC603e", arches[i]) == 0 - || strcmp ("powerpc:e500mc", arches[i]) == 0 - || strcmp ("powerpc:e500mc64", arches[i]) == 0 - || strcmp ("powerpc:titan", arches[i]) == 0 - || strcmp ("powerpc:vle", arches[i]) == 0 - || strcmp ("powerpc:e5500", arches[i]) == 0 - || strcmp ("powerpc:e6500", arches[i]) == 0) + else if (strcmp ("powerpc:EC603e", arch) == 0 + || strcmp ("powerpc:e500mc", arch) == 0 + || strcmp ("powerpc:e500mc64", arch) == 0 + || strcmp ("powerpc:titan", arch) == 0 + || strcmp ("powerpc:vle", arch) == 0 + || strcmp ("powerpc:e5500", arch) == 0 + || strcmp ("powerpc:e6500", arch) == 0) { /* PR 19797 */ continue; @@ -64,7 +64,7 @@ struct gdbarch_selftest : public selftest { struct gdbarch_info info; - info.bfd_arch_info = bfd_scan_arch (arches[i]); + info.bfd_arch_info = bfd_scan_arch (arch); struct gdbarch *gdbarch = gdbarch_find_by_info (info); SELF_CHECK (gdbarch != NULL); @@ -75,7 +75,7 @@ struct gdbarch_selftest : public selftest { pass = false; exception_fprintf (gdb_stderr, ex, - _("Self test failed: arch %s: "), arches[i]); + _("Self test failed: arch %s: "), arch); } reset (); -- 2.30.2