From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 35026 invoked by alias); 12 Nov 2016 17:31:56 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 35009 invoked by uid 89); 12 Nov 2016 17:31:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=wrapping, transferred, transfer X-HELO: gproxy5-pub.mail.unifiedlayer.com Received: from gproxy5-pub.mail.unifiedlayer.com (HELO gproxy5-pub.mail.unifiedlayer.com) (67.222.38.55) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Sat, 12 Nov 2016 17:31:44 +0000 Received: (qmail 28481 invoked by uid 0); 12 Nov 2016 17:31:43 -0000 Received: from unknown (HELO CMOut01) (10.0.90.82) by gproxy5.mail.unifiedlayer.com with SMTP; 12 Nov 2016 17:31:43 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id 75Xe1u00c2f2jeq015XhM0; Sat, 12 Nov 2016 10:31:41 -0700 X-Authority-Analysis: v=2.1 cv=beT4Do/B c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=L24OOQBejmoA:10 a=20KFwNOVAAAA:8 a=zstS-IiYAAAA:8 a=KKAkSRfTAAAA:8 a=mDV3o1hIAAAA:8 a=k62MUa6VvfBMqlAaF2oA:9 a=e_O65bzb51kRm2y5VmPK:22 a=4G6NA9xxw8l3yy4pmD5M:22 a=cvBusfyB2V15izCimMoJ:22 a=_FVE-zBwftR9WsbkzFJk:22 Received: from 174-16-143-211.hlrn.qwest.net ([174.16.143.211]:35632 helo=bapiya) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_1) (envelope-from ) id 1c5c9U-0002EK-2N; Sat, 12 Nov 2016 10:31:40 -0700 From: Tom Tromey To: Pedro Alves Cc: Tom Tromey , gdb-patches@sourceware.org Subject: Re: [RFA 01/14] Introduce py-ref.h References: <1478497656-11832-1-git-send-email-tom@tromey.com> <1478497656-11832-2-git-send-email-tom@tromey.com> <7df8b22f-cd05-c0d6-5ede-e9dc29bca6e2@redhat.com> Date: Sat, 12 Nov 2016 17:31:00 -0000 In-Reply-To: <7df8b22f-cd05-c0d6-5ede-e9dc29bca6e2@redhat.com> (Pedro Alves's message of "Thu, 10 Nov 2016 23:48:49 +0000") Message-ID: <8760nsiozp.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-BWhitelist: no X-Exim-ID: 1c5c9U-0002EK-2N X-Source-Sender: 174-16-143-211.hlrn.qwest.net (bapiya) [174.16.143.211]:35632 X-Source-Auth: tom+tromey.com X-Email-Count: 10 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-SW-Source: 2016-11/txt/msg00311.txt.bz2 >>>>> "Pedro" == Pedro Alves writes: Pedro> I think this should say something about supporting explicitly Pedro> wrapping a NULL PyObject *, and how to check whether there's Pedro> a managed object. [... other review comments...] Here's a new version of this patch, which I think addresses all the comments, from both you and others. Tom diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 99edafc..da23621 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,7 @@ +2016-11-10 Tom Tromey + + * python/py-ref.h: New file. + 2016-11-11 Yao Qi * spu-tdep.c (spu_software_single_step): Don't call diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h new file mode 100644 index 0000000..ae3b5cc --- /dev/null +++ b/gdb/python/py-ref.h @@ -0,0 +1,158 @@ +/* Python reference-holding class + + Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef GDB_PYTHON_REF_H +#define GDB_PYTHON_REF_H + +/* An instance of this class either holds a reference to a PyObject, + or is "NULL". If it holds a reference, then when the object is + destroyed, the PyObject is decref'd. + + Normally an instance is constructed using a PyObject*. This sort + of initialization lets this class manage the lifetime of that + reference. + + Assignment and copy construction will make a new reference as + appropriate. Assignment from a plain PyObject* is disallowed to + avoid confusion about whether this acquires a new reference; + instead use the "reset" method -- which, like the PyObject* + constructor, transfers ownership. +*/ +class gdbpy_ref +{ + public: + + /* Create a new NULL instance. */ + gdbpy_ref () + : m_obj (NULL) + { + } + + /* Create a new instance. OBJ is a reference, management of which + is now transferred to this class. */ + explicit gdbpy_ref (PyObject *obj) + : m_obj (obj) + { + } + + /* Copy another instance. */ + gdbpy_ref (const gdbpy_ref &other) + : m_obj (other.m_obj) + { + if (m_obj != NULL) + Py_INCREF (m_obj); + } + + /* Transfer ownership from OTHER. */ + gdbpy_ref (gdbpy_ref &&other) + : m_obj (other.m_obj) + { + other.m_obj = NULL; + } + + /* Destroy this instance. */ + ~gdbpy_ref () + { + Py_XDECREF (m_obj); + } + + /* Copy another instance. */ + gdbpy_ref &operator= (const gdbpy_ref &other) + { + /* Do nothing on self-assignment. */ + if (this != &other) + { + Py_XDECREF (m_obj); + m_obj = other.m_obj; + if (m_obj != NULL) + Py_INCREF (m_obj); + } + return *this; + } + + /* Transfer ownership from OTHER. */ + gdbpy_ref &operator= (gdbpy_ref &&other) + { + /* Note that this handles self-assignment properly. */ + std::swap (m_obj, other.m_obj); + return *this; + } + + /* Change this instance's referent. OBJ is a reference, management + of which is now transferred to this class. */ + void reset (PyObject *obj) + { + Py_XDECREF (m_obj); + m_obj = obj; + } + + /* Return this instance's referent. In Python terms this is a + borrowed pointer. */ + PyObject *get () const + { + return m_obj; + } + + /* Return this instance's referent, and stop managing this + reference. The caller is now responsible for the ownership of + the reference. */ + PyObject *release () + { + PyObject *result = m_obj; + + m_obj = NULL; + return result; + } + + private: + + PyObject *m_obj; +}; + +inline bool operator== (const gdbpy_ref &self, const gdbpy_ref &other) +{ + return self.get () == other.get (); +} + +inline bool operator== (const gdbpy_ref &self, const PyObject *other) +{ + return self.get () == other; +} + +inline bool operator== (const PyObject *self, const gdbpy_ref &other) +{ + return self == other.get (); +} + +inline bool operator!= (const gdbpy_ref &self, const gdbpy_ref &other) +{ + return self.get () != other.get (); +} + +inline bool operator!= (const gdbpy_ref &self, const PyObject *other) +{ + return self.get () != other; +} + +inline bool operator!= (const PyObject *self, const gdbpy_ref &other) +{ + return self != other.get (); +} + +#endif /* GDB_PYTHON_REF_H */