linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: Expose 'array_length()' macro in <sys/param.h>
       [not found]             ` <CAFk2RUbEtvgFb_FZmcM9L4-g1kG_E7S2p9gveM0Z5Fe=zEDm9w@mail.gmail.com>
@ 2020-09-22  9:10               ` Alejandro Colomar
  2020-09-22  9:40                 ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2020-09-22  9:10 UTC (permalink / raw)
  To: Ville Voutilainen, Jonathan Wakely, Florian Weimer
  Cc: gcc, libstdc++, Libc-alpha, libc-coord, LKML

[[ CC += LKML ]]

Thanks for all your input.  I learned some C++ :)

The following code works for all C and C++ standards:
g++ --std={c++98, c++03, c++11, c++14, c++17, c++20}
gcc --std={c89, c99, c11, c18, c2x}
With `-Wall -Wextra -Werror -pedantic -Wno-vla -Wno-sizeof-pointer-div`.
It doesn't compile when '+ __array_length(p)' is uncommented.
It compiles, and returns the correct value (18).
  With some exceptions:
c++ doesn't accept the VLA (w[]):

array_length.c: In function 'int main()':
array_length.c:101:22: error: no matching function for call to 
'__array_slength(int [y])'
   101 |   + __array_slength(w)
       |                      ^
array_length.c:38:1: note: candidate: 'template<class T, long int N> 
std::ptrdiff_t __array_slength(const T (&)[N])'
    38 | __array_slength(const T(&)[N])
       | ^~~~~~~~~~~~~~~
array_length.c:38:1: note:   template argument deduction/substitution 
failed:
array_length.c:101:22: note:   variable-sized array type 'long int' is 
not a valid template argument
   101 |   + __array_slength(w)
       |                      ^

But we can live with limited support for VLAs in C++.
So I needed to comment '+ __array_slength(w)',
and then the program correctly returns 11.

As Ville suggested, I renamed the function/macro to __array_[s]length().
However, (some) BSDs already provide nitems() in <sys/param.h>,
so it probably wouldn't be very drastic to
provide this function/macro with the name '[s]nitems()' there.

Would you like to add anything else before I write the patch?


BTW, I should note one more thing:

Linux has a macro named '__must_be_array()' with the same API,
but slightly different internal implementation,
so they should be aware of this change.
However, I don't think they include <sys/param.h> a lot,
so maybe it doesn't break anything; but they should be aware anyway.
I CC'd the LKML so they are aware and can give any suggestions.

Thanks,

Alex


------------------------------------------------------------------------

#if defined(__cplusplus)

# include <cstddef>
# if __cplusplus >= 201703L
#  include <iterator>
# endif

template<typename T, std::size_t N>
# if __cplusplus >= 201103L
constexpr
# endif
inline std::size_t
# if __cplusplus >= 201703L
__array_length(const T(&arr)[N])
# else
__array_length(const T(&)[N])
# endif
# if __cplusplus >= 201103L
noexcept
# endif
{
# if __cplusplus >= 201703L
	return	std::size(arr);
# else
	return	N;
# endif
}

template<typename T, std::ptrdiff_t N>
# if __cplusplus >= 201103L
constexpr
# endif
inline std::ptrdiff_t
# if __cplusplus >= 202002L
__array_slength(const T(&arr)[N])
# else
__array_slength(const T(&)[N])
# endif
# if __cplusplus >= 201103L
noexcept
# endif
{
# if __cplusplus >= 202002L
	return	std::ssize(arr);
# else
	return	N;
# endif
}


#else /* !defined(__cplusplus) */
#include <stddef.h>

# define __is_same_type(a, b)						\
	__builtin_types_compatible_p(__typeof__(a), __typeof__(b))
# define __is_array(arr)	(!__is_same_type((arr), &(arr)[0]))

# if __STDC_VERSION__ >= 201112L
#  define __must_be(e, msg)	(					\
	0 * (int)sizeof(						\
		struct {						\
			_Static_assert((e), msg);			\
			char ISO_C_forbids_a_struct_with_no_members__;	\
		}							\
	)								\
)
# else
#  define __must_be(e, msg)	(					\
	0 * (int)sizeof(						\
		struct {						\
			int	: (-!(e));				\
			char ISO_C_forbids_a_struct_with_no_members__;	\
		}							\
	)								\
)
# endif

