From: Yu Watanabe Date: Sun, 5 Mar 2023 05:56:15 +0000 (+0900) Subject: macro: introduce FOREACH_ARRAY() macro X-Git-Tag: v254-rc1~1093^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5716c27e1f52d2aba9dd02916c01d6271d9d0b16;p=thirdparty%2Fsystemd.git macro: introduce FOREACH_ARRAY() macro The pattern that runs all array element is quite common. But, sometimes, the number of element may be in a signed integer, or the array may be NULL. --- diff --git a/src/basic/macro.h b/src/basic/macro.h index 2425d27819a..3de319d880f 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -297,6 +297,13 @@ static inline int __coverity_check_and_return__(int condition) { p != (typeof(p)) POINTER_MAX; \ p = *(++_l)) +#define _FOREACH_ARRAY(i, array, num, m, s) \ + for (typeof(num) m = (num); m > 0; m = 0) \ + for (typeof(array[0]) *s = (array), *i = s; s && i < s + m; i++) + +#define FOREACH_ARRAY(i, array, num) \ + _FOREACH_ARRAY(i, array, num, UNIQ_T(m, UNIQ), UNIQ_T(s, UNIQ)) + #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \ static inline void name(type *p) { \ func(p); \ diff --git a/src/test/test-macro.c b/src/test/test-macro.c index aec1f1ecd42..ef74b8273ed 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -584,4 +584,54 @@ TEST(ALIGNED) { #endif } +TEST(FOREACH_ARRAY) { + int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int b[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; + int x, n; + + x = n = 0; + FOREACH_ARRAY(i, a, 10) { + x += *i; + n++; + } + assert_se(x == 45); + assert_se(n == 10); + + x = n = 0; + FOREACH_ARRAY(i, a, 10) + FOREACH_ARRAY(j, b, 10) { + x += (*i) * (*j); + n++; + } + assert_se(x == 45 * 45); + assert_se(n == 10 * 10); + + x = n = 0; + FOREACH_ARRAY(i, a, 5) + FOREACH_ARRAY(j, b, 5) { + x += (*i) * (*j); + n++; + } + assert_se(x == 10 * 35); + assert_se(n == 5 * 5); + + x = n = 0; + FOREACH_ARRAY(i, a, 0) + FOREACH_ARRAY(j, b, 0) { + x += (*i) * (*j); + n++; + } + assert_se(x == 0); + assert_se(n == 0); + + x = n = 0; + FOREACH_ARRAY(i, a, -1) + FOREACH_ARRAY(j, b, -1) { + x += (*i) * (*j); + n++; + } + assert_se(x == 0); + assert_se(n == 0); +} + DEFINE_TEST_MAIN(LOG_INFO);