public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCHv2 10/16] gdb: convert reggroups to use a std::vector
Date: Wed,  6 Apr 2022 13:04:43 +0100	[thread overview]
Message-ID: <c655c14f236ed2a4e5f0c53f9268b985faf11def.1649246539.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1649246538.git.aburgess@redhat.com>

Replace manual linked list with a std::vector.  This commit doesn't
change the reggroup_next and reggroup_prev API, but that will change
in a later commit.

This commit is focused on the minimal changes needed to manage the
reggroups using a std::vector, without changing the API exposed by the
reggroup.c file.

There should be no user visible changes after this commit.
---
 gdb/reggroups.c | 153 ++++++++++++++++++++++++------------------------
 1 file changed, 75 insertions(+), 78 deletions(-)

diff --git a/gdb/reggroups.c b/gdb/reggroups.c
index e47a6daf9a0..262bf49cea8 100644
--- a/gdb/reggroups.c
+++ b/gdb/reggroups.c
@@ -74,35 +74,46 @@ reggroup_type (const struct reggroup *group)
   return group->type;
 }
 
-/* A linked list of groups for the given architecture.  */
-
-struct reggroup_el
-{
-  struct reggroup *group;
-  struct reggroup_el *next;
-};
+/* A container holding all the register groups for a particular
+   architecture.  */
 
 struct reggroups
 {
-  struct reggroup_el *first;
-  struct reggroup_el **last;
+  /* Add GROUP to the list of register groups.  */
+
+  void add (struct reggroup *group)
+  {
+    gdb_assert (group != nullptr);
+    gdb_assert (std::find (m_groups.begin(), m_groups.end(), group)
+		== m_groups.end());
+
+    m_groups.push_back (group);
+  }
+
+  /* The number of register groups.  */
+
+  std::vector<struct reggroup *>::size_type
+  size () const
+  {
+    return m_groups.size ();
+  }
+
+  /* Return a reference to the list of all groups.  */
+
+  const std::vector<struct reggroup *> &
+  groups () const
+  {
+    return m_groups;
+  }
+
+private:
+  /* The register groups.  */
+  std::vector<struct reggroup *> m_groups;
 };
 
 static struct gdbarch_data *reggroups_data;
 
-/* Add a register group (with attribute values) to the pre-defined
-   list.  */
-
-static void
-add_group (struct reggroups *groups, struct reggroup *group,
-	   struct reggroup_el *el)
-{
-  gdb_assert (group != NULL);
-  el->group = group;
-  el->next = NULL;
-  (*groups->last) = el;
-  groups->last = &el->next;
-}
+/* Add GROUP to the list of register groups for GDBARCH.  */
 
 void
 reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
@@ -110,15 +121,10 @@ reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
   struct reggroups *groups
     = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
 
-  /* The same reggroup should not be added multiple times.  */
   gdb_assert (groups != nullptr);
-  for (struct reggroup_el *el = groups->first;
-       el != nullptr;
-       el = el->next)
-    gdb_assert (group != el->group);
+  gdb_assert (group != nullptr);
 
-  add_group (groups, group,
-	     GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
+  groups->add (group);
 }
 
 /* Called to initialize the per-gdbarch register group information.  */
