diff options
author | H. Peter Anvin <hpa@zytor.com> | 2025-06-11 18:35:44 -0700 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2025-06-17 09:57:40 -0300 |
commit | c66801f36135a7b031ca37e25a35e98255b20def (patch) | |
tree | d960299ccfe7b95769df607ce1e7e711eb882192 | |
parent | be413adedfca146a6fb8cabe3df1244a70f106f3 (diff) | |
download | glibc-c66801f36135a7b031ca37e25a35e98255b20def.zip glibc-c66801f36135a7b031ca37e25a35e98255b20def.tar.gz glibc-c66801f36135a7b031ca37e25a35e98255b20def.tar.bz2 |
include/array_length.h: add array_foreach[_const] macros
Add simple-to-use iterator macros for arrays. They are used instead
of explicit for statements, like:
/* Test all common speeds */
array_foreach_const (ts, test_speeds)
test (fd, *ts);
In this case, ts will be a const pointer to each of the elements of
test_speeds in turn.
Named array_foreach*() to allow for other kinds of equivalent iterator
macros in the future.
Signed-off-by: "H. Peter Anvin" (Intel) <hpa@zytor.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
-rw-r--r-- | include/array_length.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/include/array_length.h b/include/array_length.h index 2305e21..2a4f09f 100644 --- a/include/array_length.h +++ b/include/array_length.h @@ -33,4 +33,18 @@ VAR must evaluate to an array, not a pointer. */ #define array_end(var) (&(var)[array_length (var)]) +/* array_foreach (PTR, ARRAY) iterates over all the elements in an + array, assigning the locally defined pointer variable PTR to each + element in turn. + + array_foreach_const (PTR, ARRAY) does the same, but *PTR is declared + const even if the array is not. */ +#define array_foreach(ptr, array) \ + for (__typeof ((array)[0]) *ptr = (array) ; \ + ptr < array_end (array) ; ptr++) + +#define array_foreach_const(ptr, array) \ + for (const __typeof ((array)[0]) *ptr = (array) ; \ + ptr < array_end (array) ; ptr++) + #endif /* _ARRAY_LENGTH_H */ |