X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Farray_size%2Farray_size.h;fp=ccan%2Farray_size%2Farray_size.h;h=c561c0ac9c674ca393405ab888b941c0136ca63a;hb=c6a86ac78efbec284b444876a9ea2fbbf7e48033;hp=0000000000000000000000000000000000000000;hpb=456d13116b61c1a9fa2ace8816dbbe9b759ac9e2;p=ccan diff --git a/ccan/array_size/array_size.h b/ccan/array_size/array_size.h new file mode 100644 index 00000000..c561c0ac --- /dev/null +++ b/ccan/array_size/array_size.h @@ -0,0 +1,25 @@ +#ifndef CCAN_ARRAY_SIZE_H +#define CCAN_ARRAY_SIZE_H +#include "config.h" +#include + +/** + * 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) \ + EXPR_BUILD_ASSERT(!__builtin_types_compatible_p(typeof(arr), \ + typeof(&(arr)[0]))) +#else +#define _array_size_chk(arr) 0 +#endif +#endif /* CCAN_ALIGNOF_H */