public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Use std::string in event_location
Date: Tue, 18 Jan 2022 17:14:04 +0000 (GMT)	[thread overview]
Message-ID: <20220118171404.71288385802C@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=85e428a69f5b34770ec4f76168d3c8dbb3a454bf

commit 85e428a69f5b34770ec4f76168d3c8dbb3a454bf
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Jan 14 08:53:35 2022 -0700

    Use std::string in event_location
    
    This changes event_location to use std::string, removing some manual
    memory management, and an unnecessary string copy.

Diff:
---
 gdb/location.c | 66 +++++++++++++++++++++++++++-------------------------------
 gdb/location.h |  7 +++----
 2 files changed, 34 insertions(+), 39 deletions(-)

diff --git a/gdb/location.c b/gdb/location.c
index 4e092300cec..f8369bf8318 100644
--- a/gdb/location.c
+++ b/gdb/location.c
@@ -29,7 +29,7 @@
 #include <ctype.h>
 #include <string.h>
 
-static gdb::unique_xmalloc_ptr<char> explicit_location_to_string
+static std::string explicit_location_to_string
      (const struct explicit_location *explicit_loc);
 
 /* The base class for all an event locations used to set a stop event
@@ -37,10 +37,7 @@ static gdb::unique_xmalloc_ptr<char> explicit_location_to_string
 
 struct event_location
 {
-  virtual ~event_location ()
-  {
-    xfree (as_string);
-  }
+  virtual ~event_location () = default;
 
   /* Clone this object.  */
   virtual event_location_up clone () const = 0;
@@ -49,11 +46,13 @@ struct event_location
   virtual bool empty_p () const = 0;
 
   /* Return a string representation of this location.  */
-  const char *to_string ()
+  const char *to_string () const
   {
-    if (as_string == nullptr)
+    if (as_string.empty ())
       as_string = compute_string ();
-    return as_string;
+    if (as_string.empty ())
+      return nullptr;
+    return as_string.c_str ();
   }
 
   DISABLE_COPY_AND_ASSIGN (event_location);
@@ -61,9 +60,9 @@ struct event_location
   /* The type of this breakpoint specification.  */
   enum event_location_type type;
 
-  /* Cached string representation of this location.  This is used, e.g., to
-     save stop event locations to file.  Malloc'd.  */
-  char *as_string = nullptr;
+  /* Cached string representation of this location.  This is used,
+     e.g., to save stop event locations to file.  */
+  mutable std::string as_string;
 
 protected:
 
@@ -74,15 +73,13 @@ protected:
 
   explicit event_location (const event_location *to_clone)
     : type (to_clone->type),
-      as_string (to_clone->as_string == nullptr
-		 ? nullptr
-		 : xstrdup (to_clone->as_string))
+      as_string (to_clone->as_string)
   {
   }
 
   /* Compute the string representation of this object.  This is called
      by to_string when needed.  */
-  virtual char *compute_string () = 0;
+  virtual std::string compute_string () const = 0;
 };
 
 /* A probe.  */
@@ -123,9 +120,9 @@ protected:
   {
   }
 
-  char *compute_string () override
+  std::string compute_string () const override
   {
-    return xstrdup (addr_string);
+    return addr_string;
   }
 };
 
@@ -176,17 +173,17 @@ protected:
       linespec_location.spec_string = xstrdup (linespec_location.spec_string);
   }
 
-  char *compute_string () override
+  std::string compute_string () const override
   {
     if (linespec_location.spec_string != nullptr)
       {
-	struct linespec_location *ls = &linespec_location;
+	const struct linespec_location *ls = &linespec_location;
 	if (ls->match_type == symbol_name_match_type::FULL)
-	  return concat ("-qualified ", ls->spec_string, (char *) nullptr);
+	  return std::string ("-qualified ") + ls->spec_string;
 	else
-	  return xstrdup (ls->spec_string);
+	  return ls->spec_string;
       }
-    return nullptr;
+    return {};
   }
 };
 
