Loading...
--- Libc/Libc-583/include/struct.h
+++ Libc/Libc-1669.0.4/include/struct.h
@@ -48,4 +48,32 @@
#define strbase(name, addr, field) \
((struct name *)((char *)(addr) - fldoff(name, field)))
+/*
+ * countof() cannot be safely used in a _Static_assert statement, so we provide
+ * an unsafe variant that does not verify the input array is statically-defined.
+ */
+#define countof_unsafe(arr) \
+ (sizeof(arr) / sizeof(arr[0]))
+
+/* Number of elements in a statically-defined array */
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && __GNUC__
+#define countof(arr) ({ \
+ _Static_assert( \
+ !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])), \
+ "array must be statically defined"); \
+ (sizeof(arr) / sizeof(arr[0])); \
+})
+#else
+#define countof(arr) \
+ countof_unsafe(arr)
+#endif
+
+/* Length of a statically-defined string (does not include null terminator) */
+#define lenof(str) \
+ (sizeof(str) - 1)
+
+/* Last index of a statically-defined array */
+#define lastof(arr) \
+ (countof(arr) - 1)
+
#endif /* !_STRUCT_H_ */