unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org
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
Subject: [PATCH v4] <sys/param.h>: Add nitems()
Date: Mon, 28 Sep 2020 21:12:37 +0200	[thread overview]
Message-ID: <20200928191237.32063-1-colomar.6.4.3@gmail.com> (raw)

'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 */
-- 
2.28.0


             reply	other threads:[~2020-09-28 19:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28 19:12 Alejandro Colomar via Libc-alpha [this message]
2020-10-11 16:43 ` Ping: Re: [PATCH v4] <sys/param.h>: Add nitems() Alejandro Colomar via Libc-alpha
2020-10-27 11:38 ` Ping(2): " Alejandro Colomar via Libc-alpha
2020-11-17 22:23 ` Ping(3): " Alejandro Colomar via Libc-alpha
2020-11-17 22:44   ` Joseph Myers
2020-11-17 22:59     ` Alejandro Colomar via Libc-alpha
2020-11-17 23:11       ` Joseph Myers
2020-11-18 11:26         ` Alejandro Colomar via Libc-alpha

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

  List information: https://www.gnu.org/software/libc/involved.html

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

  git send-email \
    --in-reply-to=20200928191237.32063-1-colomar.6.4.3@gmail.com \
    --to=libc-alpha@sourceware.org \
    --cc=colomar.6.4.3@gmail.com \
    --cc=enh@google.com \
    --cc=fweimer@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libc-coord@lists.openwall.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).