diff options
author | Joel Brobecker <brobecker@adacore.com> | 2020-11-23 21:45:35 -0500 |
---|---|---|
committer | Joel Brobecker <brobecker@adacore.com> | 2020-11-23 21:45:35 -0500 |
commit | 987b670356322ba4d493f441855bf5dc8d946e9f (patch) | |
tree | 50b180ece0ac5d200979b6edefa31fff29cb8adb /gdb/gmp-utils.c | |
parent | 4fbb7ccebe1fdcbae762e8fed6af7a810c81f85c (diff) | |
download | binutils-987b670356322ba4d493f441855bf5dc8d946e9f.zip binutils-987b670356322ba4d493f441855bf5dc8d946e9f.tar.gz binutils-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.
Diffstat (limited to 'gdb/gmp-utils.c')
-rw-r--r-- | gdb/gmp-utils.c | 17 |
1 files changed, 12 insertions, 5 deletions
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. */ |