<sys/param.h>: Add nitems()
PATCH v4 1/1 <sys/param.h>: Add nitems()
'nitems()' calculates the length of an array in number of items. It is safe: if a pointer is passed to the macro (or function, in C++), the compilation is broken due to:
- In >= C11: _Static_assert()
- In C89, C99: Negative anonymous bitfield
- In C++: The template requires an array
Some BSDs already provide a macro nitems() in <sys/param.h>, although it usually doesn't provide safety against pointers.
This patch uses the same name for compatibility reasons, and to be the least disruptive with existing code.
This patch also adds some other macros, which are required by 'nitems()':
__is_same_type(a, b):
Returns non-zero if the two input arguments are of the same type.
__is_array(arr):
Returns non-zero if the input argument is of an array type.
__must_be(expr, msg):
Allows using _Static_assert() everywhere an expression can be used. It evaluates '(int)0' or breaks the compilation.
__must_be_array(arr):
It evaluates to '(int)0' if the argument is of an array type. Else, it breaks compilation.
__nitems(arr):
It implements the basic sizeof division needed to calculate the array length.
P.S.: I'd like to put this patch in the public domain.
---
A few changes since v3:
- Macros don't need reserved names in their parameters, so I simplified those names.
- I fixed some wrong indentation levels.
- Renamed __array_len() to __nitems() for consistency.
misc/sys/param.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
misc/sys/param.h +47 -0
@@ -102,5 +102,52 @@#define MIN (a,b) (((a)<(b))?(a):(b))#define MAX (a,b) (((a)>(b))?(a):(b)) +/* Macros related to the types of variables */ +#define __is_same_type (a, b) \ + __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) +#define __is_array (arr) (!__is_same_type((arr), &(arr)[0])) + +/* Macros for embedding _Static_assert() in expressions */ +#if __STDC_VERSION__ >=201112L +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + _Static_assert((expr), msg); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#else +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + int : (-!(expr)); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#endif +#define __must_be_array (arr) __must_be(__is_array(arr), "Must be an array!") + +/* Macros for array sizes */ +#if defined(__cplusplus) +# if __cplusplus >=201103L +template<typename _Tp, std::size_t _Len> + constexpr inline std::size_t + nitems(const _Tp(&)[_Len]) __THROW + { +return _Len; + } +# else /* __cplusplus < 201103L */ +template<typename _Tp, std::size_t _Len> +char + (&__nitems_chararr(const _Tp(&)[_Len]))[_Len]; +# define nitems (arr) (sizeof(__nitems_chararr(arr))) +# endif /* __cplusplus < 201103L */ +#else /* !defined(__cplusplus) */ +# define __nitems (arr) (sizeof((arr)) / sizeof((arr)[0])) +# define nitems (arr) (__nitems(arr) + __must_be_array(arr)) +#endif /* !defined(__cplusplus) */ +#endif /* sys/param.h */
Signature · 1 line
2.28.0
Ping: Re: [PATCH v4] <sys/param.h>: Add nitems()
Ping.
Quoted 109 lines · Alejandro Colomar
'
nitems()' calculates the length of an array in number of items. It is safe: if a pointer is passed to the macro (or function, in C++), the compilation is broken due to:
- In >= C11:_Static_assert()
- In C89, C99: Negative anonymous bitfield
- In C++: The template requires an arraySome BSDs already provide a macro
nitems()in <sys/param.h>, although it usually doesn't provide safety against pointers.This patch uses the same name for compatibility reasons, and to be the least disruptive with existing code.
This patch also adds some other macros, which are required by '
nitems()':
__is_same_type(a, b):
Returns non-zero if the two input arguments are of the same type.
__is_array(arr):
Returns non-zero if the input argument is of an array type.
__must_be(expr, msg):
Allows using_Static_assert()everywhere an expression can be used. It evaluates '(int)0' or breaks the compilation.
__must_be_array(arr):
It evaluates to '(int)0' if the argument is of an array type. Else, it breaks compilation.
__nitems(arr):
It implements the basic sizeof division needed to calculate the array length.
P.S.: I'd like to put this patch in the public domain.Signed-off-by Alejandro Colomar <[email protected]>---
A few changes since v3:
- Macros don't need reserved names in their parameters, so I simplified those names.
- I fixed some wrong indentation levels.
- Renamed
__array_len()to__nitems()for consistency.misc/sys/param.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)misc/sys/param.h +47
-0@@ -102,5 +102,52 @@#define MIN (a,b) (((a)<(b))?(a):(b))#define MAX (a,b) (((a)>(b))?(a):(b)) +/* Macros related to the types of variables */ +#define __is_same_type (a, b) \ + __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) +#define __is_array (arr) (!__is_same_type((arr), &(arr)[0])) + +/* Macros for embedding _Static_assert() in expressions */ +#if __STDC_VERSION__ >=201112L +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + _Static_assert((expr), msg); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#else +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + int : (-!(expr)); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#endif +#define __must_be_array (arr) __must_be(__is_array(arr), "Must be an array!") + +/* Macros for array sizes */ +#if defined(__cplusplus) +# if __cplusplus >=201103L +template<typename _Tp, std::size_t _Len> + constexpr inline std::size_t + nitems(const _Tp(&)[_Len]) __THROW + { +return _Len; + } +# else /* __cplusplus < 201103L */ +template<typename _Tp, std::size_t _Len> +char + (&__nitems_chararr(const _Tp(&)[_Len]))[_Len]; +# define nitems (arr) (sizeof(__nitems_chararr(arr))) +# endif /* __cplusplus < 201103L */ +#else /* !defined(__cplusplus) */ +# define __nitems (arr) (sizeof((arr)) / sizeof((arr)[0])) +# define nitems (arr) (__nitems(arr) + __must_be_array(arr)) +#endif /* !defined(__cplusplus) */ +#endif /* sys/param.h */
Ping(2): [PATCH v4] <sys/param.h>: Add nitems()
In reply to Alejandro ColomarPing
Quoted 109 lines · Alejandro Colomar
'
nitems()' calculates the length of an array in number of items. It is safe: if a pointer is passed to the macro (or function, in C++), the compilation is broken due to:
- In >= C11:_Static_assert()
- In C89, C99: Negative anonymous bitfield
- In C++: The template requires an arraySome BSDs already provide a macro
nitems()in <sys/param.h>, although it usually doesn't provide safety against pointers.This patch uses the same name for compatibility reasons, and to be the least disruptive with existing code.
This patch also adds some other macros, which are required by '
nitems()':
__is_same_type(a, b):
Returns non-zero if the two input arguments are of the same type.
__is_array(arr):
Returns non-zero if the input argument is of an array type.
__must_be(expr, msg):
Allows using_Static_assert()everywhere an expression can be used. It evaluates '(int)0' or breaks the compilation.
__must_be_array(arr):
It evaluates to '(int)0' if the argument is of an array type. Else, it breaks compilation.
__nitems(arr):
It implements the basic sizeof division needed to calculate the array length.
P.S.: I'd like to put this patch in the public domain.Signed-off-by Alejandro Colomar <[email protected]>---
A few changes since v3:
- Macros don't need reserved names in their parameters, so I simplified those names.
- I fixed some wrong indentation levels.
- Renamed
__array_len()to__nitems()for consistency.misc/sys/param.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)misc/sys/param.h +47
-0@@ -102,5 +102,52 @@#define MIN (a,b) (((a)<(b))?(a):(b))#define MAX (a,b) (((a)>(b))?(a):(b)) +/* Macros related to the types of variables */ +#define __is_same_type (a, b) \ + __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) +#define __is_array (arr) (!__is_same_type((arr), &(arr)[0])) + +/* Macros for embedding _Static_assert() in expressions */ +#if __STDC_VERSION__ >=201112L +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + _Static_assert((expr), msg); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#else +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + int : (-!(expr)); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#endif +#define __must_be_array (arr) __must_be(__is_array(arr), "Must be an array!") + +/* Macros for array sizes */ +#if defined(__cplusplus) +# if __cplusplus >=201103L +template<typename _Tp, std::size_t _Len> + constexpr inline std::size_t + nitems(const _Tp(&)[_Len]) __THROW + { +return _Len; + } +# else /* __cplusplus < 201103L */ +template<typename _Tp, std::size_t _Len> +char + (&__nitems_chararr(const _Tp(&)[_Len]))[_Len]; +# define nitems (arr) (sizeof(__nitems_chararr(arr))) +# endif /* __cplusplus < 201103L */ +#else /* !defined(__cplusplus) */ +# define __nitems (arr) (sizeof((arr)) / sizeof((arr)[0])) +# define nitems (arr) (__nitems(arr) + __must_be_array(arr)) +#endif /* !defined(__cplusplus) */ +#endif /* sys/param.h */
Ping(3): [PATCH v4] <sys/param.h>: Add nitems()
In reply to Alejandro ColomarHi,
Do you think the patch is ready,
or is there anything else I should do before merging it?
Thanks,
Alex
Quoted 109 lines · Alejandro Colomar
'
nitems()' calculates the length of an array in number of items. It is safe: if a pointer is passed to the macro (or function, in C++), the compilation is broken due to:
- In >= C11:_Static_assert()
- In C89, C99: Negative anonymous bitfield
- In C++: The template requires an arraySome BSDs already provide a macro
nitems()in <sys/param.h>, although it usually doesn't provide safety against pointers.This patch uses the same name for compatibility reasons, and to be the least disruptive with existing code.
This patch also adds some other macros, which are required by '
nitems()':
__is_same_type(a, b):
Returns non-zero if the two input arguments are of the same type.
__is_array(arr):
Returns non-zero if the input argument is of an array type.
__must_be(expr, msg):
Allows using_Static_assert()everywhere an expression can be used. It evaluates '(int)0' or breaks the compilation.
__must_be_array(arr):
It evaluates to '(int)0' if the argument is of an array type. Else, it breaks compilation.
__nitems(arr):
It implements the basic sizeof division needed to calculate the array length.
P.S.: I'd like to put this patch in the public domain.Signed-off-by Alejandro Colomar <[email protected]>---
A few changes since v3:
- Macros don't need reserved names in their parameters, so I simplified those names.
- I fixed some wrong indentation levels.
- Renamed
__array_len()to__nitems()for consistency.misc/sys/param.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)misc/sys/param.h +47
-0@@ -102,5 +102,52 @@#define MIN (a,b) (((a)<(b))?(a):(b))#define MAX (a,b) (((a)>(b))?(a):(b)) +/* Macros related to the types of variables */ +#define __is_same_type (a, b) \ + __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) +#define __is_array (arr) (!__is_same_type((arr), &(arr)[0])) + +/* Macros for embedding _Static_assert() in expressions */ +#if __STDC_VERSION__ >=201112L +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + _Static_assert((expr), msg); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#else +# define __must_be (expr, msg) ( \ + 0 * (int)sizeof( \ + struct { \ + int : (-!(expr)); \ + char _ISO_C_forbids_a_struct_with_no_members; \ + } \ + ) \ +) +#endif +#define __must_be_array (arr) __must_be(__is_array(arr), "Must be an array!") + +/* Macros for array sizes */ +#if defined(__cplusplus) +# if __cplusplus >=201103L +template<typename _Tp, std::size_t _Len> + constexpr inline std::size_t + nitems(const _Tp(&)[_Len]) __THROW + { +return _Len; + } +# else /* __cplusplus < 201103L */ +template<typename _Tp, std::size_t _Len> +char + (&__nitems_chararr(const _Tp(&)[_Len]))[_Len]; +# define nitems (arr) (sizeof(__nitems_chararr(arr))) +# endif /* __cplusplus < 201103L */ +#else /* !defined(__cplusplus) */ +# define __nitems (arr) (sizeof((arr)) / sizeof((arr)[0])) +# define nitems (arr) (__nitems(arr) + __must_be_array(arr)) +#endif /* !defined(__cplusplus) */ +#endif /* sys/param.h */
Re: Ping(3): [PATCH v4] <sys/param.h>: Add nitems()
I've asked the WG14 reflector why N2529 (and N2530, though that one's not relevant to this feature) doesn't seem to have made it onto a meeting agenda yet, when there have been two WG14 meetings since that proposal was made and a third one coming up.
Signature · 2 lines
Joseph S. Myers [email protected]
Re: Ping(3): [PATCH v4] <sys/param.h>: Add nitems()
Hi Joseph,
Quoted 3 lines · Joseph Myers
I've asked the WG14 reflector why N2529 (and N2530, though that one's not relevant to this feature) doesn't seem to have made it onto a meeting agenda yet, when there have been two WG14 meetings since that proposal
was
Quoted 2 lines
made and a third one coming up.
Nice!
Please update me on any feedback you receive.
So glibc will basically hold this patch
at least until the WG answers to that, right?
Thanks,
Alex
Re: Ping(3): [PATCH v4] <sys/param.h>: Add nitems()
Quoted 2 lines · Alejandro Colomar via Libc-alpha
Nice!
Please update me on any feedback you receive.
Apparently the author is planning new versions of those papers so WG14 discussion is waiting for those.
Quoted 2 lines
So glibc will basically hold this patch
at least until the WG answers to that, right?
I think that whether C2x gets an array-size feature of some kind is relevant to whether such a feature goes in glibc and what it looks like in glibc, but the fact that it will be considered in WG14 doesn't rule out glibc considering such a feature without waiting for WG14.
Signature · 2 lines
Joseph S. Myers [email protected]
Re: Ping(3): [PATCH v4] <sys/param.h>: Add nitems()
Quoted 8 lines · Joseph Myers
Quoted 2 lines · Alejandro Colomar via Libc-alpha
Nice!
Please update me on any feedback you receive.Apparently the author is planning new versions of those papers so WG14 discussion is waiting for those.
Quoted 8 lines
Quoted 2 lines
So glibc will basically hold this patch
at least until the WG answers to that, right?I think that whether C2x gets an array-size feature of some kind is relevant to whether such a feature goes in glibc and what it looks like in glibc, but the fact that it will be considered in WG14 doesn't rule out glibc considering such a feature without waiting for WG14.
Hi Joseph,
Yes, that's what I would expect, but it's been a long time since I sent the patch, so that's why I asked.
Maybe glibc is busy now with other more urgent things, I don't know.
Thanks,
Alex