Skip to contentSkip to conversation

util: Remove confusing static keyword use

PATCH Superseded Denis Kenzior 1 participant 4 messages Apr 15, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 util: Remove confusing static keyword use

'__n' is declared as static const which is confusing since it would imply that __n is only initialized once. This works out in the end since the macro used in a separate anonymous block, but some compilers (i.e. clang) will generate a symbol table entry for '__n'. This is not needed since the expression is always constant.

Declare '__n' as a const size_t instead since that is the type produced by the sizeof() operation used by L_ARRAY_SIZE.

While here, also declare '__i' as size_t to match '__n'.

Fixes 4a9f6388dd7f ("util: Add L_IN_SET macros")
---
 ell/util.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ell/util.h +2 -2
index 80b29b7bb6e3..b5d96754e8ae 100644
@@ -349,8 +349,8 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p)
 #define _L_IN_SET_CMP(val, type, cmp, ...) __extension__ ({		\
 		const type __v = (val);					\
 		const typeof(__v) __elems[] = {__VA_ARGS__};		\
-		unsigned int __i;					\
-		static const unsigned int __n = L_ARRAY_SIZE(__elems);	\
+		size_t __i;						\
+		size_t __n = L_ARRAY_SIZE(__elems);			\
 		bool __r = false;					\
 		for (__i = 0; __i < __n && !__r; __i++)			\
 			__r = (cmp);					\
Signature · 1 line
2.44.0
#1 lore raw

PATCH 2/4 util: Add L_BIT_{SET|CLEAR|TEST} macros

These macros support arbitrarily sized bitmaps using any integer data type. It is thus possible to write code like this:

	uint64_t bitmap = 0;
	uint8_t array[4] = {};
	bool r;

	L_BIT_SET(&bitmap, 63);
	r = L_BIT_TEST(&bitmap, 0);

	L_BIT_SET(array, 25);
	r = L_BIT_TEST(array, 31);
---
 ell/util.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
ell/util.h +18 -0
index b5d96754e8ae..53a932c771e6 100644
@@ -366,6 +366,24 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p)
 				(__v && __elems[__i] &&			\
 				 !strcmp(__v, __elems[__i])), ##__VA_ARGS__)
 
