mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: Florian Weimer <fweimer@redhat.com>,
	Jonathan Wakely <jwakely@redhat.com>
Cc: Ville Voutilainen <ville.voutilainen@gmail.com>,
	gcc@gcc.gnu.org, libstdc++ <libstdc++@gcc.gnu.org>,
	Libc-alpha <libc-alpha@sourceware.org>,
	LKML <linux-kernel@vger.kernel.org>,
	libc-coord@lists.openwall.com
Subject: Re: Expose 'array_length()' macro in <sys/param.h>
Date: Tue, 22 Sep 2020 12:35:21 +0200	[thread overview]
Message-ID: <ebe90ee6-b227-d1cf-c1a4-6b03b0037049@gmail.com> (raw)
In-Reply-To: <87k0wmyvtt.fsf@oldenburg2.str.redhat.com>

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 :) */
		;
}

  reply	other threads:[~2020-09-22 10:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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               ` Alejandro Colomar
2020-09-22  9:40                 ` Jonathan Wakely
2020-09-22 10:01                   ` Florian Weimer
2020-09-22 10:35                     ` Alejandro Colomar [this message]
     [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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ebe90ee6-b227-d1cf-c1a4-6b03b0037049@gmail.com \
    --to=colomar.6.4.3@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=libc-coord@lists.openwall.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ville.voutilainen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome