Message ID | 20200928191237.32063-1-colomar.6.4.3@gmail.com |
---|---|
State | Not applicable |
Headers |
Return-Path: <libc-alpha-bounces@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3179D39540C4; Mon, 28 Sep 2020 19:13:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3179D39540C4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1601320382; bh=vz8QchO9cmYjlrvcRjFTzSFLrDWK4NlG4rgcy0Z2ls0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=VHXl4ppm9cVLwA7f3rCBM7D7Y1/PU4RmzZGftsDH/2iqUojtcpTCQfRZWMaAEvZHh nZFFTu9+rbMNouY7G3Q+zXtPmVNVhyYoHxcfmcsP6CCzOLB0lNsqrapexVVHpNvuhG dDOstIlSFWcXwRMBmnFyVOcMqmb+NWU/QHuL7n3I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by sourceware.org (Postfix) with ESMTPS id 66FB33861830 for <libc-alpha@sourceware.org>; Mon, 28 Sep 2020 19:12:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 66FB33861830 Received: by mail-wr1-x443.google.com with SMTP id x14so2511590wrl.12 for <libc-alpha@sourceware.org>; Mon, 28 Sep 2020 12:12:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vz8QchO9cmYjlrvcRjFTzSFLrDWK4NlG4rgcy0Z2ls0=; b=iKoXeVB866UBhgmQ3meXhqkk5gEs/JzxyasSa6x5ab6JTjbyg7gusf8BT9hIpcqVVK 52p7LgAJTuXwveH10RkCoGVMzDY6Y+GurjFM7ZECEnisLqoID3PxAibIkLaMRoGa1OwU WnO9zEfpQwlMsdnrDWBosTybbVbjFogiAdLv45Vj85LwoXQ5Zv970Makg+O4LnpZl9cq bXlEu9gjTVDu0HbVRkVDEdXnF2DoBwBt4sVxJ2ryvaTEmbvms3fCKxDyykcQbKBYNlIT 27BcPD3zNUC65zRCpX2BPFHF3qKHa06mojcUYvTdWd/LnrpEKRUgZJ8VABS5EzLl9VPI vEdg== X-Gm-Message-State: AOAM5312mXnlkHZZZJ88xiRVxGhtL/ezS0c8qsW+qkyK3dZjkUGpIbIQ X0N6NVQX0hlgUmYUSvd92Hv4AQZ5LC4Rsg== X-Google-Smtp-Source: ABdhPJxiovG5tEEYTxk/MpoF6F52R8oux8Nz02/E+YQwQi67lZw1/Wi5MOd0u/CLQO5ZyrON/8wPTw== X-Received: by 2002:adf:dd0b:: with SMTP id a11mr3152720wrm.422.1601320376258; Mon, 28 Sep 2020 12:12:56 -0700 (PDT) Received: from localhost.localdomain ([170.253.60.68]) by smtp.googlemail.com with ESMTPSA id u17sm3072159wri.45.2020.09.28.12.12.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 12:12:55 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4] <sys/param.h>: Add nitems() Date: Mon, 28 Sep 2020 21:12:37 +0200 Message-Id: <20200928191237.32063-1-colomar.6.4.3@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list <libc-alpha.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/libc-alpha/> List-Post: <mailto:libc-alpha@sourceware.org> List-Help: <mailto:libc-alpha-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=subscribe> From: Alejandro Colomar via Libc-alpha <libc-alpha@sourceware.org> Reply-To: Alejandro Colomar <colomar.6.4.3@gmail.com> Cc: fweimer@redhat.com, gcc@gcc.gnu.org, ville.voutilainen@gmail.com, linux-man@vger.kernel.org, Alejandro Colomar <colomar.6.4.3@gmail.com>, rusty@rustcorp.com.au, linux-kernel@vger.kernel.org, libstdc++@gcc.gnu.org, libc-coord@lists.openwall.com, jwakely@redhat.com, enh@google.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" <libc-alpha-bounces@sourceware.org> |
Series |
[v4] <sys/param.h>: Add nitems()
|
|
Commit Message
Alejandro Colomar
Sept. 28, 2020, 7:12 p.m. UTC
'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.
Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
---
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(+)
Comments
Ping. On 2020-09-28 21:12, Alejandro Colomar wrote: > '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. > > Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com> > --- > > 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(+) > > diff --git a/misc/sys/param.h b/misc/sys/param.h > index d7c319b157..08d4093961 100644 > --- a/misc/sys/param.h > +++ b/misc/sys/param.h > @@ -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 On 2020-09-28 21:12, Alejandro Colomar wrote: > '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. > > Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com> > --- > > 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(+) > > diff --git a/misc/sys/param.h b/misc/sys/param.h > index d7c319b157..08d4093961 100644 > --- a/misc/sys/param.h > +++ b/misc/sys/param.h > @@ -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 */ >
Hi, Do you think the patch is ready, or is there anything else I should do before merging it? Thanks, Alex On 9/28/20 9:12 PM, Alejandro Colomar wrote: > '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. > > Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com> > --- > > 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(+) > > diff --git a/misc/sys/param.h b/misc/sys/param.h > index d7c319b157..08d4093961 100644 > --- a/misc/sys/param.h > +++ b/misc/sys/param.h > @@ -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 */ >
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.
Hi Joseph, On 11/17/20 11:44 PM, Joseph Myers wrote: > 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. > 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
On Tue, 17 Nov 2020, Alejandro Colomar via Libc-alpha wrote: > 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. > 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.
On 11/18/20 12:11 AM, Joseph Myers wrote: > On Tue, 17 Nov 2020, Alejandro Colomar via Libc-alpha wrote: > >> 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. > >> 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
diff --git a/misc/sys/param.h b/misc/sys/param.h index d7c319b157..08d4093961 100644 --- a/misc/sys/param.h +++ b/misc/sys/param.h @@ -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 */