unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <aclements@csail.mit.edu>
To: notmuch@notmuchmail.org
Cc: Austin Clements <aclements@csail.mit.edu>
Subject: [PATCH v3.1 3/9] lib: Introduce macros for bit operations
Date: Fri, 24 Oct 2014 08:49:15 -0400	[thread overview]
Message-ID: <1414154955-18114-1-git-send-email-aclements@csail.mit.edu> (raw)
In-Reply-To: <CAB+hUn_d3Wp2bie3E6n1gtb9J_NypAv5rHmr3ObAqWsOX-hzFw@mail.gmail.com>

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..b86897c 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

  reply	other threads:[~2014-10-24 12:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-23 12:30 [PATCH v3 0/9] Add ghost messages and fix thread linking Austin Clements
2014-10-23 12:30 ` [PATCH v3 1/9] lib: Add a ghost messages database feature Austin Clements
2014-10-23 12:30 ` [PATCH v3 2/9] lib: Update database schema doc for ghost messages Austin Clements
2014-10-23 12:30 ` [PATCH v3 3/9] lib: Introduce macros for bit operations Austin Clements
2014-10-24  4:40   ` Jani Nikula
2014-10-24 12:49     ` Austin Clements [this message]
2014-10-23 12:30 ` [PATCH v3 4/9] lib: Internal support for querying and creating ghost messages Austin Clements
2014-10-23 12:30 ` [PATCH v3 5/9] lib: Implement ghost-based thread linking Austin Clements
2014-10-23 12:30 ` [PATCH v3 6/9] lib: Implement upgrade to ghost messages feature Austin Clements
2014-10-23 12:30 ` [PATCH v3 7/9] lib: Enable " Austin Clements
2014-10-23 12:30 ` [PATCH v3 8/9] test: Test upgrade to " Austin Clements
2014-10-23 12:30 ` [PATCH v3 9/9] lib: Remove unnecessary thread linking steps when using ghost messages Austin Clements
2014-10-25 18:02 ` [PATCH v3 0/9] Add ghost messages and fix thread linking David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1414154955-18114-1-git-send-email-aclements@csail.mit.edu \
    --to=aclements@csail.mit.edu \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).