From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway22.websitewelcome.com (gateway22.websitewelcome.com [192.185.47.129]) by sourceware.org (Postfix) with ESMTPS id 18E0739B680C for ; Wed, 28 Apr 2021 01:01:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 18E0739B680C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway22.websitewelcome.com (Postfix) with ESMTP id B762673406 for ; Tue, 27 Apr 2021 20:01:41 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id bYaDlCxDv1cHebYaDlFDbW; Tue, 27 Apr 2021 20:01:41 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Nc9Mx0TAFpvQ4s7NE1Tc3Hhz5uUQjGkp/VskVValDXs=; b=DywSkQaL6DxyZKBF/UL1bpmtNW N4FXTLS6RP9AMg4oLbPrSpUpu2v0l5GgzNkfV1IlbJqZd0YEFCQh/DU2+HEx/d1XMgHhl+Tm09yJJ jywAOSEg6zRqNSHBXBRgDS1O7; Received: from 97-122-70-176.hlrn.qwest.net ([97.122.70.176]:32966 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1lbYaD-003tiR-CQ; Tue, 27 Apr 2021 19:01:41 -0600 From: Tom Tromey To: gcc-patches@gcc.gnu.org Cc: Tom Tromey Subject: [PATCH v2 08/21] libcc1: add deleter objects Date: Tue, 27 Apr 2021 19:01:06 -0600 Message-Id: <20210428010119.806184-9-tom@tromey.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210428010119.806184-1-tom@tromey.com> References: <20210428010119.806184-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.70.176 X-Source-L: No X-Exim-ID: 1lbYaD-003tiR-CQ X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-70-176.hlrn.qwest.net (localhost.localdomain) [97.122.70.176]:32966 X-Source-Auth: tom+tromey.com X-Email-Count: 9 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3032.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, SPF_HELO_PASS, SPF_NEUTRAL, TXREP, URIBL_CSS, URIBL_CSS_A autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Apr 2021 01:01:46 -0000 This adds deleter objects for various kinds of protocol pointers to libcc1. Existing specializations of argument_wrapper are then replaced with a single specialization that handles all pointer types via the appropriate deleter. The result here is a bit nicer because the argument_wrapper boilerplate code is completely shared, leaving just the memory-management detail to the particular specializations. libcc1/ChangeLog 2021-04-27 Tom Tromey * rpc.hh (struct deleter): New template class and specializations. (argument_wrapper): Remove specializations. Add specialization for any pointer type. --- libcc1/ChangeLog | 7 ++ libcc1/rpc.hh | 176 ++++++++++++----------------------------------- 2 files changed, 52 insertions(+), 131 deletions(-) diff --git a/libcc1/rpc.hh b/libcc1/rpc.hh index a3631cb5d7e2..4e00d61ee98d 100644 --- a/libcc1/rpc.hh +++ b/libcc1/rpc.hh @@ -22,6 +22,7 @@ along with GCC; see the file COPYING3. If not see #include "status.hh" #include "connection.hh" +#include namespace cc1_plugin { @@ -54,182 +55,95 @@ namespace cc1_plugin T m_object; }; - // Specialization for any kind of pointer. This is declared but not - // defined to avoid bugs if a new pointer type is introduced into - // the API. Instead you will just get a compilation error. - template - class argument_wrapper; + // Any pointer type requires a deleter object that knows how to + // clean up. These are used in multiple places. + template struct deleter; - // Specialization for string types. template<> - class argument_wrapper + struct deleter { - public: - argument_wrapper () : m_object (NULL) { } - ~argument_wrapper () + void operator() (char *s) { - delete[] m_object; + delete[] s; } - - argument_wrapper (const argument_wrapper &) = delete; - argument_wrapper &operator= (const argument_wrapper &) = delete; - - operator const char * () const - { - return m_object; - } - - status unmarshall (connection *conn) - { - return ::cc1_plugin::unmarshall (conn, &m_object); - } - - private: - - char *m_object; }; - // Specialization for gcc_type_array. template<> - class argument_wrapper + struct deleter { - public: - argument_wrapper () : m_object (NULL) { } - ~argument_wrapper () + void operator() (gcc_type_array *p) { - // It would be nicer if gcc_type_array could have a destructor. - // But, it is in code shared with gdb and cannot. - if (m_object != NULL) - delete[] m_object->elements; - delete m_object; + delete[] p->elements; + delete p; } - - argument_wrapper (const argument_wrapper &) = delete; - argument_wrapper &operator= (const argument_wrapper &) = delete; - - operator const gcc_type_array * () const - { - return m_object; - } - - status unmarshall (connection *conn) - { - return ::cc1_plugin::unmarshall (conn, &m_object); - } - - private: - - gcc_type_array *m_object; }; #ifdef GCC_CP_INTERFACE_H - // Specialization for gcc_vbase_array. template<> - class argument_wrapper + struct deleter { - public: - argument_wrapper () : m_object (NULL) { } - ~argument_wrapper () - { - // It would be nicer if gcc_type_array could have a destructor. - // But, it is in code shared with gdb and cannot. - if (m_object != NULL) - { - delete[] m_object->flags; - delete[] m_object->elements; - } - delete m_object; - } - - argument_wrapper (const argument_wrapper &) = delete; - argument_wrapper &operator= (const argument_wrapper &) = delete; - - operator const gcc_vbase_array * () const + void operator() (gcc_vbase_array *p) { - return m_object; - } - - status unmarshall (connection *conn) - { - return ::cc1_plugin::unmarshall (conn, &m_object); + delete[] p->flags; + delete[] p->elements; + delete p; } - - private: - - gcc_vbase_array *m_object; }; - // Specialization for gcc_cp_template_args. template<> - class argument_wrapper + struct deleter { - public: - argument_wrapper () : m_object (NULL) { } - ~argument_wrapper () - { - // It would be nicer if gcc_type_array could have a destructor. - // But, it is in code shared with gdb and cannot. - if (m_object != NULL) - { - delete[] m_object->elements; - delete[] m_object->kinds; - } - delete m_object; - } - - argument_wrapper (const argument_wrapper &) = delete; - argument_wrapper &operator= (const argument_wrapper &) = delete; - - operator const gcc_cp_template_args * () const + void operator() (gcc_cp_template_args *p) { - return m_object; + delete[] p->elements; + delete[] p->kinds; + delete p; } + }; - status unmarshall (connection *conn) + template<> + struct deleter + { + void operator() (gcc_cp_function_args *p) { - return ::cc1_plugin::unmarshall (conn, &m_object); + delete[] p->elements; + delete p; } - - private: - - gcc_cp_template_args *m_object; }; - // Specialization for gcc_cp_function_args. - template<> - class argument_wrapper +#endif // GCC_CP_INTERFACE_H + + // Specialization for any kind of pointer. + template + class argument_wrapper { public: - argument_wrapper () : m_object (NULL) { } - ~argument_wrapper () - { - // It would be nicer if gcc_type_array could have a destructor. - // But, it is in code shared with gdb and cannot. - if (m_object != NULL) - { - delete[] m_object->elements; - } - delete m_object; - } + argument_wrapper () = default; + ~argument_wrapper () = default; argument_wrapper (const argument_wrapper &) = delete; argument_wrapper &operator= (const argument_wrapper &) = delete; - operator const gcc_cp_function_args * () const + typedef typename std::remove_const::type type; + + operator const type * () const { - return m_object; + return m_object.get (); } status unmarshall (connection *conn) { - return ::cc1_plugin::unmarshall (conn, &m_object); + type *ptr; + if (!::cc1_plugin::unmarshall (conn, &ptr)) + return FAIL; + m_object.reset (ptr); + return OK; } private: - gcc_cp_function_args *m_object; + std::unique_ptr> m_object; }; -#endif /* GCC_CP_INTERFACE_H */ // There are two kinds of template functions here: "call" and // "callback". "call" is implemented with variadic templates, but -- 2.26.2