shared: add weston_assert_legal_bits()

Helper to assert that a value does not have any bit set outside of the
mask. To be used with "all bits mask" of enum types that enumerate bits.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
This commit is contained in:
Pekka Paalanen 2024-05-02 14:40:35 +03:00 committed by Pekka Paalanen
parent 74d1700f0b
commit c310516d1d
3 changed files with 25 additions and 1 deletions

View file

@ -6805,7 +6805,8 @@ WL_EXPORT void
weston_head_set_supported_eotf_mask(struct weston_head *head,
uint32_t eotf_mask)
{
assert((eotf_mask & ~WESTON_EOTF_MODE_ALL_MASK) == 0);
weston_assert_legal_bits(head->compositor,
eotf_mask, WESTON_EOTF_MODE_ALL_MASK);
if (head->supported_eotf_mask == eotf_mask)
return;

View file

@ -129,3 +129,18 @@ do { \
__FILE__, __LINE__, #bit, b, #value, v); \
cond; \
})
#define weston_assert_legal_bits(compositor, value, mask) \
({ \
struct weston_compositor *ec = compositor; \
uint64_t v_ = (value); \
uint64_t m_ = (mask); \
uint64_t ill = v_ & ~m_; \
bool cond = ill == 0; \
if (!cond) \
custom_assert_fail_(ec, "%s:%u: Assertion failed! " \
"Value %s (0x%" PRIx64 ") contains illegal bits 0x%" PRIx64 ". " \
"Legal mask is %s (0x%" PRIx64 ").\n", \
__FILE__, __LINE__, #value, v_, ill, #mask, m_); \
cond; \
})

View file

@ -136,4 +136,12 @@ TEST(asserts)
uint64_t max_uint64 = UINT64_MAX;
ret = weston_assert_uint64_eq(compositor, max_uint64, 0);
abort_if_not(ret == false);
uint64_t val = 0x200010001000ffff;
uint64_t msk = 0x00000000fffffff3;
ret = weston_assert_legal_bits(compositor, val, msk);
abort_if_not(ret == false);
ret = weston_assert_legal_bits(compositor, val, UINT64_MAX);
abort_if_not(ret);
}