diff mbox series

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

Message ID 20210406221107.1004-10-kabel@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: phy: marvell10g updates | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count fail Series longer than 15 patches
netdev/tree_selection success Clearly marked for net-next
netdev/subject_prefix success Link
netdev/cc_maintainers fail 4 maintainers not CCed: jianpeng.ma@intel.com andriy.shevchenko@linux.intel.com akpm@linux-foundation.org yury.norov@gmail.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 23259 this patch: 23259
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch fail ERROR: Macros with complex values should be enclosed in parentheses
netdev/build_allmodconfig_warn success Errors and warnings before: 22868 this patch: 22868
netdev/header_inline success Link

Commit Message

Marek Behún April 6, 2021, 10:10 p.m. UTC
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(+)

Comments

Andrew Lunn April 6, 2021, 11:38 p.m. UTC | #1
On Wed, Apr 07, 2021 at 12:10:58AM +0200, Marek Behún wrote:
> 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...'.
> + */

Doesn't the /** mean this is kernel doc? The rest does not seem to
follow kdoc. Does this compile cleanly with W=1?

       Andrew
Marek Behún April 6, 2021, 11:50 p.m. UTC | #2
On Wed, 7 Apr 2021 01:38:30 +0200
Andrew Lunn <andrew@lunn.ch> wrote:

> On Wed, Apr 07, 2021 at 12:10:58AM +0200, Marek Behún wrote:
> > 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...'.
> > + */  
> 
> Doesn't the /** mean this is kernel doc? The rest does not seem to
> follow kdoc. Does this compile cleanly with W=1?
> 
>        Andrew

Hmm. I just used the same style as was above in the same file, for
/**
 * DOC: declare bitmap
 ...

Anyway W=1 does not complain.

But it does complain about the implementation for INITIALIZE_BITMAP. It
seems that we have to use -Wno-override-init.
This seems to be a new option for gcc. For clang,
scripts/Makefile.extrawarn already uses -Wno-initializer-overrides, but
we have to add -Wno-override-init for gcc.

Marek
diff mbox series

Patch

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.