Skip to content

Commit

Permalink
sys: util: force Zephyr BIT/MIN/MAX/CLAMP definitions
Browse files Browse the repository at this point in the history
We've been guarding the definition of BIT/MIN/MAX/CLAMP macros as they
are commonly found in other projects. This is dangerous, because
depending on the order of includes, Zephyr code could end up using
macros defined by other projects (e.g. via HALs).

The current #ifndef _protection_ goes against the recently established
guidelines in zephyrproject-rtos#51963, so let's remove it.

Ref.
https://docs.zephyrproject.org/latest/contribute/coding_guidelines/
index.html#rule-a-3-macro-name-collisions

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
  • Loading branch information
gmarull committed Apr 11, 2023
1 parent db1d457 commit 2401aa4
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 8 deletions.
6 changes: 0 additions & 6 deletions include/zephyr/sys/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ extern "C" {
#define ceiling_fraction(numerator, divider) __DEPRECATED_MACRO \
DIV_ROUND_UP(numerator, divider)

#ifndef MAX
/**
* @brief Obtain the maximum of two values.
*
Expand All @@ -280,9 +279,7 @@ extern "C" {
* @returns Maximum value of @p a and @p b.
*/
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef MIN
/**
* @brief Obtain the minimum of two values.
*
Expand All @@ -295,9 +292,7 @@ extern "C" {
* @returns Minimum value of @p a and @p b.
*/
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef CLAMP
/**
* @brief Clamp a value to a given range.
*
Expand All @@ -311,7 +306,6 @@ extern "C" {
* @returns Clamped value.
*/
#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
#endif

/**
* @brief Checks if a value is within range.
Expand Down
2 changes: 0 additions & 2 deletions include/zephyr/sys/util_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ extern "C" {
*/
#include <zephyr/sys/util_internal.h>

#ifndef BIT
#if defined(_ASMLANGUAGE)
#define BIT(n) (1 << (n))
#else
Expand All @@ -43,7 +42,6 @@ extern "C" {
*/
#define BIT(n) (1UL << (n))
#endif
#endif

/** @brief 64-bit unsigned integer with bit position @p _n set. */
#define BIT64(_n) (1ULL << (_n))
Expand Down

0 comments on commit 2401aa4

Please sign in to comment.