public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Subject: [PATCH] gdb: make gdbarch_printable_names return a vector
Date: Mon,  9 Aug 2021 21:47:02 -0400	[thread overview]
Message-ID: <20210810014702.686750-1-simon.marchi@polymtl.ca> (raw)

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 4290d637ce16..862f26b6cf78 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<const char *> 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 830a86df89f3..f89dcc577542 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<const char *>
+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<const char *> 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 7db3e36d76aa..979159ba2f59 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<const char *> 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 9bc9de91c30b..39a99d0d5f34 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<const char *> 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<const char *>
+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<const char *> 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 0eef134d4841..052daed9196e 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<const char *> 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.32.0


             reply	other threads:[~2021-08-10  1:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10  1:47 Simon Marchi [this message]
2021-08-12 19:01 ` Tom Tromey
2021-08-12 19:57   ` Simon Marchi

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=20210810014702.686750-1-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --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).