From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 68452 invoked by alias); 3 Dec 2016 00:05:06 -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 68372 invoked by uid 89); 3 Dec 2016 00:05:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.8 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=transfers, policy X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 03 Dec 2016 00:04:55 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 159D6793DC; Sat, 3 Dec 2016 00:04:54 +0000 (UTC) Received: from [127.0.0.1] (ovpn03.gateway.prod.ext.phx2.redhat.com [10.5.9.3]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uB304niX006961; Fri, 2 Dec 2016 19:04:50 -0500 Subject: Re: [RFA 1/8] Add gdb_ref_ptr.h To: Tom Tromey , gdb-patches@sourceware.org References: <1480395946-10924-1-git-send-email-tom@tromey.com> <1480395946-10924-2-git-send-email-tom@tromey.com> From: Pedro Alves Message-ID: <13207c13-89b0-c5e3-fbd8-05cc19fda4b2@redhat.com> Date: Sat, 03 Dec 2016 00:05:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <1480395946-10924-2-git-send-email-tom@tromey.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2016-12/txt/msg00124.txt.bz2 On 11/29/2016 05:05 AM, Tom Tromey wrote: > This adds a new gdb_ref_ptr.h, that implements a reference-counting > smart pointer class, where the user of the class supplies a > reference-counting policy object. > > This class will be used in the next patch, which changes most explicit > BFD reference counts to use this new type. Meanwhile, this patch > changes gdbpy_ref to be a specialization of this new class. > > This change required adding new nullptr_t overloads some operators in > gdb_ref_ptr.h. I suspect this was needed because some Python header > redefines NULL, but I'm not certain. > > 2016-11-28 Tom Tromey > > * common/gdb_ref_ptr.h: New file. > * python/py-ref.h (struct gdbpy_ref_policy): New. > (gdbpy_ref): Now a typedef. > --- > gdb/ChangeLog | 6 ++ > gdb/common/gdb_ref_ptr.h | 209 +++++++++++++++++++++++++++++++++++++++++++++++ > gdb/python/py-ref.h | 135 ++---------------------------- > 3 files changed, 224 insertions(+), 126 deletions(-) > create mode 100644 gdb/common/gdb_ref_ptr.h > > diff --git a/gdb/ChangeLog b/gdb/ChangeLog > index 8cbe8ad..f455f7f 100644 > --- a/gdb/ChangeLog > +++ b/gdb/ChangeLog > @@ -1,5 +1,11 @@ > 2016-11-28 Tom Tromey > > + * common/gdb_ref_ptr.h: New file. > + * python/py-ref.h (struct gdbpy_ref_policy): New. > + (gdbpy_ref): Now a typedef. > + > +2016-11-28 Tom Tromey > + > * utils.h (make_cleanup_htab_delete): Don't declare. > * utils.c (do_htab_delete_cleanup, make_cleanup_htab_delete): > Remove. > diff --git a/gdb/common/gdb_ref_ptr.h b/gdb/common/gdb_ref_ptr.h > new file mode 100644 > index 0000000..cc8ba94 > --- /dev/null > +++ b/gdb/common/gdb_ref_ptr.h > @@ -0,0 +1,209 @@ > +/* Reference-counted smart pointer 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_REF_PTR_H > +#define GDB_REF_PTR_H > + > +#include > + > +namespace gdb > +{ > + > +/* An instance of this class either holds a reference to a > + reference-counted object or is "NULL". Reference counting is > + handled externally by a policy class. If the object holds a > + reference, then when the object is destroyed, the reference is > + decref'd. > + > + Normally an instance is constructed using a pointer. 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 pointer is disallowed to > + avoid confusion about whether this acquires a new reference; > + instead use the "reset" method -- which, like the pointer > + constructor, transfers ownership. > + > + The policy class must provide two static methods: > + void incref (T *); > + void decref (T *); > +*/ > +template > +class ref_ptr BTW, I noticed today that the GCC standards say: "Template parameter names should use CamelCase, following the C++ Standard." and they do seem to follow it. I think it's a good idea. Would you mind adjusting accordingly? I also noticed the "self/other" naming in the global operators: +template +inline bool operator== (const ref_ptr &self, + const ref_ptr &other) +{ I'd find it pedantically more correct to write lhs/rhs, since there's no actual this/self here. Could you tweak that too? Thanks, Pedro Alves