]> git.ozlabs.org Git - ccan/blob - ccan/array_size/array_size.h
crypto/shachain/tools: update to new rbuf API.
[ccan] / ccan / array_size / array_size.h
1 /* CC0 (Public domain) - see LICENSE file for details */
2 #ifndef CCAN_ARRAY_SIZE_H
3 #define CCAN_ARRAY_SIZE_H
4 #include "config.h"
5 #include <ccan/build_assert/build_assert.h>
6
7 /**
8  * ARRAY_SIZE - get the number of elements in a visible array
9  * @arr: the array whose size you want.
10  *
11  * This does not work on pointers, or arrays declared as [], or
12  * function parameters.  With correct compiler support, such usage
13  * will cause a build error (see build_assert).
14  */
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
16
17 #if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
18 /* Two gcc extensions.
19  * &a[0] degrades to a pointer: a different type from an array */
20 #define _array_size_chk(arr)                                            \
21         BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
22                                                         typeof(&(arr)[0])))
23 #else
24 #define _array_size_chk(arr) 0
25 #endif
26 #endif /* CCAN_ALIGNOF_H */