From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id iPT0GrbrsF8GPAAAWB0awg (envelope-from ) for ; Sun, 15 Nov 2020 03:49:58 -0500 Received: by simark.ca (Postfix, from userid 112) id 67A751F08B; Sun, 15 Nov 2020 03:49:58 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 7C0141E776 for ; Sun, 15 Nov 2020 03:49:53 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DF45B3850430; Sun, 15 Nov 2020 08:49:52 +0000 (GMT) Received: from rock.gnat.com (rock.gnat.com [IPv6:2620:20:4000:0:a9e:1ff:fe9b:1d1]) by sourceware.org (Postfix) with ESMTP id A19713857802 for ; Sun, 15 Nov 2020 08:49:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A19713857802 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=brobecke@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 819EF5604E; Sun, 15 Nov 2020 03:49:49 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id R3LpH07mAKgO; Sun, 15 Nov 2020 03:49:49 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 715645604D; Sun, 15 Nov 2020 03:49:49 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4233) id 6FF9B111; Sun, 15 Nov 2020 03:49:49 -0500 (EST) From: Joel Brobecker To: gdb-patches@sourceware.org Subject: [RFA 1/6] change gmp_string_asprintf to return an std::string Date: Sun, 15 Nov 2020 03:49:39 -0500 Message-Id: <1605430184-81335-2-git-send-email-brobecker@adacore.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1605430184-81335-1-git-send-email-brobecker@adacore.com> References: <1604817017-25807-1-git-send-email-brobecker@adacore.com> <1605430184-81335-1-git-send-email-brobecker@adacore.com> X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Simon Marchi , Joel Brobecker Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" 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_asprintf): Change return type to std::string. Update all callers. * gmp-utils.c (gmp_string_asprintf): Likewise. --- gdb/gdbtypes.c | 2 +- gdb/gmp-utils.c | 6 ++++-- gdb/gmp-utils.h | 10 ++++------ gdb/typeprint.c | 5 ++--- gdb/valprint.c | 4 ++-- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 2f92887..1c1b0ff 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -4922,7 +4922,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..b70aaa3 100644 --- a/gdb/gmp-utils.c +++ b/gdb/gmp-utils.c @@ -19,7 +19,7 @@ /* See gmp-utils.h. */ -gdb::unique_xmalloc_ptr +std::string gmp_string_asprintf (const char *fmt, ...) { va_list vp; @@ -29,7 +29,9 @@ gmp_string_asprintf (const char *fmt, ...) gmp_vasprintf (&buf, fmt, vp); va_end (vp); - return gdb::unique_xmalloc_ptr (buf); + std::string result(buf); + xfree (buf); + return result; } /* See gmp-utils.h. */ diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index 1214b64..7eed29a 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -29,9 +29,9 @@ #include #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 gmp_string_asprintf (const char *fmt, ...); +std::string gmp_string_asprintf (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 str () const - { return gmp_string_asprintf ("%Zd", val); } + std::string str () const { return gmp_string_asprintf ("%Zd", val); } /* The destructor. */ ~gdb_mpz () { mpz_clear (val); } @@ -163,8 +162,7 @@ struct gdb_mpq } /* Return a string representing VAL as " / ". */ - gdb::unique_xmalloc_ptr str () const - { return gmp_string_asprintf ("%Qd", val); } + std::string str () const { return gmp_string_asprintf ("%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 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..abef002 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 str = gmp_string_asprintf (fmt, f.val); - fprintf_filtered (stream, "%s", str.get ()); + std::string str = gmp_string_asprintf (fmt, f.val); + fprintf_filtered (stream, "%s", str.c_str ()); } } -- 2.1.4