   1| #ifndef GIT_COMPAT_UTIL_H
   2| #define GIT_COMPAT_UTIL_H
   4| #if __STDC_VERSION__ - 0 < 199901L
  14| #error "Required C99 support is in a test phase.  Please see git-compat-util.h for more details."
  15| #endif
  17| #ifdef USE_MSVC_CRTDBG
  22| #include <stdlib.h>
  23| #include <crtdbg.h>
  24| #endif
  26| #include "compat/posix.h"
  28| struct strbuf;
  30| #if defined(__GNUC__) || defined(__clang__)
  31| #  define PRAGMA(pragma)           _Pragma(#pragma)
  32| #  define DISABLE_WARNING(warning) PRAGMA(GCC diagnostic ignored #warning)
  33| #else
  34| #  define DISABLE_WARNING(warning)
  35| #endif
  37| #undef FLEX_ARRAY
  38| #define FLEX_ARRAY /* empty - weather balloon to require C99 FAM */
  52| #define BUILD_ASSERT_OR_ZERO(cond) \
  53| 	(sizeof(char [1 - 2*!(cond)]) - 1)
  55| #if GIT_GNUC_PREREQ(3, 1)
  57| # define BARF_UNLESS_AN_ARRAY(arr)						\
  58| 	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \
  59| 							   __typeof__(&(arr)[0])))
  60| # define BARF_UNLESS_COPYABLE(dst, src) \
  61| 	BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \
  62| 							  __typeof__(*(src))))
  64| # define BARF_UNLESS_SIGNED(var)   BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) < 0)
  65| # define BARF_UNLESS_UNSIGNED(var) BUILD_ASSERT_OR_ZERO(((__typeof__(var)) -1) > 0)
  66| #else
  67| # define BARF_UNLESS_AN_ARRAY(arr) 0
  68| # define BARF_UNLESS_COPYABLE(dst, src) \
  69| 	BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
  70| 				 sizeof(*(dst)) == sizeof(*(src)))
  72| # define BARF_UNLESS_SIGNED(var)   0
  73| # define BARF_UNLESS_UNSIGNED(var) 0
  74| #endif
  84| #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
  86| #define bitsizeof(x)  (CHA

... [truncated 16861 chars] ...

 \
1105| 	(type *)container_of_or_null_offset(ptr, offsetof(type, member))
1113| #if defined(__GNUC__) /* clang sets this, too */
1114| #define OFFSETOF_VAR(ptr, member) offsetof(__typeof__(*ptr), member)
1115| #else /* !__GNUC__ */
1116| #define OFFSETOF_VAR(ptr, member) \
1117| 	((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
1118| #endif /* !__GNUC__ */
1127| #define NOT_CONSTANT(expr) ((expr) || false_but_the_compiler_does_not_know_it_)
1128| extern int false_but_the_compiler_does_not_know_it_;
1130| #ifdef CHECK_ASSERTION_SIDE_EFFECTS
1131| #undef assert
1132| extern int not_supposed_to_survive;
1133| #define assert(expr) ((void)(not_supposed_to_survive || (expr)))
1134| #endif /* CHECK_ASSERTION_SIDE_EFFECTS */
1136| #endif
1138| #ifdef DISABLE_SIGN_COMPARE_WARNINGS
1139| DISABLE_WARNING(-Wsign-compare)
1140| #endif