public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: Pedro Alves <pedro@palves.net>
Cc: gdb-patches@sourceware.org, Tom Tromey <tom@tromey.com>
Subject: Re: [PATCHv2 1/2] gdb/python: Reuse gdb.RegisterDescriptor objects where possible
Date: Wed, 22 Jul 2020 15:05:46 +0100	[thread overview]
Message-ID: <20200722140546.GF853475@embecosm.com> (raw)
In-Reply-To: <1eaf1e55-4e95-4fe6-7c23-400bf2168e70@palves.net>

* Pedro Alves <pedro@palves.net> [2020-07-22 14:10:17 +0100]:

> Hi Andrew,
> 
> Some nits below.
> 
> On 7/14/20 6:14 PM, Andrew Burgess wrote:
> 
> >  
> > +/* Token to access per-gdbarch data related to register descriptors.  */
> > +static struct gdbarch_data *gdbpy_register_object_data = NULL;
> > +
> >  /* Structure for iterator over register descriptors.  */
> >  typedef struct {
> >    PyObject_HEAD
> > @@ -81,6 +84,17 @@ typedef struct {
> >  extern PyTypeObject reggroup_object_type
> >      CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("reggroup_object");
> >  
> > +/* Associates a vector of gdb.RegisterDescriptor objects with GDBARCH as
> > +   gdbarch_data via the gdbarch post init registration mechanism
> > +   (gdbarch_data_register_post_init).  */
> > +
> > +static void *
> > +gdbpy_register_object_data_init (struct gdbarch *gdbarch)
> > +{
> > +  std::vector<gdbpy_ref<>> *vec = new (std::vector<gdbpy_ref<>>);
> > +  return (void *) vec;
> 
> This could just be:
> 
>   return new std::vector<gdbpy_ref<>>;
> 
> 
> > +}
> > +
> >  /* Create a new gdb.RegisterGroup object wrapping REGGROUP.  */
> >  
> >  static PyObject *
> > @@ -117,20 +131,38 @@ gdbpy_reggroup_name (PyObject *self, void *closure)
> >    return gdbpy_reggroup_to_string (self);
> >  }
> >  
> > -/* Create an return a new gdb.RegisterDescriptor object.  */
> > -static PyObject *
> > -gdbpy_new_register_descriptor (struct gdbarch *gdbarch,
> > +/* Return a gdb.RegisterDescriptor object for REGNUM from GDBARCH.  For
> > +   each REGNUM (in GDBARCH) only one descriptor is ever created, which is
> > +   then cached on the GDBARCH.  */
> > +
> > +static gdbpy_ref<>
> > +gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
> >  			       int regnum)
> >  {
> > -  /* Create a new object and fill in its details.  */
> > -  register_descriptor_object *reg
> > -    = PyObject_New (register_descriptor_object,
> > -		    &register_descriptor_object_type);
> > -  if (reg == NULL)
> > -    return NULL;
> > -  reg->regnum = regnum;
> > -  reg->gdbarch = gdbarch;
> > -  return (PyObject *) reg;
> > +  auto vec = (std::vector<gdbpy_ref<>> *) gdbarch_data
> > +    (gdbarch, gdbpy_register_object_data);
> 
> Wrap the rhs in parens so the second line is properly aligned,
> per the GNU standard's "so that emacs aligns it automatically"
> rule:
> 
>   auto vec = ((std::vector<gdbpy_ref<>> *) gdbarch_data
> 	      (gdbarch, gdbpy_register_object_data));
> 
> Or you could put the = in the next line, so that it aligns
> without the parens:
> 
>   auto vec
>     = (std::vector<gdbpy_ref<>> *) gdbarch_data (gdbarch,
> 						 gdbpy_register_object_data);
> 
> Note, I think it's more idiomatic to write the * for pointers:
> 
>   auto *vec = ....
> 
> But since vec can't be NULL, I'd write a reference instead:
> 
>   auto &vec
>     = *(std::vector<gdbpy_ref<>> *) gdbarch_data (gdbarch,
> 						  gdbpy_register_object_data);
> 
> > +
> > +  /* Ensure that we have enough entries in the vector.  */
> > +  if (vec->size () <= regnum)
> > +    vec->resize ((regnum + 1), nullptr);
> > +
> > +  /* If we don't already have a descriptor for REGNUM in GDBARCH then
> > +     create one now.  */
> > +  if (vec->at (regnum) == nullptr)
> 
> Is there a reason you're using at?  Was it to avoid
> 
>  (*vec)[regnum]
> 
> or was it really about out-of-range errors?
> 
> If such as exception were thrown, it would be a logic
> bug in GDB.  And note that we don't catch it anywhere, so
> it would bring down GDB.
> 
> There's a school of thought that claims that at should not
> really exist, and I agree with it.  :-)
> 
> If you go the reference route, then you can write the natural:
> 
>   if (vec[regnum] = nullptr)
> 
> > +    {
> > +      gdbpy_ref <register_descriptor_object> reg
> > +	(PyObject_New (register_descriptor_object,
> > +		       &register_descriptor_object_type));
> > +      if (reg == NULL)
> > +	return NULL;
> > +      reg->regnum = regnum;
> > +      reg->gdbarch = gdbarch;
> > +      vec->at (regnum) = gdbpy_ref<> ((PyObject *) reg.release ());
> > +    }
> > +
> > +  /* Grab the register descriptor from the vector, the reference count is
> > +     automatically incremented thanks to gdbpy_ref.  */
> > +  return vec->at (regnum);
> 
> Ditto in these two other spots.

Pedro,

Thanks for your feedback.  As this patch was already merged, the patch
below applies on top of current master to address these issues.

If I don't hear anything I'll apply this in a couple of days.

Thanks,
Andrew

---

commit ed3d57aca93bfe3e0eaa5888486b9fc84d06a0c6
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Wed Jul 22 14:57:55 2020 +0100

    gdb/python: Use reference not pointer in py-registers.c
    
    Pedro's review comments arrived after I'd already committed this
    change:
    
      commit f7306dac19c502232f766c3881313857915f330d
      Date:   Tue Jul 7 15:00:30 2020 +0100
    
          gdb/python: Reuse gdb.RegisterDescriptor objects where possible
    
    See:
    
      https://sourceware.org/pipermail/gdb-patches/2020-July/170726.html
    
    There should be no user visible changes after this commit.
    
    gdb/ChangeLog:
    
            * python/py-registers.c (gdbpy_register_object_data_init): Remove
            redundant local variable.
            (gdbpy_get_register_descriptor): Extract descriptor vector as a
            reference, not pointer, update code accordingly.

diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index 9396498cc34..f64ca3c401b 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -92,8 +92,7 @@ extern PyTypeObject reggroup_object_type
 static void *
 gdbpy_register_object_data_init (struct gdbarch *gdbarch)
 {
-  std::vector<gdbpy_ref<>> *vec = new (std::vector<gdbpy_ref<>>);
-  return (void *) vec;
+  return new std::vector<gdbpy_ref<>>;
 }
 
 /* Return a gdb.RegisterGroup object wrapping REGGROUP.  The register
@@ -158,16 +157,17 @@ static gdbpy_ref<>
 gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
 			       int regnum)
 {
-  auto vec = (std::vector<gdbpy_ref<>> *) gdbarch_data
-    (gdbarch, gdbpy_register_object_data);
+  auto &vec
+    = *(std::vector<gdbpy_ref<>> *) gdbarch_data (gdbarch,
+						  gdbpy_register_object_data);
 
   /* Ensure that we have enough entries in the vector.  */
-  if (vec->size () <= regnum)
-    vec->resize ((regnum + 1), nullptr);
+  if (vec.size () <= regnum)
+    vec.resize ((regnum + 1), nullptr);
 
   /* If we don't already have a descriptor for REGNUM in GDBARCH then
      create one now.  */
-  if (vec->at (regnum) == nullptr)
+  if (vec[regnum] == nullptr)
     {
       gdbpy_ref <register_descriptor_object> reg
 	(PyObject_New (register_descriptor_object,
@@ -176,12 +176,12 @@ gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
 	return NULL;
       reg->regnum = regnum;
       reg->gdbarch = gdbarch;
-      vec->at (regnum) = gdbpy_ref<> ((PyObject *) reg.release ());
+      vec[regnum] = gdbpy_ref<> ((PyObject *) reg.release ());
     }
 
   /* Grab the register descriptor from the vector, the reference count is
      automatically incremented thanks to gdbpy_ref.  */
-  return vec->at (regnum);
+  return vec[regnum];
 }
 
 /* Convert the register descriptor to a string.  */

  reply	other threads:[~2020-07-22 14:05 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 ` [PATCH 2/2] gdb/python: Reuse gdb.RegisterGroup " Andrew Burgess
2020-07-13 18:23   ` 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 [this message]
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=20200722140546.GF853475@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    --cc=tom@tromey.com \
    /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).