+#define _L_BIT_TO_MASK(bits, nr) __extension__ ({			\
+	const unsigned int shift = (nr) % (sizeof(*(bits)) * 8);	\
+	const typeof(*(bits)) mask = (typeof(*(bits)))1 << shift;	\
+	mask;								\
+})
+
+#define _L_BIT_TO_OFFSET(bits, nr)					\
+	(*((bits) + ((nr) / (sizeof(*(bits)) * 8))))			\
+
+#define L_BIT_SET(bits, nr)						\
+	_L_BIT_TO_OFFSET(bits, nr) |= _L_BIT_TO_MASK(bits, nr)
+
+#define L_BIT_CLEAR(bits, nr)						\
+	_L_BIT_TO_OFFSET(bits, nr) &= ~_L_BIT_TO_MASK(bits, nr)
+
+#define L_BIT_TEST(bits, nr)						\
+	((~_L_BIT_TO_OFFSET(bits, nr) & _L_BIT_TO_MASK(bits, nr)) == 0)
+
 /*
  * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to
  * ensure the compiler does not try to optimize the constant time behavior.
Signature · 1 line
2.44.0
#2 lore raw

PATCH 3/4 util: Add L_BITS_{SET|CLEAR}

In reply to Denis Kenzior

These macros can set or clear an arbitrary number of bits in a single call, as follows:

	uint64_t bitmap = 0;

	L_BITS_CLEAR(&bitmap, 0, 1, 5, 15, 32, 63);
---
 ell/util.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
ell/util.h +14 -0
index 53a932c771e6..1d14ffb1570d 100644
@@ -384,6 +384,20 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p)
 #define L_BIT_TEST(bits, nr)						\
 	((~_L_BIT_TO_OFFSET(bits, nr) & _L_BIT_TO_MASK(bits, nr)) == 0)
 
+#define L_BITS_SET(bits, ...) __extension__ ({				\
+	const unsigned int __elems[] = {__VA_ARGS__};			\
+	size_t __i;							\
+	for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++)		\
+		L_BIT_SET(bits, __elems[__i]);				\
+})
+
+#define L_BITS_CLEAR(bits, ...) __extension__ ({			\
+	const unsigned int __elems[] = {__VA_ARGS__};			\
+	size_t __i;							\
+	for (__i = 0; __i < L_ARRAY_SIZE(__elems); __i++)		\
+		L_BIT_CLEAR(bits, __elems[__i]);			\
+})
+
 /*
  * Taken from https://github.com/chmike/cst_time_memcmp, adding a volatile to
  * ensure the compiler does not try to optimize the constant time behavior.
Signature · 1 line
2.44.0
#3 lore raw

PATCH 4/4 unit: Add tests for the new bitmap utilities

In reply to Denis Kenzior
---
 unit/test-util.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
unit/test-util.c +81 -0
index f7a895f911d5..14724f2a608b 100644
@@ -195,6 +195,81 @@ static void test_safe_atoux(const void *test_data)
 	assert(l_safe_atox8("0xffff", &c) == -ERANGE);
 }
 
+static void test_set_bit(const void *test_data)
+{
+	unsigned int bitmap[2] = { };
+	int one = 0;
+
+	L_BIT_SET(&bitmap[0], 0);
+	L_BIT_SET(bitmap, 1);
+	L_BIT_SET(bitmap, 2);
+	L_BIT_SET(bitmap, 3);
+
+	assert(bitmap[0] == 0x0f);
+	assert(bitmap[1] == 0);
+
+	L_BIT_SET(bitmap, 63);
+	L_BIT_SET(bitmap, 62);
+	L_BIT_SET(bitmap, 61);
+	L_BIT_SET(bitmap, 60);
+
+	assert(bitmap[0] == 0x0fU);
+	assert(bitmap[1] == 0xf0000000U);
+
+	L_BIT_SET(&one, 0);
+	assert(one == 1);
+}
+
+static void test_clear_bit(const void *test_data)
+{
+	unsigned int bitmap[2] = { 0xfU, 0xf0000000U };
+
+	L_BIT_CLEAR(&bitmap[0], 3);
+	L_BIT_CLEAR(bitmap, 63);
+
+	assert(bitmap[0] == 0x07U);
+	assert(bitmap[1] == 0x70000000U);
+}
+
+static void test_is_bit_set(const void *test_data)
+{
+	unsigned int bitmap[2] = { 0xfU, 0xf0000000U };
+	uint8_t one = 1;
+
+	assert(L_BIT_TEST(&bitmap[0], 0) == true);
+	assert(L_BIT_TEST(bitmap, 1) == true);
+	assert(L_BIT_TEST(bitmap, 2) == true);
+	assert(L_BIT_TEST(bitmap, 3) == true);
+	assert(L_BIT_TEST(bitmap, 4) == false);
+
+	assert(L_BIT_TEST(bitmap, 63) == true);
+	assert(L_BIT_TEST(bitmap, 55) == false);
+
+	assert(L_BIT_TEST(&one, 0) == true);
+	assert(L_BIT_TEST(&one, 1) == false);
+}
+
+static void test_set_bits(const void *test_data)
+{
+	uint16_t bitmap[4] = {};
+
+	L_BITS_SET(bitmap, 0, 1, 16, 32, 48);
+
+	assert(bitmap[0] == 0x3);
+	assert(bitmap[1] == 0x1);
+	assert(bitmap[2] == 0x1);
+	assert(bitmap[3] == 0x1);
+}
+
+static void test_clear_bits(const void *test_data)
+{
+	uint16_t bitmap[4] = { 0x3, 0x1, 0x1, 0x1 };
+
+	L_BITS_CLEAR(bitmap, 0, 1, 16, 32, 48);
+
+	assert(l_memeqzero(bitmap, sizeof(bitmap)));
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -212,5 +287,11 @@ int main(int argc, char *argv[])
 
 	l_test_add("l_safe_atoux", test_safe_atoux, NULL);
 
+	l_test_add("L_BIT_SET", test_set_bit, NULL);
+	l_test_add("L_BIT_CLEAR", test_clear_bit, NULL);
+	l_test_add("L_BIT_TEST", test_is_bit_set, NULL);
+	l_test_add("L_BITS_SET", test_set_bits, NULL);
+	l_test_add("L_BITS_CLEAR", test_clear_bits, NULL);
+
 	return l_test_run();
 }
Signature · 1 line
2.44.0