@@ -126,25 +132,16 @@ reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
 static void *
 reggroups_init (struct obstack *obstack)
 {
-  struct reggroups *groups = OBSTACK_ZALLOC (obstack, struct reggroups);
-
-  groups->last = &groups->first;
+  struct reggroups *groups = obstack_new<struct reggroups> (obstack);
 
   /* Add the default groups.  */
-  add_group (groups, general_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, float_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, system_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, vector_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, all_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, save_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
-  add_group (groups, restore_reggroup,
-	     OBSTACK_ZALLOC (obstack, struct reggroup_el));
+  groups->add (general_reggroup);
+  groups->add (float_reggroup);
+  groups->add (system_reggroup);
+  groups->add (vector_reggroup);
+  groups->add (all_reggroup);
+  groups->add (save_reggroup);
+  groups->add (restore_reggroup);
 
   return groups;
 }
@@ -154,29 +151,28 @@ reggroups_init (struct obstack *obstack)
 struct reggroup *
 reggroup_next (struct gdbarch *gdbarch, const struct reggroup *last)
 {
-  struct reggroups *groups;
-  struct reggroup_el *el;
-
   /* Don't allow this function to be called during architecture
      creation.  If there are no groups, use the default groups list.  */
-  groups = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
-  gdb_assert (groups != NULL);
-  gdb_assert (groups->first != NULL);
+  struct reggroups *groups
+    = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
+  gdb_assert (groups != nullptr);
+  gdb_assert (groups->size () > 0);
 
   /* Return the first/next reggroup.  */
-  if (last == NULL)
-    return groups->first->group;
-  for (el = groups->first; el != NULL; el = el->next)
+  if (last == nullptr)
+    return groups->groups ().front ();
+  for (int i = 0; i < groups->size (); ++i)
     {
-      if (el->group == last)
+      if (groups->groups ()[i] == last)
 	{
-	  if (el->next != NULL)
-	    return el->next->group;
+	  if (i + 1 < groups->size ())
+	    return groups->groups ()[i + 1];
 	  else
-	    return NULL;
+	    return nullptr;
 	}
     }
-  return NULL;
+
+  return nullptr;
 }
 
 /* See reggroups.h.  */
@@ -184,27 +180,28 @@ reggroup_next (struct gdbarch *gdbarch, const struct reggroup *last)
 struct reggroup *
 reggroup_prev (struct gdbarch *gdbarch, const struct reggroup *curr)
 {
-  struct reggroups *groups;
-  struct reggroup_el *el;
-  struct reggroup *prev;
-
   /* Don't allow this function to be called during architecture
      creation.  If there are no groups, use the default groups list.  */
-  groups = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
-  gdb_assert (groups != NULL);
-  gdb_assert (groups->first != NULL);
+  struct reggroups *groups
+    = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
+  gdb_assert (groups != nullptr);
+  gdb_assert (groups->size () > 0);
 
-  prev = NULL;
-  for (el = groups->first; el != NULL; el = el->next)
+  /* Return the first/next reggroup.  */
+  if (curr == nullptr)
+    return groups->groups ().back ();
+  for (int i = groups->size () - 1; i >= 0; --i)
     {
-      gdb_assert (el->group != NULL);
-      if (el->group == curr)
-	return prev;
-      prev = el->group;
+      if (groups->groups ()[i] == curr)
+	{
+	  if (i - 1 >= 0)
+	    return groups->groups ()[i - 1];
+	  else
+	    return nullptr;
+	}
     }
-  if (curr == NULL)
-    return prev;
-  return NULL;
+
+  return nullptr;
 }
 
 /* Is REGNUM a member of REGGROUP?  */
@@ -234,7 +231,7 @@ default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
     return (!vector_p && !float_p);
   if (group == save_reggroup || group == restore_reggroup)
     return raw_p;
-  return 0;   
+  return 0;
 }
 
 /* See reggroups.h.  */
@@ -273,7 +270,7 @@ reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
 	  name = reggroup_name (group);
 	gdb_printf (file, " %-10s", name);
       }
