Loading...
include/struct.h Libc-262 Libc-1583.40.7
--- Libc/Libc-262/include/struct.h
+++ Libc/Libc-1583.40.7/include/struct.h
@@ -1,24 +1,3 @@
-/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
- * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
- * 
- * @APPLE_LICENSE_HEADER_END@
- */
 /*-
  * Copyright (c) 1983, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -69,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_ */