aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Brobecker <brobecker@adacore.com>2020-11-23 21:45:35 -0500
committerJoel Brobecker <brobecker@adacore.com>2020-11-23 21:45:35 -0500
commit987b670356322ba4d493f441855bf5dc8d946e9f (patch)
tree50b180ece0ac5d200979b6edefa31fff29cb8adb
parent4fbb7ccebe1fdcbae762e8fed6af7a810c81f85c (diff)
downloadgdb-987b670356322ba4d493f441855bf5dc8d946e9f.zip
gdb-987b670356322ba4d493f441855bf5dc8d946e9f.tar.gz
gdb-987b670356322ba4d493f441855bf5dc8d946e9f.tar.bz2
change and rename gmp_string_asprintf to return an std::string
This was suggested by Simon during a code review of this package upstream. The upside is that this makes the function's API more natural and C++. The downside is an extra malloc, which might be the reason why we went for using a unique_xmalloc_ptr in the first place. Since this function is not expected to be called frequently, the API improvement might be worth the performance impact. gdb/ChangeLog: * gmp-utils.h (gmp_string_printf): Rename from gmp_string_asprintf. Change return type to std::string. Update all callers. * gmp-utils.c (gmp_string_printf): Likewise.
-rw-r--r--gdb/ChangeLog6
-rw-r--r--gdb/gdbtypes.c2
-rw-r--r--gdb/gmp-utils.c17
-rw-r--r--gdb/gmp-utils.h10
-rw-r--r--gdb/typeprint.c5
-rw-r--r--gdb/valprint.c4
6 files changed, 27 insertions, 17 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index bd74aa5..2cb64da 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
2020-11-24 Joel Brobecker <brobecker@adacore.com>
+ * gmp-utils.h (gmp_string_printf): Rename from gmp_string_asprintf.
+ Change return type to std::string. Update all callers.
+ * gmp-utils.c (gmp_string_printf): Likewise.
+
+2020-11-24 Joel Brobecker <brobecker@adacore.com>
+
* unittests/gmp-utils-selftests.c (write_fp_test): Use mpq_set_si
instead of mpq_set_ui to initialize our GMP rational.
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index e6f70bb..3879eeb 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -4927,7 +4927,7 @@ static void
print_fixed_point_type_info (struct type *type, int spaces)
{
printfi_filtered (spaces + 2, "scaling factor: %s\n",
- fixed_point_scaling_factor (type).str ().get ());
+ fixed_point_scaling_factor (type).str ().c_str ());
}
static struct obstack dont_print_type_obstack;
diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index db92e57..44fe156 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -19,17 +19,24 @@
/* See gmp-utils.h. */
-gdb::unique_xmalloc_ptr<char>
-gmp_string_asprintf (const char *fmt, ...)
+std::string
+gmp_string_printf (const char *fmt, ...)
{
va_list vp;
- char *buf;
va_start (vp, fmt);
- gmp_vasprintf (&buf, fmt, vp);
+ int size = gmp_vsnprintf (NULL, 0, fmt, vp);
va_end (vp);
- return gdb::unique_xmalloc_ptr<char> (buf);
+ std::string str (size, '\0');
+
+ /* C++11 and later guarantee std::string uses contiguous memory and
+ always includes the terminating '\0'. */
+ va_start (vp, fmt);
+ gmp_vsprintf (&str[0], fmt, vp);
+ va_end (vp);
+
+ return str;
}
/* See gmp-utils.h. */
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index 1214b64..59965e5 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -29,9 +29,9 @@
#include <gmp.h>
#include "gdbsupport/traits.h"
-/* Same as gmp_asprintf, but returning a convenient wrapper type. */
+/* Same as gmp_asprintf, but returning an std::string. */
-gdb::unique_xmalloc_ptr<char> gmp_string_asprintf (const char *fmt, ...);
+std::string gmp_string_printf (const char *fmt, ...);
/* A class to make it easier to use GMP's mpz_t values within GDB. */
@@ -110,8 +110,7 @@ struct gdb_mpz
bool unsigned_p) const;
/* Return a string containing VAL. */
- gdb::unique_xmalloc_ptr<char> str () const
- { return gmp_string_asprintf ("%Zd", val); }
+ std::string str () const { return gmp_string_printf ("%Zd", val); }
/* The destructor. */
~gdb_mpz () { mpz_clear (val); }
@@ -163,8 +162,7 @@ struct gdb_mpq
}
/* Return a string representing VAL as "<numerator> / <denominator>". */
- gdb::unique_xmalloc_ptr<char> str () const
- { return gmp_string_asprintf ("%Qd", val); }
+ std::string str () const { return gmp_string_printf ("%Qd", val); }
/* Return VAL rounded to the nearest integer. */
gdb_mpz get_rounded () const;
diff --git a/gdb/typeprint.c b/gdb/typeprint.c
index f947faf..0dd3b1c 100644
--- a/gdb/typeprint.c
+++ b/gdb/typeprint.c
@@ -667,11 +667,10 @@ print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
void
print_type_fixed_point (struct type *type, struct ui_file *stream)
{
- gdb::unique_xmalloc_ptr<char> small_img
- = fixed_point_scaling_factor (type).str ();
+ std::string small_img = fixed_point_scaling_factor (type).str ();
fprintf_filtered (stream, "%s-byte fixed point (small = %s)",
- pulongest (TYPE_LENGTH (type)), small_img.get ());
+ pulongest (TYPE_LENGTH (type)), small_img.c_str ());
}
/* Dump details of a type specified either directly or indirectly.
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 38ae0bd..b102ff4 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -814,8 +814,8 @@ generic_val_print_fixed_point (struct value *val, struct ui_file *stream,
fixed_point_scaling_factor (type));
const char *fmt = TYPE_LENGTH (type) < 4 ? "%.11Fg" : "%.17Fg";
- gdb::unique_xmalloc_ptr<char> str = gmp_string_asprintf (fmt, f.val);
- fprintf_filtered (stream, "%s", str.get ());
+ std::string str = gmp_string_printf (fmt, f.val);
+ fprintf_filtered (stream, "%s", str.c_str ());
}
}