Thread (39 messages) flat view 39 messages, 2 authors, 2021-04-07
STALE1973d

Revision v3 of 3 in this series.

Revisions (3)
  1. v2 [diff vs current]
  2. v3 current
  3. v4 [diff vs current]

[PATCH net-next v3 09/18] include: bitmap: add macro for bitmap initialization

From: Marek Behún <kabel@kernel.org>
Date: 2021-04-06 22:12:07
Subsystem: bitmap api, the rest · Maintainers: Yury Norov, Linus Torvalds

Use the new variadic-macro.h library to implement macro
INITIALIZE_BITMAP(nbits, ...), which can be used for compile time bitmap
initialization in the form
  static DECLARE_BITMAP(bm, 100) = INITIALIZE_BITMAP(100, 7, 9, 66, 98);

The macro uses the BUILD_BUG_ON_ZERO mechanism to ensure a compile-time
error if an argument is out of range.

Signed-off-by: Marek Behún <kabel@kernel.org>
---
 include/linux/bitmap.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 70a932470b2d..a9e74d3420bf 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -8,6 +8,7 @@
 #include <linux/bitops.h>
 #include <linux/string.h>
 #include <linux/kernel.h>
+#include <linux/variadic-macro.h>
 
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
@@ -114,6 +115,29 @@
  * contain all bit positions from 0 to 'bits' - 1.
  */
 
+/**
+ * DOC: initialize bitmap
+ * The INITIALIZE_BITMAP(bits, args...) macro expands to a designated
+ * initializer for bitmap of length 'bits', setting each bit specified
+ * in 'args...'.
+ */
+#define _BIT_MEMBER(bit) ((bit) / BITS_PER_LONG)
+#define _VERIFY_BIT(bit, nbits)						\
+	BUILD_BUG_ON_ZERO((bit) < 0 || (bit) >= (nbits))
+#define _INIT_BITMAP_MEMBER_VALUE(bit, member_bit)			\
+	| (_BIT_MEMBER(bit) == _BIT_MEMBER(member_bit)			\
+		? BIT((bit) % BITS_PER_LONG)				\
+		: 0)
+#define _INIT_BITMAP_MEMBER(bit, nbits, ...)				\
+	[_VERIFY_BIT((bit), (nbits)) + _BIT_MEMBER(bit)] =		\
+		(0 EXPAND_FOR_EACH(_INIT_BITMAP_MEMBER_VALUE,		\
+				   (bit), ##__VA_ARGS__)),
+#define INITIALIZE_BITMAP(nbits, ...)					\
+	{								\
+		EXPAND_FOR_EACH_PASS_ARGS(_INIT_BITMAP_MEMBER, (nbits),	\
+					  ##__VA_ARGS__)		\
+	}
+
 /*
  * Allocation and deallocation of bitmap.
  * Provided in lib/bitmap.c to avoid circular dependency.
-- 
2.26.2
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help