# define __must_be_array(arr)	__must_be(__is_array(arr), "Must be an 
array!")

# define __array_len(arr)	(sizeof(arr) / sizeof((arr)[0]))
# define __array_length(arr)	(__array_len(arr) + __must_be_array(arr))
# define __array_slength(arr)	((ptrdiff_t)__array_length(arr))
#endif


int main(void)
{
	int a[5];
	const int x = 6;
	int v[x];
	int y = 7;
	int w[y];
	int *p;
	(void)p;
	(void)v;
	(void)w;

	return	__array_slength(a)
		+ __array_slength(v)
		+ __array_slength(w)
//		+ __array_length(p)
		;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Expose 'array_length()' macro in <sys/param.h>
  2020-09-22  9:10               ` Expose 'array_length()' macro in <sys/param.h> Alejandro Colomar
@ 2020-09-22  9:40                 ` Jonathan Wakely
  2020-09-22 10:01                   ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2020-09-22  9:40 UTC (permalink / raw)
  To: Alejandro Colomar
  Cc: Ville Voutilainen, Florian Weimer, gcc, libstdc++,
	Libc-alpha, LKML, libc-coord

On 22/09/20 11:10 +0200, Alejandro Colomar via Libstdc++ wrote:
>[[ CC += LKML ]]
>
>Thanks for all your input.  I learned some C++ :)
>
>The following code works for all C and C++ standards:
>g++ --std={c++98, c++03, c++11, c++14, c++17, c++20}
>gcc --std={c89, c99, c11, c18, c2x}
>With `-Wall -Wextra -Werror -pedantic -Wno-vla -Wno-sizeof-pointer-div`.
>It doesn't compile when '+ __array_length(p)' is uncommented.
>It compiles, and returns the correct value (18).
> With some exceptions:
>c++ doesn't accept the VLA (w[]):
>
>array_length.c: In function 'int main()':
>array_length.c:101:22: error: no matching function for call to 
>'__array_slength(int [y])'
>  101 |   + __array_slength(w)
>      |                      ^
>array_length.c:38:1: note: candidate: 'template<class T, long int N> 
>std::ptrdiff_t __array_slength(const T (&)[N])'
>   38 | __array_slength(const T(&)[N])
>      | ^~~~~~~~~~~~~~~
>array_length.c:38:1: note:   template argument deduction/substitution 
>failed:
>array_length.c:101:22: note:   variable-sized array type 'long int' is 
>not a valid template argument
>  101 |   + __array_slength(w)
>      |                      ^
>
>But we can live with limited support for VLAs in C++.
>So I needed to comment '+ __array_slength(w)',
>and then the program correctly returns 11.
>
>As Ville suggested, I renamed the function/macro to __array_[s]length().
>However, (some) BSDs already provide nitems() in <sys/param.h>,
>so it probably wouldn't be very drastic to
>provide this function/macro with the name '[s]nitems()' there.
>
>Would you like to add anything else before I write the patch?
>
>
>BTW, I should note one more thing:
>
>Linux has a macro named '__must_be_array()' with the same API,
>but slightly different internal implementation,
>so they should be aware of this change.
>However, I don't think they include <sys/param.h> a lot,
>so maybe it doesn't break anything; but they should be aware anyway.
>I CC'd the LKML so they are aware and can give any suggestions.
>
>Thanks,
>
>Alex
>
>
>------------------------------------------------------------------------
>
>#if defined(__cplusplus)
>
># include <cstddef>
># if __cplusplus >= 201703L
>#  include <iterator>
># endif
>
>template<typename T, std::size_t N>

You need to use reserved names here.

># if __cplusplus >= 201103L
>constexpr
># endif
>inline std::size_t
># if __cplusplus >= 201703L
>__array_length(const T(&arr)[N])
># else
>__array_length(const T(&)[N])
># endif
># if __cplusplus >= 201103L
>noexcept

If this is going to be part of Glibc then you can use its __THROW
macro instead.


># endif
>{
># if __cplusplus >= 201703L
>	return	std::size(arr);
># else
>	return	N;
># endif
>}

I don't see much point in using std::size here. If you're going to
provide the alternative implementation for when std::size isn't
defined, why not just use it always?

template<typename _Tp, std::size_t _Num>
#if __cplusplus >= 201103L
constexpr
#endif
inline std::size_t
__array_length(const _Tp(&)[_Num]) __THROW
{
   return _Num;
}

