public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Reduce manual reference counting in py-inferior.c
Date: Wed, 10 Jul 2019 18:27:00 -0000	[thread overview]
Message-ID: <20190710182725.102928.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=05b08ac1608c8355201db21fe4c871677466f0d5

commit 05b08ac1608c8355201db21fe4c871677466f0d5
Author: Tom Tromey <tromey@adacore.com>
Date:   Fri Jun 28 08:32:28 2019 -0600

    Reduce manual reference counting in py-inferior.c
    
    This patch changes py-inferior.c to use gdbpy_ref<> when possible,
    reducing the amount of manual reference counting.
    
    Tested on x86-64 Fedora 29.
    
    gdb/ChangeLog
    2019-07-10  Tom Tromey  <tromey@adacore.com>
    
    	* python/python-internal.h (create_thread_object): Return
    	gdbpy_ref.
    	* python/py-infthread.c (create_thread_object): Return gdbpy_ref.
    	* python/py-inferior.c (struct threadlist_entry): Add
    	constructor.
    	<thread_obj>: Now a gdbpy_ref.
    	(thread_to_thread_object): Update.
    	(add_thread_object): Use new.
    	(delete_thread_object): Use delete.
    	(infpy_threads): Update.
    	(py_free_inferior): Update.  Construct "inf_obj" after acquiring
    	GIL.

Diff:
---
 gdb/ChangeLog                | 15 +++++++++++++++
 gdb/python/py-inferior.c     | 36 +++++++++++++++++++-----------------
 gdb/python/py-infthread.c    |  8 ++++----
 gdb/python/python-internal.h |  2 +-
 4 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 410a8d8..41932c1 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,20 @@
 2019-07-10  Tom Tromey  <tromey@adacore.com>
 
+	* python/python-internal.h (create_thread_object): Return
+	gdbpy_ref.
+	* python/py-infthread.c (create_thread_object): Return gdbpy_ref.
+	* python/py-inferior.c (struct threadlist_entry): Add
+	constructor.
+	<thread_obj>: Now a gdbpy_ref.
+	(thread_to_thread_object): Update.
+	(add_thread_object): Use new.
+	(delete_thread_object): Use delete.
+	(infpy_threads): Update.
+	(py_free_inferior): Update.  Construct "inf_obj" after acquiring
+	GIL.
+
+2019-07-10  Tom Tromey  <tromey@adacore.com>
+
 	* valops.c (value_cast): Specialize error message for Ada.
 
 2019-07-10  Simon Marchi  <simon.marchi@polymtl.ca>
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 161e6dc..bf43012 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -30,8 +30,14 @@
 #include "py-event.h"
 #include "py-stopevent.h"
 
-struct threadlist_entry {
-  thread_object *thread_obj;
+struct threadlist_entry
+{
+  threadlist_entry (gdbpy_ref<thread_object> &&ref)
+    : thread_obj (std::move (ref))
+  {
+  }
+
+  gdbpy_ref<thread_object> thread_obj;
   struct threadlist_entry *next;
 };
 
@@ -301,7 +307,7 @@ thread_to_thread_object (thread_info *thr)
        thread != NULL;
        thread = thread->next)
     if (thread->thread_obj->thread == thr)
-      return gdbpy_ref<>::new_reference ((PyObject *) thread->thread_obj);
+      return gdbpy_ref<>::new_reference ((PyObject *) thread->thread_obj.get ());
 
   PyErr_SetString (PyExc_SystemError,
 		   _("could not find gdb thread object"));
