summaryrefslogtreecommitdiff
path: root/ccan/array_size/array_size.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2016-03-11 21:18:42 -0900
committerKent Overstreet <kent.overstreet@gmail.com>2016-03-11 21:18:42 -0900
commit009d6db7b01462a264a8fb15477611bc2d8674eb (patch)
tree48bc88526b34c352797711dd405616bd2ca60051 /ccan/array_size/array_size.h
parent074caf716a816d3c4c62bce28503e512d0ef4169 (diff)
Redo lots of stuff
Diffstat (limited to 'ccan/array_size/array_size.h')
-rw-r--r--ccan/array_size/array_size.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/ccan/array_size/array_size.h b/ccan/array_size/array_size.h
new file mode 100644
index 00000000..0ca422a2
--- /dev/null
+++ b/ccan/array_size/array_size.h
@@ -0,0 +1,26 @@
+/* CC0 (Public domain) - see LICENSE file for details */
+#ifndef CCAN_ARRAY_SIZE_H
+#define CCAN_ARRAY_SIZE_H
+#include "config.h"
+#include <ccan/build_assert/build_assert.h>
+
+/**
+ * ARRAY_SIZE - get the number of elements in a visible array
+ * @arr: the array whose size you want.
+ *
+ * This does not work on pointers, or arrays declared as [], or
+ * function parameters. With correct compiler support, such usage
+ * will cause a build error (see build_assert).
+ */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
+
+#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
+/* Two gcc extensions.
+ * &a[0] degrades to a pointer: a different type from an array */
+#define _array_size_chk(arr) \
+ BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
+ typeof(&(arr)[0])))
+#else
+#define _array_size_chk(arr) 0
+#endif
+#endif /* CCAN_ALIGNOF_H */