54 #include "pg_config.h"
56 #include "pg_config_os.h"
68 #include <sys/types.h>
70 #if defined(WIN32) || defined(__CYGWIN__)
96 #ifdef PG_FORCE_DISABLE_INLINE
117 #ifndef __has_attribute
118 #define __has_attribute(attribute) 0
123 #define pg_attribute_unused() __attribute__((unused))
125 #define pg_attribute_unused()
135 #define pg_nodiscard __attribute__((warn_unused_result))
146 #if __clang_major__ >= 7 || __GNUC__ >= 8
147 #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
149 #define pg_attribute_no_sanitize_alignment()
157 #if __has_attribute (nonnull)
158 #define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
160 #define pg_attribute_nonnull(...)
168 #ifdef USE_ASSERT_CHECKING
169 #define PG_USED_FOR_ASSERTS_ONLY
171 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
175 #if defined(__GNUC__) || defined(__IBMC__)
176 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
177 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
179 #define pg_attribute_format_arg(a)
180 #define pg_attribute_printf(f,a)
184 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
185 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
186 #define pg_attribute_noreturn() __attribute__((noreturn))
187 #define pg_attribute_packed() __attribute__((packed))
188 #define HAVE_PG_ATTRIBUTE_NORETURN 1
189 #elif defined(_MSC_VER)
198 #define pg_attribute_aligned(a) __declspec(align(a))
199 #define pg_attribute_noreturn()
206 #define pg_attribute_noreturn()
215 #if (defined(__GNUC__) && __GNUC__ > 3 && defined(__OPTIMIZE__)) || defined(__SUNPRO_C) || defined(__IBMC__)
217 #define pg_attribute_always_inline __attribute__((always_inline)) inline
218 #elif defined(_MSC_VER)
220 #define pg_attribute_always_inline __forceinline
223 #define pg_attribute_always_inline inline
233 #if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C) || defined(__IBMC__)
234 #define pg_noinline __attribute__((noinline))
236 #elif defined(_MSC_VER)
237 #define pg_noinline __declspec(noinline)
251 #if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
253 #define pg_attribute_cold
254 #define pg_attribute_hot
261 #if __has_attribute (cold)
262 #define pg_attribute_cold __attribute__((cold))
264 #define pg_attribute_cold
267 #if __has_attribute (hot)
268 #define pg_attribute_hot __attribute__((hot))
270 #define pg_attribute_hot
280 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
281 #define pg_unreachable() __builtin_unreachable()
282 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
283 #define pg_unreachable() __assume(0)
285 #define pg_unreachable() abort()
296 #define likely(x) __builtin_expect((x) != 0, 1)
297 #define unlikely(x) __builtin_expect((x) != 0, 0)
299 #define likely(x) ((x) != 0)
300 #define unlikely(x) ((x) != 0)
315 #define CppAsString(identifier) #identifier
316 #define CppAsString2(x) CppAsString(x)
317 #define CppConcat(x, y) x##y
339 #define EXPAND(args) args
340 #define VA_ARGS_NARGS(...) \
341 VA_ARGS_NARGS_ EXPAND((__VA_ARGS__, \
343 59,58,57,56,55,54,53,52,51,50, \
344 49,48,47,46,45,44,43,42,41,40, \
345 39,38,37,36,35,34,33,32,31,30, \
346 29,28,27,26,25,24,23,22,21,20, \
347 19,18,17,16,15,14,13,12,11,10, \
348 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
351 #define VA_ARGS_NARGS(...) \
352 VA_ARGS_NARGS_(__VA_ARGS__, \
354 59,58,57,56,55,54,53,52,51,50, \
355 49,48,47,46,45,44,43,42,41,40, \
356 39,38,37,36,35,34,33,32,31,30, \
357 29,28,27,26,25,24,23,22,21,20, \
358 19,18,17,16,15,14,13,12,11,10, \
359 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
362 #define VA_ARGS_NARGS_( \
363 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
364 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
365 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
366 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
367 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
368 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
369 _61,_62,_63, N, ...) \
387 #define FLEXIBLE_ARRAY_MEMBER
414 #define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
440 #ifdef PG_USE_STDBOOL
449 #define true ((bool) 1)
453 #define false ((bool) 0)
509 #ifdef HAVE_LONG_INT_64
513 typedef long int int64;
516 typedef unsigned long int uint64;
518 #define INT64CONST(x) (x##L)
519 #define UINT64CONST(x) (x##UL)
520 #elif defined(HAVE_LONG_LONG_INT_64)
524 typedef long long int int64;
527 typedef unsigned long long int uint64;
529 #define INT64CONST(x) (x##LL)
530 #define UINT64CONST(x) (x##ULL)
533 #error must have a working 64-bit integer datatype
537 #define INT64_FORMAT "%" INT64_MODIFIER "d"
538 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
548 #if defined(PG_INT128_TYPE)
549 #if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
550 #define HAVE_INT128 1
552 typedef PG_INT128_TYPE int128
553 #if defined(pg_attribute_aligned)
554 pg_attribute_aligned(MAXIMUM_ALIGNOF)
558 typedef unsigned PG_INT128_TYPE uint128
559 #if defined(pg_attribute_aligned)
560 pg_attribute_aligned(MAXIMUM_ALIGNOF)
571 #define PG_INT8_MIN (-0x7F-1)
572 #define PG_INT8_MAX (0x7F)
573 #define PG_UINT8_MAX (0xFF)
574 #define PG_INT16_MIN (-0x7FFF-1)
575 #define PG_INT16_MAX (0x7FFF)
576 #define PG_UINT16_MAX (0xFFFF)
577 #define PG_INT32_MIN (-0x7FFFFFFF-1)
578 #define PG_INT32_MAX (0x7FFFFFFF)
579 #define PG_UINT32_MAX (0xFFFFFFFFU)
580 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
581 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
582 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
588 #define HAVE_INT64_TIMESTAMP
621 #ifdef USE_FLOAT8_BYVAL
622 #define FLOAT8PASSBYVAL true
624 #define FLOAT8PASSBYVAL false
647 #define InvalidSubTransactionId ((SubTransactionId) 0)
648 #define TopSubTransactionId ((SubTransactionId) 1)
657 #define FirstCommandId ((CommandId) 0)
658 #define InvalidCommandId (~(CommandId)0)
681 #define VARHDRSZ ((int32) sizeof(int32))
735 #define NameStr(name) ((name).data)
746 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
752 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
758 #define PointerIsAligned(pointer, type) \
759 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
761 #define OffsetToPointer(base, offset) \
762 ((void *)((char *) base + offset))
764 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
766 #define RegProcedureIsValid(p) OidIsValid(p)
777 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
793 #define TYPEALIGN(ALIGNVAL,LEN) \
794 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
796 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
797 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
798 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
799 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
800 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
802 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
803 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
805 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
806 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
808 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
809 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
810 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
811 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
812 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
813 #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
821 #define TYPEALIGN64(ALIGNVAL,LEN) \
822 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
825 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
845 #ifndef USE_ASSERT_CHECKING
847 #define Assert(condition) ((void)true)
848 #define AssertMacro(condition) ((void)true)
850 #elif defined(FRONTEND)
853 #define Assert(p) assert(p)
854 #define AssertMacro(p) ((void) assert(p))
862 #define Assert(condition) \
865 ExceptionalCondition(#condition, __FILE__, __LINE__); \
874 #define AssertMacro(condition) \
875 ((void) ((condition) || \
876 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
883 #define AssertPointerAlignment(ptr, bndr) \
884 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
917 #ifdef HAVE__STATIC_ASSERT
918 #define StaticAssertDecl(condition, errmessage) \
919 _Static_assert(condition, errmessage)
920 #define StaticAssertStmt(condition, errmessage) \
921 do { _Static_assert(condition, errmessage); } while(0)
922 #define StaticAssertExpr(condition, errmessage) \
923 ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
925 #define StaticAssertDecl(condition, errmessage) \
926 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
927 #define StaticAssertStmt(condition, errmessage) \
928 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
929 #define StaticAssertExpr(condition, errmessage) \
930 StaticAssertStmt(condition, errmessage)
933 #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
934 #define StaticAssertDecl(condition, errmessage) \
935 static_assert(condition, errmessage)
936 #define StaticAssertStmt(condition, errmessage) \
937 static_assert(condition, errmessage)
938 #define StaticAssertExpr(condition, errmessage) \
939 ({ static_assert(condition, errmessage); })
941 #define StaticAssertDecl(condition, errmessage) \
942 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
943 #define StaticAssertStmt(condition, errmessage) \
944 do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
945 #define StaticAssertExpr(condition, errmessage) \
946 ((void) ({ StaticAssertStmt(condition, errmessage); }))
962 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
963 #define AssertVariableIsOfType(varname, typename) \
964 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
965 CppAsString(varname) " does not have type " CppAsString(typename))
966 #define AssertVariableIsOfTypeMacro(varname, typename) \
967 (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
968 CppAsString(varname) " does not have type " CppAsString(typename)))
970 #define AssertVariableIsOfType(varname, typename) \
971 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
972 CppAsString(varname) " does not have type " CppAsString(typename))
973 #define AssertVariableIsOfTypeMacro(varname, typename) \
974 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
975 CppAsString(varname) " does not have type " CppAsString(typename)))
987 #define Max(x, y) ((x) > (y) ? (x) : (y))
993 #define Min(x, y) ((x) < (y) ? (x) : (y))
997 #define LONG_ALIGN_MASK (sizeof(long) - 1)
1009 #define MemSet(start, val, len) \
1013 void *_vstart = (void *) (start); \
1015 Size _len = (len); \
1017 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
1018 (_len & LONG_ALIGN_MASK) == 0 && \
1020 _len <= MEMSET_LOOP_LIMIT && \
1025 MEMSET_LOOP_LIMIT != 0) \
1027 long *_start = (long *) _vstart; \
1028 long *_stop = (long *) ((char *) _start + _len); \
1029 while (_start < _stop) \
1033 memset(_vstart, _val, _len); \
1042 #define MemSetAligned(start, val, len) \
1045 long *_start = (long *) (start); \
1047 Size _len = (len); \
1049 if ((_len & LONG_ALIGN_MASK) == 0 && \
1051 _len <= MEMSET_LOOP_LIMIT && \
1052 MEMSET_LOOP_LIMIT != 0) \
1054 long *_stop = (long *) ((char *) _start + _len); \
1055 while (_start < _stop) \
1059 memset(_start, _val, _len); \
1071 #define MemSetTest(val, len) \
1072 ( ((len) & LONG_ALIGN_MASK) == 0 && \
1073 (len) <= MEMSET_LOOP_LIMIT && \
1074 MEMSET_LOOP_LIMIT != 0 && \
1077 #define MemSetLoop(start, val, len) \
1080 long * _start = (long *) (start); \
1081 long * _stop = (long *) ((char *) _start + (Size) (len)); \
1083 while (_start < _stop) \
1098 #define FLOAT4_FITS_IN_INT16(num) \
1099 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1100 #define FLOAT4_FITS_IN_INT32(num) \
1101 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1102 #define FLOAT4_FITS_IN_INT64(num) \
1103 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1104 #define FLOAT8_FITS_IN_INT16(num) \
1105 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1106 #define FLOAT8_FITS_IN_INT32(num) \
1107 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1108 #define FLOAT8_FITS_IN_INT64(num) \
1109 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1122 #define INVERT_COMPARE_RESULT(var) \
1123 ((var) = ((var) < 0) ? 1 : -(var))
1150 #ifdef pg_attribute_aligned
1161 #ifdef pg_attribute_aligned
1170 #define HIGHBIT (0x80)
1171 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1179 #define SQL_STR_DOUBLE(ch, escape_backslash) \
1180 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1182 #define ESCAPE_STRING_SYNTAX 'E'
1185 #define STATUS_OK (0)
1186 #define STATUS_ERROR (-1)
1187 #define STATUS_EOF (-2)
1195 #define gettext(x) (x)
1196 #define dgettext(d,x) (x)
1197 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1198 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1201 #define _(x) gettext(x)
1212 #define gettext_noop(x) (x)
1227 #ifdef SO_MAJOR_VERSION
1228 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1230 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1248 #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
1249 #define unconstify(underlying_type, expr) \
1250 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
1252 (underlying_type) (expr))
1253 #define unvolatize(underlying_type, expr) \
1254 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1256 (underlying_type) (expr))
1258 #define unconstify(underlying_type, expr) \
1259 ((underlying_type) (expr))
1260 #define unvolatize(underlying_type, expr) \
1261 ((underlying_type) (expr))
1280 #if defined(WIN32) || defined(__CYGWIN__)
1281 #define PG_BINARY O_BINARY
1282 #define PG_BINARY_A "ab"
1283 #define PG_BINARY_R "rb"
1284 #define PG_BINARY_W "wb"
1287 #define PG_BINARY_A "a"
1288 #define PG_BINARY_R "r"
1289 #define PG_BINARY_W "w"
1297 #if !HAVE_DECL_FDATASYNC
1306 #ifdef HAVE_LONG_INT_64
1307 #define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1308 #define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1310 #define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1311 #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1317 #ifdef HAVE_LONG_INT_64
1318 #define i64abs(i) labs(i)
1320 #define i64abs(i) llabs(i)
1341 #ifdef HAVE_VISIBILITY_ATTRIBUTE
1342 #define PGDLLEXPORT __attribute__((visibility("default")))
1358 #define SIGNAL_ARGS int postgres_signal_arg
1369 typedef intptr_t sigjmp_buf[5];
1370 #define sigsetjmp(x,y) __builtin_setjmp(x)
1371 #define siglongjmp __builtin_longjmp
1373 #define sigjmp_buf jmp_buf
1374 #define sigsetjmp(x,y) setjmp(x)
1375 #define siglongjmp longjmp
static Datum values[MAXATTR]
union PGAlignedBlock PGAlignedBlock
union PGAlignedXLogBlock PGAlignedXLogBlock
#define pg_attribute_noreturn()
TransactionId MultiXactId
#define FLEXIBLE_ARRAY_MEMBER
uint32 LocalTransactionId
union PGIOAlignedBlock PGIOAlignedBlock
int fdatasync(int fildes)
void ExceptionalCondition(const char *conditionName, const char *fileName, int lineNumber) pg_attribute_noreturn()
void(* pg_funcptr_t)(void)
char vl_dat[FLEXIBLE_ARRAY_MEMBER]