This only requires <cstddef>, not <iterator>.

>
>template<typename T, std::ptrdiff_t N>

This declaration is wrong, the array extent has type std::size_t. The
type you return from the function doesn't change that.

The __array_slength definition should be identical to __array_length
except for its name and return type.

template<typename _Tp, std::size_t _Num>
#if __cplusplus >= 201103L
constexpr
#endif
inline std::ptrdiff_t
__array_slength(const _Tp(&)[_Num]) __THROW
{
   return _Num;
}


>
>
>#else /* !defined(__cplusplus) */
>#include <stddef.h>
>
># define __is_same_type(a, b)						\
>	__builtin_types_compatible_p(__typeof__(a), __typeof__(b))
># define __is_array(arr)	(!__is_same_type((arr), &(arr)[0]))
>
># if __STDC_VERSION__ >= 201112L
>#  define __must_be(e, msg)	(					\
>	0 * (int)sizeof(						\
>		struct {						\
>			_Static_assert((e), msg);			\
>			char ISO_C_forbids_a_struct_with_no_members__;	\
>		}							\
>	)								\
>)
># else
>#  define __must_be(e, msg)	(					\
>	0 * (int)sizeof(						\
>		struct {						\
>			int	: (-!(e));				\
>			char ISO_C_forbids_a_struct_with_no_members__;	\
>		}							\
>	)								\
>)
># endif
>
># define __must_be_array(arr)	__must_be(__is_array(arr), "Must be an 
>array!")
>
># define __array_len(arr)	(sizeof(arr) / sizeof((arr)[0]))
># define __array_length(arr)	(__array_len(arr) + __must_be_array(arr))
># define __array_slength(arr)	((ptrdiff_t)__array_length(arr))
>#endif
>
>
>int main(void)
>{
>	int a[5];
>	const int x = 6;
>	int v[x];
>	int y = 7;
>	int w[y];
>	int *p;
>	(void)p;
>	(void)v;
>	(void)w;
>
>	return	__array_slength(a)
>		+ __array_slength(v)
>		+ __array_slength(w)
>//		+ __array_length(p)
>		;
>}
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Expose 'array_length()' macro in <sys/param.h>
  2020-09-22  9:40                 ` Jonathan Wakely
@ 2020-09-22 10:01                   ` Florian Weimer
  2020-09-22 10:35                     ` Alejandro Colomar
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2020-09-22 10:01 UTC (permalink / raw)
  To: Jonathan Wakely
  Cc: Alejandro Colomar, Ville Voutilainen, gcc, libstdc++,
	Libc-alpha, LKML, libc-coord

* Jonathan Wakely:

> I don't see much point in using std::size here. If you're going to
> provide the alternative implementation for when std::size isn't
> defined, why not just use it always?
>
> template<typename _Tp, std::size_t _Num>
> #if __cplusplus >= 201103L
> constexpr
> #endif
> inline std::size_t
> __array_length(const _Tp(&)[_Num]) __THROW
> {
>   return _Num;
> }
>
> This only requires <cstddef>, not <iterator>.

I agree that this is an advantage.  But the version without constexpr is
not sufficient because __array_length does not produce a constant
expression.

I've seen something like this used instead:

  template<typename _Tp, std::size_t _Num>
  char (&___array_length(const _Tp(&)[_Num]))[_Num];
  #define __array_length(v) (sizeof(___array_length(v)))

If the function type is too cute, a helper struct could be used instead.

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Expose 'array_length()' macro in <sys/param.h>
  2020-09-22 10:01                   ` Florian Weimer
