From patchwork Thu Jun 1 09:27:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 70439 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C3BF93856632 for ; Thu, 1 Jun 2023 09:28:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3BF93856632 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685611731; bh=eTFI7WAByLniVt2XIx26IN6AGBqFnpUCCL0yDNsjJKs=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=jI7wxQhq5ZLEG0h6UEBhfj5mnJRBPeyyYdbtXCCH4LrvFxuAqGE2Le2dLTzjq+7/y slkKIR1DDihE1GmIHDZKt+CAraUby2UYU2fF8UlcQZrR6LyuN+x+fRUau8ogpS/sy7 HisnjFGFdObR7lHMz+HccdRRfY5+brN+I2bv1Rb0= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 78E383858291 for ; Thu, 1 Jun 2023 09:28:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 78E383858291 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-490-mMpbd-YIMkacnxpwXSqWIw-1; Thu, 01 Jun 2023 05:28:06 -0400 X-MC-Unique: mMpbd-YIMkacnxpwXSqWIw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-3f604260ef3so11778885e9.3 for ; Thu, 01 Jun 2023 02:28:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685611685; x=1688203685; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eTFI7WAByLniVt2XIx26IN6AGBqFnpUCCL0yDNsjJKs=; b=kJefwMu9JY8IF91uNp510NbxLmLgMYZ3O9goZpjq0LV7b+OaYxZ2lqf6xKHspAn8Cg 80UTo/kJCB8hvjhItBLcOVbrC+7rzbtXjP5yeClIHW4drUukySqxa8AI1TDIAyuvcl76 afK1oR3vtmPyISLI26qWajPZ9p/12F2SFq4ri6kqNDTC7gwK7oL1NQECwWrRVoAMFEV9 HiR+Fz3zG5ErXbRwlxquNjIlzRPk0HW+KprUK4Ob4Iiii6JkcfpC5NNblz/x1+tJBkx4 ZMg9JHawoaxujO73TuvF2n7L47ELswSLQ5n9IvkTh82l3xOsc8Rx0QDQROt1buZnXEhg 6J5g== X-Gm-Message-State: AC+VfDy8u736kUgTE9GfMfDMcMH+GblMluOq2hLR1eqbZKqk5dfNx0t+ pY/2bjkxqJLNR1t1/uJvhsJa5HgozSeaK+JaINJup5GTQgizry3MCQYm9wJK1CB/JXwE+JH76e6 ugghRswc9+qosAMiljTFGreZYs81RJM3qpE1BaPAXcVmbAhI5BJZ4aT4qrtWRzO5iGrqwfayyB2 94JsmL6Q== X-Received: by 2002:a05:600c:a391:b0:3f6:e79:3a8e with SMTP id hn17-20020a05600ca39100b003f60e793a8emr1878709wmb.12.1685611684952; Thu, 01 Jun 2023 02:28:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7nhmiBcpvZLKk8WgF+G48glfVrM7AGZxZdeggomiHXWI2dkT3mYcLS2kB+ZmJRay8BBlgrCw== X-Received: by 2002:a05:600c:a391:b0:3f6:e79:3a8e with SMTP id hn17-20020a05600ca39100b003f60e793a8emr1878695wmb.12.1685611684679; Thu, 01 Jun 2023 02:28:04 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id p17-20020a05600c205100b003f604ca479esm1672312wmg.3.2023.06.01.02.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 02:28:04 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Simon Marchi , Andrew Burgess Subject: [PATCH 3/4] gdb: remove last alloca call from printcmd.c Date: Thu, 1 Jun 2023 10:27:52 +0100 Message-Id: <35bd5c487f6cda38f1668a3ad815b7d382113a6f.1685611213.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This commit removes the last alloca call from printcmd.c. This is similar to the patches I originally posted here: https://inbox.sourceware.org/gdb-patches/cover.1677533215.git.aburgess@redhat.com/ However, this change was not included in that original series. The original series received push back because it was thought that replacing alloca with a C++ container type would introduce unnecessary malloc/free overhead. However, in this case we are building a string, and (at least for GCC), the std::string type has a small string optimisation, where small strings are stored on the stack. And in this case we are building what will usually be a very small string, we're just constructing a printf format specifier for a hex value, so it'll be something like '%#x' -- though it could also have a width in there too -- but still, it should normally fit within GCCs small string buffer. So, in this commit, I propose replacing the use of alloca with a std::string. This shouldn't result (normally) in any additional malloc or free calls, so should be similar in performance to the original approach. There should be no user visible differences after this commit. --- gdb/printcmd.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 6f8a7f1420a..61b009fb7f2 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -2653,62 +2653,58 @@ printf_pointer (struct ui_file *stream, const char *format, modifier for %p is a width; extract that, and then handle %p as glibc would: %#x or a literal "(nil)". */ - const char *p; - char *fmt, *fmt_p; #ifdef PRINTF_HAS_LONG_LONG long long val = value_as_long (value); #else long val = value_as_long (value); #endif - fmt = (char *) alloca (strlen (format) + 5); + /* Build the new output format in FMT. */ + std::string fmt; /* Copy up to the leading %. */ - p = format; - fmt_p = fmt; + const char *p = format; while (*p) { int is_percent = (*p == '%'); - *fmt_p++ = *p++; + fmt.push_back (*p++); if (is_percent) { if (*p == '%') - *fmt_p++ = *p++; + fmt.push_back (*p++); else break; } } if (val != 0) - *fmt_p++ = '#'; + fmt.push_back ('#'); /* Copy any width or flags. Only the "-" flag is valid for pointers -- see the format_pieces constructor. */ while (*p == '-' || (*p >= '0' && *p < '9')) - *fmt_p++ = *p++; + fmt.push_back (*p++); gdb_assert (*p == 'p' && *(p + 1) == '\0'); if (val != 0) { #ifdef PRINTF_HAS_LONG_LONG - *fmt_p++ = 'l'; + fmt.push_back ('l'); #endif - *fmt_p++ = 'l'; - *fmt_p++ = 'x'; - *fmt_p++ = '\0'; + fmt.push_back ('l'); + fmt.push_back ('x'); DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL - gdb_printf (stream, fmt, val); + gdb_printf (stream, fmt.c_str (), val); DIAGNOSTIC_POP } else { - *fmt_p++ = 's'; - *fmt_p++ = '\0'; + fmt.push_back ('s'); DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL - gdb_printf (stream, fmt, "(nil)"); + gdb_printf (stream, fmt.c_str (), "(nil)"); DIAGNOSTIC_POP } }