summaryrefslogtreecommitdiff
path: root/iputils_common.h
diff options
context:
space:
mode:
authorSami Kerola <kerolasa@iki.fi>2019-03-23 18:16:59 +0000
committerSami Kerola <kerolasa@iki.fi>2019-03-23 18:16:59 +0000
commit774f80f4539679a4be785809a8383cc6ccd3148e (patch)
tree67b2fc843c3d3474dbb501b2b723ec73d5096256 /iputils_common.h
parent9c581b7251a695bfc592443feff9a08a4df8d759 (diff)
downloadiputils-774f80f4539679a4be785809a8383cc6ccd3148e.tar.gz
common: use single ARRAY_SIZE definition across files
The ARRAY_SIZE is Rusty Russell’s version, that looks a bit more complicated than the original, but there is a reason to that. This macro will ensure macro is used properly, and fail compilation if it is not. See reference link for further explanation. Reference: http://zubplot.blogspot.com/2015/01/gcc-is-wonderful-better-arraysize-macro.html Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'iputils_common.h')
-rw-r--r--iputils_common.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/iputils_common.h b/iputils_common.h
index 61628ac..977365a 100644
--- a/iputils_common.h
+++ b/iputils_common.h
@@ -1,6 +1,14 @@
#ifndef IPUTILS_COMMON_H
#define IPUTILS_COMMON_H
+#include <stdio.h>
+
+#define ARRAY_SIZE(arr) \
+ (sizeof(arr) / sizeof((arr)[0]) + \
+ sizeof(__typeof__(int[1 - 2 * \
+ !!__builtin_types_compatible_p(__typeof__(arr), \
+ __typeof__(&arr[0]))])) * 0)
+
#if defined(USE_IDN) || defined(ENABLE_NLS)
# include <locale.h>
#endif