From patchwork Mon Jul 1 15:07:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 33499 Received: (qmail 42040 invoked by alias); 1 Jul 2019 15:07:54 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 42032 invoked by uid 89); 1 Jul 2019 15:07:53 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=4058, acquiring, HContent-Transfer-Encoding:8bit X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 01 Jul 2019 15:07:51 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 848E356058; Mon, 1 Jul 2019 11:07:49 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Ik974FPyY8yX; Mon, 1 Jul 2019 11:07:49 -0400 (EDT) Received: from murgatroyd.Home (174-29-48-143.hlrn.qwest.net [174.29.48.143]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 2F9515605C; Mon, 1 Jul 2019 11:07:49 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Reduce manual reference counting in py-inferior.c Date: Mon, 1 Jul 2019 09:07:47 -0600 Message-Id: <20190701150747.19075-1-tromey@adacore.com> MIME-Version: 1.0 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-01 Tom Tromey * 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. : 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. --- 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/python/py-inferior.c b/gdb/python/py-inferior.c index 7e7d518c557..9a979897eb0 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 &&ref) + : thread_obj (std::move (ref)) + { + } + + gdbpy_ref 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_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 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 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 8c556f92ea8..f1c316c1f17 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 create_thread_object (struct thread_info *tp) { - thread_object *thread_obj; + gdbpy_ref thread_obj; gdbpy_ref 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 69ff1fe30de..e6a3fe0ec1f 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 create_thread_object (struct thread_info *tp); gdbpy_ref<> thread_to_thread_object (thread_info *thr);; gdbpy_ref inferior_to_inferior_object (inferior *inf);