-      
+
       /* Group type.  */
       {
 	const char *type;
-- 
2.25.4


  parent reply	other threads:[~2022-04-06 12:05 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31 21:04 [PATCH 00/16] Default register groups, and general related cleanup Andrew Burgess
2022-03-31 21:04 ` [PATCH 01/16] gdb: don't try to use readline before it's initialized Andrew Burgess
2022-03-31 21:04 ` [PATCH 02/16] gdb: add some const in gdb/reggroups.c Andrew Burgess
2022-03-31 21:04 ` [PATCH 03/16] gdb: make gdbarch_register_reggroup_p take a const reggroup * Andrew Burgess
2022-04-04 22:35   ` Lancelot SIX
2022-04-05  8:24     ` Andrew Burgess
2022-03-31 21:04 ` [PATCH 04/16] gdb: switch to using 'const reggroup *' in tui-regs.{c, h} Andrew Burgess
2022-03-31 21:04 ` [PATCH 05/16] gdb: use 'const reggroup *' in python/py-registers.c file Andrew Burgess
2022-03-31 21:04 ` [PATCH 06/16] gdb: have reggroup_find return a const Andrew Burgess
2022-03-31 21:04 ` [PATCH 07/16] gdb/tui: avoid theoretical bug with 'tui reg' command Andrew Burgess
2022-03-31 21:04 ` [PATCH 08/16] gdb/tui: fix 'tui reg next/prev' command when data window is hidden Andrew Burgess
2022-03-31 21:04 ` [PATCH 09/16] gdb: always add the default register groups Andrew Burgess
2022-03-31 21:04 ` [PATCH 10/16] gdb: convert reggroups to use a std::vector Andrew Burgess
2022-03-31 21:04 ` [PATCH 11/16] gdb: remove reggroup_next and reggroup_prev Andrew Burgess
2022-04-05 23:11   ` Lancelot SIX
2022-04-06 12:06     ` Andrew Burgess
2022-03-31 21:04 ` [PATCH 12/16] gdb: more 'const' in gdb/reggroups.{c,h} Andrew Burgess
2022-03-31 21:04 ` [PATCH 13/16] gdb: make the pre-defined register groups const Andrew Burgess
2022-03-31 21:04 ` [PATCH 14/16] gdb: convert reggroup to a C++ class with constructor, etc Andrew Burgess
2022-03-31 21:04 ` [PATCH 15/16] gdb: move struct reggroup into reggroups.h header Andrew Burgess
2022-03-31 21:04 ` [PATCH 16/16] gdb: update comments throughout reggroups.{c,h} files Andrew Burgess
2022-04-06 14:28   ` Simon Marchi
2022-04-06 12:04 ` [PATCHv2 00/16] Default register groups, and general related cleanup Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 01/16] gdb: don't try to use readline before it's initialized Andrew Burgess
2022-04-06 12:57     ` Simon Marchi
2022-04-06 12:04   ` [PATCHv2 02/16] gdb: add some const in gdb/reggroups.c Andrew Burgess
2022-04-06 12:58     ` Simon Marchi
2022-04-06 12:04   ` [PATCHv2 03/16] gdb: make gdbarch_register_reggroup_p take a const reggroup * Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 04/16] gdb: switch to using 'const reggroup *' in tui-regs.{c, h} Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 05/16] gdb: use 'const reggroup *' in python/py-registers.c file Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 06/16] gdb: have reggroup_find return a const Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 07/16] gdb/tui: avoid theoretical bug with 'tui reg' command Andrew Burgess
2022-04-06 13:02     ` Simon Marchi
2022-04-06 12:04   ` [PATCHv2 08/16] gdb/tui: fix 'tui reg next/prev' command when data window is hidden Andrew Burgess
2022-04-06 13:13     ` Simon Marchi
2022-04-06 12:04   ` [PATCHv2 09/16] gdb: always add the default register groups Andrew Burgess
2022-04-06 13:22     ` Simon Marchi
2022-04-06 12:04   ` Andrew Burgess [this message]
2022-04-06 12:04   ` [PATCHv2 11/16] gdb: remove reggroup_next and reggroup_prev Andrew Burgess
2022-04-06 14:22     ` Simon Marchi
2022-04-06 14:23       ` Simon Marchi
2022-04-06 12:04   ` [PATCHv2 12/16] gdb: more 'const' in gdb/reggroups.{c,h} Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 13/16] gdb: make the pre-defined register groups const Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 14/16] gdb: convert reggroup to a C++ class with constructor, etc Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 15/16] gdb: move struct reggroup into reggroups.h header Andrew Burgess
2022-04-06 12:04   ` [PATCHv2 16/16] gdb: update comments throughout reggroups.{c, h} files Andrew Burgess
2022-04-06 14:34   ` [PATCHv2 00/16] Default register groups, and general related cleanup Simon Marchi
2022-04-07 15:16     ` Andrew Burgess

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=c655c14f236ed2a4e5f0c53f9268b985faf11def.1649246539.git.aburgess@redhat.com \
    --to=aburgess@redhat.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).