@@ -199,7 +196,7 @@ struct event_location_address : public event_location
       address (addr)
   {
     if (addr_string != nullptr)
-      as_string = xstrndup (addr_string, addr_string_len);
+      as_string = std::string (addr_string, addr_string_len);
   }
 
   event_location_up clone () const override
@@ -222,10 +219,10 @@ protected:
   {
   }
 
-  char *compute_string () override
+  std::string compute_string () const override
   {
     const char *addr_string = core_addr_to_string (address);
-    return xstrprintf ("*%s", addr_string).release ();
+    return std::string ("*") + addr_string;
   }
 };
 
@@ -268,9 +265,9 @@ protected:
     copy_loc (&to_clone->explicit_loc);
   }
 
-  char *compute_string () override
+  std::string compute_string () const override
   {
-    return explicit_location_to_string (&explicit_loc).release ();
+    return explicit_location_to_string (&explicit_loc);
   }
 
 private:
@@ -354,7 +351,7 @@ const char *
 get_address_string_location (const struct event_location *location)
 {
   gdb_assert (location->type == ADDRESS_LOCATION);
-  return location->as_string;
+  return location->to_string ();
 }
 
 /* See description in location.h.  */
@@ -406,7 +403,7 @@ get_explicit_location_const (const struct event_location *location)
    AS_LINESPEC is true if this string should be a linespec.
    Otherwise it will be output in explicit form.  */
 
-static gdb::unique_xmalloc_ptr<char>
+static std::string
 explicit_to_string_internal (bool as_linespec,
 			     const struct explicit_location *explicit_loc)
 {
@@ -457,12 +454,12 @@ explicit_to_string_internal (bool as_linespec,
 		  explicit_loc->line_offset.offset);
     }
 
-  return make_unique_xstrdup (buf.c_str ());
+  return std::move (buf.string ());
 }
 
 /* See description in location.h.  */
 
-static gdb::unique_xmalloc_ptr<char>
+static std::string
 explicit_location_to_string (const struct explicit_location *explicit_loc)
 {
   return explicit_to_string_internal (false, explicit_loc);
@@ -470,7 +467,7 @@ explicit_location_to_string (const struct explicit_location *explicit_loc)
 
 /* See description in location.h.  */
 
-gdb::unique_xmalloc_ptr<char>
+std::string
 explicit_location_to_linespec (const struct explicit_location *explicit_loc)
 {
   return explicit_to_string_internal (true, explicit_loc);
@@ -1020,8 +1017,7 @@ event_location_empty_p (const struct event_location *location)
 
 void
 set_event_location_string (struct event_location *location,
-			   gdb::unique_xmalloc_ptr<char> string)
+			   std::string &&string)
 {
-  xfree (location->as_string);
-  location->as_string = string.release ();
+  location->as_string = std::move (string);
 }
diff --git a/gdb/location.h b/gdb/location.h
index b391ce3b1dd..4cabf261383 100644
--- a/gdb/location.h
+++ b/gdb/location.h
@@ -112,7 +112,7 @@ extern enum event_location_type
 /* Return a linespec string representation of the given explicit
    location.  The location must already be canonicalized/valid.  */
 
-extern gdb::unique_xmalloc_ptr<char>
+extern std::string
   explicit_location_to_linespec (const struct explicit_location *explicit_loc);
 
 /* Return a string representation of the LOCATION.
@@ -275,11 +275,10 @@ extern event_location_up
 
 extern int event_location_empty_p (const struct event_location *location);
 
-/* Set the location's string representation.  If STRING is NULL, clear
-   the string representation.  */
+/* Set the location's string representation.  */
 
 extern void
   set_event_location_string (struct event_location *location,
-			     gdb::unique_xmalloc_ptr<char> string);
+			     std::string &&string);
 
 #endif /* LOCATION_H */


                 reply	other threads:[~2022-01-18 17:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220118171404.71288385802C@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@sourceware.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).