@@ -311,7 +317,6 @@ thread_to_thread_object (thread_info *thr)
 static void
 add_thread_object (struct thread_info *tp)
 {
-  thread_object *thread_obj;
   inferior_object *inf_obj;
   struct threadlist_entry *entry;
 
@@ -320,8 +325,8 @@ add_thread_object (struct thread_info *tp)
 
   gdbpy_enter enter_py (python_gdbarch, python_language);
 
-  thread_obj = create_thread_object (tp);
-  if (!thread_obj)
+  gdbpy_ref<thread_object> thread_obj = create_thread_object (tp);
+  if (thread_obj == NULL)
     {
       gdbpy_print_stack ();
       return;
@@ -329,8 +334,7 @@ add_thread_object (struct thread_info *tp)
 
   inf_obj = (inferior_object *) thread_obj->inf_obj;
 
-  entry = XNEW (struct threadlist_entry);
-  entry->thread_obj = thread_obj;
+  entry = new threadlist_entry (std::move (thread_obj));
   entry->next = inf_obj->threads;
 
   inf_obj->threads = entry;
@@ -340,7 +344,7 @@ add_thread_object (struct thread_info *tp)
     return;
 
   gdbpy_ref<> event = create_thread_event_object (&new_thread_event_object_type,
-						  (PyObject *) thread_obj);
+						  (PyObject *) thread_obj.get ());
   if (event == NULL
       || evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0)
     gdbpy_print_stack ();
@@ -375,8 +379,7 @@ delete_thread_object (struct thread_info *tp, int ignore)
   *entry = (*entry)->next;
   inf_obj->nthreads--;
 
-  Py_DECREF (tmp->thread_obj);
-  xfree (tmp);
+  delete tmp;
 }
 
 static PyObject *
@@ -405,8 +408,9 @@ infpy_threads (PyObject *self, PyObject *args)
   for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads;
        i++, entry = entry->next)
     {
-      Py_INCREF (entry->thread_obj);
-      PyTuple_SET_ITEM (tuple, i, (PyObject *) entry->thread_obj);
+      PyObject *thr = (PyObject *) entry->thread_obj.get ();
+      Py_INCREF (thr);
+      PyTuple_SET_ITEM (tuple, i, thr);
     }
 
   return tuple;
@@ -859,24 +863,22 @@ infpy_dealloc (PyObject *obj)
 static void
 py_free_inferior (struct inferior *inf, void *datum)
 {
-  gdbpy_ref<inferior_object> inf_obj ((inferior_object *) datum);
   struct threadlist_entry *th_entry, *th_tmp;
 
   if (!gdb_python_initialized)
     return;
 
   gdbpy_enter enter_py (python_gdbarch, python_language);
+  gdbpy_ref<inferior_object> inf_obj ((inferior_object *) datum);
 
   inf_obj->inferior = NULL;
 
   /* Deallocate threads list.  */
   for (th_entry = inf_obj->threads; th_entry != NULL;)
     {
-      Py_DECREF (th_entry->thread_obj);
-
       th_tmp = th_entry;
       th_entry = th_entry->next;
-      xfree (th_tmp);
+      delete th_tmp;
     }
 
   inf_obj->nthreads = 0;
diff --git a/gdb/python/py-infthread.c b/gdb/python/py-infthread.c
index 8c556f9..f1c316c 100644
--- a/gdb/python/py-infthread.c
+++ b/gdb/python/py-infthread.c
@@ -36,17 +36,17 @@ extern PyTypeObject thread_object_type
       }								\
   } while (0)
 
-thread_object *
+gdbpy_ref<thread_object>
 create_thread_object (struct thread_info *tp)
 {
-  thread_object *thread_obj;
+  gdbpy_ref<thread_object> thread_obj;
 
   gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (tp->inf);
   if (inf_obj == NULL)
     return NULL;
 
-  thread_obj = PyObject_New (thread_object, &thread_object_type);
-  if (!thread_obj)
+  thread_obj.reset (PyObject_New (thread_object, &thread_object_type));
+  if (thread_obj == NULL)
     return NULL;
 
   thread_obj->thread = tp;
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 69ff1fe..e6a3fe0 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -468,7 +468,7 @@ PyObject *gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw);
 
 PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);
 
-thread_object *create_thread_object (struct thread_info *tp);
+gdbpy_ref<thread_object> create_thread_object (struct thread_info *tp);
 gdbpy_ref<> thread_to_thread_object (thread_info *thr);;
 gdbpy_ref<inferior_object> inferior_to_inferior_object (inferior *inf);


                 reply	other threads:[~2019-07-10 18:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20190710182725.102928.qmail@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@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).