public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gcc-patches@gcc.gnu.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 08/21] libcc1: add deleter objects
Date: Tue, 27 Apr 2021 19:01:06 -0600	[thread overview]
Message-ID: <20210428010119.806184-9-tom@tromey.com> (raw)
In-Reply-To: <20210428010119.806184-1-tom@tromey.com>

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  <tom@tromey.com>

	* 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 <memory>
 
 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<typename T>
-  class argument_wrapper<const T *>;
+  // Any pointer type requires a deleter object that knows how to
+  // clean up.  These are used in multiple places.
+  template<typename T> struct deleter;
 
-  // Specialization for string types.
   template<>
-  class argument_wrapper<const char *>
+  struct deleter<char>
   {
-  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<const gcc_type_array *>
+  struct deleter<gcc_type_array>
   {
-  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<const gcc_vbase_array *>
+  struct deleter<gcc_vbase_array>
   {
-  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<const gcc_cp_template_args *>
+  struct deleter<gcc_cp_template_args>
   {
-  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<gcc_cp_function_args>
+  {
+    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<const gcc_cp_function_args *>
+#endif // GCC_CP_INTERFACE_H
+
+  // Specialization for any kind of pointer.
+  template<typename T>
+  class argument_wrapper<T *>
   {
   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<T>::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<type, deleter<type>> 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


  parent reply	other threads:[~2021-04-28  1:01 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-28  1:00 [PATCH v2 00/21] C++11-based improvements for libcc1 Tom Tromey
2021-04-28  1:00 ` [PATCH v2 01/21] libcc1: use templates to unmarshall enums Tom Tromey
2021-04-28 18:27   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 02/21] libcc1: use "override" Tom Tromey
2021-04-28 15:53   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 03/21] libcc1: inline some simple methods Tom Tromey
2021-04-28 15:54   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 04/21] libcc1: delete copy constructor and assignment operators Tom Tromey
2021-04-28 15:55   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 05/21] libcc1: use variadic templates for "call" Tom Tromey
2021-04-28 18:28   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 06/21] libcc1: use variadic templates for "rpc" Tom Tromey
2021-04-28 18:28   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 07/21] libcc1: use std::vector when building function types Tom Tromey
2021-04-28 16:01   ` Jeff Law
2021-04-28 19:56     ` Tom Tromey
2021-04-28 20:07       ` Jeff Law
2021-04-28  1:01 ` Tom Tromey [this message]
2021-04-28 21:06   ` [PATCH v2 08/21] libcc1: add deleter objects Jeff Law
2021-04-28  1:01 ` [PATCH v2 09/21] libcc1: add more uses of 'deleter' Tom Tromey
2021-04-29 12:43   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 10/21] libcc1: use unique_ptr more Tom Tromey
2021-04-29 12:44   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 11/21] libcc1: unify compiler handling Tom Tromey
2021-04-29 12:47   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 12/21] libcc1: use foreach Tom Tromey
2021-04-28 16:04   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 13/21] libcc1: use static_assert Tom Tromey
2021-04-28 16:06   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 14/21] libcc1: share basic context code Tom Tromey
2021-04-29 12:50   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 15/21] libcc1: share GDB plugin code Tom Tromey
2021-04-30 14:50   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 16/21] libcc1: use GCC_FE_VERSION_1 in C++ plugin Tom Tromey
2021-04-28 16:06   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 17/21] libcc1: share the GCC interface code Tom Tromey
2021-04-30 15:07   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 18/21] libcc1: fix a memory leak Tom Tromey
2021-04-28 16:07   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 19/21] libcc1: use variadic templates for callbacks Tom Tromey
2021-04-30 15:08   ` Jeff Law
2021-05-04 22:05     ` Tom Tromey
2021-04-28  1:01 ` [PATCH v2 20/21] libcc1: avoid extra string copies Tom Tromey
2021-04-28 16:07   ` Jeff Law
2021-04-28  1:01 ` [PATCH v2 21/21] libcc1: avoid a call to c_str Tom Tromey
2021-04-28 16:08   ` Jeff Law

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=20210428010119.806184-9-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gcc-patches@gcc.gnu.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).