From 4c96bc2dd93ae3e63e84c1164104e5d0e3a14156 Mon Sep 17 00:00:00 2001 From: Moritz Fischer Date: Wed, 14 Sep 2022 11:35:39 -0700 Subject: [PATCH 1/4] sys: util: Prevent multiple definitions of ARRAY_SIZE Prefix ARRAY_SIZE macro with an #undef ARRAY_SIZE to prevent multiple definition issues when dealing with third party libraries that have their own definitions. Signed-off-by: Moritz Fischer --- include/zephyr/sys/util.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/zephyr/sys/util.h b/include/zephyr/sys/util.h index fdc4532cf8ceae..8f0ce96b9b8e51 100644 --- a/include/zephyr/sys/util.h +++ b/include/zephyr/sys/util.h @@ -82,6 +82,7 @@ extern "C" { /* The built-in function used below for type checking in C is not * supported by GNU C++. */ +#undef ARRAY_SIZE #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) #else /* __cplusplus */ @@ -105,6 +106,7 @@ extern "C" { * * In C, passing a pointer as @p array causes a compile error. */ +#undef ARRAY_SIZE #define ARRAY_SIZE(array) \ ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0])))) From 2922060ac442747f38a5075ff4dd0bf7cc9fc070 Mon Sep 17 00:00:00 2001 From: Moritz Fischer Date: Wed, 14 Sep 2022 15:46:17 -0700 Subject: [PATCH 2/4] tests: unit: Add explicit ARRAY_SIZE tests This adds a test that explicitly (rather than implicitly) tests the ARRAY_SIZE(x) macro. Signed-off-by: Moritz Fischer --- tests/unit/util/main.c | 10 ++++++++++ tests/unit/util/test.inc | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/tests/unit/util/main.c b/tests/unit/util/main.c index 44f73bfb7d3b76..4a83146f6e5085 100644 --- a/tests/unit/util/main.c +++ b/tests/unit/util/main.c @@ -116,6 +116,11 @@ ZTEST(util_cxx, test_ARRAY_INDEX) run_ARRAY_INDEX(); } +ZTEST(util_cxx, test_ARRAY_SIZE) +{ + run_ARRAY_SIZE(); +} + ZTEST(util_cxx, test_PART_OF_ARRAY) { run_PART_OF_ARRAY(); @@ -234,6 +239,11 @@ ZTEST(util_cc, test_ARRAY_INDEX) run_ARRAY_INDEX(); } +ZTEST(util_cc, test_ARRAY_SIZE) +{ + run_ARRAY_SIZE(); +} + ZTEST(util_cc, test_PART_OF_ARRAY) { run_PART_OF_ARRAY(); diff --git a/tests/unit/util/test.inc b/tests/unit/util/test.inc index 08358c87820760..e3afc5e504e729 100644 --- a/tests/unit/util/test.inc +++ b/tests/unit/util/test.inc @@ -526,3 +526,9 @@ void run_ARRAY_INDEX_FLOOR(void) zassert_equal(array[ARRAY_INDEX_FLOOR(array, &alias[1])], 0); } + +void run_ARRAY_SIZE(void) +{ + size_t array[] = {1, 2, 3, 4}; + zassert_equal(ARRAY_SIZE(array), 4); +} From 9542e34583dcde3d47bfa06fba2094058441836f Mon Sep 17 00:00:00 2001 From: Moritz Fischer Date: Wed, 5 Oct 2022 09:52:31 -0700 Subject: [PATCH 3/4] sys: util: Take stronger stance on MIN()/MAX() Instead of guarding with #ifndef against previous definitions use a stronger stance and #undef previous definitions. Signed-off-by: Moritz Fischer --- include/zephyr/sys/util.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/zephyr/sys/util.h b/include/zephyr/sys/util.h index 8f0ce96b9b8e51..b4f60dda8977c7 100644 --- a/include/zephyr/sys/util.h +++ b/include/zephyr/sys/util.h @@ -239,7 +239,6 @@ extern "C" { #define ceiling_fraction(numerator, divider) \ (((numerator) + ((divider) - 1)) / (divider)) -#ifndef MAX /** * @brief Obtain the maximum of two values. * @@ -251,10 +250,9 @@ extern "C" { * * @returns Maximum value of @p a and @p b. */ +#undef MAX #define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#endif -#ifndef MIN /** * @brief Obtain the minimum of two values. * @@ -266,8 +264,8 @@ extern "C" { * * @returns Minimum value of @p a and @p b. */ +#undef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#endif #ifndef CLAMP /** From 37cf75d0cfb7819488214ece840abae4f413dcf2 Mon Sep 17 00:00:00 2001 From: Moritz Fischer Date: Wed, 5 Oct 2022 15:57:30 -0700 Subject: [PATCH 4/4] doc/release-note-3.3: Add notes about MAX/MIN/ARRAY_SIZE Added summary of new behavior for MAX(), MIN() and ARRAY_SIZE(). Signed-off-by: Moritz Fischer --- doc/releases/release-notes-3.3.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/releases/release-notes-3.3.rst b/doc/releases/release-notes-3.3.rst index 31a28d4415149c..42da36cdb43312 100644 --- a/doc/releases/release-notes-3.3.rst +++ b/doc/releases/release-notes-3.3.rst @@ -28,6 +28,12 @@ Deprecated in this release Stable API changes in this release ================================== +* System Utility Headers + + * Prefixed :c:func:`MIN` and :c:func:`MAX` and :c:func:`ARRAY_SIZE` with `#undef` to prevent + accidential multiple definitions when dealing with third party libraries that bring their + own definitions. + This is a departure from previous behavior where include order of header files mattered. New APIs in this release ========================