@ 2020-09-22 10:35                     ` Alejandro Colomar
  0 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-09-22 10:35 UTC (permalink / raw)
  To: Florian Weimer, Jonathan Wakely
  Cc: Ville Voutilainen, gcc, libstdc++, Libc-alpha, LKML, libc-coord

Thanks again for your improvements.

I think this might be ready for a patch already.
Any more thoughts?

Thanks,

Alex

------------------------


#if defined(__cplusplus)
# include <cstddef>

# if __cplusplus >= 201103L
template<typename _Tp, std::size_t _Len>
constexpr inline std::size_t
__array_length(const _Tp(&)[_Len]) __THROW
{
	return	_Len;
}

template<typename _Tp, std::size_t _Len>
constexpr inline std::ptrdiff_t
__array_slength(const _Tp(&)[_Len]) __THROW
{
	return	_Len;
}
# else /* __cplusplus < 201103L */
template<typename _Tp, std::size_t _Len>
char (&__array_length(const _Tp(&)[_Len]))[_Len];
#  define __array_length(_Arr)	(sizeof(__array_length(_Arr)))
#  define __array_slength(_Arr)						\
	(static_cast<std::ptrdiff_t>(__array_length(_Arr)))
# endif /* __cplusplus >= 201103L */


#else /* !defined(__cplusplus) */
#include <stddef.h>

# define __is_same_type(_A, _B)						\
	__builtin_types_compatible_p(__typeof__(_A), __typeof__(_B))
# define __is_array(_Arr)	(!__is_same_type((_Arr), &(_Arr)[0]))

# 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!")

# define __array_len(_Arr)	(sizeof(_Arr) / sizeof((_Arr)[0]))
# define __array_length(_Arr)	(__array_len(_Arr) + __must_be_array(_Arr))
# define __array_slength(_Arr)	((ptrdiff_t)__array_length(_Arr))
#endif /* defined(__cplusplus) */


static int a[5];
static int v[__array_slength(a)];
static int w[__array_length(v)];
static int *p;

int main(void)
{
	int aa[5];
	const int xx = 6;
	int vv[xx];
	int yy = 7;
	int ww[yy];
	int *pp;

	(void)p;
	(void)pp;
	(void)ww;

	return	__array_slength(a)
		+ __array_length(v)
		+ __array_slength(w)
/*		+ __array_length(p) */ /* Always breaks :) */
		+ __array_length(aa)
		+ __array_slength(vv)
		+ __array_length(ww) /* Not in C++ */
/*		+ __array_length(pp) */ /* Always breaks :) */
		;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h>
       [not found] ` <alpine.DEB.2.21.2009301557590.5720@digraph.polyomino.org.uk>
@ 2020-09-30 20:39   ` Alejandro Colomar
  0 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2020-09-30 20:39 UTC (permalink / raw)
  To: Joseph Myers
  Cc: libc-alpha, libc-coord, libstdc++,
	gcc, linux-kernel, linux-man, Jonathan Wakely, Florian Weimer,
	Ville Voutilainen, enh, Rusty Russell, Alejandro Colomar



On 2020-09-30 17:58, Joseph Myers wrote:
> For some reason http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2529.pdf
> doesn't seem to have reached the agenda of a WG14 meeting yet, but adding
> a language feature like that to the standard would be another approach.
> 

Hi Joseph,

Yes, that would be great!

I hope they add that to the language. When/if that happens, nitems() 
could be `#define nitems(arr) _Lengthof(arr)` for std >= c2x.

In the meantime, I would add this macro to libc.

Maybe gcc could add such a great feature as an extension even before the 
standard does...

Too many wishes :)

BTW, I sent a PATCH v4 that I should've sent --in-reply-to PATCH v3 in 
this thread (but I forgot to do so); I'll link to it here:

https://sourceware.org/pipermail/libc-alpha/2020-September/117986.html

Thanks,

Alex

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-09-30 20:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <946e9377-0558-3adf-3eb9-38c507afe2d0@gmail.com>
     [not found] ` <874knr8qyl.fsf@oldenburg2.str.redhat.com>
     [not found]   ` <dbcf5c85-c468-72f8-0f83-92ec2a6a2991@gmail.com>
     [not found]     ` <875z875si2.fsf@oldenburg2.str.redhat.com>
     [not found]       ` <20200921140100.GA449323@redhat.com>
     [not found]         ` <e734429a-d543-7e75-48e9-a8297a94b035@gmail.com>
     [not found]           ` <20200921220443.GP6061@redhat.com>
     [not found]             ` <CAFk2RUbEtvgFb_FZmcM9L4-g1kG_E7S2p9gveM0Z5Fe=zEDm9w@mail.gmail.com>
2020-09-22  9:10               ` Expose 'array_length()' macro in <sys/param.h> Alejandro Colomar
2020-09-22  9:40                 ` Jonathan Wakely
2020-09-22 10:01                   ` Florian Weimer
2020-09-22 10:35                     ` Alejandro Colomar
     [not found] ` <alpine.DEB.2.21.2009301557590.5720@digraph.polyomino.org.uk>
2020-09-30 20:39   ` Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> Alejandro Colomar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).