gpiolib: Make code more robust by using for_each_if()
Subsystems (3)
v2 cover gpiolib: Make code more robust by using for_each_if()
Instead of opencoding with long lines, use for_each_if() macro which makes intention clearer and less error prone.
In v2:
- moved original for_each_if() implementation to the global header (Bart)
Andy Shevchenko (2):
drm: Move for_each_if() to util_macros.h for wider use
gpiolib: Switch to use for_each_if() helper
include/drm/drm_util.h | 16 +--------------- include/linux/gpio/driver.h | 7 ++++--- include/linux/util_macros.h | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 18 deletions(-)
Signature · 1 line
2.45.1.3035.g276e886db78b
PATCH v2 1/2 drm: Move for_each_if() to util_macros.h for wider use
Other subsystem(s) may want to reuse the for_each_if() macro. Move it to util_macros.h to make it globally available.
--- include/drm/drm_util.h | 16 +--------------- include/linux/util_macros.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-)
include/drm/drm_util.h +1 -15
@@ -36,6 +36,7 @@#include <linux/kgdb.h> #include <linux/preempt.h> #include <linux/smp.h> +#include <linux/util_macros.h> /* * Use EXPORT_SYMBOL_FOR_TESTS_ONLY() for functions that shall
@@ -47,21 +48,6 @@#define EXPORT_SYMBOL_FOR_TESTS_ONLY (x)#endif -/** -* for_each_if - helper for handling conditionals in various for_each macros -* @condition: The condition to check -* -* Typical use:: -* -* #define for_each_foo_bar(x, y) \' -* list_for_each_entry(x, y->list, head) \' -* for_each_if(x->something == SOMETHING) -* -* The for_each_if() macro makes the use of for_each_foo_bar() less error -* prone. -*/ -#define for_each_if (condition) if (!(condition)) {} else -/** * drm_can_sleep - returns true if currently okay to sleep *
include/linux/util_macros.h +15 -0
@@ -4,6 +4,21 @@#include <linux/math.h> +/** +* for_each_if - helper for handling conditionals in various for_each macros +* @condition: The condition to check +* +* Typical use:: +* +* #define for_each_foo_bar(x, y) \' +* list_for_each_entry(x, y->list, head) \' +* for_each_if(x->something == SOMETHING) +* +* The for_each_if() macro makes the use of for_each_foo_bar() less error +* prone. +*/ +#define for_each_if (condition) if (!(condition)) {} else +/** * find_closest - locate the closest element in a sorted array * @x: The reference value.
Signature · 1 line
2.45.1.3035.g276e886db78b
Re: [PATCH v2 1/2] drm: Move for_each_if() to util_macros.h for wider use
Quoted 7 lines · Andy Shevchenko
Other subsystem(s) may want to reuse the
for_each_if()macro. Move it toutil_macros.hto make it globally available.Suggested-by Bartosz Golaszewski <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>---
Who would pick it up? Should I take it via the GPIO tree and provide an immutable branch for the DRM tree?
Bart
Re: [PATCH v2 1/2] drm: Move for_each_if() to util_macros.h for wider use
Quoted 10 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Other subsystem(s) may want to reuse the
for_each_if()macro. Move it toutil_macros.hto make it globally available.Reviewed-by Bartosz Golaszewski <[email protected]>Who would pick it up? Should I take it via the GPIO tree and provide an immutable branch for the DRM tree?
I believe it makes sense as the first user outside drm is GPIO.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] drm: Move for_each_if() to util_macros.h for wider use
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Other subsystem(s) may want to reuse the
for_each_if()macro. Move it toutil_macros.hto make it globally available.Suggested-by Bartosz Golaszewski <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
Yay!
If you want to go down the memory lane, see [1]. ;)
[1] https://lore.kernel.org/r/[email protected]
Quoted 65 lines
--- include/drm/drm_util.h | 16 +--------------- include/linux/util_macros.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-)include/drm/drm_util.h +1
-15@@ -36,6 +36,7 @@#include <linux/kgdb.h> #include <linux/preempt.h> #include <linux/smp.h> +#include <linux/util_macros.h> /* * Use EXPORT_SYMBOL_FOR_TESTS_ONLY() for functions that shall @@ -47,21 +48,6 @@#define EXPORT_SYMBOL_FOR_TESTS_ONLY (x)#endif -/** -* for_each_if - helper for handling conditionals in various for_each macros -* @condition: The condition to check -* -* Typical use:: -* -* #define for_each_foo_bar(x, y) \' -* list_for_each_entry(x, y->list, head) \' -* for_each_if(x->something == SOMETHING) -* -* The for_each_if() macro makes the use of for_each_foo_bar() less error -* prone. -*/ -#define for_each_if (condition) if (!(condition)) {} else -/** * drm_can_sleep - returns true if currently okay to sleep * include/linux/util_macros.h +15
-0@@ -4,6 +4,21 @@#include <linux/math.h> +/** +* for_each_if - helper for handling conditionals in various for_each macros +* @condition: The condition to check +* +* Typical use:: +* +* #define for_each_foo_bar(x, y) \' +* list_for_each_entry(x, y->list, head) \' +* for_each_if(x->something == SOMETHING) +* +* The for_each_if() macro makes the use of for_each_foo_bar() less error +* prone. +*/ +#define for_each_if (condition) if (!(condition)) {} else +/** * find_closest - locate the closest element in a sorted array * @x: The reference value.
Signature · 1 line
Jani Nikula, Intel
Re: [PATCH v2 1/2] drm: Move for_each_if() to util_macros.h for wider use
Quoted 5 lines · Jani Nikula
Quoted 2 lines · Andy Shevchenko
Other subsystem(s) may want to reuse the
for_each_if()macro. Move it toutil_macros.hto make it globally available.Acked-by Jani Nikula <[email protected]>
Thanks!
Quoted 3 lines
If you want to go down the memory lane, see [1]. ;)
Yeah, I have read it a few hours ago to refresh my memories.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 2/2 gpiolib: Switch to use for_each_if() helper
In reply to Andy ShevchenkoThe for_each_*() APIs that are conditional can be written shorter and less error prone with for_each_if() helper in use. Switch them to use this helper.
--- include/linux/gpio/driver.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/gpio/driver.h +4 -3
@@ -14,6 +14,7 @@#include <linux/property.h> #include <linux/spinlock_types.h> #include <linux/types.h> +#include <linux/util_macros.h> #ifdef CONFIG_GENERIC_MSI_IRQ #include <asm/msi.h>
@@ -561,7 +562,7 @@ DEFINE_CLASS(_gpiochip_for_each_data,for (CLASS (_gpiochip_for_each_data, _data)(&_label, &_i); \ _i < _size; \ _i++,kfree (_label), _label =NULL ) \ -if (IS_ERR (_label =gpiochip_dup_line_label (_chip, _base + _i))) {}else +for_each_if (!IS_ERR (_label =gpiochip_dup_line_label (_chip, _base + _i)))/** * for_each_hwgpio - Iterates over all GPIOs for given chip.
@@ -583,7 +584,7 @@ DEFINE_CLASS(_gpiochip_for_each_data,*/ #define for_each_requested_gpio_in_range (_chip, _i, _base, _size, _label) \ for_each_hwgpio_in_range(_chip, _i, _base, _size, _label) \ - if (_label == NULL) {} else + for_each_if(_label)/* Iterates over all requested GPIO of the given @chip */ #define for_each_requested_gpio (chip, i, label) \
@@ -869,7 +870,7 @@ static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,#define for_each_gpiochip_node (dev, child) \ device_for_each_child_node(dev, child) \ - if (!fwnode_property_present(child, "gpio-controller")) {} else + for_each_if(fwnode_property_present(child, "gpio-controller"))static inline unsigned int gpiochip_node_count (struct device *dev) {
Signature · 1 line
2.45.1.3035.g276e886db78b
Quoted 2 lines · Andy Shevchenko
Instead of opencoding with long lines, use
for_each_if()macro which makes intention clearer and less error prone.
Excellent patch series.
Yours,
Linus Walleij
Bartosz Golaszewski
AppliedFrom: Bartosz Golaszewski <[email protected]>
Quoted 11 lines · Andy Shevchenko
Instead of opencoding with long lines, use
for_each_if()macro which makes intention clearer and less error prone.In v2:
- moved originalfor_each_if()implementation to the global header (Bart)Andy Shevchenko (2):
drm: Movefor_each_if()toutil_macros.hfor wider use
gpiolib: Switch to usefor_each_if()helper[...]
Applied, thanks!
[1/2] drm: Move for_each_if() to util_macros.h for wider use
commit: b82e90c6f987e8f930523313eb803209cf9c6c97
[2/2] gpiolib: Switch to use for_each_if() helper
commit: c60fa3ba030a4d95b55fcca8945dacac89c542a6Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>