public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] gdb/python: Reuse gdb.RegisterGroup objects where possible
Date: Wed,  8 Jul 2020 09:48:12 +0100	[thread overview]
Message-ID: <14e217dbd8200e3b07baf255e754bea738093d64.1594197735.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1594197735.git.andrew.burgess@embecosm.com>

Only create one gdb.RegisterGroup Python object for each of GDB's
reggroup objects.

I could have added a field into the reggroup object to hold the Python
object pointer for each reggroup, however, as reggroups are never
deleted within GDB, and are global (not per-architecture) a simpler
solution seemed to be just to hold a single global map from reggroup
pointer to a Python object representing the reggroup.  Then we can
reuse the objects out of this map.

After this commit it is possible for a user to tell that two
gdb.RegisterGroup objects are now identical when previously they were
unique, however, as both these objects are read-only I don't think
this should be a problem.

There should be no other user visible changes after this commit.

gdb/ChangeLog:

	* python/py-registers.c : Add 'unordered_map' include.
	(gdbpy_new_reggroup): Renamed to...
	(gdbpy_get_reggroup): ...this.  Update to only create register
	group descriptors when needed.
	(gdbpy_reggroup_iter_next): Update.

gdb/testsuite/ChangeLog:

	* gdb.python/py-arch-reg-groups.exp: Additional tests.
---
 gdb/ChangeLog                                 |  8 ++++
 gdb/python/py-registers.c                     | 40 ++++++++++++++-----
 gdb/testsuite/ChangeLog                       |  4 ++
 .../gdb.python/py-arch-reg-groups.exp         | 19 +++++++++
 4 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index fec84e5d5d4..5d0f5386533 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -23,6 +23,7 @@
 #include "disasm.h"
 #include "reggroups.h"
 #include "python-internal.h"
+#include <unordered_map>
 
 /* Token to access per-gdbarch data related to register descriptors.  */
 static struct gdbarch_data *gdbpy_register_object_data = NULL;
@@ -96,18 +97,37 @@ gdbpy_register_object_data_init (struct gdbarch *gdbarch)
   return (void *) vec;
 }
 
-/* Create a new gdb.RegisterGroup object wrapping REGGROUP.  */
+/* Return a gdb.RegisterGroup object wrapping REGGROUP.  The register
+   group objects are cached, and the same Python object will always be
+   returned for the same REGGROUP pointer.  */
 
 static PyObject *
-gdbpy_new_reggroup (struct reggroup *reggroup)
+gdbpy_get_reggroup (struct reggroup *reggroup)
 {
-  /* Create a new object and fill in its details.  */
-  reggroup_object *group
-    = PyObject_New (reggroup_object, &reggroup_object_type);
-  if (group == NULL)
-    return NULL;
-  group->reggroup = reggroup;
-  return (PyObject *) group;
+  /* Map from GDB's internal reggroup objects to the Python representation.
+     GDB's reggroups are global, and are never deleted, so using a map like
+     this is safe.  */
+  static std::unordered_map<struct reggroup *,const reggroup_object *>
+    gdbpy_reggroup_object_map;
+
+  /* If there is not already a suitable Python object in the map then
+     create a new one, and add it to the map.  */
+  if (gdbpy_reggroup_object_map[reggroup] == nullptr)
+    {
+      /* Create a new object and fill in its details.  */
+      reggroup_object *group
+	= PyObject_New (reggroup_object, &reggroup_object_type);
+      if (group == NULL)
+	return NULL;
+      group->reggroup = reggroup;
+      gdbpy_reggroup_object_map[reggroup] = group;
+    }
+
+  /* Fetch the Python object wrapping REGGROUP from the map, increment its
+     reference count and return it.  */
+  PyObject *obj = (PyObject *) gdbpy_reggroup_object_map[reggroup];
+  Py_INCREF (obj);
+  return obj;
 }
 
 /* Convert a gdb.RegisterGroup to a string, it just returns the name of
@@ -219,7 +239,7 @@ gdbpy_reggroup_iter_next (PyObject *self)
     }
 
   iter_obj->reggroup = next_group;
-  return gdbpy_new_reggroup (iter_obj->reggroup);
+  return gdbpy_get_reggroup (iter_obj->reggroup);
 }
 
 /* Return a new gdb.RegisterGroupsIterator over all the register groups in
diff --git a/gdb/testsuite/gdb.python/py-arch-reg-groups.exp b/gdb/testsuite/gdb.python/py-arch-reg-groups.exp
index ea9aa77b0fa..093de2e7390 100644
--- a/gdb/testsuite/gdb.python/py-arch-reg-groups.exp
+++ b/gdb/testsuite/gdb.python/py-arch-reg-groups.exp
@@ -85,3 +85,22 @@ for { set i 0 } { $i < [llength $groups] } { incr i } {
     }
 }
 gdb_assert { $found_non_match == 0 } "all register groups match"
+
+# Check that we get the same register group descriptors from two
+# different iterators.
+gdb_py_test_silent_cmd \
+    "python iter1 = arch.register_groups ()" \
+    "get first all register group iterator" 0
+gdb_py_test_silent_cmd \
+    "python iter2 = arch.register_groups ()" \
+    "get second all register group iterator" 0
+gdb_py_test_silent_cmd \
+    [multi_line_input \
+	 "python" \
+	 "for r1, r2 in zip(iter1, iter2):" \
+	 "  if (r1.name != r2.name):"\
+	 "    raise gdb.GdbError (\"miss-matched names\")" \
+	 "  if (r1 != r2):" \
+	 "    raise gdb.GdbError (\"miss-matched objects\")" \
+	 "\004" ] \
+    "check names and objects match" 1
-- 
2.25.4


  parent reply	other threads:[~2020-07-08  8:48 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-08  8:48 [PATCH 0/2] Improvements to Python Register Descriptor API Andrew Burgess
2020-07-08  8:48 ` [PATCH 1/2] gdb/python: Reuse gdb.RegisterDescriptor objects where possible Andrew Burgess
2020-07-13 18:21   ` Tom Tromey
2020-07-08  8:48 ` Andrew Burgess [this message]
2020-07-13 18:23   ` [PATCH 2/2] gdb/python: Reuse gdb.RegisterGroup " Tom Tromey
2020-07-14 17:14 ` [PATCHv2 0/2] Improvements to Python Register Descriptor API Andrew Burgess
2020-07-14 17:14   ` [PATCHv2 1/2] gdb/python: Reuse gdb.RegisterDescriptor objects where possible Andrew Burgess
2020-07-22 13:10     ` Pedro Alves
2020-07-22 14:05       ` Andrew Burgess
2020-07-22 14:32         ` Pedro Alves
2020-07-22 15:10           ` Andrew Burgess
2020-07-14 17:14   ` [PATCHv2 2/2] gdb/python: Reuse gdb.RegisterGroup " Andrew Burgess
2020-07-21 18:26   ` [PATCHv2 0/2] Improvements to Python Register Descriptor API Tom Tromey

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=14e217dbd8200e3b07baf255e754bea738093d64.1594197735.git.andrew.burgess@embecosm.com \
    --to=andrew.burgess@embecosm.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).