54 #include "pg_config.h"
56 #include "pg_config_os.h"
68 #include <sys/types.h>
70 #if defined(WIN32) || defined(__CYGWIN__)
91 #ifdef PG_FORCE_DISABLE_INLINE
112 #ifndef __has_attribute
113 #define __has_attribute(attribute) 0
118 #define pg_attribute_unused() __attribute__((unused))
120 #define pg_attribute_unused()
130 #define pg_nodiscard __attribute__((warn_unused_result))
141 #if __clang_major__ >= 7 || __GNUC__ >= 8
142 #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
144 #define pg_attribute_no_sanitize_alignment()
152 #if __has_attribute (nonnull)
153 #define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
155 #define pg_attribute_nonnull(...)
163 #ifdef USE_ASSERT_CHECKING
164 #define PG_USED_FOR_ASSERTS_ONLY
166 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
170 #if defined(__GNUC__) || defined(__IBMC__)
171 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
172 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
174 #define pg_attribute_format_arg(a)
175 #define pg_attribute_printf(f,a)
179 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
180 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
181 #define pg_attribute_noreturn() __attribute__((noreturn))
182 #define pg_attribute_packed() __attribute__((packed))
183 #define HAVE_PG_ATTRIBUTE_NORETURN 1
190 #define pg_attribute_noreturn()
199 #if (defined(__GNUC__) && __GNUC__ > 3 && defined(__OPTIMIZE__)) || defined(__SUNPRO_C) || defined(__IBMC__)
201 #define pg_attribute_always_inline __attribute__((always_inline)) inline
202 #elif defined(_MSC_VER)
204 #define pg_attribute_always_inline __forceinline
207 #define pg_attribute_always_inline inline
217 #if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
218 #define pg_noinline __attribute__((noinline))
220 #elif defined(_MSC_VER)
221 #define pg_noinline __declspec(noinline)
235 #if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
237 #define pg_attribute_cold
238 #define pg_attribute_hot
245 #if __has_attribute (cold)
246 #define pg_attribute_cold __attribute__((cold))
248 #define pg_attribute_cold
251 #if __has_attribute (hot)
252 #define pg_attribute_hot __attribute__((hot))
254 #define pg_attribute_hot
264 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
265 #define pg_unreachable() __builtin_unreachable()
266 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
267 #define pg_unreachable() __assume(0)
269 #define pg_unreachable() abort()
280 #define likely(x) __builtin_expect((x) != 0, 1)
281 #define unlikely(x) __builtin_expect((x) != 0, 0)
283 #define likely(x) ((x) != 0)
284 #define unlikely(x) ((x) != 0)
299 #define CppAsString(identifier) #identifier
300 #define CppAsString2(x) CppAsString(x)
301 #define CppConcat(x, y) x##y
317 #define VA_ARGS_NARGS(...) \
318 VA_ARGS_NARGS_(__VA_ARGS__, \
320 59,58,57,56,55,54,53,52,51,50, \
321 49,48,47,46,45,44,43,42,41,40, \
322 39,38,37,36,35,34,33,32,31,30, \
323 29,28,27,26,25,24,23,22,21,20, \
324 19,18,17,16,15,14,13,12,11,10, \
325 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
326 #define VA_ARGS_NARGS_( \
327 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
328 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
329 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
330 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
331 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
332 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
333 _61,_62,_63, N, ...) \
341 #define dummyret void
343 #define dummyret char
361 #define FLEXIBLE_ARRAY_MEMBER
364 #ifdef HAVE_FUNCNAME__FUNC
365 #define PG_FUNCNAME_MACRO __func__
367 #ifdef HAVE_FUNCNAME__FUNCTION
368 #define PG_FUNCNAME_MACRO __FUNCTION__
370 #define PG_FUNCNAME_MACRO NULL
397 #ifdef PG_USE_STDBOOL
406 #define true ((bool) 1)
410 #define false ((bool) 0)
466 #ifdef HAVE_LONG_INT_64
470 typedef long int int64;
473 typedef unsigned long int uint64;
475 #define INT64CONST(x) (x##L)
476 #define UINT64CONST(x) (x##UL)
477 #elif defined(HAVE_LONG_LONG_INT_64)
481 typedef long long int int64;
484 typedef unsigned long long int uint64;
486 #define INT64CONST(x) (x##LL)
487 #define UINT64CONST(x) (x##ULL)
490 #error must have a working 64-bit integer datatype
494 #define INT64_FORMAT "%" INT64_MODIFIER "d"
495 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
505 #if defined(PG_INT128_TYPE)
506 #if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
507 #define HAVE_INT128 1
509 typedef PG_INT128_TYPE int128
510 #if defined(pg_attribute_aligned)
511 pg_attribute_aligned(MAXIMUM_ALIGNOF)
515 typedef unsigned PG_INT128_TYPE uint128
516 #if defined(pg_attribute_aligned)
517 pg_attribute_aligned(MAXIMUM_ALIGNOF)
528 #define PG_INT8_MIN (-0x7F-1)
529 #define PG_INT8_MAX (0x7F)
530 #define PG_UINT8_MAX (0xFF)
531 #define PG_INT16_MIN (-0x7FFF-1)
532 #define PG_INT16_MAX (0x7FFF)
533 #define PG_UINT16_MAX (0xFFFF)
534 #define PG_INT32_MIN (-0x7FFFFFFF-1)
535 #define PG_INT32_MAX (0x7FFFFFFF)
536 #define PG_UINT32_MAX (0xFFFFFFFFU)
537 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
538 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
539 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
545 #define HAVE_INT64_TIMESTAMP
578 #ifdef USE_FLOAT8_BYVAL
579 #define FLOAT8PASSBYVAL true
581 #define FLOAT8PASSBYVAL false
604 #define InvalidSubTransactionId ((SubTransactionId) 0)
605 #define TopSubTransactionId ((SubTransactionId) 1)
614 #define FirstCommandId ((CommandId) 0)
615 #define InvalidCommandId (~(CommandId)0)
638 #define VARHDRSZ ((int32) sizeof(int32))
692 #define NameStr(name) ((name).data)
703 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
709 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
715 #define PointerIsAligned(pointer, type) \
716 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
718 #define OffsetToPointer(base, offset) \
719 ((void *)((char *) base + offset))
721 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
723 #define RegProcedureIsValid(p) OidIsValid(p)
738 #define offsetof(type, field) ((long) &((type *)0)->field)
745 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
761 #define TYPEALIGN(ALIGNVAL,LEN) \
762 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
764 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
765 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
766 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
767 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
768 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
770 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
771 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
773 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
774 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
776 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
777 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
778 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
779 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
780 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
781 #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
789 #define TYPEALIGN64(ALIGNVAL,LEN) \
790 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
793 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
813 #ifndef USE_ASSERT_CHECKING
815 #define Assert(condition) ((void)true)
816 #define AssertMacro(condition) ((void)true)
817 #define AssertArg(condition) ((void)true)
818 #define AssertState(condition) ((void)true)
819 #define AssertPointerAlignment(ptr, bndr) ((void)true)
820 #define Trap(condition, errorType) ((void)true)
821 #define TrapMacro(condition, errorType) (true)
823 #elif defined(FRONTEND)
826 #define Assert(p) assert(p)
827 #define AssertMacro(p) ((void) assert(p))
828 #define AssertArg(condition) assert(condition)
829 #define AssertState(condition) assert(condition)
830 #define AssertPointerAlignment(ptr, bndr) ((void)true)
838 #define Trap(condition, errorType) \
841 ExceptionalCondition(#condition, (errorType), \
842 __FILE__, __LINE__); \
852 #define TrapMacro(condition, errorType) \
853 ((bool) (! (condition) || \
854 (ExceptionalCondition(#condition, (errorType), \
855 __FILE__, __LINE__), 0)))
857 #define Assert(condition) \
860 ExceptionalCondition(#condition, "FailedAssertion", \
861 __FILE__, __LINE__); \
864 #define AssertMacro(condition) \
865 ((void) ((condition) || \
866 (ExceptionalCondition(#condition, "FailedAssertion", \
867 __FILE__, __LINE__), 0)))
869 #define AssertArg(condition) \
872 ExceptionalCondition(#condition, "BadArgument", \
873 __FILE__, __LINE__); \
876 #define AssertState(condition) \
879 ExceptionalCondition(#condition, "BadState", \
880 __FILE__, __LINE__); \
886 #define AssertPointerAlignment(ptr, bndr) \
887 Trap(TYPEALIGN(bndr, (uintptr_t)(ptr)) != (uintptr_t)(ptr), \
900 const char *errorType,
921 #ifdef HAVE__STATIC_ASSERT
922 #define StaticAssertStmt(condition, errmessage) \
923 do { _Static_assert(condition, errmessage); } while(0)
924 #define StaticAssertExpr(condition, errmessage) \
925 ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
926 #define StaticAssertDecl(condition, errmessage) \
927 _Static_assert(condition, errmessage)
929 #define StaticAssertStmt(condition, errmessage) \
930 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
931 #define StaticAssertExpr(condition, errmessage) \
932 StaticAssertStmt(condition, errmessage)
933 #define StaticAssertDecl(condition, errmessage) \
934 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
937 #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
938 #define StaticAssertStmt(condition, errmessage) \
939 static_assert(condition, errmessage)
940 #define StaticAssertExpr(condition, errmessage) \
941 ({ static_assert(condition, errmessage); })
942 #define StaticAssertDecl(condition, errmessage) \
943 static_assert(condition, errmessage)
945 #define StaticAssertStmt(condition, errmessage) \
946 do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
947 #define StaticAssertExpr(condition, errmessage) \
948 ((void) ({ StaticAssertStmt(condition, errmessage); }))
949 #define StaticAssertDecl(condition, errmessage) \
950 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
966 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
967 #define AssertVariableIsOfType(varname, typename) \
968 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
969 CppAsString(varname) " does not have type " CppAsString(typename))
970 #define AssertVariableIsOfTypeMacro(varname, typename) \
971 (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
972 CppAsString(varname) " does not have type " CppAsString(typename)))
974 #define AssertVariableIsOfType(varname, typename) \
975 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
976 CppAsString(varname) " does not have type " CppAsString(typename))
977 #define AssertVariableIsOfTypeMacro(varname, typename) \
978 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
979 CppAsString(varname) " does not have type " CppAsString(typename)))
991 #define Max(x, y) ((x) > (y) ? (x) : (y))
997 #define Min(x, y) ((x) < (y) ? (x) : (y))
1003 #define Abs(x) ((x) >= 0 ? (x) : -(x))
1007 #define LONG_ALIGN_MASK (sizeof(long) - 1)
1019 #define MemSet(start, val, len) \
1023 void *_vstart = (void *) (start); \
1025 Size _len = (len); \
1027 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
1028 (_len & LONG_ALIGN_MASK) == 0 && \
1030 _len <= MEMSET_LOOP_LIMIT && \
1035 MEMSET_LOOP_LIMIT != 0) \
1037 long *_start = (long *) _vstart; \
1038 long *_stop = (long *) ((char *) _start + _len); \
1039 while (_start < _stop) \
1043 memset(_vstart, _val, _len); \
1052 #define MemSetAligned(start, val, len) \
1055 long *_start = (long *) (start); \
1057 Size _len = (len); \
1059 if ((_len & LONG_ALIGN_MASK) == 0 && \
1061 _len <= MEMSET_LOOP_LIMIT && \
1062 MEMSET_LOOP_LIMIT != 0) \
1064 long *_stop = (long *) ((char *) _start + _len); \
1065 while (_start < _stop) \
1069 memset(_start, _val, _len); \
1081 #define MemSetTest(val, len) \
1082 ( ((len) & LONG_ALIGN_MASK) == 0 && \
1083 (len) <= MEMSET_LOOP_LIMIT && \
1084 MEMSET_LOOP_LIMIT != 0 && \
1087 #define MemSetLoop(start, val, len) \
1090 long * _start = (long *) (start); \
1091 long * _stop = (long *) ((char *) _start + (Size) (len)); \
1093 while (_start < _stop) \
1108 #define FLOAT4_FITS_IN_INT16(num) \
1109 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1110 #define FLOAT4_FITS_IN_INT32(num) \
1111 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1112 #define FLOAT4_FITS_IN_INT64(num) \
1113 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1114 #define FLOAT8_FITS_IN_INT16(num) \
1115 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1116 #define FLOAT8_FITS_IN_INT32(num) \
1117 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1118 #define FLOAT8_FITS_IN_INT64(num) \
1119 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1127 #ifdef HAVE_STRUCT_SOCKADDR_UN
1128 #define HAVE_UNIX_SOCKETS 1
1136 #define INVERT_COMPARE_RESULT(var) \
1137 ((var) = ((var) < 0) ? 1 : -(var))
1166 #define HIGHBIT (0x80)
1167 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1175 #define SQL_STR_DOUBLE(ch, escape_backslash) \
1176 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1178 #define ESCAPE_STRING_SYNTAX 'E'
1181 #define STATUS_OK (0)
1182 #define STATUS_ERROR (-1)
1183 #define STATUS_EOF (-2)
1191 #define gettext(x) (x)
1192 #define dgettext(d,x) (x)
1193 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1194 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1197 #define _(x) gettext(x)
1208 #define gettext_noop(x) (x)
1223 #ifdef SO_MAJOR_VERSION
1224 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1226 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1244 #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
1245 #define unconstify(underlying_type, expr) \
1246 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
1248 (underlying_type) (expr))
1249 #define unvolatize(underlying_type, expr) \
1250 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1252 (underlying_type) (expr))
1254 #define unconstify(underlying_type, expr) \
1255 ((underlying_type) (expr))
1256 #define unvolatize(underlying_type, expr) \
1257 ((underlying_type) (expr))
1276 #if defined(WIN32) || defined(__CYGWIN__)
1277 #define PG_BINARY O_BINARY
1278 #define PG_BINARY_A "ab"
1279 #define PG_BINARY_R "rb"
1280 #define PG_BINARY_W "wb"
1283 #define PG_BINARY_A "a"
1284 #define PG_BINARY_R "r"
1285 #define PG_BINARY_W "w"
1293 #if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
1294 extern int fdatasync(
int fildes);
1298 #if !defined(HAVE_STRTOLL) && defined(HAVE___STRTOLL)
1299 #define strtoll __strtoll
1300 #define HAVE_STRTOLL 1
1303 #if !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ)
1304 #define strtoll strtoq
1305 #define HAVE_STRTOLL 1
1308 #if !defined(HAVE_STRTOULL) && defined(HAVE___STRTOULL)
1309 #define strtoull __strtoull
1310 #define HAVE_STRTOULL 1
1313 #if !defined(HAVE_STRTOULL) && defined(HAVE_STRTOUQ)
1314 #define strtoull strtouq
1315 #define HAVE_STRTOULL 1
1318 #if defined(HAVE_STRTOLL) && !HAVE_DECL_STRTOLL
1319 extern long long strtoll(
const char *
str,
char **endptr,
int base);
1322 #if defined(HAVE_STRTOULL) && !HAVE_DECL_STRTOULL
1323 extern unsigned long long strtoull(
const char *
str,
char **endptr,
int base);
1331 #ifdef HAVE_LONG_INT_64
1332 #define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1333 #define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1335 #define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1336 #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1369 #define SIGNAL_ARGS int postgres_signal_arg
1380 typedef intptr_t sigjmp_buf[5];
1381 #define sigsetjmp(x,y) __builtin_setjmp(x)
1382 #define siglongjmp __builtin_longjmp
1384 #define sigjmp_buf jmp_buf
1385 #define sigsetjmp(x,y) setjmp(x)
1386 #define siglongjmp longjmp
1392 #define NON_EXEC_STATIC
1394 #define NON_EXEC_STATIC static
static Datum values[MAXATTR]
union PGAlignedBlock PGAlignedBlock
union PGAlignedXLogBlock PGAlignedXLogBlock
void ExceptionalCondition(const char *conditionName, const char *errorType, const char *fileName, int lineNumber) pg_attribute_noreturn()
#define pg_attribute_noreturn()
TransactionId MultiXactId
#define FLEXIBLE_ARRAY_MEMBER
uint32 LocalTransactionId
void(* pg_funcptr_t)(void)
char vl_dat[FLEXIBLE_ARRAY_MEMBER]