pymacro.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef Py_PYMACRO_H
  2. #define Py_PYMACRO_H
  3. /* Assert a build-time dependency, as an expression.
  4. Your compile will fail if the condition isn't true, or can't be evaluated
  5. by the compiler. This can be used in an expression: its value is 0.
  6. Example:
  7. #define foo_to_char(foo) \
  8. ((char *)(foo) \
  9. + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))
  10. Written by Rusty Russell, public domain, http://ccodearchive.net/ */
  11. #define Py_BUILD_ASSERT_EXPR(cond) \
  12. (sizeof(char [1 - 2*!(cond)]) - 1)
  13. /* Get the number of elements in a visible array
  14. This does not work on pointers, or arrays declared as [], or function
  15. parameters. With correct compiler support, such usage will cause a build
  16. error (see Py_BUILD_ASSERT_EXPR).
  17. Written by Rusty Russell, public domain, http://ccodearchive.net/
  18. Requires at GCC 3.1+ */
  19. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__) && \
  20. (((__GNUC__ == 3) && (__GNU_MINOR__ >= 1)) || (__GNUC__ >= 4)))
  21. /* Two gcc extensions.
  22. &a[0] degrades to a pointer: a different type from an array */
  23. #define Py_ARRAY_LENGTH(array) \
  24. (sizeof(array) / sizeof((array)[0]) \
  25. + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
  26. typeof(&(array)[0]))))
  27. #else
  28. #define Py_ARRAY_LENGTH(array) \
  29. (sizeof(array) / sizeof((array)[0]))
  30. #endif
  31. #endif /* Py_PYMACRO_H */