From a89ddfc0ac2a93ec77c0239179c0a8c59d5e3abb Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Tue, 7 Oct 2014 17:17:12 +1800 Subject: [PATCH] [PATCH v2 06/12] lib: Introduce macros for bit operations --- 71/dafec945df81b67890f8c0a501c5bbed48ebbd | 132 ++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 71/dafec945df81b67890f8c0a501c5bbed48ebbd diff --git a/71/dafec945df81b67890f8c0a501c5bbed48ebbd b/71/dafec945df81b67890f8c0a501c5bbed48ebbd new file mode 100644 index 000000000..7e59060d2 --- /dev/null +++ b/71/dafec945df81b67890f8c0a501c5bbed48ebbd @@ -0,0 +1,132 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id D5864431FB6 + for ; Mon, 6 Oct 2014 16:17:34 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.3 +X-Spam-Level: +X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id uLINbIqK3ONC for ; + Mon, 6 Oct 2014 16:17:31 -0700 (PDT) +Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu + [18.9.25.12]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id F2914431FBC + for ; Mon, 6 Oct 2014 16:17:26 -0700 (PDT) +X-AuditID: 1209190c-f795e6d000006c66-10-543323062dd6 +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + (using TLS with cipher AES256-SHA (256/256 bits)) + (Client did not present a certificate) + by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP + id 67.75.27750.60323345; Mon, 6 Oct 2014 19:17:26 -0400 (EDT) +Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s96NHPeo032089; + Mon, 6 Oct 2014 19:17:25 -0400 +Received: from drake.dyndns.org ([12.185.136.2]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s96NHMKE014465 + (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); + Mon, 6 Oct 2014 19:17:24 -0400 +Received: from amthrax by drake.dyndns.org with local (Exim 4.84) + (envelope-from ) + id 1XbHWs-0001Jd-09; Mon, 06 Oct 2014 17:17:22 -0600 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v2 06/12] lib: Introduce macros for bit operations +Date: Mon, 6 Oct 2014 17:17:12 -0600 +Message-Id: <1412637438-4821-7-git-send-email-aclements@csail.mit.edu> +X-Mailer: git-send-email 2.1.0 +In-Reply-To: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu> +References: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrLIsWRmVeSWpSXmKPExsUixG6nrsumbBxisH0Vr8WN1m5Gi+s3ZzI7 + MHk8W3WL2WPLoffMAUxRXDYpqTmZZalF+nYJXBkvF71kLdjFV9Gz+wJbA+NB7i5GTg4JAROJ + 3R9OM0LYYhIX7q1nA7GFBGYzSTxpcOpi5AKyNzBKrNixnQXCWcok8WvlDnYIZwmjRHvrEWaQ + FjYBfYkVayexgtgiAtISO+/OBrOZBRwk3k56CrZCWMBJ4t2xGSwgNouAqsTGZX1gcV4BN4kP + LTfYIM6Qk9iw+z9YnFPAXWLNlcmsECe5STz/+ZRlAiP/AkaGVYyyKblVurmJmTnFqcm6xcmJ + eXmpRbqGermZJXqpKaWbGEGhxCnJs4PxzUGlQ4wCHIxKPLwROwxDhFgTy4orcw8xSnIwKYny + zmE3DhHiS8pPqcxILM6ILyrNSS0+xCjBwawkwsv12yhEiDclsbIqtSgfJiXNwaIkzrvpB1+I + kEB6YklqdmpqQWoRTFaGg0NJgveQItBQwaLU9NSKtMycEoQ0EwcnyHAeoOFgNbzFBYm5xZnp + EPlTjIpS4rzBIAkBkERGaR5cLyzWXzGKA70izHsTpIoHmCbgul8BDWYCGmw6Rx9kcEkiQkqq + gXFOgcSOpFcK4XPuZBtX3C73jsjQ6Nfc1KNqMWm9rI2h1JwdgR0+iq5Z0/dO6axwyRCfLZ/t + p93FVMJ7WcVv42XZ4vM8a/+y8km/9vv5LV7X5vKOkpf+E82vv5mXmLDv0gnusw6/hSOy2BRf + bJtQLBC8Qqfqzd4T8x4tE7E+c3+3nOPet/PEdJVYijMSDbWYi4oTAU+HIQfQAgAA +Cc: Austin Clements +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Mon, 06 Oct 2014 23:17:35 -0000 + +These macros help clarify basic bit-twiddling code and are written to +be robust against C undefined behavior of shift operators. +--- + lib/message.cc | 6 +++--- + lib/notmuch-private.h | 11 +++++++++++ + 2 files changed, 14 insertions(+), 3 deletions(-) + +diff --git a/lib/message.cc b/lib/message.cc +index 38bc929..55d2ff6 100644 +--- a/lib/message.cc ++++ b/lib/message.cc +@@ -869,7 +869,7 @@ notmuch_bool_t + notmuch_message_get_flag (notmuch_message_t *message, + notmuch_message_flag_t flag) + { +- return message->flags & (1 << flag); ++ return NOTMUCH_TEST_BIT (message->flags, flag); + } + + void +@@ -877,9 +877,9 @@ notmuch_message_set_flag (notmuch_message_t *message, + notmuch_message_flag_t flag, notmuch_bool_t enable) + { + if (enable) +- message->flags |= (1 << flag); ++ NOTMUCH_SET_BIT (&message->flags, flag); + else +- message->flags &= ~(1 << flag); ++ NOTMUCH_CLEAR_BIT (&message->flags, flag); + } + + time_t +diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h +index 36cc12b..7250291 100644 +--- a/lib/notmuch-private.h ++++ b/lib/notmuch-private.h +@@ -63,6 +63,17 @@ NOTMUCH_BEGIN_DECLS + #define STRNCMP_LITERAL(var, literal) \ + strncmp ((var), (literal), sizeof (literal) - 1) + ++/* Robust bit test/set/reset macros */ ++#define NOTMUCH_TEST_BIT(val, bit) \ ++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? 0 \ ++ : !!((val) & (1ull << bit))) ++#define NOTMUCH_SET_BIT(valp, bit) \ ++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \ ++ : (*(valp) |= (1ull << bit))) ++#define NOTMUCH_CLEAR_BIT(valp, bit) \ ++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \ ++ : (*(valp) &= ~(1ull << bit))) ++ + #define unused(x) x __attribute__ ((unused)) + + #ifdef __cplusplus +-- +2.1.0 + -- 2.26.2