978e96
commit c94a5688fb1228a862b2d4a3f1239cdc0e3349e5
978e96
Author: Florian Weimer <fweimer@redhat.com>
978e96
Date:   Thu Nov 2 12:14:01 2017 +0100
978e96
978e96
    <array_length.h>: New array_length and array_end macros
978e96
978e96
diff --git a/include/array_length.h b/include/array_length.h
978e96
new file mode 100644
978e96
index 0000000000000000..cb4a8b2a56bca3ad
978e96
--- /dev/null
978e96
+++ b/include/array_length.h
978e96
@@ -0,0 +1,36 @@
978e96
+/* The array_length and array_end macros.
978e96
+   Copyright (C) 2017 Free Software Foundation, Inc.
978e96
+   This file is part of the GNU C Library.
978e96
+
978e96
+   The GNU C Library is free software; you can redistribute it and/or
978e96
+   modify it under the terms of the GNU Lesser General Public
978e96
+   License as published by the Free Software Foundation; either
978e96
+   version 2.1 of the License, or (at your option) any later version.
978e96
+
978e96
+   The GNU C Library is distributed in the hope that it will be useful,
978e96
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
978e96
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
978e96
+   Lesser General Public License for more details.
978e96
+
978e96
+   You should have received a copy of the GNU Lesser General Public
978e96
+   License along with the GNU C Library; if not, see
978e96
+   <http://www.gnu.org/licenses/>.  */
978e96
+
978e96
+#ifndef _ARRAY_LENGTH_H
978e96
+#define _ARRAY_LENGTH_H
978e96
+
978e96
+/* array_length (VAR) is the number of elements in the array VAR.  VAR
978e96
+   must evaluate to an array, not a pointer.  */
978e96
+#define array_length(var)                                               \
978e96
+  __extension__ ({                                                      \
978e96
+    _Static_assert (!__builtin_types_compatible_p                       \
978e96
+                    (__typeof (var), __typeof (&(var)[0])),             \
978e96
+                    "argument must be an array");                       \
978e96
+    sizeof (var) / sizeof ((var)[0]);                                   \
978e96
+  })
978e96
+
978e96
+/* array_end (VAR) is a pointer one past the end of the array VAR.
978e96
+   VAR must evaluate to an array, not a pointer.  */
978e96
+#define array_end(var) (&(var)[array_length (var)])
978e96
+
978e96
+#endif /* _ARRAY_LENGTH_H */