array_size.h 829 B

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