mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #3178 from kaspar030/bf_get_unset
sys: bitfield: add bf_get_unset
This commit is contained in:
commit
c0f2759e60
1
sys/bitfield/Makefile
Normal file
1
sys/bitfield/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
48
sys/bitfield/bitfield.c
Normal file
48
sys/bitfield/bitfield.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup sys_util
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Bitfield auxillary functions
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "bitfield.h"
|
||||
#include "irq.h"
|
||||
|
||||
int bf_get_unset(uint8_t field[], int size)
|
||||
{
|
||||
int result = -1;
|
||||
int nbytes = (size + 7) / 8;
|
||||
int i = 0;
|
||||
|
||||
unsigned state = disableIRQ();
|
||||
|
||||
/* skip full bytes */
|
||||
for (int j = 0; (j < nbytes) && (field[j] == 255); j++) {
|
||||
i += 8;
|
||||
}
|
||||
|
||||
for (; i < size; i++) {
|
||||
if (!bf_isset(field, i)) {
|
||||
bf_set(field, i);
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
restoreIRQ(state);
|
||||
return(result);
|
||||
}
|
@ -85,6 +85,19 @@ static inline bool bf_isset(uint8_t field[], size_t idx)
|
||||
return (field[idx / 8] & (1u << (idx % 8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Atomically get the number of an unset bit and set it
|
||||
*
|
||||
* This function can be used to record e.g., empty entries in an array.
|
||||
*
|
||||
* @param[in,out] field The bitfield
|
||||
* @param[in] size The size of the bitfield
|
||||
*
|
||||
* @return number of bit that was set
|
||||
* @return -1 if no bit was unset
|
||||
*/
|
||||
int bf_get_unset(uint8_t field[], int size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
1
tests/unittests/tests-bitfield/Makefile
Normal file
1
tests/unittests/tests-bitfield/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
1
tests/unittests/tests-bitfield/Makefile.include
Normal file
1
tests/unittests/tests-bitfield/Makefile.include
Normal file
@ -0,0 +1 @@
|
||||
USEMODULE += bitfield
|
137
tests/unittests/tests-bitfield/tests-bitfield.c
Normal file
137
tests/unittests/tests-bitfield/tests-bitfield.c
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "embUnit.h"
|
||||
|
||||
#include "bitfield.h"
|
||||
|
||||
static void test_bf_get_unset_empty(void)
|
||||
{
|
||||
int res = 0;
|
||||
uint8_t field[5];
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 0);
|
||||
TEST_ASSERT_EQUAL_INT(-1, res);
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 1);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 3);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 8);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 9);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
memset(field, 0, sizeof(field));
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
}
|
||||
|
||||
static void test_bf_get_unset_firstbyte(void)
|
||||
{
|
||||
int res = 0;
|
||||
uint8_t field[5];
|
||||
memset(field, 0xff, sizeof(field));
|
||||
|
||||
field[0] = 0x00;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
field[0] = 0x01;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(1, res);
|
||||
|
||||
field[0] = 0x0f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(4, res);
|
||||
|
||||
field[0] = 0x7f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(7, res);
|
||||
|
||||
field[0] = 0xff;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(-1, res);
|
||||
}
|
||||
|
||||
static void test_bf_get_unset_middle(void)
|
||||
{
|
||||
int res = 0;
|
||||
uint8_t field[5];
|
||||
memset(field, 0xff, sizeof(field));
|
||||
|
||||
field[2] = 0x00;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(16, res);
|
||||
|
||||
field[2] = 0x01;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(17, res);
|
||||
|
||||
field[2] = 0x0f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(20, res);
|
||||
|
||||
field[2] = 0x7f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(23, res);
|
||||
}
|
||||
|
||||
static void test_bf_get_unset_lastbyte(void)
|
||||
{
|
||||
int res = 0;
|
||||
uint8_t field[5];
|
||||
memset(field, 0xff, sizeof(field));
|
||||
|
||||
field[4] = 0x00;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(32, res);
|
||||
|
||||
field[4] = 0x01;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(33, res);
|
||||
|
||||
field[4] = 0x0f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(36, res);
|
||||
|
||||
field[4] = 0x7f;
|
||||
res = bf_get_unset(field, 40);
|
||||
TEST_ASSERT_EQUAL_INT(39, res);
|
||||
}
|
||||
|
||||
Test *tests_bitfield_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_bf_get_unset_empty),
|
||||
new_TestFixture(test_bf_get_unset_firstbyte),
|
||||
new_TestFixture(test_bf_get_unset_middle),
|
||||
new_TestFixture(test_bf_get_unset_lastbyte),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(bitfield_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&bitfield_tests;
|
||||
}
|
||||
|
||||
|
||||
void tests_bitfield(void)
|
||||
{
|
||||
TESTS_RUN(tests_bitfield_tests());
|
||||
}
|
Loading…
Reference in New Issue
Block a user