Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 1/5] Introduce specialized versions of gdbpy_ref
Date: Mon, 23 Jan 2017 22:43:00 -0000	[thread overview]
Message-ID: <20170123224004.8893-2-simon.marchi@ericsson.com> (raw)
In-Reply-To: <20170123224004.8893-1-simon.marchi@ericsson.com>

From: Simon Marchi <simon.marchi@polymtl.ca>

I was playing with the Python API implementation a bit, and I thought we
could improve/simplify the gdbpy reference counting a bit for our own
Python types.  We currently have a single type, gdbpy_ref, which holds a
reference to a PyObject*.  This means that if we want to wrap an
inferior_object* (which is a sub-class-ish of PyObject), we need to cast
it when constructing the reference:

  gdbpy_ref ref ((PyObject *) an_inferior_object);

as well as when getting the reference

  inferior_object *obj = (inferior_object *) ref.get ();

This patch generalizes the gdbpy_ref concept so that it can hold a reference to
other types than PyObject.  This should mean less casting when dealing with
references to our specialized Python objects.

The main new part is the template class gdbpy_ref_base, which extends
gdb::ref_ptr with the gdbpy_ref_policy policy, much like gdbpy_ref did
previoulsy.  However, the referenced object type is now the generic type
T instead of PyObject*.  The gdbpy_ref_policy is generalized as well to
match.  Note that we don't need to cast the pointers in incref/decref
since Py_INCREF/Py_DECREF already do that.

Specializations of gdbpy_ref_base can then be added for our various
Python types.  This patch only adds gdbpy_ref to match the one that was
already there.  So no behavioral changes are expected.

We must make sure to only use gdbpy_ref_base on objects that actually
are Python objects.  For example, gdbpy_ref_base<thread_info> would make
not sense.  Since the "inheritance" from the PyObject type is done in a
C way (using PyObject_HEAD), I don't know how we can check at
compile-time that we are not using it with a wrong type.  If you have an
idea on how to do that, let me know.  We would need to check that there
exists a field named ob_base.  Bonus points for ensuring that its type
is PyObject.  More bonus points for ensuring that it's the first field
in the structure.

For convenience, I added a get_py_obj method to gdbpy_ref_base, which
returns the pointer casted to PyObject*, something we need to do
relatively often).

gdb/ChangeLog:

	* python/py-ref.h (gdbpy_ref_policy): Add template.
	(gdbpy_ref_base): New class.
	(gdbpy_ref): Define in terms of gdbpy_ref_base instead of
	gdb::ref_ptr.
---
 gdb/python/py-ref.h | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index b2479bf656..8b3b7732cc 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -23,20 +23,49 @@
 #include "common/gdb_ref_ptr.h"
 
 /* A policy class for gdb::ref_ptr for Python reference counting.  */
+
+template <typename T>
 struct gdbpy_ref_policy
 {
-  static void incref (PyObject *ptr)
+  static void incref (T *ptr)
   {
     Py_INCREF (ptr);
   }
 
-  static void decref (PyObject *ptr)
+  static void decref (T *ptr)
   {
     Py_DECREF (ptr);
   }
 };
 
-/* A gdb::ref_ptr that has been specialized for Python objects.  */
-typedef gdb::ref_ptr<PyObject, gdbpy_ref_policy> gdbpy_ref;
+/* Reference counting specialized for Python objects.
+
+   This class must only be used with Python types, i.e. types declared with
+   PyObject_HEAD as their first "field".  */
+
+template <typename T>
+class gdbpy_ref_base : public gdb::ref_ptr<T, gdbpy_ref_policy<T>>
+{
+public:
+
+  /* Create a new NULL instance.  */
+
+  gdbpy_ref_base ()
+  : gdb::ref_ptr<T, gdbpy_ref_policy<T>> ()
+  {}
+
+  explicit gdbpy_ref_base (T *ptr)
+  : gdb::ref_ptr<T, gdbpy_ref_policy<T>> (ptr)
+  {}
+
+  /* Return a pointer to the owned Python object as a generic PyObject.  */
+
+  PyObject *get_py_obj ()
+  { return (PyObject *) this->get(); }
+};
+
+/* Specializations of gdbpy_ref_base for concrete Python object types.  */
+
+typedef gdbpy_ref_base<PyObject> gdbpy_ref;
 
 #endif /* GDB_PYTHON_REF_H */
-- 
2.11.0


  reply	other threads:[~2017-01-23 22:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-23 22:43 [PATCH 0/5] Improve Python Inferior reference handling + fix a bug Simon Marchi
2017-01-23 22:43 ` Simon Marchi [this message]
2017-01-24 15:54   ` [PATCH 1/5] Introduce specialized versions of gdbpy_ref Tom Tromey
2017-01-24 16:18     ` Simon Marchi
2017-02-09 11:58   ` Pedro Alves
2017-02-09 16:18     ` Simon Marchi
2017-01-23 22:43 ` [PATCH 4/5] Make Python inferior-related internal functions return a gdbpy_inf_ref Simon Marchi
2017-01-24 16:15   ` Simon Marchi
2017-02-09 12:30   ` Pedro Alves
2017-02-09 16:39     ` Simon Marchi
2017-01-23 22:43 ` [PATCH 2/5] Add Python Inferior object debug traces Simon Marchi
2017-01-23 22:43 ` [PATCH 5/5] Add missing incref when creating Inferior Python object Simon Marchi
2017-02-25 18:41   ` Simon Marchi
2017-04-27 21:13     ` [pushed master+8.0] " Simon Marchi
2017-01-23 22:43 ` [PATCH 3/5] Make Python inferior-related internal functions return inferior_object* Simon Marchi
2017-01-24  0:03   ` Pedro Alves

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=20170123224004.8893-2-simon.marchi@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@polymtl.ca \
    /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