diff options
| author | Austin Clements <aclements@csail.mit.edu> | 2014-10-24 08:49:15 -0400 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2014-10-25 19:26:43 +0200 |
| commit | d99491f27440d83f937131a861ca547bffb8bdf1 (patch) | |
| tree | cfe2a017a4c2a6d6668413eabea1a8aadbae6f9d /lib | |
| parent | d9f5da00bb7707f3244654b4e44df2284e3634be (diff) | |
lib: Introduce macros for bit operations
These macros help clarify basic bit-twiddling code and are written to
be robust against C undefined behavior of shift operators.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/message.cc | 6 | ||||
| -rw-r--r-- | lib/notmuch-private.h | 11 |
2 files changed, 14 insertions, 3 deletions
diff --git a/lib/message.cc b/lib/message.cc index 38bc9291..55d2ff69 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 36cc12b0..b86897c2 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 |
