mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-12-22 17:34:54 +01:00
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.
This commit is contained in:
parent
d9f5da00bb
commit
d99491f274
2 changed files with 14 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue