54 #include "pg_config.h"
56 #include "pg_config_os.h"
68 #include <sys/types.h>
70 #if defined(WIN32) || defined(__CYGWIN__)
99 #ifdef PG_FORCE_DISABLE_INLINE
118 #ifndef __has_attribute
119 #define __has_attribute(attribute) 0
124 #define pg_attribute_unused() __attribute__((unused))
126 #define pg_attribute_unused()
136 #define pg_nodiscard __attribute__((warn_unused_result))
145 #if defined(__clang__) || __GNUC__ >= 8
146 #define pg_attribute_no_sanitize_address() __attribute__((no_sanitize("address")))
147 #elif __has_attribute(no_sanitize_address)
149 #define pg_attribute_no_sanitize_address() __attribute__((no_sanitize_address))
151 #define pg_attribute_no_sanitize_address()
160 #if __clang_major__ >= 7 || __GNUC__ >= 8
161 #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
163 #define pg_attribute_no_sanitize_alignment()
171 #if __has_attribute (nonnull)
172 #define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
174 #define pg_attribute_nonnull(...)
183 #if __has_attribute (target)
184 #define pg_attribute_target(...) __attribute__((target(__VA_ARGS__)))
186 #define pg_attribute_target(...)
194 #ifdef USE_ASSERT_CHECKING
195 #define PG_USED_FOR_ASSERTS_ONLY
197 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
201 #if defined(__GNUC__)
202 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
203 #define pg_attribute_printf(f,a) __attribute__((format(PG_PRINTF_ATTRIBUTE, f, a)))
205 #define pg_attribute_format_arg(a)
206 #define pg_attribute_printf(f,a)
210 #if defined(__GNUC__) || defined(__SUNPRO_C)
211 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
212 #define pg_attribute_noreturn() __attribute__((noreturn))
213 #define pg_attribute_packed() __attribute__((packed))
214 #define HAVE_PG_ATTRIBUTE_NORETURN 1
215 #elif defined(_MSC_VER)
224 #define pg_attribute_aligned(a) __declspec(align(a))
225 #define pg_attribute_noreturn()
232 #define pg_attribute_noreturn()
241 #if (defined(__GNUC__) && __GNUC__ > 3 && defined(__OPTIMIZE__)) || defined(__SUNPRO_C)
243 #define pg_attribute_always_inline __attribute__((always_inline)) inline
244 #elif defined(_MSC_VER)
246 #define pg_attribute_always_inline __forceinline
249 #define pg_attribute_always_inline inline
259 #if (defined(__GNUC__) && __GNUC__ > 2) || defined(__SUNPRO_C)
260 #define pg_noinline __attribute__((noinline))
262 #elif defined(_MSC_VER)
263 #define pg_noinline __declspec(noinline)
277 #if defined(__MINGW64__) && __GNUC__ == 8 && __GNUC_MINOR__ == 1
279 #define pg_attribute_cold
280 #define pg_attribute_hot
287 #if __has_attribute (cold)
288 #define pg_attribute_cold __attribute__((cold))
290 #define pg_attribute_cold
293 #if __has_attribute (hot)
294 #define pg_attribute_hot __attribute__((hot))
296 #define pg_attribute_hot
306 #if defined(HAVE__BUILTIN_UNREACHABLE) && !defined(USE_ASSERT_CHECKING)
307 #define pg_unreachable() __builtin_unreachable()
308 #elif defined(_MSC_VER) && !defined(USE_ASSERT_CHECKING)
309 #define pg_unreachable() __assume(0)
311 #define pg_unreachable() abort()
322 #define likely(x) __builtin_expect((x) != 0, 1)
323 #define unlikely(x) __builtin_expect((x) != 0, 0)
325 #define likely(x) ((x) != 0)
326 #define unlikely(x) ((x) != 0)
341 #define CppAsString(identifier) #identifier
342 #define CppAsString2(x) CppAsString(x)
343 #define CppConcat(x, y) x##y
365 #define EXPAND(args) args
366 #define VA_ARGS_NARGS(...) \
367 VA_ARGS_NARGS_ EXPAND((__VA_ARGS__, \
369 59,58,57,56,55,54,53,52,51,50, \
370 49,48,47,46,45,44,43,42,41,40, \
371 39,38,37,36,35,34,33,32,31,30, \
372 29,28,27,26,25,24,23,22,21,20, \
373 19,18,17,16,15,14,13,12,11,10, \
374 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
377 #define VA_ARGS_NARGS(...) \
378 VA_ARGS_NARGS_(__VA_ARGS__, \
380 59,58,57,56,55,54,53,52,51,50, \
381 49,48,47,46,45,44,43,42,41,40, \
382 39,38,37,36,35,34,33,32,31,30, \
383 29,28,27,26,25,24,23,22,21,20, \
384 19,18,17,16,15,14,13,12,11,10, \
385 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
388 #define VA_ARGS_NARGS_( \
389 _01,_02,_03,_04,_05,_06,_07,_08,_09,_10, \
390 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
391 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
392 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
393 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
394 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
395 _61,_62,_63, N, ...) \
413 #define FLEXIBLE_ARRAY_MEMBER
440 #define HAVE_PRAGMA_GCC_SYSTEM_HEADER 1
466 #ifdef PG_USE_STDBOOL
475 #define true ((bool) 1)
479 #define false ((bool) 0)
531 #ifdef HAVE_LONG_INT_64
534 typedef long int int64;
535 typedef unsigned long int uint64;
536 #define INT64CONST(x) (x##L)
537 #define UINT64CONST(x) (x##UL)
538 #elif defined(HAVE_LONG_LONG_INT_64)
541 typedef long long int int64;
542 typedef unsigned long long int uint64;
543 #define INT64CONST(x) (x##LL)
544 #define UINT64CONST(x) (x##ULL)
547 #error must have a working 64-bit integer datatype
551 #define INT64_FORMAT "%" INT64_MODIFIER "d"
552 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
562 #if defined(PG_INT128_TYPE)
563 #if defined(pg_attribute_aligned) || ALIGNOF_PG_INT128_TYPE <= MAXIMUM_ALIGNOF
564 #define HAVE_INT128 1
566 typedef PG_INT128_TYPE int128
567 #if defined(pg_attribute_aligned)
572 typedef unsigned PG_INT128_TYPE uint128
573 #if defined(pg_attribute_aligned)
585 #define PG_INT8_MIN (-0x7F-1)
586 #define PG_INT8_MAX (0x7F)
587 #define PG_UINT8_MAX (0xFF)
588 #define PG_INT16_MIN (-0x7FFF-1)
589 #define PG_INT16_MAX (0x7FFF)
590 #define PG_UINT16_MAX (0xFFFF)
591 #define PG_INT32_MIN (-0x7FFFFFFF-1)
592 #define PG_INT32_MAX (0x7FFFFFFF)
593 #define PG_UINT32_MAX (0xFFFFFFFFU)
594 #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
595 #define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
596 #define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
602 #define HAVE_INT64_TIMESTAMP
635 #ifdef USE_FLOAT8_BYVAL
636 #define FLOAT8PASSBYVAL true
638 #define FLOAT8PASSBYVAL false
661 #define InvalidSubTransactionId ((SubTransactionId) 0)
662 #define TopSubTransactionId ((SubTransactionId) 1)
671 #define FirstCommandId ((CommandId) 0)
672 #define InvalidCommandId (~(CommandId)0)
695 #define VARHDRSZ ((int32) sizeof(int32))
749 #define NameStr(name) ((name).data)
760 #define BoolIsValid(boolean) ((boolean) == false || (boolean) == true)
766 #define PointerIsValid(pointer) ((const void*)(pointer) != NULL)
772 #define PointerIsAligned(pointer, type) \
773 (((uintptr_t)(pointer) % (sizeof (type))) == 0)
775 #define OffsetToPointer(base, offset) \
776 ((void *)((char *) base + offset))
778 #define OidIsValid(objectId) ((bool) ((objectId) != InvalidOid))
780 #define RegProcedureIsValid(p) OidIsValid(p)
791 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
807 #define TYPEALIGN(ALIGNVAL,LEN) \
808 (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
810 #define SHORTALIGN(LEN) TYPEALIGN(ALIGNOF_SHORT, (LEN))
811 #define INTALIGN(LEN) TYPEALIGN(ALIGNOF_INT, (LEN))
812 #define LONGALIGN(LEN) TYPEALIGN(ALIGNOF_LONG, (LEN))
813 #define DOUBLEALIGN(LEN) TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
814 #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
816 #define BUFFERALIGN(LEN) TYPEALIGN(ALIGNOF_BUFFER, (LEN))
817 #define CACHELINEALIGN(LEN) TYPEALIGN(PG_CACHE_LINE_SIZE, (LEN))
819 #define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
820 (((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
822 #define SHORTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_SHORT, (LEN))
823 #define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_INT, (LEN))
824 #define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_LONG, (LEN))
825 #define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
826 #define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
827 #define BUFFERALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_BUFFER, (LEN))
835 #define TYPEALIGN64(ALIGNVAL,LEN) \
836 (((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
839 #define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
859 #ifndef USE_ASSERT_CHECKING
861 #define Assert(condition) ((void)true)
862 #define AssertMacro(condition) ((void)true)
864 #elif defined(FRONTEND)
867 #define Assert(p) assert(p)
868 #define AssertMacro(p) ((void) assert(p))
876 #define Assert(condition) \
879 ExceptionalCondition(#condition, __FILE__, __LINE__); \
888 #define AssertMacro(condition) \
889 ((void) ((condition) || \
890 (ExceptionalCondition(#condition, __FILE__, __LINE__), 0)))
897 #define AssertPointerAlignment(ptr, bndr) \
898 Assert(TYPEALIGN(bndr, (uintptr_t)(ptr)) == (uintptr_t)(ptr))
931 #ifdef HAVE__STATIC_ASSERT
932 #define StaticAssertDecl(condition, errmessage) \
933 _Static_assert(condition, errmessage)
934 #define StaticAssertStmt(condition, errmessage) \
935 do { _Static_assert(condition, errmessage); } while(0)
936 #define StaticAssertExpr(condition, errmessage) \
937 ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
939 #define StaticAssertDecl(condition, errmessage) \
940 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
941 #define StaticAssertStmt(condition, errmessage) \
942 ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
943 #define StaticAssertExpr(condition, errmessage) \
944 StaticAssertStmt(condition, errmessage)
947 #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
948 #define StaticAssertDecl(condition, errmessage) \
949 static_assert(condition, errmessage)
950 #define StaticAssertStmt(condition, errmessage) \
951 static_assert(condition, errmessage)
952 #define StaticAssertExpr(condition, errmessage) \
953 ({ static_assert(condition, errmessage); })
955 #define StaticAssertDecl(condition, errmessage) \
956 extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
957 #define StaticAssertStmt(condition, errmessage) \
958 do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
959 #define StaticAssertExpr(condition, errmessage) \
960 ((void) ({ StaticAssertStmt(condition, errmessage); }))
976 #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
977 #define AssertVariableIsOfType(varname, typename) \
978 StaticAssertStmt(__builtin_types_compatible_p(__typeof__(varname), typename), \
979 CppAsString(varname) " does not have type " CppAsString(typename))
980 #define AssertVariableIsOfTypeMacro(varname, typename) \
981 (StaticAssertExpr(__builtin_types_compatible_p(__typeof__(varname), typename), \
982 CppAsString(varname) " does not have type " CppAsString(typename)))
984 #define AssertVariableIsOfType(varname, typename) \
985 StaticAssertStmt(sizeof(varname) == sizeof(typename), \
986 CppAsString(varname) " does not have type " CppAsString(typename))
987 #define AssertVariableIsOfTypeMacro(varname, typename) \
988 (StaticAssertExpr(sizeof(varname) == sizeof(typename), \
989 CppAsString(varname) " does not have type " CppAsString(typename)))
1001 #define Max(x, y) ((x) > (y) ? (x) : (y))
1007 #define Min(x, y) ((x) < (y) ? (x) : (y))
1011 #define LONG_ALIGN_MASK (sizeof(long) - 1)
1023 #define MemSet(start, val, len) \
1027 void *_vstart = (void *) (start); \
1029 Size _len = (len); \
1031 if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
1032 (_len & LONG_ALIGN_MASK) == 0 && \
1034 _len <= MEMSET_LOOP_LIMIT && \
1039 MEMSET_LOOP_LIMIT != 0) \
1041 long *_start = (long *) _vstart; \
1042 long *_stop = (long *) ((char *) _start + _len); \
1043 while (_start < _stop) \
1047 memset(_vstart, _val, _len); \
1056 #define MemSetAligned(start, val, len) \
1059 long *_start = (long *) (start); \
1061 Size _len = (len); \
1063 if ((_len & LONG_ALIGN_MASK) == 0 && \
1065 _len <= MEMSET_LOOP_LIMIT && \
1066 MEMSET_LOOP_LIMIT != 0) \
1068 long *_stop = (long *) ((char *) _start + _len); \
1069 while (_start < _stop) \
1073 memset(_start, _val, _len); \
1088 #define FLOAT4_FITS_IN_INT16(num) \
1089 ((num) >= (float4) PG_INT16_MIN && (num) < -((float4) PG_INT16_MIN))
1090 #define FLOAT4_FITS_IN_INT32(num) \
1091 ((num) >= (float4) PG_INT32_MIN && (num) < -((float4) PG_INT32_MIN))
1092 #define FLOAT4_FITS_IN_INT64(num) \
1093 ((num) >= (float4) PG_INT64_MIN && (num) < -((float4) PG_INT64_MIN))
1094 #define FLOAT8_FITS_IN_INT16(num) \
1095 ((num) >= (float8) PG_INT16_MIN && (num) < -((float8) PG_INT16_MIN))
1096 #define FLOAT8_FITS_IN_INT32(num) \
1097 ((num) >= (float8) PG_INT32_MIN && (num) < -((float8) PG_INT32_MIN))
1098 #define FLOAT8_FITS_IN_INT64(num) \
1099 ((num) >= (float8) PG_INT64_MIN && (num) < -((float8) PG_INT64_MIN))
1112 #define INVERT_COMPARE_RESULT(var) \
1113 ((var) = ((var) < 0) ? 1 : -(var))
1140 #ifdef pg_attribute_aligned
1151 #ifdef pg_attribute_aligned
1160 #define HIGHBIT (0x80)
1161 #define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT)
1169 #define SQL_STR_DOUBLE(ch, escape_backslash) \
1170 ((ch) == '\'' || ((ch) == '\\' && (escape_backslash)))
1172 #define ESCAPE_STRING_SYNTAX 'E'
1175 #define STATUS_OK (0)
1176 #define STATUS_ERROR (-1)
1177 #define STATUS_EOF (-2)
1185 #define gettext(x) (x)
1186 #define dgettext(d,x) (x)
1187 #define ngettext(s,p,n) ((n) == 1 ? (s) : (p))
1188 #define dngettext(d,s,p,n) ((n) == 1 ? (s) : (p))
1191 #define _(x) gettext(x)
1202 #define gettext_noop(x) (x)
1217 #ifdef SO_MAJOR_VERSION
1218 #define PG_TEXTDOMAIN(domain) (domain CppAsString2(SO_MAJOR_VERSION) "-" PG_MAJORVERSION)
1220 #define PG_TEXTDOMAIN(domain) (domain "-" PG_MAJORVERSION)
1238 #if defined(__cplusplus)
1239 #define unconstify(underlying_type, expr) const_cast<underlying_type>(expr)
1240 #define unvolatize(underlying_type, expr) const_cast<underlying_type>(expr)
1241 #elif defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P)
1242 #define unconstify(underlying_type, expr) \
1243 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
1245 (underlying_type) (expr))
1246 #define unvolatize(underlying_type, expr) \
1247 (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1249 (underlying_type) (expr))
1251 #define unconstify(underlying_type, expr) \
1252 ((underlying_type) (expr))
1253 #define unvolatize(underlying_type, expr) \
1254 ((underlying_type) (expr))
1273 #if defined(WIN32) || defined(__CYGWIN__)
1274 #define PG_BINARY O_BINARY
1275 #define PG_BINARY_A "ab"
1276 #define PG_BINARY_R "rb"
1277 #define PG_BINARY_W "wb"
1280 #define PG_BINARY_A "a"
1281 #define PG_BINARY_R "r"
1282 #define PG_BINARY_W "w"
1290 #if !HAVE_DECL_FDATASYNC
1299 #ifdef HAVE_LONG_INT_64
1300 #define strtoi64(str, endptr, base) ((int64) strtol(str, endptr, base))
1301 #define strtou64(str, endptr, base) ((uint64) strtoul(str, endptr, base))
1303 #define strtoi64(str, endptr, base) ((int64) strtoll(str, endptr, base))
1304 #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
1310 #ifdef HAVE_LONG_INT_64
1311 #define i64abs(i) labs(i)
1313 #define i64abs(i) llabs(i)
1334 #ifdef HAVE_VISIBILITY_ATTRIBUTE
1335 #define PGDLLEXPORT __attribute__((visibility("default")))
1351 #define SIGNAL_ARGS int postgres_signal_arg
1362 typedef intptr_t sigjmp_buf[5];
1363 #define sigsetjmp(x,y) __builtin_setjmp(x)
1364 #define siglongjmp __builtin_longjmp
1366 #define sigjmp_buf jmp_buf
1367 #define sigsetjmp(x,y) setjmp(x)
1368 #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)
struct pg_attribute_aligned(8) pg_atomic_uint64
char vl_dat[FLEXIBLE_ARRAY_MEMBER]