Remove a cleanup from target-descriptions.c

Message ID 20190102155243.17250-1-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Jan. 2, 2019, 3:52 p.m. UTC
  This removes a cleanup from target-descriptions.c, by changing it to
use a unique_ptr instead.  Note that a deletion adapter is used, even
though target_desc is allocated with new, to avoid moving target_desc
to target-descriptions.h.

gdb/ChangeLog
2019-01-02  Tom Tromey  <tom@tromey.com>

	* xml-tdesc.c (xml_cache): Hold a target_desc_up.
	(tdesc_parse_xml): Remove cleanups.
	* target-descriptions.h (make_cleanup_free_target_description):
	Don't declare.
	(target_desc_deleter): New struct.
	(target_desc_up): New typedef.
	* target-descriptions.c (target_desc_deleter::operator()): Rename
	from free_target_description.
	(make_cleanup_free_target_description): Remove.
---
 gdb/ChangeLog             | 12 ++++++++++++
 gdb/target-descriptions.c | 12 ++----------
 gdb/target-descriptions.h | 13 ++++++++++++-
 gdb/xml-tdesc.c           | 13 +++++--------
 4 files changed, 31 insertions(+), 19 deletions(-)
  

Comments

Simon Marchi Jan. 2, 2019, 8:40 p.m. UTC | #1
On 2019-01-02 10:52, Tom Tromey wrote:
> This removes a cleanup from target-descriptions.c, by changing it to
> use a unique_ptr instead.  Note that a deletion adapter is used, even
> though target_desc is allocated with new, to avoid moving target_desc
> to target-descriptions.h.

Just wondering, is there something preventing the target_desc structure 
from going to target-descriptions.h?

The patch LGTM in any case.

Simon
  
Tom Tromey Jan. 2, 2019, 11:47 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> Just wondering, is there something preventing the target_desc
Simon> structure from going to target-descriptions.h?

Nothing deep -- it's enough to just move struct property and struct
target_desc to the header.

I tried it just now.  I guess it looks a little uglier, especially since
target_desc is only manipulated via accessors, thanks to common/tdesc.h.

I suppose this could all be merged more and C++-ified a bit more.

Tom
  

Patch

diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index efea97ed40..f04b8fc316 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -1138,20 +1138,12 @@  allocate_target_description (void)
   return new target_desc ();
 }
 
-static void
-free_target_description (void *arg)
+void
+target_desc_deleter::operator() (struct target_desc *target_desc) const
 {
-  struct target_desc *target_desc = (struct target_desc *) arg;
-
   delete target_desc;
 }
 
-struct cleanup *
-make_cleanup_free_target_description (struct target_desc *target_desc)
-{
-  return make_cleanup (free_target_description, target_desc);
-}
-
 void
 tdesc_add_compatible (struct target_desc *target_desc,
 		      const struct bfd_arch_info *compatible)
diff --git a/gdb/target-descriptions.h b/gdb/target-descriptions.h
index 402bef56e0..fe07d425a5 100644
--- a/gdb/target-descriptions.h
+++ b/gdb/target-descriptions.h
@@ -199,9 +199,20 @@  struct type *tdesc_find_type (struct gdbarch *gdbarch, const char *id);
 int tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
 				  struct reggroup *reggroup);
 
+
+/* A deleter adapter for a target desc.  */
+
+struct target_desc_deleter
+{
+  void operator() (struct target_desc *desc) const;
+};
+
+/* A unique pointer specialization that holds a target_desc.  */
+
+typedef std::unique_ptr<target_desc, target_desc_deleter> target_desc_up;
+
 /* Methods for constructing a target description.  */
 
-struct cleanup *make_cleanup_free_target_description (struct target_desc *);
 void set_tdesc_architecture (struct target_desc *,
 			     const struct bfd_arch_info *);
 void set_tdesc_osabi (struct target_desc *, enum gdb_osabi osabi);
diff --git a/gdb/xml-tdesc.c b/gdb/xml-tdesc.c
index 601ffe6086..7588cb0f99 100644
--- a/gdb/xml-tdesc.c
+++ b/gdb/xml-tdesc.c
@@ -66,7 +66,7 @@  tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
    then we will create unnecessary duplicate gdbarches.  See
    gdbarch_list_lookup_by_info.  */
 
-static std::unordered_map<std::string, target_desc *> xml_cache;
+static std::unordered_map<std::string, target_desc_up> xml_cache;
 
 /* Callback data for target description parsing.  */
 
@@ -637,25 +637,22 @@  tdesc_parse_xml (const char *document, xml_fetch_another fetcher,
      previously parsed.  */
   const auto it = xml_cache.find (expanded_text);
   if (it != xml_cache.end ())
-    return it->second;
+    return it->second.get ();
 
   memset (&data, 0, sizeof (struct tdesc_parsing_data));
-  data.tdesc = allocate_target_description ();
-  struct cleanup *result_cleanup
-    = make_cleanup_free_target_description (data.tdesc);
+  target_desc_up description (allocate_target_description ());
+  data.tdesc = description.get ();
 
   if (gdb_xml_parse_quick (_("target description"), "gdb-target.dtd",
 			   tdesc_elements, expanded_text.c_str (), &data) == 0)
     {
       /* Parsed successfully.  */
-      xml_cache.emplace (std::move (expanded_text), data.tdesc);
-      discard_cleanups (result_cleanup);
+      xml_cache.emplace (std::move (expanded_text), std::move (description));
       return data.tdesc;
     }
   else
     {
       warning (_("Could not load XML target description; ignoring"));
-      do_cleanups (result_cleanup);
       return NULL;
     }
 }