1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19400: sys/bitfield: don't touch unrelated bits in bf_{set, clear}_all() r=kaspar030 a=benpicco



Co-authored-by: Benjamin Valentin <benjamin.valentin@ml-pa.com>
This commit is contained in:
bors[bot] 2023-03-20 11:30:53 +00:00 committed by GitHub
commit 189d1c598c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -90,7 +90,18 @@ void bf_set_all(uint8_t field[], size_t size)
memset(field, 0xff, bytes);
if (bits) {
field[bytes] = ~((1U << (8 - bits)) - 1);
field[bytes] |= ~((1U << (8 - bits)) - 1);
}
}
void bf_clear_all(uint8_t field[], size_t size)
{
unsigned bytes = size >> 3;
unsigned bits = size & 0x7;
memset(field, 0, bytes);
if (bits) {
field[bytes] &= ((1U << (8 - bits)) - 1);
}
}

View File

@ -365,10 +365,7 @@ static inline void bf_set_all_atomic(uint8_t field[], size_t size)
* @param[in] field The bitfield
* @param[in] size The number of bits in the bitfield
*/
static inline void bf_clear_all(uint8_t field[], size_t size)
{
memset(field, 0, (size + 7) / 8);
}
void bf_clear_all(uint8_t field[], size_t size);
/**
* @brief Atomically clear all bits in the bitfield to 0

View File

@ -299,6 +299,16 @@ static void test_bf_set_all(void)
TEST_ASSERT_EQUAL_INT(0, field[4]);
}
static void test_bf_clear_all(void)
{
uint8_t field[5];
memset(field, 0xFF, sizeof(field));
bf_clear_all(field, 5);
TEST_ASSERT_EQUAL_INT(0x7, field[0]);
TEST_ASSERT_EQUAL_INT(0xFF, field[1]);
}
static void test_bf_popcnt(void)
{
uint8_t field[5];
@ -334,6 +344,7 @@ Test *tests_bitfield_tests(void) {
new_TestFixture(test_bf_find_first_set),
new_TestFixture(test_bf_find_first_unset),
new_TestFixture(test_bf_set_all),
new_TestFixture(test_bf_clear_all),
new_TestFixture(test_bf_popcnt),
};