From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id eFn1DtnKsV+YUAAAWB0awg (envelope-from ) for ; Sun, 15 Nov 2020 19:42:01 -0500 Received: by simark.ca (Postfix, from userid 112) id 30B511F08B; Sun, 15 Nov 2020 19:42:01 -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=ham 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 94ACE1E58F for ; Sun, 15 Nov 2020 19:42:00 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03DC33857C46; Mon, 16 Nov 2020 00:42:00 +0000 (GMT) Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id A10F53857C46 for ; Mon, 16 Nov 2020 00:41:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A10F53857C46 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark@simark.ca Received: from [10.0.0.11] (173-246-6-90.qc.cable.ebox.net [173.246.6.90]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 39B371E58F; Sun, 15 Nov 2020 19:41:57 -0500 (EST) Subject: Re: [RFA 1/6] change gmp_string_asprintf to return an std::string To: Joel Brobecker , gdb-patches@sourceware.org References: <1604817017-25807-1-git-send-email-brobecker@adacore.com> <1605430184-81335-1-git-send-email-brobecker@adacore.com> <1605430184-81335-2-git-send-email-brobecker@adacore.com> From: Simon Marchi Message-ID: <3e16e18e-e5bc-7d6a-1404-19c7d22cda6e@simark.ca> Date: Sun, 15 Nov 2020 19:41:56 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <1605430184-81335-2-git-send-email-brobecker@adacore.com> Content-Type: text/plain; charset=utf-8 Content-Language: fr Content-Transfer-Encoding: 7bit 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: , Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" On 2020-11-15 3:49 a.m., Joel Brobecker wrote: > 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. In fact, I was thinking of doing the exact same thing as the string_print function. By calling vsnprintf with a NULL buffer, you can get how many bytes would be required to hold the result. You can then allocate the required buffer and call vsprintf with it. So, there's no extra allocation, although it requires formatting twice. But if it's good enough for string_printf (which is heavily used), it's good enough for gmp_string_printf. I was just wondering whether gmp_vsnprintf supported passing a NULL buffer like vsnprintf does, and it seems like yes. I tried the implementation shown below and it works (with gmp 6.2.0 at least). I would suggest renaming the function to gmp_string_printf for symmetry with plain string_printf. std::string gmp_string_asprintf (const char *fmt, ...) { va_list vp; va_start (vp, fmt); int size = gmp_vsnprintf (NULL, 0, fmt, vp); va_end (vp); 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; } Simon