From patchwork Thu Jun 8 09:17:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 70776 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 295AA3856DCA for ; Thu, 8 Jun 2023 09:17:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 295AA3856DCA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686215857; bh=KXpgdf0jTr2kPuCCANwkgNIB3FhakUgb9g5rB6kJyhY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=MxQqTvZ5MBOj2Q97PdmsBFra/PvHy8/3za+1rZ4z5yCZcAWVZHdNbpI/vCPpbqb7b 7Mx/hYoRl1T4I5u0+6o6+KwebWS61ZMHw8myAaSfzCONdDzPfP6ZL0bitTVpScvIIJ f2gj6QBHrOm61HadmthIGh1OmSu7U7qQNNlISFOo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 01DBA385773C for ; Thu, 8 Jun 2023 09:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01DBA385773C Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 0ADFB1FDCC for ; Thu, 8 Jun 2023 09:17:07 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E9FF713480 for ; Thu, 8 Jun 2023 09:17:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id hFLJN5KcgWRyaQAAMHmgww (envelope-from ) for ; Thu, 08 Jun 2023 09:17:06 +0000 To: gdb-patches@sourceware.org Subject: [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Date: Thu, 8 Jun 2023 11:17:13 +0200 Message-Id: <20230608091714.19546-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: Tom de Vries via Gdb-patches From: Tom de Vries Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Add template functions assign_return_if_changed and assign_set_if_changed in gdb/utils.h: ... template void assign_set_if_changed (T &lval, T val, bool &changed) { ... } template bool assign_return_if_changed (T &lval, T val) { ... } ... This allows us to rewrite code like this: ... if (tui_border_attrs != entry->value) { tui_border_attrs = entry->value; need_redraw = true; } ... into this: ... need_redraw |= assign_return_if_changed (tui_border_attrs, entry->value); ... or: ... assign_set_if_changed (tui_border_attrs, entry->value, need_redraw); ... The names are a composition of the functionality. The functions: - assign VAL to LVAL, and either - return true if the assignment changed LVAL, or - set CHANGED to true if the assignment changed LVAL. Tested on x86_64-linux. --- gdb/utils.c | 39 +++++++++++++++++++++++++++++++++++++++ gdb/utils.h | 27 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) base-commit: 12f7174bf069f407d5b6f12e926ceabe45e450e1 diff --git a/gdb/utils.c b/gdb/utils.c index f18228d1086..46bfd9a5bbb 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3631,6 +3631,43 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset, } } +#if GDB_SELF_TEST +static void +test_assign_set_return_if_changed () +{ + bool changed; + int a; + + for (bool initial : { false, true }) + { + changed = initial; + a = 1; + assign_set_if_changed (a, 1, changed); + SELF_CHECK (a == 1); + SELF_CHECK (changed == initial); + } + + for (bool initial : { false, true }) + { + changed = initial; + a = 1; + assign_set_if_changed (a, 2, changed); + SELF_CHECK (a == 2); + SELF_CHECK (changed == true); + } + + a = 1; + changed = assign_return_if_changed (a, 1); + SELF_CHECK (a == 1); + SELF_CHECK (changed == false); + + a = 1; + assign_set_if_changed (a, 2, changed); + SELF_CHECK (a == 2); + SELF_CHECK (changed == true); +} +#endif + void _initialize_utils (); void _initialize_utils () @@ -3695,5 +3732,7 @@ When set, debugging messages will be marked with seconds and microseconds."), selftests::register_test ("strncmp_iw_with_mode", strncmp_iw_with_mode_tests); selftests::register_test ("pager", test_pager); + selftests::register_test ("assign_set_return_if_changed", + test_assign_set_return_if_changed); #endif } diff --git a/gdb/utils.h b/gdb/utils.h index 00b123e6117..d78d54911bf 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -337,4 +337,31 @@ extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset, extern int readline_hidden_cols; +/* Assign VAL to LVAL, and set CHANGED to true if the assignment changed + LVAL. */ + +template +void +assign_set_if_changed (T &lval, T val, bool &changed) +{ + if (lval == val) + return; + + lval = val; + changed = true; +} + +/* Assign VAL to LVAL, and return true if the assignment changed LVAL. */ + +template +bool +assign_return_if_changed (T &lval, T val) +{ + if (lval == val) + return false; + + lval = val; + return true; +} + #endif /* UTILS_H */