Multiple definitions of ARRAY_SIZE(arr)
From: Alejandro Colomar
Date: Sun Sep 06 2020 - 07:08:47 EST
Hi all,
I was reading through kernel code, and found that there are many
definitions of `ARRAY_SIZE(arr)` all around the kernel.
Is there a reason for all of those to exist, or should they all just
include <linux/kernel.h>? If so, I would be happy to patch them.
Moreover, only the <linux/kernel.h> one is safe because of
`__must_be_array(arr)`.
Here's what I found:
$ grep -rn "define ARRAY_SIZE("
fs/orangefs/orangefs-debug.h:21:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]))
fs/unicode/mkutf8data.c:63:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
scripts/genksyms/genksyms.c:406:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]))
scripts/dtc/util.h:26:#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
scripts/mod/file2alias.c:713:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
scripts/kconfig/preprocess.c:15:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]))
scripts/kallsyms.c:28:#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
include/linux/kernel.h:47:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]) + __must_be_array(arr))
tools/gpio/gpio-utils.h:17:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]))
tools/iio/iio_utils.h:18:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof(arr[0]))
tools/virtio/linux/kernel.h:50:#define ARRAY_SIZE(x)
(sizeof(x)/sizeof(x[0]))
tools/lib/traceevent/plugins/plugin_xen.c:103:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/include/linux/kernel.h:107:#define ARRAY_SIZE(arr) (sizeof(arr) /
sizeof((arr)[0]) + __must_be_array(arr))
tools/power/cpupower/utils/cpupower.c:23:#define ARRAY_SIZE(x)
(sizeof(x)/sizeof(x[0]))
tools/usb/usbip/libsrc/usbip_device_driver.c:30:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/spi/spidev_test.c:26:#define ARRAY_SIZE(a) (sizeof(a) /
sizeof((a)[0]))
tools/testing/selftests/seccomp/seccomp_benchmark.c:17:#define
ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
tools/testing/selftests/sparc64/drivers/adi-test.c:28:# define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
tools/testing/selftests/net/reuseport_bpf.c:28:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/net/tcp_fastopen_backup_key.c:38:#define
ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/net/socket.c:34:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/net/rxtimestamp.c:21:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/kselftest_harness.h:590:#define ARRAY_SIZE(a)
(sizeof(a) / sizeof(a[0]))
tools/testing/selftests/timens/procfs.c:27:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/timens/timens.c:25:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/cgroup/cgroup_util.h:7:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/rseq/basic_percpu_ops_test.c:14:#define
ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/rseq/rseq.c:32:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/ir/ir_loopback.c:29:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/vm/va_128TBswitch.c:12:#define ARRAY_SIZE(arr)
(sizeof(arr) / sizeof((arr)[0]))
tools/testing/selftests/x86/protection_keys.c:54:#define ARRAY_SIZE(x)
(sizeof(x) / sizeof(*(x)))
tools/testing/selftests/bpf/progs/test_sysctl_loop1.c:13:#define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
tools/testing/selftests/bpf/progs/test_sysctl_loop2.c:13:#define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
tools/testing/selftests/bpf/progs/test_sysctl_prog.c:19:#define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
tools/testing/selftests/bpf/bpf_util.h:31:# define ARRAY_SIZE(x)
(sizeof(x) / sizeof((x)[0]))
tools/vm/page-types.c:209:#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
samples/seccomp/user-trap.c:24:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof(*(x)))
samples/mic/mpssd/mpssd.c:40:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
samples/bpf/cookie_uid_helper_example.c:34:#define ARRAY_SIZE(x)
(sizeof(x) / sizeof(*(x)))
arch/powerpc/boot/types.h:7:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
arch/mips/boot/tools/relocs.h:32:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
arch/um/include/shared/user.h:17:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
arch/x86/tools/insn_sanity.c:19:#define ARRAY_SIZE(a)
(sizeof(a)/sizeof(a[0]))
arch/x86/tools/relocs.h:22:#define ARRAY_SIZE(x) (sizeof(x) /
sizeof((x)[0]))
arch/x86/boot/boot.h:31:#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Documentation/process/coding-style.rst:985: #define ARRAY_SIZE(x)
(sizeof(x) / sizeof((x)[0]))
Documentation/translations/zh_CN/process/coding-style.rst:823: #define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Documentation/translations/it_IT/process/coding-style.rst:1001: #define
ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Alex